Rev 2 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2 | Rev 3 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | /** |
1 | /** |
2 | ****************************************************************************** |
2 | ****************************************************************************** |
3 | * @file stm32f1xx_hal_flash.c |
3 | * @file stm32f1xx_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 STM32F1xx devices. |
35 | memory of all STM32F1xx 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, erase all pages |
40 | (++) Erase function: Erase page, erase all pages |
41 | (++) Program functions: half word, word and doubleword |
41 | (++) Program functions: half word, word and doubleword |
42 | (#) FLASH Option Bytes Programming functions: this group includes all needed |
42 | (#) FLASH Option Bytes Programming functions: this group includes all needed |
43 | functions to manage the Option Bytes: |
43 | functions to manage the Option Bytes: |
44 | (++) Lock and Unlock the Option Bytes |
44 | (++) Lock and Unlock the Option Bytes |
45 | (++) Set/Reset the write protection |
45 | (++) Set/Reset the write protection |
46 | (++) Set the Read protection Level |
46 | (++) Set the Read protection Level |
47 | (++) Program the user Option Bytes |
47 | (++) Program the user Option Bytes |
48 | (++) Launch the Option Bytes loader |
48 | (++) Launch the Option Bytes loader |
49 | (++) Erase Option Bytes |
49 | (++) Erase Option Bytes |
50 | (++) Program the data Option Bytes |
50 | (++) Program the data Option Bytes |
51 | (++) Get the Write protection. |
51 | (++) Get the Write protection. |
52 | (++) Get the user option bytes. |
52 | (++) Get the user option bytes. |
53 | |
53 | |
54 | (#) Interrupts and flags management functions : this group |
54 | (#) Interrupts and flags management functions : this group |
55 | includes all needed functions to: |
55 | includes all needed functions to: |
56 | (++) Handle FLASH interrupts |
56 | (++) Handle FLASH interrupts |
57 | (++) Wait for last FLASH operation according to its status |
57 | (++) Wait for last FLASH operation according to its status |
58 | (++) Get error flag status |
58 | (++) Get error flag status |
59 | 59 | ||
60 | [..] In addition to these function, this driver includes a set of macros allowing |
60 | [..] In addition to these function, this driver includes a set of macros allowing |
61 | to handle the following operations: |
61 | to handle the following operations: |
62 | |
62 | |
63 | (+) Set/Get the latency |
63 | (+) Set/Get the latency |
64 | (+) Enable/Disable the prefetch buffer |
64 | (+) Enable/Disable the prefetch buffer |
65 | (+) Enable/Disable the half cycle access |
65 | (+) Enable/Disable the half cycle access |
66 | (+) Enable/Disable the FLASH interrupts |
66 | (+) Enable/Disable the FLASH interrupts |
67 | (+) Monitor the FLASH flags status |
67 | (+) Monitor the FLASH flags status |
68 | |
68 | |
69 | @endverbatim |
69 | @endverbatim |
70 | ****************************************************************************** |
70 | ****************************************************************************** |
71 | * @attention |
71 | * @attention |
72 | * |
72 | * |
73 | * <h2><center>© Copyright (c) 2016 STMicroelectronics. |
73 | * Copyright (c) 2016 STMicroelectronics. |
74 | * All rights reserved.</center></h2> |
74 | * All rights reserved. |
75 | * |
75 | * |
76 | * This software component is licensed by ST under BSD 3-Clause license, |
76 | * This software is licensed under terms that can be found in the LICENSE file in |
77 | * the "License"; You may not use this file except in compliance with the |
77 | * the root directory of this software component. |
78 | * License. You may obtain a copy of the License at: |
78 | * If no LICENSE file comes with this software, it is provided AS-IS. |
79 | * opensource.org/licenses/BSD-3-Clause |
79 | ****************************************************************************** |
80 | * |
80 | */ |
81 | ****************************************************************************** |
81 | |
82 | */ |
82 | /* Includes ------------------------------------------------------------------*/ |
83 | 83 | #include "stm32f1xx_hal.h" |
|
84 | /* Includes ------------------------------------------------------------------*/ |
84 | |
85 | #include "stm32f1xx_hal.h" |
85 | /** @addtogroup STM32F1xx_HAL_Driver |
86 | 86 | * @{ |
|
87 | /** @addtogroup STM32F1xx_HAL_Driver |
87 | */ |
88 | * @{ |
88 | |
89 | */ |
89 | #ifdef HAL_FLASH_MODULE_ENABLED |
90 | 90 | ||
91 | #ifdef HAL_FLASH_MODULE_ENABLED |
91 | /** @defgroup FLASH FLASH |
92 | 92 | * @brief FLASH HAL module driver |
|
93 | /** @defgroup FLASH FLASH |
93 | * @{ |
94 | * @brief FLASH HAL module driver |
94 | */ |
95 | * @{ |
95 | |
96 | */ |
96 | /* Private typedef -----------------------------------------------------------*/ |
97 | 97 | /* Private define ------------------------------------------------------------*/ |
|
98 | /* Private typedef -----------------------------------------------------------*/ |
98 | /** @defgroup FLASH_Private_Constants FLASH Private Constants |
99 | /* Private define ------------------------------------------------------------*/ |
99 | * @{ |
100 | /** @defgroup FLASH_Private_Constants FLASH Private Constants |
100 | */ |
101 | * @{ |
101 | /** |
102 | */ |
102 | * @} |
103 | /** |
103 | */ |
104 | * @} |
104 | |
105 | */ |
105 | /* Private macro ---------------------------- ---------------------------------*/ |
106 | 106 | /** @defgroup FLASH_Private_Macros FLASH Private Macros |
|
107 | /* Private macro ---------------------------- ---------------------------------*/ |
107 | * @{ |
108 | /** @defgroup FLASH_Private_Macros FLASH Private Macros |
108 | */ |
109 | * @{ |
109 | |
110 | */ |
110 | /** |
111 | 111 | * @} |
|
112 | /** |
112 | */ |
113 | * @} |
113 | |
114 | */ |
114 | /* Private variables ---------------------------------------------------------*/ |
115 | 115 | /** @defgroup FLASH_Private_Variables FLASH Private Variables |
|
116 | /* Private variables ---------------------------------------------------------*/ |
116 | * @{ |
117 | /** @defgroup FLASH_Private_Variables FLASH Private Variables |
117 | */ |
118 | * @{ |
118 | /* Variables used for Erase pages under interruption*/ |
119 | */ |
119 | FLASH_ProcessTypeDef pFlash; |
120 | /* Variables used for Erase pages under interruption*/ |
120 | /** |
121 | FLASH_ProcessTypeDef pFlash; |
121 | * @} |
122 | /** |
122 | */ |
123 | * @} |
123 | |
124 | */ |
124 | /* Private function prototypes -----------------------------------------------*/ |
125 | 125 | /** @defgroup FLASH_Private_Functions FLASH Private Functions |
|
126 | /* Private function prototypes -----------------------------------------------*/ |
126 | * @{ |
127 | /** @defgroup FLASH_Private_Functions FLASH Private Functions |
127 | */ |
128 | * @{ |
128 | static void FLASH_Program_HalfWord(uint32_t Address, uint16_t Data); |
129 | */ |
129 | static void FLASH_SetErrorCode(void); |
130 | static void FLASH_Program_HalfWord(uint32_t Address, uint16_t Data); |
130 | extern void FLASH_PageErase(uint32_t PageAddress); |
131 | static void FLASH_SetErrorCode(void); |
131 | /** |
132 | extern void FLASH_PageErase(uint32_t PageAddress); |
132 | * @} |
133 | /** |
133 | */ |
134 | * @} |
134 | |
135 | */ |
135 | /* Exported functions ---------------------------------------------------------*/ |
136 | 136 | /** @defgroup FLASH_Exported_Functions FLASH Exported Functions |
|
137 | /* Exported functions ---------------------------------------------------------*/ |
137 | * @{ |
138 | /** @defgroup FLASH_Exported_Functions FLASH Exported Functions |
138 | */ |
139 | * @{ |
139 | |
140 | */ |
140 | /** @defgroup FLASH_Exported_Functions_Group1 Programming operation functions |
141 | 141 | * @brief Programming operation functions |
|
142 | /** @defgroup FLASH_Exported_Functions_Group1 Programming operation functions |
142 | * |
143 | * @brief Programming operation functions |
143 | @verbatim |
144 | * |
144 | @endverbatim |
145 | @verbatim |
145 | * @{ |
146 | @endverbatim |
146 | */ |
147 | * @{ |
147 | |
148 | */ |
148 | /** |
149 | 149 | * @brief Program halfword, word or double word at a specified address |
|
150 | /** |
150 | * @note The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface |
151 | * @brief Program halfword, word or double word at a specified address |
151 | * The function HAL_FLASH_Lock() should be called after to lock the FLASH interface |
152 | * @note The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface |
152 | * |
153 | * The function HAL_FLASH_Lock() should be called after to lock the FLASH interface |
153 | * @note If an erase and a program operations are requested simultaneously, |
154 | * |
154 | * the erase operation is performed before the program one. |
155 | * @note If an erase and a program operations are requested simultaneously, |
155 | * |
156 | * the erase operation is performed before the program one. |
156 | * @note FLASH should be previously erased before new programmation (only exception to this |
157 | * |
157 | * is when 0x0000 is programmed) |
158 | * @note FLASH should be previously erased before new programmation (only exception to this |
158 | * |
159 | * is when 0x0000 is programmed) |
159 | * @param TypeProgram: Indicate the way to program at a specified address. |
160 | * |
160 | * This parameter can be a value of @ref FLASH_Type_Program |
161 | * @param TypeProgram: Indicate the way to program at a specified address. |
161 | * @param Address: Specifies the address to be programmed. |
162 | * This parameter can be a value of @ref FLASH_Type_Program |
162 | * @param Data: Specifies the data to be programmed |
163 | * @param Address: Specifies the address to be programmed. |
163 | * |
164 | * @param Data: Specifies the data to be programmed |
164 | * @retval HAL_StatusTypeDef HAL Status |
165 | * |
165 | */ |
166 | * @retval HAL_StatusTypeDef HAL Status |
166 | HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data) |
167 | */ |
167 | { |
168 | HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data) |
168 | HAL_StatusTypeDef status = HAL_ERROR; |
169 | { |
169 | uint8_t index = 0; |
170 | HAL_StatusTypeDef status = HAL_ERROR; |
170 | uint8_t nbiterations = 0; |
171 | uint8_t index = 0; |
171 | |
172 | uint8_t nbiterations = 0; |
172 | /* Process Locked */ |
173 | 173 | __HAL_LOCK(&pFlash); |
|
174 | /* Process Locked */ |
174 | |
175 | __HAL_LOCK(&pFlash); |
175 | /* Check the parameters */ |
176 | 176 | assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram)); |
|
177 | /* Check the parameters */ |
177 | assert_param(IS_FLASH_PROGRAM_ADDRESS(Address)); |
178 | assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram)); |
178 | |
179 | assert_param(IS_FLASH_PROGRAM_ADDRESS(Address)); |
179 | #if defined(FLASH_BANK2_END) |
180 | 180 | if(Address <= FLASH_BANK1_END) |
|
181 | #if defined(FLASH_BANK2_END) |
181 | { |
182 | if(Address <= FLASH_BANK1_END) |
182 | #endif /* FLASH_BANK2_END */ |
183 | { |
183 | /* Wait for last operation to be completed */ |
184 | #endif /* FLASH_BANK2_END */ |
184 | status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE); |
185 | /* Wait for last operation to be completed */ |
185 | #if defined(FLASH_BANK2_END) |
186 | status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE); |
186 | } |
187 | #if defined(FLASH_BANK2_END) |
187 | else |
188 | } |
188 | { |
189 | else |
189 | /* Wait for last operation to be completed */ |
190 | { |
190 | status = FLASH_WaitForLastOperationBank2(FLASH_TIMEOUT_VALUE); |
191 | /* Wait for last operation to be completed */ |
191 | } |
192 | status = FLASH_WaitForLastOperationBank2(FLASH_TIMEOUT_VALUE); |
192 | #endif /* FLASH_BANK2_END */ |
193 | } |
193 | |
194 | #endif /* FLASH_BANK2_END */ |
194 | if(status == HAL_OK) |
195 | 195 | { |
|
196 | if(status == HAL_OK) |
196 | if(TypeProgram == FLASH_TYPEPROGRAM_HALFWORD) |
197 | { |
197 | { |
198 | if(TypeProgram == FLASH_TYPEPROGRAM_HALFWORD) |
198 | /* Program halfword (16-bit) at a specified address. */ |
199 | { |
199 | nbiterations = 1U; |
200 | /* Program halfword (16-bit) at a specified address. */ |
200 | } |
201 | nbiterations = 1U; |
201 | else if(TypeProgram == FLASH_TYPEPROGRAM_WORD) |
202 | } |
202 | { |
203 | else if(TypeProgram == FLASH_TYPEPROGRAM_WORD) |
203 | /* Program word (32-bit = 2*16-bit) at a specified address. */ |
204 | { |
204 | nbiterations = 2U; |
205 | /* Program word (32-bit = 2*16-bit) at a specified address. */ |
205 | } |
206 | nbiterations = 2U; |
206 | else |
207 | } |
207 | { |
208 | else |
208 | /* Program double word (64-bit = 4*16-bit) at a specified address. */ |
209 | { |
209 | nbiterations = 4U; |
210 | /* Program double word (64-bit = 4*16-bit) at a specified address. */ |
210 | } |
211 | nbiterations = 4U; |
211 | |
212 | } |
212 | for (index = 0U; index < nbiterations; index++) |
213 | 213 | { |
|
214 | for (index = 0U; index < nbiterations; index++) |
214 | FLASH_Program_HalfWord((Address + (2U*index)), (uint16_t)(Data >> (16U*index))); |
215 | { |
215 | |
216 | FLASH_Program_HalfWord((Address + (2U*index)), (uint16_t)(Data >> (16U*index))); |
216 | #if defined(FLASH_BANK2_END) |
217 | 217 | if(Address <= FLASH_BANK1_END) |
|
218 | #if defined(FLASH_BANK2_END) |
218 | { |
219 | if(Address <= FLASH_BANK1_END) |
219 | #endif /* FLASH_BANK2_END */ |
220 | { |
220 | /* Wait for last operation to be completed */ |
221 | #endif /* FLASH_BANK2_END */ |
221 | status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE); |
222 | /* Wait for last operation to be completed */ |
222 | |
223 | status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE); |
223 | /* If the program operation is completed, disable the PG Bit */ |
224 | 224 | CLEAR_BIT(FLASH->CR, FLASH_CR_PG); |
|
225 | /* If the program operation is completed, disable the PG Bit */ |
225 | #if defined(FLASH_BANK2_END) |
226 | CLEAR_BIT(FLASH->CR, FLASH_CR_PG); |
226 | } |
227 | #if defined(FLASH_BANK2_END) |
227 | else |
228 | } |
228 | { |
229 | else |
229 | /* Wait for last operation to be completed */ |
230 | { |
230 | status = FLASH_WaitForLastOperationBank2(FLASH_TIMEOUT_VALUE); |
231 | /* Wait for last operation to be completed */ |
231 | |
232 | status = FLASH_WaitForLastOperationBank2(FLASH_TIMEOUT_VALUE); |
232 | /* If the program operation is completed, disable the PG Bit */ |
233 | 233 | CLEAR_BIT(FLASH->CR2, FLASH_CR2_PG); |
|
234 | /* If the program operation is completed, disable the PG Bit */ |
234 | } |
235 | CLEAR_BIT(FLASH->CR2, FLASH_CR2_PG); |
235 | #endif /* FLASH_BANK2_END */ |
236 | } |
236 | /* In case of error, stop programation procedure */ |
237 | #endif /* FLASH_BANK2_END */ |
237 | if (status != HAL_OK) |
238 | /* In case of error, stop programation procedure */ |
238 | { |
239 | if (status != HAL_OK) |
239 | break; |
240 | { |
240 | } |
241 | break; |
241 | } |
242 | } |
242 | } |
243 | } |
243 | |
244 | } |
244 | /* Process Unlocked */ |
245 | 245 | __HAL_UNLOCK(&pFlash); |
|
246 | /* Process Unlocked */ |
246 | |
247 | __HAL_UNLOCK(&pFlash); |
247 | return status; |
248 | 248 | } |
|
249 | return status; |
249 | |
250 | } |
250 | /** |
251 | 251 | * @brief Program halfword, word or double word at a specified address with interrupt enabled. |
|
252 | /** |
252 | * @note The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface |
253 | * @brief Program halfword, word or double word at a specified address with interrupt enabled. |
253 | * The function HAL_FLASH_Lock() should be called after to lock the FLASH interface |
254 | * @note The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface |
254 | * |
255 | * The function HAL_FLASH_Lock() should be called after to lock the FLASH interface |
255 | * @note If an erase and a program operations are requested simultaneously, |
256 | * |
256 | * the erase operation is performed before the program one. |
257 | * @note If an erase and a program operations are requested simultaneously, |
257 | * |
258 | * the erase operation is performed before the program one. |
258 | * @param TypeProgram: Indicate the way to program at a specified address. |
259 | * |
259 | * This parameter can be a value of @ref FLASH_Type_Program |
260 | * @param TypeProgram: Indicate the way to program at a specified address. |
260 | * @param Address: Specifies the address to be programmed. |
261 | * This parameter can be a value of @ref FLASH_Type_Program |
261 | * @param Data: Specifies the data to be programmed |
262 | * @param Address: Specifies the address to be programmed. |
262 | * |
263 | * @param Data: Specifies the data to be programmed |
263 | * @retval HAL_StatusTypeDef HAL Status |
264 | * |
264 | */ |
265 | * @retval HAL_StatusTypeDef HAL Status |
265 | HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data) |
266 | */ |
266 | { |
267 | HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data) |
267 | HAL_StatusTypeDef status = HAL_OK; |
268 | { |
268 | |
269 | HAL_StatusTypeDef status = HAL_OK; |
269 | /* Process Locked */ |
270 | 270 | __HAL_LOCK(&pFlash); |
|
271 | /* Process Locked */ |
271 | |
272 | __HAL_LOCK(&pFlash); |
272 | /* Check the parameters */ |
273 | 273 | assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram)); |
|
274 | /* Check the parameters */ |
274 | assert_param(IS_FLASH_PROGRAM_ADDRESS(Address)); |
275 | assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram)); |
275 | |
276 | assert_param(IS_FLASH_PROGRAM_ADDRESS(Address)); |
276 | #if defined(FLASH_BANK2_END) |
277 | 277 | /* If procedure already ongoing, reject the next one */ |
|
278 | #if defined(FLASH_BANK2_END) |
278 | if (pFlash.ProcedureOnGoing != FLASH_PROC_NONE) |
279 | /* If procedure already ongoing, reject the next one */ |
279 | { |
280 | if (pFlash.ProcedureOnGoing != FLASH_PROC_NONE) |
280 | return HAL_ERROR; |
281 | { |
281 | } |
282 | return HAL_ERROR; |
282 | |
283 | } |
283 | if(Address <= FLASH_BANK1_END) |
284 | 284 | { |
|
285 | if(Address <= FLASH_BANK1_END) |
285 | /* Enable End of FLASH Operation and Error source interrupts */ |
286 | { |
286 | __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP_BANK1 | FLASH_IT_ERR_BANK1); |
287 | /* Enable End of FLASH Operation and Error source interrupts */ |
287 | |
288 | __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP_BANK1 | FLASH_IT_ERR_BANK1); |
288 | }else |
289 | 289 | { |
|
290 | }else |
290 | /* Enable End of FLASH Operation and Error source interrupts */ |
291 | { |
291 | __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP_BANK2 | FLASH_IT_ERR_BANK2); |
292 | /* Enable End of FLASH Operation and Error source interrupts */ |
292 | } |
293 | __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP_BANK2 | FLASH_IT_ERR_BANK2); |
293 | #else |
294 | } |
294 | /* Enable End of FLASH Operation and Error source interrupts */ |
295 | #else |
295 | __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP | FLASH_IT_ERR); |
296 | /* Enable End of FLASH Operation and Error source interrupts */ |
296 | #endif /* FLASH_BANK2_END */ |
297 | __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP | FLASH_IT_ERR); |
297 | |
298 | #endif /* FLASH_BANK2_END */ |
298 | pFlash.Address = Address; |
299 | 299 | pFlash.Data = Data; |
|
300 | pFlash.Address = Address; |
300 | |
301 | pFlash.Data = Data; |
301 | if(TypeProgram == FLASH_TYPEPROGRAM_HALFWORD) |
302 | 302 | { |
|
303 | if(TypeProgram == FLASH_TYPEPROGRAM_HALFWORD) |
303 | pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAMHALFWORD; |
304 | { |
304 | /* Program halfword (16-bit) at a specified address. */ |
305 | pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAMHALFWORD; |
305 | pFlash.DataRemaining = 1U; |
306 | /* Program halfword (16-bit) at a specified address. */ |
306 | } |
307 | pFlash.DataRemaining = 1U; |
307 | else if(TypeProgram == FLASH_TYPEPROGRAM_WORD) |
308 | } |
308 | { |
309 | else if(TypeProgram == FLASH_TYPEPROGRAM_WORD) |
309 | pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAMWORD; |
310 | { |
310 | /* Program word (32-bit : 2*16-bit) at a specified address. */ |
311 | pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAMWORD; |
311 | pFlash.DataRemaining = 2U; |
312 | /* Program word (32-bit : 2*16-bit) at a specified address. */ |
312 | } |
313 | pFlash.DataRemaining = 2U; |
313 | else |
314 | } |
314 | { |
315 | else |
315 | pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAMDOUBLEWORD; |
316 | { |
316 | /* Program double word (64-bit : 4*16-bit) at a specified address. */ |
317 | pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAMDOUBLEWORD; |
317 | pFlash.DataRemaining = 4U; |
318 | /* Program double word (64-bit : 4*16-bit) at a specified address. */ |
318 | } |
319 | pFlash.DataRemaining = 4U; |
319 | |
320 | } |
320 | /* Program halfword (16-bit) at a specified address. */ |
321 | 321 | FLASH_Program_HalfWord(Address, (uint16_t)Data); |
|
322 | /* Program halfword (16-bit) at a specified address. */ |
322 | |
323 | FLASH_Program_HalfWord(Address, (uint16_t)Data); |
323 | return status; |
324 | 324 | } |
|
325 | return status; |
325 | |
326 | } |
326 | /** |
327 | 327 | * @brief This function handles FLASH interrupt request. |
|
328 | /** |
328 | * @retval None |
329 | * @brief This function handles FLASH interrupt request. |
329 | */ |
330 | * @retval None |
330 | void HAL_FLASH_IRQHandler(void) |
331 | */ |
331 | { |
332 | void HAL_FLASH_IRQHandler(void) |
332 | uint32_t addresstmp = 0U; |
333 | { |
333 | |
334 | uint32_t addresstmp = 0U; |
334 | /* Check FLASH operation error flags */ |
335 | 335 | #if defined(FLASH_BANK2_END) |
|
336 | /* Check FLASH operation error flags */ |
336 | if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR_BANK1) || __HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR_BANK1) || \ |
337 | #if defined(FLASH_BANK2_END) |
337 | (__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR_BANK2) || __HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR_BANK2))) |
338 | if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR_BANK1) || __HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR_BANK1) || \ |
338 | #else |
339 | (__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR_BANK2) || __HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR_BANK2))) |
339 | if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) ||__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR)) |
340 | #else |
340 | #endif /* FLASH_BANK2_END */ |
341 | if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) ||__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR)) |
341 | { |
342 | #endif /* FLASH_BANK2_END */ |
342 | /* Return the faulty address */ |
343 | { |
343 | addresstmp = pFlash.Address; |
344 | /* Return the faulty address */ |
344 | /* Reset address */ |
345 | addresstmp = pFlash.Address; |
345 | pFlash.Address = 0xFFFFFFFFU; |
346 | /* Reset address */ |
346 | |
347 | pFlash.Address = 0xFFFFFFFFU; |
347 | /* Save the Error code */ |
348 | 348 | FLASH_SetErrorCode(); |
|
349 | /* Save the Error code */ |
349 | |
350 | FLASH_SetErrorCode(); |
350 | /* FLASH error interrupt user callback */ |
351 | 351 | HAL_FLASH_OperationErrorCallback(addresstmp); |
|
352 | /* FLASH error interrupt user callback */ |
352 | |
353 | HAL_FLASH_OperationErrorCallback(addresstmp); |
353 | /* Stop the procedure ongoing */ |
354 | 354 | pFlash.ProcedureOnGoing = FLASH_PROC_NONE; |
|
355 | /* Stop the procedure ongoing */ |
355 | } |
356 | pFlash.ProcedureOnGoing = FLASH_PROC_NONE; |
356 | |
357 | } |
357 | /* Check FLASH End of Operation flag */ |
358 | 358 | #if defined(FLASH_BANK2_END) |
|
359 | /* Check FLASH End of Operation flag */ |
359 | if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP_BANK1)) |
360 | #if defined(FLASH_BANK2_END) |
360 | { |
361 | if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP_BANK1)) |
361 | /* Clear FLASH End of Operation pending bit */ |
362 | { |
362 | __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP_BANK1); |
363 | /* Clear FLASH End of Operation pending bit */ |
363 | #else |
364 | __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP_BANK1); |
364 | if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP)) |
365 | #else |
365 | { |
366 | if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP)) |
366 | /* Clear FLASH End of Operation pending bit */ |
367 | { |
367 | __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP); |
368 | /* Clear FLASH End of Operation pending bit */ |
368 | #endif /* FLASH_BANK2_END */ |
369 | __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP); |
369 | |
370 | #endif /* FLASH_BANK2_END */ |
370 | /* Process can continue only if no error detected */ |
371 | 371 | if(pFlash.ProcedureOnGoing != FLASH_PROC_NONE) |
|
372 | /* Process can continue only if no error detected */ |
372 | { |
373 | if(pFlash.ProcedureOnGoing != FLASH_PROC_NONE) |
373 | if(pFlash.ProcedureOnGoing == FLASH_PROC_PAGEERASE) |
374 | { |
374 | { |
375 | if(pFlash.ProcedureOnGoing == FLASH_PROC_PAGEERASE) |
375 | /* Nb of pages to erased can be decreased */ |
376 | { |
376 | pFlash.DataRemaining--; |
377 | /* Nb of pages to erased can be decreased */ |
377 | |
378 | pFlash.DataRemaining--; |
378 | /* Check if there are still pages to erase */ |
379 | 379 | if(pFlash.DataRemaining != 0U) |
|
380 | /* Check if there are still pages to erase */ |
380 | { |
381 | if(pFlash.DataRemaining != 0U) |
381 | addresstmp = pFlash.Address; |
382 | { |
382 | /*Indicate user which sector has been erased */ |
383 | addresstmp = pFlash.Address; |
383 | HAL_FLASH_EndOfOperationCallback(addresstmp); |
384 | /*Indicate user which sector has been erased */ |
384 | |
385 | HAL_FLASH_EndOfOperationCallback(addresstmp); |
385 | /*Increment sector number*/ |
386 | 386 | addresstmp = pFlash.Address + FLASH_PAGE_SIZE; |
|
387 | /*Increment sector number*/ |
387 | pFlash.Address = addresstmp; |
388 | addresstmp = pFlash.Address + FLASH_PAGE_SIZE; |
388 | |
389 | pFlash.Address = addresstmp; |
389 | /* If the erase operation is completed, disable the PER Bit */ |
390 | 390 | CLEAR_BIT(FLASH->CR, FLASH_CR_PER); |
|
391 | /* If the erase operation is completed, disable the PER Bit */ |
391 | |
392 | CLEAR_BIT(FLASH->CR, FLASH_CR_PER); |
392 | FLASH_PageErase(addresstmp); |
393 | 393 | } |
|
394 | FLASH_PageErase(addresstmp); |
394 | else |
395 | } |
395 | { |
396 | else |
396 | /* No more pages to Erase, user callback can be called. */ |
397 | { |
397 | /* Reset Sector and stop Erase pages procedure */ |
398 | /* No more pages to Erase, user callback can be called. */ |
398 | pFlash.Address = addresstmp = 0xFFFFFFFFU; |
399 | /* Reset Sector and stop Erase pages procedure */ |
399 | pFlash.ProcedureOnGoing = FLASH_PROC_NONE; |
400 | pFlash.Address = addresstmp = 0xFFFFFFFFU; |
400 | /* FLASH EOP interrupt user callback */ |
401 | pFlash.ProcedureOnGoing = FLASH_PROC_NONE; |
401 | HAL_FLASH_EndOfOperationCallback(addresstmp); |
402 | /* FLASH EOP interrupt user callback */ |
402 | } |
403 | HAL_FLASH_EndOfOperationCallback(addresstmp); |
403 | } |
404 | } |
404 | else if(pFlash.ProcedureOnGoing == FLASH_PROC_MASSERASE) |
405 | } |
405 | { |
406 | else if(pFlash.ProcedureOnGoing == FLASH_PROC_MASSERASE) |
406 | /* Operation is completed, disable the MER Bit */ |
407 | { |
407 | CLEAR_BIT(FLASH->CR, FLASH_CR_MER); |
408 | /* Operation is completed, disable the MER Bit */ |
408 | |
409 | CLEAR_BIT(FLASH->CR, FLASH_CR_MER); |
409 | #if defined(FLASH_BANK2_END) |
410 | 410 | /* Stop Mass Erase procedure if no pending mass erase on other bank */ |
|
411 | #if defined(FLASH_BANK2_END) |
411 | if (HAL_IS_BIT_CLR(FLASH->CR2, FLASH_CR2_MER)) |
412 | /* Stop Mass Erase procedure if no pending mass erase on other bank */ |
412 | { |
413 | if (HAL_IS_BIT_CLR(FLASH->CR2, FLASH_CR2_MER)) |
413 | #endif /* FLASH_BANK2_END */ |
414 | { |
414 | /* MassErase ended. Return the selected bank */ |
415 | #endif /* FLASH_BANK2_END */ |
415 | /* FLASH EOP interrupt user callback */ |
416 | /* MassErase ended. Return the selected bank */ |
416 | HAL_FLASH_EndOfOperationCallback(0U); |
417 | /* FLASH EOP interrupt user callback */ |
417 | |
418 | HAL_FLASH_EndOfOperationCallback(0U); |
418 | /* Stop Mass Erase procedure*/ |
419 | 419 | pFlash.ProcedureOnGoing = FLASH_PROC_NONE; |
|
420 | /* Stop Mass Erase procedure*/ |
420 | } |
421 | pFlash.ProcedureOnGoing = FLASH_PROC_NONE; |
421 | #if defined(FLASH_BANK2_END) |
422 | } |
422 | } |
423 | #if defined(FLASH_BANK2_END) |
423 | #endif /* FLASH_BANK2_END */ |
424 | } |
424 | else |
425 | #endif /* FLASH_BANK2_END */ |
425 | { |
426 | else |
426 | /* Nb of 16-bit data to program can be decreased */ |
427 | { |
427 | pFlash.DataRemaining--; |
428 | /* Nb of 16-bit data to program can be decreased */ |
428 | |
429 | pFlash.DataRemaining--; |
429 | /* Check if there are still 16-bit data to program */ |
430 | 430 | if(pFlash.DataRemaining != 0U) |
|
431 | /* Check if there are still 16-bit data to program */ |
431 | { |
432 | if(pFlash.DataRemaining != 0U) |
432 | /* Increment address to 16-bit */ |
433 | { |
433 | pFlash.Address += 2U; |
434 | /* Increment address to 16-bit */ |
434 | addresstmp = pFlash.Address; |
435 | pFlash.Address += 2U; |
435 | |
436 | addresstmp = pFlash.Address; |
436 | /* Shift to have next 16-bit data */ |
437 | 437 | pFlash.Data = (pFlash.Data >> 16U); |
|
438 | /* Shift to have next 16-bit data */ |
438 | |
439 | pFlash.Data = (pFlash.Data >> 16U); |
439 | /* Operation is completed, disable the PG Bit */ |
440 | 440 | CLEAR_BIT(FLASH->CR, FLASH_CR_PG); |
|
441 | /* Operation is completed, disable the PG Bit */ |
441 | |
442 | CLEAR_BIT(FLASH->CR, FLASH_CR_PG); |
442 | /*Program halfword (16-bit) at a specified address.*/ |
443 | 443 | FLASH_Program_HalfWord(addresstmp, (uint16_t)pFlash.Data); |
|
444 | /*Program halfword (16-bit) at a specified address.*/ |
444 | } |
445 | FLASH_Program_HalfWord(addresstmp, (uint16_t)pFlash.Data); |
445 | else |
446 | } |
446 | { |
447 | else |
447 | /* Program ended. Return the selected address */ |
448 | { |
448 | /* FLASH EOP interrupt user callback */ |
449 | /* Program ended. Return the selected address */ |
449 | if (pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAMHALFWORD) |
450 | /* FLASH EOP interrupt user callback */ |
450 | { |
451 | if (pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAMHALFWORD) |
451 | HAL_FLASH_EndOfOperationCallback(pFlash.Address); |
452 | { |
452 | } |
453 | HAL_FLASH_EndOfOperationCallback(pFlash.Address); |
453 | else if (pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAMWORD) |
454 | } |
454 | { |
455 | else if (pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAMWORD) |
455 | HAL_FLASH_EndOfOperationCallback(pFlash.Address - 2U); |
456 | { |
456 | } |
457 | HAL_FLASH_EndOfOperationCallback(pFlash.Address - 2U); |
457 | else |
458 | } |
458 | { |
459 | else |
459 | HAL_FLASH_EndOfOperationCallback(pFlash.Address - 6U); |
460 | { |
460 | } |
461 | HAL_FLASH_EndOfOperationCallback(pFlash.Address - 6U); |
461 | |
462 | } |
462 | /* Reset Address and stop Program procedure */ |
463 | 463 | pFlash.Address = 0xFFFFFFFFU; |
|
464 | /* Reset Address and stop Program procedure */ |
464 | pFlash.ProcedureOnGoing = FLASH_PROC_NONE; |
465 | pFlash.Address = 0xFFFFFFFFU; |
465 | } |
466 | pFlash.ProcedureOnGoing = FLASH_PROC_NONE; |
466 | } |
467 | } |
467 | } |
468 | } |
468 | } |
469 | } |
469 | |
470 | } |
470 | #if defined(FLASH_BANK2_END) |
471 | 471 | /* Check FLASH End of Operation flag */ |
|
472 | #if defined(FLASH_BANK2_END) |
472 | if(__HAL_FLASH_GET_FLAG( FLASH_FLAG_EOP_BANK2)) |
473 | /* Check FLASH End of Operation flag */ |
473 | { |
474 | if(__HAL_FLASH_GET_FLAG( FLASH_FLAG_EOP_BANK2)) |
474 | /* Clear FLASH End of Operation pending bit */ |
475 | { |
475 | __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP_BANK2); |
476 | /* Clear FLASH End of Operation pending bit */ |
476 | |
477 | __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP_BANK2); |
477 | /* Process can continue only if no error detected */ |
478 | 478 | if(pFlash.ProcedureOnGoing != FLASH_PROC_NONE) |
|
479 | /* Process can continue only if no error detected */ |
479 | { |
480 | if(pFlash.ProcedureOnGoing != FLASH_PROC_NONE) |
480 | if(pFlash.ProcedureOnGoing == FLASH_PROC_PAGEERASE) |
481 | { |
481 | { |
482 | if(pFlash.ProcedureOnGoing == FLASH_PROC_PAGEERASE) |
482 | /* Nb of pages to erased can be decreased */ |
483 | { |
483 | pFlash.DataRemaining--; |
484 | /* Nb of pages to erased can be decreased */ |
484 | |
485 | pFlash.DataRemaining--; |
485 | /* Check if there are still pages to erase*/ |
486 | 486 | if(pFlash.DataRemaining != 0U) |
|
487 | /* Check if there are still pages to erase*/ |
487 | { |
488 | if(pFlash.DataRemaining != 0U) |
488 | /* Indicate user which page address has been erased*/ |
489 | { |
489 | HAL_FLASH_EndOfOperationCallback(pFlash.Address); |
490 | /* Indicate user which page address has been erased*/ |
490 | |
491 | HAL_FLASH_EndOfOperationCallback(pFlash.Address); |
491 | /* Increment page address to next page */ |
492 | 492 | pFlash.Address += FLASH_PAGE_SIZE; |
|
493 | /* Increment page address to next page */ |
493 | addresstmp = pFlash.Address; |
494 | pFlash.Address += FLASH_PAGE_SIZE; |
494 | |
495 | addresstmp = pFlash.Address; |
495 | /* Operation is completed, disable the PER Bit */ |
496 | 496 | CLEAR_BIT(FLASH->CR2, FLASH_CR2_PER); |
|
497 | /* Operation is completed, disable the PER Bit */ |
497 | |
498 | CLEAR_BIT(FLASH->CR2, FLASH_CR2_PER); |
498 | FLASH_PageErase(addresstmp); |
499 | 499 | } |
|
500 | FLASH_PageErase(addresstmp); |
500 | else |
501 | } |
501 | { |
502 | else |
502 | /*No more pages to Erase*/ |
503 | { |
503 | |
504 | /*No more pages to Erase*/ |
504 | /*Reset Address and stop Erase pages procedure*/ |
505 | 505 | pFlash.Address = 0xFFFFFFFFU; |
|
506 | /*Reset Address and stop Erase pages procedure*/ |
506 | pFlash.ProcedureOnGoing = FLASH_PROC_NONE; |
507 | pFlash.Address = 0xFFFFFFFFU; |
507 | |
508 | pFlash.ProcedureOnGoing = FLASH_PROC_NONE; |
508 | /* FLASH EOP interrupt user callback */ |
509 | 509 | HAL_FLASH_EndOfOperationCallback(pFlash.Address); |
|
510 | /* FLASH EOP interrupt user callback */ |
510 | } |
511 | HAL_FLASH_EndOfOperationCallback(pFlash.Address); |
511 | } |
512 | } |
512 | else if(pFlash.ProcedureOnGoing == FLASH_PROC_MASSERASE) |
513 | } |
513 | { |
514 | else if(pFlash.ProcedureOnGoing == FLASH_PROC_MASSERASE) |
514 | /* Operation is completed, disable the MER Bit */ |
515 | { |
515 | CLEAR_BIT(FLASH->CR2, FLASH_CR2_MER); |
516 | /* Operation is completed, disable the MER Bit */ |
516 | |
517 | CLEAR_BIT(FLASH->CR2, FLASH_CR2_MER); |
517 | if (HAL_IS_BIT_CLR(FLASH->CR, FLASH_CR_MER)) |
518 | 518 | { |
|
519 | if (HAL_IS_BIT_CLR(FLASH->CR, FLASH_CR_MER)) |
519 | /* MassErase ended. Return the selected bank*/ |
520 | { |
520 | /* FLASH EOP interrupt user callback */ |
521 | /* MassErase ended. Return the selected bank*/ |
521 | HAL_FLASH_EndOfOperationCallback(0U); |
522 | /* FLASH EOP interrupt user callback */ |
522 | |
523 | HAL_FLASH_EndOfOperationCallback(0U); |
523 | pFlash.ProcedureOnGoing = FLASH_PROC_NONE; |
524 | 524 | } |
|
525 | pFlash.ProcedureOnGoing = FLASH_PROC_NONE; |
525 | } |
526 | } |
526 | else |
527 | } |
527 | { |
528 | else |
528 | /* Nb of 16-bit data to program can be decreased */ |
529 | { |
529 | pFlash.DataRemaining--; |
530 | /* Nb of 16-bit data to program can be decreased */ |
530 | |
531 | pFlash.DataRemaining--; |
531 | /* Check if there are still 16-bit data to program */ |
532 | 532 | if(pFlash.DataRemaining != 0U) |
|
533 | /* Check if there are still 16-bit data to program */ |
533 | { |
534 | if(pFlash.DataRemaining != 0U) |
534 | /* Increment address to 16-bit */ |
535 | { |
535 | pFlash.Address += 2U; |
536 | /* Increment address to 16-bit */ |
536 | addresstmp = pFlash.Address; |
537 | pFlash.Address += 2U; |
537 | |
538 | addresstmp = pFlash.Address; |
538 | /* Shift to have next 16-bit data */ |
539 | 539 | pFlash.Data = (pFlash.Data >> 16U); |
|
540 | /* Shift to have next 16-bit data */ |
540 | |
541 | pFlash.Data = (pFlash.Data >> 16U); |
541 | /* Operation is completed, disable the PG Bit */ |
542 | 542 | CLEAR_BIT(FLASH->CR2, FLASH_CR2_PG); |
|
543 | /* Operation is completed, disable the PG Bit */ |
543 | |
544 | CLEAR_BIT(FLASH->CR2, FLASH_CR2_PG); |
544 | /*Program halfword (16-bit) at a specified address.*/ |
545 | 545 | FLASH_Program_HalfWord(addresstmp, (uint16_t)pFlash.Data); |
|
546 | /*Program halfword (16-bit) at a specified address.*/ |
546 | } |
547 | FLASH_Program_HalfWord(addresstmp, (uint16_t)pFlash.Data); |
547 | else |
548 | } |
548 | { |
549 | else |
549 | /*Program ended. Return the selected address*/ |
550 | { |
550 | /* FLASH EOP interrupt user callback */ |
551 | /*Program ended. Return the selected address*/ |
551 | if (pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAMHALFWORD) |
552 | /* FLASH EOP interrupt user callback */ |
552 | { |
553 | if (pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAMHALFWORD) |
553 | HAL_FLASH_EndOfOperationCallback(pFlash.Address); |
554 | { |
554 | } |
555 | HAL_FLASH_EndOfOperationCallback(pFlash.Address); |
555 | else if (pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAMWORD) |
556 | } |
556 | { |
557 | else if (pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAMWORD) |
557 | HAL_FLASH_EndOfOperationCallback(pFlash.Address-2U); |
558 | { |
558 | } |
559 | HAL_FLASH_EndOfOperationCallback(pFlash.Address-2U); |
559 | else |
560 | } |
560 | { |
561 | else |
561 | HAL_FLASH_EndOfOperationCallback(pFlash.Address-6U); |
562 | { |
562 | } |
563 | HAL_FLASH_EndOfOperationCallback(pFlash.Address-6U); |
563 | |
564 | } |
564 | /* Reset Address and stop Program procedure*/ |
565 | 565 | pFlash.Address = 0xFFFFFFFFU; |
|
566 | /* Reset Address and stop Program procedure*/ |
566 | pFlash.ProcedureOnGoing = FLASH_PROC_NONE; |
567 | pFlash.Address = 0xFFFFFFFFU; |
567 | } |
568 | pFlash.ProcedureOnGoing = FLASH_PROC_NONE; |
568 | } |
569 | } |
569 | } |
570 | } |
570 | } |
571 | } |
571 | #endif |
572 | } |
572 | |
573 | #endif |
573 | if(pFlash.ProcedureOnGoing == FLASH_PROC_NONE) |
574 | 574 | { |
|
575 | if(pFlash.ProcedureOnGoing == FLASH_PROC_NONE) |
575 | #if defined(FLASH_BANK2_END) |
576 | { |
576 | /* Operation is completed, disable the PG, PER and MER Bits for both bank */ |
577 | #if defined(FLASH_BANK2_END) |
577 | CLEAR_BIT(FLASH->CR, (FLASH_CR_PG | FLASH_CR_PER | FLASH_CR_MER)); |
578 | /* Operation is completed, disable the PG, PER and MER Bits for both bank */ |
578 | CLEAR_BIT(FLASH->CR2, (FLASH_CR2_PG | FLASH_CR2_PER | FLASH_CR2_MER)); |
579 | CLEAR_BIT(FLASH->CR, (FLASH_CR_PG | FLASH_CR_PER | FLASH_CR_MER)); |
579 | |
580 | CLEAR_BIT(FLASH->CR2, (FLASH_CR2_PG | FLASH_CR2_PER | FLASH_CR2_MER)); |
580 | /* Disable End of FLASH Operation and Error source interrupts for both banks */ |
581 | 581 | __HAL_FLASH_DISABLE_IT(FLASH_IT_EOP_BANK1 | FLASH_IT_ERR_BANK1 | FLASH_IT_EOP_BANK2 | FLASH_IT_ERR_BANK2); |
|
582 | /* Disable End of FLASH Operation and Error source interrupts for both banks */ |
582 | #else |
583 | __HAL_FLASH_DISABLE_IT(FLASH_IT_EOP_BANK1 | FLASH_IT_ERR_BANK1 | FLASH_IT_EOP_BANK2 | FLASH_IT_ERR_BANK2); |
583 | /* Operation is completed, disable the PG, PER and MER Bits */ |
584 | #else |
584 | CLEAR_BIT(FLASH->CR, (FLASH_CR_PG | FLASH_CR_PER | FLASH_CR_MER)); |
585 | /* Operation is completed, disable the PG, PER and MER Bits */ |
585 | |
586 | CLEAR_BIT(FLASH->CR, (FLASH_CR_PG | FLASH_CR_PER | FLASH_CR_MER)); |
586 | /* Disable End of FLASH Operation and Error source interrupts */ |
587 | 587 | __HAL_FLASH_DISABLE_IT(FLASH_IT_EOP | FLASH_IT_ERR); |
|
588 | /* Disable End of FLASH Operation and Error source interrupts */ |
588 | #endif /* FLASH_BANK2_END */ |
589 | __HAL_FLASH_DISABLE_IT(FLASH_IT_EOP | FLASH_IT_ERR); |
589 | |
590 | #endif /* FLASH_BANK2_END */ |
590 | /* Process Unlocked */ |
591 | 591 | __HAL_UNLOCK(&pFlash); |
|
592 | /* Process Unlocked */ |
592 | } |
593 | __HAL_UNLOCK(&pFlash); |
593 | } |
594 | } |
594 | |
595 | } |
595 | /** |
596 | 596 | * @brief FLASH end of operation interrupt callback |
|
597 | /** |
597 | * @param ReturnValue: The value saved in this parameter depends on the ongoing procedure |
598 | * @brief FLASH end of operation interrupt callback |
598 | * - Mass Erase: No return value expected |
599 | * @param ReturnValue: The value saved in this parameter depends on the ongoing procedure |
599 | * - Pages Erase: Address of the page which has been erased |
600 | * - Mass Erase: No return value expected |
600 | * (if 0xFFFFFFFF, it means that all the selected pages have been erased) |
601 | * - Pages Erase: Address of the page which has been erased |
601 | * - Program: Address which was selected for data program |
602 | * (if 0xFFFFFFFF, it means that all the selected pages have been erased) |
602 | * @retval none |
603 | * - Program: Address which was selected for data program |
603 | */ |
604 | * @retval none |
604 | __weak void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue) |
605 | */ |
605 | { |
606 | __weak void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue) |
606 | /* Prevent unused argument(s) compilation warning */ |
607 | { |
607 | UNUSED(ReturnValue); |
608 | /* Prevent unused argument(s) compilation warning */ |
608 | |
609 | UNUSED(ReturnValue); |
609 | /* NOTE : This function Should not be modified, when the callback is needed, |
610 | 610 | the HAL_FLASH_EndOfOperationCallback could be implemented in the user file |
|
611 | /* NOTE : This function Should not be modified, when the callback is needed, |
611 | */ |
612 | the HAL_FLASH_EndOfOperationCallback could be implemented in the user file |
612 | } |
613 | */ |
613 | |
614 | } |
614 | /** |
615 | 615 | * @brief FLASH operation error interrupt callback |
|
616 | /** |
616 | * @param ReturnValue: The value saved in this parameter depends on the ongoing procedure |
617 | * @brief FLASH operation error interrupt callback |
617 | * - Mass Erase: No return value expected |
618 | * @param ReturnValue: The value saved in this parameter depends on the ongoing procedure |
618 | * - Pages Erase: Address of the page which returned an error |
619 | * - Mass Erase: No return value expected |
619 | * - Program: Address which was selected for data program |
620 | * - Pages Erase: Address of the page which returned an error |
620 | * @retval none |
621 | * - Program: Address which was selected for data program |
621 | */ |
622 | * @retval none |
622 | __weak void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue) |
623 | */ |
623 | { |
624 | __weak void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue) |
624 | /* Prevent unused argument(s) compilation warning */ |
625 | { |
625 | UNUSED(ReturnValue); |
626 | /* Prevent unused argument(s) compilation warning */ |
626 | |
627 | UNUSED(ReturnValue); |
627 | /* NOTE : This function Should not be modified, when the callback is needed, |
628 | 628 | the HAL_FLASH_OperationErrorCallback could be implemented in the user file |
|
629 | /* NOTE : This function Should not be modified, when the callback is needed, |
629 | */ |
630 | the HAL_FLASH_OperationErrorCallback could be implemented in the user file |
630 | } |
631 | */ |
631 | |
632 | } |
632 | /** |
633 | 633 | * @} |
|
634 | /** |
634 | */ |
635 | * @} |
635 | |
636 | */ |
636 | /** @defgroup FLASH_Exported_Functions_Group2 Peripheral Control functions |
637 | 637 | * @brief management functions |
|
638 | /** @defgroup FLASH_Exported_Functions_Group2 Peripheral Control functions |
638 | * |
639 | * @brief management functions |
639 | @verbatim |
640 | * |
640 | =============================================================================== |
641 | @verbatim |
641 | ##### Peripheral Control functions ##### |
642 | =============================================================================== |
642 | =============================================================================== |
643 | ##### Peripheral Control functions ##### |
643 | [..] |
644 | =============================================================================== |
644 | This subsection provides a set of functions allowing to control the FLASH |
645 | [..] |
645 | memory operations. |
646 | This subsection provides a set of functions allowing to control the FLASH |
646 | |
647 | memory operations. |
647 | @endverbatim |
648 | 648 | * @{ |
|
649 | @endverbatim |
649 | */ |
650 | * @{ |
650 | |
651 | */ |
651 | /** |
652 | 652 | * @brief Unlock the FLASH control register access |
|
653 | /** |
653 | * @retval HAL Status |
654 | * @brief Unlock the FLASH control register access |
654 | */ |
655 | * @retval HAL Status |
655 | HAL_StatusTypeDef HAL_FLASH_Unlock(void) |
656 | */ |
656 | { |
657 | HAL_StatusTypeDef HAL_FLASH_Unlock(void) |
657 | HAL_StatusTypeDef status = HAL_OK; |
658 | { |
658 | |
659 | HAL_StatusTypeDef status = HAL_OK; |
659 | if(READ_BIT(FLASH->CR, FLASH_CR_LOCK) != RESET) |
660 | 660 | { |
|
661 | if(READ_BIT(FLASH->CR, FLASH_CR_LOCK) != RESET) |
661 | /* Authorize the FLASH Registers access */ |
662 | { |
662 | WRITE_REG(FLASH->KEYR, FLASH_KEY1); |
663 | /* Authorize the FLASH Registers access */ |
663 | WRITE_REG(FLASH->KEYR, FLASH_KEY2); |
664 | WRITE_REG(FLASH->KEYR, FLASH_KEY1); |
664 | |
665 | WRITE_REG(FLASH->KEYR, FLASH_KEY2); |
665 | /* Verify Flash is unlocked */ |
666 | 666 | if(READ_BIT(FLASH->CR, FLASH_CR_LOCK) != RESET) |
|
667 | /* Verify Flash is unlocked */ |
667 | { |
668 | if(READ_BIT(FLASH->CR, FLASH_CR_LOCK) != RESET) |
668 | status = HAL_ERROR; |
669 | { |
669 | } |
670 | status = HAL_ERROR; |
670 | } |
671 | } |
671 | #if defined(FLASH_BANK2_END) |
672 | } |
672 | if(READ_BIT(FLASH->CR2, FLASH_CR2_LOCK) != RESET) |
673 | #if defined(FLASH_BANK2_END) |
673 | { |
674 | if(READ_BIT(FLASH->CR2, FLASH_CR2_LOCK) != RESET) |
674 | /* Authorize the FLASH BANK2 Registers access */ |
675 | { |
675 | WRITE_REG(FLASH->KEYR2, FLASH_KEY1); |
676 | /* Authorize the FLASH BANK2 Registers access */ |
676 | WRITE_REG(FLASH->KEYR2, FLASH_KEY2); |
677 | WRITE_REG(FLASH->KEYR2, FLASH_KEY1); |
677 | |
678 | WRITE_REG(FLASH->KEYR2, FLASH_KEY2); |
678 | /* Verify Flash BANK2 is unlocked */ |
679 | 679 | if(READ_BIT(FLASH->CR2, FLASH_CR2_LOCK) != RESET) |
|
680 | /* Verify Flash BANK2 is unlocked */ |
680 | { |
681 | if(READ_BIT(FLASH->CR2, FLASH_CR2_LOCK) != RESET) |
681 | status = HAL_ERROR; |
682 | { |
682 | } |
683 | status = HAL_ERROR; |
683 | } |
684 | } |
684 | #endif /* FLASH_BANK2_END */ |
685 | } |
685 | |
686 | #endif /* FLASH_BANK2_END */ |
686 | return status; |
687 | 687 | } |
|
688 | return status; |
688 | |
689 | } |
689 | /** |
690 | 690 | * @brief Locks the FLASH control register access |
|
691 | /** |
691 | * @retval HAL Status |
692 | * @brief Locks the FLASH control register access |
692 | */ |
693 | * @retval HAL Status |
693 | HAL_StatusTypeDef HAL_FLASH_Lock(void) |
694 | */ |
694 | { |
695 | HAL_StatusTypeDef HAL_FLASH_Lock(void) |
695 | /* Set the LOCK Bit to lock the FLASH Registers access */ |
696 | { |
696 | SET_BIT(FLASH->CR, FLASH_CR_LOCK); |
697 | /* Set the LOCK Bit to lock the FLASH Registers access */ |
697 | |
698 | SET_BIT(FLASH->CR, FLASH_CR_LOCK); |
698 | #if defined(FLASH_BANK2_END) |
699 | 699 | /* Set the LOCK Bit to lock the FLASH BANK2 Registers access */ |
|
700 | #if defined(FLASH_BANK2_END) |
700 | SET_BIT(FLASH->CR2, FLASH_CR2_LOCK); |
701 | /* Set the LOCK Bit to lock the FLASH BANK2 Registers access */ |
701 | |
702 | SET_BIT(FLASH->CR2, FLASH_CR2_LOCK); |
702 | #endif /* FLASH_BANK2_END */ |
703 | 703 | return HAL_OK; |
|
704 | #endif /* FLASH_BANK2_END */ |
704 | } |
705 | return HAL_OK; |
705 | |
706 | } |
706 | /** |
707 | 707 | * @brief Unlock the FLASH Option Control Registers access. |
|
708 | /** |
708 | * @retval HAL Status |
709 | * @brief Unlock the FLASH Option Control Registers access. |
709 | */ |
710 | * @retval HAL Status |
710 | HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void) |
711 | */ |
711 | { |
712 | HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void) |
712 | if (HAL_IS_BIT_CLR(FLASH->CR, FLASH_CR_OPTWRE)) |
713 | { |
713 | { |
714 | if (HAL_IS_BIT_CLR(FLASH->CR, FLASH_CR_OPTWRE)) |
714 | /* Authorizes the Option Byte register programming */ |
715 | { |
715 | WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY1); |
716 | /* Authorizes the Option Byte register programming */ |
716 | WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY2); |
717 | WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY1); |
717 | } |
718 | WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY2); |
718 | else |
719 | } |
719 | { |
720 | else |
720 | return HAL_ERROR; |
721 | { |
721 | } |
722 | return HAL_ERROR; |
722 | |
723 | } |
723 | return HAL_OK; |
724 | 724 | } |
|
725 | return HAL_OK; |
725 | |
726 | } |
726 | /** |
727 | 727 | * @brief Lock the FLASH Option Control Registers access. |
|
728 | /** |
728 | * @retval HAL Status |
729 | * @brief Lock the FLASH Option Control Registers access. |
729 | */ |
730 | * @retval HAL Status |
730 | HAL_StatusTypeDef HAL_FLASH_OB_Lock(void) |
731 | */ |
731 | { |
732 | HAL_StatusTypeDef HAL_FLASH_OB_Lock(void) |
732 | /* Clear the OPTWRE Bit to lock the FLASH Option Byte Registers access */ |
733 | { |
733 | CLEAR_BIT(FLASH->CR, FLASH_CR_OPTWRE); |
734 | /* Clear the OPTWRE Bit to lock the FLASH Option Byte Registers access */ |
734 | |
735 | CLEAR_BIT(FLASH->CR, FLASH_CR_OPTWRE); |
735 | return HAL_OK; |
736 | 736 | } |
|
737 | return HAL_OK; |
737 | |
738 | } |
738 | /** |
739 | 739 | * @brief Launch the option byte loading. |
|
740 | /** |
740 | * @note This function will reset automatically the MCU. |
741 | * @brief Launch the option byte loading. |
741 | * @retval None |
742 | * @note This function will reset automatically the MCU. |
742 | */ |
743 | * @retval None |
743 | void HAL_FLASH_OB_Launch(void) |
744 | */ |
744 | { |
745 | void HAL_FLASH_OB_Launch(void) |
745 | /* Initiates a system reset request to launch the option byte loading */ |
746 | { |
746 | HAL_NVIC_SystemReset(); |
747 | /* Initiates a system reset request to launch the option byte loading */ |
747 | } |
748 | HAL_NVIC_SystemReset(); |
748 | |
749 | } |
749 | /** |
750 | 750 | * @} |
|
751 | /** |
751 | */ |
752 | * @} |
752 | |
753 | */ |
753 | /** @defgroup FLASH_Exported_Functions_Group3 Peripheral errors functions |
754 | 754 | * @brief Peripheral errors functions |
|
755 | /** @defgroup FLASH_Exported_Functions_Group3 Peripheral errors functions |
755 | * |
756 | * @brief Peripheral errors functions |
756 | @verbatim |
757 | * |
757 | =============================================================================== |
758 | @verbatim |
758 | ##### Peripheral Errors functions ##### |
759 | =============================================================================== |
759 | =============================================================================== |
760 | ##### Peripheral Errors functions ##### |
760 | [..] |
761 | =============================================================================== |
761 | This subsection permit to get in run-time errors of the FLASH peripheral. |
762 | [..] |
762 | |
763 | This subsection permit to get in run-time errors of the FLASH peripheral. |
763 | @endverbatim |
764 | 764 | * @{ |
|
765 | @endverbatim |
765 | */ |
766 | * @{ |
766 | |
767 | */ |
767 | /** |
768 | 768 | * @brief Get the specific FLASH error flag. |
|
769 | /** |
769 | * @retval FLASH_ErrorCode The returned value can be: |
770 | * @brief Get the specific FLASH error flag. |
770 | * @ref FLASH_Error_Codes |
771 | * @retval FLASH_ErrorCode The returned value can be: |
771 | */ |
772 | * @ref FLASH_Error_Codes |
772 | uint32_t HAL_FLASH_GetError(void) |
773 | */ |
773 | { |
774 | uint32_t HAL_FLASH_GetError(void) |
774 | return pFlash.ErrorCode; |
775 | { |
775 | } |
776 | return pFlash.ErrorCode; |
776 | |
777 | } |
777 | /** |
778 | 778 | * @} |
|
779 | /** |
779 | */ |
780 | * @} |
780 | |
781 | */ |
781 | /** |
782 | 782 | * @} |
|
783 | /** |
783 | */ |
784 | * @} |
784 | |
785 | */ |
785 | /** @addtogroup FLASH_Private_Functions |
786 | 786 | * @{ |
|
787 | /** @addtogroup FLASH_Private_Functions |
787 | */ |
788 | * @{ |
788 | |
789 | */ |
789 | /** |
790 | 790 | * @brief Program a half-word (16-bit) at a specified address. |
|
791 | /** |
791 | * @param Address specify the address to be programmed. |
792 | * @brief Program a half-word (16-bit) at a specified address. |
792 | * @param Data specify the data to be programmed. |
793 | * @param Address specify the address to be programmed. |
793 | * @retval None |
794 | * @param Data specify the data to be programmed. |
794 | */ |
795 | * @retval None |
795 | static void FLASH_Program_HalfWord(uint32_t Address, uint16_t Data) |
796 | */ |
796 | { |
797 | static void FLASH_Program_HalfWord(uint32_t Address, uint16_t Data) |
797 | /* Clean the error context */ |
798 | { |
798 | pFlash.ErrorCode = HAL_FLASH_ERROR_NONE; |
799 | /* Clean the error context */ |
799 | |
800 | pFlash.ErrorCode = HAL_FLASH_ERROR_NONE; |
800 | #if defined(FLASH_BANK2_END) |
801 | 801 | if(Address <= FLASH_BANK1_END) |
|
802 | #if defined(FLASH_BANK2_END) |
802 | { |
803 | if(Address <= FLASH_BANK1_END) |
803 | #endif /* FLASH_BANK2_END */ |
804 | { |
804 | /* Proceed to program the new data */ |
805 | #endif /* FLASH_BANK2_END */ |
805 | SET_BIT(FLASH->CR, FLASH_CR_PG); |
806 | /* Proceed to program the new data */ |
806 | #if defined(FLASH_BANK2_END) |
807 | SET_BIT(FLASH->CR, FLASH_CR_PG); |
807 | } |
808 | #if defined(FLASH_BANK2_END) |
808 | else |
809 | } |
809 | { |
810 | else |
810 | /* Proceed to program the new data */ |
811 | { |
811 | SET_BIT(FLASH->CR2, FLASH_CR2_PG); |
812 | /* Proceed to program the new data */ |
812 | } |
813 | SET_BIT(FLASH->CR2, FLASH_CR2_PG); |
813 | #endif /* FLASH_BANK2_END */ |
814 | } |
814 | |
815 | #endif /* FLASH_BANK2_END */ |
815 | /* Write data in the address */ |
816 | 816 | *(__IO uint16_t*)Address = Data; |
|
817 | /* Write data in the address */ |
817 | } |
818 | *(__IO uint16_t*)Address = Data; |
818 | |
819 | } |
819 | /** |
820 | 820 | * @brief Wait for a FLASH operation to complete. |
|
821 | /** |
821 | * @param Timeout maximum flash operation timeout |
822 | * @brief Wait for a FLASH operation to complete. |
822 | * @retval HAL Status |
823 | * @param Timeout maximum flash operation timeout |
823 | */ |
824 | * @retval HAL Status |
824 | HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout) |
825 | */ |
825 | { |
826 | HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout) |
826 | /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset. |
827 | { |
827 | Even if the FLASH operation fails, the BUSY flag will be reset and an error |
828 | /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset. |
828 | flag will be set */ |
829 | Even if the FLASH operation fails, the BUSY flag will be reset and an error |
829 | |
830 | flag will be set */ |
830 | uint32_t tickstart = HAL_GetTick(); |
831 | 831 | ||
832 | uint32_t tickstart = HAL_GetTick(); |
832 | while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY)) |
833 | 833 | { |
|
834 | while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY)) |
834 | if (Timeout != HAL_MAX_DELAY) |
835 | { |
835 | { |
836 | if (Timeout != HAL_MAX_DELAY) |
836 | if((Timeout == 0U) || ((HAL_GetTick()-tickstart) > Timeout)) |
837 | { |
837 | { |
838 | if((Timeout == 0U) || ((HAL_GetTick()-tickstart) > Timeout)) |
838 | return HAL_TIMEOUT; |
839 | { |
839 | } |
840 | return HAL_TIMEOUT; |
840 | } |
841 | } |
841 | } |
842 | } |
842 | |
843 | } |
843 | /* Check FLASH End of Operation flag */ |
844 | 844 | if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP)) |
|
845 | /* Check FLASH End of Operation flag */ |
845 | { |
846 | if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP)) |
846 | /* Clear FLASH End of Operation pending bit */ |
847 | { |
847 | __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP); |
848 | /* Clear FLASH End of Operation pending bit */ |
848 | } |
849 | __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP); |
849 | |
850 | } |
850 | if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) || |
851 | 851 | __HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR) || |
|
852 | if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) || |
852 | __HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR)) |
853 | __HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR) || |
853 | { |
854 | __HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR)) |
854 | /*Save the error code*/ |
855 | { |
855 | FLASH_SetErrorCode(); |
856 | /*Save the error code*/ |
856 | return HAL_ERROR; |
857 | FLASH_SetErrorCode(); |
857 | } |
858 | return HAL_ERROR; |
858 | |
859 | } |
859 | /* There is no error flag set */ |
860 | 860 | return HAL_OK; |
|
861 | /* There is no error flag set */ |
861 | } |
862 | return HAL_OK; |
862 | |
863 | } |
863 | #if defined(FLASH_BANK2_END) |
864 | 864 | /** |
|
865 | #if defined(FLASH_BANK2_END) |
865 | * @brief Wait for a FLASH BANK2 operation to complete. |
866 | /** |
866 | * @param Timeout maximum flash operation timeout |
867 | * @brief Wait for a FLASH BANK2 operation to complete. |
867 | * @retval HAL_StatusTypeDef HAL Status |
868 | * @param Timeout maximum flash operation timeout |
868 | */ |
869 | * @retval HAL_StatusTypeDef HAL Status |
869 | HAL_StatusTypeDef FLASH_WaitForLastOperationBank2(uint32_t Timeout) |
870 | */ |
870 | { |
871 | HAL_StatusTypeDef FLASH_WaitForLastOperationBank2(uint32_t Timeout) |
871 | /* Wait for the FLASH BANK2 operation to complete by polling on BUSY flag to be reset. |
872 | { |
872 | Even if the FLASH BANK2 operation fails, the BUSY flag will be reset and an error |
873 | /* Wait for the FLASH BANK2 operation to complete by polling on BUSY flag to be reset. |
873 | flag will be set */ |
874 | Even if the FLASH BANK2 operation fails, the BUSY flag will be reset and an error |
874 | |
875 | flag will be set */ |
875 | uint32_t tickstart = HAL_GetTick(); |
876 | 876 | ||
877 | uint32_t tickstart = HAL_GetTick(); |
877 | while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY_BANK2)) |
878 | 878 | { |
|
879 | while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY_BANK2)) |
879 | if (Timeout != HAL_MAX_DELAY) |
880 | { |
880 | { |
881 | if (Timeout != HAL_MAX_DELAY) |
881 | if((Timeout == 0U) || ((HAL_GetTick()-tickstart) > Timeout)) |
882 | { |
882 | { |
883 | if((Timeout == 0U) || ((HAL_GetTick()-tickstart) > Timeout)) |
883 | return HAL_TIMEOUT; |
884 | { |
884 | } |
885 | return HAL_TIMEOUT; |
885 | } |
886 | } |
886 | } |
887 | } |
887 | |
888 | } |
888 | /* Check FLASH End of Operation flag */ |
889 | 889 | if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP_BANK2)) |
|
890 | /* Check FLASH End of Operation flag */ |
890 | { |
891 | if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP_BANK2)) |
891 | /* Clear FLASH End of Operation pending bit */ |
892 | { |
892 | __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP_BANK2); |
893 | /* Clear FLASH End of Operation pending bit */ |
893 | } |
894 | __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP_BANK2); |
894 | |
895 | } |
895 | if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR_BANK2) || __HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR_BANK2)) |
896 | 896 | { |
|
897 | if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR_BANK2) || __HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR_BANK2)) |
897 | /*Save the error code*/ |
898 | { |
898 | FLASH_SetErrorCode(); |
899 | /*Save the error code*/ |
899 | return HAL_ERROR; |
900 | FLASH_SetErrorCode(); |
900 | } |
901 | return HAL_ERROR; |
901 | |
902 | } |
902 | /* If there is an error flag set */ |
903 | 903 | return HAL_OK; |
|
904 | /* If there is an error flag set */ |
904 | |
905 | return HAL_OK; |
905 | } |
906 | 906 | #endif /* FLASH_BANK2_END */ |
|
907 | } |
907 | |
908 | #endif /* FLASH_BANK2_END */ |
908 | /** |
909 | 909 | * @brief Set the specific FLASH error flag. |
|
910 | /** |
910 | * @retval None |
911 | * @brief Set the specific FLASH error flag. |
911 | */ |
912 | * @retval None |
912 | static void FLASH_SetErrorCode(void) |
913 | */ |
913 | { |
914 | static void FLASH_SetErrorCode(void) |
914 | uint32_t flags = 0U; |
915 | { |
915 | |
916 | uint32_t flags = 0U; |
916 | #if defined(FLASH_BANK2_END) |
917 | 917 | if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) || __HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR_BANK2)) |
|
918 | #if defined(FLASH_BANK2_END) |
918 | #else |
919 | if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) || __HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR_BANK2)) |
919 | if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR)) |
920 | #else |
920 | #endif /* FLASH_BANK2_END */ |
921 | if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR)) |
921 | { |
922 | #endif /* FLASH_BANK2_END */ |
922 | pFlash.ErrorCode |= HAL_FLASH_ERROR_WRP; |
923 | { |
923 | #if defined(FLASH_BANK2_END) |
924 | pFlash.ErrorCode |= HAL_FLASH_ERROR_WRP; |
924 | flags |= FLASH_FLAG_WRPERR | FLASH_FLAG_WRPERR_BANK2; |
925 | #if defined(FLASH_BANK2_END) |
925 | #else |
926 | flags |= FLASH_FLAG_WRPERR | FLASH_FLAG_WRPERR_BANK2; |
926 | flags |= FLASH_FLAG_WRPERR; |
927 | #else |
927 | #endif /* FLASH_BANK2_END */ |
928 | flags |= FLASH_FLAG_WRPERR; |
928 | } |
929 | #endif /* FLASH_BANK2_END */ |
929 | #if defined(FLASH_BANK2_END) |
930 | } |
930 | if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR) || __HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR_BANK2)) |
931 | #if defined(FLASH_BANK2_END) |
931 | #else |
932 | if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR) || __HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR_BANK2)) |
932 | if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR)) |
933 | #else |
933 | #endif /* FLASH_BANK2_END */ |
934 | if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR)) |
934 | { |
935 | #endif /* FLASH_BANK2_END */ |
935 | pFlash.ErrorCode |= HAL_FLASH_ERROR_PROG; |
936 | { |
936 | #if defined(FLASH_BANK2_END) |
937 | pFlash.ErrorCode |= HAL_FLASH_ERROR_PROG; |
937 | flags |= FLASH_FLAG_PGERR | FLASH_FLAG_PGERR_BANK2; |
938 | #if defined(FLASH_BANK2_END) |
938 | #else |
939 | flags |= FLASH_FLAG_PGERR | FLASH_FLAG_PGERR_BANK2; |
939 | flags |= FLASH_FLAG_PGERR; |
940 | #else |
940 | #endif /* FLASH_BANK2_END */ |
941 | flags |= FLASH_FLAG_PGERR; |
941 | } |
942 | #endif /* FLASH_BANK2_END */ |
942 | if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR)) |
943 | } |
943 | { |
944 | if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR)) |
944 | pFlash.ErrorCode |= HAL_FLASH_ERROR_OPTV; |
945 | { |
945 | __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR); |
946 | pFlash.ErrorCode |= HAL_FLASH_ERROR_OPTV; |
946 | } |
947 | __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR); |
947 | |
948 | } |
948 | /* Clear FLASH error pending bits */ |
949 | 949 | __HAL_FLASH_CLEAR_FLAG(flags); |
|
950 | /* Clear FLASH error pending bits */ |
950 | } |
951 | __HAL_FLASH_CLEAR_FLAG(flags); |
951 | /** |
952 | } |
952 | * @} |
953 | /** |
953 | */ |
954 | * @} |
954 | |
955 | */ |
955 | /** |
956 | 956 | * @} |
|
957 | /** |
957 | */ |
958 | * @} |
958 | |
959 | */ |
959 | #endif /* HAL_FLASH_MODULE_ENABLED */ |
960 | 960 | ||
961 | #endif /* HAL_FLASH_MODULE_ENABLED */ |
961 | /** |
962 | 962 | * @} |
|
963 | /** |
963 | */ |
964 | * @} |
964 | |
965 | */ |
- | |
966 | - | ||
967 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |
- |