47 #define PRINTF(...) printf(__VA_ARGS__)
57 #define PRINTF_CFS(...) printf(__VA_ARGS__)
59 #define PRINTF_CFS(...)
65 #include "lib/random.h"
68 #define FAIL(x) PRINTF("FAILED\n");error = (x); goto end;
73 coffee_file_test(
void)
77 unsigned char buf[256], buf2[11];
78 int r, i, j, total_read;
89 for(r = 0; r <
sizeof(buf); r++) {
106 r = cfs_write(wfd, buf,
sizeof(buf));
109 }
else if(r <
sizeof(buf)) {
118 r = cfs_read(wfd, buf,
sizeof(buf));
138 r = cfs_write(rfd, buf,
sizeof(buf));
148 memset(buf, 0,
sizeof(buf));
149 r = cfs_read(rfd, buf,
sizeof(buf));
152 }
else if(r <
sizeof(buf)) {
153 PRINTF_CFS(
"r=%d\n", r);
162 for(r = 0; r <
sizeof(buf); r++) {
164 PRINTF_CFS(
"r=%d. buf[r]=%d\n", r, buf[r]);
183 r = cfs_write(wfd, buf,
sizeof(buf));
186 }
else if(r <
sizeof(buf)) {
196 memset(buf, 0,
sizeof(buf));
197 r = cfs_read(rfd, buf,
sizeof(buf));
200 }
else if(r <
sizeof(buf)) {
209 for(r = 0; r <
sizeof(buf); r++) {
220 for(r = 0; r <
sizeof(buf); r++) {
221 buf[r] =
sizeof(buf) - r - 1;
226 r = cfs_write(wfd, buf,
sizeof(buf));
229 }
else if(r <
sizeof(buf)) {
242 memset(buf, 0,
sizeof(buf));
243 r = cfs_read(rfd, buf,
sizeof(buf));
246 }
else if(r <
sizeof(buf)) {
247 PRINTF_CFS(
"r = %d\n", r);
256 for(r = 0; r <
sizeof(buf); r++) {
257 if(buf[r] !=
sizeof(buf) - r - 1) {
274 for(r = 0; r < 100; r++) {
280 offset = random_rand() % FILE_SIZE;
282 for(r = 0; r <
sizeof(buf); r++) {
290 if(cfs_write(wfd, buf,
sizeof(buf)) !=
sizeof(buf)) {
298 memset(buf, 0,
sizeof(buf));
299 if(cfs_read(wfd, buf,
sizeof(buf)) !=
sizeof(buf)) {
303 for(i = 0; i <
sizeof(buf); i++) {
305 PRINTF_CFS(
"buf[%d] != %d\n", i, buf[i]);
314 #define APPEND_BYTES 3000
316 for (i = 0; i < APPEND_BYTES; i += BULK_SIZE) {
321 for (j = 0; j < BULK_SIZE; j++) {
322 buf[j] = 1 + ((i + j) & 0x7f);
324 if ((r = cfs_write(afd, buf, BULK_SIZE)) != BULK_SIZE) {
325 PRINTF_CFS(
"Count:%d, r=%d\n", i, r);
342 while((r = cfs_read(afd, buf2,
sizeof(buf2))) > 0) {
343 for(j = 0; j < r; j++) {
344 if(buf2[j] != 1 + ((total_read + j) & 0x7f)) {
351 PRINTF_CFS(
"FAIL:-35 r=%d\n",r);
354 if(total_read != APPEND_BYTES) {
355 PRINTF_CFS(
"FAIL:-35 total_read=%d\n",total_read);
368 #define APPEND_BYTES_1 2000
369 #define BULK_SIZE_1 10
370 for (i = 0; i < APPEND_BYTES_1; i += BULK_SIZE_1) {
375 for (j = 0; j < BULK_SIZE_1; j++) {
376 buf[j] = 1 + ((i + j) & 0x7f);
379 if ((r = cfs_write(afd, buf, BULK_SIZE_1)) != BULK_SIZE_1) {
380 PRINTF_CFS(
"Count:%d, r=%d\n", i, r);
391 while((r = cfs_read(afd, buf2,
sizeof(buf2))) > 0) {
392 for(j = 0; j < r; j++) {
393 if(buf2[j] != 1 + ((total_read + j) & 0x7f)) {
394 PRINTF_CFS(
"FAIL:-39, total_read=%d r=%d\n",total_read,r);
401 PRINTF_CFS(
"FAIL:-40 r=%d\n",r);
404 if(total_read != APPEND_BYTES_1) {
405 PRINTF_CFS(
"FAIL:-41 total_read=%d\n",total_read);
413 #define APPEND_BYTES_2 1000
414 #define BULK_SIZE_2 10
415 for (i = 0; i < APPEND_BYTES_2; i += BULK_SIZE_2) {
420 for (j = 0; j < BULK_SIZE_2; j++) {
421 buf[j] = 1 + ((i + j) & 0x7f);
424 if ((r = cfs_write(afd, buf, BULK_SIZE_2)) != BULK_SIZE_2) {
425 PRINTF_CFS(
"Count:%d, r=%d\n", i, r);
437 while((r = cfs_read(afd, buf2,
sizeof(buf2))) > 0) {
438 for(j = 0; j < r; j++) {
439 if(buf2[j] != 1 + ((total_read + j) & 0x7f)) {
440 PRINTF_CFS(
"FAIL:-45, total_read=%d r=%d\n",total_read,r);
447 PRINTF_CFS(
"FAIL:-46 r=%d\n",r);
450 if(total_read != APPEND_BYTES_2) {
451 PRINTF_CFS(
"FAIL:-47 total_read=%d\n",total_read);
465 void stm32w_flash_read(int32u address,
void * data, int32u length)
467 int8u * pdata = (int8u *)address;
468 ENERGEST_ON(ENERGEST_TYPE_FLASH_READ);
469 memcpy(data, pdata, length);
470 ENERGEST_OFF(ENERGEST_TYPE_FLASH_READ);
473 void stm32w_flash_erase(int8u sector)
478 int32u addr = COFFEE_START + (sector) * COFFEE_SECTOR_SIZE;
479 int32u end = addr + COFFEE_SECTOR_SIZE;
482 if (!(addr >= MFB_BOTTOM && end <= MFB_TOP + 1)) {
486 for(; addr < end; addr += 2){
492 #ifndef STATIC_FLASH_BUFFER
493 #define STATIC_FLASH_BUFFER 1
496 void stm32w_flash_write(int32u address,
const void * data, int32u length)
498 const int32u end = address + length;
500 int32u next_page, curr_page;
503 #if STATIC_FLASH_BUFFER
504 static int8u buf[FLASH_PAGE_SIZE];
506 int8u buf[FLASH_PAGE_SIZE];
509 for(i = address; i < end;) {
510 next_page = (i | (FLASH_PAGE_SIZE-1)) + 1;
511 curr_page = i & ~(FLASH_PAGE_SIZE-1);
512 offset = i-curr_page;
513 if(next_page > end) {
518 stm32w_flash_read(curr_page, buf, FLASH_PAGE_SIZE);
520 memcpy(buf + offset, data, next_page - i);
522 ENERGEST_ON(ENERGEST_TYPE_FLASH_WRITE);
526 ENERGEST_OFF(ENERGEST_TYPE_FLASH_WRITE);
528 data = (u8_t *)data + next_page - i;