mbed library sources

Fork of mbed-src by mbed official

Committer:
lzbpli
Date:
Thu Jul 07 06:48:59 2016 +0000
Revision:
636:b0d178e9fa10
Parent:
394:83f921546702
l053

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 354:e67efb2aab0e 1 /**
mbed_official 354:e67efb2aab0e 2 ******************************************************************************
mbed_official 354:e67efb2aab0e 3 * @file stm32l1xx_hal_flash_ramfunc.c
mbed_official 354:e67efb2aab0e 4 * @author MCD Application Team
mbed_official 354:e67efb2aab0e 5 * @version V1.0.0
mbed_official 354:e67efb2aab0e 6 * @date 5-September-2014
mbed_official 354:e67efb2aab0e 7 * @brief FLASH RAMFUNC driver.
mbed_official 354:e67efb2aab0e 8 * This file provides a Flash firmware functions which should be
mbed_official 354:e67efb2aab0e 9 * executed from internal SRAM
mbed_official 354:e67efb2aab0e 10 *
mbed_official 354:e67efb2aab0e 11 * @verbatim
mbed_official 354:e67efb2aab0e 12
mbed_official 354:e67efb2aab0e 13 *** ARM Compiler ***
mbed_official 354:e67efb2aab0e 14 --------------------
mbed_official 354:e67efb2aab0e 15 [..] RAM functions are defined using the toolchain options.
mbed_official 354:e67efb2aab0e 16 Functions that are be executed in RAM should reside in a separate
mbed_official 354:e67efb2aab0e 17 source module. Using the 'Options for File' dialog you can simply change
mbed_official 354:e67efb2aab0e 18 the 'Code / Const' area of a module to a memory space in physical RAM.
mbed_official 354:e67efb2aab0e 19 Available memory areas are declared in the 'Target' tab of the
mbed_official 354:e67efb2aab0e 20 Options for Target' dialog.
mbed_official 354:e67efb2aab0e 21
mbed_official 354:e67efb2aab0e 22 *** ICCARM Compiler ***
mbed_official 354:e67efb2aab0e 23 -----------------------
mbed_official 354:e67efb2aab0e 24 [..] RAM functions are defined using a specific toolchain keyword "__ramfunc".
mbed_official 354:e67efb2aab0e 25
mbed_official 354:e67efb2aab0e 26 *** GNU Compiler ***
mbed_official 354:e67efb2aab0e 27 --------------------
mbed_official 354:e67efb2aab0e 28 [..] RAM functions are defined using a specific toolchain attribute
mbed_official 354:e67efb2aab0e 29 "__attribute__((section(".RamFunc")))".
mbed_official 354:e67efb2aab0e 30
mbed_official 354:e67efb2aab0e 31 @endverbatim
mbed_official 354:e67efb2aab0e 32 ******************************************************************************
mbed_official 354:e67efb2aab0e 33 * @attention
mbed_official 354:e67efb2aab0e 34 *
mbed_official 354:e67efb2aab0e 35 * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
mbed_official 354:e67efb2aab0e 36 *
mbed_official 354:e67efb2aab0e 37 * Redistribution and use in source and binary forms, with or without modification,
mbed_official 354:e67efb2aab0e 38 * are permitted provided that the following conditions are met:
mbed_official 354:e67efb2aab0e 39 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 354:e67efb2aab0e 40 * this list of conditions and the following disclaimer.
mbed_official 354:e67efb2aab0e 41 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 354:e67efb2aab0e 42 * this list of conditions and the following disclaimer in the documentation
mbed_official 354:e67efb2aab0e 43 * and/or other materials provided with the distribution.
mbed_official 354:e67efb2aab0e 44 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 354:e67efb2aab0e 45 * may be used to endorse or promote products derived from this software
mbed_official 354:e67efb2aab0e 46 * without specific prior written permission.
mbed_official 354:e67efb2aab0e 47 *
mbed_official 354:e67efb2aab0e 48 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 354:e67efb2aab0e 49 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 354:e67efb2aab0e 50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 354:e67efb2aab0e 51 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 354:e67efb2aab0e 52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 354:e67efb2aab0e 53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 354:e67efb2aab0e 54 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 354:e67efb2aab0e 55 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 354:e67efb2aab0e 56 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 354:e67efb2aab0e 57 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 354:e67efb2aab0e 58 *
mbed_official 354:e67efb2aab0e 59 ******************************************************************************
mbed_official 354:e67efb2aab0e 60 */
mbed_official 354:e67efb2aab0e 61
mbed_official 354:e67efb2aab0e 62 /* Includes ------------------------------------------------------------------*/
mbed_official 354:e67efb2aab0e 63 #include "stm32l1xx_hal.h"
mbed_official 354:e67efb2aab0e 64
mbed_official 354:e67efb2aab0e 65 /** @addtogroup STM32L1xx_HAL_Driver
mbed_official 354:e67efb2aab0e 66 * @{
mbed_official 354:e67efb2aab0e 67 */
mbed_official 354:e67efb2aab0e 68
mbed_official 354:e67efb2aab0e 69 /** @defgroup FLASHRamfunc FLASHRamfunc
mbed_official 354:e67efb2aab0e 70 * @brief FLASH functions executed from RAM
mbed_official 354:e67efb2aab0e 71 * @{
mbed_official 354:e67efb2aab0e 72 */
mbed_official 354:e67efb2aab0e 73
mbed_official 354:e67efb2aab0e 74 #ifdef HAL_FLASH_MODULE_ENABLED
mbed_official 354:e67efb2aab0e 75
mbed_official 354:e67efb2aab0e 76 /* Private typedef -----------------------------------------------------------*/
mbed_official 354:e67efb2aab0e 77 /* Private define ------------------------------------------------------------*/
mbed_official 354:e67efb2aab0e 78 /* Private macro -------------------------------------------------------------*/
mbed_official 354:e67efb2aab0e 79 /* Private variables ---------------------------------------------------------*/
mbed_official 354:e67efb2aab0e 80 /* Private function prototypes -----------------------------------------------*/
mbed_official 354:e67efb2aab0e 81 static __RAM_FUNC FLASHRAM_WaitForLastOperation(uint32_t Timeout);
mbed_official 354:e67efb2aab0e 82
mbed_official 354:e67efb2aab0e 83 /* Private functions ---------------------------------------------------------*/
mbed_official 354:e67efb2aab0e 84
mbed_official 354:e67efb2aab0e 85 /** @defgroup FLASHRamfunc_Exported_Functions FLASH RAM Exported Functions
mbed_official 354:e67efb2aab0e 86 *
mbed_official 354:e67efb2aab0e 87 @verbatim
mbed_official 354:e67efb2aab0e 88 ===============================================================================
mbed_official 354:e67efb2aab0e 89 ##### ramfunc functions #####
mbed_official 354:e67efb2aab0e 90 ===============================================================================
mbed_official 354:e67efb2aab0e 91 [..]
mbed_official 354:e67efb2aab0e 92 This subsection provides a set of functions that should be executed from RAM
mbed_official 354:e67efb2aab0e 93 transfers.
mbed_official 354:e67efb2aab0e 94
mbed_official 354:e67efb2aab0e 95 @endverbatim
mbed_official 354:e67efb2aab0e 96 * @{
mbed_official 354:e67efb2aab0e 97 */
mbed_official 354:e67efb2aab0e 98
mbed_official 354:e67efb2aab0e 99 /** @defgroup FLASHRamfunc_Exported_Functions_Group1 FLASH RAM Peripheral features functions
mbed_official 354:e67efb2aab0e 100 * @{
mbed_official 354:e67efb2aab0e 101 */
mbed_official 354:e67efb2aab0e 102
mbed_official 354:e67efb2aab0e 103 /**
mbed_official 354:e67efb2aab0e 104 * @brief Enable the power down mode during RUN mode.
mbed_official 354:e67efb2aab0e 105 * @note This function can be used only when the user code is running from Internal SRAM.
mbed_official 354:e67efb2aab0e 106 * @retval None
mbed_official 354:e67efb2aab0e 107 */
mbed_official 354:e67efb2aab0e 108 __RAM_FUNC HAL_FLASHEx_EnableRunPowerDown(void)
mbed_official 354:e67efb2aab0e 109 {
mbed_official 354:e67efb2aab0e 110 /* Enable the Power Down in Run mode*/
mbed_official 354:e67efb2aab0e 111 __HAL_FLASH_POWER_DOWN_ENABLE();
mbed_official 354:e67efb2aab0e 112
mbed_official 354:e67efb2aab0e 113 return HAL_OK;
mbed_official 354:e67efb2aab0e 114 }
mbed_official 354:e67efb2aab0e 115
mbed_official 354:e67efb2aab0e 116
mbed_official 354:e67efb2aab0e 117 /**
mbed_official 354:e67efb2aab0e 118 * @brief Disable the power down mode during RUN mode.
mbed_official 354:e67efb2aab0e 119 * @note This function can be used only when the user code is running from Internal SRAM.
mbed_official 354:e67efb2aab0e 120 * @retval None
mbed_official 354:e67efb2aab0e 121 */
mbed_official 354:e67efb2aab0e 122 __RAM_FUNC HAL_FLASHEx_DisableRunPowerDown(void)
mbed_official 354:e67efb2aab0e 123 {
mbed_official 354:e67efb2aab0e 124 /* Disable the Power Down in Run mode*/
mbed_official 354:e67efb2aab0e 125 __HAL_FLASH_POWER_DOWN_DISABLE();
mbed_official 354:e67efb2aab0e 126
mbed_official 354:e67efb2aab0e 127 return HAL_OK;
mbed_official 354:e67efb2aab0e 128 }
mbed_official 354:e67efb2aab0e 129
mbed_official 354:e67efb2aab0e 130 /**
mbed_official 354:e67efb2aab0e 131 * @}
mbed_official 354:e67efb2aab0e 132 */
mbed_official 354:e67efb2aab0e 133
mbed_official 354:e67efb2aab0e 134 /** @defgroup FLASHRamfunc_Exported_Functions_Group2 FLASH RAM Programming and erasing operation functions
mbed_official 354:e67efb2aab0e 135 *
mbed_official 354:e67efb2aab0e 136 @verbatim
mbed_official 354:e67efb2aab0e 137 @endverbatim
mbed_official 354:e67efb2aab0e 138 * @{
mbed_official 354:e67efb2aab0e 139 */
mbed_official 354:e67efb2aab0e 140
mbed_official 354:e67efb2aab0e 141 #if defined (STM32L151xD) || defined (STM32L152xD) || defined (STM32L162xD) || \
mbed_official 354:e67efb2aab0e 142 defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)
mbed_official 354:e67efb2aab0e 143 /**
mbed_official 354:e67efb2aab0e 144 * @brief Erases a specified 2 page in program memory in parallel.
mbed_official 354:e67efb2aab0e 145 * @note This function can be used only for STM32L151xD, STM32L152xD), STM32L162xD and Cat5 devices.
mbed_official 354:e67efb2aab0e 146 * To correctly run this function, the HAL_FLASH_Unlock() function
mbed_official 354:e67efb2aab0e 147 * must be called before.
mbed_official 354:e67efb2aab0e 148 * Call the HAL_FLASH_Lock() to disable the flash memory access
mbed_official 354:e67efb2aab0e 149 * (recommended to protect the FLASH memory against possible unwanted operation).
mbed_official 354:e67efb2aab0e 150 * @param Page_Address1: The page address in program memory to be erased in
mbed_official 354:e67efb2aab0e 151 * the first Bank (BANK1). This parameter should be between FLASH_BASE
mbed_official 354:e67efb2aab0e 152 * and FLASH_BANK1_END.
mbed_official 354:e67efb2aab0e 153 * @param Page_Address2: The page address in program memory to be erased in
mbed_official 354:e67efb2aab0e 154 * the second Bank (BANK2). This parameter should be between FLASH_BANK2_BASE
mbed_official 354:e67efb2aab0e 155 * and FLASH_BANK2_END.
mbed_official 354:e67efb2aab0e 156 * @note A Page is erased in the Program memory only if the address to load
mbed_official 354:e67efb2aab0e 157 * is the start address of a page (multiple of 256 bytes).
mbed_official 354:e67efb2aab0e 158 * @retval HAL Status: The returned value can be:
mbed_official 354:e67efb2aab0e 159 * HAL_ERROR, HAL_OK or HAL_TIMEOUT.
mbed_official 354:e67efb2aab0e 160 */
mbed_official 354:e67efb2aab0e 161 __RAM_FUNC HAL_FLASHEx_EraseParallelPage(uint32_t Page_Address1, uint32_t Page_Address2)
mbed_official 354:e67efb2aab0e 162 {
mbed_official 354:e67efb2aab0e 163 HAL_StatusTypeDef status = HAL_OK;
mbed_official 354:e67efb2aab0e 164
mbed_official 354:e67efb2aab0e 165 /* Wait for last operation to be completed */
mbed_official 354:e67efb2aab0e 166 status = FLASHRAM_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
mbed_official 354:e67efb2aab0e 167
mbed_official 354:e67efb2aab0e 168 if(status == HAL_OK)
mbed_official 354:e67efb2aab0e 169 {
mbed_official 354:e67efb2aab0e 170 /* If the previous operation is completed, proceed to erase the page */
mbed_official 354:e67efb2aab0e 171
mbed_official 354:e67efb2aab0e 172 /* Set the PARALLBANK bit */
mbed_official 354:e67efb2aab0e 173 FLASH->PECR |= FLASH_PECR_PARALLBANK;
mbed_official 354:e67efb2aab0e 174
mbed_official 354:e67efb2aab0e 175 /* Set the ERASE bit */
mbed_official 354:e67efb2aab0e 176 FLASH->PECR |= FLASH_PECR_ERASE;
mbed_official 354:e67efb2aab0e 177
mbed_official 354:e67efb2aab0e 178 /* Set PROG bit */
mbed_official 354:e67efb2aab0e 179 FLASH->PECR |= FLASH_PECR_PROG;
mbed_official 354:e67efb2aab0e 180
mbed_official 354:e67efb2aab0e 181 /* Write 00000000h to the first word of the first program page to erase */
mbed_official 354:e67efb2aab0e 182 *(__IO uint32_t *)Page_Address1 = 0x00000000;
mbed_official 354:e67efb2aab0e 183 /* Write 00000000h to the first word of the second program page to erase */
mbed_official 354:e67efb2aab0e 184 *(__IO uint32_t *)Page_Address2 = 0x00000000;
mbed_official 354:e67efb2aab0e 185
mbed_official 354:e67efb2aab0e 186 /* Wait for last operation to be completed */
mbed_official 354:e67efb2aab0e 187 status = FLASHRAM_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
mbed_official 354:e67efb2aab0e 188
mbed_official 354:e67efb2aab0e 189 /* If the erase operation is completed, disable the ERASE, PROG and PARALLBANK bits */
mbed_official 354:e67efb2aab0e 190 FLASH->PECR &= (uint32_t)(~FLASH_PECR_PROG);
mbed_official 354:e67efb2aab0e 191 FLASH->PECR &= (uint32_t)(~FLASH_PECR_ERASE);
mbed_official 354:e67efb2aab0e 192 FLASH->PECR &= (uint32_t)(~FLASH_PECR_PARALLBANK);
mbed_official 354:e67efb2aab0e 193 }
mbed_official 354:e67efb2aab0e 194 /* Return the Erase Status */
mbed_official 354:e67efb2aab0e 195 return status;
mbed_official 354:e67efb2aab0e 196 }
mbed_official 354:e67efb2aab0e 197
mbed_official 354:e67efb2aab0e 198 /**
mbed_official 354:e67efb2aab0e 199 * @brief Programs 2 half page in program memory in parallel.
mbed_official 354:e67efb2aab0e 200 * @note This function can be used only for STM32L151xD, STM32L152xD), STM32L162xD and Cat5 devices.
mbed_official 354:e67efb2aab0e 201 * @param Address1: specifies the first address to be written in the first bank
mbed_official 354:e67efb2aab0e 202 * (BANK1). This parameter should be between FLASH_BASE and (FLASH_BANK1_END - FLASH_PAGE_SIZE).
mbed_official 354:e67efb2aab0e 203 * @param pBuffer1: pointer to the buffer containing the data to be written
mbed_official 354:e67efb2aab0e 204 * to the first half page in the first bank.
mbed_official 354:e67efb2aab0e 205 * @param Address2: specifies the second address to be written in the second bank
mbed_official 354:e67efb2aab0e 206 * (BANK2). This parameter should be between FLASH_BANK2_BASE and (FLASH_BANK2_END - FLASH_PAGE_SIZE).
mbed_official 354:e67efb2aab0e 207 * @param pBuffer2: pointer to the buffer containing the data to be written
mbed_official 354:e67efb2aab0e 208 * to the second half page in the second bank.
mbed_official 354:e67efb2aab0e 209 * @note To correctly run this function, the HAL_FLASH_Unlock() function
mbed_official 354:e67efb2aab0e 210 * must be called before.
mbed_official 354:e67efb2aab0e 211 * Call the HAL_FLASH_Lock() to disable the flash memory access
mbed_official 354:e67efb2aab0e 212 * (recommended to protect the FLASH memory against possible unwanted operation).
mbed_official 354:e67efb2aab0e 213 * @note Half page write is possible only from SRAM.
mbed_official 354:e67efb2aab0e 214 * @note If there are more than 32 words to write, after 32 words another
mbed_official 354:e67efb2aab0e 215 * Half Page programming operation starts and has to be finished.
mbed_official 354:e67efb2aab0e 216 * @note A half page is written to the program memory only if the first
mbed_official 354:e67efb2aab0e 217 * address to load is the start address of a half page (multiple of 128
mbed_official 354:e67efb2aab0e 218 * bytes) and the 31 remaining words to load are in the same half page.
mbed_official 354:e67efb2aab0e 219 * @note During the Program memory half page write all read operations are
mbed_official 354:e67efb2aab0e 220 * forbidden (this includes DMA read operations and debugger read
mbed_official 354:e67efb2aab0e 221 * operations such as breakpoints, periodic updates, etc.).
mbed_official 354:e67efb2aab0e 222 * @note If a PGAERR is set during a Program memory half page write, the
mbed_official 354:e67efb2aab0e 223 * complete write operation is aborted. Software should then reset the
mbed_official 354:e67efb2aab0e 224 * FPRG and PROG/DATA bits and restart the write operation from the
mbed_official 354:e67efb2aab0e 225 * beginning.
mbed_official 354:e67efb2aab0e 226 * @retval HAL Status: The returned value can be:
mbed_official 354:e67efb2aab0e 227 * HAL_ERROR, HAL_OK or HAL_TIMEOUT.
mbed_official 354:e67efb2aab0e 228 */
mbed_official 354:e67efb2aab0e 229 __RAM_FUNC HAL_FLASHEx_ProgramParallelHalfPage(uint32_t Address1, uint32_t* pBuffer1, uint32_t Address2, uint32_t* pBuffer2)
mbed_official 354:e67efb2aab0e 230 {
mbed_official 354:e67efb2aab0e 231 uint32_t count = 0;
mbed_official 354:e67efb2aab0e 232
mbed_official 354:e67efb2aab0e 233 HAL_StatusTypeDef status = HAL_OK;
mbed_official 354:e67efb2aab0e 234
mbed_official 354:e67efb2aab0e 235 /* Set the DISMCYCINT[0] bit in the Auxillary Control Register (0xE000E008)
mbed_official 354:e67efb2aab0e 236 This bit prevents the interruption of multicycle instructions and therefore
mbed_official 354:e67efb2aab0e 237 will increase the interrupt latency. of Cortex-M3. */
mbed_official 354:e67efb2aab0e 238 SCnSCB->ACTLR |= SCnSCB_ACTLR_DISMCYCINT_Msk;
mbed_official 354:e67efb2aab0e 239
mbed_official 354:e67efb2aab0e 240 /* Wait for last operation to be completed */
mbed_official 354:e67efb2aab0e 241 status = FLASHRAM_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
mbed_official 354:e67efb2aab0e 242
mbed_official 354:e67efb2aab0e 243 if(status == HAL_OK)
mbed_official 354:e67efb2aab0e 244 {
mbed_official 354:e67efb2aab0e 245 /* If the previous operation is completed, proceed to program the new
mbed_official 354:e67efb2aab0e 246 half page */
mbed_official 354:e67efb2aab0e 247 FLASH->PECR |= FLASH_PECR_PARALLBANK;
mbed_official 354:e67efb2aab0e 248 FLASH->PECR |= FLASH_PECR_FPRG;
mbed_official 354:e67efb2aab0e 249 FLASH->PECR |= FLASH_PECR_PROG;
mbed_official 354:e67efb2aab0e 250
mbed_official 354:e67efb2aab0e 251 /* Wait for last operation to be completed */
mbed_official 354:e67efb2aab0e 252 status = FLASHRAM_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
mbed_official 354:e67efb2aab0e 253 if(status == HAL_OK)
mbed_official 354:e67efb2aab0e 254 {
mbed_official 354:e67efb2aab0e 255 /* Write the first half page directly with 32 different words */
mbed_official 354:e67efb2aab0e 256 while(count < 32)
mbed_official 354:e67efb2aab0e 257 {
mbed_official 354:e67efb2aab0e 258 *(__IO uint32_t*) ((uint32_t)(Address1 + (4 * count))) = *(pBuffer1++);
mbed_official 354:e67efb2aab0e 259 count ++;
mbed_official 354:e67efb2aab0e 260 }
mbed_official 354:e67efb2aab0e 261 count = 0;
mbed_official 354:e67efb2aab0e 262
mbed_official 354:e67efb2aab0e 263 /* Write the second half page directly with 32 different words */
mbed_official 354:e67efb2aab0e 264 while(count < 32)
mbed_official 354:e67efb2aab0e 265 {
mbed_official 354:e67efb2aab0e 266 *(__IO uint32_t*) ((uint32_t)(Address2 + (4 * count))) = *(pBuffer2++);
mbed_official 354:e67efb2aab0e 267 count ++;
mbed_official 354:e67efb2aab0e 268 }
mbed_official 354:e67efb2aab0e 269 /* Wait for last operation to be completed */
mbed_official 354:e67efb2aab0e 270 status = FLASHRAM_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
mbed_official 354:e67efb2aab0e 271 }
mbed_official 354:e67efb2aab0e 272 /* if the write operation is completed, disable the PROG, FPRG and PARALLBANK bits */
mbed_official 354:e67efb2aab0e 273 FLASH->PECR &= (uint32_t)(~FLASH_PECR_PROG);
mbed_official 354:e67efb2aab0e 274 FLASH->PECR &= (uint32_t)(~FLASH_PECR_FPRG);
mbed_official 354:e67efb2aab0e 275 FLASH->PECR &= (uint32_t)(~FLASH_PECR_PARALLBANK);
mbed_official 354:e67efb2aab0e 276 }
mbed_official 354:e67efb2aab0e 277
mbed_official 354:e67efb2aab0e 278 SCnSCB->ACTLR &= ~SCnSCB_ACTLR_DISMCYCINT_Msk;
mbed_official 354:e67efb2aab0e 279
mbed_official 354:e67efb2aab0e 280 /* Return the Write Status */
mbed_official 354:e67efb2aab0e 281 return status;
mbed_official 354:e67efb2aab0e 282 }
mbed_official 354:e67efb2aab0e 283 #endif /* STM32L151xD || STM32L152xD || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
mbed_official 354:e67efb2aab0e 284
mbed_official 354:e67efb2aab0e 285 /**
mbed_official 354:e67efb2aab0e 286 * @brief Programs a half page in program memory.
mbed_official 354:e67efb2aab0e 287 * @param Address: specifies the address to be written.
mbed_official 354:e67efb2aab0e 288 * @param pBuffer: pointer to the buffer containing the data to be written to
mbed_official 354:e67efb2aab0e 289 * the half page.
mbed_official 354:e67efb2aab0e 290 * @note To correctly run this function, the HAL_FLASH_Unlock() function
mbed_official 354:e67efb2aab0e 291 * must be called before.
mbed_official 354:e67efb2aab0e 292 * Call the HAL_FLASH_Lock() to disable the flash memory access
mbed_official 354:e67efb2aab0e 293 * (recommended to protect the FLASH memory against possible unwanted operation)
mbed_official 354:e67efb2aab0e 294 * @note Half page write is possible only from SRAM.
mbed_official 354:e67efb2aab0e 295 * @note If there are more than 32 words to write, after 32 words another
mbed_official 354:e67efb2aab0e 296 * Half Page programming operation starts and has to be finished.
mbed_official 354:e67efb2aab0e 297 * @note A half page is written to the program memory only if the first
mbed_official 354:e67efb2aab0e 298 * address to load is the start address of a half page (multiple of 128
mbed_official 354:e67efb2aab0e 299 * bytes) and the 31 remaining words to load are in the same half page.
mbed_official 354:e67efb2aab0e 300 * @note During the Program memory half page write all read operations are
mbed_official 354:e67efb2aab0e 301 * forbidden (this includes DMA read operations and debugger read
mbed_official 354:e67efb2aab0e 302 * operations such as breakpoints, periodic updates, etc.).
mbed_official 354:e67efb2aab0e 303 * @note If a PGAERR is set during a Program memory half page write, the
mbed_official 354:e67efb2aab0e 304 * complete write operation is aborted. Software should then reset the
mbed_official 354:e67efb2aab0e 305 * FPRG and PROG/DATA bits and restart the write operation from the
mbed_official 354:e67efb2aab0e 306 * beginning.
mbed_official 354:e67efb2aab0e 307 * @retval HAL Status: The returned value can be:
mbed_official 354:e67efb2aab0e 308 * HAL_ERROR, HAL_OK or HAL_TIMEOUT.
mbed_official 354:e67efb2aab0e 309 */
mbed_official 354:e67efb2aab0e 310 __RAM_FUNC HAL_FLASHEx_HalfPageProgram(uint32_t Address, uint32_t* pBuffer)
mbed_official 354:e67efb2aab0e 311 {
mbed_official 354:e67efb2aab0e 312 uint32_t count = 0;
mbed_official 354:e67efb2aab0e 313
mbed_official 354:e67efb2aab0e 314 HAL_StatusTypeDef status = HAL_OK;
mbed_official 354:e67efb2aab0e 315
mbed_official 354:e67efb2aab0e 316 /* Set the DISMCYCINT[0] bit in the Auxillary Control Register (0xE000E008)
mbed_official 354:e67efb2aab0e 317 This bit prevents the interruption of multicycle instructions and therefore
mbed_official 354:e67efb2aab0e 318 will increase the interrupt latency. of Cortex-M3. */
mbed_official 354:e67efb2aab0e 319 SCnSCB->ACTLR |= SCnSCB_ACTLR_DISMCYCINT_Msk;
mbed_official 354:e67efb2aab0e 320
mbed_official 354:e67efb2aab0e 321 /* Wait for last operation to be completed */
mbed_official 354:e67efb2aab0e 322 status = FLASHRAM_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
mbed_official 354:e67efb2aab0e 323
mbed_official 354:e67efb2aab0e 324 if(status == HAL_OK)
mbed_official 354:e67efb2aab0e 325 {
mbed_official 354:e67efb2aab0e 326 /* if the previous operation is completed, proceed to program the new
mbed_official 354:e67efb2aab0e 327 half page */
mbed_official 354:e67efb2aab0e 328 FLASH->PECR |= FLASH_PECR_FPRG;
mbed_official 354:e67efb2aab0e 329 FLASH->PECR |= FLASH_PECR_PROG;
mbed_official 354:e67efb2aab0e 330
mbed_official 354:e67efb2aab0e 331 /* Write one half page directly with 32 different words */
mbed_official 354:e67efb2aab0e 332 while(count < 32)
mbed_official 354:e67efb2aab0e 333 {
mbed_official 354:e67efb2aab0e 334 *(__IO uint32_t*) ((uint32_t)(Address + (4 * count))) = *(pBuffer++);
mbed_official 354:e67efb2aab0e 335 count ++;
mbed_official 354:e67efb2aab0e 336 }
mbed_official 354:e67efb2aab0e 337 /* Wait for last operation to be completed */
mbed_official 354:e67efb2aab0e 338 status = FLASHRAM_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
mbed_official 354:e67efb2aab0e 339
mbed_official 354:e67efb2aab0e 340 /* if the write operation is completed, disable the PROG and FPRG bits */
mbed_official 354:e67efb2aab0e 341 FLASH->PECR &= (uint32_t)(~FLASH_PECR_PROG);
mbed_official 354:e67efb2aab0e 342 FLASH->PECR &= (uint32_t)(~FLASH_PECR_FPRG);
mbed_official 354:e67efb2aab0e 343 }
mbed_official 354:e67efb2aab0e 344
mbed_official 354:e67efb2aab0e 345 SCnSCB->ACTLR &= ~SCnSCB_ACTLR_DISMCYCINT_Msk;
mbed_official 354:e67efb2aab0e 346
mbed_official 354:e67efb2aab0e 347 /* Return the Write Status */
mbed_official 354:e67efb2aab0e 348 return status;
mbed_official 354:e67efb2aab0e 349 }
mbed_official 354:e67efb2aab0e 350
mbed_official 354:e67efb2aab0e 351 /**
mbed_official 354:e67efb2aab0e 352 * @}
mbed_official 354:e67efb2aab0e 353 */
mbed_official 354:e67efb2aab0e 354
mbed_official 354:e67efb2aab0e 355 /** @defgroup FLASHRamfunc_Exported_Functions_Group3 FLASH RAM DATA EEPROM functions
mbed_official 354:e67efb2aab0e 356 *
mbed_official 354:e67efb2aab0e 357 @verbatim
mbed_official 354:e67efb2aab0e 358 @endverbatim
mbed_official 354:e67efb2aab0e 359 * @{
mbed_official 354:e67efb2aab0e 360 */
mbed_official 354:e67efb2aab0e 361
mbed_official 354:e67efb2aab0e 362 /**
mbed_official 354:e67efb2aab0e 363 * @brief Erase a double word in data memory.
mbed_official 354:e67efb2aab0e 364 * @param Address: specifies the address to be erased.
mbed_official 354:e67efb2aab0e 365 * @note To correctly run this function, the HAL_FLASH_EEPROM_Unlock() function
mbed_official 354:e67efb2aab0e 366 * must be called before.
mbed_official 354:e67efb2aab0e 367 * Call the HAL_FLASH_EEPROM_Lock() to he data EEPROM access
mbed_official 354:e67efb2aab0e 368 * and Flash program erase control register access(recommended to protect
mbed_official 354:e67efb2aab0e 369 * the DATA_EEPROM against possible unwanted operation).
mbed_official 354:e67efb2aab0e 370 * @note Data memory double word erase is possible only from SRAM.
mbed_official 354:e67efb2aab0e 371 * @note A double word is erased to the data memory only if the first address
mbed_official 354:e67efb2aab0e 372 * to load is the start address of a double word (multiple of 8 bytes).
mbed_official 354:e67efb2aab0e 373 * @note During the Data memory double word erase, all read operations are
mbed_official 354:e67efb2aab0e 374 * forbidden (this includes DMA read operations and debugger read
mbed_official 354:e67efb2aab0e 375 * operations such as breakpoints, periodic updates, etc.).
mbed_official 354:e67efb2aab0e 376 * @retval HAL Status: The returned value can be:
mbed_official 354:e67efb2aab0e 377 * HAL_ERROR, HAL_OK or HAL_TIMEOUT.
mbed_official 354:e67efb2aab0e 378 */
mbed_official 354:e67efb2aab0e 379
mbed_official 354:e67efb2aab0e 380 __RAM_FUNC HAL_FLASHEx_DATAEEPROM_EraseDoubleWord(uint32_t Address)
mbed_official 354:e67efb2aab0e 381 {
mbed_official 354:e67efb2aab0e 382 HAL_StatusTypeDef status = HAL_OK;
mbed_official 354:e67efb2aab0e 383
mbed_official 354:e67efb2aab0e 384 /* Set the DISMCYCINT[0] bit in the Auxillary Control Register (0xE000E008)
mbed_official 354:e67efb2aab0e 385 This bit prevents the interruption of multicycle instructions and therefore
mbed_official 354:e67efb2aab0e 386 will increase the interrupt latency. of Cortex-M3. */
mbed_official 354:e67efb2aab0e 387 SCnSCB->ACTLR |= SCnSCB_ACTLR_DISMCYCINT_Msk;
mbed_official 354:e67efb2aab0e 388
mbed_official 354:e67efb2aab0e 389 /* Wait for last operation to be completed */
mbed_official 354:e67efb2aab0e 390 status = FLASHRAM_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
mbed_official 354:e67efb2aab0e 391
mbed_official 354:e67efb2aab0e 392 if(status == HAL_OK)
mbed_official 354:e67efb2aab0e 393 {
mbed_official 354:e67efb2aab0e 394 /* If the previous operation is completed, proceed to erase the next double word */
mbed_official 354:e67efb2aab0e 395 /* Set the ERASE bit */
mbed_official 354:e67efb2aab0e 396 FLASH->PECR |= FLASH_PECR_ERASE;
mbed_official 354:e67efb2aab0e 397
mbed_official 354:e67efb2aab0e 398 /* Set DATA bit */
mbed_official 354:e67efb2aab0e 399 FLASH->PECR |= FLASH_PECR_DATA;
mbed_official 354:e67efb2aab0e 400
mbed_official 354:e67efb2aab0e 401 /* Write 00000000h to the 2 words to erase */
mbed_official 354:e67efb2aab0e 402 *(__IO uint32_t *)Address = 0x00000000;
mbed_official 354:e67efb2aab0e 403 Address += 4;
mbed_official 354:e67efb2aab0e 404 *(__IO uint32_t *)Address = 0x00000000;
mbed_official 354:e67efb2aab0e 405
mbed_official 354:e67efb2aab0e 406 /* Wait for last operation to be completed */
mbed_official 354:e67efb2aab0e 407 status = FLASHRAM_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
mbed_official 354:e67efb2aab0e 408
mbed_official 354:e67efb2aab0e 409 /* If the erase operation is completed, disable the ERASE and DATA bits */
mbed_official 354:e67efb2aab0e 410 FLASH->PECR &= (uint32_t)(~FLASH_PECR_ERASE);
mbed_official 354:e67efb2aab0e 411 FLASH->PECR &= (uint32_t)(~FLASH_PECR_DATA);
mbed_official 354:e67efb2aab0e 412 }
mbed_official 354:e67efb2aab0e 413
mbed_official 354:e67efb2aab0e 414 SCnSCB->ACTLR &= ~SCnSCB_ACTLR_DISMCYCINT_Msk;
mbed_official 354:e67efb2aab0e 415
mbed_official 354:e67efb2aab0e 416 /* Return the erase status */
mbed_official 354:e67efb2aab0e 417 return status;
mbed_official 354:e67efb2aab0e 418 }
mbed_official 354:e67efb2aab0e 419
mbed_official 354:e67efb2aab0e 420 /**
mbed_official 354:e67efb2aab0e 421 * @brief Write a double word in data memory without erase.
mbed_official 354:e67efb2aab0e 422 * @param Address: specifies the address to be written.
mbed_official 354:e67efb2aab0e 423 * @param Data: specifies the data to be written.
mbed_official 354:e67efb2aab0e 424 * @note To correctly run this function, the HAL_FLASH_EEPROM_Unlock() function
mbed_official 354:e67efb2aab0e 425 * must be called before.
mbed_official 354:e67efb2aab0e 426 * Call the HAL_FLASH_EEPROM_Lock() to he data EEPROM access
mbed_official 354:e67efb2aab0e 427 * and Flash program erase control register access(recommended to protect
mbed_official 354:e67efb2aab0e 428 * the DATA_EEPROM against possible unwanted operation).
mbed_official 354:e67efb2aab0e 429 * @note Data memory double word write is possible only from SRAM.
mbed_official 354:e67efb2aab0e 430 * @note A data memory double word is written to the data memory only if the
mbed_official 354:e67efb2aab0e 431 * first address to load is the start address of a double word (multiple
mbed_official 354:e67efb2aab0e 432 * of double word).
mbed_official 354:e67efb2aab0e 433 * @note During the Data memory double word write, all read operations are
mbed_official 354:e67efb2aab0e 434 * forbidden (this includes DMA read operations and debugger read
mbed_official 354:e67efb2aab0e 435 * operations such as breakpoints, periodic updates, etc.).
mbed_official 354:e67efb2aab0e 436 * @retval HAL Status: The returned value can be:
mbed_official 354:e67efb2aab0e 437 * HAL_ERROR, HAL_OK or HAL_TIMEOUT.
mbed_official 354:e67efb2aab0e 438 */
mbed_official 354:e67efb2aab0e 439 __RAM_FUNC HAL_FLASHEx_DATAEEPROM_ProgramDoubleWord(uint32_t Address, uint64_t Data)
mbed_official 354:e67efb2aab0e 440 {
mbed_official 354:e67efb2aab0e 441 HAL_StatusTypeDef status = HAL_OK;
mbed_official 354:e67efb2aab0e 442
mbed_official 354:e67efb2aab0e 443 /* Set the DISMCYCINT[0] bit in the Auxillary Control Register (0xE000E008)
mbed_official 354:e67efb2aab0e 444 This bit prevents the interruption of multicycle instructions and therefore
mbed_official 354:e67efb2aab0e 445 will increase the interrupt latency. of Cortex-M3. */
mbed_official 354:e67efb2aab0e 446 SCnSCB->ACTLR |= SCnSCB_ACTLR_DISMCYCINT_Msk;
mbed_official 354:e67efb2aab0e 447
mbed_official 354:e67efb2aab0e 448 /* Wait for last operation to be completed */
mbed_official 354:e67efb2aab0e 449 status = FLASHRAM_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
mbed_official 354:e67efb2aab0e 450
mbed_official 354:e67efb2aab0e 451 if(status == HAL_OK)
mbed_official 354:e67efb2aab0e 452 {
mbed_official 354:e67efb2aab0e 453 /* If the previous operation is completed, proceed to program the new data*/
mbed_official 354:e67efb2aab0e 454 FLASH->PECR |= FLASH_PECR_FPRG;
mbed_official 354:e67efb2aab0e 455 FLASH->PECR |= FLASH_PECR_DATA;
mbed_official 354:e67efb2aab0e 456
mbed_official 354:e67efb2aab0e 457 /* Write the 2 words */
mbed_official 354:e67efb2aab0e 458 *(__IO uint32_t *)Address = (uint32_t) Data;
mbed_official 354:e67efb2aab0e 459 Address += 4;
mbed_official 354:e67efb2aab0e 460 *(__IO uint32_t *)Address = (uint32_t) (Data >> 32);
mbed_official 354:e67efb2aab0e 461
mbed_official 354:e67efb2aab0e 462 /* Wait for last operation to be completed */
mbed_official 354:e67efb2aab0e 463 status = FLASHRAM_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
mbed_official 354:e67efb2aab0e 464
mbed_official 354:e67efb2aab0e 465 /* If the write operation is completed, disable the FPRG and DATA bits */
mbed_official 354:e67efb2aab0e 466 FLASH->PECR &= (uint32_t)(~FLASH_PECR_FPRG);
mbed_official 354:e67efb2aab0e 467 FLASH->PECR &= (uint32_t)(~FLASH_PECR_DATA);
mbed_official 354:e67efb2aab0e 468 }
mbed_official 354:e67efb2aab0e 469
mbed_official 354:e67efb2aab0e 470 SCnSCB->ACTLR &= ~SCnSCB_ACTLR_DISMCYCINT_Msk;
mbed_official 354:e67efb2aab0e 471
mbed_official 354:e67efb2aab0e 472 /* Return the Write Status */
mbed_official 354:e67efb2aab0e 473 return status;
mbed_official 354:e67efb2aab0e 474 }
mbed_official 354:e67efb2aab0e 475
mbed_official 354:e67efb2aab0e 476 /**
mbed_official 354:e67efb2aab0e 477 * @}
mbed_official 354:e67efb2aab0e 478 */
mbed_official 354:e67efb2aab0e 479
mbed_official 354:e67efb2aab0e 480 /**
mbed_official 354:e67efb2aab0e 481 * @}
mbed_official 354:e67efb2aab0e 482 */
mbed_official 354:e67efb2aab0e 483
mbed_official 354:e67efb2aab0e 484 /** @defgroup FLASHRamfunc_Private_Functions FLASH RAM Private Functions
mbed_official 354:e67efb2aab0e 485 * @{
mbed_official 354:e67efb2aab0e 486 */
mbed_official 354:e67efb2aab0e 487
mbed_official 354:e67efb2aab0e 488 /**
mbed_official 354:e67efb2aab0e 489 * @brief Wait for a FLASH operation to complete.
mbed_official 354:e67efb2aab0e 490 * @param Timeout: maximum flash operationtimeout
mbed_official 354:e67efb2aab0e 491 * @retval HAL status
mbed_official 354:e67efb2aab0e 492 */
mbed_official 354:e67efb2aab0e 493 static __RAM_FUNC FLASHRAM_WaitForLastOperation(uint32_t Timeout)
mbed_official 354:e67efb2aab0e 494 {
mbed_official 354:e67efb2aab0e 495 /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
mbed_official 354:e67efb2aab0e 496 Even if the FLASH operation fails, the BUSY flag will be reset and an error
mbed_official 354:e67efb2aab0e 497 flag will be set */
mbed_official 354:e67efb2aab0e 498
mbed_official 354:e67efb2aab0e 499 while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY) && (Timeout != 0x00))
mbed_official 354:e67efb2aab0e 500 {
mbed_official 354:e67efb2aab0e 501 Timeout--;
mbed_official 354:e67efb2aab0e 502 }
mbed_official 354:e67efb2aab0e 503
mbed_official 354:e67efb2aab0e 504 if(Timeout == 0x00 )
mbed_official 354:e67efb2aab0e 505 {
mbed_official 354:e67efb2aab0e 506 return HAL_TIMEOUT;
mbed_official 354:e67efb2aab0e 507 }
mbed_official 354:e67efb2aab0e 508
mbed_official 354:e67efb2aab0e 509 if( (__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) != RESET) ||
mbed_official 354:e67efb2aab0e 510 (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR) != RESET) ||
mbed_official 354:e67efb2aab0e 511 (__HAL_FLASH_GET_FLAG(FLASH_FLAG_SIZERR) != RESET) ||
mbed_official 354:e67efb2aab0e 512 #if defined (STM32L151xBA) || defined (STM32L152xBA) || \
mbed_official 354:e67efb2aab0e 513 defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC)
mbed_official 354:e67efb2aab0e 514 (__HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR) != RESET) ||
mbed_official 354:e67efb2aab0e 515 #endif /* STM32L151xBA || STM32L152xBA || STM32L151xC || STM32L152xC || STM32L162xC */
mbed_official 354:e67efb2aab0e 516 #if defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || \
mbed_official 354:e67efb2aab0e 517 defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || \
mbed_official 354:e67efb2aab0e 518 defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)
mbed_official 354:e67efb2aab0e 519 (__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERRUSR) != RESET) ||
mbed_official 354:e67efb2aab0e 520 #endif /* STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
mbed_official 354:e67efb2aab0e 521 (__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR) != RESET) )
mbed_official 354:e67efb2aab0e 522 {
mbed_official 354:e67efb2aab0e 523 return HAL_ERROR;
mbed_official 354:e67efb2aab0e 524 }
mbed_official 354:e67efb2aab0e 525
mbed_official 354:e67efb2aab0e 526 /* If there is an error flag set */
mbed_official 354:e67efb2aab0e 527 return HAL_OK;
mbed_official 354:e67efb2aab0e 528 }
mbed_official 354:e67efb2aab0e 529
mbed_official 354:e67efb2aab0e 530 #endif /* HAL_FLASH_MODULE_ENABLED */
mbed_official 354:e67efb2aab0e 531
mbed_official 354:e67efb2aab0e 532 /**
mbed_official 354:e67efb2aab0e 533 * @}
mbed_official 354:e67efb2aab0e 534 */
mbed_official 354:e67efb2aab0e 535
mbed_official 354:e67efb2aab0e 536 /**
mbed_official 354:e67efb2aab0e 537 * @}
mbed_official 354:e67efb2aab0e 538 */
mbed_official 354:e67efb2aab0e 539
mbed_official 354:e67efb2aab0e 540 /**
mbed_official 354:e67efb2aab0e 541 * @}
mbed_official 354:e67efb2aab0e 542 */
mbed_official 354:e67efb2aab0e 543
mbed_official 354:e67efb2aab0e 544
mbed_official 354:e67efb2aab0e 545 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/