Xmega Application Note


flash_write_example.c File Reference

Writing EEPROM and Flash in ATxmega256A3 / ATxmega256A3B - Rev B. More...

#include "sp_driver.h"

Go to the source code of this file.

Defines

#define APP_END   0x40000
#define FLASH_PAGE_SIZE   512

Functions

void ClearFlashBuffer (void)
void EraseApplicationPage (uint32_t address)
void EraseWriteApplicationPage (uint32_t address)
 ISR (NVM_SPM_vect)
void LoadFlashWord (uint32_t address, uint16_t word)
int main (void)
 Example to show how to read and write to the flash.
void PMIC_SetVectorLocationToApplication (void)
void PMIC_SetVectorLocationToBoot (void)
void Prepare_to_Sleep (void)

Variables

uint8_t globalInt
uint8_t pmicStore
uint8_t sleepCtr
uint8_t spmintStore
uint8_t statusStore


Detailed Description

Writing EEPROM and Flash in ATxmega256A3 / ATxmega256A3B - Rev B.

This file contains an example application that demonstrates Self-programming for ATxmega256A3 / ATxmega256A3B - Rev B.

Application note:
AVR1008: Writing EEPROM and Flash in ATxmega256A3 / ATxmega256A3B - Rev B
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
2740
Date
2009-09-02 10:43:01 +0200 (on, 02 sep 2009)

Copyright (c) 2009, 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.


Define Documentation

#define APP_END   0x40000

Definition at line 51 of file flash_write_example.c.

Referenced by main().

#define FLASH_PAGE_SIZE   512

Definition at line 52 of file flash_write_example.c.

Referenced by main().


Function Documentation

void ClearFlashBuffer ( void   ) 

Definition at line 133 of file flash_write_example.c.

References Prepare_to_Sleep(), and SP_EraseFlashBuffer().

Referenced by main().

00134 {
00135         /*Set the correct settings and store critical registers before NVM-workaround*/
00136         Prepare_to_Sleep(); 
00137         /*Assembly "function" to erase flash buffer*/
00138         SP_EraseFlashBuffer();
00139 }

void EraseApplicationPage ( uint32_t  address  ) 

Definition at line 117 of file flash_write_example.c.

References Prepare_to_Sleep(), and SP_EraseApplicationPage().

00118 {
00119         /*Set the correct settings and store critical registers before NVM-workaround*/
00120         Prepare_to_Sleep();
00121         /*Assembly "function" to preform page erase*/
00122         SP_EraseApplicationPage(address);
00123 }

void EraseWriteApplicationPage ( uint32_t  address  ) 

Definition at line 125 of file flash_write_example.c.

References Prepare_to_Sleep(), and SP_EraseWriteApplicationPage().

Referenced by main().

00126 {
00127         /*Set the correct settings and store critical registers before NVM-workaround*/
00128         Prepare_to_Sleep(); 
00129         /*Assembly "function" to preform page erase-write*/
00130         SP_EraseWriteApplicationPage(address);
00131 }

ISR ( NVM_SPM_vect   ) 

Definition at line 65 of file flash_write_example.c.

References globalInt, pmicStore, sleepCtr, spmintStore, and statusStore.

00066 {
00067         /* Disable the SPM interrupt */
00068         NVM.INTCTRL = (NVM.INTCTRL & ~NVM_SPMLVL_gm);
00069         /* Restore sleep settings */
00070         SLEEP.CTRL = sleepCtr;
00071         /* Restore PMIC status and control registers */
00072         PMIC.STATUS = statusStore;
00073         PMIC.CTRL = pmicStore;
00074         /* Restore SPM interruptsettings */
00075         NVM.INTCTRL = spmintStore;
00076         /* Restore global interrupt settings */
00077         SREG = globalInt;
00078         return;
00079 }

void LoadFlashWord ( uint32_t  address,
uint16_t  word 
)

Definition at line 141 of file flash_write_example.c.

References Prepare_to_Sleep(), and SP_LoadFlashWord().

Referenced by main().

00142 {
00143         /*Set the correct settings and store critical registers before NVM-workaround*/
00144         Prepare_to_Sleep();   
00145         /*Assembly "function" to load flash buffer*/
00146         SP_LoadFlashWord(address, word);
00147 }

int main ( void   ) 

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

Definition at line 152 of file flash_write_example.c.

References APP_END, ClearFlashBuffer(), EraseWriteApplicationPage(), FLASH_PAGE_SIZE, LoadFlashWord(), PMIC_SetVectorLocationToBoot(), SP_ReadByte(), and SP_WaitForSPM().

00153 {
00154         /* Assume success until proven otherwise. */
00155         bool success = true;
00156         PMIC_SetVectorLocationToBoot();
00157         /* Fill the entire application section of the flash memory with bogus data */
00158         for (uint32_t address = 0; address < APP_END; address+=FLASH_PAGE_SIZE) {
00159         
00160                 /* Load flash buffer with data */
00161                 ClearFlashBuffer(); //Clear the flash buffer first to avoid data corruption
00162                 for (uint16_t i = 0; i < FLASH_PAGE_SIZE; i += 2) {
00163                         uint8_t lowByte = i;
00164                         uint8_t highByte = i+1;
00165                         
00166                         LoadFlashWord((uint32_t)i, ((uint16_t) highByte << 8) | lowByte);
00167                         SP_WaitForSPM();
00168                 }
00169                 
00170                 /* Perform erase-page write. */ 
00171                 EraseWriteApplicationPage(address);
00172         }
00173         
00174         /* Check if data is written correctly */
00175         for (uint32_t address = 0; address < APP_END; address++)
00176         {
00177            if(SP_ReadByte(address) != (uint8_t)address) success = false;
00178         }
00179         
00180         /* Allow for breakpoint to check the value of "success". */
00181         if (success) {
00182                 while(true){
00183                         nop();
00184                 }
00185         } else {
00186                 while(true){
00187                         nop();
00188                 }
00189         }
00190 }

void PMIC_SetVectorLocationToApplication ( void   ) 

Definition at line 90 of file flash_write_example.c.

00091 {
00092         uint8_t temp = PMIC.CTRL & ~PMIC_IVSEL_bm;
00093         CCP = CCP_IOREG_gc;
00094         PMIC.CTRL = temp;
00095 }

void PMIC_SetVectorLocationToBoot ( void   ) 

Definition at line 82 of file flash_write_example.c.

Referenced by main().

00083 {
00084         uint8_t temp = PMIC.CTRL | PMIC_IVSEL_bm;
00085         CCP = CCP_IOREG_gc;
00086         PMIC.CTRL = temp;
00087 }

void Prepare_to_Sleep ( void   ) 

Definition at line 98 of file flash_write_example.c.

References globalInt, pmicStore, sleepCtr, spmintStore, and statusStore.

Referenced by ClearFlashBuffer(), EraseApplicationPage(), EraseWriteApplicationPage(), and LoadFlashWord().

00099 {
00100         sleepCtr = SLEEP.CTRL;
00101         /* Set sleep mode to IDLE */
00102         SLEEP.CTRL =  0x00; 
00103         /* Save the PMIC Status and control registers */
00104         statusStore = PMIC.STATUS;                                                              
00105         pmicStore = PMIC.CTRL;          
00106         /* Enable only the highest level of interrupts */                                                                       
00107         PMIC.CTRL = (PMIC.CTRL & ~PMIC_HILVLEN_bm) | PMIC_HILVLEN_bm;
00108         /* Save SREG for later use */
00109         globalInt = SREG;
00110         /* Enable global interrupts */
00111         sei();
00112         /* Save SPM interrupt settings for later */ 
00113         spmintStore = NVM.INTCTRL;
00114 }


Variable Documentation

uint8_t globalInt

Definition at line 61 of file flash_write_example.c.

Referenced by ISR(), and Prepare_to_Sleep().

uint8_t pmicStore

Definition at line 60 of file flash_write_example.c.

Referenced by ISR(), and Prepare_to_Sleep().

uint8_t sleepCtr

Definition at line 58 of file flash_write_example.c.

Referenced by ISR(), and Prepare_to_Sleep().

uint8_t spmintStore

Definition at line 62 of file flash_write_example.c.

Referenced by ISR(), and Prepare_to_Sleep().

uint8_t statusStore

Definition at line 59 of file flash_write_example.c.

Referenced by ISR(), and Prepare_to_Sleep().

@DOC_TITLE@
Generated on Wed Sep 2 10:55:22 2009 for AVR1008: Writing EEPROM and Flash in ATxmega256A3 / ATxmega256A3B - Rev B by doxygen 1.5.9