@@ -36,6 +36,20 @@ typedef uint8_t flash_prg_t;
3636#error  Write block size must be a power of 2, from 1 to 8
3737#endif 
3838
39+ #if  defined(CONFIG_FLASH_STM32_ASYNC )
40+ #if  FLASH_STM32_WRITE_BLOCK_SIZE  ==  8 
41+ #define  FLASH_TYPEPROGRAM_SIZE  FLASH_TYPEPROGRAM_DOUBLEWORD
42+ #elif  FLASH_STM32_WRITE_BLOCK_SIZE  ==  4 
43+ #define  FLASH_TYPEPROGRAM_SIZE  FLASH_TYPEPROGRAM_WORD
44+ #elif  FLASH_STM32_WRITE_BLOCK_SIZE  ==  2 
45+ #define  FLASH_TYPEPROGRAM_SIZE  FLASH_TYPEPROGRAM_HALFWORD
46+ #elif  FLASH_STM32_WRITE_BLOCK_SIZE  ==  1 
47+ #define  FLASH_TYPEPROGRAM_SIZE  FLASH_TYPEPROGRAM_BYTE
48+ #else 
49+ #error  Write block size must be a power of 2, from 1 to 8
50+ #endif 
51+ #endif  /* CONFIG_FLASH_STM32_ASYNC */ 
52+ 
3953bool  flash_stm32_valid_range (const  struct  device  * dev , off_t  offset ,
4054			     uint32_t  len ,
4155			     bool  write )
@@ -82,6 +96,27 @@ static inline void flush_cache(FLASH_TypeDef *regs)
8296
8397static  int  write_value (const  struct  device  * dev , off_t  offset , flash_prg_t  val )
8498{
99+ #if  defined(CONFIG_FLASH_STM32_ASYNC )
100+ 	FLASH_STM32_PRIV (dev )-> async_complete  =  false;
101+ 	FLASH_STM32_PRIV (dev )-> async_error  =  false;
102+ 
103+ 	HAL_FLASH_Program_IT (FLASH_TYPEPROGRAM_SIZE , offset  +  FLASH_STM32_BASE_ADDRESS , val );
104+ 	k_sem_take (& FLASH_STM32_PRIV (dev )-> async_sem , K_FOREVER );
105+ 	if  (FLASH_STM32_PRIV (dev )-> async_complete ) {
106+ 		LOG_DBG ("Flash write successful. Wrote 0x%x at 0x%lx" , val ,
107+ 			offset  +  FLASH_STM32_BASE_ADDRESS );
108+ 		return  0 ;
109+ 	}
110+ 
111+ 	if  (FLASH_STM32_PRIV (dev )-> async_error ) {
112+ 		LOG_ERR ("Flash write failed at 0x%x" , FLASH_STM32_PRIV (dev )-> async_ret );
113+ 		return  - EIO ;
114+ 	}
115+ 
116+ 	/* Should never be reached */ 
117+ 	return  - EFAULT ;
118+ #else  /* CONFIG_FLASH_STM32_ASYNC */ 
119+ 
85120	FLASH_TypeDef  * regs  =  FLASH_STM32_REGS (dev );
86121#if  defined(FLASH_OPTCR_DB1M )
87122	bool  dcache_enabled  =  false;
@@ -132,10 +167,40 @@ static int write_value(const struct device *dev, off_t offset, flash_prg_t val)
132167#endif  /* FLASH_OPTCR_DB1M */ 
133168
134169	return  rc ;
170+ #endif  /* CONFIG_FLASH_STM32_ASYNC */ 
135171}
136172
137173static  int  erase_sector (const  struct  device  * dev , uint32_t  sector )
138174{
175+ #if  defined(CONFIG_FLASH_STM32_ASYNC )
176+ 	FLASH_STM32_PRIV (dev )-> async_complete  =  false;
177+ 	FLASH_STM32_PRIV (dev )-> async_error  =  false;
178+ 
179+ 	FLASH_EraseInitTypeDef  erase_init  =  {
180+ 		.TypeErase  =  FLASH_TYPEERASE_SECTORS ,
181+ 		.Banks  =  1 , /* dual bank flash not supported */ 
182+ 		.Sector  =  sector ,
183+ 		.NbSectors  =  1 ,
184+ 		.VoltageRange  =  FLASH_VOLTAGE_RANGE_4 ,
185+ 	};
186+ 
187+ 	HAL_FLASHEx_Erase_IT (& erase_init );
188+ 	k_sem_take (& FLASH_STM32_PRIV (dev )-> async_sem , K_FOREVER );
189+ 	if  (FLASH_STM32_PRIV (dev )-> async_complete ) {
190+ 		LOG_DBG ("Flash erase successful. Erased sector %d at 0x%x" , sector ,
191+ 			FLASH_STM32_PRIV (dev )-> async_ret );
192+ 		return  =  0 ;
193+ 	}
194+ 
195+ 	if  (FLASH_STM32_PRIV (dev )-> async_error ) {
196+ 		LOG_ERR ("Flash erase failed at 0x%x" , FLASH_STM32_PRIV (dev )-> async_ret );
197+ 		return  =  - EIO ;
198+ 	}
199+ 
200+ 	/* Should never be reached */ 
201+ 	return  - EFAULT ;
202+ #else  /* CONFIG_FLASH_STM32_ASYNC */ 
203+ 
139204	FLASH_TypeDef  * regs  =  FLASH_STM32_REGS (dev );
140205	uint32_t  tmp ;
141206	int  rc ;
@@ -182,6 +247,7 @@ static int erase_sector(const struct device *dev, uint32_t sector)
182247	regs -> CR  &= ~(FLASH_CR_SER  | FLASH_CR_SNB );
183248
184249	return  rc ;
250+ #endif  /* CONFIG_FLASH_STM32_ASYNC */ 
185251}
186252
187253int  flash_stm32_block_erase_loop (const  struct  device  * dev ,
0 commit comments