50 #define PRINTF(...) printf(__VA_ARGS__)
60 #define PRINTF_CFS(...) printf(__VA_ARGS__)
62 #define PRINTF_CFS(...)
68 #include "lib/random.h"
71 #define FAIL(x) PRINTF("FAILED\n");error = (x); goto end;
77 coffee_file_test(
void)
81 unsigned char buf[256], buf2[11];
82 int r, i, j, total_read;
92 for(r = 0; r <
sizeof(buf); r++) {
109 r = cfs_write(wfd, buf,
sizeof(buf));
112 }
else if(r <
sizeof(buf)) {
121 r = cfs_read(wfd, buf,
sizeof(buf));
141 r = cfs_write(rfd, buf,
sizeof(buf));
150 memset(buf, 0,
sizeof(buf));
151 r = cfs_read(rfd, buf,
sizeof(buf));
154 }
else if(r <
sizeof(buf)) {
155 PRINTF_CFS(
"r=%d\n", r);
164 for(r = 0; r <
sizeof(buf); r++) {
166 PRINTF_CFS(
"r=%d. buf[r]=%d\n", r, buf[r]);
185 r = cfs_write(wfd, buf,
sizeof(buf));
188 }
else if(r <
sizeof(buf)) {
198 memset(buf, 0,
sizeof(buf));
199 r = cfs_read(rfd, buf,
sizeof(buf));
202 }
else if(r <
sizeof(buf)) {
211 for(r = 0; r <
sizeof(buf); r++) {
222 for(r = 0; r <
sizeof(buf); r++) {
223 buf[r] =
sizeof(buf) - r - 1;
228 r = cfs_write(wfd, buf,
sizeof(buf));
231 }
else if(r <
sizeof(buf)) {
244 memset(buf, 0,
sizeof(buf));
245 r = cfs_read(rfd, buf,
sizeof(buf));
248 }
else if(r <
sizeof(buf)) {
249 PRINTF_CFS(
"r = %d\n", r);
258 for(r = 0; r <
sizeof(buf); r++) {
259 if(buf[r] !=
sizeof(buf) - r - 1) {
276 for(r = 0; r < 100; r++) {
282 for(r = 0; r <
sizeof(buf); r++) {
288 if(cfs_write(wfd, buf,
sizeof(buf)) !=
sizeof(buf)) {
294 memset(buf, 0,
sizeof(buf));
295 if(cfs_read(wfd, buf,
sizeof(buf)) !=
sizeof(buf)) {
298 for(i = 0; i <
sizeof(buf); i++) {
300 PRINTF_CFS(
"buf[%d] != %d\n", i, buf[i]);
310 #define APPEND_BYTES 3000
312 for(i = 0; i < APPEND_BYTES; i += BULK_SIZE) {
317 for(j = 0; j < BULK_SIZE; j++) {
318 buf[j] = 1 + ((i + j) & 0x7f);
320 if((r = cfs_write(afd, buf, BULK_SIZE)) != BULK_SIZE) {
321 PRINTF_CFS(
"Count:%d, r=%d\n", i, r);
337 while((r = cfs_read(afd, buf2,
sizeof(buf2))) > 0) {
338 for(j = 0; j < r; j++) {
339 if(buf2[j] != 1 + ((total_read + j) & 0x7f)) {
346 PRINTF_CFS(
"FAIL:-35 r=%d\n", r);
349 if(total_read != APPEND_BYTES) {
350 PRINTF_CFS(
"FAIL:-35 total_read=%d\n", total_read);
364 #define APPEND_BYTES_1 2000
365 #define BULK_SIZE_1 10
366 for(i = 0; i < APPEND_BYTES_1; i += BULK_SIZE_1) {
371 for(j = 0; j < BULK_SIZE_1; j++) {
372 buf[j] = 1 + ((i + j) & 0x7f);
375 if((r = cfs_write(afd, buf, BULK_SIZE_1)) != BULK_SIZE_1) {
376 PRINTF_CFS(
"Count:%d, r=%d\n", i, r);
388 while((r = cfs_read(afd, buf2,
sizeof(buf2))) > 0) {
389 for(j = 0; j < r; j++) {
390 if(buf2[j] != 1 + ((total_read + j) & 0x7f)) {
391 PRINTF_CFS(
"FAIL:-39, total_read=%d r=%d\n", total_read, r);
398 PRINTF_CFS(
"FAIL:-40 r=%d\n", r);
401 if(total_read != APPEND_BYTES_1) {
402 PRINTF_CFS(
"FAIL:-41 total_read=%d\n", total_read);
411 #define APPEND_BYTES_2 1000
412 #define BULK_SIZE_2 10
413 for(i = 0; i < APPEND_BYTES_2; i += BULK_SIZE_2) {
418 for(j = 0; j < BULK_SIZE_2; j++) {
419 buf[j] = 1 + ((i + j) & 0x7f);
422 if((r = cfs_write(afd, buf, BULK_SIZE_2)) != BULK_SIZE_2) {
423 PRINTF_CFS(
"Count:%d, r=%d\n", i, r);
435 while((r = cfs_read(afd, buf2,
sizeof(buf2))) > 0) {
436 for(j = 0; j < r; j++) {
437 if(buf2[j] != 1 + ((total_read + j) & 0x7f)) {
438 PRINTF_CFS(
"FAIL:-45, total_read=%d r=%d\n", total_read, r);
445 PRINTF_CFS(
"FAIL:-46 r=%d\n", r);
448 if(total_read != APPEND_BYTES_2) {
449 PRINTF_CFS(
"FAIL:-47 total_read=%d\n", total_read);
466 stm32w_flash_read(uint32_t address,
void *data, uint32_t length)
468 uint8_t *pdata = (uint8_t *) address;
470 ENERGEST_ON(ENERGEST_TYPE_FLASH_READ);
471 memcpy(data, pdata, length);
472 ENERGEST_OFF(ENERGEST_TYPE_FLASH_READ);
476 stm32w_flash_erase(uint8_t sector)
481 uint32_t addr = COFFEE_START + (sector) * COFFEE_SECTOR_SIZE;
482 uint32_t end = addr + COFFEE_SECTOR_SIZE;
485 if(!(addr >= MFB_BOTTOM && end <= MFB_TOP + 1)) {
489 for(; addr < end; addr += 2) {
498 #ifndef STATIC_FLASH_BUFFER
499 #define STATIC_FLASH_BUFFER 1
503 stm32w_flash_write(uint32_t address,
const void *data, uint32_t length)
505 const uint32_t end = address + length;
507 uint32_t next_page, curr_page;
509 #if STATIC_FLASH_BUFFER
510 static uint8_t buf[FLASH_PAGE_SIZE];
512 uint8_t buf[FLASH_PAGE_SIZE];
515 for(i = address; i < end;) {
516 next_page = (i | (FLASH_PAGE_SIZE - 1)) + 1;
517 curr_page = i & ~(FLASH_PAGE_SIZE - 1);
518 offset = i - curr_page;
519 if(next_page > end) {
524 stm32w_flash_read(curr_page, buf, FLASH_PAGE_SIZE);
526 memcpy(buf + offset, data, next_page - i);
528 ENERGEST_ON(ENERGEST_TYPE_FLASH_WRITE);
532 ENERGEST_OFF(ENERGEST_TYPE_FLASH_WRITE);
534 data = (uint8_t *) data + next_page - i;
int cfs_open(const char *name, int flags)
Open a file.
cfs_offset_t cfs_seek(int fd, cfs_offset_t offset, int whence)
Seek to a specified position in an open file.
#define CFS_WRITE
Specify that cfs_open() should open a file for writing.
#define CFS_READ
Specify that cfs_open() should open a file for reading.
StStatus halInternalFlashWrite(uint32_t address, uint16_t *data, uint32_t length)
Writes a block of words to flash.
#define CFS_APPEND
Specify that cfs_open() should append written data to the file rather than overwriting it...
int cfs_coffee_reserve(const char *name, cfs_offset_t size)
Reserve space for a file.
int cfs_remove(const char *name)
Remove a file.
Coffee architecture-dependent header for the STM32W108-based mb851 platform.
StStatus halInternalFlashErase(uint8_t eraseType, uint32_t address)
Erases a section of flash back to all 0xFFFF.
#define CFS_SEEK_SET
Specify that cfs_seek() should compute the offset from the beginning of the file. ...
Header for the Coffee file system.
Header file for the CRC16 calculcation
unsigned short random_rand(void)
Generate the next state and return the upper part of it.
void cfs_close(int fd)
Close an open file.