Self_programming.h File Reference


Detailed Description

Atmel Corporation

ReadFlashByte() ReadFlashPage() WriteFlashByte() WriteFlashPage() RecoverFlash()

The remaining functions contained in this file are used by the functions listet above.

Revision
2.0
Date
Wednesday, January 18, 2006 15:18:54 UTC

Definition in file Self_programming.h.

#include <ioavr.h>

Include dependency graph for Self_programming.h:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Defines

#define __FLASH_RECOVER
#define ADR_FLASH_BUFFER   0
#define ADR_LIMIT_HIGH   FLASHEND-BOOTSECTORSIZE
#define ADR_LIMIT_LOW   0
#define BOOTSECTORSIZE   0x2000
#define FALSE   0
#define FLASH_BUFFER_FULL_ID   0xAA
#define SPMControllRegister   SPMCSR
#define TRUE   1

Typedefs

typedef unsigned int MyAddressType
typedef unsigned char __flash * MyFlashCharPointer
typedef unsigned int __flash * MyFlashIntPointer

Functions

unsigned char AddressCheck (MyAddressType flashAdr)
void LpmReplaceSpm (MyAddressType flashAddr, unsigned char data)
unsigned char ReadFlashByte (MyAddressType flashAdr)
unsigned char ReadFlashPage (MyAddressType flashStartAdr, unsigned char *dataPage)
unsigned char RecoverFlash ()
void WriteBufToFlash (MyAddressType flashStartAdr)
unsigned char WriteFlashByte (MyAddressType flashAddr, unsigned char data)
unsigned char WriteFlashPage (MyAddressType flashStartAdr, unsigned char *dataPage)


Define Documentation

#define __FLASH_RECOVER
 

Definition at line 43 of file Self_programming.h.

Referenced by WriteFlashByte().

#define ADR_FLASH_BUFFER   0
 

Definition at line 45 of file Self_programming.h.

Referenced by AddressCheck(), RecoverFlash(), WriteFlashByte(), and WriteFlashPage().

#define ADR_LIMIT_HIGH   FLASHEND-BOOTSECTORSIZE
 

Definition at line 50 of file Self_programming.h.

Referenced by AddressCheck().

#define ADR_LIMIT_LOW   0
 

Definition at line 47 of file Self_programming.h.

Referenced by AddressCheck().

#define BOOTSECTORSIZE   0x2000
 

Definition at line 49 of file Self_programming.h.

#define FALSE   0
 

Definition at line 134 of file Self_programming.h.

Referenced by AddressCheck().

#define FLASH_BUFFER_FULL_ID   0xAA
 

Definition at line 132 of file Self_programming.h.

Referenced by RecoverFlash(), WriteFlashByte(), and WriteFlashPage().

#define SPMControllRegister   SPMCSR
 

Definition at line 125 of file Self_programming.h.

Referenced by WriteBufToFlash().

#define TRUE   1
 

Definition at line 133 of file Self_programming.h.

Referenced by AddressCheck(), ReadFlashPage(), RecoverFlash(), WriteFlashByte(), and WriteFlashPage().


Typedef Documentation

typedef unsigned int MyAddressType
 

Definition at line 104 of file Self_programming.h.

typedef unsigned char __flash* MyFlashCharPointer
 

Definition at line 105 of file Self_programming.h.

typedef unsigned int __flash* MyFlashIntPointer
 

Definition at line 106 of file Self_programming.h.


Function Documentation

unsigned char AddressCheck MyAddressType  flashAdr  ) 
 

The function checks if input argument is a valid Flash page address for writing. Returns TRUE only if:

  • Address points to the beginning of a Flash page
  • Address is within the limits defined in Self_programming.h
  • Address is not equal to page address used for buffring by the Flash recovery functions (if enabled). Returns FALSE else.

Definition at line 207 of file Self_programming.c.

References ADR_FLASH_BUFFER, ADR_LIMIT_HIGH, ADR_LIMIT_LOW, FALSE, and TRUE.

Referenced by WriteFlashByte(), and WriteFlashPage().

00207                                                   {
00208   #ifdef __FLASH_RECOVER
00209   // The next line gives a warning 'pointless comparison with zero' if ADR_LIMIT_LOW is 0. Ignore it.
00210   if( (flashAdr >= ADR_LIMIT_LOW) && (flashAdr <= ADR_LIMIT_HIGH) &&
00211       (flashAdr != ADR_FLASH_BUFFER) && !(flashAdr & (PAGESIZE-1)) )
00212     return TRUE;                            // Address is a valid page address
00213   else
00214     return FALSE;                           // Address is not a valid page address
00215   #else
00216   if((flashAdr >= ADR_LIMIT_LOW) && (flashAdr <= ADR_LIMIT_HIGH) && !(flashAdr & (PAGESIZE-1) ) )
00217     return TRUE;                            // Address is a valid page address
00218   else
00219     return FALSE;                           // Address is not a valid page address
00220   #endif
00221 }

void LpmReplaceSpm MyAddressType  flashAddr,
unsigned char  data
 

The function reads Flash page given by flashAddr, replaces one byte given by flashAddr with data, and stores entire page in Flash temporary buffer.

Definition at line 247 of file Self_programming.c.

Referenced by WriteFlashByte().

00247                                                                {
00248 #pragma diag_suppress=Pe1053 // Suppress warning for conversion from long-type address to flash ptr.
00249   unsigned int index, oddByte, pcWord;
00250   MyAddressType  pageAdr;
00251   oddByte=(unsigned char)flashAddr & 1;
00252   pcWord=(unsigned int)flashAddr & (PAGESIZE-2); // Used when writing FLASH temp buffer
00253   pageAdr=flashAddr & ~(PAGESIZE-1);        // Get FLASH page address from byte address
00254   for(index=0; index < PAGESIZE; index+=2){
00255     if(index==pcWord){
00256       if(oddByte){
00257         _SPM_FILLTEMP( index, (*(MyFlashCharPointer)(flashAddr & ~1) | ((unsigned int)data<<8)) );
00258       }                                     // Write odd byte in temporary buffer
00259       else{
00260         _SPM_FILLTEMP( index, ( (*( (MyFlashCharPointer)flashAddr+1)<<8)  | data ) );
00261       }                                     // Write even byte in temporary buffer
00262     }
00263     else{
00264       _SPM_FILLTEMP(index, *( (MyFlashIntPointer)(pageAdr+index) ) );
00265     }                                       // Write Flash word directly to temporary buffer
00266   }
00267 #pragma diag_default=Pe1053 // Back to default.
00268 }

unsigned char ReadFlashByte MyAddressType  flashStartAdr  ) 
 

The function Returns one byte located on Flash address given by ucFlashStartAdr.

Definition at line 53 of file Self_programming.c.

Referenced by ReadFlashPage().

00053                                                         {
00054 #pragma diag_suppress=Pe1053 // Suppress warning for conversion from long-type address to flash ptr.
00055   return (unsigned char)*((MyFlashCharPointer)flashStartAdr);
00056 #pragma diag_default=Pe1053 // Back to default.
00057 } // Returns data from Flash

unsigned char ReadFlashPage MyAddressType  flashStartAdr,
unsigned char *  dataPage
 

The function reads one Flash page from address flashStartAdr and stores data in array dataPage[]. The number of bytes stored is depending upon the Flash page size. The function returns FALSE if input address is not a Flash page address, else TRUE.

Definition at line 65 of file Self_programming.c.

References ReadFlashByte(), and TRUE.

00065                                                                                  {
00066   unsigned int index;
00067   if(!(flashStartAdr & (PAGESIZE-1))){      // If input address is a page address
00068     for(index = 0; index < PAGESIZE; index++){
00069       dataPage[index] = ReadFlashByte(flashStartAdr + index);
00070     }
00071     return TRUE;                            // Return TRUE if valid page address
00072   }
00073   else{
00074     return FALSE;                           // Return FALSE if not valid page address
00075   }
00076 }

Here is the call graph for this function:

unsigned char RecoverFlash  ) 
 

The function checks if global variable FlashBackup.status indicates that Flash recovery buffer contains data that needs to be written to Flash. Writes data from Flash recovery buffer to Flash page address given by FLASH_recovery.pageAdr. This function should be called at program startup if FLASH recovery option is enabeled.

Definition at line 178 of file Self_programming.c.

References ADR_FLASH_BUFFER, FLASH_BUFFER_FULL_ID, FlashBackup, TRUE, and WriteBufToFlash().

Referenced by main().

00178                             {
00179 #ifdef __FLASH_RECOVER
00180   unsigned int index;
00181   if(FlashBackup.status == FLASH_BUFFER_FULL_ID){ // Checks if Flash recovery
00182                                                   //  buffer contains data
00183     for(index=0; index < PAGESIZE; index+=2){     // Writes to Flash write buffer
00184         _SPM_FILLTEMP( index, *((MyFlashIntPointer)(ADR_FLASH_BUFFER+index)) );
00185     }
00186     WriteBufToFlash((MyAddressType)FlashBackup.pageNumber * PAGESIZE);
00187     FlashBackup.status=0;                   // Inicate that Flash buffer does
00188                                             // not contain data for writing
00189     while(EECR & (1<<EEWE));
00190     return TRUE;                            // Returns TRUE if recovery has
00191                                             // taken place
00192   }
00193 #endif
00194   return FALSE;
00195 }

Here is the call graph for this function:

void WriteBufToFlash MyAddressType  flashStartAdr  ) 
 

The function writes Flash temporary buffer to Flash page address given by input argument.

Definition at line 228 of file Self_programming.c.

References SPMControllRegister.

Referenced by RecoverFlash(), WriteFlashByte(), and WriteFlashPage().

00228                                                  {
00229 #pragma diag_suppress=Pe1053 // Suppress warning for conversion from long-type address to flash ptr.
00230   #ifdef __HAS_RAMPZ__
00231   RAMPZ = (unsigned char)(flashStartAdr >> 16);
00232   #endif
00233   _SPM_ERASE(flashStartAdr);
00234   while( SPMControllRegister & (1<<SPMEN) ); // Wait until Flash write completed
00235   _SPM_PAGEWRITE(flashStartAdr);
00236   while( SPMControllRegister & (1<<SPMEN) ); // Wait until Flash write completed
00237   #ifdef RWWSRE
00238   __DataToR0ByteToSPMCR_SPM( 0, (unsigned char)(1<<RWWSRE)|(1<<SPMEN)); // Enable RWW
00239   #endif
00240 #pragma diag_default=Pe1053 // Back to default.
00241 }

unsigned char WriteFlashByte MyAddressType  flashAddr,
unsigned char  data
 

The function writes byte data to Flash address flashAddr. Returns FALSE if input address is not valid Flash byte address for writing, else TRUE.

Definition at line 82 of file Self_programming.c.

References __FLASH_RECOVER, AddressCheck(), ADR_FLASH_BUFFER, FLASH_BUFFER_FULL_ID, FlashBackup, LpmReplaceSpm(), TRUE, and WriteBufToFlash().

00082                                                                          {
00083   MyAddressType  pageAdr;
00084   unsigned char eepromInterruptSettings;
00085   if( AddressCheck( flashAddr & ~(PAGESIZE-1) )){
00086 
00087     eepromInterruptSettings= EECR & (1<<EERIE); // Stores EEPROM interrupt mask
00088     EECR &= ~(1<<EERIE);                    // Disable EEPROM interrupt
00089     while(EECR & (1<<EEWE));                // Wait if ongoing EEPROM write
00090 
00091     pageAdr=flashAddr & ~(PAGESIZE-1);      // Gets Flash page address from byte address
00092 
00093     #ifdef __FLASH_RECOVER
00094     FlashBackup.status=0;                   // Inicate that Flash buffer does
00095                                             // not contain data for writing
00096     while(EECR & (1<<EEWE));
00097     LpmReplaceSpm(flashAddr, data);         // Fills Flash write buffer
00098     WriteBufToFlash(ADR_FLASH_BUFFER);      // Writes to Flash recovery buffer
00099     FlashBackup.pageNumber = (unsigned int) (pageAdr/PAGESIZE); // Stores page address
00100                                                        // data should be written to
00101     FlashBackup.status = FLASH_BUFFER_FULL_ID; // Indicates that Flash recovery buffer
00102                                                // contains unwritten data
00103     while(EECR & (1<<EEWE));
00104     #endif
00105 
00106     LpmReplaceSpm(flashAddr, data);         // Fills Flash write buffer
00107     WriteBufToFlash(pageAdr);               // Writes to Flash
00108 
00109     #ifdef __FLASH_RECOVER
00110     FlashBackup.status = 0;                 // Indicates that Flash recovery buffer
00111                                             // does not contain unwritten data
00112     while(EECR & (1<<EEWE));
00113     #endif
00114 
00115     EECR |= eepromInterruptSettings;        // Restore EEPROM interrupt mask
00116     return TRUE;                            // Return TRUE if address
00117                                             // valid for writing
00118   }
00119   else
00120     return FALSE;                           // Return FALSE if address not
00121                                             // valid for writing
00122 }

Here is the call graph for this function:

unsigned char WriteFlashPage MyAddressType  flashStartAdr,
unsigned char *  dataPage
 

The function writes data from array dataPage[] to Flash page address flashStartAdr. The Number of bytes written is depending upon the Flash page size. Returns FALSE if input argument ucFlashStartAdr is not a valid Flash page address for writing, else TRUE.

Definition at line 130 of file Self_programming.c.

References AddressCheck(), ADR_FLASH_BUFFER, FLASH_BUFFER_FULL_ID, FlashBackup, TRUE, and WriteBufToFlash().

00131 {
00132   unsigned int index;
00133   unsigned char eepromInterruptSettings;
00134   if( AddressCheck(flashStartAdr) ){
00135     eepromInterruptSettings = EECR & (1<<EERIE); // Stoes EEPROM interrupt mask
00136     EECR &= ~(1<<EERIE);                    // Disable EEPROM interrupt
00137     while(EECR & (1<<EEWE));                // Wait if ongoing EEPROM write
00138 
00139     #ifdef __FLASH_RECOVER
00140     FlashBackup.status=0;                   // Inicate that Flash buffer does
00141                                             // not contain data for writing
00142     while(EECR & (1<<EEWE));
00143     for(index = 0; index < PAGESIZE; index+=2){ // Fills Flash write buffer
00144       _SPM_FILLTEMP(index, (unsigned int)dataPage[index]+((unsigned int)dataPage[index+1] << 8));
00145     }
00146     WriteBufToFlash(ADR_FLASH_BUFFER);      // Writes to Flash recovery buffer
00147     FlashBackup.pageNumber=(unsigned int)(flashStartAdr/PAGESIZE);
00148     FlashBackup.status = FLASH_BUFFER_FULL_ID; // Indicates that Flash recovery buffer
00149                                            // contains unwritten data
00150     while(EECR & (1<<EEWE));
00151     #endif
00152     for(index = 0; index < PAGESIZE; index+=2){ // Fills Flash write buffer
00153       _SPM_FILLTEMP(index, (unsigned int)dataPage[index]+((unsigned int)dataPage[index+1] << 8));
00154     }
00155     WriteBufToFlash(flashStartAdr);         // Writes to Flash
00156     #ifdef __FLASH_RECOVER
00157       FlashBackup.status=0;                 // Inicate that Flash buffer does
00158                                             // not contain data for writing
00159       while(EECR & (1<<EEWE));
00160     #endif
00161 
00162     EECR |= eepromInterruptSettings;        // Restore EEPROM interrupt mask
00163     return TRUE;                            // Return TRUE if address
00164                                             // valid for writing
00165   }
00166   else
00167     return FALSE;                           // Return FALSE if not address not
00168                                             // valid for writing
00169 }

Here is the call graph for this function:


Generated on Wed Jan 18 16:19:25 2006 for AVR106 - C Functions for Reading and Writing to Flash Memory by  doxygen 1.4.5