ButterflyMP3

eeprom.c

Go to the documentation of this file.
00001 
00023 #include <avr/io.h>
00024 #include <avr/pgmspace.h>
00025 #include "types.h"
00026 #include "eeprom.h"
00027 // include workaround for ATmega169 and avr-libc 1.0
00028 #include "eeprom169.h"
00029 #include "main.h"
00030 //#include "avrfat16.h"
00031 #include "uart.h"
00032 
00033 // player variables from main.c
00034 extern uint8    gmusic_vol;             // music volume.
00035 extern uint8    gbass_boost;            // bass boost off.
00036 extern uint8    gMode;          // mode of player, idle, playing, off, etc
00037 extern uint32   gFAT_entry;     // global current entry number in FAT table. (aka file number)
00038 extern uint32   gCluster;                       // current sector (cluster) in song
00039 extern uint32   gFileSectorsPlayed; // number of full sectors played. (aka position)
00040 extern uint32   FAT16_dir_first_sector; //first sector of curent directory.
00041 extern volatile uint16  gPlayTimeSeconds;       // # of seconds current file has been playing.
00042 
00043 
00044 /*****************************************************************************
00045 *
00046 *   Function name : StoreEEPROM
00047 *
00048 *   Returns :       None
00049 *
00050 *   Parameters :    Pointer to string, number of bytes to write, adress in EEPROM
00051 *
00052 *   Purpose :       Write byte(s) to the EEPROM
00053 *
00054 *****************************************************************************/
00055 void StoreEEPROM(uint8 *pBuffer, uint8 num_bytes, unsigned int EE_START_ADR)
00056 {
00057     unsigned char i;
00058     for (i=0;i<num_bytes;i++) {
00059         // mt __EEPUT(EEPROM_START_ADR++, pBuffer[i]); // Store parameters
00060                 eeprom_write_byte_169(&EE_START_ADR, pBuffer[i]);
00061                 EE_START_ADR++;
00062         }
00063 }
00064 
00065 
00066 /*****************************************************************************
00067 *
00068 *   Function name : ReadEEPROM
00069 *
00070 *   Returns :       None
00071 *
00072 *   Parameters :    Pointer to string, number of bytes to read, adress in EEPROM
00073 *
00074 *   Purpose :       Read byte(s) to the EEPROM
00075 *
00076 *****************************************************************************/
00077 void LoadEEPROM(uint8 *pBuffer, uint8 num_bytes, unsigned int EE_START_ADR)
00078 {
00079     unsigned char i;
00080     for (i=0;i<num_bytes;i++) {
00081         // mt __EEGET(pBuffer[i], EEPROM_START_ADR++); // Load parameters
00082                 pBuffer[i]=eeprom_read_byte_169(&EE_START_ADR);
00083                 EE_START_ADR++;
00084         }
00085 }
00086 
00087 /*****************************************************************************
00088 *
00089 *   Function name : ReadEEPROM
00090 *
00091 *   Returns :       byte read from EEPROM
00092 *
00093 *   Parameters :    adress in EEPROM
00094 *
00095 *   Purpose :       Read a single byte from the EEPROM
00096 *
00097 *****************************************************************************/
00098 uint8 ReadEEPROM(unsigned int EE_START_ADR)
00099 {
00100         return eeprom_read_byte_169(&EE_START_ADR);
00101 }
00102 
00103 
00104 
00113 uint8 findCurrentEepromAddr(uint16 index_buffer )
00114 {
00115 #ifdef USE_EEPROM
00116 #ifdef ENDURANCE_EEPROM
00117         // Modified from AVR application note 
00118         // AVR101: High Endurance EEPROM Storage
00119         uint16 temp;
00120         uint16 EeBufPtr;
00121         uint16 EeBufEnd;
00122         
00123         EeBufPtr = index_buffer ;              // Point the the status buffer
00124         EeBufEnd = EeBufPtr + EEPROM_BFFRSZE;   // The first address outside the buffer
00125         
00126         /* Identify the last writen element of the status buffer */
00127         do{
00128                 if( EeBufPtr == EeBufEnd )
00129                         break;
00130                 temp = ReadEEPROM( EeBufPtr++ );
00131         }while ( ReadEEPROM( EeBufPtr ) == temp +1 );
00132         
00133         EeBufPtr -=  (index_buffer + 1);       // offset to the last used element of the parameter buffer
00134         return (uint8) EeBufPtr;
00135 #else
00136         return 0;
00137 #endif
00138 #endif
00139 
00140 }
00141 
00142 
00148 void save_player_options(void)
00149 {
00150 #ifdef USE_EEPROM
00151         uint8 tmp;
00152         uint8 index=0;
00153         uint8 oldIndexVal;
00154         
00155 #ifdef ENDURANCE_EEPROM 
00156         /* Store the old status value and move pointer to the next element in the buffer */
00157         index= findCurrentEepromAddr(EEPROM_OPTIONS_INDEX);
00158         //PRINT("INDEX =");UART_Printfu08(index);EOL();
00159         
00160         oldIndexVal = ReadEEPROM(EEPROM_OPTIONS_INDEX+index);
00161         //PRINT("VALUE =");UART_Printfu08(oldIndexVal);
00162         if (oldIndexVal == 0xFF) 
00163                 index--; //workaround for odd bug at 255 writes 
00164 #endif  
00165         
00166         //load saved options
00167         LoadEEPROM(&tmp,1,EEPROM_VOL+index);
00168         
00169         // compare saved to current options
00170         if (tmp!=gmusic_vol){ // save vol if changed 
00171                 
00172 #ifdef ENDURANCE_EEPROM 
00173                 // Move to  next available position in buffer
00174                 index++;
00175                 if (index == EEPROM_BFFRSZE){
00176                         index =0;
00177                 }
00178 #endif          
00179                 
00180                 // write options to eeprom
00181                 StoreEEPROM(&gmusic_vol,1,EEPROM_VOL+index);
00182                 
00183 #ifdef ENDURANCE_EEPROM                 
00184                 // write new buffer index
00185                 oldIndexVal++;
00186                 StoreEEPROM(&oldIndexVal,1,EEPROM_OPTIONS_INDEX+index);
00187                 //UART_Printfu08(oldIndexVal); UART_Printfu08(index); EOL();
00188 #endif
00189                 
00190                 //PRINT("vol saved as ");       UART_Printfu08(gmusic_vol);EOL();
00191         }// else leave eeprom unchanged.
00192 #endif
00193 }
00194 
00195 
00201 void save_player_state(void)
00202 {
00203 #ifdef USE_EEPROM
00204         uint8 index=0;
00205         uint8 oldIndexVal;
00206         
00207 #ifdef ENDURANCE_EEPROM 
00208         /* Store the old status value and move pointer to the next element in the buffer */
00209         index= findCurrentEepromAddr(EEPROM_STATE_INDEX);
00210         //PRINT("INDEX =");UART_Printfu08(index);EOL();
00211         
00212         oldIndexVal = ReadEEPROM(EEPROM_STATE_INDEX+index);
00213         //PRINT("VALUE =");UART_Printfu08(oldIndexVal);
00214         if (oldIndexVal == 0xFF) 
00215                 index--; //workaround for odd bug at 255 writes 
00216         
00217         // Move to  next available position in buffer
00218         index++;
00219         if (index == EEPROM_BFFRSZE){
00220                 index =0;
00221         }
00222 #endif
00223         
00224         // Store player state
00225         StoreEEPROM(&gMode,1,EEPROM_MODE+index);
00226         StoreEEPROM((uint8 *)&gFAT_entry,4,EEPROM_FILE+index);
00227         StoreEEPROM((uint8 *)&gCluster,4,EEPROM_SECTOR+index);
00228         StoreEEPROM((uint8 *)&gFileSectorsPlayed,4,EEPROM_PLAYED+index);
00229         StoreEEPROM((uint8 *)&gPlayTimeSeconds,2,EEPROM_TIME+index);
00230         StoreEEPROM((uint8 *)&FAT16_dir_first_sector,4,EEPROM_DIR+index);
00231         
00232         
00233 #ifdef ENDURANCE_EEPROM 
00234         // write new buffer index
00235         oldIndexVal++;
00236         StoreEEPROM(&oldIndexVal,1,EEPROM_STATE_INDEX+index);
00237 #endif
00238         
00239         // Debug data about buffer contents
00240 //      PRINT("saving to eeprom");EOL();
00241 //      UART_Printfu08(gMode);EOL();
00242 //      UART_Printfu32(gFAT_entry);EOL();
00243 //      UART_Printfu16(gPlayTimeSeconds);EOL();
00244 //      UART_Printfu32(gCluster);EOL();
00245 //      UART_Printfu32(gFileSectorsPlayed);EOL();
00246 #endif
00247 }
00248 
00249 
00255 void load_player_options(void)
00256 {
00257 #ifdef USE_EEPROM
00258         uint8 index=0;
00259         
00260 #ifdef ENDURANCE_EEPROM 
00261         // get position in ring buffer
00262         index= findCurrentEepromAddr(EEPROM_OPTIONS_INDEX);
00263         //PRINT("INDEX =");UART_Printfu08(index);EOL();
00264 #endif
00265         
00266         // get options (volume only at this stage
00267         LoadEEPROM(&gmusic_vol,1,EEPROM_VOL+index);
00268         if (gmusic_vol>9)
00269                 gmusic_vol = 5; // set to default if invalid
00270         
00271         //PRINT("vol Loaded as ");UART_Printfu08(gmusic_vol);EOL();
00272 #endif
00273 }
00274 
00275 
00282 void load_player_state(uint8 *mode, uint32 *file, uint32 *sector, uint32 *played,uint16 *time,uint32 *dir)
00283 {
00284 #ifdef USE_EEPROM
00285         uint8 index=0;
00286         
00287 #ifdef ENDURANCE_EEPROM 
00288         // get position in ring buffer
00289         index= findCurrentEepromAddr(EEPROM_STATE_INDEX);
00290         //PRINT("INDEX =");UART_Printfu08(index);EOL(); 
00291 #endif
00292         
00293         // try to find last song played
00294         LoadEEPROM(mode,1,EEPROM_MODE+index);
00295         LoadEEPROM((uint8 *)file,4,EEPROM_FILE+index);
00296         LoadEEPROM((uint8 *)sector,4,EEPROM_SECTOR+index);
00297         LoadEEPROM((uint8 *)played,4,EEPROM_PLAYED+index);
00298         LoadEEPROM((uint8 *)time,2,EEPROM_TIME+index);
00299         LoadEEPROM((uint8 *)dir,4,EEPROM_DIR+index);
00300 #endif
00301 }
00302 
00303 
 All Files Functions Variables Typedefs Enumerations Enumerator Defines