43 #include <avr/interrupt.h>
44 #include <avr/pgmspace.h>
52 #define PRINTF(FORMAT,args...) printf_P(PSTR(FORMAT),##args)
62 #define PRINTF_CFS(FORMAT,args...) printf_P(PSTR(FORMAT),##args)
64 #define PRINTF_CFS(...)
70 #include "lib/random.h"
73 #define FAIL(x) error = (x); goto end;
78 coffee_file_test(
void)
82 unsigned char buf[256], buf2[11];
83 int r, i, j, total_read;
94 for(r = 0; r <
sizeof(buf); r++) {
105 r = cfs_write(wfd, buf,
sizeof(buf));
108 }
else if(r <
sizeof(buf)) {
113 r = cfs_read(wfd, buf,
sizeof(buf));
125 r = cfs_write(rfd, buf,
sizeof(buf));
131 memset(buf, 0,
sizeof(buf));
132 r = cfs_read(rfd, buf,
sizeof(buf));
135 }
else if(r <
sizeof(buf)) {
136 PRINTF_CFS(
"r=%d\n", r);
141 for(r = 0; r <
sizeof(buf); r++) {
143 PRINTF_CFS(
"r=%d. buf[r]=%d\n", r, buf[r]);
154 r = cfs_write(wfd, buf,
sizeof(buf));
157 }
else if(r <
sizeof(buf)) {
163 memset(buf, 0,
sizeof(buf));
164 r = cfs_read(rfd, buf,
sizeof(buf));
167 }
else if(r <
sizeof(buf)) {
172 for(r = 0; r <
sizeof(buf); r++) {
179 for(r = 0; r <
sizeof(buf); r++) {
180 buf[r] =
sizeof(buf) - r - 1;
185 r = cfs_write(wfd, buf,
sizeof(buf));
188 }
else if(r <
sizeof(buf)) {
197 memset(buf, 0,
sizeof(buf));
198 r = cfs_read(rfd, buf,
sizeof(buf));
201 }
else if(r <
sizeof(buf)) {
202 PRINTF_CFS(
"r = %d\n", r);
207 for(r = 0; r <
sizeof(buf); r++) {
208 if(buf[r] !=
sizeof(buf) - r - 1) {
221 for(r = 0; r < 100; r++) {
227 offset = random_rand() % FILE_SIZE;
229 for(r = 0; r <
sizeof(buf); r++) {
237 if(cfs_write(wfd, buf,
sizeof(buf)) !=
sizeof(buf)) {
245 memset(buf, 0,
sizeof(buf));
246 if(cfs_read(wfd, buf,
sizeof(buf)) !=
sizeof(buf)) {
250 for(i = 0; i <
sizeof(buf); i++) {
252 PRINTF_CFS(
"buf[%d] != %d\n", i, buf[i]);
258 #define APPEND_BYTES 3000
260 for (i = 0; i < APPEND_BYTES; i += BULK_SIZE) {
265 for (j = 0; j < BULK_SIZE; j++) {
266 buf[j] = 1 + ((i + j) & 0x7f);
268 if ((r = cfs_write(afd, buf, BULK_SIZE)) != BULK_SIZE) {
269 PRINTF_CFS(
"Count:%d, r=%d\n", i, r);
282 while((r = cfs_read(afd, buf2,
sizeof(buf2))) > 0) {
283 for(j = 0; j < r; j++) {
284 if(buf2[j] != 1 + ((total_read + j) & 0x7f)) {
291 PRINTF_CFS(
"FAIL:-35 r=%d\n",r);
294 if(total_read != APPEND_BYTES) {
295 PRINTF_CFS(
"FAIL:-35 total_read=%d\n",total_read);
304 #define APPEND_BYTES_1 2000
305 #define BULK_SIZE_1 10
306 for (i = 0; i < APPEND_BYTES_1; i += BULK_SIZE_1) {
311 for (j = 0; j < BULK_SIZE_1; j++) {
312 buf[j] = 1 + ((i + j) & 0x7f);
316 if ((r = cfs_write(afd, buf, BULK_SIZE_1)) != BULK_SIZE_1) {
317 PRINTF_CFS(
"Count:%d, r=%d\n", i, r);
328 while((r = cfs_read(afd, buf2,
sizeof(buf2))) > 0) {
329 for(j = 0; j < r; j++) {
330 if(buf2[j] != 1 + ((total_read + j) & 0x7f)) {
331 PRINTF_CFS(
"FAIL:-39, total_read=%d r=%d\n",total_read,r);
338 PRINTF_CFS(
"FAIL:-40 r=%d\n",r);
341 if(total_read != APPEND_BYTES_1) {
342 PRINTF_CFS(
"FAIL:-41 total_read=%d\n",total_read);
347 #define APPEND_BYTES_2 1000
348 #define BULK_SIZE_2 10
349 for (i = 0; i < APPEND_BYTES_2; i += BULK_SIZE_2) {
354 for (j = 0; j < BULK_SIZE_2; j++) {
355 buf[j] = 1 + ((i + j) & 0x7f);
358 if ((r = cfs_write(afd, buf, BULK_SIZE_2)) != BULK_SIZE_2) {
359 PRINTF_CFS(
"Count:%d, r=%d\n", i, r);
371 while((r = cfs_read(afd, buf2,
sizeof(buf2))) > 0) {
372 for(j = 0; j < r; j++) {
373 if(buf2[j] != 1 + ((total_read + j) & 0x7f)) {
374 PRINTF_CFS(
"FAIL:-45, total_read=%d r=%d\n",total_read,r);
381 PRINTF_CFS(
"FAIL:-46 r=%d\n",r);
384 if(total_read != APPEND_BYTES_2) {
385 PRINTF_CFS(
"FAIL:-47 total_read=%d\n",total_read);
400 #ifdef COFFEE_AVR_EEPROM
404 static const unsigned char nullb[COFFEE_SECTOR_SIZE];
410 avr_eeprom_erase(uint16_t sector)
412 eeprom_write(COFFEE_START + sector * COFFEE_SECTOR_SIZE,
413 (
unsigned char *)nullb,
sizeof(nullb));
417 #ifdef COFFEE_AVR_FLASH
425 avr_flash_read(CFS_CONF_OFFSET_TYPE addr, uint8_t *buf, CFS_CONF_OFFSET_TYPE size)
427 uint32_t addr32=COFFEE_START+addr;
430 unsigned char *bufo=(
unsigned char *)buf;
432 uint16_t w=addr32>>1;
433 PRINTF(
"r0x%04x(%u) ",w,size);
435 #ifndef FLASH_WORD_READS
436 for (;isize>0;isize--) {
437 #if FLASH_COMPLEMENT_DATA
438 *buf++=~(uint8_t)pgm_read_byte_far(addr32++);
440 *buf++=(uint8_t)pgm_read_byte_far(addr32++);
446 #if FLASH_COMPLEMENT_DATA
447 *buf++=~(uint8_t)pgm_read_byte_far(addr32++);
449 *buf++=(uint8_t)pgm_read_byte_far(addr32++);
453 for (;isize>1;isize-=2) {
454 #if FLASH_COMPLEMENT_DATA
455 *(uint16_t *)buf=~(uint16_t)pgm_read_word_far(addr32);
457 *(uint16_t *)buf=(uint16_t)pgm_read_word_far(addr32);
463 #if FLASH_COMPLEMENT_DATA
464 *buf++=~(uint8_t)pgm_read_byte_far(addr32);
466 *buf++=(uint8_t)pgm_read_byte_far(addr32);
484 void avr_flash_erase(coffee_page_t sector) {
487 #if FLASH_COMPLEMENT_DATA
489 volatile uint8_t sreg;
495 for (i = 0; i < COFFEE_SECTOR_SIZE / COFFEE_PAGE_SIZE; i++) {
496 for (addr32 = COFFEE_START + (((sector + i) * COFFEE_PAGE_SIZE)
497 & ~(COFFEE_PAGE_SIZE - 1)); addr32 < (COFFEE_START + (((sector
498 + i + 1) * COFFEE_PAGE_SIZE) & ~(COFFEE_PAGE_SIZE - 1))); addr32
500 boot_page_erase(addr32);
501 boot_spm_busy_wait();
509 for (i=0;i<COFFEE_SECTOR_SIZE/COFFEE_PAGE_SIZE;i++) {
510 avr_flash_write((sector+i)*COFFEE_PAGE_SIZE,0,0);
520 if ((sector+i)==COFFEE_PAGES-1) {
521 int j=(int)(COFFEE_START>>1),k=(int)((COFFEE_START>>1)+(COFFEE_SIZE>>1)),l=(int)(COFFEE_SIZE/1024UL);
522 printf_P(PSTR(
"\nTesting coffee filesystem [0x%08x -> 0x%08x (%uKb)] ..."),j,k,l);
523 int r= coffee_file_test();
525 printf_P(PSTR(
"\nFailed with return %d! :-(\n"),r);
527 printf_P(PSTR(
"Passed! :-)\n"));
540 char avr_httpd_fs_getchar(
char *addr) {
542 avr_flash_read((CFS_CONF_OFFSET_TYPE) addr, (uint8_t*) &r, 1);
545 int avr_httpd_fs_strcmp (
char *ram,
char *addr) {
546 uint8_t i,*in,buf[32];
547 avr_flash_read((CFS_CONF_OFFSET_TYPE)addr, buf,
sizeof(buf));
551 if (buf[i]==0)
return(0);
552 if (buf[i]!=*in)
break;
559 char * avr_httpd_fs_strchr (
char *addr,
int character) {
563 avr_flash_read((CFS_CONF_OFFSET_TYPE)addr, (uint8_t *) buf, 128);
564 pptr=strchr(buf, character);
565 if (pptr!=&buf[128]) {
566 if (pptr==0)
return 0;
567 return (addr+(pptr-buf));
583 avr_flash_write(CFS_CONF_OFFSET_TYPE addr, uint8_t *buf, CFS_CONF_OFFSET_TYPE size)
597 #if 0 //this is 8 bytes longer
598 uint16_t startpage=addr/COFFEE_PAGE_SIZE;
599 addr32=COFFEE_START+startpage*COFFEE_PAGE_SIZE;
601 addr32=(COFFEE_ADDRESS&~(SPM_PAGESIZE-1))+(addr&~(SPM_PAGESIZE-1));
603 bb=addr & (SPM_PAGESIZE-1);
604 ba=COFFEE_PAGE_SIZE-((addr+size)&0xff);
607 uint16_t startpage=addr/COFFEE_PAGE_SIZE;
610 PRINTF(
"w0x%04x %u %u %u",w,size,bb,ba);
612 PRINTF(
"e0x%04x %u ",w,startpage);
621 w=pgm_read_word_far(addr32);
622 boot_page_fill(addr32,w);
629 w=pgm_read_word_far(addr32);
630 #if FLASH_COMPLEMENT_DATA
640 #if FLASH_COMPLEMENT_DATA
643 boot_page_fill(addr32, w);
660 if ((addr32&0x000000ff)==0) {
663 boot_page_erase(addr32);
664 boot_spm_busy_wait();
665 boot_page_write(addr32);
666 boot_spm_busy_wait();
673 w=pgm_read_word_far(addr32);
676 #if FLASH_COMPLEMENT_DATA
683 boot_page_fill(addr32,w);
689 #if FLASH_COMPLEMENT_DATA
690 addr32+=2*SPM_PAGESIZE;
692 for (w=0;w<SPM_PAGESIZE;w++) {
693 boot_page_fill(addr32, 0);
700 boot_page_erase(addr32);
701 boot_spm_busy_wait();
702 #if FLASH_COMPLEMENT_DATA
704 boot_page_write(addr32);
705 boot_spm_busy_wait();
708 boot_page_write(addr32);
709 boot_spm_busy_wait();