• Main Page
  • Modules
  • Index
  • File List
  • Globals

lib/eeprom/lib_eeprom.c

Go to the documentation of this file.
00001 /* Copyright (c) 2008 Nordic Semiconductor. All Rights Reserved.
00002  *
00003  * The information contained herein is property of Nordic Semiconductor ASA.
00004  * Terms and conditions of usage are described in detail in NORDIC
00005  * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT. 
00006  *
00007  * Licensees are granted free, non-transferable use of the information. NO
00008  * WARRENTY of ANY KIND is provided. This heading must NOT be removed from
00009  * the file.
00010  *
00011  * $LastChangedRevision: 133 $
00012  */ 
00013 
00018 #include "hal_flash.h"
00019 #include "lib_eeprom.h"
00020 
00021 #define EEPROM_SIZE         16
00022 
00023 #define EEPROM_PAGES (HAL_FLASH_PAGE_SIZE/EEPROM_SIZE)
00024 
00025 static uint8_t xdata *fa;
00026 static uint8_t eeprom_buf[EEPROM_SIZE];
00027 
00028 uint8_t xdata *get_eeprom_address(uint8_t a)
00029 {
00030     uint8_t i;
00031     uint8_t xdata *p;
00032 
00033     fa = (uint8_t xdata *)HAL_DATA_NV_BASE_ADDRESS;
00034     p = (uint8_t xdata *)(HAL_DATA_NV_BASE_ADDRESS + EEPROM_SIZE + a);
00035     for(i=0;i<EEPROM_PAGES;)
00036     {
00037         if (*fa == 0xff)
00038             break;
00039         i++;
00040         p = (uint8_t xdata *)(p + EEPROM_SIZE);
00041         if (*fa == 0xf0)
00042             break;
00043         i++;
00044         p = (uint8_t xdata *)(p + EEPROM_SIZE);
00045         fa++;
00046     }
00047     return p;
00048 }
00049 
00050 void lib_eeprom_byte_write(uint8_t adr, uint8_t dat)
00051 {
00052     uint8_t i;
00053     uint8_t xdata *ea = get_eeprom_address(adr);
00054     if (dat == *ea)
00055         return;
00056     if (*ea != 0xff)
00057     {
00058         ea -= adr;
00059         for(i=0;i<EEPROM_SIZE;i++)
00060             eeprom_buf[i] = *ea++;
00061         eeprom_buf[adr] = dat;
00062         if (ea == (HAL_DATA_NV_BASE_ADDRESS + HAL_FLASH_PAGE_SIZE))
00063         {
00064           hal_flash_page_erase(HAL_DATA_NV_FLASH_PN0);
00065          
00066           if(HAL_DATA_NV_FLASH_PAGES == 2)
00067           {
00068             hal_flash_page_erase(HAL_DATA_NV_FLASH_PN1);
00069           }
00070              
00071           ea = (uint8_t xdata *)(HAL_DATA_NV_BASE_ADDRESS + EEPROM_SIZE);
00072         }
00073         else
00074         {
00075             if (*fa == 0x00)
00076                 fa++;
00077             if (*fa == 0xff)
00078                 hal_flash_byte_write((uint16_t)fa, 0xf0);
00079             else
00080                 hal_flash_byte_write((uint16_t)fa, 0x00);
00081         }
00082         for(i=0;i<EEPROM_SIZE;i++)
00083         {
00084             hal_flash_byte_write((uint16_t)ea, eeprom_buf[i]);
00085             ea++;
00086         }
00087     }
00088     else
00089     {
00090         // When the byte at current location is 0xff we write the
00091         // byte directly to the flash:
00092         hal_flash_byte_write((uint16_t)ea, dat);
00093     }
00094 }
00095 
00096 void lib_eeprom_bytes_write(uint8_t adr, uint8_t *p, uint8_t n)
00097 {
00098     while(n--)
00099         lib_eeprom_byte_write(adr++, *p++);
00100 }
00101 
00102 uint8_t lib_eeprom_byte_read(uint8_t adr)
00103 {
00104     uint8_t xdata *fa = get_eeprom_address(adr);
00105     return *fa;
00106 }
00107 
00108 void lib_eeprom_bytes_read(uint8_t adr, uint8_t *p, uint8_t n)
00109 {
00110     uint8_t xdata *fa = get_eeprom_address(adr);
00111     while(n--)
00112     {
00113         *p++ = *fa++;
00114     }
00115 }

Generated on Fri Apr 20 2012 14:11:45 for nRFGo SDK by  doxygen 1.7.2