STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
stm32f7xx_hal_flash.c
Go to the documentation of this file.
1 
102 /* Includes ------------------------------------------------------------------*/
103 #include "stm32f7xx_hal.h"
104 
114 #ifdef HAL_FLASH_MODULE_ENABLED
115 
116 /* Private typedef -----------------------------------------------------------*/
117 /* Private define ------------------------------------------------------------*/
121 #define SECTOR_MASK ((uint32_t)0xFFFFFF07U)
122 #define FLASH_TIMEOUT_VALUE ((uint32_t)50000U)/* 50 s */
123 
126 /* Private macro -------------------------------------------------------------*/
127 /* Private variables ---------------------------------------------------------*/
131 /* Variable used for Erase sectors under interruption */
132 FLASH_ProcessTypeDef pFlash;
137 /* Private function prototypes -----------------------------------------------*/
141 /* Program operations */
142 static void FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data);
143 static void FLASH_Program_Word(uint32_t Address, uint32_t Data);
144 static void FLASH_Program_HalfWord(uint32_t Address, uint16_t Data);
145 static void FLASH_Program_Byte(uint32_t Address, uint8_t Data);
146 static void FLASH_SetErrorCode(void);
147 
153 /* Exported functions --------------------------------------------------------*/
182 HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
183 {
184  HAL_StatusTypeDef status = HAL_ERROR;
185 
186  /* Process Locked */
187  __HAL_LOCK(&pFlash);
188 
189  /* Check the parameters */
190  assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
191 
192  /* Wait for last operation to be completed */
193  status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
194 
195  if(status == HAL_OK)
196  {
197  switch(TypeProgram)
198  {
200  {
201  /*Program byte (8-bit) at a specified address.*/
202  FLASH_Program_Byte(Address, (uint8_t) Data);
203  break;
204  }
205 
207  {
208  /*Program halfword (16-bit) at a specified address.*/
209  FLASH_Program_HalfWord(Address, (uint16_t) Data);
210  break;
211  }
212 
214  {
215  /*Program word (32-bit) at a specified address.*/
216  FLASH_Program_Word(Address, (uint32_t) Data);
217  break;
218  }
219 
221  {
222  /*Program double word (64-bit) at a specified address.*/
223  FLASH_Program_DoubleWord(Address, Data);
224  break;
225  }
226  default :
227  break;
228  }
229  /* Wait for last operation to be completed */
230  status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
231 
232  /* If the program operation is completed, disable the PG Bit */
233  FLASH->CR &= (~FLASH_CR_PG);
234  }
235 
236  /* Process Unlocked */
237  __HAL_UNLOCK(&pFlash);
238 
239  return status;
240 }
241 
251 HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
252 {
253  HAL_StatusTypeDef status = HAL_OK;
254 
255  /* Process Locked */
256  __HAL_LOCK(&pFlash);
257 
258  /* Check the parameters */
259  assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
260 
261  /* Enable End of FLASH Operation interrupt */
263 
264  /* Enable Error source interrupt */
266 
267  /* Clear pending flags (if any) */
270 
272  pFlash.Address = Address;
273 
274  switch(TypeProgram)
275  {
277  {
278  /*Program byte (8-bit) at a specified address.*/
279  FLASH_Program_Byte(Address, (uint8_t) Data);
280  break;
281  }
282 
284  {
285  /*Program halfword (16-bit) at a specified address.*/
286  FLASH_Program_HalfWord(Address, (uint16_t) Data);
287  break;
288  }
289 
291  {
292  /*Program word (32-bit) at a specified address.*/
293  FLASH_Program_Word(Address, (uint32_t) Data);
294  break;
295  }
296 
298  {
299  /*Program double word (64-bit) at a specified address.*/
300  FLASH_Program_DoubleWord(Address, Data);
301  break;
302  }
303  default :
304  break;
305  }
306  return status;
307 }
308 
313 void HAL_FLASH_IRQHandler(void)
314 {
315  uint32_t temp = 0;
316 
317  /* If the program operation is completed, disable the PG Bit */
318  FLASH->CR &= (~FLASH_CR_PG);
319 
320  /* If the erase operation is completed, disable the SER Bit */
321  FLASH->CR &= (~FLASH_CR_SER);
322  FLASH->CR &= SECTOR_MASK;
323 
324  /* if the erase operation is completed, disable the MER Bit */
325  FLASH->CR &= (~FLASH_MER_BIT);
326 
327  /* Check FLASH End of Operation flag */
329  {
330  switch (pFlash.ProcedureOnGoing)
331  {
332  case FLASH_PROC_SECTERASE :
333  {
334  /* Nb of sector to erased can be decreased */
335  pFlash.NbSectorsToErase--;
336 
337  /* Check if there are still sectors to erase */
338  if(pFlash.NbSectorsToErase != 0)
339  {
340  temp = pFlash.Sector;
341  /* Indicate user which sector has been erased */
343 
344  /* Clear pending flags (if any) */
346 
347  /* Increment sector number */
348  temp = ++pFlash.Sector;
349  FLASH_Erase_Sector(temp, pFlash.VoltageForErase);
350  }
351  else
352  {
353  /* No more sectors to Erase, user callback can be called.*/
354  /* Reset Sector and stop Erase sectors procedure */
355  pFlash.Sector = temp = 0xFFFFFFFFU;
356  /* FLASH EOP interrupt user callback */
358  /* Sector Erase procedure is completed */
360  /* Clear FLASH End of Operation pending bit */
362  }
363  break;
364  }
365 
366  case FLASH_PROC_MASSERASE :
367  {
368  /* MassErase ended. Return the selected bank : in this product we don't have Banks */
369  /* FLASH EOP interrupt user callback */
371  /* MAss Erase procedure is completed */
373  /* Clear FLASH End of Operation pending bit */
375  break;
376  }
377 
378  case FLASH_PROC_PROGRAM :
379  {
380  /*Program ended. Return the selected address*/
381  /* FLASH EOP interrupt user callback */
383  /* Programming procedure is completed */
385  /* Clear FLASH End of Operation pending bit */
387  break;
388  }
389  default :
390  break;
391  }
392  }
393 
394  /* Check FLASH operation error flags */
396  {
397  switch (pFlash.ProcedureOnGoing)
398  {
399  case FLASH_PROC_SECTERASE :
400  {
401  /* return the faulty sector */
402  temp = pFlash.Sector;
403  pFlash.Sector = 0xFFFFFFFFU;
404  break;
405  }
406  case FLASH_PROC_MASSERASE :
407  {
408  /* No return in case of Mass Erase */
409  temp = 0;
410  break;
411  }
412  case FLASH_PROC_PROGRAM :
413  {
414  /*return the faulty address*/
415  temp = pFlash.Address;
416  break;
417  }
418  default :
419  break;
420  }
421  /*Save the Error code*/
422  FLASH_SetErrorCode();
423 
424  /* FLASH error interrupt user callback */
426  /* Clear FLASH error pending bits */
428 
429  /*Stop the procedure ongoing */
431  }
432 
433  if(pFlash.ProcedureOnGoing == FLASH_PROC_NONE)
434  {
435  /* Disable End of FLASH Operation interrupt */
437 
438  /* Disable Error source interrupt */
440 
441  /* Process Unlocked */
442  __HAL_UNLOCK(&pFlash);
443  }
444 
445 }
446 
456 __weak void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue)
457 {
458  /* Prevent unused argument(s) compilation warning */
459  UNUSED(ReturnValue);
460  /* NOTE : This function Should not be modified, when the callback is needed,
461  the HAL_FLASH_EndOfOperationCallback could be implemented in the user file
462  */
463 }
464 
474 __weak void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue)
475 {
476  /* Prevent unused argument(s) compilation warning */
477  UNUSED(ReturnValue);
478  /* NOTE : This function Should not be modified, when the callback is needed,
479  the HAL_FLASH_OperationErrorCallback could be implemented in the user file
480  */
481 }
482 
507 {
508  if((FLASH->CR & FLASH_CR_LOCK) != RESET)
509  {
510  /* Authorize the FLASH Registers access */
511  FLASH->KEYR = FLASH_KEY1;
512  FLASH->KEYR = FLASH_KEY2;
513  }
514  else
515  {
516  return HAL_ERROR;
517  }
518 
519  return HAL_OK;
520 }
521 
527 {
528  /* Set the LOCK Bit to lock the FLASH Registers access */
529  FLASH->CR |= FLASH_CR_LOCK;
530 
531  return HAL_OK;
532 }
533 
539 {
540  if((FLASH->OPTCR & FLASH_OPTCR_OPTLOCK) != RESET)
541  {
542  /* Authorizes the Option Byte register programming */
543  FLASH->OPTKEYR = FLASH_OPT_KEY1;
544  FLASH->OPTKEYR = FLASH_OPT_KEY2;
545  }
546  else
547  {
548  return HAL_ERROR;
549  }
550 
551  return HAL_OK;
552 }
553 
559 {
560  /* Set the OPTLOCK Bit to lock the FLASH Option Byte Registers access */
561  FLASH->OPTCR |= FLASH_OPTCR_OPTLOCK;
562 
563  return HAL_OK;
564 }
565 
571 {
572  /* Set the OPTSTRT bit in OPTCR register */
573  FLASH->OPTCR |= FLASH_OPTCR_OPTSTRT;
574 
575  /* Wait for last operation to be completed */
576  return(FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE));
577 }
578 
606 uint32_t HAL_FLASH_GetError(void)
607 {
608  return pFlash.ErrorCode;
609 }
610 
621 {
622  uint32_t tickstart = 0;
623 
624  /* Clear Error Code */
626 
627  /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
628  Even if the FLASH operation fails, the BUSY flag will be reset and an error
629  flag will be set */
630  /* Get tick */
631  tickstart = HAL_GetTick();
632 
634  {
635  if(Timeout != HAL_MAX_DELAY)
636  {
637  if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
638  {
639  return HAL_TIMEOUT;
640  }
641  }
642  }
643 
646  {
647  /*Save the error code*/
648  FLASH_SetErrorCode();
649  return HAL_ERROR;
650  }
651 
652  /* If there is an error flag set */
653  return HAL_OK;
654 
655 }
656 
669 static void FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data)
670 {
671  /* Check the parameters */
672  assert_param(IS_FLASH_ADDRESS(Address));
673 
674  /* If the previous operation is completed, proceed to program the new data */
675  FLASH->CR &= CR_PSIZE_MASK;
677  FLASH->CR |= FLASH_CR_PG;
678 
679  *(__IO uint64_t*)Address = Data;
680 
681  /* Data synchronous Barrier (DSB) Just after the write operation
682  This will force the CPU to respect the sequence of instruction (no optimization).*/
683  __DSB();
684 }
685 
686 
699 static void FLASH_Program_Word(uint32_t Address, uint32_t Data)
700 {
701  /* Check the parameters */
702  assert_param(IS_FLASH_ADDRESS(Address));
703 
704  /* If the previous operation is completed, proceed to program the new data */
705  FLASH->CR &= CR_PSIZE_MASK;
706  FLASH->CR |= FLASH_PSIZE_WORD;
707  FLASH->CR |= FLASH_CR_PG;
708 
709  *(__IO uint32_t*)Address = Data;
710 
711  /* Data synchronous Barrier (DSB) Just after the write operation
712  This will force the CPU to respect the sequence of instruction (no optimization).*/
713  __DSB();
714 }
715 
728 static void FLASH_Program_HalfWord(uint32_t Address, uint16_t Data)
729 {
730  /* Check the parameters */
731  assert_param(IS_FLASH_ADDRESS(Address));
732 
733  /* If the previous operation is completed, proceed to program the new data */
734  FLASH->CR &= CR_PSIZE_MASK;
736  FLASH->CR |= FLASH_CR_PG;
737 
738  *(__IO uint16_t*)Address = Data;
739 
740  /* Data synchronous Barrier (DSB) Just after the write operation
741  This will force the CPU to respect the sequence of instruction (no optimization).*/
742  __DSB();
743 
744 }
745 
758 static void FLASH_Program_Byte(uint32_t Address, uint8_t Data)
759 {
760  /* Check the parameters */
761  assert_param(IS_FLASH_ADDRESS(Address));
762 
763  /* If the previous operation is completed, proceed to program the new data */
764  FLASH->CR &= CR_PSIZE_MASK;
765  FLASH->CR |= FLASH_PSIZE_BYTE;
766  FLASH->CR |= FLASH_CR_PG;
767 
768  *(__IO uint8_t*)Address = Data;
769 
770  /* Data synchronous Barrier (DSB) Just after the write operation
771  This will force the CPU to respect the sequence of instruction (no optimization).*/
772  __DSB();
773 }
774 
779 static void FLASH_SetErrorCode(void)
780 {
782  {
783  pFlash.ErrorCode |= HAL_FLASH_ERROR_WRP;
784  }
785 
787  {
788  pFlash.ErrorCode |= HAL_FLASH_ERROR_PGA;
789  }
790 
792  {
793  pFlash.ErrorCode |= HAL_FLASH_ERROR_PGP;
794  }
795 
797  {
798  pFlash.ErrorCode |= HAL_FLASH_ERROR_ERS;
799  }
800 
802  {
804  }
805 }
806 
811 #endif /* HAL_FLASH_MODULE_ENABLED */
812 
821 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
#define FLASH_FLAG_BSY
void FLASH_Erase_Sector(uint32_t Sector, uint8_t VoltageRange)
#define FLASH_FLAG_ERSERR
HAL_StatusTypeDef HAL_FLASH_OB_Lock(void)
#define assert_param(expr)
Include module's header file.
void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue)
#define __HAL_FLASH_GET_FLAG(__FLAG__)
Get the specified FLASH flag status.
#define FLASH_CR_SER
Definition: stm32f745xx.h:3875
#define CR_PSIZE_MASK
#define __HAL_UNLOCK(__HANDLE__)
#define FLASH_TYPEPROGRAM_DOUBLEWORD
void HAL_FLASH_IRQHandler(void)
#define FLASH_FLAG_PGAERR
#define FLASH_PSIZE_HALF_WORD
#define FLASH
Definition: stm32f745xx.h:1326
#define FLASH_FLAG_OPERR
#define FLASH_CR_LOCK
Definition: stm32f745xx.h:3888
#define FLASH_TYPEPROGRAM_WORD
__IO FLASH_ProcedureTypeDef ProcedureOnGoing
#define FLASH_FLAG_WRPERR
#define FLASH_OPT_KEY2
HAL_StatusTypeDef HAL_FLASH_OB_Launch(void)
HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
#define FLASH_PSIZE_BYTE
FLASH handle Structure definition.
#define FLASH_TYPEPROGRAM_BYTE
#define __HAL_LOCK(__HANDLE__)
#define FLASH_CR_PG
Definition: stm32f745xx.h:3874
#define __DSB()
Data Synchronization Barrier.
Definition: cmsis_armcc.h:355
#define FLASH_OPTCR_OPTLOCK
Definition: stm32f745xx.h:3891
#define HAL_MAX_DELAY
__IO uint32_t NbSectorsToErase
#define FLASH_FLAG_EOP
#define HAL_FLASH_ERROR_NONE
#define __IO
Definition: core_cm0.h:213
This file contains all the functions prototypes for the HAL module driver.
#define FLASH_KEY1
HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)
uint32_t HAL_FLASH_GetError(void)
void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue)
#define __HAL_FLASH_CLEAR_FLAG(__FLAG__)
Clear the specified FLASH flag.
#define FLASH_IT_ERR
#define FLASH_OPTCR_OPTSTRT
Definition: stm32f745xx.h:3892
#define FLASH_TYPEPROGRAM_HALFWORD
#define FLASH_MER_BIT
#define HAL_FLASH_ERROR_PGA
#define UNUSED(x)
HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void)
#define FLASH_FLAG_PGPERR
#define IS_FLASH_ADDRESS(ADDRESS)
#define HAL_FLASH_ERROR_PGP
HAL_StatusTypeDef HAL_FLASH_Lock(void)
#define FLASH_OPT_KEY1
#define IS_FLASH_TYPEPROGRAM(VALUE)
#define HAL_FLASH_ERROR_OPERATION
HAL_StatusTypeDef HAL_FLASH_Unlock(void)
#define FLASH_IT_EOP
#define HAL_FLASH_ERROR_ERS
#define FLASH_PSIZE_WORD
HAL_StatusTypeDef
HAL Status structures definition.
uint32_t HAL_GetTick(void)
Provides a tick value in millisecond.
#define HAL_FLASH_ERROR_WRP
HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
#define __HAL_FLASH_ENABLE_IT(__INTERRUPT__)
Enable the specified FLASH interrupt.
#define __HAL_FLASH_DISABLE_IT(__INTERRUPT__)
Disable the specified FLASH interrupt.
#define FLASH_PSIZE_DOUBLE_WORD
#define FLASH_KEY2