00001
00016
00017 #include "config.h"
00018 #include "flash_lib.h"
00019 #include "flash_drv.h"
00020
00021
00022 #ifndef FLASH_SIZE
00023 #error You must define FLASH_SIZE in bytes in config.h
00024 #endif
00025
00026
00027
00028
00029
00030
00031
00032
00033 void flash_wr_byte(Uint32 addr_byte, Uchar value)
00034 {
00035 Enable_flash();
00036 flash_wr_block(&value, addr_byte, 1);
00037 Disable_flash();
00038 }
00039
00040
00041
00042 Uchar flash_wr_block(Byte _MemType_* src, Uint32 dst, Uchar n)
00043 {
00044 Uint16 nb_byte, temp16,temp1,temp2;
00045 Uint32 address, dst_save;;
00046
00047
00048 if (!n) return (TRUE);
00049 Enable_flash();
00050 do
00051 {
00052 dst_save = dst;
00053 if ((dst%FLASH_PAGE_SIZE)==0)
00054 {
00055 for (nb_byte=0; (nb_byte<n)&&(nb_byte<FLASH_PAGE_SIZE)&&((nb_byte+2)<=n); nb_byte+=2,src+=2,dst+=2)
00056 {
00057 flash_fill_temp_buffer(htons(*(Uint16 *)src),dst);
00058 }
00059 address=dst;
00060
00061 if (n&0x01)
00062 {
00063 temp1 = (*(Uchar*)src)<<8;
00064 temp2 = flash_rd_byte((Uchar code*)address+1);
00065 temp16= temp1+temp2;
00066 flash_fill_temp_buffer(temp16, dst);
00067 nb_byte+=2;
00068 address+=2;
00069 src++;
00070 }
00071
00072
00073 if (n>nb_byte)
00074 {
00075 n-=nb_byte;
00076 }
00077 else
00078 {n = 0;}
00079
00080 for (; nb_byte<FLASH_PAGE_SIZE; nb_byte+=2,address+=2)
00081 {
00082 flash_fill_temp_buffer(flash_rd_word((Uint16 code*)address),address);
00083 }
00084 Flash_page_erase(dst_save);
00085 Flash_page_write(dst_save);
00086
00087 }else{
00088
00089 nb_byte = LOW(dst)%FLASH_PAGE_SIZE;
00090 address = dst-nb_byte;
00091 for (nb_byte =0; nb_byte<(LOW(dst)%FLASH_PAGE_SIZE)&&((nb_byte+2)<= LOW(dst));nb_byte+=2, address+=2)
00092 {
00093 flash_fill_temp_buffer(flash_rd_word((Uint16 code*)address),address);
00094 }
00095
00096 if (LOW(dst)&0x01)
00097 {
00098 temp16 = flash_rd_byte((Uchar code*)address)<<8;
00099 temp16 |= *(Uchar*) (src);
00100 flash_fill_temp_buffer(temp16, address);
00101 nb_byte+=2;
00102 address+=2;
00103 src++;
00104 }
00105
00106 temp16 = nb_byte;
00107
00108 for (; (nb_byte<(LOW(dst)+n))&&(nb_byte<FLASH_PAGE_SIZE)&&((nb_byte+2)<=(LOW(dst)+n));nb_byte+=2,src+=2,address+=2)
00109 {
00110 flash_fill_temp_buffer(htons(*(Uint16 *)src),address);
00111 }
00112 if (nb_byte < FLASH_PAGE_SIZE)
00113 {
00114 if (((LOW(dst)&0x01)&&!(n&0x01))||(!(LOW(dst)&0x01)&&(n&0x01)))
00115 {
00116 temp16 = *(Uchar*) (src)<<8;
00117 temp16 |= flash_rd_byte((Uchar code*)address+1);
00118 flash_fill_temp_buffer(temp16, address);
00119 address+=2;
00120 nb_byte+=2;
00121 }
00122 }
00123 if (n>nb_byte)
00124 {
00125 n-=nb_byte;
00126 }
00127 else if (nb_byte==FLASH_PAGE_SIZE)
00128 {
00129 if ((nb_byte%dst)>n){ n =0;}
00130 else {n -=(nb_byte%dst);}
00131 dst +=(nb_byte%dst);
00132 }else
00133 {n = 0;}
00134
00135 for (; nb_byte<FLASH_PAGE_SIZE; nb_byte+=2,address+=2)
00136 {
00137 flash_fill_temp_buffer(flash_rd_word((Uint16 code*)address),address);
00138 }
00139
00140 Flash_page_erase(dst_save);
00141 Flash_page_write(dst_save);
00142 }
00143 }while(n);
00144 Disable_flash();
00145 return TRUE;
00146 }
00147
00148 void flash_erase(void)
00149 {
00150 #if FLASH_SIZE > 0
00151 Uint32 nb_page;
00152
00153 Enable_flash();
00154 nb_page=0;
00155 do {
00156 Flash_page_erase(nb_page);
00157 nb_page += FLASH_PAGE_SIZE;
00158 } while (nb_page<(FLASH_SIZE-BOOT_SIZE));
00159 Flash_RWW_Read_enable();
00160 Disable_flash();
00161 #endif
00162
00163 }
00164
00165 Uchar flash_rd_byte(Uchar code* addr)
00166 {
00167 unsigned char temp;
00168
00169 Enable_flash();
00170 temp = *addr;
00171 Disable_flash();
00172 return temp;
00173 }
00174
00175
00176 Uint16 flash_rd_word(Uint16 code* addr)
00177 {
00178 Union16 temp;
00179
00180 Enable_flash();
00181 temp.b[1] = flash_rd_byte ((Uchar code*) addr);
00182 temp.b[0] = flash_rd_byte ((Uchar code*)addr+1)
00183 Disable_flash();
00184
00185 return temp.w;
00186 }
00187