Kim Youngsik / Mbed 2 deprecated 0ROBOFRIEN_FCC_v1_12

Dependencies:   mbed BufferedSerial ConfigFile

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers eeprom.h Source File

eeprom.h

00001 #define eeprom_length 255
00002 
00003 
00004 LocalFileSystem local("local");               // Create the local filesystem under the name "local"
00005 
00006 volatile int eeprom_address[eeprom_length],eeprom_data[eeprom_length];
00007 void eeprom_init(){
00008     int eeprom_chksum;
00009     FILE *file = fopen("/local/eeprom.txt", "r");  // Open "out.txt" on the local file system for writing    
00010     if(file == NULL){
00011         FILE *file = fopen("/local/eeprom.txt", "w");   // Write "out.txt ~~//
00012         for(int i=0; i<eeprom_length; i++){
00013             fprintf(file, "%d\t%d\r\n", i, 0);   
00014             wait(0.0001);
00015         }        
00016     }else{
00017         for(int i=0; i<eeprom_length; i++) {eeprom_address[i] = 0; eeprom_data[i] = 0; }
00018         for(int i=0; i<eeprom_length; i++){
00019             fscanf(file,"%d\t%d\r\n",&eeprom_address[i], &eeprom_data[i]);
00020             wait(0.0001);
00021         }
00022         int tmp;
00023         fscanf(file,"%d\t%d\t\n",&tmp, &eeprom_chksum);
00024     }
00025     fclose(file);
00026     int cal_eeprom_chksum = 0;
00027     for(int i=0; i<eeprom_length; i++){
00028         cal_eeprom_chksum += eeprom_data[i];
00029     }
00030     if( cal_eeprom_chksum != eeprom_chksum){
00031         while(1){
00032             DigitalOut myled1(LED1);
00033             DigitalOut myled2(LED2);
00034             DigitalOut myled3(LED3);
00035             DigitalOut myled4(LED4);
00036             myled1 = 1; myled2 = 1; myled3 = 1; myled4 = 1;
00037             wait(1);
00038             myled1 = 0; myled2 = 0; myled3 = 0; myled4 = 0;
00039             wait(1);
00040         }
00041     }
00042 }
00043 
00044 bool eeprom_refresh(){
00045     /// Write Data to EEPROM //
00046     bool OUTPUT_VALUE = false;
00047     int eeprom_chksum = 0;
00048     __disable_irq();
00049     FILE *file = fopen("/local/eeprom.txt", "w");  // Open "out.txt" on the local file system for writing    
00050 //    if(file != NULL){
00051         OUTPUT_VALUE = true;
00052         for(int i=0; i<eeprom_length; i++){
00053             fprintf(file,"%d\t%d\r\n",i, eeprom_data[i]);
00054             wait(0.0001);
00055             eeprom_chksum += eeprom_data[i];
00056         }
00057         fprintf(file,"%d\t%d",eeprom_length,eeprom_chksum);    
00058 //    }
00059     fclose(file);
00060     __enable_irq();
00061     return OUTPUT_VALUE;
00062 }
00063 
00064 void eeprom_write(int addr, int data){
00065     /// Change Data //
00066     eeprom_data[addr] = data;
00067 }
00068 
00069 
00070 int eeprom_read(int addr){
00071     return eeprom_data[addr];    
00072     
00073 }
00074