Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* USER CODE BEGIN Header */
- /**
- ******************************************************************************
- * @file FLASH/FLASH_EraseProgram/Src/main.c
- * @author MCD Application Team
- * @brief This example provides a description of how to erase and program the
- * STM32G0xx FLASH.
- ******************************************************************************
- * @attention
- *
- * <h2><center>© Copyright (c) 2018 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"
- /* 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 */
- #define FLASH_USER_START_ADDR (FLASH_BASE + (3 * FLASH_PAGE_SIZE)) /* Start @ of user Flash area */
- #define FLASH_USER_END_ADDR FLASH_BASE+(4*FLASH_PAGE_SIZE)-1//(FLASH_BASE + FLASH_SIZE - 1) /* End @ of user Flash area */
- #define DATA_64 ((uint64_t)0x1234567812345678)
- #define DATA_32 ((uint32_t)0x12345678)
- /* USER CODE END PD */
- /* Private macro -------------------------------------------------------------*/
- /* USER CODE BEGIN PM */
- /* USER CODE END PM */
- /* Private variables ---------------------------------------------------------*/
- /* USER CODE BEGIN PV */
- uint32_t FirstPage = 0, NbOfPages = 0;
- uint32_t Address = 0, PageError = 0;
- __IO uint32_t data32 = 0 , MemoryProgramStatus = 0;
- /*Variable used for Erase procedure*/
- static FLASH_EraseInitTypeDef EraseInitStruct = {0};
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- static void MX_GPIO_Init(void);
- /* USER CODE BEGIN PFP */
- void SystemClock_Config(void);
- static uint32_t GetPage(uint32_t Address);
- /* USER CODE END PFP */
- /* Private user code ---------------------------------------------------------*/
- /* USER CODE BEGIN 0 */
- /* USER CODE END 0 */
- /**
- * @brief The application entry point.
- * @retval int
- */
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- /* STM32G0xx HAL library initialization:
- - Configure the Flash prefetch
- - Systick timer is configured by default as source of time base, but user
- can eventually implement his proper time base source (a general purpose
- timer for example or other time source), keeping in mind that Time base
- duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
- handled in milliseconds basis.
- - Low Level Initialization
- */
- /* USER CODE END 1 */
- /* MCU Configuration--------------------------------------------------------*/
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
- /* USER CODE BEGIN Init */
- /* Configure the system clock to 56 MHz */
- /* 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 */
- /* Initialize LED4 */
- BSP_LED_Init(LED4);
- /* 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 page to erase */
- FirstPage = GetPage(FLASH_USER_START_ADDR);
- /* Get the number of pages to erase from 1st page */
- NbOfPages = GetPage(FLASH_USER_END_ADDR) - FirstPage + 1;
- /* Fill EraseInit structure*/
- EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
- EraseInitStruct.Page = FirstPage;
- EraseInitStruct.NbPages = NbOfPages;
- /* 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. */
- if (HAL_FLASHEx_Erase(&EraseInitStruct, &PageError) != HAL_OK)
- {
- /*
- Error occurred while page erase.
- User can add here some code to deal with this error.
- PageError will contain the faulty page and then to know the code error on this page,
- user can call function 'HAL_FLASH_GetError()'
- */
- /* Infinite loop */
- while (1)
- {
- /* Make LED4 blink (100ms on, 2s off) to indicate error in Erase operation */
- BSP_LED_On(LED4);
- HAL_Delay(100);
- BSP_LED_Off(LED4);
- HAL_Delay(2000);
- }
- }
- /* 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_DOUBLEWORD, Address, DATA_64) == HAL_OK)
- {
- Address = Address + 8;
- }
- else
- {
- /* Error occurred while writing data in Flash memory.
- User can add here some code to deal with this error */
- while (1)
- {
- /* Make LED4 blink (100ms on, 2s off) to indicate error in Write operation */
- BSP_LED_On(LED4);
- HAL_Delay(100);
- BSP_LED_Off(LED4);
- HAL_Delay(2000);
- }
- }
- }
- /* 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. LED4 will blink with 1s period */
- while (1)
- {
- BSP_LED_On(LED4);
- HAL_Delay(1000);
- BSP_LED_Off(LED4);
- HAL_Delay(1000);
- }
- }
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- }
- /* USER CODE END 3 */
- }
- /**
- * @brief System Clock Configuration
- * @retval None
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct = {0};
- RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
- /** Initializes the CPU, AHB and APB busses clocks
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;
- RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1;
- RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
- RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV4;
- RCC_OscInitStruct.PLL.PLLN = 70;
- RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV10;
- RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV5;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- Error_Handler();
- }
- /** Initializes the CPU, AHB and APB busses clocks
- */
- RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
- |RCC_CLOCKTYPE_PCLK1;
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
- {
- Error_Handler();
- }
- }
- /**
- * @brief GPIO Initialization Function
- * @param None
- * @retval None
- */
- static void MX_GPIO_Init(void)
- {
- /* GPIO Ports Clock Enable */
- __HAL_RCC_GPIOF_CLK_ENABLE();
- }
- /* USER CODE BEGIN 4 */
- /**
- * @brief Gets the page of a given address
- * @param Addr: Address of the FLASH Memory
- * @retval The page of a given address
- */
- static uint32_t GetPage(uint32_t Addr)
- {
- return (Addr - FLASH_BASE) / FLASH_PAGE_SIZE;;
- }
- /* USER CODE END 4 */
- /**
- * @brief This function is executed in case of error occurrence.
- * @retval None
- */
- void Error_Handler(void)
- {
- /* USER CODE BEGIN Error_Handler_Debug */
- /* User can add his own implementation to report the HAL error return state */
- while(1)
- {
- }
- /* USER CODE END Error_Handler_Debug */
- }
- #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 CODE BEGIN 6 */
- /* 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)
- {
- }
- /* USER CODE END 6 */
- }
- #endif /* USE_FULL_ASSERT */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement