STM32F4xx HAL Driver master
STM32CubeF4 HAL / LL Drivers API Reference
Loading...
Searching...
No Matches
stm32f4xx_hal_flash.c
Go to the documentation of this file.
1
74
75/* Includes ------------------------------------------------------------------*/
76#include "stm32f4xx_hal.h"
77
81
86
87#ifdef HAL_FLASH_MODULE_ENABLED
88
89/* Private typedef -----------------------------------------------------------*/
90/* Private define ------------------------------------------------------------*/
94#define FLASH_TIMEOUT_VALUE 50000U /* 50 s */
98/* Private macro -------------------------------------------------------------*/
99/* Private variables ---------------------------------------------------------*/
103/* Variable used for Erase sectors under interruption */
104FLASH_ProcessTypeDef pFlash = {.ProcedureOnGoing = FLASH_PROC_NONE,
105 .NbSectorsToErase = 0U,
106 .VoltageForErase= FLASH_VOLTAGE_RANGE_1,
107 .Sector = 0U,
108 .Bank = FLASH_BANK_1,
109 .Address = 0U,
110 .Lock = HAL_UNLOCKED,
111 .ErrorCode = HAL_FLASH_ERROR_NONE};
115
116/* Private function prototypes -----------------------------------------------*/
120/* Program operations */
121static void FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data);
122static void FLASH_Program_Word(uint32_t Address, uint32_t Data);
123static void FLASH_Program_HalfWord(uint32_t Address, uint16_t Data);
124static void FLASH_Program_Byte(uint32_t Address, uint8_t Data);
125static void FLASH_SetErrorCode(void);
126
131
132/* Exported functions --------------------------------------------------------*/
136
151
161HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
162{
163 HAL_StatusTypeDef status;
164
165 /* Process Locked */
166 __HAL_LOCK(&pFlash);
167
168 /* Check the parameters */
170
171 /* Wait for last operation to be completed */
172 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
173
174 if (status == HAL_OK)
175 {
176 if (TypeProgram == FLASH_TYPEPROGRAM_BYTE)
177 {
178 /*Program byte (8-bit) at a specified address.*/
179 FLASH_Program_Byte(Address, (uint8_t) Data);
180 }
181 else if (TypeProgram == FLASH_TYPEPROGRAM_HALFWORD)
182 {
183 /*Program halfword (16-bit) at a specified address.*/
184 FLASH_Program_HalfWord(Address, (uint16_t) Data);
185 }
186 else if (TypeProgram == FLASH_TYPEPROGRAM_WORD)
187 {
188 /*Program word (32-bit) at a specified address.*/
189 FLASH_Program_Word(Address, (uint32_t) Data);
190 }
191 else
192 {
193 /*Program double word (64-bit) at a specified address.*/
194 FLASH_Program_DoubleWord(Address, Data);
195 }
196
197 /* Wait for last operation to be completed */
198 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
199
200 /* If the program operation is completed, disable the PG Bit */
201 FLASH->CR &= (~FLASH_CR_PG);
202 }
203
204 /* Process Unlocked */
205 __HAL_UNLOCK(&pFlash);
206
207 return status;
208}
209
219HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
220{
221 HAL_StatusTypeDef status = HAL_OK;
222
223 /* Check the parameters */
225
226 /* Enable End of FLASH Operation interrupt */
228
229 /* Enable Error source interrupt */
231
233 pFlash.Address = Address;
234
235 if (TypeProgram == FLASH_TYPEPROGRAM_BYTE)
236 {
237 /*Program byte (8-bit) at a specified address.*/
238 FLASH_Program_Byte(Address, (uint8_t) Data);
239 }
240 else if (TypeProgram == FLASH_TYPEPROGRAM_HALFWORD)
241 {
242 /*Program halfword (16-bit) at a specified address.*/
243 FLASH_Program_HalfWord(Address, (uint16_t) Data);
244 }
245 else if (TypeProgram == FLASH_TYPEPROGRAM_WORD)
246 {
247 /*Program word (32-bit) at a specified address.*/
248 FLASH_Program_Word(Address, (uint32_t) Data);
249 }
250 else
251 {
252 /*Program double word (64-bit) at a specified address.*/
253 FLASH_Program_DoubleWord(Address, Data);
254 }
255
256 return status;
257}
258
263void HAL_FLASH_IRQHandler(void)
264{
265 uint32_t addresstmp = 0U;
266
267 /* Check FLASH operation error flags */
268#if defined(FLASH_SR_RDERR)
270 FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR | FLASH_FLAG_RDERR)) != RESET)
271#else
274#endif /* FLASH_SR_RDERR */
275 {
277 {
278 /*return the faulty sector*/
279 addresstmp = pFlash.Sector;
280 pFlash.Sector = 0xFFFFFFFFU;
281 }
282 else if (pFlash.ProcedureOnGoing == FLASH_PROC_MASSERASE)
283 {
284 /*return the faulty bank*/
285 addresstmp = pFlash.Bank;
286 }
287 else
288 {
289 /*return the faulty address*/
290 addresstmp = pFlash.Address;
291 }
292
293 /*Save the Error code*/
294 FLASH_SetErrorCode();
295
296 /* FLASH error interrupt user callback */
298
299 /*Stop the procedure ongoing*/
301 }
302
303 /* Check FLASH End of Operation flag */
305 {
306 /* Clear FLASH End of Operation pending bit */
308
310 {
311 /*Nb of sector to erased can be decreased*/
312 pFlash.NbSectorsToErase--;
313
314 /* Check if there are still sectors to erase*/
315 if (pFlash.NbSectorsToErase != 0U)
316 {
317 addresstmp = pFlash.Sector;
318 /*Indicate user which sector has been erased*/
320
321 /*Increment sector number*/
322 pFlash.Sector++;
323 addresstmp = pFlash.Sector;
324 FLASH_Erase_Sector(addresstmp, pFlash.VoltageForErase);
325 }
326 else
327 {
328 /*No more sectors to Erase, user callback can be called.*/
329 /*Reset Sector and stop Erase sectors procedure*/
330 pFlash.Sector = addresstmp = 0xFFFFFFFFU;
332
333 /* Flush the caches to be sure of the data consistency */
335
336 /* FLASH EOP interrupt user callback */
338 }
339 }
340 else
341 {
343 {
344 /* MassErase ended. Return the selected bank */
345 /* Flush the caches to be sure of the data consistency */
347
348 /* FLASH EOP interrupt user callback */
350 }
351 else
352 {
353 /*Program ended. Return the selected address*/
354 /* FLASH EOP interrupt user callback */
356 }
358 }
359 }
360
361 if (pFlash.ProcedureOnGoing == FLASH_PROC_NONE)
362 {
363 /* Operation is completed, disable the PG, SER, SNB and MER Bits */
364 CLEAR_BIT(FLASH->CR, (FLASH_CR_PG | FLASH_CR_SER | FLASH_CR_SNB | FLASH_MER_BIT));
365
366 /* Disable End of FLASH Operation interrupt */
368
369 /* Disable Error source interrupt */
371 }
372}
373
383__weak void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue)
384{
385 /* Prevent unused argument(s) compilation warning */
386 UNUSED(ReturnValue);
387 /* NOTE : This function Should not be modified, when the callback is needed,
388 the HAL_FLASH_EndOfOperationCallback could be implemented in the user file
389 */
390}
391
400__weak void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue)
401{
402 /* Prevent unused argument(s) compilation warning */
403 UNUSED(ReturnValue);
404 /* NOTE : This function Should not be modified, when the callback is needed,
405 the HAL_FLASH_OperationErrorCallback could be implemented in the user file
406 */
407}
408
412
427
433{
434 HAL_StatusTypeDef status = HAL_OK;
435
436 if (READ_BIT(FLASH->CR, FLASH_CR_LOCK) != RESET)
437 {
438 /* Authorize the FLASH Registers access */
439 WRITE_REG(FLASH->KEYR, FLASH_KEY1);
440 WRITE_REG(FLASH->KEYR, FLASH_KEY2);
441
442 /* Verify Flash is unlocked */
443 if (READ_BIT(FLASH->CR, FLASH_CR_LOCK) != RESET)
444 {
445 status = HAL_ERROR;
446 }
447 }
448
449 return status;
450}
451
457{
458 /* Set the LOCK Bit to lock the FLASH Registers access */
459 FLASH->CR |= FLASH_CR_LOCK;
460
461 return HAL_OK;
462}
463
469{
470 if ((FLASH->OPTCR & FLASH_OPTCR_OPTLOCK) != RESET)
471 {
472 /* Authorizes the Option Byte register programming */
473 FLASH->OPTKEYR = FLASH_OPT_KEY1;
474 FLASH->OPTKEYR = FLASH_OPT_KEY2;
475 }
476 else
477 {
478 return HAL_ERROR;
479 }
480
481 return HAL_OK;
482}
483
489{
490 /* Set the OPTLOCK Bit to lock the FLASH Option Byte Registers access */
491 FLASH->OPTCR |= FLASH_OPTCR_OPTLOCK;
492
493 return HAL_OK;
494}
495
501{
502 /* Set the OPTSTRT bit in OPTCR register */
503 *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS |= FLASH_OPTCR_OPTSTRT;
504
505 /* Wait for last operation to be completed */
506 return (FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE));
507}
508
512
526
537uint32_t HAL_FLASH_GetError(void)
538{
539 return pFlash.ErrorCode;
540}
541
545
552{
553 uint32_t tickstart = 0U;
554
555 /* Clear Error Code */
557
558 /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
559 Even if the FLASH operation fails, the BUSY flag will be reset and an error
560 flag will be set */
561 /* Get tick */
562 tickstart = HAL_GetTick();
563
564 while (__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY) != RESET)
565 {
566 if (Timeout != HAL_MAX_DELAY)
567 {
568 if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
569 {
570 return HAL_TIMEOUT;
571 }
572 }
573 }
574
575 /* Check FLASH End of Operation flag */
577 {
578 /* Clear FLASH End of Operation pending bit */
580 }
581#if defined(FLASH_SR_RDERR)
583 FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR | FLASH_FLAG_RDERR)) != RESET)
584#else
587#endif /* FLASH_SR_RDERR */
588 {
589 /*Save the error code*/
590 FLASH_SetErrorCode();
591 return HAL_ERROR;
592 }
593
594 /* If there is no error flag set */
595 return HAL_OK;
596
597}
598
611static void FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data)
612{
613 /* Check the parameters */
615
616 /* If the previous operation is completed, proceed to program the new data */
617 CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
618 FLASH->CR |= FLASH_PSIZE_DOUBLE_WORD;
619 FLASH->CR |= FLASH_CR_PG;
620
621 /* Program first word */
622 *(__IO uint32_t *)Address = (uint32_t)Data;
623
624 /* Barrier to ensure programming is performed in 2 steps, in right order
625 (independently of compiler optimization behavior) */
626 __ISB();
627
628 /* Program second word */
629 *(__IO uint32_t *)(Address + 4) = (uint32_t)(Data >> 32);
630}
631
632
645static void FLASH_Program_Word(uint32_t Address, uint32_t Data)
646{
647 /* Check the parameters */
649
650 /* If the previous operation is completed, proceed to program the new data */
651 CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
652 FLASH->CR |= FLASH_PSIZE_WORD;
653 FLASH->CR |= FLASH_CR_PG;
654
655 *(__IO uint32_t *)Address = Data;
656}
657
670static void FLASH_Program_HalfWord(uint32_t Address, uint16_t Data)
671{
672 /* Check the parameters */
674
675 /* If the previous operation is completed, proceed to program the new data */
676 CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
677 FLASH->CR |= FLASH_PSIZE_HALF_WORD;
678 FLASH->CR |= FLASH_CR_PG;
679
680 *(__IO uint16_t *)Address = Data;
681}
682
695static void FLASH_Program_Byte(uint32_t Address, uint8_t Data)
696{
697 /* Check the parameters */
699
700 /* If the previous operation is completed, proceed to program the new data */
701 CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
702 FLASH->CR |= FLASH_PSIZE_BYTE;
703 FLASH->CR |= FLASH_CR_PG;
704
705 *(__IO uint8_t *)Address = Data;
706}
707
712static void FLASH_SetErrorCode(void)
713{
715 {
717
718 /* Clear FLASH write protection error pending bit */
720 }
721
723 {
725
726 /* Clear FLASH Programming alignment error pending bit */
728 }
729
731 {
733
734 /* Clear FLASH Programming parallelism error pending bit */
736 }
737
739 {
741
742 /* Clear FLASH Programming sequence error pending bit */
744 }
745#if defined(FLASH_SR_RDERR)
746 if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR) != RESET)
747 {
749
750 /* Clear FLASH Proprietary readout protection error pending bit */
751 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_RDERR);
752 }
753#endif /* FLASH_SR_RDERR */
755 {
757
758 /* Clear FLASH Operation error pending bit */
760 }
761}
762
766
767#endif /* HAL_FLASH_MODULE_ENABLED */
768
772
776
#define IS_FLASH_ADDRESS(ADDRESS)
void FLASH_FlushCaches(void)
void FLASH_Erase_Sector(uint32_t Sector, uint8_t VoltageRange)
#define FLASH_VOLTAGE_RANGE_1
#define HAL_FLASH_ERROR_WRP
#define HAL_FLASH_ERROR_RD
#define HAL_FLASH_ERROR_PGP
#define HAL_FLASH_ERROR_PGS
#define HAL_FLASH_ERROR_PGA
#define HAL_FLASH_ERROR_NONE
#define HAL_FLASH_ERROR_OPERATION
void HAL_FLASH_IRQHandler(void)
void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue)
void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue)
HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
HAL_StatusTypeDef HAL_FLASH_OB_Launch(void)
HAL_StatusTypeDef HAL_FLASH_Unlock(void)
HAL_StatusTypeDef HAL_FLASH_OB_Lock(void)
HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void)
HAL_StatusTypeDef HAL_FLASH_Lock(void)
uint32_t HAL_FLASH_GetError(void)
HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)
#define __HAL_FLASH_GET_FLAG(__FLAG__)
Get the specified FLASH flag status.
#define __HAL_FLASH_ENABLE_IT(__INTERRUPT__)
Enable the specified FLASH interrupt.
#define __HAL_FLASH_DISABLE_IT(__INTERRUPT__)
Disable the specified FLASH interrupt.
#define __HAL_FLASH_CLEAR_FLAG(__FLAG__)
Clear the specified FLASH flags.
@ FLASH_PROC_MASSERASE
@ FLASH_PROC_NONE
@ FLASH_PROC_PROGRAM
@ FLASH_PROC_SECTERASE
#define FLASH_FLAG_PGSERR
#define FLASH_FLAG_PGAERR
#define FLASH_FLAG_WRPERR
#define FLASH_FLAG_PGPERR
#define FLASH_FLAG_BSY
#define FLASH_FLAG_OPERR
#define FLASH_FLAG_EOP
#define IS_FLASH_TYPEPROGRAM(VALUE)
#define FLASH_IT_ERR
#define FLASH_IT_EOP
#define FLASH_OPT_KEY2
#define FLASH_OPT_KEY1
#define FLASH_KEY2
#define FLASH_KEY1
#define OPTCR_BYTE0_ADDRESS
OPTCR register byte 0 (Bits[7:0]) base address.
#define FLASH_PSIZE_DOUBLE_WORD
#define FLASH_PSIZE_BYTE
#define FLASH_PSIZE_WORD
#define FLASH_PSIZE_HALF_WORD
#define FLASH_TYPEPROGRAM_HALFWORD
#define FLASH_TYPEPROGRAM_BYTE
#define FLASH_TYPEPROGRAM_WORD
uint32_t HAL_GetTick(void)
Provides a tick value in millisecond.
#define assert_param(expr)
This file contains all the functions prototypes for the HAL module driver.
HAL_StatusTypeDef
HAL Status structures definition.
@ HAL_TIMEOUT
@ HAL_ERROR
@ HAL_OK
#define __HAL_UNLOCK(__HANDLE__)
#define HAL_MAX_DELAY
@ HAL_UNLOCKED
#define __HAL_LOCK(__HANDLE__)
FLASH handle Structure definition.
volatile uint32_t NbSectorsToErase
volatile uint32_t Sector
volatile FLASH_ProcedureTypeDef ProcedureOnGoing
volatile uint32_t Address
volatile uint32_t ErrorCode
volatile uint8_t VoltageForErase