mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Revision:
489:119543c9f674
Parent:
382:ee426a420dbb
--- a/targets/cmsis/TARGET_STM/TARGET_STM32L0/stm32l0xx_hal_flash_ramfunc.c	Thu Mar 05 13:15:07 2015 +0000
+++ b/targets/cmsis/TARGET_STM/TARGET_STM32L0/stm32l0xx_hal_flash_ramfunc.c	Thu Mar 12 14:30:49 2015 +0000
@@ -2,13 +2,11 @@
   ******************************************************************************
   * @file    stm32l0xx_hal_flash_ramfunc.c
   * @author  MCD Application Team
-  * @version V1.1.0
-  * @date    18-June-2014
+  * @version V1.2.0
+  * @date    06-February-2015
   * @brief   FLASH RAMFUNC driver.
   *          This file provides a Flash firmware functions which should be 
   *          executed from internal SRAM
-  *            + FLASH HalfPage Programming
-  *            + FLASH Power Down in Run mode
   *
   *  @verbatim
 
@@ -29,12 +27,12 @@
     --------------------
     [..] RAM functions are defined using a specific toolchain attribute
          "__attribute__((section(".RamFunc")))".
-  
-  @endverbatim
+
+@endverbatim
   ******************************************************************************
   * @attention
   *
-  * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
+  * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
   *
   * Redistribution and use in source and binary forms, with or without modification,
   * are permitted provided that the following conditions are met:
@@ -63,32 +61,27 @@
 
 /* Includes ------------------------------------------------------------------*/
 #include "stm32l0xx_hal.h"
-/** @addtogroup STM32L0XX_HAL_Driver
+
+#ifdef HAL_FLASH_MODULE_ENABLED
+/** @addtogroup STM32L0xx_HAL_Driver
   * @{
   */
 
-/** @defgroup FLASHRamfunc Driver
+/** @addtogroup FLASHRamfunc
   * @brief FLASH functions executed from RAM
   * @{
   */
 
-#ifdef HAL_FLASH_MODULE_ENABLED
-
 /* Private typedef -----------------------------------------------------------*/
 /* Private define ------------------------------------------------------------*/
 /* Private macro -------------------------------------------------------------*/
 /* Private variables ---------------------------------------------------------*/
 /* Private function prototypes -----------------------------------------------*/
-static __RAM_FUNC  FLASH_Program_HalfPage(uint32_t Address, uint32_t *Data);
-
-/* Private functions ---------------------------------------------------------*/
+static __RAM_FUNC FLASHRAM_WaitForLastOperation(uint32_t Timeout);
+static __RAM_FUNC FLASHRAM_SetErrorCode(void);
 
-/** @defgroup FLASHRamfunc_Private_Functions
-  * @{
-  */
 
-/** @defgroup FLASHRamfunc_Group1 Peripheral  features functions 
- *  @brief   Data transfers functions 
+/** @addtogroup FLASHRamfunc_Exported_Functions
  *
 @verbatim   
  ===============================================================================
@@ -102,66 +95,33 @@
   * @{
   */
 
+/** @addtogroup FLASHRamfunc_Exported_Functions_Group1
+  * @{
+  */  
+
 /**
-  * @brief  Program a half page word at a specified address, 
-  * @note   This function should be executed from RAM        
-  * @param  Address:  specifies the address to be programmed, 
-  *         the address should be half page aligned.
-  * @param  *Data: specifies the buffer of data to be programmed,
-  *                the size of the buffer is 16 words.
-  * 
-  * @retval HAL_StatusTypeDef HAL Status
+  * @brief  Enable  the power down mode during RUN mode.
+  * @note   This function can be used only when the user code is running from Internal SRAM.
+  * @param  None
+  * @retval None
   */
-__RAM_FUNC  HAL_FLASHEx_HalfPageProgram(uint32_t Address, uint32_t *Data)
+__RAM_FUNC HAL_FLASHEx_EnableRunPowerDown(void)
 {
-  HAL_StatusTypeDef status = HAL_OK;
-  uint32_t timeout = 0xFFFFFFFF;
-  /*Program word (32-bit) at a specified address.*/
-  FLASH_Program_HalfPage(Address, Data);
-  
-  /* Wait for a FLASH operation to complete or a TIMEOUT to occur */
-  while(((FLASH->SR & FLASH_FLAG_BSY) != 0x00) && (timeout != 0x00))
-  {
-    timeout--;
-  }
-  
-  if(timeout == 0x00 )
-    {
-      return HAL_TIMEOUT;
-    }
-  
-  /* Reset PROG bit */
-  FLASH->PECR &= ~FLASH_PECR_PROG;
-  FLASH->PECR &= ~FLASH_PECR_FPRG;
-  return status;
+  /* Enable the Power Down in Run mode*/
+  __HAL_FLASH_POWER_DOWN_ENABLE();
+  return HAL_OK;
 }
 
 /**
-  * @brief   Enable the Power down in Run Mode
-  * @note    This function should be called and executed from SRAM memory
+  * @brief  Disable the power down mode during RUN mode.
+  * @note   This function can be used only when the user code is running from Internal SRAM.
   * @param  None
   * @retval None
   */
-__RAM_FUNC  HAL_FLASHEx_EnableRunPowerDown(void)
-{
-  /* Enable the Power Down in Run mode*/
-  __HAL_FLASH_POWER_DOWN_ENABLE();
-  
-  return HAL_OK;
-  
-}
-
-/**
-  * @brief   Disable the Power down in Run Mode
-  * @note    This function should be called and executed from SRAM memory
-  * @param  None
-  * @retval None
-  */
-__RAM_FUNC  HAL_FLASHEx_DisableRunPowerDown(void)
+__RAM_FUNC HAL_FLASHEx_DisableRunPowerDown(void)
 {
   /* Disable the Power Down in Run mode*/
   __HAL_FLASH_POWER_DOWN_DISABLE();
-
   return HAL_OK;  
 }
 
@@ -169,39 +129,238 @@
   * @}
   */
 
+/** @addtogroup FLASHRamfunc_Exported_Functions_Group2
+ *
+@verbatim  
+@endverbatim
+  * @{
+  */
+
+#if defined(STM32L071xx) || defined(STM32L072xx) || defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)  
 /**
-  * @brief  Program a half page in program memory.
-  * @param  Address: The Half page address in program memory to be written.
-  * @param  Data:
-  * @retval None
+  * @brief  Erases a specified 2 pages in program memory in parallel.
+  * @note   This function can be used only for STM32L07xxx/STM32L08xxx  devices.
+  *         To correctly run this function, the HAL_FLASH_Unlock() function
+  *         must be called before.
+  *         Call the HAL_FLASH_Lock() to disable the flash memory access 
+  *         (recommended to protect the FLASH memory against possible unwanted operation).
+  * @param  Page_Address1: The page address in program memory to be erased in 
+  *         the first Bank (BANK1). This parameter should be between FLASH_BASE
+  *         and FLASH_BANK1_END.
+  * @param  Page_Address2: The page address in program memory to be erased in 
+  *         the second Bank (BANK2). This parameter should be between FLASH_BANK2_BASE
+  *         and FLASH_BANK2_END.
+  * @note   A Page is erased in the Program memory only if the address to load 
+  *         is the start address of a page (multiple of 128 bytes).
+  * @retval HAL Status: The returned value can be: 
+  *         HAL_ERROR, HAL_OK or HAL_TIMEOUT.
   */
-static __RAM_FUNC  FLASH_Program_HalfPage(uint32_t Address, uint32_t *Data)
+__RAM_FUNC HAL_FLASHEx_EraseParallelPage(uint32_t Page_Address1, uint32_t Page_Address2)
 {
   HAL_StatusTypeDef status = HAL_OK;
-  uint32_t i =0;
+
+  /* Wait for last operation to be completed */
+  status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
   
-  /* Set PROG bit */
-  FLASH->PECR |= FLASH_PECR_PROG;
+  if(status == HAL_OK)
+  {
+    /* Proceed to erase the page */
+    SET_BIT(FLASH->PECR, FLASH_PECR_PARALLBANK);
+    SET_BIT(FLASH->PECR, FLASH_PECR_ERASE);
+    SET_BIT(FLASH->PECR, FLASH_PECR_PROG);
   
-  /* Set FPRG bit */
-  FLASH->PECR |= FLASH_PECR_FPRG;
+    /* Write 00000000h to the first word of the first program page to erase */
+    *(__IO uint32_t *)Page_Address1 = 0x00000000;
+    /* Write 00000000h to the first word of the second program page to erase */    
+    *(__IO uint32_t *)Page_Address2 = 0x00000000;    
+ 
+    /* Wait for last operation to be completed */
+    status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
 
-  *(__IO uint32_t*)Address = Data[0];
-  
-  for(i = 1; i <= 15; i++)
-  {  
-    *(__IO uint32_t*)(Address + 4) = Data[i];
-  } 
+    /* If the erase operation is completed, disable the ERASE, PROG and PARALLBANK bits */
+    CLEAR_BIT(FLASH->PECR, FLASH_PECR_PROG);
+    CLEAR_BIT(FLASH->PECR, FLASH_PECR_ERASE);
+    CLEAR_BIT(FLASH->PECR, FLASH_PECR_PARALLBANK);
+  }     
+  /* Return the erasesStatus */
   return status;
 }
 
 /**
-  * @}
-  */   
-#endif /* HAL_FLASH_MODULE_ENABLED */
+  * @brief  Programs 2 half pages in program memory in parallel. The half page size is 16 Words.
+  * @note   This function can be used only for STM32L07xxx/STM32L08xxx devices.
+  * @param  Address1: specifies the first address to be written in the first bank 
+  *         (BANK1). This parameter should be between FLASH_BASE and (FLASH_BANK1_END - FLASH_PAGE_SIZE).
+  * @param  pBuffer1: pointer to the buffer  containing the data to be  written 
+  *         to the first half page in the first bank.
+  * @param  Address2: specifies the second address to be written in the second bank
+  *         (BANK2). This parameter should be between FLASH_BANK2_BASE and (FLASH_BANK2_END - FLASH_PAGE_SIZE).
+  * @param  pBuffer2: pointer to the buffer containing the data to be  written 
+  *         to the second half page in the second bank.
+  * @note   To correctly run this function, the HAL_FLASH_Unlock() function
+  *         must be called before.
+  *         Call the HAL_FLASH_Lock() to disable the flash memory access  
+  *         (recommended to protect the FLASH memory against possible unwanted operation).
+  * @note   Half page write is possible only from SRAM.
+  * @note   A half page is written to the program memory only if the first 
+  *         address to load is the start address of a half page (multiple of 64 
+  *         bytes) and the 15 remaining words to load are in the same half page.
+  * @note   During the Program memory half page write all read operations are 
+  *         forbidden (this includes DMA read operations and debugger read 
+  *         operations such as breakpoints, periodic updates, etc.).
+  * @note   If a PGAERR is set during a Program memory half page write, the 
+  *         complete write operation is aborted. Software should then reset the 
+  *         FPRG and PROG/DATA bits and restart the write operation from the 
+  *         beginning.
+  * @retval HAL Status: The returned value can be:  
+  *         HAL_ERROR, HAL_OK or HAL_TIMEOUT.
+  */
+__RAM_FUNC HAL_FLASHEx_ProgramParallelHalfPage(uint32_t Address1, uint32_t* pBuffer1, uint32_t Address2, uint32_t* pBuffer2)
+{
+  uint32_t count; 
+  HAL_StatusTypeDef status;
+
+  /* Wait for last operation to be completed */
+  status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
+  
+  if(status == HAL_OK)
+  {
+    /* Proceed to program the new half page */
+    SET_BIT(FLASH->PECR, FLASH_PECR_PARALLBANK);
+    SET_BIT(FLASH->PECR, FLASH_PECR_FPRG);
+    SET_BIT(FLASH->PECR, FLASH_PECR_PROG);
+
+ 
+    /* Wait for last operation to be completed */
+    status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
+
+    if(status == HAL_OK)
+    {
+      /* Write one half page,
+         Address1 doesn't need to be increased */ 
+      count = 0;
+
+      /* Disable all IRQs */
+      __disable_irq();
+
+      while(count < 16)
+      {
+        *(__IO uint32_t*) Address1 = *pBuffer1;
+        pBuffer1++;
+        count++;
+      }
+      
+      /* Write the second half page,
+         Address2 doesn't need to be increased */ 
+      count = 0;
+      while(count < 16)
+      {
+        *(__IO uint32_t*) Address2 = *pBuffer2;
+        pBuffer2++;
+        count++;
+      }
+
+      /* Enable IRQs */
+      __enable_irq();
+      
+      /* Wait for last operation to be completed */
+      status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
+    } 
+    /* if the write operation is completed, disable the PROG, FPRG and PARALLBANK bits */
+    CLEAR_BIT(FLASH->PECR, FLASH_PECR_PROG);
+    CLEAR_BIT(FLASH->PECR, FLASH_PECR_FPRG);
+    CLEAR_BIT(FLASH->PECR, FLASH_PECR_PARALLBANK);
+  }
+  /* Return the Write Status */
+  return status;
+}
+#endif /* STM32L071xx || STM32L072xx || STM32L073xx || STM32L081xx || STM32L082xx || STM32L083xx */
+  
+/**
+  * @brief  Program a half page in program memory. 
+  * @param  Address: specifies the address to be written.
+  * @param  pBuffer: pointer to the buffer  containing the data to be  written to 
+  *         the half page.
+  * @note   To correctly run this function, the HAL_FLASH_Unlock() function
+  *         must be called before.
+  *         Call the HAL_FLASH_Lock() to disable the flash memory access  
+  *         (recommended to protect the FLASH memory against possible unwanted operation)
+  * @note   Half page write is possible only from SRAM.
+  * @note   A half page is written to the program memory only if the first 
+  *         address to load is the start address of a half page (multiple of 64 
+  *         bytes) and the 15 remaining words to load are in the same half page.
+  * @note   During the Program memory half page write all read operations are 
+  *         forbidden (this includes DMA read operations and debugger read 
+  *         operations such as breakpoints, periodic updates, etc.).
+  * @note   If a PGAERR is set during a Program memory half page write, the 
+  *         complete write operation is aborted. Software should then reset the 
+  *         FPRG and PROG/DATA bits and restart the write operation from the 
+  *         beginning.
+  * @retval HAL Status: The returned value can be:  
+  *         HAL_ERROR, HAL_OK or HAL_TIMEOUT. 
+  */
+__RAM_FUNC HAL_FLASHEx_HalfPageProgram(uint32_t Address, uint32_t *pBuffer)
+{
+  uint32_t count; 
+  HAL_StatusTypeDef status;
+
+  /* Wait for last operation to be completed */
+  status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
+
+  if(status == HAL_OK)
+  {
+    /* Proceed to program the new half page */
+    SET_BIT(FLASH->PECR, FLASH_PECR_FPRG);
+    SET_BIT(FLASH->PECR, FLASH_PECR_PROG);
 
 
+    count = 0;
+    /* Write one half page,
+       Address doesn't need to be increased */ 
+
+    /* Disable all IRQs */
+    __disable_irq();
+
+    while(count < 16)
+    {
+      *(__IO uint32_t*) Address = *pBuffer;
+      pBuffer++;
+      count++;
+    }
+
+    /* Enable IRQs */
+    __enable_irq();
   
+    /* Wait for last operation to be completed */
+    status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
+
+    /* If the write operation is completed, disable the PROG and FPRG bits */
+    CLEAR_BIT(FLASH->PECR, FLASH_PECR_PROG);
+    CLEAR_BIT(FLASH->PECR, FLASH_PECR_FPRG);
+  }
+  /* Return the write status */
+  return status;
+}
+
+/**
+  * @brief  Get the specific FLASH errors flag.
+  * @param error pointer is the error value. It can be a mixed of :
+  *            @arg HAL_FLASH_ERROR_RD: FLASH Read Protection error flag (PCROP)
+  *            @arg HAL_FLASH_ERROR_SIZE: FLASH Programming Parallelism error flag  
+  *            @arg HAL_FLASH_ERROR_PGA: FLASH Programming Alignment error flag
+  *            @arg HAL_FLASH_ERROR_WRP: FLASH Write protected error flag
+  *            @arg HAL_FLASH_ERROR_OPTV: FLASH Option valid error flag 
+  *            @arg HAL_FLASH_ERROR_FWWERR: FLASH Write or Errase operation aborted
+  *            @arg HAL_FLASH_ERROR_NOTZERO: FLASH Write operation is done in a not-erased region 
+  */
+__RAM_FUNC HAL_FLASHRAM_GetError(uint32_t * error)
+{ 
+  *error = ProcFlash.ErrorCode;
+  return HAL_OK;  
+}
+
+
+
 /**
   * @}
   */
@@ -210,7 +369,104 @@
   * @}
   */
 
-     
+/** @defgroup FLASHRamfunc_Private_Functions FLASH RAM Private Functions
+  * @{
+  */ 
+
+/**
+  * @brief  Set the specific FLASH error flag.
+  * @param  None
+  * @retval None
+  */
+static __RAM_FUNC FLASHRAM_SetErrorCode(void)
+{  
+  if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR))
+  { 
+    ProcFlash.ErrorCode |= HAL_FLASH_ERROR_WRP;
+  }
+  if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR))
+  { 
+    ProcFlash.ErrorCode |= HAL_FLASH_ERROR_PGA;
+  }
+  if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_SIZERR))
+  { 
+    ProcFlash.ErrorCode |= HAL_FLASH_ERROR_SIZE;
+  }
+  if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR))
+  { 
+    ProcFlash.ErrorCode |= HAL_FLASH_ERROR_OPTV;
+  }
+  if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR))
+  { 
+    ProcFlash.ErrorCode |= HAL_FLASH_ERROR_RD;
+  }
+  if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_FWWERR))
+  { 
+   ProcFlash.ErrorCode |= HAL_FLASH_ERROR_FWWERR;
+  }
+  if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_NOTZEROERR))
+  { 
+   ProcFlash.ErrorCode |= HAL_FLASH_ERROR_NOTZERO;
+  }
+  
+  /* Errors are now stored, clear errors flags */
+  __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_SIZERR |
+                         FLASH_FLAG_OPTVERR | FLASH_FLAG_RDERR | FLASH_FLAG_FWWERR | 
+                         FLASH_FLAG_NOTZEROERR);
+  return HAL_OK;
+} 
+
+
+/**
+  * @brief  Wait for a FLASH operation to complete.
+  * @param  Timeout: maximum flash operationtimeout
+  * @retval HAL status
+  */
+static __RAM_FUNC FLASHRAM_WaitForLastOperation(uint32_t Timeout)
+{ 
+    /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
+       Even if the FLASH operation fails, the BUSY flag will be reset and an error
+       flag will be set */
+       
+    while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY) && (Timeout != 0x00)) 
+    { 
+      Timeout--;
+    }
+    
+    if(Timeout == 0x00 )
+    {
+      return HAL_TIMEOUT;
+    }
+    
+    if( (__HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR)      != RESET) || 
+        (__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR)     != RESET) || 
+        (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR)     != RESET) || 
+        (__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR)    != RESET) || 
+        (__HAL_FLASH_GET_FLAG(FLASH_FLAG_SIZERR)     != RESET) || 
+        (__HAL_FLASH_GET_FLAG(FLASH_FLAG_FWWERR)     != RESET) || 
+        (__HAL_FLASH_GET_FLAG(FLASH_FLAG_NOTZEROERR) != RESET) )
+    {
+      /*Save the error code*/
+      FLASHRAM_SetErrorCode();
+      return HAL_ERROR;
+    }
+  
+    /* If there is an error flag set */
+    return HAL_OK;
+}
+
+/**
+  * @}
+  */
+
+/**
+  * @}
+  */
+
+/**
+  * @}
+  */
+#endif /* HAL_FLASH_MODULE_ENABLED */
+
 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
 
-