Xmega Application Note | |||||
#include "avr_compiler.h"
Go to the source code of this file.
Defines | |
#define | EEPROM(_pageAddr, _byteAddr) ((uint8_t *) MAPPED_EEPROM_START)[_pageAddr*EEPROM_PAGESIZE + _byteAddr] |
#define | EEPROM_DisableMapping() ( NVM.CTRLB &= ~NVM_EEMAPEN_bm ) |
Disable EEPROM mapping into data space. | |
#define | EEPROM_DisablePowerReduction() ( NVM.CTRLB &= ~NVM_EPRM_bm ) |
Disable EEPROM block sleep-when-not-used mode. | |
#define | EEPROM_EnableMapping() ( NVM.CTRLB |= NVM_EEMAPEN_bm ) |
Enable EEPROM mapping into data space. | |
#define | EEPROM_EnablePowerReduction() ( NVM.CTRLB |= NVM_EPRM_bm ) |
Enable EEPROM block sleep-when-not-used mode. | |
#define | EEPROM_PAGESIZE 32 |
#define | MAPPED_EEPROM_START 0x1000 |
#define | NVM_EXEC() |
Non-Volatile Memory Execute Command. | |
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 pageAddress) |
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. |
This file contains the function prototypes and enumerator definitions for various configuration parameters 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.
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.h.
#define EEPROM | ( | _pageAddr, | |||
_byteAddr | ) | ((uint8_t *) MAPPED_EEPROM_START)[_pageAddr*EEPROM_PAGESIZE + _byteAddr] |
Definition at line 66 of file eeprom_driver.h.
#define EEPROM_DisableMapping | ( | ) | ( NVM.CTRLB &= ~NVM_EEMAPEN_bm ) |
Disable EEPROM mapping into data space.
This macro disables mapping of EEPROM into data space. IO mapped access is now enabled.
Definition at line 104 of file eeprom_driver.h.
Referenced by BlockLoad(), BlockRead(), and main().
#define EEPROM_DisablePowerReduction | ( | ) | ( NVM.CTRLB &= ~NVM_EPRM_bm ) |
Disable EEPROM block sleep-when-not-used mode.
This macro disables power reduction mode for EEPROM.
Definition at line 86 of file eeprom_driver.h.
#define EEPROM_EnableMapping | ( | ) | ( NVM.CTRLB |= NVM_EEMAPEN_bm ) |
Enable EEPROM mapping into data space.
This macro enables mapping of EEPROM into data space. EEPROM starts at EEPROM_START in data memory. Read access can be done similar to ordinary SRAM access.
Definition at line 97 of file eeprom_driver.h.
#define EEPROM_EnablePowerReduction | ( | ) | ( NVM.CTRLB |= NVM_EPRM_bm ) |
Enable EEPROM block sleep-when-not-used mode.
This macro enables power reduction mode for EEPROM. It means that the EEPROM block is disabled when not used. Note that there will be a penalty of 6 CPU cycles if EEPROM is accessed.
Definition at line 80 of file eeprom_driver.h.
#define EEPROM_PAGESIZE 32 |
Definition at line 65 of file eeprom_driver.h.
Referenced by EEPROM_AtomicWritePage(), EEPROM_ErasePage(), EEPROM_LoadPage(), EEPROM_ReadByte(), EEPROM_SplitWritePage(), and EEPROM_WriteByte().
#define MAPPED_EEPROM_START 0x1000 |
Definition at line 64 of file eeprom_driver.h.
#define NVM_EXEC | ( | ) |
Value:
asm("push r30" "\n\t" \ "push r31" "\n\t" \ "push r16" "\n\t" \ "push r18" "\n\t" \ "ldi r30, 0xCB" "\n\t" \ "ldi r31, 0x01" "\n\t" \ "ldi r16, 0xD8" "\n\t" \ "ldi r18, 0x01" "\n\t" \ "out 0x34, r16" "\n\t" \ "st Z, r18" "\n\t" \ "pop r18" "\n\t" \ "pop r16" "\n\t" \ "pop r31" "\n\t" \ "pop r30" "\n\t" \ )
This macro set the CCP register before setting the CMDEX bit in the NVM.CTRLA register.
Definition at line 114 of file eeprom_driver.h.
Referenced by EEPROM_AtomicWritePage(), EEPROM_EraseAll(), EEPROM_ErasePage(), EEPROM_FlushBuffer(), EEPROM_ReadByte(), EEPROM_SplitWritePage(), and EEPROM_WriteByte().
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.
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.
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 }
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.
Referenced by main().
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 }
void EEPROM_ErasePage | ( | uint8_t | pageAddr | ) |
Erase EEPROM page.
This function erases one EEPROM page, so that every location reads 0xFF.
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.
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 }
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.
Definition at line 155 of file eeprom_driver.c.
References EEPROM_WaitForNVM(), and NVM_EXEC.
Referenced by BlockLoad(), BlockRead(), 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 }
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.
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 }
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.
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 }
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.
pageAddr | EEPROM Page address, between 0 and EEPROM_SIZE/EEPROM_PAGESIZE | |
byteAddr | EEPROM Byte address, between 0 and EEPROM_PAGESIZE. |
Definition at line 110 of file eeprom_driver.c.
References EEPROM_PAGESIZE, EEPROM_WaitForNVM(), and NVM_EXEC.
Referenced by BlockRead(), and 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 }
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.
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.
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 }
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(), and EEPROM_SplitWritePage().
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.
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 BlockLoad(), and 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 }
Generated on Fri Mar 27 14:05:26 2009 for AVR1605: XMEGA BOOTLOADER by ![]() |