]> git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32L1/stm32l1xx_hal_flash_ex.c
Merge commit '22b6e15a179031afb7c3534cf7b109b0668b602c'
[tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32L1 / stm32l1xx_hal_flash_ex.c
1 /**
2 ******************************************************************************
3 * @file stm32l1xx_hal_flash_ex.c
4 * @author MCD Application Team
5 * @version V1.0.0
6 * @date 5-September-2014
7 * @brief FLASH HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of the internal FLASH memory:
10 * + FLASH Interface configuration
11 * + FLASH Memory Erasing
12 * + DATA EEPROM Programming/Erasing
13 * + Option Bytes Programming
14 * + Interrupts management
15 *
16 * @verbatim
17 ==============================================================================
18 ##### Flash peripheral Extended features #####
19 ==============================================================================
20
21 [..] Comparing to other products, the FLASH interface for STM32L1xx
22 devices contains the following additional features
23 (+) Erase functions
24 (+) DATA_EEPROM memory management
25 (+) BOOT option bit configuration
26 (+) PCROP protection for all sectors
27
28 ##### How to use this driver #####
29 ==============================================================================
30 [..] This driver provides functions to configure and program the FLASH memory
31 of all STM32L1xx. It includes:
32 (+) Full DATA_EEPROM erase and program management
33 (+) Boot activation
34 (+) PCROP protection configuration and control for all pages
35
36 @endverbatim
37 ******************************************************************************
38 * @attention
39 *
40 * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
41 *
42 * Redistribution and use in source and binary forms, with or without modification,
43 * are permitted provided that the following conditions are met:
44 * 1. Redistributions of source code must retain the above copyright notice,
45 * this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright notice,
47 * this list of conditions and the following disclaimer in the documentation
48 * and/or other materials provided with the distribution.
49 * 3. Neither the name of STMicroelectronics nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
54 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
56 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
59 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
60 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
61 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
62 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63 *
64 ******************************************************************************
65 */
66
67 /* Includes ------------------------------------------------------------------*/
68 #include "stm32l1xx_hal.h"
69
70 /** @addtogroup STM32L1xx_HAL_Driver
71 * @{
72 */
73
74 /** @defgroup FLASHEx FLASHEx
75 * @brief FLASH HAL Extension module driver
76 * @{
77 */
78
79 #ifdef HAL_FLASH_MODULE_ENABLED
80
81 /* Private typedef -----------------------------------------------------------*/
82 /* Private define ------------------------------------------------------------*/
83 /* Private macro -------------------------------------------------------------*/
84 /* Private variables ---------------------------------------------------------*/
85 /* Private function prototypes -----------------------------------------------*/
86 static void FLASH_SetErrorCode(void);
87 static void FLASH_ErasePage(uint32_t PageAddress);
88
89 static HAL_StatusTypeDef FLASH_OB_WRPConfig(FLASH_OBProgramInitTypeDef *pOBInit, FunctionalState NewState);
90 static void FLASH_OB_WRPConfigWRP1OrPCROP1(uint32_t WRP1OrPCROP1, FunctionalState NewState);
91 #if defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || \
92 defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || \
93 defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)
94 static void FLASH_OB_WRPConfigWRP2OrPCROP2(uint32_t WRP2OrPCROP2, FunctionalState NewState);
95 #endif /* STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
96 #if defined (STM32L151xD) || defined (STM32L152xD) || defined (STM32L162xD) || \
97 defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)
98 static void FLASH_OB_WRPConfigWRP3(uint32_t WRP3, FunctionalState NewState);
99 #endif /* STM32L151xD || STM32L152xD || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
100 #if defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)
101 static void FLASH_OB_WRPConfigWRP4(uint32_t WRP4, FunctionalState NewState);
102 #endif /* STM32L151xE || STM32L152xE || STM32L162xE */
103 static HAL_StatusTypeDef FLASH_OB_RDPConfig(uint8_t OB_RDP);
104 static HAL_StatusTypeDef FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY);
105 static HAL_StatusTypeDef FLASH_OB_BORConfig(uint8_t OB_BOR);
106 static uint8_t FLASH_OB_GetRDP(void);
107 static uint8_t FLASH_OB_GetUser(void);
108 static uint8_t FLASH_OB_GetBOR(void);
109 #if defined (STM32L151xBA) || defined (STM32L152xBA) || \
110 defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC)
111 static HAL_StatusTypeDef FLASH_OB_PCROPConfig(FLASH_AdvOBProgramInitTypeDef *pAdvOBInit, FunctionalState NewState);
112 #endif /* STM32L151xBA || STM32L152xBA || STM32L151xC || STM32L152xC || STM32L162xC */
113 #if defined (STM32L151xD) || defined (STM32L152xD) || defined (STM32L162xD) || \
114 defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)
115 static HAL_StatusTypeDef FLASH_OB_BootConfig(uint8_t OB_BOOT);
116 #endif /* STM32L151xD || STM32L152xD || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
117
118 static HAL_StatusTypeDef FLASH_DATAEEPROM_FastProgramByte(uint32_t Address, uint8_t Data);
119 static HAL_StatusTypeDef FLASH_DATAEEPROM_FastProgramHalfWord(uint32_t Address, uint16_t Data);
120 static HAL_StatusTypeDef FLASH_DATAEEPROM_FastProgramWord(uint32_t Address, uint32_t Data);
121 static HAL_StatusTypeDef FLASH_DATAEEPROM_ProgramWord(uint32_t Address, uint32_t Data);
122 static HAL_StatusTypeDef FLASH_DATAEEPROM_ProgramHalfWord(uint32_t Address, uint16_t Data);
123 static HAL_StatusTypeDef FLASH_DATAEEPROM_ProgramByte(uint32_t Address, uint8_t Data);
124
125 /* Exported functions ---------------------------------------------------------*/
126
127 /** @defgroup FLASHEx_Exported_Functions FLASHEx Exported functions
128 * @{
129 */
130
131 /** @defgroup FLASHEx_Exported_Functions_Group1 FLASH Memory Erasing functions
132 * @brief FLASH Memory Erasing functions
133 *
134 @verbatim
135 ==============================================================================
136 ##### FLASH Erasing Programming functions #####
137 ==============================================================================
138
139 [..] The FLASH Memory Erasing functions, includes the following functions:
140 (+) HAL_FLASHEx_Erase: return only when erase has been done
141 (+) HAL_FLASHEx_Erase_IT: end of erase is done when HAL_FLASH_EndOfOperationCallback is called with parameter
142 0xFFFFFFFF
143
144 [..] Any operation of erase should follow these steps:
145 (#) Call the HAL_FLASH_Unlock() function to enable the flash control register and
146 program memory access.
147 (#) Call the desired function to erase page.
148 (#) Call the HAL_FLASH_Lock() to disable the flash program memory access
149 (recommended to protect the FLASH memory against possible unwanted operation).
150
151 @endverbatim
152 * @{
153 */
154
155 /**
156 * @brief Erase the specified FLASH memory Pages
157 * @note To correctly run this function, the HAL_FLASH_Unlock() function
158 * must be called before.
159 * Call the HAL_FLASH_Lock() to disable the flash memory access
160 * (recommended to protect the FLASH memory against possible unwanted operation)
161 * @param[in] pEraseInit: pointer to an FLASH_EraseInitTypeDef structure that
162 * contains the configuration information for the erasing.
163 *
164 * @param[out] PageError: pointer to variable that
165 * contains the configuration information on faulty sector in case of error
166 * (0xFFFFFFFF means that all the sectors have been correctly erased)
167 *
168 * @retval HAL_StatusTypeDef HAL Status
169 */
170 HAL_StatusTypeDef HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *PageError)
171 {
172 HAL_StatusTypeDef status = HAL_ERROR;
173 uint32_t index = 0;
174
175 /* Process Locked */
176 __HAL_LOCK(&ProcFlash);
177
178 /* Wait for last operation to be completed */
179 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
180
181 if (status == HAL_OK)
182 {
183 /*Initialization of PageError variable*/
184 *PageError = 0xFFFFFFFF;
185
186 /* Check the parameters */
187 assert_param(IS_NBPAGES(pEraseInit->NbPages));
188 assert_param(IS_TYPEERASE(pEraseInit->TypeErase));
189 assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit->PageAddress));
190 assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit->PageAddress + pEraseInit->NbPages * FLASH_PAGE_SIZE - 1));
191
192 /* Erase by sector by sector to be done*/
193 for(index = pEraseInit->PageAddress; index < ((pEraseInit->NbPages * FLASH_PAGE_SIZE)+ pEraseInit->PageAddress); index += FLASH_PAGE_SIZE)
194 {
195 FLASH_ErasePage(index);
196
197 /* Wait for last operation to be completed */
198 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
199
200 /* If the erase operation is completed, disable the ERASE Bit */
201 CLEAR_BIT(FLASH->PECR, FLASH_PECR_PROG);
202 CLEAR_BIT(FLASH->PECR, FLASH_PECR_ERASE);
203
204 if (status != HAL_OK)
205 {
206 /* In case of error, stop erase procedure and return the faulty sector*/
207 *PageError = index;
208 break;
209 }
210 }
211 }
212
213 /* Process Unlocked */
214 __HAL_UNLOCK(&ProcFlash);
215
216 return status;
217 }
218
219 /**
220 * @brief Perform a page erase of the specified FLASH memory pages with interrupt enabled
221 * @note To correctly run this function, the HAL_FLASH_Unlock() function
222 * must be called before.
223 * Call the HAL_FLASH_Lock() to disable the flash memory access
224 * (recommended to protect the FLASH memory against possible unwanted operation)
225 * @param pEraseInit: pointer to an FLASH_EraseInitTypeDef structure that
226 * contains the configuration information for the erasing.
227 *
228 * @retval HAL_StatusTypeDef HAL Status
229 */
230 HAL_StatusTypeDef HAL_FLASHEx_Erase_IT(FLASH_EraseInitTypeDef *pEraseInit)
231 {
232 HAL_StatusTypeDef status = HAL_OK;
233
234 /* Process Locked */
235 __HAL_LOCK(&ProcFlash);
236
237 /* Enable End of FLASH Operation interrupt */
238 __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP);
239
240 /* Enable Error source interrupt */
241 __HAL_FLASH_ENABLE_IT(FLASH_IT_ERR);
242
243 /* Clear pending flags (if any) */
244 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_MASK);
245
246 /* Check the parameters */
247 assert_param(IS_NBPAGES(pEraseInit->NbPages));
248 assert_param(IS_TYPEERASE(pEraseInit->TypeErase));
249 assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit->PageAddress));
250 assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit->PageAddress + pEraseInit->NbPages * FLASH_PAGE_SIZE - 1));
251
252 ProcFlash.ProcedureOnGoing = FLASH_PROC_PAGEERASE;
253 ProcFlash.NbPagesToErase = pEraseInit->NbPages;
254 ProcFlash.Page = pEraseInit->PageAddress;
255
256 /*Erase 1st page and wait for IT*/
257 FLASH_ErasePage(pEraseInit->PageAddress);
258
259 return status;
260 }
261
262 /**
263 * @}
264 */
265
266
267 /** @defgroup FLASHEx_Exported_Functions_Group2 Option Bytes Programming functions
268 * @brief Option Bytes Programming functions
269 *
270 @verbatim
271 ==============================================================================
272 ##### Option Bytes Programming functions #####
273 ==============================================================================
274
275 [..] Any operation of erase or program should follow these steps:
276 (#) Call the HAL_FLASH_OB_Unlock() function to enable the Flash option control
277 register access.
278 (#) Call following function to program the desired option bytes.
279 (++) HAL_FLASHEx_OBProgram:
280 - To Enable/Disable the desired sector write protection.
281 - To set the desired read Protection Level.
282 - To configure the user option Bytes: IWDG, STOP and the Standby.
283 - To Set the BOR level.
284 (#) Once all needed option bytes to be programmed are correctly written, call the
285 HAL_FLASH_OB_Launch(void) function to launch the Option Bytes programming process.
286 (#) Call the HAL_FLASH_OB_Lock() to disable the Flash option control register access (recommended
287 to protect the option Bytes against possible unwanted operations).
288
289 [..] Proprietary code Read Out Protection (PcROP):
290 (#) The PcROP sector is selected by using the same option bytes as the Write
291 protection (nWRPi bits). As a result, these 2 options are exclusive each other.
292 (#) In order to activate the PcROP (change the function of the nWRPi option bits),
293 the SPRMOD option bit must be activated.
294 (#) The active value of nWRPi bits is inverted when PCROP mode is active, this
295 means: if SPRMOD = 1 and nWRPi = 1 (default value), then the user sector "i"
296 is read/write protected.
297 (#) To activate PCROP mode for Flash sector(s), you need to call the following function:
298 (++) HAL_FLASHEx_AdvOBProgram in selecting sectors to be read/write protected
299 (++) HAL_FLASHEx_OB_SelectPCROP to enable the read/write protection
300 (#) PcROP is available only in STM32L151xBA, STM32L152xBA, STM32L151xC, STM32L152xC & STM32L162xC devices.
301
302 @endverbatim
303 * @{
304 */
305
306 /**
307 * @brief Program option bytes
308 * @param pOBInit: pointer to an FLASH_OBInitStruct structure that
309 * contains the configuration information for the programming.
310 *
311 * @retval HAL_StatusTypeDef HAL Status
312 */
313 HAL_StatusTypeDef HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit)
314 {
315 HAL_StatusTypeDef status = HAL_ERROR;
316
317 /* Process Locked */
318 __HAL_LOCK(&ProcFlash);
319
320 /* Check the parameters */
321 assert_param(IS_OPTIONBYTE(pOBInit->OptionType));
322
323 /*Write protection configuration*/
324 if((pOBInit->OptionType & OPTIONBYTE_WRP) == OPTIONBYTE_WRP)
325 {
326 assert_param(IS_WRPSTATE(pOBInit->WRPState));
327 if (pOBInit->WRPState == WRPSTATE_ENABLE)
328 {
329 /* Enable of Write protection on the selected Sector*/
330 status = FLASH_OB_WRPConfig(pOBInit, ENABLE);
331 }
332 else
333 {
334 /* Disable of Write protection on the selected Sector*/
335 status = FLASH_OB_WRPConfig(pOBInit, DISABLE);
336 }
337 if (status != HAL_OK)
338 {
339 /* Process Unlocked */
340 __HAL_UNLOCK(&ProcFlash);
341 return status;
342 }
343 }
344
345 /* Read protection configuration*/
346 if((pOBInit->OptionType & OPTIONBYTE_RDP) == OPTIONBYTE_RDP)
347 {
348 status = FLASH_OB_RDPConfig(pOBInit->RDPLevel);
349 if (status != HAL_OK)
350 {
351 /* Process Unlocked */
352 __HAL_UNLOCK(&ProcFlash);
353 return status;
354 }
355 }
356
357 /* USER configuration*/
358 if((pOBInit->OptionType & OPTIONBYTE_USER) == OPTIONBYTE_USER)
359 {
360 status = FLASH_OB_UserConfig(pOBInit->USERConfig & OB_IWDG_SW,
361 pOBInit->USERConfig & OB_STOP_NORST,
362 pOBInit->USERConfig & OB_STDBY_NORST);
363 if (status != HAL_OK)
364 {
365 /* Process Unlocked */
366 __HAL_UNLOCK(&ProcFlash);
367 return status;
368 }
369 }
370
371 /* BOR Level configuration*/
372 if((pOBInit->OptionType & OPTIONBYTE_BOR) == OPTIONBYTE_BOR)
373 {
374 status = FLASH_OB_BORConfig(pOBInit->BORLevel);
375 }
376 /* Process Unlocked */
377 __HAL_UNLOCK(&ProcFlash);
378
379 return status;
380 }
381
382 /**
383 * @brief Get the Option byte configuration
384 * @param pOBInit: pointer to an FLASH_OBInitStruct structure that
385 * contains the configuration information for the programming.
386 *
387 * @retval None
388 */
389 void HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit)
390 {
391 pOBInit->OptionType = OPTIONBYTE_WRP | OPTIONBYTE_RDP | OPTIONBYTE_USER | OPTIONBYTE_BOR;
392
393 /*Get WRP1*/
394 pOBInit->WRPSector0To31 = (uint32_t)(FLASH->WRPR1);
395
396 #if defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || \
397 defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || \
398 defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)
399
400 /*Get WRP2*/
401 pOBInit->WRPSector32To63 = (uint32_t)(FLASH->WRPR2);
402
403 #endif /* STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
404
405 #if defined (STM32L151xD) || defined (STM32L152xD) || defined (STM32L162xD) || \
406 defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)
407
408 /*Get WRP3*/
409 pOBInit->WRPSector64To95 = (uint32_t)(FLASH->WRPR3);
410
411 #endif /* STM32L151xD || STM32L152xD || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
412
413 #if defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)
414
415 /*Get WRP4*/
416 pOBInit->WRPSector96To127 = (uint32_t)(FLASH->WRPR4);
417
418 #endif /* STM32L151xE || STM32L152xE || STM32L162xE */
419
420 /*Get RDP Level*/
421 pOBInit->RDPLevel = FLASH_OB_GetRDP();
422
423 /*Get USER*/
424 pOBInit->USERConfig = FLASH_OB_GetUser();
425
426 /*Get BOR Level*/
427 pOBInit->BORLevel = FLASH_OB_GetBOR();
428
429 }
430
431 #if defined (STM32L151xBA) || defined (STM32L152xBA) || \
432 defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || \
433 defined (STM32L151xD) || defined (STM32L152xD) || defined (STM32L162xD) || \
434 defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)
435
436 /**
437 * @brief Program option bytes
438 * @note This function can be used only for Cat2 & Cat3 devices for PCROP and Cat4 & Cat5 for BFB2.
439 * @param pAdvOBInit: pointer to an FLASH_AdvOBProgramInitTypeDef structure that
440 * contains the configuration information for the programming.
441 *
442 * @retval HAL_StatusTypeDef HAL Status
443 */
444 HAL_StatusTypeDef HAL_FLASHEx_AdvOBProgram (FLASH_AdvOBProgramInitTypeDef *pAdvOBInit)
445 {
446 HAL_StatusTypeDef status = HAL_ERROR;
447
448 /* Check the parameters */
449 assert_param(IS_OBEX(pAdvOBInit->OptionType));
450
451 #if defined (STM32L151xBA) || defined (STM32L152xBA) || \
452 defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC)
453
454 /* Cat2 & Cat3 devices only */
455 /*Program PCROP option byte*/
456 if ((pAdvOBInit->OptionType & OBEX_PCROP) == OBEX_PCROP)
457 {
458 /* Check the parameters */
459 assert_param(IS_PCROPSTATE(pAdvOBInit->PCROPState));
460 if (pAdvOBInit->PCROPState == PCROPSTATE_ENABLE)
461 {
462 /*Enable of Write protection on the selected Sector*/
463 status = FLASH_OB_PCROPConfig(pAdvOBInit, ENABLE);
464 if (status != HAL_OK)
465 {
466 return status;
467 }
468 }
469 else
470 {
471 /*Disable of Write protection on the selected Sector*/
472 status = FLASH_OB_PCROPConfig(pAdvOBInit, DISABLE);
473 if (status != HAL_OK)
474 {
475 return status;
476 }
477 }
478 }
479
480 #endif /* STM32L151xBA || STM32L152xBA || STM32L151xC || STM32L152xC || STM32L162xC */
481
482 #if defined (STM32L151xD) || defined (STM32L152xD) || defined (STM32L162xD) || \
483 defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)
484
485 /* Cat4 & Cat5 devices only */
486 /*Program BOOT config option byte*/
487 if ((pAdvOBInit->OptionType & OBEX_BOOTCONFIG) == OBEX_BOOTCONFIG)
488 {
489 status = FLASH_OB_BootConfig(pAdvOBInit->BootConfig);
490 }
491
492 #endif /* STM32L151xD || STM32L152xD || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
493
494 return status;
495 }
496
497 /**
498 * @brief Get the OBEX byte configuration
499 * @note This function can be used only for Cat2 & Cat3 devices for PCROP and Cat4 & Cat5 for BFB2.
500 * @param pAdvOBInit: pointer to an FLASH_AdvOBProgramInitTypeDef structure that
501 * contains the configuration information for the programming.
502 *
503 * @retval None
504 */
505 void HAL_FLASHEx_AdvOBGetConfig(FLASH_AdvOBProgramInitTypeDef *pAdvOBInit)
506 {
507 #if defined (STM32L151xBA) || defined (STM32L152xBA) || \
508 defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC)
509
510 pAdvOBInit->OptionType = OBEX_PCROP;
511
512 /*Get PCROP state */
513 pAdvOBInit->PCROPState = (FLASH->OBR & FLASH_OBR_SPRMOD) >> POSITION_VAL(FLASH_OBR_SPRMOD);
514
515 /*Get PCROP protected sector from 0 to 31 */
516 pAdvOBInit->PCROPSector0To31 = FLASH->WRPR1;
517
518 #if defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC)
519
520 /*Get PCROP protected sector from 32 to 63 */
521 pAdvOBInit->PCROPSector32To63 = FLASH->WRPR2;
522
523 #endif /* STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC */
524
525 #endif /* STM32L151xBA || STM32L152xBA || STM32L151xC || STM32L152xC || STM32L162xC */
526
527 #if defined (STM32L151xD) || defined (STM32L152xD) || defined (STM32L162xD) || \
528 defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)
529
530 pAdvOBInit->OptionType = OBEX_BOOTCONFIG;
531
532 /*Get Boot config OB*/
533 pAdvOBInit->BootConfig = (FLASH->OBR & 0x80000000) >> 24;
534
535 #endif /* STM32L151xD || STM32L152xD || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
536 }
537
538 #endif /* STM32L151xBA || STM32L152xBA || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xD || STM32L152xD || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
539
540 #if defined (STM32L151xBA) || defined (STM32L152xBA) || \
541 defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC)
542
543 /**
544 * @brief Select the Protection Mode (SPRMOD).
545 * @note This function can be used only for STM32L151xBA, STM32L152xBA, STM32L151xC, STM32L152xC & STM32L162xC devices
546 * @note Once SPRMOD bit is active, unprotection of a protected sector is not possible
547 * @note Read a protected sector will set RDERR Flag and write a protected sector will set WRPERR Flag
548 * @retval HAL status
549 */
550 HAL_StatusTypeDef HAL_FLASHEx_OB_SelectPCROP(void)
551 {
552 HAL_StatusTypeDef status = HAL_OK;
553 uint16_t tmp1 = 0;
554 uint32_t tmp2 = 0;
555 uint8_t optiontmp = 0;
556 uint16_t optiontmp2 = 0;
557
558 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
559
560 /* Mask RDP Byte */
561 optiontmp = (uint8_t)(*(__IO uint8_t *)(OB_BASE));
562
563 /* Update Option Byte */
564 optiontmp2 = (uint16_t)(OB_PCROP_SELECTED | optiontmp);
565
566 /* calculate the option byte to write */
567 tmp1 = (uint16_t)(~(optiontmp2 ));
568 tmp2 = (uint32_t)(((uint32_t)((uint32_t)(tmp1) << 16)) | ((uint32_t)optiontmp2));
569
570 if(status == HAL_OK)
571 {
572 /* program PCRop */
573 OB->RDP = tmp2;
574 }
575
576 /* Wait for last operation to be completed */
577 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
578
579 /* Return the Read protection operation Status */
580 return status;
581 }
582
583 /**
584 * @brief Deselect the Protection Mode (SPRMOD).
585 * @note This function can be used only for STM32L151xBA, STM32L152xBA, STM32L151xC, STM32L152xC & STM32L162xC devices
586 * @note Once SPRMOD bit is active, unprotection of a protected sector is not possible
587 * @note Read a protected sector will set RDERR Flag and write a protected sector will set WRPERR Flag
588 * @retval HAL status
589 */
590 HAL_StatusTypeDef HAL_FLASHEx_OB_DeSelectPCROP(void)
591 {
592 HAL_StatusTypeDef status = HAL_OK;
593 uint16_t tmp1 = 0;
594 uint32_t tmp2 = 0;
595 uint8_t optiontmp = 0;
596 uint16_t optiontmp2 = 0;
597
598 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
599
600 /* Mask RDP Byte */
601 optiontmp = (uint8_t)(*(__IO uint8_t *)(OB_BASE));
602
603 /* Update Option Byte */
604 optiontmp2 = (uint16_t)(OB_PCROP_DESELECTED | optiontmp);
605
606 /* calculate the option byte to write */
607 tmp1 = (uint16_t)(~(optiontmp2 ));
608 tmp2 = (uint32_t)(((uint32_t)((uint32_t)(tmp1) << 16)) | ((uint32_t)optiontmp2));
609
610 if(status == HAL_OK)
611 {
612 /* program PCRop */
613 OB->RDP = tmp2;
614 }
615
616 /* Wait for last operation to be completed */
617 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
618
619 /* Return the Read protection operation Status */
620 return status;
621 }
622
623 #endif /* STM32L151xBA || STM32L152xBA || STM32L151xC || STM32L152xC || STM32L162xC */
624
625 /**
626 * @}
627 */
628
629 /** @defgroup FLASHEx_Exported_Functions_Group3 DATA EEPROM Programming functions
630 * @brief DATA EEPROM Programming functions
631 *
632 @verbatim
633 ===============================================================================
634 ##### DATA EEPROM Programming functions #####
635 ===============================================================================
636
637 [..] Any operation of erase or program should follow these steps:
638 (#) Call the HAL_FLASHEx_DATAEEPROM_Unlock() function to enable the data EEPROM access
639 and Flash program erase control register access.
640 (#) Call the desired function to erase or program data.
641 (#) Call the HAL_FLASHEx_DATAEEPROM_Lock() to disable the data EEPROM access
642 and Flash program erase control register access(recommended
643 to protect the DATA_EEPROM against possible unwanted operation).
644
645 @endverbatim
646 * @{
647 */
648
649 /**
650 * @brief Unlocks the data memory and FLASH_PECR register access.
651 * @retval HAL_StatusTypeDef HAL Status
652 */
653 HAL_StatusTypeDef HAL_FLASHEx_DATAEEPROM_Unlock(void)
654 {
655 if((FLASH->PECR & FLASH_PECR_PELOCK) != RESET)
656 {
657 /* Unlocking the Data memory and FLASH_PECR register access*/
658 FLASH->PEKEYR = FLASH_PEKEY1;
659 FLASH->PEKEYR = FLASH_PEKEY2;
660 }
661 else
662 {
663 return HAL_ERROR;
664 }
665 return HAL_OK;
666 }
667
668 /**
669 * @brief Locks the Data memory and FLASH_PECR register access.
670 * @retval HAL_StatusTypeDef HAL Status
671 */
672 HAL_StatusTypeDef HAL_FLASHEx_DATAEEPROM_Lock(void)
673 {
674 /* Set the PELOCK Bit to lock the data memory and FLASH_PECR register access */
675 SET_BIT(FLASH->PECR, FLASH_PECR_PELOCK);
676
677 return HAL_OK;
678 }
679
680 /**
681 * @brief Erase a word in data memory.
682 * @param Address: specifies the address to be erased.
683 * @param TypeErase: Indicate the way to erase at a specified address.
684 * This parameter can be a value of @ref FLASH_Type_Program
685 * @note To correctly run this function, the DATA_EEPROM_Unlock() function
686 * must be called before.
687 * Call the DATA_EEPROM_Lock() to the data EEPROM access
688 * and Flash program erase control register access(recommended to protect
689 * the DATA_EEPROM against possible unwanted operation).
690 * @retval FLASH Status: The returned value can be:
691 * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
692 */
693 HAL_StatusTypeDef HAL_FLASHEx_DATAEEPROM_Erase(uint32_t TypeErase, uint32_t Address)
694 {
695 HAL_StatusTypeDef status = HAL_OK;
696
697 /* Check the parameters */
698 assert_param(IS_TYPEPROGRAMDATA(TypeErase));
699 assert_param(IS_FLASH_DATA_ADDRESS(Address));
700
701 /* Wait for last operation to be completed */
702 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
703
704 if(status == HAL_OK)
705 {
706 if(TypeErase == TYPEERASEDATA_WORD)
707 {
708 /* Write 00000000h to valid address in the data memory */
709 *(__IO uint32_t *) Address = 0x00000000;
710 }
711
712 if(TypeErase == TYPEERASEDATA_HALFWORD)
713 {
714 /* Write 0000h to valid address in the data memory */
715 *(__IO uint16_t *) Address = (uint16_t)0x0000;
716 }
717
718 if(TypeErase == TYPEERASEDATA_BYTE)
719 {
720 /* Write 00h to valid address in the data memory */
721 *(__IO uint8_t *) Address = (uint8_t)0x00;
722 }
723 }
724
725 /* Return the erase status */
726 return status;
727 }
728
729 /**
730 * @brief Program word at a specified address
731 * @note To correctly run this function, the HAL_FLASH_EEPROM_Unlock() function
732 * must be called before.
733 * Call the HAL_FLASHEx_DATAEEPROM_Unlock() to he data EEPROM access
734 * and Flash program erase control register access(recommended to protect
735 * the DATA_EEPROM against possible unwanted operation).
736 * @note The function HAL_FLASHEx_DATAEEPROM_EnableFixedTimeProgram() can be called before
737 * this function to configure the Fixed Time Programming.
738 * @param TypeProgram: Indicate the way to program at a specified address.
739 * This parameter can be a value of @ref FLASHEx_Type_Program_Data
740 * @param Address: specifies the address to be programmed.
741 * @param Data: specifies the data to be programmed
742 *
743 * @retval HAL_StatusTypeDef HAL Status
744 */
745
746 HAL_StatusTypeDef HAL_FLASHEx_DATAEEPROM_Program(uint32_t TypeProgram, uint32_t Address, uint32_t Data)
747 {
748 HAL_StatusTypeDef status = HAL_ERROR;
749
750 /* Process Locked */
751 __HAL_LOCK(&ProcFlash);
752
753 /* Check the parameters */
754 assert_param(IS_TYPEPROGRAMDATA(TypeProgram));
755
756 /* Wait for last operation to be completed */
757 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
758
759 if(status == HAL_OK)
760 {
761 if(TypeProgram == TYPEPROGRAMDATA_FASTBYTE)
762 {
763 /*Program word (8-bit) at a specified address.*/
764 FLASH_DATAEEPROM_FastProgramByte(Address, (uint8_t) Data);
765 }
766
767 if(TypeProgram == TYPEPROGRAMDATA_FASTHALFWORD)
768 {
769 /*Program word (16-bit) at a specified address.*/
770 FLASH_DATAEEPROM_FastProgramHalfWord(Address, (uint16_t) Data);
771 }
772
773 if(TypeProgram == TYPEPROGRAMDATA_FASTWORD)
774 {
775 /*Program word (32-bit) at a specified address.*/
776 FLASH_DATAEEPROM_FastProgramWord(Address, (uint32_t) Data);
777 }
778
779 if(TypeProgram == TYPEPROGRAMDATA_WORD)
780 {
781 /*Program word (32-bit) at a specified address.*/
782 FLASH_DATAEEPROM_ProgramWord(Address, (uint32_t) Data);
783 }
784
785 if(TypeProgram == TYPEPROGRAMDATA_HALFWORD)
786 {
787 /*Program word (16-bit) at a specified address.*/
788 FLASH_DATAEEPROM_ProgramHalfWord(Address, (uint16_t) Data);
789 }
790
791 if(TypeProgram == TYPEPROGRAMDATA_BYTE)
792 {
793 /*Program word (8-bit) at a specified address.*/
794 FLASH_DATAEEPROM_ProgramByte(Address, (uint8_t) Data);
795 }
796
797 /* Wait for last operation to be completed */
798 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
799 }
800
801 /* Process Unlocked */
802 __HAL_UNLOCK(&ProcFlash);
803
804 return status;
805 }
806
807 /**
808 * @brief Enable DATA EEPROM fixed Time programming (2*Tprog).
809 * @retval None
810 */
811 void HAL_FLASHEx_DATAEEPROM_EnableFixedTimeProgram(void)
812 {
813 SET_BIT(FLASH->PECR, FLASH_PECR_FTDW);
814 }
815
816 /**
817 * @brief Disables DATA EEPROM fixed Time programming (2*Tprog).
818 * @retval None
819 */
820 void HAL_FLASHEx_DATAEEPROM_DisableFixedTimeProgram(void)
821 {
822 CLEAR_BIT(FLASH->PECR, FLASH_PECR_FTDW);
823 }
824
825 /**
826 * @}
827 */
828
829 /**
830 * @}
831 */
832
833 /** @defgroup FLASHEx_Private_Functions FLASHEx Private functions
834 * @{
835 */
836
837 /*
838 ==============================================================================
839 ##### FLASH STATIC functions #####
840 ==============================================================================
841 */
842
843 /*
844 ==============================================================================
845 FLASH
846 ==============================================================================
847 */
848
849 /**
850 * @brief Set the specific FLASH error flag.
851 * @retval None
852 */
853 static void FLASH_SetErrorCode(void)
854 {
855 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) != RESET)
856 {
857 ProcFlash.ErrorCode = FLASH_ERROR_WRP;
858 }
859
860 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR) != RESET)
861 {
862 ProcFlash.ErrorCode |= FLASH_ERROR_PGA;
863 }
864
865 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_SIZERR) != RESET)
866 {
867 ProcFlash.ErrorCode |= FLASH_ERROR_SIZE;
868 }
869
870 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR) != RESET)
871 {
872 ProcFlash.ErrorCode |= FLASH_ERROR_OPTV;
873 }
874
875 #if defined (STM32L151xBA) || defined (STM32L152xBA) || \
876 defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC)
877 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR) != RESET)
878 {
879 ProcFlash.ErrorCode |= FLASH_ERROR_RD;
880 }
881 #endif /* STM32L151xBA || STM32L152xBA || STM32L151xC || STM32L152xC || STM32L162xC */
882
883 #if defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || \
884 defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || \
885 defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)
886 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERRUSR) != RESET)
887 {
888 ProcFlash.ErrorCode |= FLASH_ERROR_OPTVUSR;
889 }
890 #endif /* STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
891 }
892
893 /**
894 * @brief Erases a specified page in program memory.
895 * @param PageAddress: The page address in program memory to be erased.
896 * @note A Page is erased in the Program memory only if the address to load
897 * is the start address of a page (multiple of 256 bytes).
898 * @retval None
899 */
900 static void FLASH_ErasePage(uint32_t PageAddress)
901 {
902 /* Set the ERASE bit */
903 SET_BIT(FLASH->PECR, FLASH_PECR_ERASE);
904
905 /* Set PROG bit */
906 SET_BIT(FLASH->PECR, FLASH_PECR_PROG);
907
908 /* Write 00000000h to the first word of the program page to erase */
909 *(__IO uint32_t *)PageAddress = 0x00000000;
910 }
911
912
913 /*
914 ==============================================================================
915 OPTIONS BYTES
916 ==============================================================================
917 */
918 /**
919 * @brief Enables or disables the read out protection.
920 * @note To correctly run this function, the HAL_FLASH_OB_Unlock() function
921 * must be called before.
922 * @param OB_RDP: specifies the read protection level.
923 * This parameter can be:
924 * @arg OB_RDP_LEVEL0: No protection
925 * @arg OB_RDP_LEVEL1: Read protection of the memory
926 * @arg OB_RDP_LEVEL2: Chip protection
927 *
928 * !!!Warning!!! When enabling OB_RDP_LEVEL2 it's no more possible to go back to level 1 or 0
929 *
930 * @retval HAL status
931 */
932 static HAL_StatusTypeDef FLASH_OB_RDPConfig(uint8_t OB_RDP)
933 {
934 HAL_StatusTypeDef status = HAL_OK;
935 uint32_t tmp1 = 0, tmp2 = 0, sprmod = 0;
936
937 /* Check the parameters */
938 assert_param(IS_OB_RDP(OB_RDP));
939
940 /* According to errata sheet, DocID022054 Rev 5, par2.1.5
941 Before setting Level0 in the RDP register, check that the current level is not equal to Level0.
942 If the current level is not equal to Level0, Level0 can be activated.
943 If the current level is Level0 then the RDP register must not be written again with Level0. */
944 tmp1 = (uint32_t)(OB->RDP & 0x000000FF);
945
946 if ((tmp1 == OB_RDP_LEVEL0) && (OB_RDP == OB_RDP_LEVEL0))
947 {
948 /*current level is Level0 then the RDP register must not be written again with Level0. */
949 status = HAL_ERROR;
950 }
951 else
952 {
953 /* Mask SPRMOD bit */
954 sprmod = (uint32_t)(OB->RDP & 0x00000100);
955
956 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
957
958 /* calculate the option byte to write */
959 tmp1 = (~((uint32_t)(OB_RDP | sprmod)));
960 tmp2 = (uint32_t)(((uint32_t)((uint32_t)(tmp1) << 16)) | ((uint32_t)(OB_RDP | sprmod)));
961
962 if(status == HAL_OK)
963 {
964 /* program read protection level */
965 OB->RDP = tmp2;
966 }
967
968 /* Wait for last operation to be completed */
969 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
970 }
971
972 /* Return the Read protection operation Status */
973 return status;
974 }
975
976 /**
977 * @brief Programs the FLASH brownout reset threshold level Option Byte.
978 * @param OB_BOR: Selects the brownout reset threshold level.
979 * This parameter can be one of the following values:
980 * @arg OB_BOR_OFF: BOR is disabled at power down, the reset is asserted when the VDD
981 * power supply reaches the PDR(Power Down Reset) threshold (1.5V)
982 * @arg OB_BOR_LEVEL1: BOR Reset threshold levels for 1.7V - 1.8V VDD power supply
983 * @arg OB_BOR_LEVEL2: BOR Reset threshold levels for 1.9V - 2.0V VDD power supply
984 * @arg OB_BOR_LEVEL3: BOR Reset threshold levels for 2.3V - 2.4V VDD power supply
985 * @arg OB_BOR_LEVEL4: BOR Reset threshold levels for 2.55V - 2.65V VDD power supply
986 * @arg OB_BOR_LEVEL5: BOR Reset threshold levels for 2.8V - 2.9V VDD power supply
987 * @retval HAL status
988 */
989 static HAL_StatusTypeDef FLASH_OB_BORConfig(uint8_t OB_BOR)
990 {
991 HAL_StatusTypeDef status = HAL_OK;
992 uint32_t tmp = 0, tmp1 = 0;
993
994 /* Check the parameters */
995 assert_param(IS_OB_BOR_LEVEL(OB_BOR));
996
997 /* Get the User Option byte register */
998 tmp1 = (FLASH->OBR & (FLASH_OBR_USER)) >> 16;
999
1000 /* Calculate the option byte to write - [0xFF | nUSER | 0x00 | USER]*/
1001 tmp = (uint32_t)~((OB_BOR | tmp1)) << 16;
1002 tmp |= (OB_BOR | tmp1);
1003
1004 /* Wait for last operation to be completed */
1005 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1006
1007 if(status == HAL_OK)
1008 {
1009 /* Write the BOR Option Byte */
1010 OB->USER = tmp;
1011 }
1012
1013 /* Wait for last operation to be completed */
1014 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1015
1016 /* Return the Option Byte program Status */
1017 return status;
1018 }
1019
1020 /**
1021 * @brief Returns the FLASH User Option Bytes values.
1022 * @retval The FLASH User Option Bytes.
1023 */
1024 static uint8_t FLASH_OB_GetUser(void)
1025 {
1026 /* Return the User Option Byte */
1027 return (uint8_t)((FLASH->OBR & FLASH_OBR_USER) >> POSITION_VAL(FLASH_OBR_USER));
1028 }
1029
1030 /**
1031 * @brief Checks whether the FLASH Read out Protection Status is set or not.
1032 * @retval FLASH ReadOut Protection
1033 */
1034 static uint8_t FLASH_OB_GetRDP(void)
1035 {
1036 return (uint8_t)(FLASH->OBR & FLASH_OBR_RDPRT);
1037 }
1038
1039 /**
1040 * @brief Returns the FLASH BOR level.
1041 * @retval The FLASH User Option Bytes.
1042 */
1043 static uint8_t FLASH_OB_GetBOR(void)
1044 {
1045 /* Return the BOR level */
1046 return (uint8_t)((FLASH->OBR & (uint32_t)FLASH_OBR_BOR_LEV) >> POSITION_VAL(FLASH_OBR_BOR_LEV));
1047 }
1048
1049 /**
1050 * @brief Write protects the desired pages of the first 64KB of the Flash.
1051 * @param pOBInit: pointer to an FLASH_OBInitStruct structure that
1052 * contains WRP parameters.
1053 * @param NewState: new state of the specified FLASH Pages Wtite protection.
1054 * This parameter can be: ENABLE or DISABLE.
1055 * @retval HAL_StatusTypeDef
1056 */
1057 static HAL_StatusTypeDef FLASH_OB_WRPConfig(FLASH_OBProgramInitTypeDef *pOBInit, FunctionalState NewState)
1058 {
1059 HAL_StatusTypeDef status = HAL_OK;
1060
1061 /* Wait for last operation to be completed */
1062 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1063
1064 if(status == HAL_OK)
1065 {
1066 /* WRP for sector between 0 to 31 */
1067 if (pOBInit->WRPSector0To31 != 0)
1068 {
1069 FLASH_OB_WRPConfigWRP1OrPCROP1(pOBInit->WRPSector0To31, NewState);
1070 }
1071
1072 #if defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || \
1073 defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || \
1074 defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)
1075
1076 /* Pages for Cat3, Cat4 & Cat5 devices*/
1077 /* WRP for sector between 32 to 63 */
1078 if (pOBInit->WRPSector32To63 != 0)
1079 {
1080 FLASH_OB_WRPConfigWRP2OrPCROP2(pOBInit->WRPSector32To63, NewState);
1081 }
1082
1083 #endif /* STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
1084
1085 #if defined (STM32L151xD) || defined (STM32L152xD) || defined (STM32L162xD) || \
1086 defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)
1087
1088 /* Pages for devices with FLASH >= 256KB*/
1089 /* WRP for sector between 64 to 95 */
1090 if (pOBInit->WRPSector64To95 != 0)
1091 {
1092 FLASH_OB_WRPConfigWRP3(pOBInit->WRPSector64To95, NewState);
1093 }
1094
1095 #endif /* STM32L151xD || STM32L152xD || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
1096
1097 #if defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)
1098
1099 /* Pages for Cat5 devices*/
1100 /* WRP for sector between 96 to 127 */
1101 if (pOBInit->WRPSector96To127 != 0)
1102 {
1103 FLASH_OB_WRPConfigWRP4(pOBInit->WRPSector96To127, NewState);
1104 }
1105
1106 #endif /* STM32L151xE || STM32L152xE || STM32L162xE */
1107
1108 /* Wait for last operation to be completed */
1109 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1110 }
1111
1112 /* Return the write protection operation Status */
1113 return status;
1114 }
1115
1116 #if defined (STM32L151xBA) || defined (STM32L152xBA) || \
1117 defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC)
1118 /**
1119 * @brief Enables the read/write protection (PCROP) of the desired
1120 * sectors.
1121 * @note This function can be used only for Cat2 & Cat3 devices
1122 * @param pAdvOBInit: pointer to an FLASH_AdvOBProgramInitTypeDef structure that
1123 * contains PCROP parameters.
1124 * @param NewState: new state of the specified FLASH Pages read/Write protection.
1125 * This parameter can be: ENABLE or DISABLE.
1126 * @retval HAL status
1127 */
1128 static HAL_StatusTypeDef FLASH_OB_PCROPConfig(FLASH_AdvOBProgramInitTypeDef *pAdvOBInit, FunctionalState NewState)
1129 {
1130 HAL_StatusTypeDef status = HAL_OK;
1131 FunctionalState pcropstate = DISABLE;
1132
1133 /* Wait for last operation to be completed */
1134 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1135
1136 /* Invert state to use same function of WRP */
1137 if (NewState == DISABLE)
1138 {
1139 pcropstate = ENABLE;
1140 }
1141
1142 if(status == HAL_OK)
1143 {
1144 /* Pages for Cat2 devices*/
1145 /* PCROP for sector between 0 to 31 */
1146 if (pAdvOBInit->PCROPSector0To31 != 0)
1147 {
1148 FLASH_OB_WRPConfigWRP1OrPCROP1(pAdvOBInit->PCROPSector0To31, pcropstate);
1149 }
1150
1151 #if defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC)
1152
1153 /* Pages for Cat3 devices*/
1154 /* WRP for sector between 32 to 63 */
1155 if (pAdvOBInit->PCROPSector32To63 != 0)
1156 {
1157 FLASH_OB_WRPConfigWRP2OrPCROP2(pAdvOBInit->PCROPSector32To63, pcropstate);
1158 }
1159
1160 #endif /* STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC */
1161
1162 /* Wait for last operation to be completed */
1163 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1164 }
1165
1166 /* Return the write protection operation Status */
1167 return status;
1168 }
1169 #endif /* STM32L151xBA || STM32L152xBA || STM32L151xC || STM32L152xC || STM32L162xC */
1170
1171 /**
1172 * @brief Write protects the desired pages of the first 128KB of the Flash.
1173 * @param WRP1OrPCROP1: specifies the address of the pages to be write protected.
1174 * This parameter can be:
1175 * @arg value between OB_WRP1/PCROP1_PAGES0TO15 and OB_WRP1/PCROP1_PAGES496TO511
1176 * @arg OB_WRP1/PCROP1_ALLPAGES
1177 * @param NewState: new state of the specified FLASH Pages Wtite protection.
1178 * This parameter can be: ENABLE or DISABLE.
1179 * @retval None
1180 */
1181 static void FLASH_OB_WRPConfigWRP1OrPCROP1(uint32_t WRP1OrPCROP1, FunctionalState NewState)
1182 {
1183 uint32_t wrp01data = 0, wrp23data = 0;
1184
1185 uint32_t tmp1 = 0, tmp2 = 0;
1186
1187 if (NewState != DISABLE)
1188 {
1189 wrp01data = (uint16_t)(((WRP1OrPCROP1 & WRP_MASK_LOW) | OB->WRP01));
1190 wrp23data = (uint16_t)((((WRP1OrPCROP1 & WRP_MASK_HIGH)>>16 | OB->WRP23)));
1191 tmp1 = (uint32_t)(~(wrp01data) << 16)|(wrp01data);
1192 OB->WRP01 = tmp1;
1193
1194 tmp2 = (uint32_t)(~(wrp23data) << 16)|(wrp23data);
1195 OB->WRP23 = tmp2;
1196 }
1197 else
1198 {
1199 wrp01data = (uint16_t)(~WRP1OrPCROP1 & (WRP_MASK_LOW & OB->WRP01));
1200 wrp23data = (uint16_t)((((~WRP1OrPCROP1 & WRP_MASK_HIGH)>>16 & OB->WRP23)));
1201
1202 tmp1 = (uint32_t)((~wrp01data) << 16)|(wrp01data);
1203 OB->WRP01 = tmp1;
1204
1205 tmp2 = (uint32_t)((~wrp23data) << 16)|(wrp23data);
1206 OB->WRP23 = tmp2;
1207 }
1208 }
1209
1210 #if defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || \
1211 defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || \
1212 defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)
1213 /**
1214 * @brief Enable Write protects the desired pages of the second 128KB of the Flash.
1215 * @note This function can be used only for Cat3, Cat4 & Cat5 devices.
1216 * @param WRP2OrPCROP2: specifies the address of the pages to be write protected.
1217 * This parameter can be:
1218 * @arg value between OB_WRP2/PCROP2_PAGES512TO527 and OB_WRP2/PCROP2_PAGES1008TO1023
1219 * @arg OB_WRP2/PCROP2_ALLPAGES
1220 * @param NewState: new state of the specified FLASH Pages Wtite protection.
1221 * This parameter can be: ENABLE or DISABLE.
1222 * @retval None
1223 */
1224 static void FLASH_OB_WRPConfigWRP2OrPCROP2(uint32_t WRP2OrPCROP2, FunctionalState NewState)
1225 {
1226 uint32_t wrp45data = 0, wrp67data = 0;
1227
1228 uint32_t tmp1 = 0, tmp2 = 0;
1229
1230 if (NewState != DISABLE)
1231 {
1232 wrp45data = (uint16_t)(((WRP2OrPCROP2 & WRP_MASK_LOW) | OB->WRP45));
1233 wrp67data = (uint16_t)((((WRP2OrPCROP2 & WRP_MASK_HIGH)>>16 | OB->WRP67)));
1234 tmp1 = (uint32_t)(~(wrp45data) << 16)|(wrp45data);
1235 OB->WRP45 = tmp1;
1236
1237 tmp2 = (uint32_t)(~(wrp67data) << 16)|(wrp67data);
1238 OB->WRP67 = tmp2;
1239 }
1240 else
1241 {
1242 wrp45data = (uint16_t)(~WRP2OrPCROP2 & (WRP_MASK_LOW & OB->WRP45));
1243 wrp67data = (uint16_t)((((~WRP2OrPCROP2 & WRP_MASK_HIGH)>>16 & OB->WRP67)));
1244
1245 tmp1 = (uint32_t)((~wrp45data) << 16)|(wrp45data);
1246 OB->WRP45 = tmp1;
1247
1248 tmp2 = (uint32_t)((~wrp67data) << 16)|(wrp67data);
1249 OB->WRP67 = tmp2;
1250 }
1251 }
1252 #endif /* STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
1253
1254 #if defined (STM32L151xD) || defined (STM32L152xD) || defined (STM32L162xD) || \
1255 defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)
1256 /**
1257 * @brief Enable Write protects the desired pages of the third 128KB of the Flash.
1258 * @note This function can be used only for STM32L151xD, STM32L152xD, STM32L162xD & Cat5 devices.
1259 * @param WRP3: specifies the address of the pages to be write protected.
1260 * This parameter can be:
1261 * @arg value between WRP3_PAGES1024TO1039 and OB_WRP3_PAGES1520TO1535
1262 * @arg OB_WRP3_ALLPAGES
1263 * @param NewState: new state of the specified FLASH Pages Wtite protection.
1264 * This parameter can be: ENABLE or DISABLE.
1265 * @retval None
1266 */
1267 static void FLASH_OB_WRPConfigWRP3(uint32_t WRP3, FunctionalState NewState)
1268 {
1269 uint32_t wrp89data = 0, wrp1011data = 0;
1270
1271 uint32_t tmp1 = 0, tmp2 = 0;
1272
1273 if (NewState != DISABLE)
1274 {
1275 wrp89data = (uint16_t)(((WRP3 & WRP_MASK_LOW) | OB->WRP89));
1276 wrp1011data = (uint16_t)((((WRP3 & WRP_MASK_HIGH)>>16 | OB->WRP1011)));
1277 tmp1 = (uint32_t)(~(wrp89data) << 16)|(wrp89data);
1278 OB->WRP89 = tmp1;
1279
1280 tmp2 = (uint32_t)(~(wrp1011data) << 16)|(wrp1011data);
1281 OB->WRP1011 = tmp2;
1282 }
1283 else
1284 {
1285 wrp89data = (uint16_t)(~WRP3 & (WRP_MASK_LOW & OB->WRP89));
1286 wrp1011data = (uint16_t)((((~WRP3 & WRP_MASK_HIGH)>>16 & OB->WRP1011)));
1287
1288 tmp1 = (uint32_t)((~wrp89data) << 16)|(wrp89data);
1289 OB->WRP89 = tmp1;
1290
1291 tmp2 = (uint32_t)((~wrp1011data) << 16)|(wrp1011data);
1292 OB->WRP1011 = tmp2;
1293 }
1294 }
1295 #endif /* STM32L151xD || STM32L152xD || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
1296
1297 #if defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)
1298 /**
1299 * @brief Enable Write protects the desired pages of the Fourth 128KB of the Flash.
1300 * @note This function can be used only for Cat5 devices.
1301 * @param WRP4: specifies the address of the pages to be write protected.
1302 * This parameter can be:
1303 * @arg value between OB_WRP4_PAGES1536TO1551 and OB_WRP4_PAGES2032TO2047
1304 * @arg OB_WRP4_ALLPAGES
1305 * @param NewState: new state of the specified FLASH Pages Wtite protection.
1306 * This parameter can be: ENABLE or DISABLE.
1307 * @retval None
1308 */
1309 static void FLASH_OB_WRPConfigWRP4(uint32_t WRP4, FunctionalState NewState)
1310 {
1311 uint32_t wrp1213data = 0, wrp1415data = 0;
1312
1313 uint32_t tmp1 = 0, tmp2 = 0;
1314
1315 if (NewState != DISABLE)
1316 {
1317 wrp1213data = (uint16_t)(((WRP4 & WRP_MASK_LOW) | OB->WRP1213));
1318 wrp1415data = (uint16_t)((((WRP4 & WRP_MASK_HIGH)>>16 | OB->WRP1415)));
1319 tmp1 = (uint32_t)(~(wrp1213data) << 16)|(wrp1213data);
1320 OB->WRP1213 = tmp1;
1321
1322 tmp2 = (uint32_t)(~(wrp1415data) << 16)|(wrp1415data);
1323 OB->WRP1415 = tmp2;
1324 }
1325 else
1326 {
1327 wrp1213data = (uint16_t)(~WRP4 & (WRP_MASK_LOW & OB->WRP1213));
1328 wrp1415data = (uint16_t)((((~WRP4 & WRP_MASK_HIGH)>>16 & OB->WRP1415)));
1329
1330 tmp1 = (uint32_t)((~wrp1213data) << 16)|(wrp1213data);
1331 OB->WRP1213 = tmp1;
1332
1333 tmp2 = (uint32_t)((~wrp1415data) << 16)|(wrp1415data);
1334 OB->WRP1415 = tmp2;
1335 }
1336 }
1337 #endif /* STM32L151xE || STM32L152xE || STM32L162xE */
1338
1339 /**
1340 * @brief Programs the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY.
1341 * @param OB_IWDG: Selects the WDG mode.
1342 * This parameter can be one of the following values:
1343 * @arg OB_IWDG_SW: Software WDG selected
1344 * @arg OB_IWDG_HW: Hardware WDG selected
1345 * @param OB_STOP: Reset event when entering STOP mode.
1346 * This parameter can be one of the following values:
1347 * @arg OB_STOP_NORST: No reset generated when entering in STOP
1348 * @arg OB_STOP_RST: Reset generated when entering in STOP
1349 * @param OB_STDBY: Reset event when entering Standby mode.
1350 * This parameter can be one of the following values:
1351 * @arg OB_STDBY_NORST: No reset generated when entering in STANDBY
1352 * @arg OB_STDBY_RST: Reset generated when entering in STANDBY
1353 * @retval HAL status
1354 */
1355 static HAL_StatusTypeDef FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY)
1356 {
1357 HAL_StatusTypeDef status = HAL_OK;
1358 uint32_t tmp = 0, tmp1 = 0;
1359
1360 /* Check the parameters */
1361 assert_param(IS_OB_IWDG_SOURCE(OB_IWDG));
1362 assert_param(IS_OB_STOP_SOURCE(OB_STOP));
1363 assert_param(IS_OB_STDBY_SOURCE(OB_STDBY));
1364
1365 /* Get the User Option byte register */
1366 tmp1 = (FLASH->OBR & FLASH_OBR_BOR_LEV) >> 16;
1367
1368 /* Calculate the user option byte to write */
1369 tmp = (uint32_t)(((uint32_t)~((uint32_t)((uint32_t)(OB_IWDG) | (uint32_t)(OB_STOP) | (uint32_t)(OB_STDBY) | tmp1))) << 16);
1370 tmp |= ((uint32_t)(OB_IWDG) | ((uint32_t)OB_STOP) | (uint32_t)(OB_STDBY) | tmp1);
1371
1372 /* Wait for last operation to be completed */
1373 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1374
1375 if(status == HAL_OK)
1376 {
1377 /* Write the User Option Byte */
1378 OB->USER = tmp;
1379 }
1380
1381 /* Wait for last operation to be completed */
1382 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1383
1384 /* Return the Option Byte program Status */
1385 return status;
1386 }
1387
1388 #if defined (STM32L151xD) || defined (STM32L152xD) || defined (STM32L162xD) || \
1389 defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)
1390 /**
1391 * @brief Configures to boot from Bank1 or Bank2.
1392 * @param OB_BOOT: select the FLASH Bank to boot from.
1393 * This parameter can be one of the following values:
1394 * @arg OB_BOOT_BANK2: At startup, if boot pins are set in boot from user Flash
1395 * position and this parameter is selected the device will boot from Bank2 or Bank1,
1396 * depending on the activation of the bank. The active banks are checked in
1397 * the following order: Bank2, followed by Bank1.
1398 * The active bank is recognized by the value programmed at the base address
1399 * of the respective bank (corresponding to the initial stack pointer value
1400 * in the interrupt vector table).
1401 * @arg OB_BOOT_BANK1: At startup, if boot pins are set in boot from user Flash
1402 * position and this parameter is selected the device will boot from Bank1(Default).
1403 * For more information, please refer to AN2606 from www.st.com.
1404 * @retval HAL status
1405 */
1406 static HAL_StatusTypeDef FLASH_OB_BootConfig(uint8_t OB_BOOT)
1407 {
1408 HAL_StatusTypeDef status = HAL_OK;
1409 uint32_t tmp = 0, tmp1 = 0;
1410
1411 /* Check the parameters */
1412 assert_param(IS_OB_BOOT_BANK(OB_BOOT));
1413
1414 /* Get the User Option byte register and BOR Level*/
1415 tmp1 = (FLASH->OBR & (FLASH_OBR_nRST_STDBY | FLASH_OBR_nRST_STOP | FLASH_OBR_IWDG_SW | FLASH_OBR_BOR_LEV)) >> 16;
1416
1417 /* Calculate the option byte to write */
1418 tmp = (uint32_t)~(OB_BOOT | tmp1) << 16;
1419 tmp |= (OB_BOOT | tmp1);
1420
1421 /* Wait for last operation to be completed */
1422 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1423
1424 if(status == HAL_OK)
1425 {
1426 /* Write the BOOT Option Byte */
1427 OB->USER = tmp;
1428 }
1429
1430 /* Wait for last operation to be completed */
1431 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1432
1433 /* Return the Option Byte program Status */
1434 return status;
1435 }
1436
1437 #endif /* STM32L151xD || STM32L152xD || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
1438
1439 /*
1440 ==============================================================================
1441 DATA
1442 ==============================================================================
1443 */
1444
1445 /**
1446 * @brief Write a Byte at a specified address in data memory.
1447 * @param Address: specifies the address to be written.
1448 * @param Data: specifies the data to be written.
1449 * @note This function assumes that the is data word is already erased.
1450 * @retval HAL status
1451 */
1452 static HAL_StatusTypeDef FLASH_DATAEEPROM_FastProgramByte(uint32_t Address, uint8_t Data)
1453 {
1454 HAL_StatusTypeDef status = HAL_OK;
1455 #if defined(STM32L100xB) || defined (STM32L151xB) || defined (STM32L152xB)
1456 uint32_t tmp = 0, tmpaddr = 0;
1457 #endif /* STM32L100xB || STM32L151xB || STM32L152xB */
1458
1459 /* Check the parameters */
1460 assert_param(IS_FLASH_DATA_ADDRESS(Address));
1461
1462 /* Wait for last operation to be completed */
1463 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1464
1465 if(status == HAL_OK)
1466 {
1467 /* Clear the FTDW bit */
1468 CLEAR_BIT(FLASH->PECR, FLASH_PECR_FTDW);
1469
1470 #if defined(STM32L100xB) || defined (STM32L151xB) || defined (STM32L152xB)
1471 /* Possible only on Cat1 devices */
1472 if(Data != (uint8_t)0x00)
1473 {
1474 /* If the previous operation is completed, proceed to write the new Data */
1475 *(__IO uint8_t *)Address = Data;
1476
1477 /* Wait for last operation to be completed */
1478 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1479 }
1480 else
1481 {
1482 tmpaddr = Address & 0xFFFFFFFC;
1483 tmp = * (__IO uint32_t *) tmpaddr;
1484 tmpaddr = 0xFF << ((uint32_t) (0x8 * (Address & 0x3)));
1485 tmp &= ~tmpaddr;
1486 status = HAL_FLASHEx_DATAEEPROM_Erase(TYPEERASEDATA_WORD, Address & 0xFFFFFFFC);
1487 status = HAL_FLASHEx_DATAEEPROM_Program(TYPEPROGRAMDATA_FASTWORD, (Address & 0xFFFFFFFC), tmp);
1488 }
1489 #else /*!Cat1*/
1490 /* If the previous operation is completed, proceed to write the new Data */
1491 *(__IO uint8_t *)Address = Data;
1492
1493 /* Wait for last operation to be completed */
1494 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1495 #endif /* STM32L100xB || STM32L151xB || STM32L152xB */
1496 }
1497 /* Return the Write Status */
1498 return status;
1499 }
1500
1501 /**
1502 * @brief Writes a half word at a specified address in data memory.
1503 * @param Address: specifies the address to be written.
1504 * @param Data: specifies the data to be written.
1505 * @note This function assumes that the is data word is already erased.
1506 * @retval HAL status
1507 */
1508 static HAL_StatusTypeDef FLASH_DATAEEPROM_FastProgramHalfWord(uint32_t Address, uint16_t Data)
1509 {
1510 HAL_StatusTypeDef status = HAL_OK;
1511 #if defined(STM32L100xB) || defined (STM32L151xB) || defined (STM32L152xB)
1512 uint32_t tmp = 0, tmpaddr = 0;
1513 #endif /* STM32L100xB || STM32L151xB || STM32L152xB */
1514
1515 /* Check the parameters */
1516 assert_param(IS_FLASH_DATA_ADDRESS(Address));
1517
1518 /* Wait for last operation to be completed */
1519 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1520
1521 if(status == HAL_OK)
1522 {
1523 /* Clear the FTDW bit */
1524 CLEAR_BIT(FLASH->PECR, FLASH_PECR_FTDW);
1525
1526 #if defined(STM32L100xB) || defined (STM32L151xB) || defined (STM32L152xB)
1527 /* Possible only on Cat1 devices */
1528 if(Data != (uint16_t)0x0000)
1529 {
1530 /* If the previous operation is completed, proceed to write the new data */
1531 *(__IO uint16_t *)Address = Data;
1532
1533 /* Wait for last operation to be completed */
1534 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1535 }
1536 else
1537 {
1538 if((Address & 0x3) != 0x3)
1539 {
1540 tmpaddr = Address & 0xFFFFFFFC;
1541 tmp = * (__IO uint32_t *) tmpaddr;
1542 tmpaddr = 0xFFFF << ((uint32_t) (0x8 * (Address & 0x3)));
1543 tmp &= ~tmpaddr;
1544 status = HAL_FLASHEx_DATAEEPROM_Erase(TYPEERASEDATA_WORD, Address & 0xFFFFFFFC);
1545 status = HAL_FLASHEx_DATAEEPROM_Program(TYPEPROGRAMDATA_FASTWORD, (Address & 0xFFFFFFFC), tmp);
1546 }
1547 else
1548 {
1549 HAL_FLASHEx_DATAEEPROM_Program(TYPEPROGRAMDATA_FASTBYTE, Address, 0x00);
1550 HAL_FLASHEx_DATAEEPROM_Program(TYPEPROGRAMDATA_FASTBYTE, Address + 1, 0x00);
1551 }
1552 }
1553 #else /* !Cat1 */
1554 /* If the previous operation is completed, proceed to write the new data */
1555 *(__IO uint16_t *)Address = Data;
1556
1557 /* Wait for last operation to be completed */
1558 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1559 #endif /* STM32L100xB || STM32L151xB || STM32L152xB */
1560 }
1561 /* Return the Write Status */
1562 return status;
1563 }
1564
1565 /**
1566 * @brief Programs a word at a specified address in data memory.
1567 * @param Address: specifies the address to be written.
1568 * @param Data: specifies the data to be written.
1569 * @note This function assumes that the is data word is already erased.
1570 * @retval HAL status
1571 */
1572 static HAL_StatusTypeDef FLASH_DATAEEPROM_FastProgramWord(uint32_t Address, uint32_t Data)
1573 {
1574 HAL_StatusTypeDef status = HAL_OK;
1575
1576 /* Check the parameters */
1577 assert_param(IS_FLASH_DATA_ADDRESS(Address));
1578
1579 /* Wait for last operation to be completed */
1580 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1581
1582 if(status == HAL_OK)
1583 {
1584 /* Clear the FTDW bit */
1585 CLEAR_BIT(FLASH->PECR, FLASH_PECR_FTDW);
1586
1587 /* If the previous operation is completed, proceed to program the new data */
1588 *(__IO uint32_t *)Address = Data;
1589
1590 /* Wait for last operation to be completed */
1591 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1592 }
1593 /* Return the Write Status */
1594 return status;
1595 }
1596
1597 /**
1598 * @brief Write a Byte at a specified address in data memory without erase.
1599 * @param Address: specifies the address to be written.
1600 * @param Data: specifies the data to be written.
1601 * @retval HAL status
1602 */
1603 static HAL_StatusTypeDef FLASH_DATAEEPROM_ProgramByte(uint32_t Address, uint8_t Data)
1604 {
1605 HAL_StatusTypeDef status = HAL_OK;
1606 #if defined(STM32L100xB) || defined (STM32L151xB) || defined (STM32L152xB)
1607 uint32_t tmp = 0, tmpaddr = 0;
1608 #endif /* STM32L100xB || STM32L151xB || STM32L152xB */
1609
1610 /* Check the parameters */
1611 assert_param(IS_FLASH_DATA_ADDRESS(Address));
1612
1613 /* Wait for last operation to be completed */
1614 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1615
1616 if(status == HAL_OK)
1617 {
1618 #if defined(STM32L100xB) || defined (STM32L151xB) || defined (STM32L152xB)
1619 if(Data != (uint8_t) 0x00)
1620 {
1621 *(__IO uint8_t *)Address = Data;
1622
1623 /* Wait for last operation to be completed */
1624 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1625
1626 }
1627 else
1628 {
1629 tmpaddr = Address & 0xFFFFFFFC;
1630 tmp = * (__IO uint32_t *) tmpaddr;
1631 tmpaddr = 0xFF << ((uint32_t) (0x8 * (Address & 0x3)));
1632 tmp &= ~tmpaddr;
1633 status = HAL_FLASHEx_DATAEEPROM_Erase(TYPEERASEDATA_WORD, Address & 0xFFFFFFFC);
1634 status = HAL_FLASHEx_DATAEEPROM_Program(TYPEPROGRAMDATA_FASTWORD, (Address & 0xFFFFFFFC), tmp);
1635 }
1636 #else /* Not Cat1*/
1637 *(__IO uint8_t *)Address = Data;
1638
1639 /* Wait for last operation to be completed */
1640 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1641 #endif /* STM32L100xB || STM32L151xB || STM32L152xB */
1642 }
1643 /* Return the Write Status */
1644 return status;
1645 }
1646
1647 /**
1648 * @brief Writes a half word at a specified address in data memory without erase.
1649 * @param Address: specifies the address to be written.
1650 * @param Data: specifies the data to be written.
1651 * @retval HAL status
1652 */
1653 static HAL_StatusTypeDef FLASH_DATAEEPROM_ProgramHalfWord(uint32_t Address, uint16_t Data)
1654 {
1655 HAL_StatusTypeDef status = HAL_OK;
1656 #if defined(STM32L100xB) || defined (STM32L151xB) || defined (STM32L152xB)
1657 uint32_t tmp = 0, tmpaddr = 0;
1658 #endif /* STM32L100xB || STM32L151xB || STM32L152xB */
1659
1660 /* Check the parameters */
1661 assert_param(IS_FLASH_DATA_ADDRESS(Address));
1662
1663 /* Wait for last operation to be completed */
1664 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1665
1666 if(status == HAL_OK)
1667 {
1668 #if defined(STM32L100xB) || defined (STM32L151xB) || defined (STM32L152xB)
1669 if(Data != (uint16_t)0x0000)
1670 {
1671 *(__IO uint16_t *)Address = Data;
1672
1673 /* Wait for last operation to be completed */
1674 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1675 }
1676 else
1677 {
1678 if((Address & 0x3) != 0x3)
1679 {
1680 tmpaddr = Address & 0xFFFFFFFC;
1681 tmp = * (__IO uint32_t *) tmpaddr;
1682 tmpaddr = 0xFFFF << ((uint32_t) (0x8 * (Address & 0x3)));
1683 tmp &= ~tmpaddr;
1684 status = HAL_FLASHEx_DATAEEPROM_Erase(TYPEERASEDATA_WORD, Address & 0xFFFFFFFC);
1685 status = HAL_FLASHEx_DATAEEPROM_Program(TYPEPROGRAMDATA_FASTWORD, (Address & 0xFFFFFFFC), tmp);
1686 }
1687 else
1688 {
1689 HAL_FLASHEx_DATAEEPROM_Program(TYPEPROGRAMDATA_FASTBYTE, Address, 0x00);
1690 HAL_FLASHEx_DATAEEPROM_Program(TYPEPROGRAMDATA_FASTBYTE, Address + 1, 0x00);
1691 }
1692 }
1693 #else /* Not Cat1*/
1694 *(__IO uint16_t *)Address = Data;
1695
1696 /* Wait for last operation to be completed */
1697 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1698 #endif /* STM32L100xB || STM32L151xB || STM32L152xB */
1699 }
1700 /* Return the Write Status */
1701 return status;
1702 }
1703
1704 /**
1705 * @brief Programs a word at a specified address in data memory without erase.
1706 * @param Address: specifies the address to be written.
1707 * @param Data: specifies the data to be written.
1708 * @retval HAL status
1709 */
1710 static HAL_StatusTypeDef FLASH_DATAEEPROM_ProgramWord(uint32_t Address, uint32_t Data)
1711 {
1712 HAL_StatusTypeDef status = HAL_OK;
1713
1714 /* Check the parameters */
1715 assert_param(IS_FLASH_DATA_ADDRESS(Address));
1716
1717 /* Wait for last operation to be completed */
1718 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1719
1720 if(status == HAL_OK)
1721 {
1722 *(__IO uint32_t *)Address = Data;
1723
1724 /* Wait for last operation to be completed */
1725 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1726 }
1727 /* Return the Write Status */
1728 return status;
1729 }
1730
1731 /**
1732 * @}
1733 */
1734
1735 /**
1736 * @}
1737 */
1738
1739 /** @addtogroup FLASH
1740 * @{
1741 */
1742
1743 /** @addtogroup FLASH_Exported_Functions
1744 * @{
1745 */
1746
1747 /** @addtogroup FLASH_Exported_Functions_Group1
1748 * @brief Interrupts functions
1749 *
1750 @verbatim
1751 ==============================================================================
1752 ##### Interrupts functions #####
1753 ==============================================================================
1754
1755 @endverbatim
1756 * @{
1757 */
1758
1759 /**
1760 * @brief This function handles FLASH interrupt request.
1761 * @retval None
1762 */
1763 void HAL_FLASH_IRQHandler(void)
1764 {
1765 uint32_t temp;
1766
1767 /* If the program operation is completed, disable the PROG Bit */
1768 CLEAR_BIT(FLASH->PECR, FLASH_PECR_PROG);
1769
1770 /* If the erase operation is completed, disable the ERASE Bit */
1771 CLEAR_BIT(FLASH->PECR, FLASH_PECR_ERASE);
1772
1773 /* Check FLASH End of Operation flag */
1774 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP) != RESET)
1775 {
1776 if(ProcFlash.ProcedureOnGoing == FLASH_PROC_PAGEERASE)
1777 {
1778 /*Nb of sector to erased can be decreased*/
1779 ProcFlash.NbPagesToErase--;
1780
1781 /* Check if there are still sectors to erase*/
1782 if(ProcFlash.NbPagesToErase != 0)
1783 {
1784 temp = ProcFlash.Page;
1785 /*Indicate user which sector has been erased*/
1786 HAL_FLASH_EndOfOperationCallback(temp);
1787
1788 /* Clear pending flags (if any) */
1789 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_MASK);
1790
1791 /*Increment sector number*/
1792 temp = ProcFlash.Page + FLASH_PAGE_SIZE;
1793 ProcFlash.Page = ProcFlash.Page + FLASH_PAGE_SIZE;
1794 FLASH_ErasePage(temp);
1795 }
1796 else
1797 {
1798 /*No more sectors to Erase, user callback can be called.*/
1799 /*Reset Sector and stop Erase sectors procedure*/
1800 ProcFlash.Page = temp = 0xFFFFFFFF;
1801 ProcFlash.ProcedureOnGoing = FLASH_PROC_NONE;
1802 /* FLASH EOP interrupt user callback */
1803 HAL_FLASH_EndOfOperationCallback(temp);
1804 /* Clear FLASH End of Operation pending bit */
1805 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
1806 }
1807 }
1808 else
1809 {
1810 if(ProcFlash.ProcedureOnGoing == FLASH_PROC_PROGRAM)
1811 {
1812 /*Program ended. Return the selected address*/
1813 /* FLASH EOP interrupt user callback */
1814 HAL_FLASH_EndOfOperationCallback(ProcFlash.Address);
1815 }
1816 ProcFlash.ProcedureOnGoing = FLASH_PROC_NONE;
1817 /* Clear FLASH End of Operation pending bit */
1818 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
1819 }
1820
1821 }
1822
1823 /* Check FLASH operation error flags */
1824 if( (__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) != RESET) ||
1825 (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR) != RESET) ||
1826 (__HAL_FLASH_GET_FLAG(FLASH_FLAG_SIZERR) != RESET) ||
1827 #if defined (STM32L151xBA) || defined (STM32L152xBA) || \
1828 defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC)
1829 (__HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR) != RESET) ||
1830 #endif /* STM32L151xBA || STM32L152xBA || STM32L151xC || STM32L152xC || STM32L162xC */
1831 #if defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || \
1832 defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || \
1833 defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)
1834 (__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERRUSR) != RESET) ||
1835 #endif /* STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
1836 (__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR) != RESET) )
1837 {
1838 if(ProcFlash.ProcedureOnGoing == FLASH_PROC_PAGEERASE)
1839 {
1840 /*return the faulty sector*/
1841 temp = ProcFlash.Page;
1842 ProcFlash.Page = 0xFFFFFFFF;
1843 }
1844 else
1845 {
1846 /*retrun the faulty address*/
1847 temp = ProcFlash.Address;
1848 }
1849
1850 /*Save the Error code*/
1851 FLASH_SetErrorCode();
1852
1853 /* FLASH error interrupt user callback */
1854 HAL_FLASH_OperationErrorCallback(temp);
1855
1856 /* Clear FLASH error pending bits */
1857 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_MASK);
1858
1859 /*Stop the procedure ongoing*/
1860 ProcFlash.ProcedureOnGoing = FLASH_PROC_NONE;
1861 }
1862
1863 if(ProcFlash.ProcedureOnGoing == FLASH_PROC_NONE)
1864 {
1865 /* Disable End of FLASH Operation interrupt */
1866 __HAL_FLASH_DISABLE_IT(FLASH_IT_EOP);
1867
1868 /* Disable Error source interrupt */
1869 __HAL_FLASH_DISABLE_IT(FLASH_IT_ERR);
1870
1871 /* Process Unlocked */
1872 __HAL_UNLOCK(&ProcFlash);
1873 }
1874
1875 }
1876
1877 /**
1878 * @}
1879 */
1880
1881 /**
1882 * @}
1883 */
1884
1885 /** @defgroup FLASH_Internal_Functions FLASH Internal function
1886 * @{
1887 */
1888
1889 /**
1890 * @brief Wait for a FLASH operation to complete.
1891 * @param Timeout: maximum flash operationtimeout
1892 * @retval HAL status
1893 */
1894 HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)
1895 {
1896 /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
1897 Even if the FLASH operation fails, the BUSY flag will be reset and an error
1898 flag will be set */
1899
1900 uint32_t tickstart = HAL_GetTick();
1901
1902 while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY) != RESET)
1903 {
1904 if(Timeout != HAL_MAX_DELAY)
1905 {
1906 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
1907 {
1908 return HAL_TIMEOUT;
1909 }
1910 }
1911 }
1912
1913 if( (__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) != RESET) ||
1914 (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR) != RESET) ||
1915 (__HAL_FLASH_GET_FLAG(FLASH_FLAG_SIZERR) != RESET) ||
1916 #if defined (STM32L151xBA) || defined (STM32L152xBA) || \
1917 defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC)
1918 (__HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR) != RESET) ||
1919 #endif /* STM32L151xBA || STM32L152xBA || STM32L151xC || STM32L152xC || STM32L162xC */
1920 #if defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || \
1921 defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || \
1922 defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)
1923 (__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERRUSR) != RESET) ||
1924 #endif /* STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
1925 (__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR) != RESET) )
1926 {
1927 /*Save the error code*/
1928 FLASH_SetErrorCode();
1929 return HAL_ERROR;
1930 }
1931
1932 /* There is no error flag set */
1933 return HAL_OK;
1934 }
1935
1936
1937 /**
1938 * @}
1939 */
1940
1941 #endif /* HAL_FLASH_MODULE_ENABLED */
1942
1943 /**
1944 * @}
1945 */
1946
1947 /**
1948 * @}
1949 */
1950
1951 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
1952
Imprint / Impressum