2021-09-02 02:52 AM
I m a beginner with the FLash and I struggle to find a solution to write and read in FLASH.
I wrote the following program but at the function HAL_FLASH_Program the program goes in HardFault_Handler . Looking inside the HAL_FLASH_Program it is possible to see that
FLASH_Program_HalfWord((Address + (2U*index)), (uint16_t)(Data >> (16U*index))); create the error.
thanks for any help.
#define FLASH_STORAGE 0x08005000
#define page_size 0x800
-----main----
HAL_StatusTypeDef HAL_FLASH_Unlock(void);
//Instantiate the FLASH_EraseInitTypeDef struct needed for the HAL_FLASHEx_Erase() function
FLASH_EraseInitTypeDef FLASH_EraseInitStruct = {0};
FLASH_EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES; //Erase type set to erase pages( Available other type is mass erase)
FLASH_EraseInitStruct.PageAddress = 0x08005000; //Starting address of flash page (0x0800 0000 - 0x0801 FC00)
FLASH_EraseInitStruct.NbPages = 1; //The number of pages to be erased
uint32_t errorStatus = 0;
HAL_FLASHEx_Erase(&FLASH_EraseInitStruct,&errorStatus);
uint16_t FData = 1023;
HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD,0x0801FC00, (uint16_t)FData);
HAL_StatusTypeDef HAL_FLASH_Lock(void);
uint64_t * RDAddr = (uint64_t *) 0x08005000;
uint64_t RData = *RDAddr;
2021-09-02 11:41 AM
> FLASH_EraseInitStruct.PageAddress = 0x08005000;
> HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD,0x0801FC00, (uint16_t)FData);
Doesn't seem like you're erasing the location you're writing.
You're erasing 0x08005000 - 0x08005800 and writing to 0x0801FC00.
> HAL_StatusTypeDef HAL_FLASH_Unlock(void);
What is this supposed to do? It doesn't call HAL_FLASH_Unlock.
HAL_FLASH_Unlock();
2021-09-03 02:51 AM
Thanks TDK.
I m still confused. In reference manual of the STMF303 I could see that the Flash is divided into "Pages". in my case 255 which one is 2k.
in STM32F4 is devided in "Sectors", for example 12. what is the difference ?
I have found in internet this code:
STM32-Tutorials/MY_FLASH.c at master · MYaqoobEmbedded/STM32-Tutorials · GitHub
in the funtion
static voidMY_FLASH_EraseSector(void)
what is my case MY_SectorNum and FLASH_VOLTAGE_RANGE_3
{
HAL_FLASH_Unlock(); //Erase the required Flash sector
FLASH_Erase_Sector(MY_SectorNum, FLASH_VOLTAGE_RANGE_3);
HAL_FLASH_Lock();
}
let say that I would like to write at the address 0x0807 F800 an array of uint16_t
int16_t arr= {0x1111, 0x2222};
HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, arr , 2);
is it correct?
2021-09-03 12:13 PM
> I m still confused. In reference manual of the STMF303 I could see that the Flash is divided into "Pages". in my case 255 which one is 2k.
> in STM32F4 is devided in "Sectors", for example 12. what is the difference ?
Pages and sectors are used interchangeably in this instance. Perhaps they use the term sector when they are varying sizes. In any case, they both refer to the smallest block of flash memory that can be erased.
> is it correct?
FLASH_Erase_Sector isn't a function on the STM32F3. Unclear what you're asking now. Are you using an STM32F3 or an STM32F4? The code will not be interchangeable. Let's stick to one of them.
Also, your argument to HAL_FLASH_Program don't line up with what is expected. You got them right in the OP, why are they different now?
2021-09-04 11:45 AM
Hi TDK
thanks for your help. I understood my problem.
I leave the code in case can helo someone. II wil be happy for any improvement.
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2021 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
//#include "stm32f3xx_hal_flash.h"
#include <string.h>
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
#define FLASH_STORAGE 0x08005000
#define page_size 0x800 // total memory 2048k
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
/* USER CODE BEGIN 2 */
uint16_t data = 0x1234;
uint16_t read_data=0;
/*I have 32 bxte for each pages
HAL_FLASH_Unlock();
HAL_FLASH_OB_Unlock();
FLASH_EraseInitTypeDef InitEraseStruct;
InitEraseStruct.TypeErase=FLASH_TYPEERASE_PAGES;
InitEraseStruct.PageAddress=FLASH_STORAGE;
InitEraseStruct.NbPages=1;
volatile uint16_t write_cnt=0, index=0;
uint16_t PageError;
volatile HAL_StatusTypeDef status;
status = HAL_FLASHEx_Erase(&InitEraseStruct, &PageError);
status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, FLASH_STORAGE, data);
HAL_FLASH_OB_Lock();
HAL_FLASH_Lock();
read_data = *(uint32_t*)(FLASH_STORAGE);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
Thaks in advance
Fausto