00001
00028
00029 #include <string.h>
00030 #include "roboot.h"
00031
00032 #include "71x_lib.h"
00033 #include "7xx_flash.h"
00034
00035 #define FLASH_ERROR 1
00036
00037 #if defined(BOOTLOADER_USES_SECTOR_1) && (BOOTLOADER_USES_SECTOR_1 != 0)
00038 #define FLASH_PROTECT_MASK (FLASH_BANK0_SECTOR2 | FLASH_BANK0_SECTOR3 | FLASH_BANK0_SECTOR3 | FLASH_BANK0_SECTOR4 | FLASH_BANK0_SECTOR5 | FLASH_BANK0_SECTOR6 | FLASH_BANK0_SECTOR7)
00039 #else
00040 #define FLASH_PROTECT_MASK (FLASH_BANK0_SECTOR1 | FLASH_BANK0_SECTOR2 | FLASH_BANK0_SECTOR3 | FLASH_BANK0_SECTOR3 | FLASH_BANK0_SECTOR4 | FLASH_BANK0_SECTOR5 | FLASH_BANK0_SECTOR6 | FLASH_BANK0_SECTOR7)
00041 #endif
00042
00043 static uint32_t FlashUnlock(uint32_t page_address);
00044 static void FlashLock(uint32_t page_address);
00045 static uint32_t FlashErasePage(uint32_t page_address);
00046 static uint32_t FlashWriteData(uint32_t write_addr, const uint8_t *chunk, uint32_t chunk_len);
00047
00048 const ROBOOT_FLASH_Operations_T flash_oper = { FlashUnlock, FlashLock, FlashErasePage, FlashWriteData };
00049
00050
00051
00052
00053 static uint32_t flash_unlock_and_wait(void)
00054 {
00055 FLASH_WriteProtectionCmd(FLASH_PROTECT_MASK, DISABLE);
00056 FLASH_WaitForLastOperation();
00057 return FLASH_GetFlagStatus();
00058 }
00059
00060
00061
00062 static void flash_lock_and_wait(void)
00063 {
00064 FLASH_WriteProtectionCmd(FLASH_PROTECT_MASK, ENABLE);
00065
00066 FLASH_WaitForLastOperation();
00067 }
00068
00069
00070
00071 static uint32_t flash_erase_and_wait(uint32_t page_address)
00072 {
00073 u32 FLASH_Sector;
00074
00075 switch (page_address)
00076 {
00077 #if 0
00078 case 0x40000000:
00079 FLASH_Sector = FLASH_BANK0_SECTOR0;
00080 break;
00081 #endif
00082
00083 #if !defined(BOOTLOADER_USES_SECTOR_1) || (BOOTLOADER_USES_SECTOR_1 == 0)
00084 case 0x40002000:
00085 FLASH_Sector = FLASH_BANK0_SECTOR1;
00086 break;
00087 #endif
00088
00089 case 0x40004000:
00090 FLASH_Sector = FLASH_BANK0_SECTOR2;
00091 break;
00092
00093 case 0x40006000:
00094 FLASH_Sector = FLASH_BANK0_SECTOR3;
00095 break;
00096
00097 case 0x40008000:
00098 FLASH_Sector = FLASH_BANK0_SECTOR4;
00099 break;
00100
00101 case 0x40010000:
00102 FLASH_Sector = FLASH_BANK0_SECTOR5;
00103 break;
00104
00105 case 0x40020000:
00106 FLASH_Sector = FLASH_BANK0_SECTOR6;
00107 break;
00108
00109 case 0x40030000:
00110 FLASH_Sector = FLASH_BANK0_SECTOR7;
00111 break;
00112
00113 default:
00114 return FLASH_ERROR;
00115 }
00116
00117 FLASH_EraseSector(FLASH_Sector);
00118 FLASH_WaitForLastOperation();
00119
00120 return FLASH_GetFlagStatus();
00121 }
00122
00123
00124
00125 static uint32_t flash_write_and_wait(uint32_t prg_addr, uint32_t word)
00126 {
00127 FLASH_WriteWord(prg_addr, word);
00128 FLASH_WaitForLastOperation();
00129 return FLASH_GetFlagStatus();
00130 }
00131
00132
00133
00134 static uint32_t FlashUnlock(uint32_t page_address)
00135 {
00136 (void) page_address;
00137
00138 return flash_unlock_and_wait();
00139 }
00140
00141
00142
00143 static void FlashLock(uint32_t page_address)
00144 {
00145 (void) page_address;
00146
00147 flash_lock_and_wait();
00148
00149 }
00150
00151
00152
00153
00154 static uint32_t FlashErasePage(uint32_t page_address)
00155 {
00156 return flash_erase_and_wait(page_address);
00157 }
00158
00159
00160
00161
00162 static uint32_t FlashWriteData(uint32_t write_addr, const uint8_t *chunk, uint32_t chunk_len)
00163 {
00164 uint32_t status;
00165 const uint32_t *data;
00166 uint32_t data_len;
00167 uint32_t prg_addr;
00168
00169 data = (const uint32_t*)chunk;
00170 data_len = (chunk_len + 3UL) / 4UL;
00171 prg_addr = write_addr;
00172
00173 while (data_len)
00174 {
00175 status = flash_write_and_wait(prg_addr, *data);
00176
00177 if (status)
00178 {
00179 return status;
00180 }
00181 else
00182 {
00183 data++;
00184 prg_addr += 4UL;
00185 data_len--;
00186 }
00187 }
00188
00189 return memcmp((const void*) write_addr, chunk, chunk_len);
00190 }