Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00018 #ifndef LIB_EEPROM255_H__
00019 #define LIB_EEPROM255_H__
00020
00021 #include "nrf24le1.h"
00022 #include "hal_flash.h"
00023
00024 #define BK_BYTE 255
00025 #define PAGE_0_XDATA 0xFA00
00026 #define PAGE_1_XDATA 0xFB00
00027 #define PAGE_0_FLASH_PN 32
00028 #define PAGE_1_FLASH_PN 33
00029
00030 void lib_eeprom255_byte_write(uint8_t adr, uint8_t dat)
00031 {
00032 uint8_t xdata i;
00033 uint16_t xdata flash_dst_pn, xdata_dst_adr, flash_old_pn, xdata_old_adr;
00034
00035 if(*((uint8_t xdata *)(PAGE_0_XDATA + BK_BYTE)) == 0xff)
00036 {
00037 flash_dst_pn = PAGE_0_FLASH_PN;
00038 xdata_dst_adr = PAGE_0_XDATA;
00039 flash_old_pn = PAGE_1_FLASH_PN;
00040 xdata_old_adr = PAGE_1_XDATA;
00041 }
00042 else
00043 {
00044 flash_dst_pn = PAGE_1_FLASH_PN;
00045 xdata_dst_adr = PAGE_1_XDATA;
00046 flash_old_pn = PAGE_0_FLASH_PN;
00047 xdata_old_adr = PAGE_0_XDATA;
00048 }
00049
00050 if((*((uint8_t xdata *)(xdata_old_adr + adr))) != dat)
00051 {
00052 hal_flash_page_erase(flash_dst_pn);
00053
00054 PCON &= ~(1 << 4);
00055 for(i = 0; i < 255; i++)
00056 {
00057 if(i == adr)
00058 {
00059 hal_flash_byte_write(xdata_dst_adr + adr, dat);
00060 }
00061 else
00062 {
00063 hal_flash_byte_write(xdata_dst_adr + i, *((uint8_t xdata *)(xdata_old_adr + i)));
00064 }
00065 }
00066
00067 hal_flash_byte_write(xdata_dst_adr + BK_BYTE, 0);
00068 hal_flash_page_erase(flash_old_pn);
00069 }
00070 }
00071
00072 uint8_t lib_eeprom255_byte_read(uint8_t adr)
00073 {
00074 if(*((uint8_t xdata *)(PAGE_0_XDATA + BK_BYTE)) == 0)
00075 {
00076 return (*((uint8_t xdata *)(PAGE_0_XDATA + adr)));
00077 }
00078 else
00079 {
00080 return (*((uint8_t xdata *)(PAGE_1_XDATA + adr)));
00081 }
00082 }
00083
00084 void lib_eeprom255_bytes_write(uint8_t adr, uint8_t *src, uint8_t n)
00085 {
00086 uint8_t xdata i;
00087 uint16_t xdata flash_dst_pn, xdata_dst_adr, flash_old_pn, xdata_old_adr;
00088
00089 if(*((uint8_t xdata *)(PAGE_0_XDATA + BK_BYTE)) == 0xff)
00090 {
00091 flash_dst_pn = PAGE_0_FLASH_PN;
00092 xdata_dst_adr = PAGE_0_XDATA;
00093 flash_old_pn = PAGE_1_FLASH_PN;
00094 xdata_old_adr = PAGE_1_XDATA;
00095 }
00096 else
00097 {
00098 flash_dst_pn = PAGE_1_FLASH_PN;
00099 xdata_dst_adr = PAGE_1_XDATA;
00100 flash_old_pn = PAGE_0_FLASH_PN;
00101 xdata_old_adr = PAGE_0_XDATA;
00102 }
00103
00104 for(i = 0; i < n; i++)
00105 {
00106 if((*((uint8_t xdata *)(xdata_old_adr + adr + i))) != src[i])
00107 {
00108 break;
00109 }
00110 }
00111
00112 if(i < n)
00113 {
00114 hal_flash_page_erase(flash_dst_pn);
00115 for(i = 0; i < 255; i++)
00116 {
00117 if(i >= adr && i < (adr + n))
00118 {
00119 hal_flash_byte_write(xdata_dst_adr + i, src[i - adr]);
00120 }
00121 else
00122 {
00123 hal_flash_byte_write(xdata_dst_adr + i, *((uint8_t xdata *)(xdata_old_adr + i)));
00124 }
00125 }
00126
00127 hal_flash_byte_write(xdata_dst_adr + BK_BYTE, 0);
00128 hal_flash_page_erase(flash_old_pn);
00129 }
00130 }
00131
00132 void lib_eeprom255_bytes_read(uint8_t adr, uint8_t *dst, uint8_t n)
00133 {
00134 uint8_t xdata i;
00135 uint16_t xdata xdata_src_adr;
00136
00137 if(*((uint8_t xdata *)(PAGE_0_XDATA + BK_BYTE)) == 0)
00138 {
00139 xdata_src_adr = PAGE_0_XDATA;
00140 }
00141 else
00142 {
00143 xdata_src_adr = PAGE_1_XDATA;
00144 }
00145
00146 for(i = 0; i < n; i++)
00147 {
00148 dst[i] = *((uint8_t xdata *)(xdata_src_adr + adr + i));
00149 }
00150 }
00151
00152 #endif // LIB_EEPROM255_H__