@@ -214,6 +214,11 @@ static void hal_semaphore_give(I2CBusState *bus_state)
214214 // If this fails, something is very wrong
215215 xSemaphoreGive (bus_state -> event_semaphore );
216216}
217+ static portBASE_TYPE hal_semaphore_give_from_isr (I2CBusState * bus ) {
218+ portBASE_TYPE should_context_switch = pdFALSE ;
219+ (void )xSemaphoreGiveFromISR (bus -> event_semaphore , & should_context_switch );
220+ return should_context_switch ;
221+ }
217222
218223static void I2Cx_IRQHandler (uint16_t index )
219224{
@@ -228,7 +233,7 @@ static void I2Cx_IRQHandler(uint16_t index)
228233
229234 if ((HAL_I2C_STATE_BUSY_TX != handle -> State ) && (HAL_I2C_STATE_BUSY_RX != handle -> State ))
230235 {
231- hal_semaphore_give (bus -> state );
236+ hal_semaphore_give_from_isr (bus -> state );
232237 HAL_I2C_StateTypeDef i2c_state = HAL_I2C_GetState (handle );
233238 if (i2c_state == HAL_I2C_STATE_READY )
234239 bus -> state -> transfer_event = I2CTransferEvent_TransferComplete ;
@@ -432,11 +437,11 @@ static HAL_StatusTypeDef master_xfer(struct I2CBusHal * i2c_hal, struct rt_i2c_m
432437 {
433438 mem_addr_type = I2C_MEMADD_SIZE_16BIT ;
434439 }
435- PBL_LOG (LOG_LEVEL_ALWAYS , "msg->addr = 0x%x, msg->mem_addr = 0x%x, type = %d, buf = 0x%x, len = 0x%d, timeout = %d;" ,
436- (unsigned int )msg -> addr , (unsigned int )msg -> mem_addr , (int )mem_addr_type , (unsigned int )msg -> buf , (int )msg -> len , (int )bf0_i2c -> bf0_i2c_cfg -> timeout );
440+ PBL_LOG (LOG_LEVEL_ALWAYS , "msg->addr = 0x%x, msg->mem_addr = 0x%x, type = %d, buf = 0x%x, len = 0x%d, timeout = %d;" ,
441+ (unsigned int )msg -> addr , (unsigned int )msg -> mem_addr , (int )mem_addr_type , (unsigned int )msg -> buf , (int )msg -> len , (int )bf0_i2c -> bf0_i2c_cfg -> timeout );
437442 if (msg -> flags & RT_I2C_RD )
438443 {
439-
444+
440445 if ((bf0_i2c -> i2c_dma_flag ) && (bf0_i2c -> bf0_i2c_cfg -> open_flag & RT_DEVICE_FLAG_DMA_RX ))
441446 {
442447 HAL_DMA_Init (& bf0_i2c -> dma .dma_rx );
@@ -509,7 +514,7 @@ static HAL_StatusTypeDef master_xfer(struct I2CBusHal * i2c_hal, struct rt_i2c_m
509514 }
510515 }
511516 }
512- PBL_LOG (LOG_LEVEL_ALWAYS , "I2C hal transmit result = %d" , (int )status );
517+ PBL_LOG (LOG_LEVEL_ALWAYS , "I2C hal transmit result = %d" , (int )status );
513518
514519 if (HAL_OK != status ) goto exit ;
515520#if 1
@@ -632,12 +637,16 @@ void i2c_hal_abort_transfer(I2CBus *bus)
632637void i2c_hal_start_transfer (I2CBus * bus )
633638{
634639 struct I2CBusHal * hal = (struct I2CBusHal * )bus -> hal ;
635- PBL_LOG (LOG_LEVEL_ALWAYS ,"i2c_hal_start_transfer, 0x%p" , hal );
640+ PBL_LOG (LOG_LEVEL_ALWAYS ,"i2c_hal_start_transfer, 0x%p" , hal );
636641 HAL_StatusTypeDef status = master_xfer (hal , & msgs [0 ], msgs_num );
637642 if (status == HAL_BUSY )
638643 return ;
639644 if (status == HAL_OK )
645+ {
640646 bus -> state -> transfer_event = I2CTransferEvent_TransferComplete ;
647+ if ((bus -> hal -> i2c_dma_flag != 1 ) && (bus -> hal -> i2c_int_flag != 1 ))
648+ hal_semaphore_give (bus -> state );
649+ }
641650 else if (status == HAL_TIMEOUT )
642651 bus -> state -> transfer_event = I2CTransferEvent_Timeout ;
643652 else
@@ -871,15 +880,15 @@ int rt_hw_i2c_init(struct I2CBusHal * i2c_hal, bf0_i2c_config_t *cfg, struct rt_
871880 __HAL_LINKDMA (& (i2c_hal -> handle ), hdmarx , i2c_hal -> dma .dma_rx );
872881 __HAL_LINKDMA (& (i2c_hal -> handle ), hdmatx , i2c_hal -> dma .dma_tx );
873882 HAL_I2C_DMA_Init (& (i2c_hal -> handle ), cfg -> dma_rx , cfg -> dma_tx );
874-
875- HAL_NVIC_SetPriority (i2c_hal -> bf0_i2c_cfg -> dma_rx -> dma_irq , 5 , 0 );
876- NVIC_EnableIRQ (i2c_hal -> bf0_i2c_cfg -> dma_rx -> dma_irq );
877- HAL_NVIC_SetPriority (i2c_hal -> bf0_i2c_cfg -> dma_rx -> dma_irq , 5 , 0 );
878- NVIC_EnableIRQ (i2c_hal -> bf0_i2c_cfg -> dma_rx -> dma_irq );
883+
884+ HAL_NVIC_SetPriority (i2c_hal -> bf0_i2c_cfg -> dma_rx -> dma_irq , 5 , 0 );
885+ NVIC_EnableIRQ (i2c_hal -> bf0_i2c_cfg -> dma_rx -> dma_irq );
886+ HAL_NVIC_SetPriority (i2c_hal -> bf0_i2c_cfg -> dma_rx -> dma_irq , 5 , 0 );
887+ NVIC_EnableIRQ (i2c_hal -> bf0_i2c_cfg -> dma_rx -> dma_irq );
879888 }
880889 else if (i2c_hal -> i2c_int_flag )
881890 {
882- HAL_NVIC_SetPriority (i2c_hal -> bf0_i2c_cfg -> irq_type , 5 , 0 );
891+ HAL_NVIC_SetPriority (i2c_hal -> bf0_i2c_cfg -> irq_type , 5 , 0 );
883892 NVIC_EnableIRQ (i2c_hal -> bf0_i2c_cfg -> irq_type );
884893 }
885894 ret = i2c_bus_configure (i2c_hal , cfg_default );
0 commit comments