Xmega Application Note


eeprom_driver.c File Reference


Detailed Description

XMEGA EEPROM driver source file.

This file contains the function implementations for the XMEGA EEPROM driver.

The driver is not intended for size and/or speed critical code, since most functions are just a few lines of code, and the function call overhead would decrease code performance. The driver is intended for rapid prototyping and documentation purposes for getting started with the XMEGA EEPROM module.

For size and/or speed critical code, it is recommended to copy the function contents directly into your application instead of making a function call.

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_driver.c.

#include "eeprom_driver.h"

Include dependency graph for eeprom_driver.c:

Go to the source code of this file.

Functions

void EEPROM_AtomicWritePage (uint8_t pageAddr)
 Write already loaded page into EEPROM.
void EEPROM_EraseAll (void)
 Erase entire EEPROM memory.
void EEPROM_ErasePage (uint8_t pageAddr)
 Erase EEPROM page.
void EEPROM_FlushBuffer (void)
 Flush temporary EEPROM page buffer.
void EEPROM_LoadByte (uint8_t byteAddr, uint8_t value)
 Load single byte into temporary page buffer.
void EEPROM_LoadPage (const uint8_t *values)
 Load entire page into temporary EEPROM page buffer.
uint8_t EEPROM_ReadByte (uint8_t pageAddr, uint8_t byteAddr)
 Read one byte from EEPROM using IO mapping.
void EEPROM_SplitWritePage (uint8_t pageAddr)
 Write (without erasing) EEPROM page.
void EEPROM_WaitForNVM (void)
 Wait for any NVM access to finish, including EEPROM.
void EEPROM_WriteByte (uint8_t pageAddr, uint8_t byteAddr, uint8_t value)
 Write one byte to EEPROM using IO mapping.


Function Documentation

void EEPROM_AtomicWritePage ( uint8_t  pageAddr  ) 

Write already loaded page into EEPROM.

This function writes the contents of an already loaded EEPROM page buffer into EEPROM memory.

As this is an atomic write, the page in EEPROM will be erased automatically before writing. Note that only the page buffer locations that have been loaded will be used when writing to EEPROM. Page buffer locations that have not been loaded will be left untouched in EEPROM.

Parameters:
pageAddr EEPROM Page address, between 0 and EEPROM_SIZE/EEPROM_PAGESIZE

Definition at line 245 of file eeprom_driver.c.

References EEPROM_PAGESIZE, EEPROM_WaitForNVM(), and NVM_EXEC.

Referenced by main().

00246 {
00247         /* Wait until NVM is not busy. */
00248         EEPROM_WaitForNVM();
00249 
00250         /* Calculate page address */
00251         uint16_t address = (uint16_t)(pageAddr*EEPROM_PAGESIZE);
00252 
00253         /* Set address. */
00254         NVM.ADDR0 = address & 0xFF;
00255         NVM.ADDR1 = (address >> 8) & 0x1F;
00256         NVM.ADDR2 = 0x00;
00257 
00258         /* Issue EEPROM Atomic Write (Erase&Write) command. */
00259         NVM.CMD = NVM_CMD_ERASE_WRITE_EEPROM_PAGE_gc;
00260         NVM_EXEC();
00261 }

Here is the call graph for this function:

void EEPROM_EraseAll ( void   ) 

Erase entire EEPROM memory.

This function erases the entire EEPROM memory block to 0xFF.

Definition at line 321 of file eeprom_driver.c.

References EEPROM_WaitForNVM(), and NVM_EXEC.

00322 {
00323         /* Wait until NVM is not busy. */
00324         EEPROM_WaitForNVM();
00325 
00326         /* Issue EEPROM Erase All command. */
00327         NVM.CMD = NVM_CMD_ERASE_EEPROM_gc;
00328         NVM_EXEC();
00329 }

Here is the call graph for this function:

void EEPROM_ErasePage ( uint8_t  pageAddr  ) 

Erase EEPROM page.

This function erases one EEPROM page, so that every location reads 0xFF.

Parameters:
pageAddr EEPROM Page address, between 0 and EEPROM_SIZE/EEPROM_PAGESIZE

Definition at line 270 of file eeprom_driver.c.

References EEPROM_PAGESIZE, EEPROM_WaitForNVM(), and NVM_EXEC.

Referenced by main().

00271 {
00272         /* Wait until NVM is not busy. */
00273         EEPROM_WaitForNVM();
00274 
00275         /* Calculate page address */
00276         uint16_t address = (uint16_t)(pageAddr*EEPROM_PAGESIZE);
00277 
00278         /* Set address. */
00279         NVM.ADDR0 = address & 0xFF;
00280         NVM.ADDR1 = (address >> 8) & 0x1F;
00281         NVM.ADDR2 = 0x00;
00282 
00283         /* Issue EEPROM Erase command. */
00284         NVM.CMD = NVM_CMD_ERASE_EEPROM_PAGE_gc;
00285         NVM_EXEC();
00286 }

Here is the call graph for this function:

void EEPROM_FlushBuffer ( void   ) 

Flush temporary EEPROM page buffer.

This function flushes the EEPROM page buffers. This function will cancel any ongoing EEPROM page buffer loading operations, if any. This function also works for memory mapped EEPROM access.

Note:
The EEPROM write operations will automatically flush the buffer for you.

Definition at line 155 of file eeprom_driver.c.

References EEPROM_WaitForNVM(), and NVM_EXEC.

Referenced by EEPROM_WriteByte(), and main().

00156 {
00157         /* Wait until NVM is not busy. */
00158         EEPROM_WaitForNVM();
00159 
00160         /* Flush EEPROM page buffer if necessary. */
00161         if ((NVM.STATUS & NVM_EELOAD_bm) != 0) {
00162                 NVM.CMD = NVM_CMD_ERASE_EEPROM_BUFFER_gc;
00163                 NVM_EXEC();
00164         }
00165 }

Here is the call graph for this function:

void EEPROM_LoadByte ( uint8_t  byteAddr,
uint8_t  value 
)

Load single byte into temporary page buffer.

This function loads one byte into the temporary EEPROM page buffers. If memory mapped EEPROM is enabled, this function will not work. Make sure that the buffer is flushed before starting to load bytes. Also, if multiple bytes are loaded into the same location, they will be ANDed together, thus 0x55 and 0xAA will result in 0x00 in the buffer.

Note:
Only one page buffer exist, thus only one page can be loaded with data and programmed into one page. If data needs to be written to different pages, the loading and writing needs to be repeated.
Parameters:
byteAddr EEPROM Byte address, between 0 and EEPROM_PAGESIZE.
value Byte value to write to buffer.

Definition at line 183 of file eeprom_driver.c.

References EEPROM_WaitForNVM().

00184 {
00185         /* Wait until NVM is not busy and prepare NVM command.*/
00186         EEPROM_WaitForNVM();
00187         NVM.CMD = NVM_CMD_LOAD_EEPROM_BUFFER_gc;
00188 
00189         /* Set address. */
00190         NVM.ADDR0 = byteAddr & 0xFF;
00191         NVM.ADDR1 = 0x00;
00192         NVM.ADDR2 = 0x00;
00193 
00194         /* Set data, which triggers loading of EEPROM page buffer. */
00195         NVM.DATA0 = value;
00196 }

Here is the call graph for this function:

void EEPROM_LoadPage ( const uint8_t *  values  ) 

Load entire page into temporary EEPROM page buffer.

This function loads an entire EEPROM page from an SRAM buffer to the EEPROM page buffers. If memory mapped EEPROM is enabled, this function will not work. Make sure that the buffer is flushed before starting to load bytes.

Note:
Only the lower part of the address is used to address the buffer. Therefore, no address parameter is needed. In the end, the data is written to the EEPROM page given by the address parameter to the EEPROM write page operation.
Parameters:
values Pointer to SRAM buffer containing an entire page.

Definition at line 213 of file eeprom_driver.c.

References EEPROM_PAGESIZE, and EEPROM_WaitForNVM().

Referenced by main().

00214 {
00215         /* Wait until NVM is not busy. */
00216         EEPROM_WaitForNVM();
00217         NVM.CMD = NVM_CMD_LOAD_EEPROM_BUFFER_gc;
00218 
00219         /*  Set address to zero, as only the lower bits matters. ADDR0 is
00220          *  maintained inside the loop below.
00221          */
00222         NVM.ADDR1 = 0x00;
00223         NVM.ADDR2 = 0x00;
00224 
00225         /* Load multible bytes into page buffer. */
00226         for (uint8_t i = 0; i < EEPROM_PAGESIZE; ++i) {
00227                 NVM.ADDR0 = i;
00228                 NVM.DATA0 = *values;
00229                 ++values;
00230         }
00231 }

Here is the call graph for this function:

uint8_t EEPROM_ReadByte ( uint8_t  pageAddr,
uint8_t  byteAddr 
)

Read one byte from EEPROM using IO mapping.

This function reads one byte from EEPROM using IO-mapped access. If memory mapped EEPROM is enabled, this function will not work.

Parameters:
pageAddr EEPROM Page address, between 0 and EEPROM_SIZE/EEPROM_PAGESIZE
byteAddr EEPROM Byte address, between 0 and EEPROM_PAGESIZE.
Returns:
Byte value read from EEPROM.

Definition at line 110 of file eeprom_driver.c.

References EEPROM_PAGESIZE, EEPROM_WaitForNVM(), and NVM_EXEC.

Referenced by main().

00111 {
00112         /* Wait until NVM is not busy. */
00113         EEPROM_WaitForNVM();
00114 
00115         /* Calculate address */
00116         uint16_t address = (uint16_t)(pageAddr*EEPROM_PAGESIZE)
00117                                     |(byteAddr & (EEPROM_PAGESIZE-1));
00118 
00119         /* Set address to read from. */
00120         NVM.ADDR0 = address & 0xFF;
00121         NVM.ADDR1 = (address >> 8) & 0x1F;
00122         NVM.ADDR2 = 0x00;
00123 
00124         /* Issue EEPROM Read command. */
00125         NVM.CMD = NVM_CMD_READ_EEPROM_gc;
00126         NVM_EXEC();
00127 
00128         return NVM.DATA0;
00129 }

Here is the call graph for this function:

void EEPROM_SplitWritePage ( uint8_t  pageAddr  ) 

Write (without erasing) EEPROM page.

This function writes the contents of an already loaded EEPROM page buffer into EEPROM memory.

As this is a split write, the page in EEPROM will _not_ be erased before writing.

Parameters:
pageAddr EEPROM Page address, between 0 and EEPROM_SIZE/EEPROM_PAGESIZE

Definition at line 299 of file eeprom_driver.c.

References EEPROM_PAGESIZE, EEPROM_WaitForNVM(), and NVM_EXEC.

Referenced by main().

00300 {
00301         /* Wait until NVM is not busy. */
00302         EEPROM_WaitForNVM();
00303 
00304         /* Calculate page address */
00305         uint16_t address = (uint16_t)(pageAddr*EEPROM_PAGESIZE);
00306 
00307         /* Set address. */
00308         NVM.ADDR0 = address & 0xFF;
00309         NVM.ADDR1 = (address >> 8) & 0x1F;
00310         NVM.ADDR2 = 0x00;
00311 
00312         /* Issue EEPROM Split Write command. */
00313         NVM.CMD = NVM_CMD_WRITE_EEPROM_PAGE_gc;
00314         NVM_EXEC();
00315 }

Here is the call graph for this function:

void EEPROM_WaitForNVM ( void   ) 

Wait for any NVM access to finish, including EEPROM.

This function is blcoking and waits for any NVM access to finish, including EEPROM. Use this function before any EEPROM accesses, if you are not certain that any previous operations are finished yet, like an EEPROM write.

Definition at line 139 of file eeprom_driver.c.

Referenced by EEPROM_AtomicWritePage(), EEPROM_EraseAll(), EEPROM_ErasePage(), EEPROM_FlushBuffer(), EEPROM_LoadByte(), EEPROM_LoadPage(), EEPROM_ReadByte(), EEPROM_SplitWritePage(), and main().

00140 {
00141         do {
00142                 /* Block execution while waiting for the NVM to be ready. */
00143         } while ((NVM.STATUS & NVM_NVMBUSY_bm) == NVM_NVMBUSY_bm);
00144 }

void EEPROM_WriteByte ( uint8_t  pageAddr,
uint8_t  byteAddr,
uint8_t  value 
)

Write one byte to EEPROM using IO mapping.

This function writes one byte to EEPROM using IO-mapped access. If memory mapped EEPROM is enabled, this function will not work. This functiom will cancel all ongoing EEPROM page buffer loading operations, if any.

Parameters:
pageAddr EEPROM Page address, between 0 and EEPROM_SIZE/EEPROM_PAGESIZE
byteAddr EEPROM Byte address, between 0 and EEPROM_PAGESIZE.
value Byte value to write to EEPROM.

Definition at line 72 of file eeprom_driver.c.

References EEPROM_FlushBuffer(), EEPROM_PAGESIZE, and NVM_EXEC.

Referenced by main().

00073 {
00074         /*  Flush buffer to make sure no unintetional data is written and load
00075          *  the "Page Load" command into the command register.
00076          */
00077         EEPROM_FlushBuffer();
00078         NVM.CMD = NVM_CMD_LOAD_EEPROM_BUFFER_gc;
00079 
00080         /* Calculate address */
00081         uint16_t address = (uint16_t)(pageAddr*EEPROM_PAGESIZE)
00082                                     |(byteAddr & (EEPROM_PAGESIZE-1));
00083 
00084         /* Set address to write to. */
00085         NVM.ADDR0 = address & 0xFF;
00086         NVM.ADDR1 = (address >> 8) & 0x1F;
00087         NVM.ADDR2 = 0x00;
00088 
00089         /* Load data to write, which triggers the loading of EEPROM page buffer. */
00090         NVM.DATA0 = value;
00091 
00092         /*  Issue EEPROM Atomic Write (Erase&Write) command. Load command, write
00093          *  the protection signature and execute command.
00094          */
00095         NVM.CMD = NVM_CMD_ERASE_WRITE_EEPROM_PAGE_gc;
00096         NVM_EXEC();
00097 }

Here is the call graph for this function:

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