Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2020
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.02 KB | None | 0 0
  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * @file    FLASH/FLASH_EraseProgram/Src/main.c
  5.   * @author  MCD Application Team
  6.   * @brief   This example provides a description of how to erase and program the
  7.   *          STM32G0xx FLASH.
  8.   ******************************************************************************
  9.   * @attention
  10.   *
  11.   * <h2><center>&copy; Copyright (c) 2018 STMicroelectronics.
  12.   * All rights reserved.</center></h2>
  13.   *
  14.   * This software component is licensed by ST under BSD 3-Clause license,
  15.   * the "License"; You may not use this file except in compliance with the
  16.   * License. You may obtain a copy of the License at:
  17.   *                        opensource.org/licenses/BSD-3-Clause
  18.   *
  19.   ******************************************************************************
  20.   */
  21. /* USER CODE END Header */
  22.  
  23. /* Includes ------------------------------------------------------------------*/
  24. #include "main.h"
  25.  
  26. /* Private includes ----------------------------------------------------------*/
  27. /* USER CODE BEGIN Includes */
  28.  
  29. /* USER CODE END Includes */
  30.  
  31. /* Private typedef -----------------------------------------------------------*/
  32. /* USER CODE BEGIN PTD */
  33.  
  34. /* USER CODE END PTD */
  35.  
  36. /* Private define ------------------------------------------------------------*/
  37. /* USER CODE BEGIN PD */
  38. #define FLASH_USER_START_ADDR   (FLASH_BASE + (3 * FLASH_PAGE_SIZE))   /* Start @ of user Flash area */
  39. #define FLASH_USER_END_ADDR     FLASH_BASE+(4*FLASH_PAGE_SIZE)-1//(FLASH_BASE + FLASH_SIZE - 1)   /* End @ of user Flash area */
  40.  
  41. #define DATA_64                 ((uint64_t)0x1234567812345678)
  42. #define DATA_32                 ((uint32_t)0x12345678)
  43.  
  44. /* USER CODE END PD */
  45.  
  46. /* Private macro -------------------------------------------------------------*/
  47. /* USER CODE BEGIN PM */
  48.  
  49. /* USER CODE END PM */
  50.  
  51. /* Private variables ---------------------------------------------------------*/
  52.  
  53. /* USER CODE BEGIN PV */
  54. uint32_t FirstPage = 0, NbOfPages = 0;
  55. uint32_t Address = 0, PageError = 0;
  56. __IO uint32_t data32 = 0 , MemoryProgramStatus = 0;
  57.  
  58. /*Variable used for Erase procedure*/
  59. static FLASH_EraseInitTypeDef EraseInitStruct = {0};
  60.  
  61. /* USER CODE END PV */
  62.  
  63. /* Private function prototypes -----------------------------------------------*/
  64. void SystemClock_Config(void);
  65. static void MX_GPIO_Init(void);
  66. /* USER CODE BEGIN PFP */
  67. void SystemClock_Config(void);
  68. static uint32_t GetPage(uint32_t Address);
  69.  
  70. /* USER CODE END PFP */
  71.  
  72. /* Private user code ---------------------------------------------------------*/
  73. /* USER CODE BEGIN 0 */
  74.  
  75. /* USER CODE END 0 */
  76.  
  77. /**
  78.   * @brief  The application entry point.
  79.   * @retval int
  80.   */
  81. int main(void)
  82. {
  83.   /* USER CODE BEGIN 1 */
  84.   /* STM32G0xx HAL library initialization:
  85.        - Configure the Flash prefetch
  86.        - Systick timer is configured by default as source of time base, but user
  87.          can eventually implement his proper time base source (a general purpose
  88.          timer for example or other time source), keeping in mind that Time base
  89.          duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
  90.          handled in milliseconds basis.
  91.        - Low Level Initialization
  92.      */
  93.  
  94.   /* USER CODE END 1 */
  95.  
  96.   /* MCU Configuration--------------------------------------------------------*/
  97.  
  98.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  99.   HAL_Init();
  100.  
  101.   /* USER CODE BEGIN Init */
  102.   /* Configure the system clock to 56 MHz */
  103.   /* USER CODE END Init */
  104.  
  105.   /* Configure the system clock */
  106.   SystemClock_Config();
  107.  
  108.   /* USER CODE BEGIN SysInit */
  109.  
  110.   /* USER CODE END SysInit */
  111.  
  112.   /* Initialize all configured peripherals */
  113.   MX_GPIO_Init();
  114.   /* USER CODE BEGIN 2 */
  115.   /* Initialize LED4 */
  116.   BSP_LED_Init(LED4);
  117.  
  118.   /* Unlock the Flash to enable the flash control register access *************/
  119.   HAL_FLASH_Unlock();
  120.  
  121.   /* Erase the user Flash area
  122.     (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/
  123.  
  124.  
  125.   /* Get the 1st page to erase */
  126.   FirstPage = GetPage(FLASH_USER_START_ADDR);
  127.  
  128.   /* Get the number of pages to erase from 1st page */
  129.   NbOfPages = GetPage(FLASH_USER_END_ADDR) - FirstPage + 1;
  130.  
  131.   /* Fill EraseInit structure*/
  132.   EraseInitStruct.TypeErase   = FLASH_TYPEERASE_PAGES;
  133.   EraseInitStruct.Page        = FirstPage;
  134.   EraseInitStruct.NbPages     = NbOfPages;
  135.  
  136.   /* Note: If an erase operation in Flash memory also concerns data in the data or instruction cache,
  137.      you have to make sure that these data are rewritten before they are accessed during code
  138.      execution. If this cannot be done safely, it is recommended to flush the caches by setting the
  139.      DCRST and ICRST bits in the FLASH_CR register. */
  140.   if (HAL_FLASHEx_Erase(&EraseInitStruct, &PageError) != HAL_OK)
  141.   {
  142.     /*
  143.       Error occurred while page erase.
  144.       User can add here some code to deal with this error.
  145.       PageError will contain the faulty page and then to know the code error on this page,
  146.       user can call function 'HAL_FLASH_GetError()'
  147.     */
  148.     /* Infinite loop */
  149.     while (1)
  150.     {
  151.       /* Make LED4 blink (100ms on, 2s off) to indicate error in Erase operation */
  152.       BSP_LED_On(LED4);
  153.       HAL_Delay(100);
  154.       BSP_LED_Off(LED4);
  155.       HAL_Delay(2000);
  156.     }
  157.   }
  158.  
  159.   /* Program the user Flash area word by word
  160.     (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/
  161.  
  162.   Address = FLASH_USER_START_ADDR;
  163.  
  164.   while (Address < FLASH_USER_END_ADDR)
  165.   {
  166.     if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, Address, DATA_64) == HAL_OK)
  167.     {
  168.       Address = Address + 8;
  169.     }
  170.    else
  171.     {
  172.       /* Error occurred while writing data in Flash memory.
  173.          User can add here some code to deal with this error */
  174.       while (1)
  175.       {
  176.         /* Make LED4 blink (100ms on, 2s off) to indicate error in Write operation */
  177.         BSP_LED_On(LED4);
  178.         HAL_Delay(100);
  179.         BSP_LED_Off(LED4);
  180.         HAL_Delay(2000);
  181.       }
  182.     }
  183.   }
  184.  
  185.   /* Lock the Flash to disable the flash control register access (recommended
  186.      to protect the FLASH memory against possible unwanted operation) *********/
  187.   HAL_FLASH_Lock();
  188.  
  189.   /* Check if the programmed data is OK
  190.       MemoryProgramStatus = 0: data programmed correctly
  191.       MemoryProgramStatus != 0: number of words not programmed correctly ******/
  192.   Address = FLASH_USER_START_ADDR;
  193.   MemoryProgramStatus = 0x0;
  194.  
  195.   while (Address < FLASH_USER_END_ADDR)
  196.   {
  197.     data32 = *(__IO uint32_t *)Address;
  198.  
  199.     if (data32 != DATA_32)
  200.     {
  201.       MemoryProgramStatus++;
  202.     }
  203.     Address = Address + 4;
  204.   }
  205.  
  206.   /*Check if there is an issue to program data*/
  207.   if (MemoryProgramStatus == 0)
  208.   {
  209.     /* No error detected. Switch on LED4*/
  210.     BSP_LED_On(LED4);
  211.   }
  212.   else
  213.   {
  214.     /* Error detected. LED4 will blink with 1s period */
  215.     while (1)
  216.     {
  217.       BSP_LED_On(LED4);
  218.       HAL_Delay(1000);
  219.       BSP_LED_Off(LED4);
  220.       HAL_Delay(1000);
  221.     }
  222.   }
  223.  
  224.   /* USER CODE END 2 */
  225.  
  226.   /* Infinite loop */
  227.   /* USER CODE BEGIN WHILE */
  228.   while (1)
  229.   {
  230.     /* USER CODE END WHILE */
  231.  
  232.     /* USER CODE BEGIN 3 */
  233.  
  234.   }
  235.   /* USER CODE END 3 */
  236. }
  237.  
  238. /**
  239.   * @brief System Clock Configuration
  240.   * @retval None
  241.   */
  242. void SystemClock_Config(void)
  243. {
  244.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  245.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  246.  
  247.   /** Initializes the CPU, AHB and APB busses clocks
  248.   */
  249.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  250.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  251.   RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1;
  252.   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  253.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  254.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  255.   RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV4;
  256.   RCC_OscInitStruct.PLL.PLLN = 70;
  257.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV10;
  258.   RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV5;
  259.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  260.   {
  261.     Error_Handler();
  262.   }
  263.   /** Initializes the CPU, AHB and APB busses clocks
  264.   */
  265.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  266.                               |RCC_CLOCKTYPE_PCLK1;
  267.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  268.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  269.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  270.  
  271.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  272.   {
  273.     Error_Handler();
  274.   }
  275. }
  276.  
  277. /**
  278.   * @brief GPIO Initialization Function
  279.   * @param None
  280.   * @retval None
  281.   */
  282. static void MX_GPIO_Init(void)
  283. {
  284.  
  285.   /* GPIO Ports Clock Enable */
  286.   __HAL_RCC_GPIOF_CLK_ENABLE();
  287.  
  288. }
  289.  
  290. /* USER CODE BEGIN 4 */
  291.  
  292.  
  293.  
  294. /**
  295.   * @brief  Gets the page of a given address
  296.   * @param  Addr: Address of the FLASH Memory
  297.   * @retval The page of a given address
  298.   */
  299. static uint32_t GetPage(uint32_t Addr)
  300. {
  301.   return (Addr - FLASH_BASE) / FLASH_PAGE_SIZE;;
  302. }
  303.  
  304.  
  305.  
  306. /* USER CODE END 4 */
  307.  
  308. /**
  309.   * @brief  This function is executed in case of error occurrence.
  310.   * @retval None
  311.   */
  312. void Error_Handler(void)
  313. {
  314.   /* USER CODE BEGIN Error_Handler_Debug */
  315.   /* User can add his own implementation to report the HAL error return state */
  316.   while(1)
  317.   {
  318.   }
  319.   /* USER CODE END Error_Handler_Debug */
  320. }
  321.  
  322. #ifdef  USE_FULL_ASSERT
  323. /**
  324.   * @brief  Reports the name of the source file and the source line number
  325.   *         where the assert_param error has occurred.
  326.   * @param  file: pointer to the source file name
  327.   * @param  line: assert_param error line source number
  328.   * @retval None
  329.   */
  330. void assert_failed(uint8_t *file, uint32_t line)
  331. {
  332.   /* USER CODE BEGIN 6 */
  333.   /* User can add his own implementation to report the file name and line number,
  334.     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  335.  
  336.   /* Infinite loop */
  337.   while (1)
  338.   {
  339.   }
  340.   /* USER CODE END 6 */
  341. }
  342. #endif /* USE_FULL_ASSERT */
  343.  
  344. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement