Subversion Repositories DashDisplay

Rev

Rev 30 | Rev 50 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /**
  2.   ******************************************************************************
  3.   * @file    stm32l1xx_hal_flash.c
  4.   * @author  MCD Application Team
  5.   * @version V1.2.0
  6.   * @date    01-July-2016
  7.   * @brief   FLASH HAL module driver.
  8.   *          This file provides firmware functions to manage the following
  9.   *          functionalities of the internal FLASH memory:
  10.   *           + Program operations functions
  11.   *           + Memory Control functions
  12.   *           + Peripheral State functions
  13.   *        
  14.   @verbatim
  15.   ==============================================================================
  16.                         ##### FLASH peripheral features #####
  17.   ==============================================================================
  18.   [..] The Flash memory interface manages CPU AHB I-Code and D-Code accesses
  19.        to the Flash memory. It implements the erase and program Flash memory operations
  20.        and the read and write protection mechanisms.
  21.  
  22.   [..] The Flash memory interface accelerates code execution with a system of instruction
  23.       prefetch.
  24.  
  25.   [..] The FLASH main features are:
  26.       (+) Flash memory read operations
  27.       (+) Flash memory program/erase operations
  28.       (+) Read / write protections
  29.       (+) Prefetch on I-Code
  30.       (+) Option Bytes programming
  31.  
  32.  
  33.                      ##### How to use this driver #####
  34.   ==============================================================================
  35.   [..]                            
  36.       This driver provides functions and macros to configure and program the FLASH
  37.       memory of all STM32L1xx devices.
  38.    
  39.       (#) FLASH Memory I/O Programming functions: this group includes all needed
  40.           functions to erase and program the main memory:
  41.         (++) Lock and Unlock the FLASH interface
  42.         (++) Erase function: Erase page
  43.         (++) Program functions: Fast Word and Half Page(should be
  44.         executed from internal SRAM).
  45.  
  46.       (#) DATA EEPROM Programming functions: this group includes all
  47.           needed functions to erase and program the DATA EEPROM memory:
  48.         (++) Lock and Unlock the DATA EEPROM interface.
  49.         (++) Erase function: Erase Byte, erase HalfWord, erase Word, erase
  50.              Double Word (should be executed from internal SRAM).
  51.         (++) Program functions: Fast Program Byte, Fast Program Half-Word,
  52.              FastProgramWord, Program Byte, Program Half-Word,
  53.              Program Word and Program Double-Word (should be executed
  54.              from internal SRAM).
  55.  
  56.       (#) FLASH Option Bytes Programming functions: this group includes all needed
  57.           functions to manage the Option Bytes:
  58.         (++) Lock and Unlock the Option Bytes
  59.         (++) Set/Reset the write protection
  60.         (++) Set the Read protection Level
  61.         (++) Program the user Option Bytes
  62.         (++) Launch the Option Bytes loader
  63.         (++) Set/Get the Read protection Level.
  64.         (++) Set/Get the BOR level.
  65.         (++) Get the Write protection.
  66.         (++) Get the user option bytes.
  67.    
  68.       (#) Interrupts and flags management functions : this group
  69.           includes all needed functions to:
  70.         (++) Handle FLASH interrupts
  71.         (++) Wait for last FLASH operation according to its status
  72.         (++) Get error flag status
  73.  
  74.     (#) FLASH Interface configuration functions: this group includes
  75.       the management of following features:
  76.       (++) Enable/Disable the RUN PowerDown mode.
  77.       (++) Enable/Disable the SLEEP PowerDown mode.  
  78.  
  79.     (#) FLASH Peripheral State methods: this group includes
  80.       the management of following features:
  81.       (++) Wait for the FLASH operation
  82.       (++)  Get the specific FLASH error flag
  83.    
  84.   [..] In addition to these function, this driver includes a set of macros allowing
  85.        to handle the following operations:
  86.      
  87.       (+) Set/Get the latency
  88.       (+) Enable/Disable the prefetch buffer
  89.       (+) Enable/Disable the 64 bit Read Access.
  90.       (+) Enable/Disable the Flash power-down
  91.       (+) Enable/Disable the FLASH interrupts
  92.       (+) Monitor the FLASH flags status
  93.          
  94.                  ##### Programming operation functions #####
  95.   ===============================================================================  
  96.      [..]
  97.      This subsection provides a set of functions allowing to manage the FLASH
  98.      program operations.
  99.  
  100.     [..] The FLASH Memory Programming functions, includes the following functions:
  101.      (+) HAL_FLASH_Unlock(void);
  102.      (+) HAL_FLASH_Lock(void);
  103.      (+) HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint32_t Data)
  104.      (+) HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint32_t Data)
  105.    
  106.      [..] Any operation of erase or program should follow these steps:
  107.      (#) Call the HAL_FLASH_Unlock() function to enable the flash control register and
  108.          program memory access.
  109.      (#) Call the desired function to erase page or program data.
  110.      (#) Call the HAL_FLASH_Lock() to disable the flash program memory access
  111.         (recommended to protect the FLASH memory against possible unwanted operation).
  112.  
  113.                ##### Option Bytes Programming functions #####
  114.    ==============================================================================  
  115.  
  116.      [..] The FLASH_Option Bytes Programming_functions, includes the following functions:
  117.      (+) HAL_FLASH_OB_Unlock(void);
  118.      (+) HAL_FLASH_OB_Lock(void);
  119.      (+) HAL_FLASH_OB_Launch(void);
  120.      (+) HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit);
  121.      (+) HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit);
  122.    
  123.      [..] Any operation of erase or program should follow these steps:
  124.      (#) Call the HAL_FLASH_OB_Unlock() function to enable the Flash option control
  125.          register access.
  126.      (#) Call the following functions to program the desired option bytes.
  127.          (++) HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit);      
  128.      (#) Once all needed option bytes to be programmed are correctly written, call the
  129.          HAL_FLASH_OB_Launch(void) function to launch the Option Bytes programming process.
  130.      (#) Call the HAL_FLASH_OB_Lock() to disable the Flash option control register access (recommended
  131.          to protect the option Bytes against possible unwanted operations).
  132.  
  133.     [..] Proprietary code Read Out Protection (PcROP):    
  134.     (#) The PcROP sector is selected by using the same option bytes as the Write
  135.         protection. As a result, these 2 options are exclusive each other.
  136.     (#) To activate PCROP mode for Flash sectors(s), you need to follow the sequence below:
  137.         (++) Use this function HAL_FLASHEx_AdvOBProgram with PCROPState = OB_PCROP_STATE_ENABLE.
  138.  
  139.   @endverbatim
  140.   ******************************************************************************
  141.   * @attention
  142.   *
  143.   * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  144.   *
  145.   * Redistribution and use in source and binary forms, with or without modification,
  146.   * are permitted provided that the following conditions are met:
  147.   *   1. Redistributions of source code must retain the above copyright notice,
  148.   *      this list of conditions and the following disclaimer.
  149.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  150.   *      this list of conditions and the following disclaimer in the documentation
  151.   *      and/or other materials provided with the distribution.
  152.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  153.   *      may be used to endorse or promote products derived from this software
  154.   *      without specific prior written permission.
  155.   *
  156.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  157.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  158.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  159.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  160.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  161.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  162.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  163.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  164.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  165.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  166.   *
  167.   ******************************************************************************  
  168.   */
  169.  
  170. /* Includes ------------------------------------------------------------------*/
  171. #include "stm32l1xx_hal.h"
  172.  
  173. /** @addtogroup STM32L1xx_HAL_Driver
  174.   * @{
  175.   */
  176.  
  177. #ifdef HAL_FLASH_MODULE_ENABLED
  178.  
  179. /** @defgroup FLASH FLASH
  180.   * @brief FLASH HAL module driver
  181.   * @{
  182.   */
  183.  
  184. /* Private typedef -----------------------------------------------------------*/
  185. /* Private define ------------------------------------------------------------*/
  186. /** @defgroup FLASH_Private_Constants FLASH Private Constants
  187.   * @{
  188.   */
  189. /**
  190.   * @}
  191.   */
  192.  
  193. /* Private macro ---------------------------- ---------------------------------*/
  194. /** @defgroup FLASH_Private_Macros FLASH Private Macros
  195.   * @{
  196.   */
  197.  
  198. /**
  199.   * @}
  200.   */
  201.  
  202. /* Private variables ---------------------------------------------------------*/
  203. /** @defgroup FLASH_Private_Variables FLASH Private Variables
  204.   * @{
  205.   */
  206. /* Variables used for Erase pages under interruption*/
  207. FLASH_ProcessTypeDef pFlash;
  208. /**
  209.   * @}
  210.   */
  211.  
  212. /* Private function prototypes -----------------------------------------------*/
  213. /** @defgroup FLASH_Private_Functions FLASH Private Functions
  214.   * @{
  215.   */
  216. static  void   FLASH_SetErrorCode(void);
  217. extern void    FLASH_PageErase(uint32_t PageAddress);
  218. /**
  219.   * @}
  220.   */
  221.  
  222. /* Exported functions ---------------------------------------------------------*/
  223. /** @defgroup FLASH_Exported_Functions FLASH Exported Functions
  224.   * @{
  225.   */
  226.  
  227. /** @defgroup FLASH_Exported_Functions_Group1 Programming operation functions
  228.   *  @brief   Programming operation functions
  229.   *
  230. @verbatim  
  231. @endverbatim
  232.   * @{
  233.   */
  234.  
  235. /**
  236.   * @brief  Program word at a specified address
  237.   * @note   To correctly run this function, the HAL_FLASH_Unlock() function
  238.   *         must be called before.
  239.   *         Call the HAL_FLASH_Lock() to disable the flash memory access
  240.   *         (recommended to protect the FLASH memory against possible unwanted operation).
  241.   *
  242.   * @param  TypeProgram:  Indicate the way to program at a specified address.
  243.   *                       This parameter can be a value of @ref FLASH_Type_Program
  244.   * @param  Address:      Specifies the address to be programmed.
  245.   * @param  Data:         Specifies the data to be programmed
  246.   *
  247.   * @retval HAL_StatusTypeDef HAL Status
  248.   */
  249. HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint32_t Data)
  250. {
  251.   HAL_StatusTypeDef status = HAL_ERROR;
  252.  
  253.   /* Process Locked */
  254.   __HAL_LOCK(&pFlash);
  255.  
  256.   /* Check the parameters */
  257.   assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
  258.   assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
  259.  
  260.   /* Wait for last operation to be completed */
  261.   status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  262.  
  263.   if(status == HAL_OK)
  264.   {
  265.     /* Clean the error context */
  266.     pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  267.  
  268.     /*Program word (32-bit) at a specified address.*/
  269.     *(__IO uint32_t *)Address = Data;
  270.  
  271.     /* Wait for last operation to be completed */
  272.     status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  273.   }
  274.  
  275.   /* Process Unlocked */
  276.   __HAL_UNLOCK(&pFlash);
  277.  
  278.   return status;
  279. }
  280.  
  281. /**
  282.   * @brief   Program word at a specified address  with interrupt enabled.
  283.   *
  284.   * @param  TypeProgram: Indicate the way to program at a specified address.
  285.   *                      This parameter can be a value of @ref FLASH_Type_Program
  286.   * @param  Address:     Specifies the address to be programmed.
  287.   * @param  Data:        Specifies the data to be programmed
  288.   *
  289.   * @retval HAL_StatusTypeDef HAL Status
  290.   */
  291. HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint32_t Data)
  292. {
  293.   HAL_StatusTypeDef status = HAL_OK;
  294.  
  295.   /* Process Locked */
  296.   __HAL_LOCK(&pFlash);
  297.  
  298.   /* Check the parameters */
  299.   assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
  300.   assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
  301.  
  302.   /* Enable End of FLASH Operation and Error source interrupts */
  303.   __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP | FLASH_IT_ERR);
  304.  
  305.   pFlash.Address = Address;
  306.   pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAM;
  307.   /* Clean the error context */
  308.   pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  309.  
  310.   if(TypeProgram == FLASH_TYPEPROGRAM_WORD)
  311.   {
  312.     /* Program word (32-bit) at a specified address. */
  313.     *(__IO uint32_t *)Address = Data;
  314.   }
  315.   return status;
  316. }
  317.  
  318. /**
  319.   * @brief This function handles FLASH interrupt request.
  320.   * @retval None
  321.   */
  322. void HAL_FLASH_IRQHandler(void)
  323. {
  324.   uint32_t addresstmp = 0;
  325.  
  326.   /* Check FLASH operation error flags */
  327.   if( __HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR)     ||
  328.       __HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR)     ||
  329.       __HAL_FLASH_GET_FLAG(FLASH_FLAG_SIZERR)     ||
  330. #if defined(FLASH_SR_RDERR)
  331.       __HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR)      ||
  332. #endif /* FLASH_SR_RDERR */
  333. #if defined(FLASH_SR_OPTVERRUSR)
  334.       __HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERRUSR) ||
  335. #endif /* FLASH_SR_OPTVERRUSR */
  336.       __HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR) )
  337.   {
  338.     if(pFlash.ProcedureOnGoing == FLASH_PROC_PAGEERASE)
  339.     {
  340.       /* Return the faulty sector */
  341.       addresstmp = pFlash.Page;
  342.       pFlash.Page = 0xFFFFFFFFU;
  343.     }
  344.     else
  345.     {
  346.       /* Return the faulty address */
  347.       addresstmp = pFlash.Address;
  348.     }
  349.     /* Save the Error code */
  350.     FLASH_SetErrorCode();
  351.    
  352.     /* FLASH error interrupt user callback */
  353.     HAL_FLASH_OperationErrorCallback(addresstmp);
  354.  
  355.     /* Stop the procedure ongoing */
  356.     pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  357.   }
  358.  
  359.   /* Check FLASH End of Operation flag  */
  360.   if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
  361.   {
  362.     /* Clear FLASH End of Operation pending bit */
  363.     __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
  364.    
  365.     /* Process can continue only if no error detected */
  366.     if(pFlash.ProcedureOnGoing != FLASH_PROC_NONE)
  367.     {
  368.       if(pFlash.ProcedureOnGoing == FLASH_PROC_PAGEERASE)
  369.       {
  370.         /* Nb of pages to erased can be decreased */
  371.         pFlash.NbPagesToErase--;
  372.  
  373.         /* Check if there are still pages to erase */
  374.         if(pFlash.NbPagesToErase != 0)
  375.         {
  376.           addresstmp = pFlash.Page;
  377.           /*Indicate user which sector has been erased */
  378.           HAL_FLASH_EndOfOperationCallback(addresstmp);
  379.  
  380.           /*Increment sector number*/
  381.           addresstmp = pFlash.Page + FLASH_PAGE_SIZE;
  382.           pFlash.Page = addresstmp;
  383.  
  384.           /* If the erase operation is completed, disable the ERASE Bit */
  385.           CLEAR_BIT(FLASH->PECR, FLASH_PECR_ERASE);
  386.  
  387.           FLASH_PageErase(addresstmp);
  388.         }
  389.         else
  390.         {
  391.           /* No more pages to Erase, user callback can be called. */
  392.           /* Reset Sector and stop Erase pages procedure */
  393.           pFlash.Page = addresstmp = 0xFFFFFFFFU;
  394.           pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  395.           /* FLASH EOP interrupt user callback */
  396.           HAL_FLASH_EndOfOperationCallback(addresstmp);
  397.         }
  398.       }
  399.       else
  400.       {
  401.           /* If the program operation is completed, disable the PROG Bit */
  402.           CLEAR_BIT(FLASH->PECR, FLASH_PECR_PROG);
  403.  
  404.           /* Program ended. Return the selected address */
  405.           /* FLASH EOP interrupt user callback */
  406.           HAL_FLASH_EndOfOperationCallback(pFlash.Address);
  407.        
  408.           /* Reset Address and stop Program procedure */
  409.           pFlash.Address = 0xFFFFFFFFU;
  410.           pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  411.       }
  412.     }
  413.   }
  414.  
  415.  
  416.   if(pFlash.ProcedureOnGoing == FLASH_PROC_NONE)
  417.   {
  418.     /* Operation is completed, disable the PROG and ERASE */
  419.     CLEAR_BIT(FLASH->PECR, (FLASH_PECR_ERASE | FLASH_PECR_PROG));
  420.  
  421.     /* Disable End of FLASH Operation and Error source interrupts */
  422.     __HAL_FLASH_DISABLE_IT(FLASH_IT_EOP | FLASH_IT_ERR);
  423.  
  424.     /* Process Unlocked */
  425.     __HAL_UNLOCK(&pFlash);
  426.   }
  427. }
  428.  
  429. /**
  430.   * @brief  FLASH end of operation interrupt callback
  431.   * @param  ReturnValue: The value saved in this parameter depends on the ongoing procedure
  432.   *                 - Pages Erase: Address of the page which has been erased
  433.   *                    (if 0xFFFFFFFF, it means that all the selected pages have been erased)
  434.   *                 - Program: Address which was selected for data program
  435.   * @retval none
  436.   */
  437. __weak void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue)
  438. {
  439.   /* Prevent unused argument(s) compilation warning */
  440.   UNUSED(ReturnValue);
  441.  
  442.   /* NOTE : This function Should not be modified, when the callback is needed,
  443.             the HAL_FLASH_EndOfOperationCallback could be implemented in the user file
  444.    */
  445. }
  446.  
  447. /**
  448.   * @brief  FLASH operation error interrupt callback
  449.   * @param  ReturnValue: The value saved in this parameter depends on the ongoing procedure
  450.   *                 - Pages Erase: Address of the page which returned an error
  451.   *                 - Program: Address which was selected for data program
  452.   * @retval none
  453.   */
  454. __weak void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue)
  455. {
  456.   /* Prevent unused argument(s) compilation warning */
  457.   UNUSED(ReturnValue);
  458.  
  459.   /* NOTE : This function Should not be modified, when the callback is needed,
  460.             the HAL_FLASH_OperationErrorCallback could be implemented in the user file
  461.    */
  462. }
  463.  
  464. /**
  465.   * @}
  466.   */
  467.  
  468. /** @defgroup FLASH_Exported_Functions_Group2 Peripheral Control functions
  469.  *  @brief   management functions
  470.  *
  471. @verbatim  
  472.  ===============================================================================
  473.                       ##### Peripheral Control functions #####
  474.  ===============================================================================  
  475.     [..]
  476.     This subsection provides a set of functions allowing to control the FLASH
  477.     memory operations.
  478.  
  479. @endverbatim
  480.   * @{
  481.   */
  482.  
  483. /**
  484.   * @brief  Unlock the FLASH control register access
  485.   * @retval HAL Status
  486.   */
  487. HAL_StatusTypeDef HAL_FLASH_Unlock(void)
  488. {
  489.   if (HAL_IS_BIT_SET(FLASH->PECR, FLASH_PECR_PRGLOCK))
  490.   {
  491.     /* Unlocking FLASH_PECR register access*/
  492.     if(HAL_IS_BIT_SET(FLASH->PECR, FLASH_PECR_PELOCK))
  493.     {  
  494.       WRITE_REG(FLASH->PEKEYR, FLASH_PEKEY1);
  495.       WRITE_REG(FLASH->PEKEYR, FLASH_PEKEY2);
  496.     }
  497.    
  498.     /* Unlocking the program memory access */
  499.     WRITE_REG(FLASH->PRGKEYR, FLASH_PRGKEY1);
  500.     WRITE_REG(FLASH->PRGKEYR, FLASH_PRGKEY2);  
  501.   }
  502.   else
  503.   {
  504.     return HAL_ERROR;
  505.   }
  506.  
  507.   return HAL_OK;
  508. }
  509.  
  510. /**
  511.   * @brief  Locks the FLASH control register access
  512.   * @retval HAL Status
  513.   */
  514. HAL_StatusTypeDef HAL_FLASH_Lock(void)
  515. {
  516.   /* Set the PRGLOCK Bit to lock the FLASH Registers access */
  517.   SET_BIT(FLASH->PECR, FLASH_PECR_PRGLOCK);
  518.  
  519.   return HAL_OK;  
  520. }
  521.  
  522. /**
  523.   * @brief  Unlock the FLASH Option Control Registers access.
  524.   * @retval HAL Status
  525.   */
  526. HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void)
  527. {
  528.   if(HAL_IS_BIT_SET(FLASH->PECR, FLASH_PECR_OPTLOCK))
  529.   {
  530.     /* Unlocking FLASH_PECR register access*/
  531.     if(HAL_IS_BIT_SET(FLASH->PECR, FLASH_PECR_PELOCK))
  532.     {  
  533.       /* Unlocking FLASH_PECR register access*/
  534.       WRITE_REG(FLASH->PEKEYR, FLASH_PEKEY1);
  535.       WRITE_REG(FLASH->PEKEYR, FLASH_PEKEY2);
  536.     }
  537.  
  538.     /* Unlocking the option bytes block access */
  539.     WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY1);
  540.     WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY2);
  541.   }
  542.   else
  543.   {
  544.     return HAL_ERROR;
  545.   }  
  546.  
  547.   return HAL_OK;  
  548. }
  549.  
  550. /**
  551.   * @brief  Lock the FLASH Option Control Registers access.
  552.   * @retval HAL Status
  553.   */
  554. HAL_StatusTypeDef HAL_FLASH_OB_Lock(void)
  555. {
  556.   /* Set the OPTLOCK Bit to lock the option bytes block access */
  557.   SET_BIT(FLASH->PECR, FLASH_PECR_OPTLOCK);
  558.  
  559.   return HAL_OK;  
  560. }
  561.  
  562. /**
  563.   * @brief  Launch the option byte loading.
  564.   * @note   This function will reset automatically the MCU.
  565.   * @retval HAL Status
  566.   */
  567. HAL_StatusTypeDef HAL_FLASH_OB_Launch(void)
  568. {
  569.   /* Set the OBL_Launch bit to launch the option byte loading */
  570.   SET_BIT(FLASH->PECR, FLASH_PECR_OBL_LAUNCH);
  571.  
  572.   /* Wait for last operation to be completed */
  573.   return(FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE));
  574. }
  575.  
  576. /**
  577.   * @}
  578.   */  
  579.  
  580. /** @defgroup FLASH_Exported_Functions_Group3 Peripheral errors functions
  581.  *  @brief    Peripheral errors functions
  582.  *
  583. @verbatim  
  584.  ===============================================================================
  585.                       ##### Peripheral Errors functions #####
  586.  ===============================================================================  
  587.     [..]
  588.     This subsection permit to get in run-time errors of  the FLASH peripheral.
  589.  
  590. @endverbatim
  591.   * @{
  592.   */
  593.  
  594. /**
  595.   * @brief  Get the specific FLASH error flag.
  596.   * @retval FLASH_ErrorCode The returned value can be:
  597.   *            @ref FLASH_Error_Codes
  598.   */
  599. uint32_t HAL_FLASH_GetError(void)
  600. {
  601.    return pFlash.ErrorCode;
  602. }
  603.  
  604. /**
  605.   * @}
  606.   */
  607.  
  608. /**
  609.   * @}
  610.   */
  611.  
  612. /** @addtogroup FLASH_Private_Functions
  613.  * @{
  614.  */
  615.  
  616. /**
  617.   * @brief  Wait for a FLASH operation to complete.
  618.   * @param  Timeout  maximum flash operation timeout
  619.   * @retval HAL Status
  620.   */
  621. HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)
  622. {
  623.   /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
  624.      Even if the FLASH operation fails, the BUSY flag will be reset and an error
  625.      flag will be set */
  626.      
  627.   uint32_t tickstart = HAL_GetTick();
  628.      
  629.   while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY))
  630.   {
  631.     if (Timeout != HAL_MAX_DELAY)
  632.     {
  633.       if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
  634.       {
  635.         return HAL_TIMEOUT;
  636.       }
  637.     }
  638.   }
  639.  
  640.   /* Check FLASH End of Operation flag  */
  641.   if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
  642.   {
  643.     /* Clear FLASH End of Operation pending bit */
  644.     __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
  645.   }
  646.  
  647.   if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR)  ||
  648.      __HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR) ||
  649. #if defined(FLASH_SR_RDERR)
  650.       __HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR) ||
  651. #endif /* FLASH_SR_RDERR */
  652. #if defined(FLASH_SR_OPTVERRUSR)
  653.       __HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERRUSR) ||
  654. #endif /* FLASH_SR_OPTVERRUSR */
  655.      __HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR))
  656.   {
  657.     /*Save the error code*/
  658.     FLASH_SetErrorCode();
  659.     return HAL_ERROR;
  660.   }
  661.  
  662.   /* There is no error flag set */
  663.   return HAL_OK;
  664. }
  665.  
  666.  
  667. /**
  668.   * @brief  Set the specific FLASH error flag.
  669.   * @retval None
  670.   */
  671. static void FLASH_SetErrorCode(void)
  672. {
  673.   uint32_t flags = 0;
  674.  
  675.   if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR))
  676.   {
  677.     pFlash.ErrorCode |= HAL_FLASH_ERROR_WRP;
  678.     flags |= FLASH_FLAG_WRPERR;
  679.   }
  680.   if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR))
  681.   {
  682.     pFlash.ErrorCode |= HAL_FLASH_ERROR_PGA;
  683.     flags |= FLASH_FLAG_PGAERR;
  684.   }
  685.   if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR))
  686.   {
  687.     pFlash.ErrorCode |= HAL_FLASH_ERROR_OPTV;
  688.     flags |= FLASH_FLAG_OPTVERR;
  689.   }
  690.  
  691. #if defined(FLASH_SR_RDERR)
  692.   if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR))
  693.   {
  694.     pFlash.ErrorCode |= HAL_FLASH_ERROR_RD;
  695.     flags |= FLASH_FLAG_RDERR;
  696.   }
  697. #endif /* FLASH_SR_RDERR */
  698. #if defined(FLASH_SR_OPTVERRUSR)
  699.   if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERRUSR))
  700.   {
  701.     pFlash.ErrorCode |= HAL_FLASH_ERROR_OPTVUSR;
  702.     flags |= FLASH_FLAG_OPTVERRUSR;
  703.   }
  704. #endif /* FLASH_SR_OPTVERRUSR */
  705.  
  706.   /* Clear FLASH error pending bits */
  707.   __HAL_FLASH_CLEAR_FLAG(flags);
  708. }  
  709. /**
  710.   * @}
  711.   */
  712.  
  713. /**
  714.   * @}
  715.   */
  716.  
  717. #endif /* HAL_FLASH_MODULE_ENABLED */
  718.  
  719. /**
  720.   * @}
  721.   */
  722.  
  723. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  724.