Xmega Application Note


flash_write_example.c File Reference


Detailed Description

XMEGA Self-programming driver example source.

This file contains an example application that demonstrates the Self-programming driver. It shows how to program different values into the Application Table Section.

Application note:
AVR1316: XMEGA Self-programming
Documentation
For comprehensive code documentation, supported compilers, compiler settings and supported devices see readme.html
Author:
Atmel Corporation: http://www.atmel.com
Support email: avr@atmel.com
Revision
1691
Date
2008-07-29 13:25:40 +0200 (ti, 29 jul 2008)

Copyright (c) 2008, Atmel Corporation All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. The name of ATMEL may not be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Definition in file flash_write_example.c.

#include "sp_driver.h"

Include dependency graph for flash_write_example.c:

Go to the source code of this file.

Defines

#define appTable(__tableIndex)   SP_ReadByte(APPTABLE_SECTION_START + __tableIndex)

Functions

void EraseAppTablePage (uint8_t pageAddress)
 Function to erase one page in the Application Table Section.
void EraseWriteAppTablePage (uint8_t pageAddress)
 Function to do an atomic erase-write on one page in the Application Table Section.
void LoadAppTableWord (uint16_t tableAddress, uint8_t lowByte, uint8_t highByte)
 Function to load one word into page buffer.
int main (void)
 Example to show how to read and write to the flash.
void ReadFlashPage (const uint8_t *data, uint8_t pageAddress)
 Function to read a flash page.
void WriteAppTablePage (uint8_t pageAddress)
 Function to program one page in the Application Table Section.

Variables

uint8_t message [32] = {"This is the user signature row.."}
uint8_t ReadBuffer [FLASH_PAGE_SIZE]
uint8_t WriteBuffer [FLASH_PAGE_SIZE]


Define Documentation

#define appTable ( __tableIndex   )     SP_ReadByte(APPTABLE_SECTION_START + __tableIndex)

Macro defined to read from the Application Table Section.

Definition at line 54 of file flash_write_example.c.

Referenced by main().


Function Documentation

void EraseAppTablePage ( uint8_t  pageAddress  ) 

Function to erase one page in the Application Table Section.

Note:
The maximum pageAddress must not be exceeded. The maximum number of pages can be found in the datasheet. For the ATxmega128A1, the maximum number of pages in the application table is 16.
Parameters:
pageAddress Page address to the page to erase.

Definition at line 72 of file flash_write_example.c.

References SP_EraseApplicationPage(), and SP_WaitForSPM().

Referenced by main().

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 }

Here is the call graph for this function:

void EraseWriteAppTablePage ( uint8_t  pageAddress  ) 

Function to do an atomic erase-write on one page in the Application Table Section.

Note:
The maximum pageAddress must not be exceeded. The maximum number of pages can be found in the datasheet. For the ATxmega128A1, the maximum number of pages in the application table is 16.
Parameters:
pageAddress Page address to the page to erase/write.

Definition at line 93 of file flash_write_example.c.

References SP_EraseWriteApplicationPage(), and SP_WaitForSPM().

Referenced by main().

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 }

Here is the call graph for this function:

void LoadAppTableWord ( uint16_t  tableAddress,
uint8_t  lowByte,
uint8_t  highByte 
)

Function to load one word into page buffer.

Parameters:
tableAddress Address in buffer to write the word.
lowByte The low byte of the word to load.
highByte The high byte of the word to load.

Definition at line 133 of file flash_write_example.c.

References SP_LoadFlashWord(), and SP_WaitForSPM().

Referenced by main().

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 }

Here is the call graph for this function:

int main ( void   ) 

Example to show how to read and write to the flash.

Definition at line 164 of file flash_write_example.c.

References appTable, EraseAppTablePage(), EraseWriteAppTablePage(), LoadAppTableWord(), message, ReadBuffer, ReadFlashPage(), SP_EraseUserSignatureRow(), SP_LoadFlashPage(), SP_ReadUserSignatureByte(), SP_WaitForSPM(), SP_WriteUserSignatureRow(), WriteAppTablePage(), and WriteBuffer.

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 }

Here is the call graph for this function:

void ReadFlashPage ( const uint8_t *  data,
uint8_t  pageAddress 
)

Function to read a flash page.

Note:
The maximum pageAddress must not be exceeded. The maximum number of pages can be found in the datasheet. For the ATxmega128A1, the maximum number of pages in the application table is 16.
Parameters:
data Pointer to a data buffer to store the data.
pageAddress Page address to read from.

Definition at line 152 of file flash_write_example.c.

References SP_ReadFlashPage().

Referenced by main().

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 }

Here is the call graph for this function:

void WriteAppTablePage ( uint8_t  pageAddress  ) 

Function to program one page in the Application Table Section.

Note:
The maximum pageAddress must not be exceeded. The maximum number of pages can be found in the datasheet. For the ATxmega128A1, the maximum number of pages in the application table is 16.
Parameters:
pageAddress Page address to the page to write.

Definition at line 114 of file flash_write_example.c.

References SP_WaitForSPM(), and SP_WriteApplicationPage().

Referenced by main().

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 }

Here is the call graph for this function:


Variable Documentation

uint8_t message[32] = {"This is the user signature row.."}

Definition at line 61 of file flash_write_example.c.

Referenced by main().

uint8_t ReadBuffer[FLASH_PAGE_SIZE]

Definition at line 58 of file flash_write_example.c.

Referenced by main().

uint8_t WriteBuffer[FLASH_PAGE_SIZE]

Definition at line 57 of file flash_write_example.c.

Referenced by main().

@DOC_TITLE@
Generated on Tue Jul 29 13:31:06 2008 for AVR1316 XMEGA Self programming by doxygen 1.5.5