2013-03-15 15:14:09 +00:00
|
|
|
/**
|
2013-07-12 07:53:53 +00:00
|
|
|
* \addtogroup mbxxx-platform
|
2013-03-15 15:14:09 +00:00
|
|
|
*
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2010-11-10 11:20:44 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2009, Swedish Institute of Computer Science
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* Coffee architecture-dependent functionality for the STM32W108-based mb851
|
|
|
|
* platform.
|
|
|
|
* \author
|
|
|
|
* Salvatore Pitrulli <salvopitru@users.sourceforge.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cfs-coffee-arch.h"
|
|
|
|
|
|
|
|
#define DEBUG 1
|
|
|
|
#if DEBUG
|
|
|
|
#include <stdio.h>
|
|
|
|
#define PRINTF(...) printf(__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define PRINTF(...)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define TESTCOFFEE 1
|
|
|
|
#define DEBUG_CFS 1
|
|
|
|
#if TESTCOFFEE
|
|
|
|
#if DEBUG_CFS
|
|
|
|
#include <stdio.h>
|
|
|
|
#define PRINTF_CFS(...) printf(__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define PRINTF_CFS(...)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "cfs/cfs.h"
|
|
|
|
#include "cfs/cfs-coffee.h"
|
|
|
|
#include "lib/crc16.h"
|
|
|
|
#include "lib/random.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#define FAIL(x) PRINTF("FAILED\n");error = (x); goto end;
|
|
|
|
|
|
|
|
#define FILE_SIZE 512
|
|
|
|
|
2013-03-15 15:14:09 +00:00
|
|
|
/*--------------------------------------------------------------------------*/
|
2010-11-10 11:20:44 +00:00
|
|
|
int
|
|
|
|
coffee_file_test(void)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
int wfd, rfd, afd;
|
|
|
|
unsigned char buf[256], buf2[11];
|
|
|
|
int r, i, j, total_read;
|
|
|
|
unsigned offset;
|
|
|
|
|
|
|
|
cfs_remove("T1");
|
|
|
|
cfs_remove("T2");
|
|
|
|
cfs_remove("T3");
|
|
|
|
cfs_remove("T4");
|
|
|
|
cfs_remove("T5");
|
|
|
|
wfd = rfd = afd = -1;
|
|
|
|
|
|
|
|
for(r = 0; r < sizeof(buf); r++) {
|
|
|
|
buf[r] = r;
|
|
|
|
}
|
2013-03-15 15:14:09 +00:00
|
|
|
|
2010-11-10 11:20:44 +00:00
|
|
|
PRINTF("TEST 1\n");
|
|
|
|
|
|
|
|
/* Test 1: Open for writing. */
|
|
|
|
wfd = cfs_open("T1", CFS_WRITE);
|
|
|
|
if(wfd < 0) {
|
|
|
|
FAIL(-1);
|
|
|
|
}
|
2013-03-15 15:14:09 +00:00
|
|
|
|
2010-11-10 11:20:44 +00:00
|
|
|
PRINTF("PASSED\n");
|
|
|
|
PRINTF("TEST ");
|
|
|
|
PRINTF("2\n");
|
|
|
|
|
|
|
|
/* Test 2: Write buffer. */
|
|
|
|
r = cfs_write(wfd, buf, sizeof(buf));
|
|
|
|
if(r < 0) {
|
|
|
|
FAIL(-2);
|
|
|
|
} else if(r < sizeof(buf)) {
|
|
|
|
FAIL(-3);
|
|
|
|
}
|
2013-03-15 15:14:09 +00:00
|
|
|
|
2010-11-10 11:20:44 +00:00
|
|
|
PRINTF("PASSED\n");
|
|
|
|
PRINTF("TEST ");
|
|
|
|
PRINTF("3\n");
|
|
|
|
|
|
|
|
/* Test 3: Deny reading. */
|
|
|
|
r = cfs_read(wfd, buf, sizeof(buf));
|
|
|
|
if(r >= 0) {
|
|
|
|
FAIL(-4);
|
|
|
|
}
|
2013-03-15 15:14:09 +00:00
|
|
|
|
2010-11-10 11:20:44 +00:00
|
|
|
PRINTF("PASSED\n");
|
|
|
|
PRINTF("TEST ");
|
|
|
|
PRINTF("4\n");
|
|
|
|
|
|
|
|
/* Test 4: Open for reading. */
|
|
|
|
rfd = cfs_open("T1", CFS_READ);
|
|
|
|
if(rfd < 0) {
|
|
|
|
FAIL(-5);
|
|
|
|
}
|
2013-03-15 15:14:09 +00:00
|
|
|
|
2010-11-10 11:20:44 +00:00
|
|
|
PRINTF("PASSED\n");
|
|
|
|
PRINTF("TEST ");
|
|
|
|
PRINTF("5\n");
|
|
|
|
|
|
|
|
/* Test 5: Write to read-only file. */
|
|
|
|
r = cfs_write(rfd, buf, sizeof(buf));
|
|
|
|
if(r >= 0) {
|
|
|
|
FAIL(-6);
|
|
|
|
}
|
|
|
|
PRINTF("PASSED\n");
|
|
|
|
PRINTF("TEST ");
|
|
|
|
PRINTF("7\n");
|
|
|
|
|
|
|
|
/* Test 7: Read the buffer written in Test 2. */
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
r = cfs_read(rfd, buf, sizeof(buf));
|
|
|
|
if(r < 0) {
|
|
|
|
FAIL(-8);
|
|
|
|
} else if(r < sizeof(buf)) {
|
|
|
|
PRINTF_CFS("r=%d\n", r);
|
|
|
|
FAIL(-9);
|
|
|
|
}
|
2013-03-15 15:14:09 +00:00
|
|
|
|
2010-11-10 11:20:44 +00:00
|
|
|
PRINTF("PASSED\n");
|
|
|
|
PRINTF("TEST ");
|
|
|
|
PRINTF("8\n");
|
|
|
|
|
|
|
|
/* Test 8: Verify that the buffer is correct. */
|
|
|
|
for(r = 0; r < sizeof(buf); r++) {
|
|
|
|
if(buf[r] != r) {
|
|
|
|
PRINTF_CFS("r=%d. buf[r]=%d\n", r, buf[r]);
|
|
|
|
FAIL(-10);
|
|
|
|
}
|
|
|
|
}
|
2013-03-15 15:14:09 +00:00
|
|
|
|
2010-11-10 11:20:44 +00:00
|
|
|
PRINTF("PASSED\n");
|
|
|
|
PRINTF("TEST ");
|
|
|
|
PRINTF("9\n");
|
|
|
|
|
|
|
|
/* Test 9: Seek to beginning. */
|
|
|
|
if(cfs_seek(wfd, 0, CFS_SEEK_SET) != 0) {
|
|
|
|
FAIL(-11);
|
|
|
|
}
|
2013-03-15 15:14:09 +00:00
|
|
|
|
2010-11-10 11:20:44 +00:00
|
|
|
PRINTF("PASSED\n");
|
|
|
|
PRINTF("TEST ");
|
|
|
|
PRINTF("10\n");
|
|
|
|
|
|
|
|
/* Test 10: Write to the log. */
|
|
|
|
r = cfs_write(wfd, buf, sizeof(buf));
|
|
|
|
if(r < 0) {
|
|
|
|
FAIL(-12);
|
|
|
|
} else if(r < sizeof(buf)) {
|
|
|
|
FAIL(-13);
|
|
|
|
}
|
2013-03-15 15:14:09 +00:00
|
|
|
|
2010-11-10 11:20:44 +00:00
|
|
|
PRINTF("PASSED\n");
|
|
|
|
PRINTF("TEST ");
|
|
|
|
PRINTF("11\n");
|
|
|
|
|
|
|
|
/* Test 11: Read the data from the log. */
|
|
|
|
cfs_seek(rfd, 0, CFS_SEEK_SET);
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
r = cfs_read(rfd, buf, sizeof(buf));
|
|
|
|
if(r < 0) {
|
|
|
|
FAIL(-14);
|
|
|
|
} else if(r < sizeof(buf)) {
|
|
|
|
FAIL(-15);
|
|
|
|
}
|
2013-03-15 15:14:09 +00:00
|
|
|
|
2010-11-10 11:20:44 +00:00
|
|
|
PRINTF("PASSED\n");
|
|
|
|
PRINTF("TEST ");
|
|
|
|
PRINTF("12\n");
|
|
|
|
|
|
|
|
/* Test 12: Verify that the data is correct. */
|
|
|
|
for(r = 0; r < sizeof(buf); r++) {
|
|
|
|
if(buf[r] != r) {
|
|
|
|
FAIL(-16);
|
|
|
|
}
|
|
|
|
}
|
2013-03-15 15:14:09 +00:00
|
|
|
|
2010-11-10 11:20:44 +00:00
|
|
|
PRINTF("PASSED\n");
|
|
|
|
PRINTF("TEST ");
|
|
|
|
PRINTF("13\n");
|
|
|
|
|
|
|
|
/* Test 13: Write a reversed buffer to the file. */
|
|
|
|
for(r = 0; r < sizeof(buf); r++) {
|
|
|
|
buf[r] = sizeof(buf) - r - 1;
|
|
|
|
}
|
|
|
|
if(cfs_seek(wfd, 0, CFS_SEEK_SET) != 0) {
|
|
|
|
FAIL(-17);
|
|
|
|
}
|
|
|
|
r = cfs_write(wfd, buf, sizeof(buf));
|
|
|
|
if(r < 0) {
|
|
|
|
FAIL(-18);
|
|
|
|
} else if(r < sizeof(buf)) {
|
|
|
|
FAIL(-19);
|
|
|
|
}
|
|
|
|
if(cfs_seek(rfd, 0, CFS_SEEK_SET) != 0) {
|
|
|
|
FAIL(-20);
|
|
|
|
}
|
2013-03-15 15:14:09 +00:00
|
|
|
|
2010-11-10 11:20:44 +00:00
|
|
|
PRINTF("PASSED\n");
|
|
|
|
PRINTF("TEST ");
|
|
|
|
PRINTF("14\n");
|
|
|
|
|
|
|
|
/* Test 14: Read the reversed buffer. */
|
|
|
|
cfs_seek(rfd, 0, CFS_SEEK_SET);
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
r = cfs_read(rfd, buf, sizeof(buf));
|
|
|
|
if(r < 0) {
|
|
|
|
FAIL(-21);
|
|
|
|
} else if(r < sizeof(buf)) {
|
|
|
|
PRINTF_CFS("r = %d\n", r);
|
|
|
|
FAIL(-22);
|
|
|
|
}
|
2013-03-15 15:14:09 +00:00
|
|
|
|
2010-11-10 11:20:44 +00:00
|
|
|
PRINTF("PASSED\n");
|
|
|
|
PRINTF("TEST ");
|
|
|
|
PRINTF("15\n");
|
|
|
|
|
|
|
|
/* Test 15: Verify that the data is correct. */
|
|
|
|
for(r = 0; r < sizeof(buf); r++) {
|
|
|
|
if(buf[r] != sizeof(buf) - r - 1) {
|
|
|
|
FAIL(-23);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cfs_close(rfd);
|
|
|
|
cfs_close(wfd);
|
|
|
|
|
|
|
|
if(cfs_coffee_reserve("T2", FILE_SIZE) < 0) {
|
|
|
|
FAIL(-24);
|
|
|
|
}
|
2013-03-15 15:14:09 +00:00
|
|
|
|
2010-11-10 11:20:44 +00:00
|
|
|
PRINTF("PASSED\n");
|
|
|
|
PRINTF("TEST ");
|
|
|
|
PRINTF("16\n");
|
|
|
|
|
|
|
|
/* Test 16: Test multiple writes at random offset. */
|
|
|
|
for(r = 0; r < 100; r++) {
|
|
|
|
wfd = cfs_open("T2", CFS_WRITE | CFS_READ);
|
|
|
|
if(wfd < 0) {
|
|
|
|
FAIL(-25);
|
|
|
|
}
|
|
|
|
offset = random_rand() % FILE_SIZE;
|
|
|
|
for(r = 0; r < sizeof(buf); r++) {
|
|
|
|
buf[r] = r;
|
|
|
|
}
|
|
|
|
if(cfs_seek(wfd, offset, CFS_SEEK_SET) != offset) {
|
|
|
|
FAIL(-26);
|
|
|
|
}
|
|
|
|
if(cfs_write(wfd, buf, sizeof(buf)) != sizeof(buf)) {
|
|
|
|
FAIL(-27);
|
|
|
|
}
|
|
|
|
if(cfs_seek(wfd, offset, CFS_SEEK_SET) != offset) {
|
|
|
|
FAIL(-28);
|
|
|
|
}
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
if(cfs_read(wfd, buf, sizeof(buf)) != sizeof(buf)) {
|
|
|
|
FAIL(-29);
|
|
|
|
}
|
|
|
|
for(i = 0; i < sizeof(buf); i++) {
|
|
|
|
if(buf[i] != i) {
|
|
|
|
PRINTF_CFS("buf[%d] != %d\n", i, buf[i]);
|
|
|
|
FAIL(-30);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PRINTF("PASSED\n");
|
|
|
|
PRINTF("TEST ");
|
|
|
|
PRINTF("17\n");
|
2013-03-15 15:14:09 +00:00
|
|
|
|
2010-11-10 11:20:44 +00:00
|
|
|
/* Test 17: Append data to the same file many times. */
|
|
|
|
#define APPEND_BYTES 3000
|
|
|
|
#define BULK_SIZE 10
|
2013-03-15 15:14:09 +00:00
|
|
|
for(i = 0; i < APPEND_BYTES; i += BULK_SIZE) {
|
2010-11-10 11:20:44 +00:00
|
|
|
afd = cfs_open("T3", CFS_WRITE | CFS_APPEND);
|
2013-03-15 15:14:09 +00:00
|
|
|
if(afd < 0) {
|
2010-11-10 11:20:44 +00:00
|
|
|
FAIL(-31);
|
|
|
|
}
|
2013-03-15 15:14:09 +00:00
|
|
|
for(j = 0; j < BULK_SIZE; j++) {
|
2010-11-10 11:20:44 +00:00
|
|
|
buf[j] = 1 + ((i + j) & 0x7f);
|
|
|
|
}
|
2013-03-15 15:14:09 +00:00
|
|
|
if((r = cfs_write(afd, buf, BULK_SIZE)) != BULK_SIZE) {
|
2010-11-10 11:20:44 +00:00
|
|
|
PRINTF_CFS("Count:%d, r=%d\n", i, r);
|
|
|
|
FAIL(-32);
|
|
|
|
}
|
|
|
|
cfs_close(afd);
|
|
|
|
}
|
2013-03-15 15:14:09 +00:00
|
|
|
|
2010-11-10 11:20:44 +00:00
|
|
|
PRINTF("PASSED\n");
|
|
|
|
PRINTF("TEST ");
|
|
|
|
PRINTF("18\n");
|
|
|
|
|
2013-03-15 15:14:09 +00:00
|
|
|
/* Test 18: Read back the data written in Test 17 and verify. */
|
2010-11-10 11:20:44 +00:00
|
|
|
afd = cfs_open("T3", CFS_READ);
|
|
|
|
if(afd < 0) {
|
|
|
|
FAIL(-33);
|
|
|
|
}
|
|
|
|
total_read = 0;
|
|
|
|
while((r = cfs_read(afd, buf2, sizeof(buf2))) > 0) {
|
|
|
|
for(j = 0; j < r; j++) {
|
|
|
|
if(buf2[j] != 1 + ((total_read + j) & 0x7f)) {
|
2013-03-15 15:14:09 +00:00
|
|
|
FAIL(-34);
|
2010-11-10 11:20:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
total_read += r;
|
|
|
|
}
|
|
|
|
if(r < 0) {
|
2013-03-15 15:14:09 +00:00
|
|
|
PRINTF_CFS("FAIL:-35 r=%d\n", r);
|
2010-11-10 11:20:44 +00:00
|
|
|
FAIL(-35);
|
|
|
|
}
|
|
|
|
if(total_read != APPEND_BYTES) {
|
2013-03-15 15:14:09 +00:00
|
|
|
PRINTF_CFS("FAIL:-35 total_read=%d\n", total_read);
|
2010-11-10 11:20:44 +00:00
|
|
|
FAIL(-35);
|
|
|
|
}
|
|
|
|
cfs_close(afd);
|
2013-03-15 15:14:09 +00:00
|
|
|
|
2010-11-10 11:20:44 +00:00
|
|
|
PRINTF("PASSED\n");
|
|
|
|
PRINTF("TEST ");
|
|
|
|
PRINTF("19\n");
|
|
|
|
|
2013-03-15 15:14:09 +00:00
|
|
|
/* T4 */
|
|
|
|
/*
|
|
|
|
* file T4 and T5 writing forces to use garbage collector in greedy mode
|
|
|
|
* this test is designed for 10kb of file system
|
|
|
|
*/
|
2010-11-10 11:20:44 +00:00
|
|
|
#define APPEND_BYTES_1 2000
|
|
|
|
#define BULK_SIZE_1 10
|
2013-03-15 15:14:09 +00:00
|
|
|
for(i = 0; i < APPEND_BYTES_1; i += BULK_SIZE_1) {
|
2010-11-10 11:20:44 +00:00
|
|
|
afd = cfs_open("T4", CFS_WRITE | CFS_APPEND);
|
2013-03-15 15:14:09 +00:00
|
|
|
if(afd < 0) {
|
|
|
|
FAIL(-36);
|
2010-11-10 11:20:44 +00:00
|
|
|
}
|
2013-03-15 15:14:09 +00:00
|
|
|
for(j = 0; j < BULK_SIZE_1; j++) {
|
2010-11-10 11:20:44 +00:00
|
|
|
buf[j] = 1 + ((i + j) & 0x7f);
|
|
|
|
}
|
|
|
|
|
2013-03-15 15:14:09 +00:00
|
|
|
if((r = cfs_write(afd, buf, BULK_SIZE_1)) != BULK_SIZE_1) {
|
|
|
|
PRINTF_CFS("Count:%d, r=%d\n", i, r);
|
|
|
|
FAIL(-37);
|
|
|
|
}
|
|
|
|
cfs_close(afd);
|
|
|
|
}
|
2010-11-10 11:20:44 +00:00
|
|
|
|
|
|
|
afd = cfs_open("T4", CFS_READ);
|
|
|
|
if(afd < 0) {
|
|
|
|
FAIL(-38);
|
|
|
|
}
|
2013-03-15 15:14:09 +00:00
|
|
|
|
2010-11-10 11:20:44 +00:00
|
|
|
total_read = 0;
|
|
|
|
while((r = cfs_read(afd, buf2, sizeof(buf2))) > 0) {
|
|
|
|
for(j = 0; j < r; j++) {
|
|
|
|
if(buf2[j] != 1 + ((total_read + j) & 0x7f)) {
|
2013-03-15 15:14:09 +00:00
|
|
|
PRINTF_CFS("FAIL:-39, total_read=%d r=%d\n", total_read, r);
|
|
|
|
FAIL(-39);
|
2010-11-10 11:20:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
total_read += r;
|
|
|
|
}
|
|
|
|
if(r < 0) {
|
2013-03-15 15:14:09 +00:00
|
|
|
PRINTF_CFS("FAIL:-40 r=%d\n", r);
|
2010-11-10 11:20:44 +00:00
|
|
|
FAIL(-40);
|
|
|
|
}
|
|
|
|
if(total_read != APPEND_BYTES_1) {
|
2013-03-15 15:14:09 +00:00
|
|
|
PRINTF_CFS("FAIL:-41 total_read=%d\n", total_read);
|
2010-11-10 11:20:44 +00:00
|
|
|
FAIL(-41);
|
|
|
|
}
|
|
|
|
cfs_close(afd);
|
2013-03-15 15:14:09 +00:00
|
|
|
|
|
|
|
/* T5 */
|
2010-11-10 11:20:44 +00:00
|
|
|
PRINTF("PASSED\n");
|
|
|
|
PRINTF("TEST ");
|
|
|
|
PRINTF("20\n");
|
|
|
|
#define APPEND_BYTES_2 1000
|
|
|
|
#define BULK_SIZE_2 10
|
2013-03-15 15:14:09 +00:00
|
|
|
for(i = 0; i < APPEND_BYTES_2; i += BULK_SIZE_2) {
|
|
|
|
afd = cfs_open("T5", CFS_WRITE | CFS_APPEND);
|
2010-11-10 11:20:44 +00:00
|
|
|
if(afd < 0) {
|
2013-03-15 15:14:09 +00:00
|
|
|
FAIL(-42);
|
2010-11-10 11:20:44 +00:00
|
|
|
}
|
2013-03-15 15:14:09 +00:00
|
|
|
for(j = 0; j < BULK_SIZE_2; j++) {
|
|
|
|
buf[j] = 1 + ((i + j) & 0x7f);
|
2010-11-10 11:20:44 +00:00
|
|
|
}
|
2013-03-15 15:14:09 +00:00
|
|
|
|
|
|
|
if((r = cfs_write(afd, buf, BULK_SIZE_2)) != BULK_SIZE_2) {
|
|
|
|
PRINTF_CFS("Count:%d, r=%d\n", i, r);
|
|
|
|
FAIL(-43);
|
2010-11-10 11:20:44 +00:00
|
|
|
}
|
2013-03-15 15:14:09 +00:00
|
|
|
|
2010-11-10 11:20:44 +00:00
|
|
|
cfs_close(afd);
|
2013-03-15 15:14:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
afd = cfs_open("T5", CFS_READ);
|
|
|
|
if(afd < 0) {
|
|
|
|
FAIL(-44);
|
|
|
|
}
|
|
|
|
total_read = 0;
|
|
|
|
while((r = cfs_read(afd, buf2, sizeof(buf2))) > 0) {
|
|
|
|
for(j = 0; j < r; j++) {
|
|
|
|
if(buf2[j] != 1 + ((total_read + j) & 0x7f)) {
|
|
|
|
PRINTF_CFS("FAIL:-45, total_read=%d r=%d\n", total_read, r);
|
|
|
|
FAIL(-45);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
total_read += r;
|
|
|
|
}
|
|
|
|
if(r < 0) {
|
|
|
|
PRINTF_CFS("FAIL:-46 r=%d\n", r);
|
|
|
|
FAIL(-46);
|
|
|
|
}
|
|
|
|
if(total_read != APPEND_BYTES_2) {
|
|
|
|
PRINTF_CFS("FAIL:-47 total_read=%d\n", total_read);
|
|
|
|
FAIL(-47);
|
|
|
|
}
|
|
|
|
cfs_close(afd);
|
|
|
|
|
|
|
|
PRINTF("PASSED\n");
|
2010-11-10 11:20:44 +00:00
|
|
|
|
|
|
|
error = 0;
|
|
|
|
end:
|
2013-03-15 15:14:09 +00:00
|
|
|
cfs_close(wfd);
|
|
|
|
cfs_close(rfd);
|
|
|
|
cfs_close(afd);
|
2010-11-10 11:20:44 +00:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
#endif /* TESTCOFFEE */
|
2013-03-15 15:14:09 +00:00
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
stm32w_flash_read(uint32_t address, void *data, uint32_t length)
|
2010-11-10 11:20:44 +00:00
|
|
|
{
|
2013-03-15 15:14:09 +00:00
|
|
|
uint8_t *pdata = (uint8_t *) address;
|
|
|
|
|
2010-11-10 11:20:44 +00:00
|
|
|
ENERGEST_ON(ENERGEST_TYPE_FLASH_READ);
|
|
|
|
memcpy(data, pdata, length);
|
|
|
|
ENERGEST_OFF(ENERGEST_TYPE_FLASH_READ);
|
|
|
|
}
|
2013-03-15 15:14:09 +00:00
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
stm32w_flash_erase(uint8_t sector)
|
2010-11-10 11:20:44 +00:00
|
|
|
{
|
2013-03-15 15:14:09 +00:00
|
|
|
/* halInternalFlashErase(MFB_PAGE_ERASE, COFFEE_START +
|
|
|
|
(sector) * COFFEE_SECTOR_SIZE); */
|
|
|
|
uint16_t data = 0;
|
|
|
|
uint32_t addr = COFFEE_START + (sector) * COFFEE_SECTOR_SIZE;
|
|
|
|
uint32_t end = addr + COFFEE_SECTOR_SIZE;
|
|
|
|
|
2010-11-10 11:20:44 +00:00
|
|
|
/* This prevents from accidental write to CIB. */
|
2013-03-15 15:14:09 +00:00
|
|
|
if(!(addr >= MFB_BOTTOM && end <= MFB_TOP + 1)) {
|
2010-11-10 11:20:44 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-03-15 15:14:09 +00:00
|
|
|
|
|
|
|
for(; addr < end; addr += 2) {
|
2010-11-10 11:20:44 +00:00
|
|
|
halInternalFlashWrite(addr, &data, 1);
|
|
|
|
}
|
|
|
|
}
|
2013-03-15 15:14:09 +00:00
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
/*
|
|
|
|
* Allocates a buffer of FLASH_PAGE_SIZE bytes statically (rather than on
|
|
|
|
* the stack).
|
|
|
|
*/
|
2010-11-10 11:20:44 +00:00
|
|
|
#ifndef STATIC_FLASH_BUFFER
|
2013-03-15 15:14:09 +00:00
|
|
|
#define STATIC_FLASH_BUFFER 1
|
2010-11-10 11:20:44 +00:00
|
|
|
#endif
|
|
|
|
|
2013-03-15 15:14:09 +00:00
|
|
|
void
|
|
|
|
stm32w_flash_write(uint32_t address, const void *data, uint32_t length)
|
2010-11-10 11:20:44 +00:00
|
|
|
{
|
2013-03-15 15:14:09 +00:00
|
|
|
const uint32_t end = address + length;
|
|
|
|
uint32_t i;
|
|
|
|
uint32_t next_page, curr_page;
|
|
|
|
uint16_t offset;
|
2010-11-10 11:20:44 +00:00
|
|
|
#if STATIC_FLASH_BUFFER
|
2013-03-15 15:14:09 +00:00
|
|
|
static uint8_t buf[FLASH_PAGE_SIZE];
|
2010-11-10 11:20:44 +00:00
|
|
|
#else
|
2013-03-15 15:14:09 +00:00
|
|
|
uint8_t buf[FLASH_PAGE_SIZE];
|
2010-11-10 11:20:44 +00:00
|
|
|
#endif
|
2013-03-15 15:14:09 +00:00
|
|
|
|
2010-11-10 11:20:44 +00:00
|
|
|
for(i = address; i < end;) {
|
2013-03-15 15:14:09 +00:00
|
|
|
next_page = (i | (FLASH_PAGE_SIZE - 1)) + 1;
|
|
|
|
curr_page = i & ~(FLASH_PAGE_SIZE - 1);
|
|
|
|
offset = i - curr_page;
|
2010-11-10 11:20:44 +00:00
|
|
|
if(next_page > end) {
|
|
|
|
next_page = end;
|
|
|
|
}
|
|
|
|
|
2013-03-15 15:14:09 +00:00
|
|
|
/* Read a page from flash and put it into a mirror buffer. */
|
2010-11-10 11:20:44 +00:00
|
|
|
stm32w_flash_read(curr_page, buf, FLASH_PAGE_SIZE);
|
2013-03-15 15:14:09 +00:00
|
|
|
/* Update flash mirror data with new data. */
|
2010-11-10 11:20:44 +00:00
|
|
|
memcpy(buf + offset, data, next_page - i);
|
2013-03-15 15:14:09 +00:00
|
|
|
/* Erase flash page. */
|
2010-11-10 11:20:44 +00:00
|
|
|
ENERGEST_ON(ENERGEST_TYPE_FLASH_WRITE);
|
|
|
|
halInternalFlashErase(MFB_PAGE_ERASE, i);
|
2013-03-15 15:14:09 +00:00
|
|
|
/* Write modified data form mirror buffer into the flash. */
|
|
|
|
halInternalFlashWrite(curr_page, (uint16_t *) buf, FLASH_PAGE_SIZE / 2);
|
2010-11-10 11:20:44 +00:00
|
|
|
ENERGEST_OFF(ENERGEST_TYPE_FLASH_WRITE);
|
2013-03-15 15:14:09 +00:00
|
|
|
|
|
|
|
data = (uint8_t *) data + next_page - i;
|
2010-11-10 11:20:44 +00:00
|
|
|
i = next_page;
|
|
|
|
}
|
|
|
|
}
|
2013-03-15 15:14:09 +00:00
|
|
|
/** @} */
|