Permalink
Browse files

Added check for EEPROM file name, if a file name exists at end of EEP…

…ROM (in reverse order) AND the file name can be opened, then use it instead of the hard-coded "app.bin"
  • Loading branch information...
1 parent 211d995 commit 6767d2abc9a2e5bb7ec7c38909b8cb32358eff0c @mharizanov mharizanov committed May 9, 2012
Showing with 43 additions and 9 deletions.
  1. +43 −9 main.c
View
52 main.c
@@ -37,6 +37,7 @@
/-------------------------------------------------------------------------*/
+
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <string.h>
@@ -49,6 +50,7 @@ void flash_write (DWORD, const BYTE*); /* Program a flash page (asmfunc.S) */
FATFS Fatfs; /* Petit-FatFs work area */
BYTE Buff[SPM_PAGESIZE]; /* Page data buffer */
+
static uint8_t
pagecmp(uint16_t addr, uint8_t *data)
{
@@ -58,28 +60,60 @@ pagecmp(uint16_t addr, uint8_t *data)
if (pgm_read_byte(addr++) != *data++)
return 1;
}
+
return 0;
}
+
int main (void)
{
+
DWORD fa; /* Flash address */
WORD br; /* Bytes read */
-
+ uint8_t i = 0;
+ uint8_t ch = 0;
pf_mount(&Fatfs); /* Initialize file system */
- if (pf_open("app.bin") == FR_OK) { /* Open application file */
+
+
+ /* read board name from eeprom to Buff */
+ while(i<13) { //8+'.'+3
+ #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
+ while(EECR & (1<<EEPE));
+ EEAR = (uint16_t)(void *)E2END -i;
+ EECR |= (1<<EERE);
+ ch =EEDR;
+ #else
+ ch = eeprom_read_byte((void *)E2END - i);
+ #endif
+ if( ch == 0xFF) break;
+ Buff[i] = ch;
+ i++;
+ }
+ Buff[i] = '\0';
+
+ ch=0; /* Re-use the variable to save space, now we use it to check if EEPROM or hard-coded file name exists */
+ if (i) { /* File name found in EEPROM */
+ if (pf_open(Buff) == FR_OK) { /* File opens normally */
+ ch=1;
+ }
+ }
+ else { /* No EEPROM file name found, or the EEPROM file name could not be located on the SD card, so revert tp the hard-coded name */
+ if (pf_open("app.bin") == FR_OK) ch=1;
+ }
+
+
+ if (ch) { /* Open application file */
for (fa = 0; fa < BOOT_ADR; fa += SPM_PAGESIZE) { /* Update all application pages */
- memset(Buff, 0xFF, SPM_PAGESIZE); /* Clear buffer */
- pf_read(Buff, SPM_PAGESIZE, &br); /* Load a page data */
+ memset(Buff, 0xFF, SPM_PAGESIZE); /* Clear buffer */
+ pf_read(Buff, SPM_PAGESIZE, &br); /* Load a page data */
- if (br) { /* Bytes Read > 0? */
- uint16_t j;
- for (j = br; j < SPM_PAGESIZE; j++) /* Pad the remaining last page with 0xFF so that comparison goes OK */
- Buff[j] = 0xFF;
+ if (br) { /* Bytes Read > 0? */
+ for (i = br; i < SPM_PAGESIZE; i++) /* Pad the remaining last page with 0xFF so that comparison goes OK */
+ Buff[i] = 0xFF;
if (pagecmp(fa, Buff)) { /* Only flash if page is changed */
flash_erase(fa); /* Erase a page */
- flash_write(fa, Buff); /* Write it if the data is available */
+ flash_write(fa, Buff); /* Write it if the data is available */
}
}
}

0 comments on commit 6767d2a

Please sign in to comment.