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 << 8) | 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 << 8) | 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
}
}