@@ -514,7 +514,30 @@ static int erase_sector(const struct device *dev, int offset)
514514{
515515	int  rc ;
516516	struct  flash_stm32_sector_t  sector  =  get_sector (dev , offset );
517- 
517+ #if  defined(CONFIG_FLASH_STM32_ASYNC )
518+ 	FLASH_STM32_PRIV (dev )-> async_complete  =  false;
519+ 	FLASH_STM32_PRIV (dev )-> async_error  =  false;
520+ 	FLASH_EraseInitTypeDef  erase_init  =  {
521+         .TypeErase     =  FLASH_TYPEERASE_SECTORS ,
522+ 		.Banks         =  sector .bank ,
523+ 		.Sector        =  sector .sector_index ,
524+ 		.NbSectors     =  1 ,
525+ 		.VoltageRange  =  FLASH_VOLTAGE_RANGE_4 ,
526+ 
527+ 	};
528+ 	HAL_FLASHEx_Erase_IT (& erase_init );
529+ 	k_sem_take (& FLASH_STM32_PRIV (dev )-> async_sem , K_FOREVER );
530+ 	if  (FLASH_STM32_PRIV (dev )-> async_complete ) {
531+ 		LOG_DBG ("Flash erase successful. Erased %lu bytes at 0x%x" ,
532+ 			FLASH_SECTOR_SIZE , FLASH_STM32_PRIV (dev )-> async_ret );
533+ 		rc  =  0 ;
534+ 	} else  {
535+ 		if  (FLASH_STM32_PRIV (dev )-> async_error ) {
536+ 			LOG_ERR ("Flash erase failed %d" , FLASH_STM32_PRIV (dev )-> async_ret );
537+ 		}
538+ 		rc  =  - EIO ;
539+ 	}
540+ #else 
518541	if  (sector .bank  ==  0 ) {
519542
520543		LOG_ERR ("Offset %ld does not exist" , (long )offset );
@@ -539,7 +562,7 @@ static int erase_sector(const struct device *dev, int offset)
539562
540563	rc  =  flash_stm32_wait_flash_idle (dev );
541564	* (sector .cr ) &= ~(FLASH_CR_SER  | FLASH_CR_SNB );
542- 
565+ #endif 
543566	return  rc ;
544567}
545568
@@ -557,6 +580,7 @@ int flash_stm32_block_erase_loop(const struct device *dev, unsigned int offset,
557580	return  rc ;
558581}
559582
583+ #if  !defined(CONFIG_FLASH_STM32_ASYNC )
560584static  int  wait_write_queue (const  struct  flash_stm32_sector_t  * sector )
561585{
562586	int64_t  timeout_time  =  k_uptime_get () +  100 ;
@@ -570,11 +594,33 @@ static int wait_write_queue(const struct flash_stm32_sector_t *sector)
570594
571595	return  0 ;
572596}
597+ #endif  /* !CONFIG_FLASH_STM32_ASYNC */ 
573598
574599static  int  write_ndwords (const  struct  device  * dev , off_t  offset , const  uint64_t  * data , uint8_t  n )
575600{
576- 	volatile  uint64_t  * flash  =  (uint64_t  * )(offset  +  FLASH_STM32_BASE_ADDRESS );
577601	int  rc ;
602+ #if  defined(CONFIG_FLASH_STM32_ASYNC )
603+ 	for (size_t  i  =  0 ; i  <  n ; i  +=  FLASH_NB_32BITWORD_IN_FLASHWORD ) {
604+ 		FLASH_STM32_PRIV (dev )-> async_complete  =  false;
605+ 		FLASH_STM32_PRIV (dev )-> async_error  =  false;
606+ 		uint32_t  wordAddr  =  (uint32_t )& data [i ];
607+ 		HAL_FLASH_Program_IT (FLASH_TYPEPROGRAM_FLASHWORD , offset  +  FLASH_STM32_BASE_ADDRESS , wordAddr );
608+ 		k_sem_take (& FLASH_STM32_PRIV (dev )-> async_sem , K_FOREVER );
609+ 		if  (FLASH_STM32_PRIV (dev )-> async_complete ) {
610+ 			LOG_DBG ("Flash write successful. Wrote %u bytes to 0x%x" ,
611+ 				FLASH_STM32_WRITE_BLOCK_SIZE , FLASH_STM32_PRIV (dev )-> async_ret );
612+ 			rc  =  0 ;
613+ 		}
614+ 		else {
615+ 			if  (FLASH_STM32_PRIV (dev )-> async_error ) {
616+ 				LOG_ERR ("Flash write failed %d" , FLASH_STM32_PRIV (dev )-> async_ret );
617+ 			}
618+ 			rc  =  - EIO ;
619+ 		}
620+ 
621+ 	}
622+ #else 
623+ 	volatile  uint64_t  * flash  =  (uint64_t  * )(offset  +  FLASH_STM32_BASE_ADDRESS );
578624	int  i ;
579625	struct  flash_stm32_sector_t  sector  =  get_sector (dev , offset );
580626
@@ -623,7 +669,7 @@ static int write_ndwords(const struct device *dev, off_t offset, const uint64_t
623669
624670	/* Clear the PG bit */ 
625671	* (sector .cr ) &= (~FLASH_CR_PG );
626- 
672+ #endif 
627673	return  rc ;
628674}
629675
@@ -927,6 +973,31 @@ static DEVICE_API(flash, flash_stm32h7_api) = {
927973#endif 
928974};
929975
976+ #if  defined(CONFIG_FLASH_STM32_ASYNC )
977+ static  struct  flash_stm32_priv  * flash_dev ;
978+ 
979+ /* IRQ handler function for async flash mode */ 
980+ void  flash_stm32_irq_handler (void )
981+ {
982+ 	HAL_FLASH_IRQHandler ();
983+ 	if (flash_dev -> async_complete  ||  flash_dev -> async_error ) {
984+ 		k_sem_give (& flash_dev -> async_sem );
985+ 	}
986+ }
987+ 
988+ /* cube hal functions for end of a async flash operation */ 
989+ void  HAL_FLASH_EndOfOperationCallback (uint32_t  op_ret_val )
990+ {
991+ 	flash_dev -> async_complete  =  true;
992+ 	flash_dev -> async_ret  =  op_ret_val ;
993+ }
994+ void  HAL_FLASH_OperationErrorCallback (uint32_t  op_ret_val )
995+ {
996+ 	flash_dev -> async_error  =  true;
997+ 	flash_dev -> async_ret  =  op_ret_val ;
998+ }
999+ #endif  /* CONFIG_FLASH_STM32_ASYNC */ 
1000+ 
9301001static  int  stm32h7_flash_init (const  struct  device  * dev )
9311002{
9321003#if  DT_NODE_HAS_PROP (DT_INST (0 , st_stm32h7_flash_controller ), clocks )
@@ -946,6 +1017,12 @@ static int stm32h7_flash_init(const struct device *dev)
9461017	}
9471018#endif 
9481019	flash_stm32_sem_init (dev );
1020+ #if  defined(CONFIG_FLASH_STM32_ASYNC )
1021+ 	flash_dev  =  FLASH_STM32_PRIV (dev );
1022+ 	flash_stm32_async_sem_init (dev );
1023+ 	IRQ_CONNECT (FLASH_IRQn , 0 , flash_stm32_irq_handler , NULL , 0 );
1024+ 	irq_enable (FLASH_IRQn );
1025+ #endif  /* CONFIG_FLASH_STM32_ASYNC */ 
9491026
9501027	LOG_DBG ("Flash initialized. BS: %zu" , flash_stm32h7_parameters .write_block_size );
9511028
0 commit comments