cancel
Showing results for 
Search instead for 
Did you mean: 

Assistance Needed with Reading Flash Memory Data on STM32F091RC

SANTHOSH1
Associate II

Dear STM Community,

I hope this message finds you well. I am currently facing an issue while attempting to write and read data in the flash memory of an STM32F091RC using STM Cube IDE.

Specifically, while the write operation executes correctly, I encounter difficulties during the read operation. Upon reading the data, I notice that in addition to the expected data I have written, there is also unwanted junk data present (the junk data consistently appears as the number 2). I have confirmed this inconsistency through UART.

To ensure data integrity, I perform a flash memory erase operation before each write operation. However, despite this precaution, the issue persists.

I kindly request your assistance and expertise in resolving this matter. Attached, please find my code and the output for your reference.

Thank you very much for your time and support.

 

 

#include "main.h"
#include "stm32f0xx_hal.h"
 
#define FLASH_START_ADDRESS  ((uint32_t)0x0800F000)
#define FLASH_END_ADDRESS      ((uint32_t)0x0800FFFF)
#define FLASH_DATA_SIZE              ((uint8_t)1)
#define PAGE_SIZE                           ((uint32_t)FLASH_PAGE_SIZE)
 
char UART_READ_VALUE[10]={0,0,0,0,0,0,0,0,0,0};
 
uint8_t flashdData[FLASH_DATA_SIZE]={0};
uint8_t flashwriteData[FLASH_DATA_SIZE]={0};
 
 
void FLASH_Write(uint32_t address, uint8_t *data, uint32_t size) {
    HAL_FLASH_Unlock();
    for (uint32_t i = 0; i < size; i++) {
        HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, address + i, data[i]);
    }
    HAL_FLASH_Lock();
}
 
 
void FLASH_Read(uint32_t address, uint8_t *data, uint32_t size) {
    for (uint32_t i = 0; i < size; i++) {
        data[i] = *((uint8_t *)(address + i));
    }
}
 
 
void FLASH_ErasePage(uint32_t address) {
    HAL_FLASH_Unlock(); // Unlock flash memory
    FLASH_EraseInitTypeDef eraseInitStruct;
    eraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;  
    eraseInitStruct.PageAddress = address;
    eraseInitStruct.NbPages = 1;
    uint32_t pageError = 0;
    HAL_FLASHEx_Erase(&eraseInitStruct, &pageError); // Erase page in flash memory
    HAL_FLASH_Lock(); // Lock flash memory
}
 
int main()
{
 
 
//READ OPERATION
 
  FLASH_Read(FLASH_START_ADDRESS,flashreadData, FLASH_DATA_SIZE);
 
  IntToString(UART_READ_VALUE,flashreadData[0]);
 
    HAL_UART_Transmit(&huart2,UART_READ_VALUE,1,500);
 
 
//WRITE OPEARTION
 
FLASH_ErasePage(FLASH_START_ADDRESS);
 
  flashwriteData[0]=3;
 
    FLASH_Write(FLASH_START_ADDRESS,flashwriteData, FLASH_DATA_SIZE);
 
return 0;
}
 

 

 

2 REPLIES 2
Andrew Neil
Evangelist III

Please use this button to properly post source code:

AndrewNeil_0-1709808423992.png

 

#PostSourceCode

 

Check the minimum write size / width. You're writing a half word, 16 bits, but I think width is 64 bits

How you dump the data is less than helpful. It's obfuscated by a function you don't provide. Show memory dump from debugger or output bytes in hex individually. 

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