Rev 2 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2 | Rev 28 | ||
---|---|---|---|
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 | * @version V1.2.0 |
- | |
6 | * @date 01-July-2016 |
- | |
7 | * @brief FLASH HAL module driver. |
5 | * @brief FLASH HAL module driver. |
8 | * This file provides firmware functions to manage the following |
6 | * This file provides firmware functions to manage the following |
9 | * functionalities of the internal FLASH memory: |
7 | * functionalities of the internal FLASH memory: |
10 | * + Program operations functions |
8 | * + Program operations functions |
11 | * + Memory Control functions |
9 | * + Memory Control functions |
Line 138... | Line 136... | ||
138 | 136 | ||
139 | @endverbatim |
137 | @endverbatim |
140 | ****************************************************************************** |
138 | ****************************************************************************** |
141 | * @attention |
139 | * @attention |
142 | * |
140 | * |
143 | * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> |
141 | * <h2><center>© Copyright (c) 2017 STMicroelectronics. |
- | 142 | * All rights reserved.</center></h2> |
|
144 | * |
143 | * |
145 | * Redistribution and use in source and binary forms, with or without modification, |
144 | * This software component is licensed by ST under BSD 3-Clause license, |
146 | * are permitted provided that the following conditions are met: |
145 | * the "License"; You may not use this file except in compliance with the |
147 | * 1. Redistributions of source code must retain the above copyright notice, |
- | |
148 | * this list of conditions and the following disclaimer. |
- | |
149 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
- | |
150 | * this list of conditions and the following disclaimer in the documentation |
- | |
151 | * and/or other materials provided with the distribution. |
146 | * License. You may obtain a copy of the License at: |
152 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
- | |
153 | * may be used to endorse or promote products derived from this software |
147 | * opensource.org/licenses/BSD-3-Clause |
154 | * without specific prior written permission. |
- | |
155 | * |
148 | * |
156 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
- | |
157 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
- | |
158 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
- | |
159 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
- | |
160 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
- | |
161 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
- | |
162 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
- | |
163 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
- | |
164 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
- | |
165 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
- | |
166 | * |
- | |
167 | ****************************************************************************** |
149 | ****************************************************************************** |
168 | */ |
150 | */ |
169 | 151 | ||
170 | /* Includes ------------------------------------------------------------------*/ |
152 | /* Includes ------------------------------------------------------------------*/ |
171 | #include "stm32l1xx_hal.h" |
153 | #include "stm32l1xx_hal.h" |
172 | 154 | ||
Line 237... | Line 219... | ||
237 | * @note To correctly run this function, the HAL_FLASH_Unlock() function |
219 | * @note To correctly run this function, the HAL_FLASH_Unlock() function |
238 | * must be called before. |
220 | * must be called before. |
239 | * Call the HAL_FLASH_Lock() to disable the flash memory access |
221 | * Call the HAL_FLASH_Lock() to disable the flash memory access |
240 | * (recommended to protect the FLASH memory against possible unwanted operation). |
222 | * (recommended to protect the FLASH memory against possible unwanted operation). |
241 | * |
223 | * |
242 | * @param TypeProgram: Indicate the way to program at a specified address. |
224 | * @param TypeProgram Indicate the way to program at a specified address. |
243 | * This parameter can be a value of @ref FLASH_Type_Program |
225 | * This parameter can be a value of @ref FLASH_Type_Program |
244 | * @param Address: Specifies the address to be programmed. |
226 | * @param Address Specifie the address to be programmed. |
245 | * @param Data: Specifies the data to be programmed |
227 | * @param Data Specifie the data to be programmed |
246 | * |
228 | * |
247 | * @retval HAL_StatusTypeDef HAL Status |
229 | * @retval HAL_StatusTypeDef HAL Status |
248 | */ |
230 | */ |
249 | HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint32_t Data) |
231 | HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint32_t Data) |
250 | { |
232 | { |
Line 279... | Line 261... | ||
279 | } |
261 | } |
280 | 262 | ||
281 | /** |
263 | /** |
282 | * @brief Program word at a specified address with interrupt enabled. |
264 | * @brief Program word at a specified address with interrupt enabled. |
283 | * |
265 | * |
284 | * @param TypeProgram: Indicate the way to program at a specified address. |
266 | * @param TypeProgram Indicate the way to program at a specified address. |
285 | * This parameter can be a value of @ref FLASH_Type_Program |
267 | * This parameter can be a value of @ref FLASH_Type_Program |
286 | * @param Address: Specifies the address to be programmed. |
268 | * @param Address Specifie the address to be programmed. |
287 | * @param Data: Specifies the data to be programmed |
269 | * @param Data Specifie the data to be programmed |
288 | * |
270 | * |
289 | * @retval HAL_StatusTypeDef HAL Status |
271 | * @retval HAL_StatusTypeDef HAL Status |
290 | */ |
272 | */ |
291 | HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint32_t Data) |
273 | HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint32_t Data) |
292 | { |
274 | { |
Line 319... | Line 301... | ||
319 | * @brief This function handles FLASH interrupt request. |
301 | * @brief This function handles FLASH interrupt request. |
320 | * @retval None |
302 | * @retval None |
321 | */ |
303 | */ |
322 | void HAL_FLASH_IRQHandler(void) |
304 | void HAL_FLASH_IRQHandler(void) |
323 | { |
305 | { |
324 | uint32_t addresstmp = 0; |
306 | uint32_t addresstmp = 0U; |
325 | 307 | ||
326 | /* Check FLASH operation error flags */ |
308 | /* Check FLASH operation error flags */ |
327 | if( __HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) || |
309 | if( __HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) || |
328 | __HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR) || |
310 | __HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR) || |
329 | __HAL_FLASH_GET_FLAG(FLASH_FLAG_SIZERR) || |
311 | __HAL_FLASH_GET_FLAG(FLASH_FLAG_SIZERR) || |
Line 369... | Line 351... | ||
369 | { |
351 | { |
370 | /* Nb of pages to erased can be decreased */ |
352 | /* Nb of pages to erased can be decreased */ |
371 | pFlash.NbPagesToErase--; |
353 | pFlash.NbPagesToErase--; |
372 | 354 | ||
373 | /* Check if there are still pages to erase */ |
355 | /* Check if there are still pages to erase */ |
374 | if(pFlash.NbPagesToErase != 0) |
356 | if(pFlash.NbPagesToErase != 0U) |
375 | { |
357 | { |
376 | addresstmp = pFlash.Page; |
358 | addresstmp = pFlash.Page; |
377 | /*Indicate user which sector has been erased */ |
359 | /*Indicate user which sector has been erased */ |
378 | HAL_FLASH_EndOfOperationCallback(addresstmp); |
360 | HAL_FLASH_EndOfOperationCallback(addresstmp); |
379 | 361 | ||
Line 426... | Line 408... | ||
426 | } |
408 | } |
427 | } |
409 | } |
428 | 410 | ||
429 | /** |
411 | /** |
430 | * @brief FLASH end of operation interrupt callback |
412 | * @brief FLASH end of operation interrupt callback |
431 | * @param ReturnValue: The value saved in this parameter depends on the ongoing procedure |
413 | * @param ReturnValue The value saved in this parameter depends on the ongoing procedure |
432 | * - Pages Erase: Address of the page which has been erased |
414 | * - Pages Erase: Address of the page which has been erased |
433 | * (if 0xFFFFFFFF, it means that all the selected pages have been erased) |
415 | * (if 0xFFFFFFFF, it means that all the selected pages have been erased) |
434 | * - Program: Address which was selected for data program |
416 | * - Program: Address which was selected for data program |
435 | * @retval none |
417 | * @retval none |
436 | */ |
418 | */ |
Line 444... | Line 426... | ||
444 | */ |
426 | */ |
445 | } |
427 | } |
446 | 428 | ||
447 | /** |
429 | /** |
448 | * @brief FLASH operation error interrupt callback |
430 | * @brief FLASH operation error interrupt callback |
449 | * @param ReturnValue: The value saved in this parameter depends on the ongoing procedure |
431 | * @param ReturnValue The value saved in this parameter depends on the ongoing procedure |
450 | * - Pages Erase: Address of the page which returned an error |
432 | * - Pages Erase: Address of the page which returned an error |
451 | * - Program: Address which was selected for data program |
433 | * - Program: Address which was selected for data program |
452 | * @retval none |
434 | * @retval none |
453 | */ |
435 | */ |
454 | __weak void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue) |
436 | __weak void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue) |
Line 491... | Line 473... | ||
491 | /* Unlocking FLASH_PECR register access*/ |
473 | /* Unlocking FLASH_PECR register access*/ |
492 | if(HAL_IS_BIT_SET(FLASH->PECR, FLASH_PECR_PELOCK)) |
474 | if(HAL_IS_BIT_SET(FLASH->PECR, FLASH_PECR_PELOCK)) |
493 | { |
475 | { |
494 | WRITE_REG(FLASH->PEKEYR, FLASH_PEKEY1); |
476 | WRITE_REG(FLASH->PEKEYR, FLASH_PEKEY1); |
495 | WRITE_REG(FLASH->PEKEYR, FLASH_PEKEY2); |
477 | WRITE_REG(FLASH->PEKEYR, FLASH_PEKEY2); |
- | 478 | ||
- | 479 | /* Verify that PELOCK is unlocked */ |
|
- | 480 | if(HAL_IS_BIT_SET(FLASH->PECR, FLASH_PECR_PELOCK)) |
|
- | 481 | { |
|
- | 482 | return HAL_ERROR; |
|
- | 483 | } |
|
496 | } |
484 | } |
497 | 485 | ||
498 | /* Unlocking the program memory access */ |
486 | /* Unlocking the program memory access */ |
499 | WRITE_REG(FLASH->PRGKEYR, FLASH_PRGKEY1); |
487 | WRITE_REG(FLASH->PRGKEYR, FLASH_PRGKEY1); |
500 | WRITE_REG(FLASH->PRGKEYR, FLASH_PRGKEY2); |
488 | WRITE_REG(FLASH->PRGKEYR, FLASH_PRGKEY2); |
- | 489 | ||
- | 490 | /* Verify that PRGLOCK is unlocked */ |
|
- | 491 | if (HAL_IS_BIT_SET(FLASH->PECR, FLASH_PECR_PRGLOCK)) |
|
- | 492 | { |
|
- | 493 | return HAL_ERROR; |
|
- | 494 | } |
|
501 | } |
495 | } |
502 | else |
- | |
503 | { |
- | |
504 | return HAL_ERROR; |
- | |
505 | } |
496 | |
506 | - | ||
507 | return HAL_OK; |
497 | return HAL_OK; |
508 | } |
498 | } |
509 | 499 | ||
510 | /** |
500 | /** |
511 | * @brief Locks the FLASH control register access |
501 | * @brief Locks the FLASH control register access |
Line 531... | Line 521... | ||
531 | if(HAL_IS_BIT_SET(FLASH->PECR, FLASH_PECR_PELOCK)) |
521 | if(HAL_IS_BIT_SET(FLASH->PECR, FLASH_PECR_PELOCK)) |
532 | { |
522 | { |
533 | /* Unlocking FLASH_PECR register access*/ |
523 | /* Unlocking FLASH_PECR register access*/ |
534 | WRITE_REG(FLASH->PEKEYR, FLASH_PEKEY1); |
524 | WRITE_REG(FLASH->PEKEYR, FLASH_PEKEY1); |
535 | WRITE_REG(FLASH->PEKEYR, FLASH_PEKEY2); |
525 | WRITE_REG(FLASH->PEKEYR, FLASH_PEKEY2); |
- | 526 | ||
- | 527 | /* Verify that PELOCK is unlocked */ |
|
- | 528 | if(HAL_IS_BIT_SET(FLASH->PECR, FLASH_PECR_PELOCK)) |
|
- | 529 | { |
|
- | 530 | return HAL_ERROR; |
|
- | 531 | } |
|
536 | } |
532 | } |
537 | 533 | ||
538 | /* Unlocking the option bytes block access */ |
534 | /* Unlocking the option bytes block access */ |
539 | WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY1); |
535 | WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY1); |
540 | WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY2); |
536 | WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY2); |
- | 537 | ||
- | 538 | /* Verify that OPTLOCK is unlocked */ |
|
- | 539 | if (HAL_IS_BIT_SET(FLASH->PECR, FLASH_PECR_OPTLOCK)) |
|
- | 540 | { |
|
- | 541 | return HAL_ERROR; |
|
- | 542 | } |
|
541 | } |
543 | } |
542 | else |
- | |
543 | { |
- | |
544 | return HAL_ERROR; |
- | |
545 | } |
- | |
546 | 544 | ||
547 | return HAL_OK; |
545 | return HAL_OK; |
548 | } |
546 | } |
549 | 547 | ||
550 | /** |
548 | /** |
Line 628... | Line 626... | ||
628 | 626 | ||
629 | while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY)) |
627 | while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY)) |
630 | { |
628 | { |
631 | if (Timeout != HAL_MAX_DELAY) |
629 | if (Timeout != HAL_MAX_DELAY) |
632 | { |
630 | { |
633 | if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout)) |
631 | if((Timeout == 0U) || ((HAL_GetTick()-tickstart) > Timeout)) |
634 | { |
632 | { |
635 | return HAL_TIMEOUT; |
633 | return HAL_TIMEOUT; |
636 | } |
634 | } |
637 | } |
635 | } |
638 | } |
636 | } |
Line 668... | Line 666... | ||
668 | * @brief Set the specific FLASH error flag. |
666 | * @brief Set the specific FLASH error flag. |
669 | * @retval None |
667 | * @retval None |
670 | */ |
668 | */ |
671 | static void FLASH_SetErrorCode(void) |
669 | static void FLASH_SetErrorCode(void) |
672 | { |
670 | { |
673 | uint32_t flags = 0; |
671 | uint32_t flags = 0U; |
674 | 672 | ||
675 | if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR)) |
673 | if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR)) |
676 | { |
674 | { |
677 | pFlash.ErrorCode |= HAL_FLASH_ERROR_WRP; |
675 | pFlash.ErrorCode |= HAL_FLASH_ERROR_WRP; |
678 | flags |= FLASH_FLAG_WRPERR; |
676 | flags |= FLASH_FLAG_WRPERR; |