2021-08-16 11:54 PM
I am trying to program my flash halfword per halfword. There is no HAL library to achieve this work so I am working with registers.
After erasing the specific sector that I want to write, I am trying to change the control register 1 with below code.
Which is 0101 0010 in binary (You can check The CR1 register from the ref manual for further details).
While debugging, I can see that FLASH->CR1 register goes 0x12 in HEX, 0001 0010 in binary. I cant set 6th bit for a reason that I can not understand. I tried to change bits one by one too. Still cant set the FW1(6th) bit. Thats what I am asking. I cant program my flash with halfword chunks. Any application code, any help will be appreciated.
I can write with hal library(256 bits). I am trying to say here that there is no problem except mentioned situation above.
eymen
Solved! Go to Solution.
2021-08-17 07:57 AM
I'm not sure, but maybe it is needed to set bit 6 (FW1) after writing 16-bit data to Flash address.
Be careful:
Overall the STM32H7 prefers the Flash memory to be written in 256-bit sections.
You can use the Force Write bit but there are consequences. As written in reference manual:
"FW1 forces a write operation even if the write buffer is not full. In this case all bits not written
are set to 1 by hardware. "
Also:
"Note: Using a force-write operation prevents the application from updating later the missing
bits with something else than 1, because it is likely that it will lead to permanent ECC
error."
Maybe a better solution would be to read a 256-bit Flash chunk into RAM, update the 16-bit part you want to update in the RAM buffer, then write it back to Flash memory as a full 256-bit section. Thus you avoid to handle the Force Write bit and you can update later the other parts of the 256-bit sector.
2021-08-17 12:50 AM
Hello
did you check the error bits in FLASH_SR1 ? any error must be cleared before writing in Flash.
2021-08-17 01:20 AM
Hello Mr. Guillaume,
Yeah I checked it right now. It is 0 (0x00) until *(__IO uint16_t*)Address = Data; line. When it comes to this line it becomes 0x02 which is 0010 in binary. So I checked it in ref manual, It is the Bit 1 WBNE1: Bank 1 write buffer not empty flag. And I think its natural to see it set right?
eymen
2021-08-17 03:22 AM
0x01 is 1 in binary. So bit 0.
2021-08-17 03:46 AM
Right! I corrected that. Thanks!
2021-08-17 07:57 AM
I'm not sure, but maybe it is needed to set bit 6 (FW1) after writing 16-bit data to Flash address.
Be careful:
Overall the STM32H7 prefers the Flash memory to be written in 256-bit sections.
You can use the Force Write bit but there are consequences. As written in reference manual:
"FW1 forces a write operation even if the write buffer is not full. In this case all bits not written
are set to 1 by hardware. "
Also:
"Note: Using a force-write operation prevents the application from updating later the missing
bits with something else than 1, because it is likely that it will lead to permanent ECC
error."
Maybe a better solution would be to read a 256-bit Flash chunk into RAM, update the 16-bit part you want to update in the RAM buffer, then write it back to Flash memory as a full 256-bit section. Thus you avoid to handle the Force Write bit and you can update later the other parts of the 256-bit sector.
2021-08-17 09:07 AM
***"I'm not sure, but maybe it is needed to set bit 6 (FW1) after writing 16-bit data to Flash address."***
Exactly! It worked. From my understandings FW1 bit works like a toggle bit. After pointed the address that Iwant to write, you need to toggle the Force Write bit and thats it.
***Note: Using a force-write operation prevents the application from updating later the missing
bits with something else than 1, because it is likely that it will lead to permanent ECC
error."***
Yes, writen data could change when I toggle the FW1 bit. Its described in 5th mention. https://community.st.com/s/question/0D50X00009XkXbw/stm32h7-hal-flash-program
***Maybe a better solution would be to read a 256-bit Flash chunk into RAM, update the 16-bit part you want to update in the RAM buffer, then write it back to Flash memory as a full 256-bit section. Thus you avoid to handle the Force Write bit and you can update later the other parts of the 256-bit sector.***
Sounds good.
Thanks for the help!