cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 Flash Write fail

YJHUNG
Associate II

Due to engineering requirements, the code is divided into two parts: USB DFU, including encrypted signature verification, and the APP code with header signature file. Currently, there is an attempt to rewrite the flash in the USB DFU orange section from the APP code, but it has not been successful. The reason has not been identified yet. Are there any other directions to test, considering that writing Flash data at the end of the APP code is working fine?
The MCU in use is STM32F401RET6.

code as below

int pending_len = writing_len;//writing_len=374
int wr_idx = 0;

ret = HAL_FLASH_Unlock();
if (ret != 0) {
uart_printf("flash unlock failed\n");
goto failed;
}

for (; wr_idx < writing_len - 4; wr_idx+=4) {
uint32_t *src32 = &cached_data[wr_idx];
ret = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, 0x800F7FF + wr_idx, *src32);
if (ret != 0) {
uart_printf("flash program 32 failed\n");
goto failed;
} pending_len -= 4;
}
if (pending_len > 0) {
// writing by 8 bits
for (; wr_idx < writing_len; wr_idx++) {
uint8_t *src8 = &cached_data[wr_idx];
ret = HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, flash_addr + wr_idx, *src8);
if (ret != 0) {
uart_printf("flash program 8 failed\n");
goto failed;
}
pending_len--;
}
}
// lock
ret = HAL_FLASH_Lock();
if (ret != 0) {
uart_printf("flash lock failed\n");
goto failed;
}

 

 

螢幕擷取畫面 2023-11-10 112223.png

1 ACCEPTED SOLUTION

Accepted Solutions

>>HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, 0x800F7FF + wr_idx, *src32);

Writing words to odd misalign addresses is NOT permitted.

 

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

2 REPLIES 2
Bob S
Principal

The usual suggestions - what IS the error code that you get?  Debug the code, stepping into the HAL_FLASH_Program() function and see where/why it generates that error.  Or do you get an "OK" response but the FLASH isn't actually programmed?

 

ret = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, 0x800F7FF + wr_idx, *src32);

The first time this is called, wr_idx = 0 so you are trying to write to 0x0800F7FF. Is that really the first address of the ununsed area?  I would have expected 0x0800F800.  Is the DFU area write protected or otherwise restricted?

>>HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, 0x800F7FF + wr_idx, *src32);

Writing words to odd misalign addresses is NOT permitted.

 

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..