@@ -195,6 +195,8 @@ static rt_err_t stm32_spi_init(struct stm32_spi *spi_drv, struct rt_spi_configur
195
195
spi_handle -> Init .BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256 ;
196
196
}
197
197
198
+ cfg -> usage_freq = SPI_CLOCK / (rt_size_t )pow (2 ,(spi_handle -> Init .BaudRatePrescaler >> 28 ) + 1 );
199
+
198
200
LOG_D ("sys freq: %d, pclk freq: %d, SPI limiting freq: %d, SPI usage freq: %d" ,
199
201
#if defined(SOC_SERIES_STM32MP1 )
200
202
HAL_RCC_GetSystemCoreClockFreq (),
@@ -203,7 +205,7 @@ static rt_err_t stm32_spi_init(struct stm32_spi *spi_drv, struct rt_spi_configur
203
205
#endif
204
206
SPI_CLOCK ,
205
207
cfg -> max_hz ,
206
- SPI_CLOCK / ( rt_size_t ) pow ( 2 ,( spi_handle -> Init . BaudRatePrescaler >> 28 ) + 1 ) );
208
+ cfg -> usage_freq );
207
209
208
210
if (cfg -> mode & RT_SPI_MSB )
209
211
{
@@ -280,7 +282,7 @@ static rt_err_t stm32_spi_init(struct stm32_spi *spi_drv, struct rt_spi_configur
280
282
281
283
static rt_ssize_t spixfer (struct rt_spi_device * device , struct rt_spi_message * message )
282
284
{
283
- #define DMA_TRANS_MIN_LEN 10 /* only buffer length >= DMA_TRANS_MIN_LEN will use DMA mode */
285
+ #define DMA_TRANS_MIN_LEN 10 /* only buffer length >= DMA_TRANS_MIN_LEN will use DMA mode */
284
286
285
287
HAL_StatusTypeDef state = HAL_OK ;
286
288
rt_size_t message_length , already_send_length ;
@@ -294,6 +296,8 @@ static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *m
294
296
295
297
struct stm32_spi * spi_drv = rt_container_of (device -> bus , struct stm32_spi , spi_bus );
296
298
SPI_HandleTypeDef * spi_handle = & spi_drv -> handle ;
299
+ rt_uint32_t timeout = message -> length / (1000 * spi_drv -> cfg -> usage_freq ) + 5 ;
300
+
297
301
298
302
if (message -> cs_take && !(device -> config .mode & RT_SPI_NO_CS ) && (device -> cs_pin != PIN_NONE ))
299
303
{
@@ -424,7 +428,7 @@ static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *m
424
428
}
425
429
else
426
430
{
427
- state = HAL_SPI_TransmitReceive (spi_handle , (uint8_t * )send_buf , (uint8_t * )recv_buf , send_length , 1000 );
431
+ state = HAL_SPI_TransmitReceive (spi_handle , (uint8_t * )send_buf , (uint8_t * )recv_buf , send_length , timeout );
428
432
}
429
433
}
430
434
else if (message -> send_buf )
@@ -435,7 +439,7 @@ static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *m
435
439
}
436
440
else
437
441
{
438
- state = HAL_SPI_Transmit (spi_handle , (uint8_t * )send_buf , send_length , 1000 );
442
+ state = HAL_SPI_Transmit (spi_handle , (uint8_t * )send_buf , send_length , timeout );
439
443
}
440
444
441
445
if (message -> cs_release && (device -> config .mode & RT_SPI_3WIRE ))
@@ -455,7 +459,7 @@ static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *m
455
459
{
456
460
/* clear the old error flag */
457
461
__HAL_SPI_CLEAR_OVRFLAG (spi_handle );
458
- state = HAL_SPI_Receive (spi_handle , (uint8_t * )recv_buf , send_length , 1000 );
462
+ state = HAL_SPI_Receive (spi_handle , (uint8_t * )recv_buf , send_length , timeout );
459
463
}
460
464
}
461
465
else
@@ -482,7 +486,7 @@ static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *m
482
486
if ((spi_drv -> spi_dma_flag & (SPI_USING_TX_DMA_FLAG | SPI_USING_RX_DMA_FLAG )) && (send_length >= DMA_TRANS_MIN_LEN ))
483
487
{
484
488
/* blocking the thread,and the other tasks can run */
485
- if (rt_completion_wait (& spi_drv -> cpt , 1000 ) != RT_EOK )
489
+ if (rt_completion_wait (& spi_drv -> cpt , timeout ) != RT_EOK )
486
490
{
487
491
state = HAL_ERROR ;
488
492
LOG_E ("wait for DMA interrupt overtime!" );
0 commit comments