Xmega Application Note


flash_write_example.c

Go to the documentation of this file.
00001 /* This file has been prepared for Doxygen automatic documentation generation.*/
00051 #include "sp_driver.h"
00052 
00054 #define appTable(__tableIndex)  SP_ReadByte(APPTABLE_SECTION_START + __tableIndex)
00055 
00056 /* Buffers for testing Load/Read Flash Page. */
00057 uint8_t WriteBuffer[FLASH_PAGE_SIZE];
00058 uint8_t ReadBuffer[FLASH_PAGE_SIZE];
00059 
00060 /* Test message to write to the signature row. */
00061 uint8_t message[32] = {"This is the user signature row.."};
00062 
00063 
00072 void EraseAppTablePage(uint8_t pageAddress)
00073 {
00074         /* Calculate actual start address of the page.*/
00075         uint16_t tableAddress = (pageAddress * FLASH_PAGE_SIZE);
00076         
00077         /* Perform page erase. */
00078         SP_EraseApplicationPage(APPTABLE_SECTION_START + tableAddress);
00079 
00080         /* Wait for NVM to finish. */
00081         SP_WaitForSPM();
00082 }
00083 
00084 
00093 void EraseWriteAppTablePage(uint8_t pageAddress)
00094 {
00095         /* Calculate actual start address of the page.*/
00096         uint16_t tableAddress = (pageAddress * FLASH_PAGE_SIZE);
00097         
00098         /* Perform page erase. */
00099         SP_EraseWriteApplicationPage(APPTABLE_SECTION_START + tableAddress);
00100 
00101         /* Wait for NVM to finish. */
00102         SP_WaitForSPM();
00103 }
00104 
00105 
00114 void WriteAppTablePage(uint8_t pageAddress)
00115 {
00116         /* Calculate actual start address of the page.*/
00117         uint16_t tableAddress = (pageAddress * FLASH_PAGE_SIZE);
00118         
00119         /* Perform page write. */
00120         SP_WriteApplicationPage(APPTABLE_SECTION_START + tableAddress);
00121 
00122         /* Wait for NVM to finish. */
00123         SP_WaitForSPM();
00124 }
00125 
00126 
00133 void LoadAppTableWord(uint16_t tableAddress, uint8_t lowByte, uint8_t highByte)
00134 {
00135         /* Perform word load. */
00136         SP_LoadFlashWord(tableAddress, ((uint16_t) highByte << 8) | lowByte);
00137 
00138         /* Wait for NVM to finish. */
00139         SP_WaitForSPM();
00140 }
00141 
00142 
00152 void ReadFlashPage(const uint8_t * data, uint8_t pageAddress)
00153 {
00154         /* Calculate actual start address of the page.*/
00155         uint16_t tableAddress = (pageAddress * FLASH_PAGE_SIZE);
00156         
00157         /* Read the flash page into the buffer. */
00158         SP_ReadFlashPage(data, APPTABLE_SECTION_START + tableAddress);
00159 }
00160 
00161 
00164 int main(void)
00165 {
00166         /* Assume success until proven otherwise. */
00167         bool success = true;
00168         
00169         /* Erase first page. */
00170         EraseAppTablePage(0);
00171 
00172         /* Load 100 bytes. */
00173         for (uint8_t i = 0; i < 100; i += 2) {
00174                 uint8_t lowByte = 0xFF - i;
00175                 uint8_t highByte = 0xFE - i;
00176                 LoadAppTableWord(i, lowByte, highByte);
00177         }
00178 
00179         /* Write page. */
00180         WriteAppTablePage(0);
00181 
00182         /* Verify Flash contents. */
00183         for (uint8_t i = 0; i < 100; i++) {
00184                 if (appTable(i) != (0xFF - i)) {
00185                         success = 0;
00186                         break;
00187                 }
00188         }
00189 
00190         
00191         /*  If success, try with another method using load/read flash page function
00192          *  and an erase-write of the page.
00193          */
00194         if (success) {
00195 
00196                 
00197 
00198                 /* Fill up a test buffer with 512 bytes with other values. */
00199                 for (uint16_t i = 0; i < FLASH_PAGE_SIZE; i++) {
00200                         WriteBuffer[i] = (uint8_t) i;
00201                 }
00202 
00203                 /* Load the flashbuffer with the test buffer. */
00204                 SP_LoadFlashPage(WriteBuffer);
00205 
00206                 /* Do a Erase-Write of the page. */
00207                 EraseWriteAppTablePage(1);
00208 
00209                 /* Read a flashpage into the read buffer. */
00210                 ReadFlashPage(ReadBuffer, 1);
00211 
00212                 /* Verify Flash contents. */
00213                 for (uint16_t i = 0; i < FLASH_PAGE_SIZE; i++) {
00214                         if (ReadBuffer[i] != WriteBuffer[i]){
00215                                 success = 0;
00216                                 break;
00217                         }
00218                 }
00219         }
00220         
00221         /* If success, write a message to the user signature row. */
00222         if (success) {
00223                 
00224                 /* Erase the user signature row. */ 
00225                 SP_EraseUserSignatureRow();
00226                 
00227                 /* Wait for NVM to finish. */
00228                 SP_WaitForSPM();
00229 
00230                 for (uint8_t i = 0; i < 32; i += 2) {
00231                         LoadAppTableWord(i, message[i], message[i+1]);
00232                 }
00233                 
00234                 /* Write the Flash buffer to the user signature row. */ 
00235                 SP_WriteUserSignatureRow();
00236                 
00237                 /* Wait for NVM to finish. */
00238                 SP_WaitForSPM();
00239                 
00240                 /* Verify Flash contents. */
00241                 for (uint8_t i = 0; i < 32; i++) {
00242                         if (message[i] != SP_ReadUserSignatureByte(i)){
00243                                 success = 0;
00244                                 break;
00245                         }
00246                 }
00247 
00248         }
00249 
00250 
00251         /* Allow for breakpoint to check the value of "success". */
00252         if (success) {
00253                 while(true){
00254                         nop();
00255                 }
00256         } else {
00257                 while(true){
00258                         nop();
00259                 }
00260         }
00261 }
00262 
00263 
@DOC_TITLE@
Generated on Tue Jul 29 13:31:05 2008 for AVR1316 XMEGA Self programming by doxygen 1.5.5