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(uint32_t address, uint8_t *data, uint32_t length, uint32_t 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 = (uint16_t *)address;
104 for (i = 0; i < length; i += 2) {
105 uint16_t currentData = *ptr;
108 if (currentData == newData) {
110 }
else if (currentData == 0xFFFF || newData == 0x0000) {
114 if ((CIB_OB_BOTTOM <= (uint32_t)ptr) && ((uint32_t)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(uint32_t address, uint8_t *data, uint32_t length)
150 uint8_t *ptr = (uint8_t *)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, uint32_t address)
165 FibStatus status = FIB_SUCCESS;
167 eraseOp = FLASH_CTRL_MASSERASE;
168 ptr = (uint32_t *)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 = (uint32_t *)(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 = (uint32_t *)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 (((uint32_t)eraseType) | DO_ERASE),
245 status = fibFlashErase((((uint32_t)eraseType) | DO_ERASE), address);
252 status = halFixedAddressTable.fibFlashErase(
253 (((uint32_t)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 (((uint32_t)eraseType) | DO_VERIFY),
276 status = fibFlashErase((((uint32_t)eraseType) | DO_VERIFY), address);
279 status = halFixedAddressTable.fibFlashErase(
280 (((uint32_t)eraseType) | DO_VERIFY),
283 return fibToStStatus[status];
302 #
if defined(ST_EMU_TEST)
305 status = halFixedAddressTable.fibFlashWrite(address,
310 status = fibFlashWrite(address, (uint8_t *)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, (uint8_t *)data, length);
341 status = halFixedAddressTable.fibFlashWrite(address,
347 return fibToStStatus[status];
360 return ST_ERR_FLASH_PROG_FAIL;
Header for flash for APIs.
#define MFB_MASS_ERASE
Assign numerical value to the type of erasure requested.
StStatus halInternalFlashWrite(uint32_t address, uint16_t *data, uint32_t length)
Writes a block of words to flash.
#define BYTE_0(n)
Returns the low byte of the 32-bit value n as an uint8_t.
#define HIGH_LOW_TO_INT(high, low)
Returns the value built from the two uint8_t values high and low.
StStatus halInternalCibOptionByteWrite(uint8_t byte, uint8_t data)
Writes an option byte to the customer information block.
#define TRUE
An alias for one, used for clarity.
boolean halFlashEraseIsActive(void)
Tells the calling code if a Flash Erase operation is active.
STM32W108 series memory map definitions used by the full hal.
StStatus halInternalFlashErase(uint8_t eraseType, uint32_t address)
Erases a section of flash back to all 0xFFFF.
#define FALSE
An alias for zero, used for clarity.
Definition and description of FIB bootloader shared functions.
#define ATOMIC(blah)
A block of code may be made atomic by wrapping it with this macro.