@@ -97,13 +97,15 @@ struct arm_flash_dev_t
9797 struct low_level_device * dev ;
9898 ARM_FLASH_INFO * data ; /*!< FLASH memory device data */
9999};
100+
100101/**
101102 * @}
102103 */
103104/** @defgroup FLASH_Private_Variables Private Variables
104105 * @{
105106 */
106107static __IO uint32_t DoubleECC_Error_Counter = 0U ;
108+
107109/**
108110 * \brief Check if the Flash memory boundaries are not violated.
109111 * \param[in] flash_dev Flash device structure \ref arm_flash_dev_t
@@ -122,6 +124,7 @@ static bool is_range_valid(struct arm_flash_dev_t *flash_dev,
122124
123125 return (offset > flash_limit ) ? (false) : (true) ;
124126}
127+
125128#if defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3U )
126129/**
127130 * \brief Check if the range is secure .
@@ -144,13 +147,16 @@ static bool is_range_secure(struct arm_flash_dev_t *flash_dev,
144147 return true;
145148 }
146149 for (nb = 0 ; nb < vect -> nb ; nb ++ )
150+ {
147151 if ((start >= vect -> range [nb ].base ) && ((start + len - 1 ) <= vect -> range [nb ].limit ))
148152 {
149153 return true;
150154 }
155+ }
151156 return false;
152157}
153158#endif /* defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U) */
159+
154160/**
155161 * \brief Check if the parameter is an erasebale page.
156162 * \param[in] flash_dev Flash device structure \ref arm_flash_dev_t
@@ -167,12 +173,15 @@ static bool is_erase_allow(struct arm_flash_dev_t *flash_dev,
167173 struct flash_vect * vect = & flash_dev -> dev -> erase ;
168174 uint32_t nb ;
169175 for (nb = 0 ; nb < vect -> nb ; nb ++ )
176+ {
170177 if ((param >= vect -> range [nb ].base ) && (param <= vect -> range [nb ].limit ))
171178 {
172179 return true;
173180 }
181+ }
174182 return false;
175183}
184+
176185/**
177186 * \brief Check if the parameter is writeable area.
178187 * \param[in] flash_dev Flash device structure \ref arm_flash_dev_t
@@ -189,10 +198,12 @@ static bool is_write_allow(struct arm_flash_dev_t *flash_dev,
189198 struct flash_vect * vect = & flash_dev -> dev -> write ;
190199 uint32_t nb ;
191200 for (nb = 0 ; nb < vect -> nb ; nb ++ )
201+ {
192202 if ((start >= vect -> range [nb ].base ) && ((start + len - 1 ) <= vect -> range [nb ].limit ))
193203 {
194204 return true;
195205 }
206+ }
196207 return false;
197208}
198209
@@ -252,8 +263,9 @@ static uint32_t bank_number(struct arm_flash_dev_t *flash_dev,
252263static uint32_t page_number (struct arm_flash_dev_t * flash_dev ,
253264 uint32_t param )
254265{
255- uint32_t page = param / flash_dev -> data -> page_size ;
256- page = ((page > (flash_dev -> data -> sector_count ))) ? page - ((flash_dev -> data -> sector_count )) : page ;
266+ uint32_t page = param / flash_dev -> data -> page_size ;
267+
268+ page = (page > flash_dev -> data -> sector_count ) ? page - flash_dev -> data -> sector_count : page ;
257269#ifdef DEBUG_FLASH_ACCESS
258270 printf ("page = %x \r\n" , page );
259271#endif /* DEBUG_FLASH_ACCESS */
@@ -415,10 +427,13 @@ static int32_t Flash_ReadData(uint32_t addr, void *data, uint32_t cnt)
415427 ret = ARM_DRIVER_OK ;
416428 }
417429 DoubleECC_Error_Counter = 0U ;
418- if (ret == ARM_DRIVER_OK ) {
430+ if (ret == ARM_DRIVER_OK )
431+ {
419432 cnt /= data_width_byte [DriverCapabilities .data_width ];
420433 return cnt ;
421- } else {
434+ }
435+ else
436+ {
422437 return ret ;
423438 }
424439}
@@ -441,13 +456,16 @@ static int stm32_icache_disable(void)
441456 tickstart = HAL_GetTick ();
442457
443458 /* Wait for instruction cache to get disabled */
444- while (HAL_ICACHE_IsEnabled ()) {
459+ while (HAL_ICACHE_IsEnabled ())
460+ {
445461 if ((HAL_GetTick () - tickstart ) >
446- ICACHE_DISABLE_TIMEOUT_VALUE ) {
462+ ICACHE_DISABLE_TIMEOUT_VALUE )
463+ {
447464 /* New check to avoid false timeout detection in case
448465 * of preemption.
449466 */
450- if (HAL_ICACHE_IsEnabled ()) {
467+ if (HAL_ICACHE_IsEnabled ())
468+ {
451469 status = ARM_DRIVER_ERROR_TIMEOUT ;
452470 break ;
453471 }
@@ -468,28 +486,35 @@ static int icache_wait_for_invalidate_complete(void)
468486 uint32_t tickstart ;
469487
470488 /* Check if ongoing invalidation operation */
471- if (__HAL_ICACHE_GET_FLAG (ICACHE_FLAG_BUSY )) {
489+ if (__HAL_ICACHE_GET_FLAG (ICACHE_FLAG_BUSY ))
490+ {
472491 /* Get tick */
473492 tickstart = HAL_GetTick ();
474493
475494 /* Wait for end of cache invalidation */
476- while (!__HAL_ICACHE_GET_FLAG (ICACHE_FLAG_BUSYEND )) {
495+ while (!__HAL_ICACHE_GET_FLAG (ICACHE_FLAG_BUSYEND ))
496+ {
477497 if ((HAL_GetTick () - tickstart ) >
478- ICACHE_INVALIDATE_TIMEOUT_VALUE ) {
498+ ICACHE_INVALIDATE_TIMEOUT_VALUE )
499+ {
479500 break ;
480501 }
481502 }
482503 }
483504
484505 /* Clear any pending flags */
485- if (__HAL_ICACHE_GET_FLAG (ICACHE_FLAG_BUSYEND )) {
506+ if (__HAL_ICACHE_GET_FLAG (ICACHE_FLAG_BUSYEND ))
507+ {
486508 __HAL_ICACHE_CLEAR_FLAG (ICACHE_FLAG_BUSYEND );
487509 status = 0 ;
488- } else {
510+ }
511+ else
512+ {
489513 status = ARM_DRIVER_ERROR_TIMEOUT ;
490514 }
491515
492- if (__HAL_ICACHE_GET_FLAG (ICACHE_FLAG_ERROR )) {
516+ if (__HAL_ICACHE_GET_FLAG (ICACHE_FLAG_ERROR ))
517+ {
493518 __HAL_ICACHE_CLEAR_FLAG (ICACHE_FLAG_ERROR );
494519 status = ARM_DRIVER_ERROR ;
495520 }
@@ -514,9 +539,11 @@ static int32_t Flash_ProgramData(uint32_t addr,
514539#if defined(CHECK_WRITE ) || defined(DEBUG_FLASH_ACCESS )
515540 void * dest ;
516541#endif
542+
517543 /* Conversion between data items and bytes */
518544 cnt *= data_width_byte [DriverCapabilities .data_width ];
519545 ARM_FLASH0_STATUS .error = DRIVER_STATUS_NO_ERROR ;
546+
520547#if defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3U )
521548 if (is_range_secure (& ARM_FLASH0_DEV , addr , cnt ))
522549 {
@@ -541,12 +568,14 @@ static int32_t Flash_ProgramData(uint32_t addr,
541568#endif
542569 }
543570#endif
571+
544572#if defined(CHECK_WRITE ) || defined(DEBUG_FLASH_ACCESS )
545573 dest = (void * )(flash_base + addr );
546574#endif
547575#ifdef DEBUG_FLASH_ACCESS
548576 printf ("write %x n=%x \r\n" , (uint32_t ) dest , cnt );
549577#endif /* DEBUG_FLASH_ACCESS */
578+
550579 /* Check Flash memory boundaries and alignment with minimum write size
551580 * (program_unit), data size also needs to be a multiple of program_unit.
552581 */
@@ -569,9 +598,12 @@ static int32_t Flash_ProgramData(uint32_t addr,
569598 stm32_icache_disable ();
570599#endif /* TFM_ICACHE_ENABLE */
571600
572- if (is_range_secure (& ARM_FLASH0_DEV , addr , cnt )) {
601+ if (is_range_secure (& ARM_FLASH0_DEV , addr , cnt ))
602+ {
573603 HAL_FLASH_Unlock_SEC ();
574- } else {
604+ }
605+ else
606+ {
575607 HAL_FLASH_Unlock_NS ();
576608 }
577609
@@ -597,33 +629,41 @@ static int32_t Flash_ProgramData(uint32_t addr,
597629 memcpy (dword , (void * )((uint32_t )data + loop ), sizeof (dword ));
598630 if ((dword [0 ] != -1 ) || (dword [1 ] != -1 ))
599631 {
600- err = HAL_FLASH_Program (write_type , ( flash_base + addr ) , (uint32_t )& dword [0 ]);
632+ err = HAL_FLASH_Program (write_type , flash_base + addr , (uint32_t )& dword [0 ]);
601633 }
602634 else
603- {
635+ {
604636 err = HAL_OK ;
605- }
637+ }
606638 #endif /* STM32L5xx_HAL_H */
607639#elif FLASH0_PROG_UNIT == 0x10
608640 /* quadword api*/
609641 uint64_t dword [2 ];
610642 memcpy (dword , (void * )((uint32_t )data + loop ), sizeof (dword ));
611643 if ((dword [0 ] != -1 ) || (dword [1 ] != -1 ))
644+ {
612645 err = HAL_FLASH_Program (write_type , (flash_base + addr ), (uint32_t )& dword [0 ]);
646+ }
613647 else
648+ {
614649 err = HAL_OK ;
615- #else
650+ }
651+ #else /* FLASH0_PROG_UNIT */
616652#error "flash configuration must be defined here"
617- #endif
653+ #endif /* FLASH0_PROG_UNIT */
654+
618655 loop += sizeof (dword );
619656 addr += sizeof (dword );
620657 } while ((loop != cnt ) && (err == HAL_OK ));
621658
622659 ARM_FLASH0_STATUS .busy = DRIVER_STATUS_IDLE ;
623660
624- if (is_range_secure (& ARM_FLASH0_DEV , addr , cnt )) {
661+ if (is_range_secure (& ARM_FLASH0_DEV , addr , cnt ))
662+ {
625663 HAL_FLASH_Lock_SEC ();
626- } else {
664+ }
665+ else
666+ {
627667 HAL_FLASH_Lock_NS ();
628668 }
629669
@@ -646,13 +686,16 @@ static int32_t Flash_ProgramData(uint32_t addr,
646686#endif /* DEBUG_FLASH_ACCESS */
647687 }
648688#endif /* CHECK_WRITE */
689+
649690#ifdef DEBUG_FLASH_ACCESS
650691 if (err != HAL_OK )
651692 {
652693 printf ("failed write %x n=%x \r\n" , (uint32_t )(dest ), cnt );
653694 }
654695#endif /* DEBUG_FLASH_ACCESS */
696+
655697 cnt /= data_width_byte [DriverCapabilities .data_width ];
698+
656699 return (err == HAL_OK ) ? cnt : ARM_DRIVER_ERROR ;
657700}
658701
@@ -665,9 +708,11 @@ static int32_t Flash_EraseSector(uint32_t addr)
665708 uint32_t i ;
666709 uint32_t * pt ;
667710#endif /* CHECK_ERASE */
711+
668712#ifdef DEBUG_FLASH_ACCESS
669713 printf ("erase %x\r\n" , addr );
670714#endif /* DEBUG_FLASH_ACCESS */
715+
671716 if (!(is_range_valid (& ARM_FLASH0_DEV , addr )) ||
672717 !(is_erase_aligned (& ARM_FLASH0_DEV , addr )) ||
673718 !(is_erase_allow (& ARM_FLASH0_DEV , addr )))
@@ -682,6 +727,7 @@ static int32_t Flash_EraseSector(uint32_t addr)
682727#endif /* DEBUG_FLASH_ACCESS */
683728 return ARM_DRIVER_ERROR_PARAMETER ;
684729 }
730+
685731#if defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3U )
686732 if (is_range_secure (& ARM_FLASH0_DEV , addr , 4 ))
687733 {
@@ -692,14 +738,17 @@ static int32_t Flash_EraseSector(uint32_t addr)
692738#endif
693739 }
694740 else
741+ {
695742#if defined(FLASH_TYPEERASE_SECTORS_NS )
696743 EraseInit .TypeErase = FLASH_TYPEERASE_SECTORS_NS ;
697744#else
698745 EraseInit .TypeErase = FLASH_TYPEERASE_PAGES_NS ;
699746#endif
747+ }
700748#else
701- EraseInit .TypeErase = FLASH_TYPEERASE_PAGES ;
749+ EraseInit .TypeErase = FLASH_TYPEERASE_PAGES ;
702750#endif /* defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U) */
751+
703752 /* fix me assume dual bank, reading DBANK in OPTR in Flash init is better */
704753 /* flash size in DB256K in OPTR */
705754#if defined (FLASH_BANK_2 )
@@ -709,37 +758,44 @@ static int32_t Flash_EraseSector(uint32_t addr)
709758#endif /*FLASH_BANK_2*/
710759
711760#if defined(FLASH_SECTOR_SIZE )
712- EraseInit .NbSectors = FLASH0_SECTOR_SIZE / FLASH_SECTOR_SIZE ;
761+ EraseInit .NbSectors = FLASH0_SECTOR_SIZE / FLASH_SECTOR_SIZE ;
713762#else
714- EraseInit .NbPages = FLASH0_SECTOR_SIZE / FLASH_PAGE_SIZE ;
763+ EraseInit .NbPages = FLASH0_SECTOR_SIZE / FLASH_PAGE_SIZE ;
715764#endif
716765#if defined(FLASH_SECTOR_SIZE )
717- EraseInit .Sector = page_number (& ARM_FLASH0_DEV , addr );
766+ EraseInit .Sector = page_number (& ARM_FLASH0_DEV , addr );
718767#else
719- EraseInit .Page = page_number (& ARM_FLASH0_DEV , addr );
768+ EraseInit .Page = page_number (& ARM_FLASH0_DEV , addr );
720769#endif
721770
722771#ifdef TFM_ICACHE_ENABLE
723772 /* Disable icache, this will start the invalidation procedure.
724- * All changes(erase/write) to flash memory should happen when
725- * i-cache is disabled. A write to flash performed without
726- * disabling i-cache will set ERRF error flag in SR register.
727- */
773+ * All changes(erase/write) to flash memory should happen when
774+ * i-cache is disabled. A write to flash performed without
775+ * disabling i-cache will set ERRF error flag in SR register.
776+ */
728777 stm32_icache_disable ();
729778#endif /* TFM_ICACHE_ENABLE */
730779
731780 ARM_FLASH0_STATUS .error = DRIVER_STATUS_NO_ERROR ;
732- if (is_range_secure (& ARM_FLASH0_DEV , addr , 4 )) {
781+ if (is_range_secure (& ARM_FLASH0_DEV , addr , 4 ))
782+ {
733783 HAL_FLASH_Unlock_SEC ();
734- } else {
784+ }
785+ else
786+ {
735787 HAL_FLASH_Unlock_NS ();
736788 }
789+
737790 ARM_FLASH0_STATUS .busy = DRIVER_STATUS_BUSY ;
738791 err = HAL_FLASHEx_Erase (& EraseInit , & pageError );
739792 ARM_FLASH0_STATUS .busy = DRIVER_STATUS_IDLE ;
740- if (is_range_secure (& ARM_FLASH0_DEV , addr , 4 )) {
793+ if (is_range_secure (& ARM_FLASH0_DEV , addr , 4 ))
794+ {
741795 HAL_FLASH_Lock_SEC ();
742- } else {
796+ }
797+ else
798+ {
743799 HAL_FLASH_Lock_NS ();
744800 }
745801
@@ -758,6 +814,7 @@ static int32_t Flash_EraseSector(uint32_t addr)
758814 printf ("erase failed \r\n" );
759815 }
760816#endif /* DEBUG_FLASH_ACCESS */
817+
761818#ifdef CHECK_ERASE
762819#if defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3U )
763820 if (is_range_secure (& ARM_FLASH0_DEV , addr , 4 ))
@@ -783,6 +840,7 @@ static int32_t Flash_EraseSector(uint32_t addr)
783840 }
784841 }
785842#endif /* CHECK_ERASE */
843+
786844 return (err == HAL_OK ) ? ARM_DRIVER_OK : ARM_DRIVER_ERROR ;
787845}
788846
0 commit comments