Xmega Application Note


eeprom_example.c File Reference


Detailed Description

XMEGA EEPROM driver example source.

This file contains an example application that demonstrates the EEPROM driver. It shows how to read and write to EEPROM, both in atomic mode (erase+write) and split mode (erase and/or write). Also, both IO-mapped and memory mapped access is demonstrated.

Application note:
AVR1315: Accessing the XMEGA EEPROM
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
1569
Date
2008-04-22 13:03:43 +0200 (ti, 22 apr 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 eeprom_example.c.

#include "avr_compiler.h"
#include "eeprom_driver.h"

Include dependency graph for eeprom_example.c:

Go to the source code of this file.

Defines

#define TEST_BYTE_1   0x55
#define TEST_BYTE_2   0xAA
#define TEST_BYTE_ADDR_1   0x00
#define TEST_BYTE_ADDR_2   0x08
#define TEST_PAGE_ADDR_1   0
#define TEST_PAGE_ADDR_2   2
#define TEST_PAGE_ADDR_3   5

Functions

int main (void)
 Example code writing and reading the EEPROM with different methods.

Variables

uint8_t testBuffer [EEPROM_PAGESIZE] = {"Accessing Atmel AVR XMEGA EEPROM"}


Define Documentation

#define TEST_BYTE_1   0x55

Definition at line 55 of file eeprom_example.c.

Referenced by main().

#define TEST_BYTE_2   0xAA

Definition at line 56 of file eeprom_example.c.

Referenced by main().

#define TEST_BYTE_ADDR_1   0x00

Definition at line 58 of file eeprom_example.c.

Referenced by main().

#define TEST_BYTE_ADDR_2   0x08

Definition at line 59 of file eeprom_example.c.

Referenced by main().

#define TEST_PAGE_ADDR_1   0

Definition at line 61 of file eeprom_example.c.

Referenced by main().

#define TEST_PAGE_ADDR_2   2

Definition at line 62 of file eeprom_example.c.

Referenced by main().

#define TEST_PAGE_ADDR_3   5

Definition at line 63 of file eeprom_example.c.

Referenced by main().


Function Documentation

int main ( void   ) 

Example code writing and reading the EEPROM with different methods.

This example code show how to read and write the EEPROM while it is both memory mapped and IO mapped.

Definition at line 74 of file eeprom_example.c.

References EEPROM, EEPROM_AtomicWritePage(), EEPROM_DisableMapping, EEPROM_EnableMapping, EEPROM_ErasePage(), EEPROM_FlushBuffer(), EEPROM_LoadPage(), EEPROM_PAGESIZE, EEPROM_ReadByte(), EEPROM_SplitWritePage(), EEPROM_WaitForNVM(), EEPROM_WriteByte(), TEST_BYTE_1, TEST_BYTE_2, TEST_BYTE_ADDR_1, TEST_BYTE_ADDR_2, TEST_PAGE_ADDR_1, TEST_PAGE_ADDR_2, TEST_PAGE_ADDR_3, and testBuffer.

00075 {
00076         /* Assume OK from the start. Set to 0 on first error.*/
00077         bool test_ok = true;
00078 
00079         /* Flush buffer just to be sure when we start.*/
00080         EEPROM_FlushBuffer();
00081 
00082 
00083         /* ***
00084          * Write and read two bytes using IO-mapped access.
00085          * ***/
00086 
00087         EEPROM_DisableMapping();
00088 
00089         /* Write bytes. */
00090         EEPROM_WriteByte(TEST_PAGE_ADDR_1, TEST_BYTE_ADDR_1, TEST_BYTE_1);
00091         EEPROM_WriteByte(TEST_PAGE_ADDR_1, TEST_BYTE_ADDR_2, TEST_BYTE_2);
00092 
00093         /* Read back and check bytes.*/
00094         if (EEPROM_ReadByte(TEST_PAGE_ADDR_1, TEST_BYTE_ADDR_1) != TEST_BYTE_1) {
00095                 test_ok = false;
00096         }
00097 
00098         if (EEPROM_ReadByte(TEST_PAGE_ADDR_1, TEST_BYTE_ADDR_2) != TEST_BYTE_2) {
00099                 test_ok = false;
00100         }
00101 
00102         /* Now write the other way round.*/
00103         EEPROM_WriteByte(TEST_PAGE_ADDR_1, TEST_BYTE_ADDR_1, TEST_BYTE_2);
00104         EEPROM_WriteByte(TEST_PAGE_ADDR_1, TEST_BYTE_ADDR_2, TEST_BYTE_1);
00105 
00106         /* Again, read back and check bytes. */
00107         if (EEPROM_ReadByte(TEST_PAGE_ADDR_1, TEST_BYTE_ADDR_1) != TEST_BYTE_2) {
00108                 test_ok = false;
00109         }
00110 
00111         if (EEPROM_ReadByte(TEST_PAGE_ADDR_1, TEST_BYTE_ADDR_2) != TEST_BYTE_1) {
00112                 test_ok = false;
00113         }
00114 
00115         /* ***
00116          * Do a full page with split operations.
00117          * ***/
00118 
00119         /* Load, erase and write. */
00120         EEPROM_LoadPage(testBuffer);
00121         EEPROM_ErasePage(TEST_PAGE_ADDR_2);
00122         EEPROM_SplitWritePage(TEST_PAGE_ADDR_2);
00123 
00124         /* Read back and check. */
00125         for (uint8_t i = 0; i < EEPROM_PAGESIZE; ++i) {
00126                 if (EEPROM_ReadByte(TEST_PAGE_ADDR_2, i ) != testBuffer[i] ) {
00127                         test_ok = false;
00128                         break;
00129                 }
00130         }
00131 
00132         /* ***
00133          * Now write and read two bytes using memory mapped access.
00134          * ***/
00135 
00136         EEPROM_EnableMapping();
00137 
00138         /* Write bytes.*/
00139         EEPROM_WaitForNVM();
00140         EEPROM(TEST_PAGE_ADDR_1, TEST_BYTE_ADDR_1) = TEST_BYTE_1;
00141         EEPROM_AtomicWritePage(TEST_PAGE_ADDR_1);
00142         EEPROM_WaitForNVM();
00143         EEPROM(TEST_PAGE_ADDR_1, TEST_BYTE_ADDR_2) = TEST_BYTE_2;
00144         EEPROM_AtomicWritePage(TEST_PAGE_ADDR_1);
00145 
00146         /* Read back and check. */
00147         EEPROM_WaitForNVM();
00148         if (EEPROM(TEST_PAGE_ADDR_1, TEST_BYTE_ADDR_1) != TEST_BYTE_1) {
00149                 test_ok = false;
00150         }
00151         if (EEPROM(TEST_PAGE_ADDR_1, TEST_BYTE_ADDR_2) != TEST_BYTE_2) {
00152                 test_ok = false;
00153         }
00154 
00155 
00156         /* ***
00157          * Test the other way round with memory mapped access too.
00158          * ***/
00159 
00160         /* Write bytes. */
00161         EEPROM_WaitForNVM();
00162         EEPROM(TEST_PAGE_ADDR_1, TEST_BYTE_ADDR_1) = TEST_BYTE_2;
00163         EEPROM_AtomicWritePage(TEST_PAGE_ADDR_1);
00164         EEPROM_WaitForNVM();
00165         EEPROM(TEST_PAGE_ADDR_1, TEST_BYTE_ADDR_2) = TEST_BYTE_1;
00166         EEPROM_AtomicWritePage(TEST_PAGE_ADDR_1);
00167 
00168         /* Read back and check. */
00169         EEPROM_WaitForNVM();
00170         if (EEPROM(TEST_PAGE_ADDR_1, TEST_BYTE_ADDR_1) != TEST_BYTE_2) {
00171                 test_ok = false;
00172         }
00173 
00174         if (EEPROM(TEST_PAGE_ADDR_1, TEST_BYTE_ADDR_2) != TEST_BYTE_1) {
00175                 test_ok = false;
00176         }
00177 
00178         /* ***
00179          * Do a full page write with split operations and memory mapped access.
00180          * ***/
00181 
00182         /* Load buffer, erase and write. */
00183         EEPROM_WaitForNVM();
00184         for (uint8_t i = 0; i < EEPROM_PAGESIZE; ++i) {
00185                 EEPROM(TEST_PAGE_ADDR_3, i) = testBuffer[i];
00186         }
00187 
00188         /*  Erase bytes in eeprom page corresponding to page buffer. The page
00189          *  buffer will not be flushed.
00190          */
00191         EEPROM_ErasePage(TEST_PAGE_ADDR_3);
00192 
00193         /*  Split write page buffer to eeprom page. Buffer will be flushed after
00194          *  write operation.
00195          */
00196         EEPROM_SplitWritePage(TEST_PAGE_ADDR_3);
00197 
00198         /* Read back and check. */
00199         EEPROM_WaitForNVM();
00200         for (uint8_t i = 0; i < EEPROM_PAGESIZE; ++i) {
00201                 if (EEPROM(TEST_PAGE_ADDR_3, i) != testBuffer[i] ) {
00202                         test_ok = false;
00203                         break;
00204                 }
00205         }
00206 
00207         /* ***
00208          * Now, check if everything went well.
00209          * ***/
00210         if (true == test_ok) {
00211                 while(1) {
00212                         /* Success. */
00213                         nop();
00214                 }
00215         } else {
00216                 while(1) {
00217                         /* Failure. */
00218                         nop();
00219                 }
00220         }
00221 }

Here is the call graph for this function:


Variable Documentation

uint8_t testBuffer[EEPROM_PAGESIZE] = {"Accessing Atmel AVR XMEGA EEPROM"}

Testbuffer to write into EEPROM.

Definition at line 66 of file eeprom_example.c.

Referenced by main().

@DOC_TITLE@
Generated on Wed Apr 23 08:27:40 2008 for AVR1315 Accessing the XMEGA EEPROM by doxygen 1.5.5