Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | mjames | 1 | /** |
2 | ****************************************************************************** |
||
3 | * @file stm32f1xx_hal_flash_ex.c |
||
4 | * @author MCD Application Team |
||
5 | * @brief Extended FLASH HAL module driver. |
||
6 | * |
||
7 | * This file provides firmware functions to manage the following |
||
8 | * functionalities of the FLASH peripheral: |
||
9 | * + Extended Initialization/de-initialization functions |
||
10 | * + Extended I/O operation functions |
||
11 | * + Extended Peripheral Control functions |
||
12 | * |
||
13 | @verbatim |
||
14 | ============================================================================== |
||
15 | ##### Flash peripheral extended features ##### |
||
16 | ============================================================================== |
||
17 | |||
18 | ##### How to use this driver ##### |
||
19 | ============================================================================== |
||
20 | [..] This driver provides functions to configure and program the FLASH memory |
||
21 | of all STM32F1xxx devices. It includes |
||
22 | |||
23 | (++) Set/Reset the write protection |
||
24 | (++) Program the user Option Bytes |
||
25 | (++) Get the Read protection Level |
||
26 | |||
27 | @endverbatim |
||
28 | ****************************************************************************** |
||
29 | * @attention |
||
30 | * |
||
31 | * <h2><center>© Copyright (c) 2016 STMicroelectronics. |
||
32 | * All rights reserved.</center></h2> |
||
33 | * |
||
34 | * This software component is licensed by ST under BSD 3-Clause license, |
||
35 | * the "License"; You may not use this file except in compliance with the |
||
36 | * License. You may obtain a copy of the License at: |
||
37 | * opensource.org/licenses/BSD-3-Clause |
||
38 | * |
||
39 | ****************************************************************************** |
||
40 | */ |
||
41 | |||
42 | /* Includes ------------------------------------------------------------------*/ |
||
43 | #include "stm32f1xx_hal.h" |
||
44 | |||
45 | /** @addtogroup STM32F1xx_HAL_Driver |
||
46 | * @{ |
||
47 | */ |
||
48 | #ifdef HAL_FLASH_MODULE_ENABLED |
||
49 | |||
50 | /** @addtogroup FLASH |
||
51 | * @{ |
||
52 | */ |
||
53 | /** @addtogroup FLASH_Private_Variables |
||
54 | * @{ |
||
55 | */ |
||
56 | /* Variables used for Erase pages under interruption*/ |
||
57 | extern FLASH_ProcessTypeDef pFlash; |
||
58 | /** |
||
59 | * @} |
||
60 | */ |
||
61 | |||
62 | /** |
||
63 | * @} |
||
64 | */ |
||
65 | |||
66 | /** @defgroup FLASHEx FLASHEx |
||
67 | * @brief FLASH HAL Extension module driver |
||
68 | * @{ |
||
69 | */ |
||
70 | |||
71 | /* Private typedef -----------------------------------------------------------*/ |
||
72 | /* Private define ------------------------------------------------------------*/ |
||
73 | /** @defgroup FLASHEx_Private_Constants FLASHEx Private Constants |
||
74 | * @{ |
||
75 | */ |
||
76 | #define FLASH_POSITION_IWDGSW_BIT FLASH_OBR_IWDG_SW_Pos |
||
77 | #define FLASH_POSITION_OB_USERDATA0_BIT FLASH_OBR_DATA0_Pos |
||
78 | #define FLASH_POSITION_OB_USERDATA1_BIT FLASH_OBR_DATA1_Pos |
||
79 | /** |
||
80 | * @} |
||
81 | */ |
||
82 | |||
83 | /* Private macro -------------------------------------------------------------*/ |
||
84 | /** @defgroup FLASHEx_Private_Macros FLASHEx Private Macros |
||
85 | * @{ |
||
86 | */ |
||
87 | /** |
||
88 | * @} |
||
89 | */ |
||
90 | |||
91 | /* Private variables ---------------------------------------------------------*/ |
||
92 | /* Private function prototypes -----------------------------------------------*/ |
||
93 | /** @defgroup FLASHEx_Private_Functions FLASHEx Private Functions |
||
94 | * @{ |
||
95 | */ |
||
96 | /* Erase operations */ |
||
97 | static void FLASH_MassErase(uint32_t Banks); |
||
98 | void FLASH_PageErase(uint32_t PageAddress); |
||
99 | |||
100 | /* Option bytes control */ |
||
101 | static HAL_StatusTypeDef FLASH_OB_EnableWRP(uint32_t WriteProtectPage); |
||
102 | static HAL_StatusTypeDef FLASH_OB_DisableWRP(uint32_t WriteProtectPage); |
||
103 | static HAL_StatusTypeDef FLASH_OB_RDP_LevelConfig(uint8_t ReadProtectLevel); |
||
104 | static HAL_StatusTypeDef FLASH_OB_UserConfig(uint8_t UserConfig); |
||
105 | static HAL_StatusTypeDef FLASH_OB_ProgramData(uint32_t Address, uint8_t Data); |
||
106 | static uint32_t FLASH_OB_GetWRP(void); |
||
107 | static uint32_t FLASH_OB_GetRDP(void); |
||
108 | static uint8_t FLASH_OB_GetUser(void); |
||
109 | |||
110 | /** |
||
111 | * @} |
||
112 | */ |
||
113 | |||
114 | /* Exported functions ---------------------------------------------------------*/ |
||
115 | /** @defgroup FLASHEx_Exported_Functions FLASHEx Exported Functions |
||
116 | * @{ |
||
117 | */ |
||
118 | |||
119 | /** @defgroup FLASHEx_Exported_Functions_Group1 FLASHEx Memory Erasing functions |
||
120 | * @brief FLASH Memory Erasing functions |
||
121 | * |
||
122 | @verbatim |
||
123 | ============================================================================== |
||
124 | ##### FLASH Erasing Programming functions ##### |
||
125 | ============================================================================== |
||
126 | |||
127 | [..] The FLASH Memory Erasing functions, includes the following functions: |
||
128 | (+) @ref HAL_FLASHEx_Erase: return only when erase has been done |
||
129 | (+) @ref HAL_FLASHEx_Erase_IT: end of erase is done when @ref HAL_FLASH_EndOfOperationCallback |
||
130 | is called with parameter 0xFFFFFFFF |
||
131 | |||
132 | [..] Any operation of erase should follow these steps: |
||
133 | (#) Call the @ref HAL_FLASH_Unlock() function to enable the flash control register and |
||
134 | program memory access. |
||
135 | (#) Call the desired function to erase page. |
||
136 | (#) Call the @ref HAL_FLASH_Lock() to disable the flash program memory access |
||
137 | (recommended to protect the FLASH memory against possible unwanted operation). |
||
138 | |||
139 | @endverbatim |
||
140 | * @{ |
||
141 | */ |
||
142 | |||
143 | |||
144 | /** |
||
145 | * @brief Perform a mass erase or erase the specified FLASH memory pages |
||
146 | * @note To correctly run this function, the @ref HAL_FLASH_Unlock() function |
||
147 | * must be called before. |
||
148 | * Call the @ref HAL_FLASH_Lock() to disable the flash memory access |
||
149 | * (recommended to protect the FLASH memory against possible unwanted operation) |
||
150 | * @param[in] pEraseInit pointer to an FLASH_EraseInitTypeDef structure that |
||
151 | * contains the configuration information for the erasing. |
||
152 | * |
||
153 | * @param[out] PageError pointer to variable that |
||
154 | * contains the configuration information on faulty page in case of error |
||
155 | * (0xFFFFFFFF means that all the pages have been correctly erased) |
||
156 | * |
||
157 | * @retval HAL_StatusTypeDef HAL Status |
||
158 | */ |
||
159 | HAL_StatusTypeDef HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *PageError) |
||
160 | { |
||
161 | HAL_StatusTypeDef status = HAL_ERROR; |
||
162 | uint32_t address = 0U; |
||
163 | |||
164 | /* Process Locked */ |
||
165 | __HAL_LOCK(&pFlash); |
||
166 | |||
167 | /* Check the parameters */ |
||
168 | assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase)); |
||
169 | |||
170 | if (pEraseInit->TypeErase == FLASH_TYPEERASE_MASSERASE) |
||
171 | { |
||
172 | #if defined(FLASH_BANK2_END) |
||
173 | if (pEraseInit->Banks == FLASH_BANK_BOTH) |
||
174 | { |
||
175 | /* Mass Erase requested for Bank1 and Bank2 */ |
||
176 | /* Wait for last operation to be completed */ |
||
177 | if ((FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE) == HAL_OK) && \ |
||
178 | (FLASH_WaitForLastOperationBank2((uint32_t)FLASH_TIMEOUT_VALUE) == HAL_OK)) |
||
179 | { |
||
180 | /*Mass erase to be done*/ |
||
181 | FLASH_MassErase(FLASH_BANK_BOTH); |
||
182 | |||
183 | /* Wait for last operation to be completed */ |
||
184 | if ((FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE) == HAL_OK) && \ |
||
185 | (FLASH_WaitForLastOperationBank2((uint32_t)FLASH_TIMEOUT_VALUE) == HAL_OK)) |
||
186 | { |
||
187 | status = HAL_OK; |
||
188 | } |
||
189 | |||
190 | /* If the erase operation is completed, disable the MER Bit */ |
||
191 | CLEAR_BIT(FLASH->CR, FLASH_CR_MER); |
||
192 | CLEAR_BIT(FLASH->CR2, FLASH_CR2_MER); |
||
193 | } |
||
194 | } |
||
195 | else if (pEraseInit->Banks == FLASH_BANK_2) |
||
196 | { |
||
197 | /* Mass Erase requested for Bank2 */ |
||
198 | /* Wait for last operation to be completed */ |
||
199 | if (FLASH_WaitForLastOperationBank2((uint32_t)FLASH_TIMEOUT_VALUE) == HAL_OK) |
||
200 | { |
||
201 | /*Mass erase to be done*/ |
||
202 | FLASH_MassErase(FLASH_BANK_2); |
||
203 | |||
204 | /* Wait for last operation to be completed */ |
||
205 | status = FLASH_WaitForLastOperationBank2((uint32_t)FLASH_TIMEOUT_VALUE); |
||
206 | |||
207 | /* If the erase operation is completed, disable the MER Bit */ |
||
208 | CLEAR_BIT(FLASH->CR2, FLASH_CR2_MER); |
||
209 | } |
||
210 | } |
||
211 | else |
||
212 | #endif /* FLASH_BANK2_END */ |
||
213 | { |
||
214 | /* Mass Erase requested for Bank1 */ |
||
215 | /* Wait for last operation to be completed */ |
||
216 | if (FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE) == HAL_OK) |
||
217 | { |
||
218 | /*Mass erase to be done*/ |
||
219 | FLASH_MassErase(FLASH_BANK_1); |
||
220 | |||
221 | /* Wait for last operation to be completed */ |
||
222 | status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE); |
||
223 | |||
224 | /* If the erase operation is completed, disable the MER Bit */ |
||
225 | CLEAR_BIT(FLASH->CR, FLASH_CR_MER); |
||
226 | } |
||
227 | } |
||
228 | } |
||
229 | else |
||
230 | { |
||
231 | /* Page Erase is requested */ |
||
232 | /* Check the parameters */ |
||
233 | assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit->PageAddress)); |
||
234 | assert_param(IS_FLASH_NB_PAGES(pEraseInit->PageAddress, pEraseInit->NbPages)); |
||
235 | |||
236 | #if defined(FLASH_BANK2_END) |
||
237 | /* Page Erase requested on address located on bank2 */ |
||
238 | if(pEraseInit->PageAddress > FLASH_BANK1_END) |
||
239 | { |
||
240 | /* Wait for last operation to be completed */ |
||
241 | if (FLASH_WaitForLastOperationBank2((uint32_t)FLASH_TIMEOUT_VALUE) == HAL_OK) |
||
242 | { |
||
243 | /*Initialization of PageError variable*/ |
||
244 | *PageError = 0xFFFFFFFFU; |
||
245 | |||
246 | /* Erase by page by page to be done*/ |
||
247 | for(address = pEraseInit->PageAddress; |
||
248 | address < (pEraseInit->PageAddress + (pEraseInit->NbPages)*FLASH_PAGE_SIZE); |
||
249 | address += FLASH_PAGE_SIZE) |
||
250 | { |
||
251 | FLASH_PageErase(address); |
||
252 | |||
253 | /* Wait for last operation to be completed */ |
||
254 | status = FLASH_WaitForLastOperationBank2((uint32_t)FLASH_TIMEOUT_VALUE); |
||
255 | |||
256 | /* If the erase operation is completed, disable the PER Bit */ |
||
257 | CLEAR_BIT(FLASH->CR2, FLASH_CR2_PER); |
||
258 | |||
259 | if (status != HAL_OK) |
||
260 | { |
||
261 | /* In case of error, stop erase procedure and return the faulty address */ |
||
262 | *PageError = address; |
||
263 | break; |
||
264 | } |
||
265 | } |
||
266 | } |
||
267 | } |
||
268 | else |
||
269 | #endif /* FLASH_BANK2_END */ |
||
270 | { |
||
271 | /* Page Erase requested on address located on bank1 */ |
||
272 | /* Wait for last operation to be completed */ |
||
273 | if (FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE) == HAL_OK) |
||
274 | { |
||
275 | /*Initialization of PageError variable*/ |
||
276 | *PageError = 0xFFFFFFFFU; |
||
277 | |||
278 | /* Erase page by page to be done*/ |
||
279 | for(address = pEraseInit->PageAddress; |
||
280 | address < ((pEraseInit->NbPages * FLASH_PAGE_SIZE) + pEraseInit->PageAddress); |
||
281 | address += FLASH_PAGE_SIZE) |
||
282 | { |
||
283 | FLASH_PageErase(address); |
||
284 | |||
285 | /* Wait for last operation to be completed */ |
||
286 | status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE); |
||
287 | |||
288 | /* If the erase operation is completed, disable the PER Bit */ |
||
289 | CLEAR_BIT(FLASH->CR, FLASH_CR_PER); |
||
290 | |||
291 | if (status != HAL_OK) |
||
292 | { |
||
293 | /* In case of error, stop erase procedure and return the faulty address */ |
||
294 | *PageError = address; |
||
295 | break; |
||
296 | } |
||
297 | } |
||
298 | } |
||
299 | } |
||
300 | } |
||
301 | |||
302 | /* Process Unlocked */ |
||
303 | __HAL_UNLOCK(&pFlash); |
||
304 | |||
305 | return status; |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * @brief Perform a mass erase or erase the specified FLASH memory pages with interrupt enabled |
||
310 | * @note To correctly run this function, the @ref HAL_FLASH_Unlock() function |
||
311 | * must be called before. |
||
312 | * Call the @ref HAL_FLASH_Lock() to disable the flash memory access |
||
313 | * (recommended to protect the FLASH memory against possible unwanted operation) |
||
314 | * @param pEraseInit pointer to an FLASH_EraseInitTypeDef structure that |
||
315 | * contains the configuration information for the erasing. |
||
316 | * |
||
317 | * @retval HAL_StatusTypeDef HAL Status |
||
318 | */ |
||
319 | HAL_StatusTypeDef HAL_FLASHEx_Erase_IT(FLASH_EraseInitTypeDef *pEraseInit) |
||
320 | { |
||
321 | HAL_StatusTypeDef status = HAL_OK; |
||
322 | |||
323 | /* Process Locked */ |
||
324 | __HAL_LOCK(&pFlash); |
||
325 | |||
326 | /* If procedure already ongoing, reject the next one */ |
||
327 | if (pFlash.ProcedureOnGoing != FLASH_PROC_NONE) |
||
328 | { |
||
329 | return HAL_ERROR; |
||
330 | } |
||
331 | |||
332 | /* Check the parameters */ |
||
333 | assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase)); |
||
334 | |||
335 | /* Enable End of FLASH Operation and Error source interrupts */ |
||
336 | __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP | FLASH_IT_ERR); |
||
337 | |||
338 | #if defined(FLASH_BANK2_END) |
||
339 | /* Enable End of FLASH Operation and Error source interrupts */ |
||
340 | __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP_BANK2 | FLASH_IT_ERR_BANK2); |
||
341 | |||
342 | #endif |
||
343 | if (pEraseInit->TypeErase == FLASH_TYPEERASE_MASSERASE) |
||
344 | { |
||
345 | /*Mass erase to be done*/ |
||
346 | pFlash.ProcedureOnGoing = FLASH_PROC_MASSERASE; |
||
347 | FLASH_MassErase(pEraseInit->Banks); |
||
348 | } |
||
349 | else |
||
350 | { |
||
351 | /* Erase by page to be done*/ |
||
352 | |||
353 | /* Check the parameters */ |
||
354 | assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit->PageAddress)); |
||
355 | assert_param(IS_FLASH_NB_PAGES(pEraseInit->PageAddress, pEraseInit->NbPages)); |
||
356 | |||
357 | pFlash.ProcedureOnGoing = FLASH_PROC_PAGEERASE; |
||
358 | pFlash.DataRemaining = pEraseInit->NbPages; |
||
359 | pFlash.Address = pEraseInit->PageAddress; |
||
360 | |||
361 | /*Erase 1st page and wait for IT*/ |
||
362 | FLASH_PageErase(pEraseInit->PageAddress); |
||
363 | } |
||
364 | |||
365 | return status; |
||
366 | } |
||
367 | |||
368 | /** |
||
369 | * @} |
||
370 | */ |
||
371 | |||
372 | /** @defgroup FLASHEx_Exported_Functions_Group2 Option Bytes Programming functions |
||
373 | * @brief Option Bytes Programming functions |
||
374 | * |
||
375 | @verbatim |
||
376 | ============================================================================== |
||
377 | ##### Option Bytes Programming functions ##### |
||
378 | ============================================================================== |
||
379 | [..] |
||
380 | This subsection provides a set of functions allowing to control the FLASH |
||
381 | option bytes operations. |
||
382 | |||
383 | @endverbatim |
||
384 | * @{ |
||
385 | */ |
||
386 | |||
387 | /** |
||
388 | * @brief Erases the FLASH option bytes. |
||
389 | * @note This functions erases all option bytes except the Read protection (RDP). |
||
390 | * The function @ref HAL_FLASH_Unlock() should be called before to unlock the FLASH interface |
||
391 | * The function @ref HAL_FLASH_OB_Unlock() should be called before to unlock the options bytes |
||
392 | * The function @ref HAL_FLASH_OB_Launch() should be called after to force the reload of the options bytes |
||
393 | * (system reset will occur) |
||
394 | * @retval HAL status |
||
395 | */ |
||
396 | |||
397 | HAL_StatusTypeDef HAL_FLASHEx_OBErase(void) |
||
398 | { |
||
399 | uint8_t rdptmp = OB_RDP_LEVEL_0; |
||
400 | HAL_StatusTypeDef status = HAL_ERROR; |
||
401 | |||
402 | /* Get the actual read protection Option Byte value */ |
||
403 | rdptmp = FLASH_OB_GetRDP(); |
||
404 | |||
405 | /* Wait for last operation to be completed */ |
||
406 | status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE); |
||
407 | |||
408 | if(status == HAL_OK) |
||
409 | { |
||
410 | /* Clean the error context */ |
||
411 | pFlash.ErrorCode = HAL_FLASH_ERROR_NONE; |
||
412 | |||
413 | /* If the previous operation is completed, proceed to erase the option bytes */ |
||
414 | SET_BIT(FLASH->CR, FLASH_CR_OPTER); |
||
415 | SET_BIT(FLASH->CR, FLASH_CR_STRT); |
||
416 | |||
417 | /* Wait for last operation to be completed */ |
||
418 | status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE); |
||
419 | |||
420 | /* If the erase operation is completed, disable the OPTER Bit */ |
||
421 | CLEAR_BIT(FLASH->CR, FLASH_CR_OPTER); |
||
422 | |||
423 | if(status == HAL_OK) |
||
424 | { |
||
425 | /* Restore the last read protection Option Byte value */ |
||
426 | status = FLASH_OB_RDP_LevelConfig(rdptmp); |
||
427 | } |
||
428 | } |
||
429 | |||
430 | /* Return the erase status */ |
||
431 | return status; |
||
432 | } |
||
433 | |||
434 | /** |
||
435 | * @brief Program option bytes |
||
436 | * @note The function @ref HAL_FLASH_Unlock() should be called before to unlock the FLASH interface |
||
437 | * The function @ref HAL_FLASH_OB_Unlock() should be called before to unlock the options bytes |
||
438 | * The function @ref HAL_FLASH_OB_Launch() should be called after to force the reload of the options bytes |
||
439 | * (system reset will occur) |
||
440 | * |
||
441 | * @param pOBInit pointer to an FLASH_OBInitStruct structure that |
||
442 | * contains the configuration information for the programming. |
||
443 | * |
||
444 | * @retval HAL_StatusTypeDef HAL Status |
||
445 | */ |
||
446 | HAL_StatusTypeDef HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit) |
||
447 | { |
||
448 | HAL_StatusTypeDef status = HAL_ERROR; |
||
449 | |||
450 | /* Process Locked */ |
||
451 | __HAL_LOCK(&pFlash); |
||
452 | |||
453 | /* Check the parameters */ |
||
454 | assert_param(IS_OPTIONBYTE(pOBInit->OptionType)); |
||
455 | |||
456 | /* Write protection configuration */ |
||
457 | if((pOBInit->OptionType & OPTIONBYTE_WRP) == OPTIONBYTE_WRP) |
||
458 | { |
||
459 | assert_param(IS_WRPSTATE(pOBInit->WRPState)); |
||
460 | if (pOBInit->WRPState == OB_WRPSTATE_ENABLE) |
||
461 | { |
||
462 | /* Enable of Write protection on the selected page */ |
||
463 | status = FLASH_OB_EnableWRP(pOBInit->WRPPage); |
||
464 | } |
||
465 | else |
||
466 | { |
||
467 | /* Disable of Write protection on the selected page */ |
||
468 | status = FLASH_OB_DisableWRP(pOBInit->WRPPage); |
||
469 | } |
||
470 | if (status != HAL_OK) |
||
471 | { |
||
472 | /* Process Unlocked */ |
||
473 | __HAL_UNLOCK(&pFlash); |
||
474 | return status; |
||
475 | } |
||
476 | } |
||
477 | |||
478 | /* Read protection configuration */ |
||
479 | if((pOBInit->OptionType & OPTIONBYTE_RDP) == OPTIONBYTE_RDP) |
||
480 | { |
||
481 | status = FLASH_OB_RDP_LevelConfig(pOBInit->RDPLevel); |
||
482 | if (status != HAL_OK) |
||
483 | { |
||
484 | /* Process Unlocked */ |
||
485 | __HAL_UNLOCK(&pFlash); |
||
486 | return status; |
||
487 | } |
||
488 | } |
||
489 | |||
490 | /* USER configuration */ |
||
491 | if((pOBInit->OptionType & OPTIONBYTE_USER) == OPTIONBYTE_USER) |
||
492 | { |
||
493 | status = FLASH_OB_UserConfig(pOBInit->USERConfig); |
||
494 | if (status != HAL_OK) |
||
495 | { |
||
496 | /* Process Unlocked */ |
||
497 | __HAL_UNLOCK(&pFlash); |
||
498 | return status; |
||
499 | } |
||
500 | } |
||
501 | |||
502 | /* DATA configuration*/ |
||
503 | if((pOBInit->OptionType & OPTIONBYTE_DATA) == OPTIONBYTE_DATA) |
||
504 | { |
||
505 | status = FLASH_OB_ProgramData(pOBInit->DATAAddress, pOBInit->DATAData); |
||
506 | if (status != HAL_OK) |
||
507 | { |
||
508 | /* Process Unlocked */ |
||
509 | __HAL_UNLOCK(&pFlash); |
||
510 | return status; |
||
511 | } |
||
512 | } |
||
513 | |||
514 | /* Process Unlocked */ |
||
515 | __HAL_UNLOCK(&pFlash); |
||
516 | |||
517 | return status; |
||
518 | } |
||
519 | |||
520 | /** |
||
521 | * @brief Get the Option byte configuration |
||
522 | * @param pOBInit pointer to an FLASH_OBInitStruct structure that |
||
523 | * contains the configuration information for the programming. |
||
524 | * |
||
525 | * @retval None |
||
526 | */ |
||
527 | void HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit) |
||
528 | { |
||
529 | pOBInit->OptionType = OPTIONBYTE_WRP | OPTIONBYTE_RDP | OPTIONBYTE_USER; |
||
530 | |||
531 | /*Get WRP*/ |
||
532 | pOBInit->WRPPage = FLASH_OB_GetWRP(); |
||
533 | |||
534 | /*Get RDP Level*/ |
||
535 | pOBInit->RDPLevel = FLASH_OB_GetRDP(); |
||
536 | |||
537 | /*Get USER*/ |
||
538 | pOBInit->USERConfig = FLASH_OB_GetUser(); |
||
539 | } |
||
540 | |||
541 | /** |
||
542 | * @brief Get the Option byte user data |
||
543 | * @param DATAAdress Address of the option byte DATA |
||
544 | * This parameter can be one of the following values: |
||
545 | * @arg @ref OB_DATA_ADDRESS_DATA0 |
||
546 | * @arg @ref OB_DATA_ADDRESS_DATA1 |
||
547 | * @retval Value programmed in USER data |
||
548 | */ |
||
549 | uint32_t HAL_FLASHEx_OBGetUserData(uint32_t DATAAdress) |
||
550 | { |
||
551 | uint32_t value = 0; |
||
552 | |||
553 | if (DATAAdress == OB_DATA_ADDRESS_DATA0) |
||
554 | { |
||
555 | /* Get value programmed in OB USER Data0 */ |
||
556 | value = READ_BIT(FLASH->OBR, FLASH_OBR_DATA0) >> FLASH_POSITION_OB_USERDATA0_BIT; |
||
557 | } |
||
558 | else |
||
559 | { |
||
560 | /* Get value programmed in OB USER Data1 */ |
||
561 | value = READ_BIT(FLASH->OBR, FLASH_OBR_DATA1) >> FLASH_POSITION_OB_USERDATA1_BIT; |
||
562 | } |
||
563 | |||
564 | return value; |
||
565 | } |
||
566 | |||
567 | /** |
||
568 | * @} |
||
569 | */ |
||
570 | |||
571 | /** |
||
572 | * @} |
||
573 | */ |
||
574 | |||
575 | /** @addtogroup FLASHEx_Private_Functions |
||
576 | * @{ |
||
577 | */ |
||
578 | |||
579 | /** |
||
580 | * @brief Full erase of FLASH memory Bank |
||
581 | * @param Banks Banks to be erased |
||
582 | * This parameter can be one of the following values: |
||
583 | * @arg @ref FLASH_BANK_1 Bank1 to be erased |
||
584 | @if STM32F101xG |
||
585 | * @arg @ref FLASH_BANK_2 Bank2 to be erased |
||
586 | * @arg @ref FLASH_BANK_BOTH Bank1 and Bank2 to be erased |
||
587 | @endif |
||
588 | @if STM32F103xG |
||
589 | * @arg @ref FLASH_BANK_2 Bank2 to be erased |
||
590 | * @arg @ref FLASH_BANK_BOTH Bank1 and Bank2 to be erased |
||
591 | @endif |
||
592 | * |
||
593 | * @retval None |
||
594 | */ |
||
595 | static void FLASH_MassErase(uint32_t Banks) |
||
596 | { |
||
597 | /* Check the parameters */ |
||
598 | assert_param(IS_FLASH_BANK(Banks)); |
||
599 | |||
600 | /* Clean the error context */ |
||
601 | pFlash.ErrorCode = HAL_FLASH_ERROR_NONE; |
||
602 | |||
603 | #if defined(FLASH_BANK2_END) |
||
604 | if(Banks == FLASH_BANK_BOTH) |
||
605 | { |
||
606 | /* bank1 & bank2 will be erased*/ |
||
607 | SET_BIT(FLASH->CR, FLASH_CR_MER); |
||
608 | SET_BIT(FLASH->CR2, FLASH_CR2_MER); |
||
609 | SET_BIT(FLASH->CR, FLASH_CR_STRT); |
||
610 | SET_BIT(FLASH->CR2, FLASH_CR2_STRT); |
||
611 | } |
||
612 | else if(Banks == FLASH_BANK_2) |
||
613 | { |
||
614 | /*Only bank2 will be erased*/ |
||
615 | SET_BIT(FLASH->CR2, FLASH_CR2_MER); |
||
616 | SET_BIT(FLASH->CR2, FLASH_CR2_STRT); |
||
617 | } |
||
618 | else |
||
619 | { |
||
620 | #endif /* FLASH_BANK2_END */ |
||
621 | #if !defined(FLASH_BANK2_END) |
||
622 | /* Prevent unused argument(s) compilation warning */ |
||
623 | UNUSED(Banks); |
||
624 | #endif /* FLASH_BANK2_END */ |
||
625 | /* Only bank1 will be erased*/ |
||
626 | SET_BIT(FLASH->CR, FLASH_CR_MER); |
||
627 | SET_BIT(FLASH->CR, FLASH_CR_STRT); |
||
628 | #if defined(FLASH_BANK2_END) |
||
629 | } |
||
630 | #endif /* FLASH_BANK2_END */ |
||
631 | } |
||
632 | |||
633 | /** |
||
634 | * @brief Enable the write protection of the desired pages |
||
635 | * @note An option byte erase is done automatically in this function. |
||
636 | * @note When the memory read protection level is selected (RDP level = 1), |
||
637 | * it is not possible to program or erase the flash page i if |
||
638 | * debug features are connected or boot code is executed in RAM, even if nWRPi = 1 |
||
639 | * |
||
640 | * @param WriteProtectPage specifies the page(s) to be write protected. |
||
641 | * The value of this parameter depend on device used within the same series |
||
642 | * @retval HAL status |
||
643 | */ |
||
644 | static HAL_StatusTypeDef FLASH_OB_EnableWRP(uint32_t WriteProtectPage) |
||
645 | { |
||
646 | HAL_StatusTypeDef status = HAL_OK; |
||
647 | uint16_t WRP0_Data = 0xFFFF; |
||
648 | #if defined(FLASH_WRP1_WRP1) |
||
649 | uint16_t WRP1_Data = 0xFFFF; |
||
650 | #endif /* FLASH_WRP1_WRP1 */ |
||
651 | #if defined(FLASH_WRP2_WRP2) |
||
652 | uint16_t WRP2_Data = 0xFFFF; |
||
653 | #endif /* FLASH_WRP2_WRP2 */ |
||
654 | #if defined(FLASH_WRP3_WRP3) |
||
655 | uint16_t WRP3_Data = 0xFFFF; |
||
656 | #endif /* FLASH_WRP3_WRP3 */ |
||
657 | |||
658 | /* Check the parameters */ |
||
659 | assert_param(IS_OB_WRP(WriteProtectPage)); |
||
660 | |||
661 | /* Get current write protected pages and the new pages to be protected ******/ |
||
662 | WriteProtectPage = (uint32_t)(~((~FLASH_OB_GetWRP()) | WriteProtectPage)); |
||
663 | |||
664 | #if defined(OB_WRP_PAGES0TO15MASK) |
||
665 | WRP0_Data = (uint16_t)(WriteProtectPage & OB_WRP_PAGES0TO15MASK); |
||
666 | #elif defined(OB_WRP_PAGES0TO31MASK) |
||
667 | WRP0_Data = (uint16_t)(WriteProtectPage & OB_WRP_PAGES0TO31MASK); |
||
668 | #endif /* OB_WRP_PAGES0TO31MASK */ |
||
669 | |||
670 | #if defined(OB_WRP_PAGES16TO31MASK) |
||
671 | WRP1_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES16TO31MASK) >> 8U); |
||
672 | #elif defined(OB_WRP_PAGES32TO63MASK) |
||
673 | WRP1_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES32TO63MASK) >> 8U); |
||
674 | #endif /* OB_WRP_PAGES32TO63MASK */ |
||
675 | |||
676 | #if defined(OB_WRP_PAGES64TO95MASK) |
||
677 | WRP2_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES64TO95MASK) >> 16U); |
||
678 | #endif /* OB_WRP_PAGES64TO95MASK */ |
||
679 | #if defined(OB_WRP_PAGES32TO47MASK) |
||
680 | WRP2_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES32TO47MASK) >> 16U); |
||
681 | #endif /* OB_WRP_PAGES32TO47MASK */ |
||
682 | |||
683 | #if defined(OB_WRP_PAGES96TO127MASK) |
||
684 | WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES96TO127MASK) >> 24U); |
||
685 | #elif defined(OB_WRP_PAGES48TO255MASK) |
||
686 | WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES48TO255MASK) >> 24U); |
||
687 | #elif defined(OB_WRP_PAGES48TO511MASK) |
||
688 | WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES48TO511MASK) >> 24U); |
||
689 | #elif defined(OB_WRP_PAGES48TO127MASK) |
||
690 | WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES48TO127MASK) >> 24U); |
||
691 | #endif /* OB_WRP_PAGES96TO127MASK */ |
||
692 | |||
693 | /* Wait for last operation to be completed */ |
||
694 | status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE); |
||
695 | |||
696 | if(status == HAL_OK) |
||
697 | { |
||
698 | /* Clean the error context */ |
||
699 | pFlash.ErrorCode = HAL_FLASH_ERROR_NONE; |
||
700 | |||
701 | /* To be able to write again option byte, need to perform a option byte erase */ |
||
702 | status = HAL_FLASHEx_OBErase(); |
||
703 | if (status == HAL_OK) |
||
704 | { |
||
705 | /* Enable write protection */ |
||
706 | SET_BIT(FLASH->CR, FLASH_CR_OPTPG); |
||
707 | |||
708 | #if defined(FLASH_WRP0_WRP0) |
||
709 | if(WRP0_Data != 0xFFU) |
||
710 | { |
||
711 | OB->WRP0 &= WRP0_Data; |
||
712 | |||
713 | /* Wait for last operation to be completed */ |
||
714 | status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE); |
||
715 | } |
||
716 | #endif /* FLASH_WRP0_WRP0 */ |
||
717 | |||
718 | #if defined(FLASH_WRP1_WRP1) |
||
719 | if((status == HAL_OK) && (WRP1_Data != 0xFFU)) |
||
720 | { |
||
721 | OB->WRP1 &= WRP1_Data; |
||
722 | |||
723 | /* Wait for last operation to be completed */ |
||
724 | status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE); |
||
725 | } |
||
726 | #endif /* FLASH_WRP1_WRP1 */ |
||
727 | |||
728 | #if defined(FLASH_WRP2_WRP2) |
||
729 | if((status == HAL_OK) && (WRP2_Data != 0xFFU)) |
||
730 | { |
||
731 | OB->WRP2 &= WRP2_Data; |
||
732 | |||
733 | /* Wait for last operation to be completed */ |
||
734 | status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE); |
||
735 | } |
||
736 | #endif /* FLASH_WRP2_WRP2 */ |
||
737 | |||
738 | #if defined(FLASH_WRP3_WRP3) |
||
739 | if((status == HAL_OK) && (WRP3_Data != 0xFFU)) |
||
740 | { |
||
741 | OB->WRP3 &= WRP3_Data; |
||
742 | |||
743 | /* Wait for last operation to be completed */ |
||
744 | status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE); |
||
745 | } |
||
746 | #endif /* FLASH_WRP3_WRP3 */ |
||
747 | |||
748 | /* if the program operation is completed, disable the OPTPG Bit */ |
||
749 | CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG); |
||
750 | } |
||
751 | } |
||
752 | |||
753 | return status; |
||
754 | } |
||
755 | |||
756 | /** |
||
757 | * @brief Disable the write protection of the desired pages |
||
758 | * @note An option byte erase is done automatically in this function. |
||
759 | * @note When the memory read protection level is selected (RDP level = 1), |
||
760 | * it is not possible to program or erase the flash page i if |
||
761 | * debug features are connected or boot code is executed in RAM, even if nWRPi = 1 |
||
762 | * |
||
763 | * @param WriteProtectPage specifies the page(s) to be write unprotected. |
||
764 | * The value of this parameter depend on device used within the same series |
||
765 | * @retval HAL status |
||
766 | */ |
||
767 | static HAL_StatusTypeDef FLASH_OB_DisableWRP(uint32_t WriteProtectPage) |
||
768 | { |
||
769 | HAL_StatusTypeDef status = HAL_OK; |
||
770 | uint16_t WRP0_Data = 0xFFFF; |
||
771 | #if defined(FLASH_WRP1_WRP1) |
||
772 | uint16_t WRP1_Data = 0xFFFF; |
||
773 | #endif /* FLASH_WRP1_WRP1 */ |
||
774 | #if defined(FLASH_WRP2_WRP2) |
||
775 | uint16_t WRP2_Data = 0xFFFF; |
||
776 | #endif /* FLASH_WRP2_WRP2 */ |
||
777 | #if defined(FLASH_WRP3_WRP3) |
||
778 | uint16_t WRP3_Data = 0xFFFF; |
||
779 | #endif /* FLASH_WRP3_WRP3 */ |
||
780 | |||
781 | /* Check the parameters */ |
||
782 | assert_param(IS_OB_WRP(WriteProtectPage)); |
||
783 | |||
784 | /* Get current write protected pages and the new pages to be unprotected ******/ |
||
785 | WriteProtectPage = (FLASH_OB_GetWRP() | WriteProtectPage); |
||
786 | |||
787 | #if defined(OB_WRP_PAGES0TO15MASK) |
||
788 | WRP0_Data = (uint16_t)(WriteProtectPage & OB_WRP_PAGES0TO15MASK); |
||
789 | #elif defined(OB_WRP_PAGES0TO31MASK) |
||
790 | WRP0_Data = (uint16_t)(WriteProtectPage & OB_WRP_PAGES0TO31MASK); |
||
791 | #endif /* OB_WRP_PAGES0TO31MASK */ |
||
792 | |||
793 | #if defined(OB_WRP_PAGES16TO31MASK) |
||
794 | WRP1_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES16TO31MASK) >> 8U); |
||
795 | #elif defined(OB_WRP_PAGES32TO63MASK) |
||
796 | WRP1_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES32TO63MASK) >> 8U); |
||
797 | #endif /* OB_WRP_PAGES32TO63MASK */ |
||
798 | |||
799 | #if defined(OB_WRP_PAGES64TO95MASK) |
||
800 | WRP2_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES64TO95MASK) >> 16U); |
||
801 | #endif /* OB_WRP_PAGES64TO95MASK */ |
||
802 | #if defined(OB_WRP_PAGES32TO47MASK) |
||
803 | WRP2_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES32TO47MASK) >> 16U); |
||
804 | #endif /* OB_WRP_PAGES32TO47MASK */ |
||
805 | |||
806 | #if defined(OB_WRP_PAGES96TO127MASK) |
||
807 | WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES96TO127MASK) >> 24U); |
||
808 | #elif defined(OB_WRP_PAGES48TO255MASK) |
||
809 | WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES48TO255MASK) >> 24U); |
||
810 | #elif defined(OB_WRP_PAGES48TO511MASK) |
||
811 | WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES48TO511MASK) >> 24U); |
||
812 | #elif defined(OB_WRP_PAGES48TO127MASK) |
||
813 | WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES48TO127MASK) >> 24U); |
||
814 | #endif /* OB_WRP_PAGES96TO127MASK */ |
||
815 | |||
816 | |||
817 | /* Wait for last operation to be completed */ |
||
818 | status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE); |
||
819 | |||
820 | if(status == HAL_OK) |
||
821 | { |
||
822 | /* Clean the error context */ |
||
823 | pFlash.ErrorCode = HAL_FLASH_ERROR_NONE; |
||
824 | |||
825 | /* To be able to write again option byte, need to perform a option byte erase */ |
||
826 | status = HAL_FLASHEx_OBErase(); |
||
827 | if (status == HAL_OK) |
||
828 | { |
||
829 | SET_BIT(FLASH->CR, FLASH_CR_OPTPG); |
||
830 | |||
831 | #if defined(FLASH_WRP0_WRP0) |
||
832 | if(WRP0_Data != 0xFFU) |
||
833 | { |
||
834 | OB->WRP0 |= WRP0_Data; |
||
835 | |||
836 | /* Wait for last operation to be completed */ |
||
837 | status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE); |
||
838 | } |
||
839 | #endif /* FLASH_WRP0_WRP0 */ |
||
840 | |||
841 | #if defined(FLASH_WRP1_WRP1) |
||
842 | if((status == HAL_OK) && (WRP1_Data != 0xFFU)) |
||
843 | { |
||
844 | OB->WRP1 |= WRP1_Data; |
||
845 | |||
846 | /* Wait for last operation to be completed */ |
||
847 | status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE); |
||
848 | } |
||
849 | #endif /* FLASH_WRP1_WRP1 */ |
||
850 | |||
851 | #if defined(FLASH_WRP2_WRP2) |
||
852 | if((status == HAL_OK) && (WRP2_Data != 0xFFU)) |
||
853 | { |
||
854 | OB->WRP2 |= WRP2_Data; |
||
855 | |||
856 | /* Wait for last operation to be completed */ |
||
857 | status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE); |
||
858 | } |
||
859 | #endif /* FLASH_WRP2_WRP2 */ |
||
860 | |||
861 | #if defined(FLASH_WRP3_WRP3) |
||
862 | if((status == HAL_OK) && (WRP3_Data != 0xFFU)) |
||
863 | { |
||
864 | OB->WRP3 |= WRP3_Data; |
||
865 | |||
866 | /* Wait for last operation to be completed */ |
||
867 | status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE); |
||
868 | } |
||
869 | #endif /* FLASH_WRP3_WRP3 */ |
||
870 | |||
871 | /* if the program operation is completed, disable the OPTPG Bit */ |
||
872 | CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG); |
||
873 | } |
||
874 | } |
||
875 | return status; |
||
876 | } |
||
877 | |||
878 | /** |
||
879 | * @brief Set the read protection level. |
||
880 | * @param ReadProtectLevel specifies the read protection level. |
||
881 | * This parameter can be one of the following values: |
||
882 | * @arg @ref OB_RDP_LEVEL_0 No protection |
||
883 | * @arg @ref OB_RDP_LEVEL_1 Read protection of the memory |
||
884 | * @retval HAL status |
||
885 | */ |
||
886 | static HAL_StatusTypeDef FLASH_OB_RDP_LevelConfig(uint8_t ReadProtectLevel) |
||
887 | { |
||
888 | HAL_StatusTypeDef status = HAL_OK; |
||
889 | |||
890 | /* Check the parameters */ |
||
891 | assert_param(IS_OB_RDP_LEVEL(ReadProtectLevel)); |
||
892 | |||
893 | /* Wait for last operation to be completed */ |
||
894 | status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE); |
||
895 | |||
896 | if(status == HAL_OK) |
||
897 | { |
||
898 | /* Clean the error context */ |
||
899 | pFlash.ErrorCode = HAL_FLASH_ERROR_NONE; |
||
900 | |||
901 | /* If the previous operation is completed, proceed to erase the option bytes */ |
||
902 | SET_BIT(FLASH->CR, FLASH_CR_OPTER); |
||
903 | SET_BIT(FLASH->CR, FLASH_CR_STRT); |
||
904 | |||
905 | /* Wait for last operation to be completed */ |
||
906 | status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE); |
||
907 | |||
908 | /* If the erase operation is completed, disable the OPTER Bit */ |
||
909 | CLEAR_BIT(FLASH->CR, FLASH_CR_OPTER); |
||
910 | |||
911 | if(status == HAL_OK) |
||
912 | { |
||
913 | /* Enable the Option Bytes Programming operation */ |
||
914 | SET_BIT(FLASH->CR, FLASH_CR_OPTPG); |
||
915 | |||
916 | WRITE_REG(OB->RDP, ReadProtectLevel); |
||
917 | |||
918 | /* Wait for last operation to be completed */ |
||
919 | status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE); |
||
920 | |||
921 | /* if the program operation is completed, disable the OPTPG Bit */ |
||
922 | CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG); |
||
923 | } |
||
924 | } |
||
925 | |||
926 | return status; |
||
927 | } |
||
928 | |||
929 | /** |
||
930 | * @brief Program the FLASH User Option Byte. |
||
931 | * @note Programming of the OB should be performed only after an erase (otherwise PGERR occurs) |
||
932 | * @param UserConfig The FLASH User Option Bytes values FLASH_OBR_IWDG_SW(Bit2), |
||
933 | * FLASH_OBR_nRST_STOP(Bit3),FLASH_OBR_nRST_STDBY(Bit4). |
||
934 | * And BFBF2(Bit5) for STM32F101xG and STM32F103xG . |
||
935 | * @retval HAL status |
||
936 | */ |
||
937 | static HAL_StatusTypeDef FLASH_OB_UserConfig(uint8_t UserConfig) |
||
938 | { |
||
939 | HAL_StatusTypeDef status = HAL_OK; |
||
940 | |||
941 | /* Check the parameters */ |
||
942 | assert_param(IS_OB_IWDG_SOURCE((UserConfig&OB_IWDG_SW))); |
||
943 | assert_param(IS_OB_STOP_SOURCE((UserConfig&OB_STOP_NO_RST))); |
||
944 | assert_param(IS_OB_STDBY_SOURCE((UserConfig&OB_STDBY_NO_RST))); |
||
945 | #if defined(FLASH_BANK2_END) |
||
946 | assert_param(IS_OB_BOOT1((UserConfig&OB_BOOT1_SET))); |
||
947 | #endif /* FLASH_BANK2_END */ |
||
948 | |||
949 | /* Wait for last operation to be completed */ |
||
950 | status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE); |
||
951 | |||
952 | if(status == HAL_OK) |
||
953 | { |
||
954 | /* Clean the error context */ |
||
955 | pFlash.ErrorCode = HAL_FLASH_ERROR_NONE; |
||
956 | |||
957 | /* Enable the Option Bytes Programming operation */ |
||
958 | SET_BIT(FLASH->CR, FLASH_CR_OPTPG); |
||
959 | |||
960 | #if defined(FLASH_BANK2_END) |
||
961 | OB->USER = (UserConfig | 0xF0U); |
||
962 | #else |
||
963 | OB->USER = (UserConfig | 0x88U); |
||
964 | #endif /* FLASH_BANK2_END */ |
||
965 | |||
966 | /* Wait for last operation to be completed */ |
||
967 | status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE); |
||
968 | |||
969 | /* if the program operation is completed, disable the OPTPG Bit */ |
||
970 | CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG); |
||
971 | } |
||
972 | |||
973 | return status; |
||
974 | } |
||
975 | |||
976 | /** |
||
977 | * @brief Programs a half word at a specified Option Byte Data address. |
||
978 | * @note The function @ref HAL_FLASH_Unlock() should be called before to unlock the FLASH interface |
||
979 | * The function @ref HAL_FLASH_OB_Unlock() should be called before to unlock the options bytes |
||
980 | * The function @ref HAL_FLASH_OB_Launch() should be called after to force the reload of the options bytes |
||
981 | * (system reset will occur) |
||
982 | * Programming of the OB should be performed only after an erase (otherwise PGERR occurs) |
||
983 | * @param Address specifies the address to be programmed. |
||
984 | * This parameter can be 0x1FFFF804 or 0x1FFFF806. |
||
985 | * @param Data specifies the data to be programmed. |
||
986 | * @retval HAL status |
||
987 | */ |
||
988 | static HAL_StatusTypeDef FLASH_OB_ProgramData(uint32_t Address, uint8_t Data) |
||
989 | { |
||
990 | HAL_StatusTypeDef status = HAL_ERROR; |
||
991 | |||
992 | /* Check the parameters */ |
||
993 | assert_param(IS_OB_DATA_ADDRESS(Address)); |
||
994 | |||
995 | /* Wait for last operation to be completed */ |
||
996 | status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE); |
||
997 | |||
998 | if(status == HAL_OK) |
||
999 | { |
||
1000 | /* Clean the error context */ |
||
1001 | pFlash.ErrorCode = HAL_FLASH_ERROR_NONE; |
||
1002 | |||
1003 | /* Enables the Option Bytes Programming operation */ |
||
1004 | SET_BIT(FLASH->CR, FLASH_CR_OPTPG); |
||
1005 | *(__IO uint16_t*)Address = Data; |
||
1006 | |||
1007 | /* Wait for last operation to be completed */ |
||
1008 | status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE); |
||
1009 | |||
1010 | /* If the program operation is completed, disable the OPTPG Bit */ |
||
1011 | CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG); |
||
1012 | } |
||
1013 | /* Return the Option Byte Data Program Status */ |
||
1014 | return status; |
||
1015 | } |
||
1016 | |||
1017 | /** |
||
1018 | * @brief Return the FLASH Write Protection Option Bytes value. |
||
1019 | * @retval The FLASH Write Protection Option Bytes value |
||
1020 | */ |
||
1021 | static uint32_t FLASH_OB_GetWRP(void) |
||
1022 | { |
||
1023 | /* Return the FLASH write protection Register value */ |
||
1024 | return (uint32_t)(READ_REG(FLASH->WRPR)); |
||
1025 | } |
||
1026 | |||
1027 | /** |
||
1028 | * @brief Returns the FLASH Read Protection level. |
||
1029 | * @retval FLASH RDP level |
||
1030 | * This parameter can be one of the following values: |
||
1031 | * @arg @ref OB_RDP_LEVEL_0 No protection |
||
1032 | * @arg @ref OB_RDP_LEVEL_1 Read protection of the memory |
||
1033 | */ |
||
1034 | static uint32_t FLASH_OB_GetRDP(void) |
||
1035 | { |
||
1036 | uint32_t readstatus = OB_RDP_LEVEL_0; |
||
1037 | uint32_t tmp_reg = 0U; |
||
1038 | |||
1039 | /* Read RDP level bits */ |
||
1040 | tmp_reg = READ_BIT(FLASH->OBR, FLASH_OBR_RDPRT); |
||
1041 | |||
1042 | if (tmp_reg == FLASH_OBR_RDPRT) |
||
1043 | { |
||
1044 | readstatus = OB_RDP_LEVEL_1; |
||
1045 | } |
||
1046 | else |
||
1047 | { |
||
1048 | readstatus = OB_RDP_LEVEL_0; |
||
1049 | } |
||
1050 | |||
1051 | return readstatus; |
||
1052 | } |
||
1053 | |||
1054 | /** |
||
1055 | * @brief Return the FLASH User Option Byte value. |
||
1056 | * @retval The FLASH User Option Bytes values: FLASH_OBR_IWDG_SW(Bit2), |
||
1057 | * FLASH_OBR_nRST_STOP(Bit3),FLASH_OBR_nRST_STDBY(Bit4). |
||
1058 | * And FLASH_OBR_BFB2(Bit5) for STM32F101xG and STM32F103xG . |
||
1059 | */ |
||
1060 | static uint8_t FLASH_OB_GetUser(void) |
||
1061 | { |
||
1062 | /* Return the User Option Byte */ |
||
1063 | return (uint8_t)((READ_REG(FLASH->OBR) & FLASH_OBR_USER) >> FLASH_POSITION_IWDGSW_BIT); |
||
1064 | } |
||
1065 | |||
1066 | /** |
||
1067 | * @} |
||
1068 | */ |
||
1069 | |||
1070 | /** |
||
1071 | * @} |
||
1072 | */ |
||
1073 | |||
1074 | /** @addtogroup FLASH |
||
1075 | * @{ |
||
1076 | */ |
||
1077 | |||
1078 | /** @addtogroup FLASH_Private_Functions |
||
1079 | * @{ |
||
1080 | */ |
||
1081 | |||
1082 | /** |
||
1083 | * @brief Erase the specified FLASH memory page |
||
1084 | * @param PageAddress FLASH page to erase |
||
1085 | * The value of this parameter depend on device used within the same series |
||
1086 | * |
||
1087 | * @retval None |
||
1088 | */ |
||
1089 | void FLASH_PageErase(uint32_t PageAddress) |
||
1090 | { |
||
1091 | /* Clean the error context */ |
||
1092 | pFlash.ErrorCode = HAL_FLASH_ERROR_NONE; |
||
1093 | |||
1094 | #if defined(FLASH_BANK2_END) |
||
1095 | if(PageAddress > FLASH_BANK1_END) |
||
1096 | { |
||
1097 | /* Proceed to erase the page */ |
||
1098 | SET_BIT(FLASH->CR2, FLASH_CR2_PER); |
||
1099 | WRITE_REG(FLASH->AR2, PageAddress); |
||
1100 | SET_BIT(FLASH->CR2, FLASH_CR2_STRT); |
||
1101 | } |
||
1102 | else |
||
1103 | { |
||
1104 | #endif /* FLASH_BANK2_END */ |
||
1105 | /* Proceed to erase the page */ |
||
1106 | SET_BIT(FLASH->CR, FLASH_CR_PER); |
||
1107 | WRITE_REG(FLASH->AR, PageAddress); |
||
1108 | SET_BIT(FLASH->CR, FLASH_CR_STRT); |
||
1109 | #if defined(FLASH_BANK2_END) |
||
1110 | } |
||
1111 | #endif /* FLASH_BANK2_END */ |
||
1112 | } |
||
1113 | |||
1114 | /** |
||
1115 | * @} |
||
1116 | */ |
||
1117 | |||
1118 | /** |
||
1119 | * @} |
||
1120 | */ |
||
1121 | |||
1122 | #endif /* HAL_FLASH_MODULE_ENABLED */ |
||
1123 | /** |
||
1124 | * @} |
||
1125 | */ |
||
1126 | |||
1127 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |