mirror of
https://github.com/STMicroelectronics/STM32CubeF4.git
synced 2025-04-28 13:48:54 +08:00
375 lines
11 KiB
C
375 lines
11 KiB
C
/**
|
|
******************************************************************************
|
|
* @file FLASH/FLASH_EraseProgram/Src/main.c
|
|
* @author MCD Application Team
|
|
* @brief This example provides a description of how to erase and program the
|
|
* STM32F4xx FLASH.
|
|
******************************************************************************
|
|
* @attention
|
|
*
|
|
* <h2><center>© Copyright (c) 2017 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
|
|
*
|
|
******************************************************************************
|
|
*/
|
|
|
|
/* Includes ------------------------------------------------------------------*/
|
|
#include "main.h"
|
|
|
|
/** @addtogroup STM32F4xx_HAL_Examples
|
|
* @{
|
|
*/
|
|
|
|
/** @addtogroup FLASH_EraseProgram
|
|
* @{
|
|
*/
|
|
|
|
/* Private typedef -----------------------------------------------------------*/
|
|
/* Private define ------------------------------------------------------------*/
|
|
#define FLASH_USER_START_ADDR ADDR_FLASH_SECTOR_2 /* Start @ of user Flash area */
|
|
#define FLASH_USER_END_ADDR ADDR_FLASH_SECTOR_11 + GetSectorSize(ADDR_FLASH_SECTOR_11) -1 /* End @ of user Flash area : sector start address + sector size -1 */
|
|
|
|
#define DATA_32 ((uint32_t)0x12345678)
|
|
|
|
/* Private macro -------------------------------------------------------------*/
|
|
/* Private variables ---------------------------------------------------------*/
|
|
uint32_t FirstSector = 0, NbOfSectors = 0, Address = 0;
|
|
uint32_t SectorError = 0;
|
|
__IO uint32_t data32 = 0 , MemoryProgramStatus = 0;
|
|
|
|
/*Variable used for Erase procedure*/
|
|
static FLASH_EraseInitTypeDef EraseInitStruct;
|
|
|
|
/* Private function prototypes -----------------------------------------------*/
|
|
static void SystemClock_Config(void);
|
|
static void Error_Handler(void);
|
|
static uint32_t GetSector(uint32_t Address);
|
|
static uint32_t GetSectorSize(uint32_t Sector);
|
|
|
|
/* Private functions ---------------------------------------------------------*/
|
|
|
|
/**
|
|
* @brief Main program
|
|
* @param None
|
|
* @retval None
|
|
*/
|
|
int main(void)
|
|
{
|
|
/* STM32F4xx HAL library initialization:
|
|
- Configure the Flash prefetch, instruction and Data caches
|
|
- Configure the Systick to generate an interrupt each 1 msec
|
|
- Set NVIC Group Priority to 4
|
|
- Global MSP (MCU Support Package) initialization
|
|
*/
|
|
HAL_Init();
|
|
|
|
/* Configure LED3, LED4, LED5 and LED6 */
|
|
BSP_LED_Init(LED3);
|
|
BSP_LED_Init(LED4);
|
|
BSP_LED_Init(LED5);
|
|
BSP_LED_Init(LED6);
|
|
|
|
/* Configure the system clock to 168 MHz */
|
|
SystemClock_Config();
|
|
|
|
/* Unlock the Flash to enable the flash control register access *************/
|
|
HAL_FLASH_Unlock();
|
|
|
|
/* Erase the user Flash area
|
|
(area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/
|
|
|
|
/* Get the 1st sector to erase */
|
|
FirstSector = GetSector(FLASH_USER_START_ADDR);
|
|
/* Get the number of sector to erase from 1st sector*/
|
|
NbOfSectors = GetSector(FLASH_USER_END_ADDR) - FirstSector + 1;
|
|
|
|
/* Fill EraseInit structure*/
|
|
EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
|
|
EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
|
|
EraseInitStruct.Sector = FirstSector;
|
|
EraseInitStruct.NbSectors = NbOfSectors;
|
|
if(HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK)
|
|
{
|
|
/*
|
|
Error occurred while sector erase.
|
|
User can add here some code to deal with this error.
|
|
SectorError will contain the faulty sector and then to know the code error on this sector,
|
|
user can call function 'HAL_FLASH_GetError()'
|
|
*/
|
|
/*
|
|
FLASH_ErrorTypeDef errorcode = HAL_FLASH_GetError();
|
|
*/
|
|
Error_Handler();
|
|
}
|
|
|
|
/* Note: If an erase operation in Flash memory also concerns data in the data or instruction cache,
|
|
you have to make sure that these data are rewritten before they are accessed during code
|
|
execution. If this cannot be done safely, it is recommended to flush the caches by setting the
|
|
DCRST and ICRST bits in the FLASH_CR register. */
|
|
__HAL_FLASH_DATA_CACHE_DISABLE();
|
|
__HAL_FLASH_INSTRUCTION_CACHE_DISABLE();
|
|
|
|
__HAL_FLASH_DATA_CACHE_RESET();
|
|
__HAL_FLASH_INSTRUCTION_CACHE_RESET();
|
|
|
|
__HAL_FLASH_INSTRUCTION_CACHE_ENABLE();
|
|
__HAL_FLASH_DATA_CACHE_ENABLE();
|
|
|
|
/* Program the user Flash area word by word
|
|
(area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/
|
|
|
|
Address = FLASH_USER_START_ADDR;
|
|
|
|
while (Address < FLASH_USER_END_ADDR)
|
|
{
|
|
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, Address, DATA_32) == HAL_OK)
|
|
{
|
|
Address = Address + 4;
|
|
}
|
|
else
|
|
{
|
|
/* Error occurred while writing data in Flash memory.
|
|
User can add here some code to deal with this error */
|
|
/*
|
|
FLASH_ErrorTypeDef errorcode = HAL_FLASH_GetError();
|
|
*/
|
|
Error_Handler();
|
|
}
|
|
}
|
|
|
|
/* Lock the Flash to disable the flash control register access (recommended
|
|
to protect the FLASH memory against possible unwanted operation) *********/
|
|
HAL_FLASH_Lock();
|
|
|
|
/* Check if the programmed data is OK
|
|
MemoryProgramStatus = 0: data programmed correctly
|
|
MemoryProgramStatus != 0: number of words not programmed correctly ******/
|
|
Address = FLASH_USER_START_ADDR;
|
|
MemoryProgramStatus = 0x0;
|
|
|
|
while (Address < FLASH_USER_END_ADDR)
|
|
{
|
|
data32 = *(__IO uint32_t*)Address;
|
|
|
|
if (data32 != DATA_32)
|
|
{
|
|
MemoryProgramStatus++;
|
|
}
|
|
|
|
Address = Address + 4;
|
|
}
|
|
|
|
/* Check if there is an issue to program data */
|
|
if (MemoryProgramStatus == 0)
|
|
{
|
|
/* No error detected. Switch on LED4 */
|
|
BSP_LED_On(LED4);
|
|
}
|
|
else
|
|
{
|
|
/* Error detected. Switch on LED5 */
|
|
Error_Handler();
|
|
}
|
|
|
|
/* Infinite loop */
|
|
while (1)
|
|
{
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief Gets the sector of a given address
|
|
* @param None
|
|
* @retval The sector of a given address
|
|
*/
|
|
static uint32_t GetSector(uint32_t Address)
|
|
{
|
|
uint32_t sector = 0;
|
|
|
|
if((Address < ADDR_FLASH_SECTOR_1) && (Address >= ADDR_FLASH_SECTOR_0))
|
|
{
|
|
sector = FLASH_SECTOR_0;
|
|
}
|
|
else if((Address < ADDR_FLASH_SECTOR_2) && (Address >= ADDR_FLASH_SECTOR_1))
|
|
{
|
|
sector = FLASH_SECTOR_1;
|
|
}
|
|
else if((Address < ADDR_FLASH_SECTOR_3) && (Address >= ADDR_FLASH_SECTOR_2))
|
|
{
|
|
sector = FLASH_SECTOR_2;
|
|
}
|
|
else if((Address < ADDR_FLASH_SECTOR_4) && (Address >= ADDR_FLASH_SECTOR_3))
|
|
{
|
|
sector = FLASH_SECTOR_3;
|
|
}
|
|
else if((Address < ADDR_FLASH_SECTOR_5) && (Address >= ADDR_FLASH_SECTOR_4))
|
|
{
|
|
sector = FLASH_SECTOR_4;
|
|
}
|
|
else if((Address < ADDR_FLASH_SECTOR_6) && (Address >= ADDR_FLASH_SECTOR_5))
|
|
{
|
|
sector = FLASH_SECTOR_5;
|
|
}
|
|
else if((Address < ADDR_FLASH_SECTOR_7) && (Address >= ADDR_FLASH_SECTOR_6))
|
|
{
|
|
sector = FLASH_SECTOR_6;
|
|
}
|
|
else if((Address < ADDR_FLASH_SECTOR_8) && (Address >= ADDR_FLASH_SECTOR_7))
|
|
{
|
|
sector = FLASH_SECTOR_7;
|
|
}
|
|
else if((Address < ADDR_FLASH_SECTOR_9) && (Address >= ADDR_FLASH_SECTOR_8))
|
|
{
|
|
sector = FLASH_SECTOR_8;
|
|
}
|
|
else if((Address < ADDR_FLASH_SECTOR_10) && (Address >= ADDR_FLASH_SECTOR_9))
|
|
{
|
|
sector = FLASH_SECTOR_9;
|
|
}
|
|
else if((Address < ADDR_FLASH_SECTOR_11) && (Address >= ADDR_FLASH_SECTOR_10))
|
|
{
|
|
sector = FLASH_SECTOR_10;
|
|
}
|
|
else /* (Address < FLASH_END_ADDR) && (Address >= ADDR_FLASH_SECTOR_11) */
|
|
{
|
|
sector = FLASH_SECTOR_11;
|
|
}
|
|
|
|
return sector;
|
|
}
|
|
|
|
/**
|
|
* @brief Gets sector Size
|
|
* @param None
|
|
* @retval The size of a given sector
|
|
*/
|
|
static uint32_t GetSectorSize(uint32_t Sector)
|
|
{
|
|
uint32_t sectorsize = 0x00;
|
|
|
|
if((Sector == FLASH_SECTOR_0) || (Sector == FLASH_SECTOR_1) || (Sector == FLASH_SECTOR_2) || (Sector == FLASH_SECTOR_3))
|
|
{
|
|
sectorsize = 16 * 1024;
|
|
}
|
|
else if(Sector == FLASH_SECTOR_4)
|
|
{
|
|
sectorsize = 64 * 1024;
|
|
}
|
|
else
|
|
{
|
|
sectorsize = 128 * 1024;
|
|
}
|
|
return sectorsize;
|
|
}
|
|
|
|
/**
|
|
* @brief This function is executed in case of error occurrence.
|
|
* @param None
|
|
* @retval None
|
|
*/
|
|
static void Error_Handler(void)
|
|
{
|
|
/* Turn LED5 on */
|
|
BSP_LED_On(LED5);
|
|
while(1)
|
|
{
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief System Clock Configuration
|
|
* The system Clock is configured as follow :
|
|
* System Clock source = PLL (HSE)
|
|
* SYSCLK(Hz) = 168000000
|
|
* HCLK(Hz) = 168000000
|
|
* AHB Prescaler = 1
|
|
* APB1 Prescaler = 4
|
|
* APB2 Prescaler = 2
|
|
* HSE Frequency(Hz) = 8000000
|
|
* PLL_M = 8
|
|
* PLL_N = 336
|
|
* PLL_P = 2
|
|
* PLL_Q = 7
|
|
* VDD(V) = 3.3
|
|
* Main regulator output voltage = Scale1 mode
|
|
* Flash Latency(WS) = 5
|
|
* @param None
|
|
* @retval None
|
|
*/
|
|
static void SystemClock_Config(void)
|
|
{
|
|
RCC_ClkInitTypeDef RCC_ClkInitStruct;
|
|
RCC_OscInitTypeDef RCC_OscInitStruct;
|
|
|
|
/* Enable Power Control clock */
|
|
__HAL_RCC_PWR_CLK_ENABLE();
|
|
|
|
/* The voltage scaling allows optimizing the power consumption when the device is
|
|
clocked below the maximum system frequency, to update the voltage scaling value
|
|
regarding system frequency refer to product datasheet. */
|
|
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
|
|
|
|
/* Enable HSE Oscillator and activate PLL with HSE as source */
|
|
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
|
|
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
|
|
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
|
|
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
|
|
RCC_OscInitStruct.PLL.PLLM = 8;
|
|
RCC_OscInitStruct.PLL.PLLN = 336;
|
|
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
|
|
RCC_OscInitStruct.PLL.PLLQ = 7;
|
|
HAL_RCC_OscConfig(&RCC_OscInitStruct);
|
|
|
|
/* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
|
|
clocks dividers */
|
|
RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
|
|
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
|
|
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
|
|
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
|
|
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
|
|
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
|
|
|
|
/* STM32F405x/407x/415x/417x Revision Z devices: prefetch is supported */
|
|
if (HAL_GetREVID() == 0x1001)
|
|
{
|
|
/* Enable the Flash prefetch */
|
|
__HAL_FLASH_PREFETCH_BUFFER_ENABLE();
|
|
}
|
|
}
|
|
|
|
#ifdef USE_FULL_ASSERT
|
|
|
|
/**
|
|
* @brief Reports the name of the source file and the source line number
|
|
* where the assert_param error has occurred.
|
|
* @param file: pointer to the source file name
|
|
* @param line: assert_param error line source number
|
|
* @retval None
|
|
*/
|
|
void assert_failed(uint8_t* file, uint32_t line)
|
|
{
|
|
/* User can add his own implementation to report the file name and line number,
|
|
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
|
|
|
|
/* Infinite loop */
|
|
while (1)
|
|
{
|
|
}
|
|
}
|
|
#endif
|
|
|
|
/**
|
|
* @}
|
|
*/
|
|
|
|
/**
|
|
* @}
|
|
*/
|
|
|
|
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|