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