]> git.sur5r.net Git - freertos/blobdiff - FreeRTOS/Demo/CORTEX_MPU_STM32L4_Discovery_GCC_IAR_Keil/ST_Code/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_flash.c
Rename STM32Cube to GCC for STM32L4 Discovery projects as GCC is
[freertos] / FreeRTOS / Demo / CORTEX_MPU_STM32L4_Discovery_GCC_IAR_Keil / ST_Code / Drivers / STM32L4xx_HAL_Driver / Src / stm32l4xx_hal_flash.c
diff --git a/FreeRTOS/Demo/CORTEX_MPU_STM32L4_Discovery_GCC_IAR_Keil/ST_Code/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_flash.c b/FreeRTOS/Demo/CORTEX_MPU_STM32L4_Discovery_GCC_IAR_Keil/ST_Code/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_flash.c
new file mode 100644 (file)
index 0000000..a746962
--- /dev/null
@@ -0,0 +1,769 @@
+/**\r
+  ******************************************************************************\r
+  * @file    stm32l4xx_hal_flash.c\r
+  * @author  MCD Application Team\r
+  * @brief   FLASH HAL module driver.\r
+  *          This file provides firmware functions to manage the following\r
+  *          functionalities of the internal FLASH memory:\r
+  *           + Program operations functions\r
+  *           + Memory Control functions\r
+  *           + Peripheral Errors functions\r
+  *\r
+ @verbatim\r
+  ==============================================================================\r
+                        ##### FLASH peripheral features #####\r
+  ==============================================================================\r
+\r
+  [..] The Flash memory interface manages CPU AHB I-Code and D-Code accesses\r
+       to the Flash memory. It implements the erase and program Flash memory operations\r
+       and the read and write protection mechanisms.\r
+\r
+  [..] The Flash memory interface accelerates code execution with a system of instruction\r
+       prefetch and cache lines.\r
+\r
+  [..] The FLASH main features are:\r
+      (+) Flash memory read operations\r
+      (+) Flash memory program/erase operations\r
+      (+) Read / write protections\r
+      (+) Option bytes programming\r
+      (+) Prefetch on I-Code\r
+      (+) 32 cache lines of 4*64 bits on I-Code\r
+      (+) 8 cache lines of 4*64 bits on D-Code\r
+      (+) Error code correction (ECC) : Data in flash are 72-bits word\r
+          (8 bits added per double word)\r
+\r
+\r
+                        ##### How to use this driver #####\r
+ ==============================================================================\r
+    [..]\r
+      This driver provides functions and macros to configure and program the FLASH\r
+      memory of all STM32L4xx devices.\r
+\r
+      (#) Flash Memory IO Programming functions:\r
+           (++) Lock and Unlock the FLASH interface using HAL_FLASH_Unlock() and\r
+                HAL_FLASH_Lock() functions\r
+           (++) Program functions: double word and fast program (full row programming)\r
+           (++) There Two modes of programming :\r
+            (+++) Polling mode using HAL_FLASH_Program() function\r
+            (+++) Interrupt mode using HAL_FLASH_Program_IT() function\r
+\r
+      (#) Interrupts and flags management functions :\r
+           (++) Handle FLASH interrupts by calling HAL_FLASH_IRQHandler()\r
+           (++) Callback functions are called when the flash operations are finished :\r
+                HAL_FLASH_EndOfOperationCallback() when everything is ok, otherwise\r
+                HAL_FLASH_OperationErrorCallback()\r
+           (++) Get error flag status by calling HAL_GetError()\r
+\r
+      (#) Option bytes management functions :\r
+           (++) Lock and Unlock the option bytes using HAL_FLASH_OB_Unlock() and\r
+                HAL_FLASH_OB_Lock() functions\r
+           (++) Launch the reload of the option bytes using HAL_FLASH_Launch() function.\r
+                In this case, a reset is generated\r
+\r
+    [..]\r
+      In addition to these functions, this driver includes a set of macros allowing\r
+      to handle the following operations:\r
+       (+) Set the latency\r
+       (+) Enable/Disable the prefetch buffer\r
+       (+) Enable/Disable the Instruction cache and the Data cache\r
+       (+) Reset the Instruction cache and the Data cache\r
+       (+) Enable/Disable the Flash power-down during low-power run and sleep modes\r
+       (+) Enable/Disable the Flash interrupts\r
+       (+) Monitor the Flash flags status\r
+\r
+ @endverbatim\r
+  ******************************************************************************\r
+  * @attention\r
+  *\r
+  * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.\r
+  * All rights reserved.</center></h2>\r
+  *\r
+  * This software component is licensed by ST under BSD 3-Clause license,\r
+  * the "License"; You may not use this file except in compliance with the\r
+  * License. You may obtain a copy of the License at:\r
+  *                       opensource.org/licenses/BSD-3-Clause\r
+  *\r
+  ******************************************************************************\r
+  */\r
+\r
+/* Includes ------------------------------------------------------------------*/\r
+#include "stm32l4xx_hal.h"\r
+\r
+/** @addtogroup STM32L4xx_HAL_Driver\r
+  * @{\r
+  */\r
+\r
+/** @defgroup FLASH FLASH\r
+  * @brief FLASH HAL module driver\r
+  * @{\r
+  */\r
+\r
+#ifdef HAL_FLASH_MODULE_ENABLED\r
+\r
+/* Private typedef -----------------------------------------------------------*/\r
+/* Private defines -----------------------------------------------------------*/\r
+#if defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)\r
+#define FLASH_NB_DOUBLE_WORDS_IN_ROW  64\r
+#else\r
+#define FLASH_NB_DOUBLE_WORDS_IN_ROW  32\r
+#endif\r
+/* Private macros ------------------------------------------------------------*/\r
+/* Private variables ---------------------------------------------------------*/\r
+/** @defgroup FLASH_Private_Variables FLASH Private Variables\r
+ * @{\r
+ */\r
+/**\r
+  * @brief  Variable used for Program/Erase sectors under interruption\r
+  */\r
+FLASH_ProcessTypeDef pFlash = {.Lock = HAL_UNLOCKED, \\r
+                               .ErrorCode = HAL_FLASH_ERROR_NONE, \\r
+                               .ProcedureOnGoing = FLASH_PROC_NONE, \\r
+                               .Address = 0U, \\r
+                               .Bank = FLASH_BANK_1, \\r
+                               .Page = 0U, \\r
+                               .NbPagesToErase = 0U, \\r
+                               .CacheToReactivate = FLASH_CACHE_DISABLED};\r
+/**\r
+  * @}\r
+  */\r
+\r
+/* Private function prototypes -----------------------------------------------*/\r
+/** @defgroup FLASH_Private_Functions FLASH Private Functions\r
+ * @{\r
+ */\r
+static void          FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data);\r
+static void          FLASH_Program_Fast(uint32_t Address, uint32_t DataAddress);\r
+/**\r
+  * @}\r
+  */\r
+\r
+/* Exported functions --------------------------------------------------------*/\r
+/** @defgroup FLASH_Exported_Functions FLASH Exported Functions\r
+  * @{\r
+  */\r
+\r
+/** @defgroup FLASH_Exported_Functions_Group1 Programming operation functions\r
+ *  @brief   Programming operation functions\r
+ *\r
+@verbatim\r
+ ===============================================================================\r
+                  ##### Programming operation functions #####\r
+ ===============================================================================\r
+    [..]\r
+    This subsection provides a set of functions allowing to manage the FLASH\r
+    program operations.\r
+\r
+@endverbatim\r
+  * @{\r
+  */\r
+\r
+/**\r
+  * @brief  Program double word or fast program of a row at a specified address.\r
+  * @param  TypeProgram:  Indicate the way to program at a specified address.\r
+  *                           This parameter can be a value of @ref FLASH_Type_Program\r
+  * @param  Address:  specifies the address to be programmed.\r
+  * @param  Data: specifies the data to be programmed\r
+  *                This parameter is the data for the double word program and the address where\r
+  *                are stored the data for the row fast program\r
+  *\r
+  * @retval HAL_StatusTypeDef HAL Status\r
+  */\r
+HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data)\r
+{\r
+  HAL_StatusTypeDef status;\r
+  uint32_t prog_bit = 0;\r
+\r
+  /* Process Locked */\r
+  __HAL_LOCK(&pFlash);\r
+\r
+  /* Check the parameters */\r
+  assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));\r
+\r
+  /* Wait for last operation to be completed */\r
+  status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);\r
+\r
+  if(status == HAL_OK)\r
+  {\r
+    pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;\r
+\r
+    /* Deactivate the data cache if they are activated to avoid data misbehavior */\r
+    if(READ_BIT(FLASH->ACR, FLASH_ACR_DCEN) != 0U)\r
+    {\r
+      /* Disable data cache  */\r
+      __HAL_FLASH_DATA_CACHE_DISABLE();\r
+      pFlash.CacheToReactivate = FLASH_CACHE_DCACHE_ENABLED;\r
+    }\r
+    else\r
+    {\r
+      pFlash.CacheToReactivate = FLASH_CACHE_DISABLED;\r
+    }\r
+\r
+    if(TypeProgram == FLASH_TYPEPROGRAM_DOUBLEWORD)\r
+    {\r
+      /* Program double-word (64-bit) at a specified address */\r
+      FLASH_Program_DoubleWord(Address, Data);\r
+      prog_bit = FLASH_CR_PG;\r
+    }\r
+    else if((TypeProgram == FLASH_TYPEPROGRAM_FAST) || (TypeProgram == FLASH_TYPEPROGRAM_FAST_AND_LAST))\r
+    {\r
+      /* Fast program a 32 row double-word (64-bit) at a specified address */\r
+      FLASH_Program_Fast(Address, (uint32_t)Data);\r
+\r
+      /* If it is the last row, the bit will be cleared at the end of the operation */\r
+      if(TypeProgram == FLASH_TYPEPROGRAM_FAST_AND_LAST)\r
+      {\r
+        prog_bit = FLASH_CR_FSTPG;\r
+      }\r
+    }\r
+    else\r
+    {\r
+      /* Nothing to do */\r
+    }\r
+\r
+    /* Wait for last operation to be completed */\r
+    status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);\r
+\r
+    /* If the program operation is completed, disable the PG or FSTPG Bit */\r
+    if (prog_bit != 0U)\r
+    {\r
+      CLEAR_BIT(FLASH->CR, prog_bit);\r
+    }\r
+\r
+    /* Flush the caches to be sure of the data consistency */\r
+    FLASH_FlushCaches();\r
+  }\r
+\r
+  /* Process Unlocked */\r
+  __HAL_UNLOCK(&pFlash);\r
+\r
+  return status;\r
+}\r
+\r
+/**\r
+  * @brief  Program double word or fast program of a row at a specified address with interrupt enabled.\r
+  * @param  TypeProgram:  Indicate the way to program at a specified address.\r
+  *                           This parameter can be a value of @ref FLASH_Type_Program\r
+  * @param  Address:  specifies the address to be programmed.\r
+  * @param  Data: specifies the data to be programmed\r
+  *                This parameter is the data for the double word program and the address where\r
+  *                are stored the data for the row fast program\r
+  *\r
+  * @retval HAL Status\r
+  */\r
+HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data)\r
+{\r
+  HAL_StatusTypeDef status = HAL_OK;\r
+\r
+  /* Check the parameters */\r
+  assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));\r
+\r
+  /* Process Locked */\r
+  __HAL_LOCK(&pFlash);\r
+\r
+  pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;\r
+\r
+  /* Deactivate the data cache if they are activated to avoid data misbehavior */\r
+  if(READ_BIT(FLASH->ACR, FLASH_ACR_DCEN) != 0U)\r
+  {\r
+    /* Disable data cache  */\r
+    __HAL_FLASH_DATA_CACHE_DISABLE();\r
+    pFlash.CacheToReactivate = FLASH_CACHE_DCACHE_ENABLED;\r
+  }\r
+  else\r
+  {\r
+    pFlash.CacheToReactivate = FLASH_CACHE_DISABLED;\r
+  }\r
+\r
+  /* Set internal variables used by the IRQ handler */\r
+  if(TypeProgram == FLASH_TYPEPROGRAM_FAST_AND_LAST)\r
+  {\r
+    pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAM_LAST;\r
+  }\r
+  else\r
+  {\r
+    pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAM;\r
+  }\r
+  pFlash.Address = Address;\r
+\r
+  /* Enable End of Operation and Error interrupts */\r
+  __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP | FLASH_IT_OPERR);\r
+\r
+  if(TypeProgram == FLASH_TYPEPROGRAM_DOUBLEWORD)\r
+  {\r
+    /* Program double-word (64-bit) at a specified address */\r
+    FLASH_Program_DoubleWord(Address, Data);\r
+  }\r
+  else if((TypeProgram == FLASH_TYPEPROGRAM_FAST) || (TypeProgram == FLASH_TYPEPROGRAM_FAST_AND_LAST))\r
+  {\r
+    /* Fast program a 32 row double-word (64-bit) at a specified address */\r
+    FLASH_Program_Fast(Address, (uint32_t)Data);\r
+  }\r
+  else\r
+  {\r
+    /* Nothing to do */\r
+  }\r
+\r
+  return status;\r
+}\r
+\r
+/**\r
+  * @brief Handle FLASH interrupt request.\r
+  * @retval None\r
+  */\r
+void HAL_FLASH_IRQHandler(void)\r
+{\r
+  uint32_t tmp_page;\r
+  uint32_t error;\r
+  FLASH_ProcedureTypeDef procedure;\r
+\r
+  /* If the operation is completed, disable the PG, PNB, MER1, MER2 and PER Bit */\r
+  CLEAR_BIT(FLASH->CR, (FLASH_CR_PG | FLASH_CR_MER1 | FLASH_CR_PER | FLASH_CR_PNB));\r
+#if defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || \\r
+    defined (STM32L496xx) || defined (STM32L4A6xx) || \\r
+    defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)\r
+  CLEAR_BIT(FLASH->CR, FLASH_CR_MER2);\r
+#endif\r
+\r
+  /* Disable the FSTPG Bit only if it is the last row programmed */\r
+  if(pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAM_LAST)\r
+  {\r
+    CLEAR_BIT(FLASH->CR, FLASH_CR_FSTPG);\r
+  }\r
+\r
+  /* Check FLASH operation error flags */\r
+  error = (FLASH->SR & FLASH_FLAG_SR_ERRORS);\r
+  error |= (FLASH->ECCR & FLASH_FLAG_ECCC);\r
+\r
+  if (error !=0U)\r
+  {\r
+    /*Save the error code*/\r
+    pFlash.ErrorCode |= error;\r
+\r
+    /* Clear error programming flags */\r
+    __HAL_FLASH_CLEAR_FLAG(error);\r
+\r
+    /* Flush the caches to be sure of the data consistency */\r
+    FLASH_FlushCaches() ;\r
+\r
+    /* FLASH error interrupt user callback */\r
+    procedure = pFlash.ProcedureOnGoing;\r
+    if(procedure == FLASH_PROC_PAGE_ERASE)\r
+    {\r
+       HAL_FLASH_OperationErrorCallback(pFlash.Page);\r
+    }\r
+    else if(procedure == FLASH_PROC_MASS_ERASE)\r
+    {\r
+        HAL_FLASH_OperationErrorCallback(pFlash.Bank);\r
+    }\r
+    else if((procedure == FLASH_PROC_PROGRAM) ||\r
+            (procedure == FLASH_PROC_PROGRAM_LAST))\r
+    {\r
+       HAL_FLASH_OperationErrorCallback(pFlash.Address);\r
+    }\r
+    else\r
+    {\r
+       HAL_FLASH_OperationErrorCallback(0U);\r
+    }\r
+\r
+    /*Stop the procedure ongoing*/\r
+    pFlash.ProcedureOnGoing = FLASH_PROC_NONE;\r
+  }\r
+\r
+  /* Check FLASH End of Operation flag  */\r
+  if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP) != 0U)\r
+  {\r
+    /* Clear FLASH End of Operation pending bit */\r
+    __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);\r
+\r
+    if(pFlash.ProcedureOnGoing == FLASH_PROC_PAGE_ERASE)\r
+    {\r
+      /* Nb of pages to erased can be decreased */\r
+      pFlash.NbPagesToErase--;\r
+\r
+      /* Check if there are still pages to erase*/\r
+      if(pFlash.NbPagesToErase != 0U)\r
+      {\r
+        /* Indicate user which page has been erased*/\r
+        HAL_FLASH_EndOfOperationCallback(pFlash.Page);\r
+\r
+        /* Increment page number */\r
+        pFlash.Page++;\r
+        tmp_page = pFlash.Page;\r
+        FLASH_PageErase(tmp_page, pFlash.Bank);\r
+      }\r
+      else\r
+      {\r
+        /* No more pages to Erase */\r
+        /* Reset Address and stop Erase pages procedure */\r
+        pFlash.Page = 0xFFFFFFFFU;\r
+        pFlash.ProcedureOnGoing = FLASH_PROC_NONE;\r
+\r
+        /* Flush the caches to be sure of the data consistency */\r
+        FLASH_FlushCaches() ;\r
+\r
+        /* FLASH EOP interrupt user callback */\r
+        HAL_FLASH_EndOfOperationCallback(pFlash.Page);\r
+      }\r
+    }\r
+    else\r
+    {\r
+      /* Flush the caches to be sure of the data consistency */\r
+      FLASH_FlushCaches() ;\r
+\r
+      procedure = pFlash.ProcedureOnGoing;\r
+      if(procedure == FLASH_PROC_MASS_ERASE)\r
+      {\r
+        /* MassErase ended. Return the selected bank */\r
+        /* FLASH EOP interrupt user callback */\r
+        HAL_FLASH_EndOfOperationCallback(pFlash.Bank);\r
+      }\r
+      else if((procedure == FLASH_PROC_PROGRAM) ||\r
+              (procedure == FLASH_PROC_PROGRAM_LAST))\r
+      {\r
+        /* Program ended. Return the selected address */\r
+        /* FLASH EOP interrupt user callback */\r
+        HAL_FLASH_EndOfOperationCallback(pFlash.Address);\r
+      }\r
+      else\r
+      {\r
+        /* Nothing to do */\r
+      }\r
+\r
+      /*Clear the procedure ongoing*/\r
+      pFlash.ProcedureOnGoing = FLASH_PROC_NONE;\r
+    }\r
+  }\r
+\r
+  if(pFlash.ProcedureOnGoing == FLASH_PROC_NONE)\r
+  {\r
+    /* Disable End of Operation and Error interrupts */\r
+    __HAL_FLASH_DISABLE_IT(FLASH_IT_EOP | FLASH_IT_OPERR);\r
+\r
+    /* Process Unlocked */\r
+    __HAL_UNLOCK(&pFlash);\r
+  }\r
+}\r
+\r
+/**\r
+  * @brief  FLASH end of operation interrupt callback.\r
+  * @param  ReturnValue: The value saved in this parameter depends on the ongoing procedure\r
+  *                  Mass Erase: Bank number which has been requested to erase\r
+  *                  Page Erase: Page which has been erased\r
+  *                    (if 0xFFFFFFFF, it means that all the selected pages have been erased)\r
+  *                  Program: Address which was selected for data program\r
+  * @retval None\r
+  */\r
+__weak void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue)\r
+{\r
+  /* Prevent unused argument(s) compilation warning */\r
+  UNUSED(ReturnValue);\r
+\r
+  /* NOTE : This function should not be modified, when the callback is needed,\r
+            the HAL_FLASH_EndOfOperationCallback could be implemented in the user file\r
+   */\r
+}\r
+\r
+/**\r
+  * @brief  FLASH operation error interrupt callback.\r
+  * @param  ReturnValue: The value saved in this parameter depends on the ongoing procedure\r
+  *                 Mass Erase: Bank number which has been requested to erase\r
+  *                 Page Erase: Page number which returned an error\r
+  *                 Program: Address which was selected for data program\r
+  * @retval None\r
+  */\r
+__weak void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue)\r
+{\r
+  /* Prevent unused argument(s) compilation warning */\r
+  UNUSED(ReturnValue);\r
+\r
+  /* NOTE : This function should not be modified, when the callback is needed,\r
+            the HAL_FLASH_OperationErrorCallback could be implemented in the user file\r
+   */\r
+}\r
+\r
+/**\r
+  * @}\r
+  */\r
+\r
+/** @defgroup FLASH_Exported_Functions_Group2 Peripheral Control functions\r
+ *  @brief   Management functions\r
+ *\r
+@verbatim\r
+ ===============================================================================\r
+                      ##### Peripheral Control functions #####\r
+ ===============================================================================\r
+    [..]\r
+    This subsection provides a set of functions allowing to control the FLASH\r
+    memory operations.\r
+\r
+@endverbatim\r
+  * @{\r
+  */\r
+\r
+/**\r
+  * @brief  Unlock the FLASH control register access.\r
+  * @retval HAL Status\r
+  */\r
+HAL_StatusTypeDef HAL_FLASH_Unlock(void)\r
+{\r
+  HAL_StatusTypeDef status = HAL_OK;\r
+\r
+  if(READ_BIT(FLASH->CR, FLASH_CR_LOCK) != 0U)\r
+  {\r
+    /* Authorize the FLASH Registers access */\r
+    WRITE_REG(FLASH->KEYR, FLASH_KEY1);\r
+    WRITE_REG(FLASH->KEYR, FLASH_KEY2);\r
+\r
+    /* Verify Flash is unlocked */\r
+    if(READ_BIT(FLASH->CR, FLASH_CR_LOCK) != 0U)\r
+    {\r
+      status = HAL_ERROR;\r
+    }\r
+  }\r
+\r
+  return status;\r
+}\r
+\r
+/**\r
+  * @brief  Lock the FLASH control register access.\r
+  * @retval HAL Status\r
+  */\r
+HAL_StatusTypeDef HAL_FLASH_Lock(void)\r
+{\r
+  /* Set the LOCK Bit to lock the FLASH Registers access */\r
+  SET_BIT(FLASH->CR, FLASH_CR_LOCK);\r
+\r
+  return HAL_OK;\r
+}\r
+\r
+/**\r
+  * @brief  Unlock the FLASH Option Bytes Registers access.\r
+  * @retval HAL Status\r
+  */\r
+HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void)\r
+{\r
+  if(READ_BIT(FLASH->CR, FLASH_CR_OPTLOCK) != 0U)\r
+  {\r
+    /* Authorizes the Option Byte register programming */\r
+    WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY1);\r
+    WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY2);\r
+  }\r
+  else\r
+  {\r
+    return HAL_ERROR;\r
+  }\r
+\r
+  return HAL_OK;\r
+}\r
+\r
+/**\r
+  * @brief  Lock the FLASH Option Bytes Registers access.\r
+  * @retval HAL Status\r
+  */\r
+HAL_StatusTypeDef HAL_FLASH_OB_Lock(void)\r
+{\r
+  /* Set the OPTLOCK Bit to lock the FLASH Option Byte Registers access */\r
+  SET_BIT(FLASH->CR, FLASH_CR_OPTLOCK);\r
+\r
+  return HAL_OK;\r
+}\r
+\r
+/**\r
+  * @brief  Launch the option byte loading.\r
+  * @retval HAL Status\r
+  */\r
+HAL_StatusTypeDef HAL_FLASH_OB_Launch(void)\r
+{\r
+  /* Set the bit to force the option byte reloading */\r
+  SET_BIT(FLASH->CR, FLASH_CR_OBL_LAUNCH);\r
+\r
+  /* Wait for last operation to be completed */\r
+  return(FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE));\r
+}\r
+\r
+/**\r
+  * @}\r
+  */\r
+\r
+/** @defgroup FLASH_Exported_Functions_Group3 Peripheral State and Errors functions\r
+ *  @brief   Peripheral Errors functions\r
+ *\r
+@verbatim\r
+ ===============================================================================\r
+                ##### Peripheral Errors functions #####\r
+ ===============================================================================\r
+    [..]\r
+    This subsection permits to get in run-time Errors of the FLASH peripheral.\r
+\r
+@endverbatim\r
+  * @{\r
+  */\r
+\r
+/**\r
+  * @brief  Get the specific FLASH error flag.\r
+  * @retval FLASH_ErrorCode: The returned value can be:\r
+  *            @arg HAL_FLASH_ERROR_RD: FLASH Read Protection error flag (PCROP)\r
+  *            @arg HAL_FLASH_ERROR_PGS: FLASH Programming Sequence error flag\r
+  *            @arg HAL_FLASH_ERROR_PGP: FLASH Programming Parallelism error flag\r
+  *            @arg HAL_FLASH_ERROR_PGA: FLASH Programming Alignment error flag\r
+  *            @arg HAL_FLASH_ERROR_WRP: FLASH Write protected error flag\r
+  *            @arg HAL_FLASH_ERROR_OPERATION: FLASH operation Error flag\r
+  *            @arg HAL_FLASH_ERROR_NONE: No error set\r
+  *            @arg HAL_FLASH_ERROR_OP: FLASH Operation error\r
+  *            @arg HAL_FLASH_ERROR_PROG: FLASH Programming error\r
+  *            @arg HAL_FLASH_ERROR_WRP: FLASH Write protection error\r
+  *            @arg HAL_FLASH_ERROR_PGA: FLASH Programming alignment error\r
+  *            @arg HAL_FLASH_ERROR_SIZ: FLASH Size error\r
+  *            @arg HAL_FLASH_ERROR_PGS: FLASH Programming sequence error\r
+  *            @arg HAL_FLASH_ERROR_MIS: FLASH Fast programming data miss error\r
+  *            @arg HAL_FLASH_ERROR_FAST: FLASH Fast programming error\r
+  *            @arg HAL_FLASH_ERROR_RD: FLASH PCROP read error\r
+  *            @arg HAL_FLASH_ERROR_OPTV: FLASH Option validity error\r
+  *            @arg FLASH_FLAG_PEMPTY : FLASH Boot from not programmed flash (apply only for STM32L43x/STM32L44x devices)\r
+  *            @arg HAL_FLASH_ERROR_ECCD: FLASH two ECC errors have been detected\r
+  */\r
+uint32_t HAL_FLASH_GetError(void)\r
+{\r
+   return pFlash.ErrorCode;\r
+}\r
+\r
+/**\r
+  * @}\r
+  */\r
+\r
+/**\r
+  * @}\r
+  */\r
+\r
+/* Private functions ---------------------------------------------------------*/\r
+\r
+/** @addtogroup FLASH_Private_Functions\r
+  * @{\r
+  */\r
+\r
+/**\r
+  * @brief  Wait for a FLASH operation to complete.\r
+  * @param  Timeout: maximum flash operation timeout\r
+  * @retval HAL_StatusTypeDef HAL Status\r
+  */\r
+HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)\r
+{\r
+  /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.\r
+     Even if the FLASH operation fails, the BUSY flag will be reset and an error\r
+     flag will be set */\r
+\r
+  uint32_t tickstart = HAL_GetTick();\r
+  uint32_t error;\r
+\r
+  while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY))\r
+  {\r
+    if(Timeout != HAL_MAX_DELAY)\r
+    {\r
+      if((HAL_GetTick() - tickstart) >= Timeout)\r
+      {\r
+        return HAL_TIMEOUT;\r
+      }\r
+    }\r
+  }\r
+\r
+  error = (FLASH->SR & FLASH_FLAG_SR_ERRORS);\r
+  error |= (FLASH->ECCR & FLASH_FLAG_ECCD);\r
+\r
+  if(error != 0u)\r
+  {\r
+    /*Save the error code*/\r
+    pFlash.ErrorCode |= error;\r
+\r
+    /* Clear error programming flags */\r
+    __HAL_FLASH_CLEAR_FLAG(error);\r
+\r
+    return HAL_ERROR;\r
+  }\r
+\r
+  /* Check FLASH End of Operation flag  */\r
+  if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))\r
+  {\r
+    /* Clear FLASH End of Operation pending bit */\r
+    __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);\r
+  }\r
+\r
+  /* If there is an error flag set */\r
+  return HAL_OK;\r
+}\r
+\r
+/**\r
+  * @brief  Program double-word (64-bit) at a specified address.\r
+  * @param  Address: specifies the address to be programmed.\r
+  * @param  Data: specifies the data to be programmed.\r
+  * @retval None\r
+  */\r
+static void FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data)\r
+{\r
+  /* Check the parameters */\r
+  assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));\r
+\r
+  /* Set PG bit */\r
+  SET_BIT(FLASH->CR, FLASH_CR_PG);\r
+\r
+  /* Program first word */\r
+  *(__IO uint32_t*)Address = (uint32_t)Data;\r
+\r
+  /* Barrier to ensure programming is performed in 2 steps, in right order\r
+    (independently of compiler optimization behavior) */\r
+  __ISB();\r
+\r
+  /* Program second word */\r
+  *(__IO uint32_t*)(Address+4U) = (uint32_t)(Data >> 32);\r
+}\r
+\r
+/**\r
+  * @brief  Fast program a row double-word (64-bit) at a specified address.\r
+  * @param  Address: specifies the address to be programmed.\r
+  * @param  DataAddress: specifies the address where the data are stored.\r
+  * @retval None\r
+  */\r
+static void FLASH_Program_Fast(uint32_t Address, uint32_t DataAddress)\r
+{\r
+  uint32_t primask_bit;\r
+  uint8_t row_index = (2*FLASH_NB_DOUBLE_WORDS_IN_ROW);\r
+  __IO uint32_t *dest_addr = (__IO uint32_t*)Address;\r
+  __IO uint32_t *src_addr = (__IO uint32_t*)DataAddress;\r
+\r
+  /* Check the parameters */\r
+  assert_param(IS_FLASH_MAIN_MEM_ADDRESS(Address));\r
+\r
+  /* Set FSTPG bit */\r
+  SET_BIT(FLASH->CR, FLASH_CR_FSTPG);\r
+\r
+  /* Disable interrupts to avoid any interruption during the loop */\r
+  primask_bit = __get_PRIMASK();\r
+  __disable_irq();\r
+\r
+  /* Program the double word of the row */\r
+  do\r
+  {\r
+    *dest_addr = *src_addr;\r
+    dest_addr++;\r
+    src_addr++;\r
+    row_index--;\r
+  } while (row_index != 0U);\r
+\r
+  /* Re-enable the interrupts */\r
+  __set_PRIMASK(primask_bit);\r
+}\r
+\r
+/**\r
+  * @}\r
+  */\r
+\r
+#endif /* HAL_FLASH_MODULE_ENABLED */\r
+\r
+/**\r
+  * @}\r
+  */\r
+\r
+/**\r
+  * @}\r
+  */\r
+\r
+/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/\r