00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00036 #include "pic24_all.h"
00037 #include <stdio.h>
00038
00046
00047 void doWriteLatchFlash(uint16 u16_addrhi, uint16 u16_addrlo, uint16 u16_wordhi, uint16 u16_wordlo) {
00048 TBLPAG = u16_addrhi;
00049 __builtin_tblwtl(u16_addrlo,u16_wordlo);
00050 __builtin_tblwth(u16_addrlo,u16_wordhi);
00051 }
00052
00059
00060 uint32 doReadLatchFlash(uint16 u16_addrhi, uint16 u16_addrlo) {
00061 union32 u32_a;
00062 TBLPAG = u16_addrhi;
00063 u32_a.u16.ls16 = __builtin_tblrdl(u16_addrlo);
00064 u32_a.u16.ms16 = __builtin_tblrdh(u16_addrlo);
00065 return(u32_a.u32);
00066 }
00067
00068
00074 void doErasePageFlash (uint16 u16_addrhi, uint16 u16_addrlo) {
00075 uint16 u16_save_SR, u16_save_TBLPAG;
00076
00077
00078 u16_save_SR = SR;
00079 u16_save_TBLPAG = TBLPAG;
00080
00081 SR = SR | 0xE0;
00082
00083 NVMCON = 0x4042;
00084 TBLPAG = u16_addrhi;
00085
00086
00087 asm("tblwtl %0,[%0]"::"r"(u16_addrlo));
00088
00089 __builtin_write_NVM();
00090
00091 SR = u16_save_SR;
00092
00093 while (NVMCON & 0x8000)
00094 doHeartbeat();
00095
00096 TBLPAG = u16_save_TBLPAG;
00097 }
00098
00103 void doWriteRowFlash() {
00104 uint16 u16_save_SR;
00105
00106 u16_save_SR = SR;
00107
00108 SR = SR | 0xE0;
00109
00110 NVMCON = 0x4001;
00111
00112 __builtin_write_NVM();
00113
00114 SR = u16_save_SR;
00115
00116 while (NVMCON & 0x8000)
00117 doHeartbeat();
00118 }
00119
00129 void doWritePageFlash(union32 u32_pmemAddress, uint8* pu8_data, uint16 u16_len) {
00130 uint16 u16_byteCnt;
00131 union32 u32_a;
00132 uint16 u16_ICnt, u16_numInstructions;
00133
00134 ASSERT(u16_len <= FLASH_PAGEBYTES);
00135 doErasePageFlash(u32_pmemAddress.u16.ms16, u32_pmemAddress.u16.ls16);
00136
00137
00138 u16_numInstructions = u16_len/3;
00139 if (u16_len % 3 != 0) u16_numInstructions++;
00140 u16_numInstructions += (u16_numInstructions%64);
00141 for (u16_ICnt = 0, u16_byteCnt=0;u16_ICnt<u16_numInstructions; u16_ICnt += 1,u16_byteCnt += 3) {
00142 u32_a.u8[0] = pu8_data[u16_byteCnt];
00143 u32_a.u8[1] = pu8_data[u16_byteCnt+1];
00144 u32_a.u8[2] = pu8_data[u16_byteCnt+2];
00145 u32_a.u8[3] = 0;
00146 doWriteLatchFlash(u32_pmemAddress.u16.ms16, u32_pmemAddress.u16.ls16,u32_a.u16.ms16,u32_a.u16.ls16);
00147 if ((u16_ICnt+1)%64 == 0) {
00148
00149 doWriteRowFlash();
00150 }
00151 u32_pmemAddress.u32 += 2;
00152 }
00153 }
00154
00162 void doReadPageFlash(union32 u32_pmemAddress, uint8* pu8_data, uint16 u16_len) {
00163 uint16 u16_byteCnt;
00164 union32 u32_a;
00165
00166 ASSERT(u16_len <= FLASH_PAGEBYTES);
00167 for (u16_byteCnt=0;u16_byteCnt<u16_len;u16_byteCnt += 3) {
00168 u32_a = (union32) doReadLatchFlash(u32_pmemAddress.u16.ms16, u32_pmemAddress.u16.ls16);
00169 pu8_data[u16_byteCnt] = u32_a.u8[0];
00170 pu8_data[u16_byteCnt+1] = u32_a.u8[1];
00171 pu8_data[u16_byteCnt+2] = u32_a.u8[2];
00172 u32_pmemAddress.u32 += 2;
00173 }
00174 }