Showing results for 
Search instead for 
Did you mean: 


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:

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



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
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);
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);
if ( status== HAL_OK) {
}else {
Send_NACK_NOIT(&huart2,NACK); // Veri yazılamadı Anlamsız veri - Corrupted data

Accepted Solutions
Associate II

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

View solution in original post

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



If you feel a post has answered your question, please click Accept as Solution.
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.

Associate II

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