cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F0 FLASH_PROGRAM PAGE BOUNDARY

lmuratsimsek
Associate II

I'm working on a bootloader with an STM32F072 device. At the end of the programming process, I followed the steps in the link below to switch to the user application:

https://community.st.com/s/question/0D53W00000trgpiSAA/how-to-boot-to-random-address-without-vecttaboffset-stm32f072

I applied these steps to both the user application and the bootloader application (the transition should have been from the user application to the bootloader application). After doing this, I can easily write data to the 0x08004000 address, but the problem starts here. Without optimization, I write data to the 0x08003800 address, and after a while, when it reaches the page boundaries, it stops writing data. This only happens for values below the 0x08004000 address. The HAL_FLASH_PROGRAM command gets corrupted when it reaches the page boundaries. What is your solution?

#define USER_APP_BASE_ADDR 0x08004000UL

.ram_vector :
{
*(.ram_vector)
} >RAM

 

 

void Write(void)
{
    volatile uint8_t numBytes; // Data Len
    volatile uint8_t initial_data_start_index = 4;
    uint32_t byte_difference = 0;
    volatile HAL_StatusTypeDef status;
HAL_UART_Receive(&huart2, &programData.BufferByte[0], PACKET_SIZE, PACKET_TIMEOUT_VALUE); // Remaining part of data - 19 bytes.
numBytes =  programData.BufferByte[0];
 
HAL_UART_Receive(&huart2, crcUnion.crcBuffer, CRC_PACKET_SIZE , PACKET_TIMEOUT_VALUE); // Get CRC - 4 bytes
 
__HAL_CRC_DR_RESET(&hcrc);
__HAL_RCC_CRC_CLK_ENABLE();
crc_value = HAL_CRC_Calculate(&hcrc, (uint32_t * )&programData.BufferByte[0], PACKET_SIZE);
 
if (crc_value == crcUnion.crc32Val)
{
byte_difference = (USER_APP_BASE_ADDR | ((uint32_t)programData.buffer_hexFormat.low_address1 << 😎 | programData.buffer_hexFormat.low_address2 ) - USER_APP_BASE_ADDR;
bootloader_decode(&programData.BufferByte[4], programData.BufferByte[0], byte_difference);
 
HAL_FLASH_Unlock();
for(volatile uint32_t i = 0 ; i <numBytes ; i+=2)
{
volatile uint32_t addr = (USER_APP_BASE_ADDR | ((uint32_t)programData.buffer_hexFormat.low_address1 << 😎 | programData.buffer_hexFormat.low_address2 ) + i ;
volatile uint16_t data = programData.BufferByte[initial_data_start_index + i] + (programData.BufferByte[initial_data_start_index+ i + 1] << 8);
status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, addr , data);
}
HAL_FLASH_Lock();
if ( status== HAL_OK) {
Send_ACK_NOIT(&huart2);
}else{
 
Send_NACK_NOIT(&huart2,ACKFAULT); 
}
 
}else {
 
Send_NACK_NOIT(&huart2,NACK); // Veri yazılamadı Anlamsız veri - Corrupted data
}
}
 
1 ACCEPTED SOLUTION

Accepted Solutions
lmuratsimsek
Associate II

It is about address problem, double check your base adress.

View solution in original post

3 REPLIES 3
MikaelM
ST Employee

Hello @lmuratsimsek ,

not sure to well understand but are you sure that the memory at address 0x08004000 is erased before trying to write into it ?

Address 0X08004000 is the starting address of sector 4, page 8 and seems to be the first address of your application.

If you have already written your application code here, You first have to erase it before writing the new one. Or you have to use "multiple start address" with an APP1 at an address and APP2 at another address to guaranteed that you always have a valid application loaded in case of "power issue".

Best regards

Mikael

 

If you feel a post has answered your question, please click Accept as Solution.
lmuratsimsek
Associate II

Yes, I am erasing all pages from the final address of the application, "Bootloader." The final address of my "Bootloader" application is 0x08002400, and I can only write my application starting from the address 0x08000400. When I try to load at lower addresses, such as 0x08000300, before 0x08000400, some data is being written. When it reaches the page boundaries, it jumps a page or sector and continues writing. I am sure that all of my pages are erased. I am using the STM32F072RB processor, and when jumping from one application to another for this processor, I perform vector table relocation operations as mentioned in the link above. However, I haven't been able to solve the problem. I need to save space in terms of size and write applications on top of each other, but the page gaps in between are causing issues.

lmuratsimsek
Associate II

It is about address problem, double check your base adress.