11 #include PLATFORM_HEADER
18 #ifdef FLASH_PROGRAMMING_WITH_EMPTY_FIB
23 static const StStatus fibToStStatus[] = {
28 ST_ERR_FLASH_PROG_FAIL,
29 ST_ERR_FLASH_PROG_FAIL,
30 ST_ERR_FLASH_WRITE_INHIBITED,
31 ST_ERR_FLASH_VERIFY_FAILED
40 boolean flashEraseIsActive =
FALSE;
43 return flashEraseIsActive;
50 #if defined(ST_EMU_TEST)
52 static void enableFlitf(
void)
65 FPEC_CLKREQ = FPEC_CLKREQ_FIELD;
68 while( (FPEC_CLKSTAT&FPEC_CLKACK) != FPEC_CLKACK) {}
71 while( (FLASH_STATUS&FLASH_STATUS_FLA_BSY) == FLASH_STATUS_FLA_BSY ) {}
74 static void disableFlitf(
void)
77 while( (FPEC_CLKSTAT&FPEC_CLKBSY) == FPEC_CLKBSY) {}
80 FPEC_CLKREQ = FPEC_CLKREQ_RESET;
85 FLASH_CTRL = FLASH_CTRL_LOCK;
88 static FibStatus fibFlashWrite(int32u address, int8u *data, int32u length, int32u dummy)
92 FibStatus status = FIB_SUCCESS;
94 if ((address & 1) || (length & 1)) {
95 return FIB_ERR_UNALIGNED;
98 if (!((address >= MFB_BOTTOM && address + length <= MFB_TOP + 1)
99 || (address >= CIB_BOTTOM && address + length <= CIB_TOP + 1))) {
100 return FIB_ERR_INVALID_ADDRESS;
103 ptr = (int16u *)address;
104 for (i = 0; i < length; i += 2) {
105 int16u currentData = *ptr;
108 if (currentData == newData) {
110 }
else if (currentData == 0xFFFF || newData == 0x0000) {
114 if ((CIB_OB_BOTTOM <= (int32u)ptr) && ((int32u)ptr <= CIB_OB_TOP)) {
115 FLASH_CTRL = (FLASH_CTRL_OPTWREN | FLASH_CTRL_OPTPROG);
117 FLASH_CTRL = (FLASH_CTRL_OPTWREN | FLASH_CTRL_PROG);
122 while ((FLASH_STATUS & FLASH_STATUS_FLA_BSY) != 0) {}
124 FLASH_STATUS = FLASH_STATUS_EOP;
127 if (FLASH_STATUS & (FLASH_STATUS_WRP_ERR | FLASH_STATUS_PROG_ERR)) {
128 if (FLASH_STATUS & FLASH_STATUS_WRP_ERR) {
129 status = FIB_ERR_WRITE_PROTECTED;
131 status = FIB_ERR_WRITE_FAILED;
133 FLASH_STATUS = FLASH_STATUS_WRP_ERR;
134 FLASH_STATUS = FLASH_STATUS_PROG_ERR;
138 status = FIB_ERR_ERASE_REQUIRED;
147 static FibStatus fibFlashWriteVerify(int32u address, int8u *data, int32u length)
150 int8u *ptr = (int8u *)address;
151 for (i = 0; i < length; i++) {
152 if (*ptr != data[i]) {
153 return FIB_ERR_VERIFY_FAILED;
160 static FibStatus fibFlashErase(FibEraseType eraseType, int32u address)
165 FibStatus status = FIB_SUCCESS;
167 eraseOp = FLASH_CTRL_MASSERASE;
168 ptr = (int32u *)MFB_BOTTOM;
170 }
else if (
BYTE_0(eraseType) == MFB_PAGE_ERASE) {
171 if (address < MFB_BOTTOM || address > MFB_TOP) {
172 return FIB_ERR_INVALID_ADDRESS;
174 eraseOp = FLASH_CTRL_PAGEERASE;
175 ptr = (int32u *)(address & MFB_PAGE_MASK_B);
176 length = MFB_PAGE_SIZE_W;
177 }
else if (
BYTE_0(eraseType) == CIB_ERASE) {
178 eraseOp = FLASH_CTRL_OPTWREN | FLASH_CTRL_OPTERASE;
179 ptr = (int32u *)CIB_BOTTOM;
182 return FIB_ERR_INVALID_TYPE;
184 if ((eraseType & DO_ERASE) != 0) {
186 FLASH_CTRL = eraseOp;
187 if (
BYTE_0(eraseType) == MFB_PAGE_ERASE) {
188 FLASH_ADDR = (address & MFB_PAGE_MASK_B);
190 eraseOp |= FLASH_CTRL_FLA_START;
192 FLASH_CTRL = eraseOp;
194 while ((FLASH_STATUS & FLASH_STATUS_FLA_BSY) != 0) {}
196 FLASH_STATUS = FLASH_STATUS_EOP;
198 if (FLASH_STATUS & FLASH_STATUS_WRP_ERR) {
199 FLASH_STATUS = FLASH_STATUS_WRP_ERR;
200 status = FIB_ERR_WRITE_PROTECTED;
204 if (status == FIB_SUCCESS
205 && (eraseType & DO_VERIFY) != 0) {
207 for (i = 0; i < length; i++) {
208 if (*ptr != 0xFFFFFFFF) {
209 return FIB_ERR_VERIFY_FAILED;
216 #endif // ST_EMU_TEST
218 static boolean verifyFib(
void)
221 return ( (halFixedAddressTable.baseTable.type == FIXED_ADDRESS_TABLE_TYPE) &&
222 ( ( (halFixedAddressTable.baseTable.version & FAT_MAJOR_VERSION_MASK)
224 (halFixedAddressTable.baseTable.version >= 0x0002)
237 flashEraseIsActive =
TRUE;
238 #if defined(ST_EMU_TEST)
241 status = halFixedAddressTable.fibFlashErase(
242 (((int32u)eraseType) | DO_ERASE),
245 status = fibFlashErase((((int32u)eraseType) | DO_ERASE), address);
252 status = halFixedAddressTable.fibFlashErase(
253 (((int32u)eraseType) | DO_ERASE),
263 flashEraseIsActive =
FALSE;
265 if(status!=FIB_SUCCESS) {
266 return fibToStStatus[status];
269 #if defined(ST_EMU_TEST)
272 status = halFixedAddressTable.fibFlashErase(
273 (((int32u)eraseType) | DO_VERIFY),
276 status = fibFlashErase((((int32u)eraseType) | DO_VERIFY), address);
279 status = halFixedAddressTable.fibFlashErase(
280 (((int32u)eraseType) | DO_VERIFY),
283 return fibToStStatus[status];
302 #
if defined(ST_EMU_TEST)
305 status = halFixedAddressTable.fibFlashWrite(address,
310 status = fibFlashWrite(address, (int8u *)data, length, 0);
318 status = halFixedAddressTable.fibFlashWrite(address,
326 if(status!=FIB_SUCCESS) {
327 return fibToStStatus[status];
330 #if defined(ST_EMU_TEST)
333 status = halFixedAddressTable.fibFlashWrite(address,
338 status = fibFlashWriteVerify(address, (int8u *)data, length);
341 status = halFixedAddressTable.fibFlashWrite(address,
347 return fibToStStatus[status];
360 return ST_ERR_FLASH_PROG_FAIL;