原因:若在接收的时候发送数据,
发送中的处理:
hal_can_transmit()中会进行hal_lock(hcan);然后更改can状态为HAL_CAN_STATE_BUSY_TX相关的。
再谈一下接收中的处理,不用多说肯定是在接收中断国会开启下一次接收,即hal_can_receive_it();那么这里边又干了什么:
/* Check if CAN state is not busy for RX FIFO0 */
if((FIFONumber == CAN_FIFO0) && ((hcan->State == HAL_CAN_STATE_BUSY_RX0) || \
(hcan->State == HAL_CAN_STATE_BUSY_TX_RX0) || \
(hcan->State == HAL_CAN_STATE_BUSY_RX0_RX1) || \
(hcan->State == HAL_CAN_STATE_BUSY_TX_RX0_RX1)))
{
return HAL_BUSY;
}
/* Check if CAN state is not busy for RX FIFO1 */
if((FIFONumber == CAN_FIFO1) && ((hcan->State == HAL_CAN_STATE_BUSY_RX1) || \
(hcan->State == HAL_CAN_STATE_BUSY_TX_RX1) || \
(hcan->State == HAL_CAN_STATE_BUSY_RX0_RX1) || \
(hcan->State == HAL_CAN_STATE_BUSY_TX_RX0_RX1)))
{
return HAL_BUSY;
}
这儿会直接返回,也就是说如果此时处于这些状态说明一定有执行过hal_lock(), 既然我们返回了那相应的中断标签并没有得到相应的处理。
对比后发现hal_can_transmit和hal_can_receive_it中在lock之后都会执行以下语句
__HAL_CAN_ENABLE_IT(hcan, CAN_IT_EWG |
CAN_IT_EPV |
CAN_IT_BOF |
CAN_IT_LEC |
CAN_IT_ERR |
CAN_IT_TME);
不过hal_can_receive_it中在unlock之后还多执行了
/* Process unlocked */
__HAL_UNLOCK(hcan);
if(FIFONumber == CAN_FIFO0)
{
/* Enable FIFO 0 overrun and message pending Interrupt */
__HAL_CAN_ENABLE_IT(hcan, CAN_IT_FOV0 | CAN_IT_FMP0);
}
else
{
/* Enable FIFO 1 overrun and message pending Interrupt */
__HAL_CAN_ENABLE_IT(hcan, CAN_IT_FOV1 | CAN_IT_FMP1);
}
好,那我们是不是如果hal_can_receive_it(&hcan1)== hal_busy时添加执行以上语句就可以了 ,实现证明是可以的。
那在接收中断中对果hal_can_receive_it作如下处理:
if( HAL_BUSY == HAL_CAN_Receive_IT(hcanx, CAN_FIFO0))//开启中断接收
{
/* Enable FIFO 0 overrun and message pending Interrupt */
__HAL_CAN_ENABLE_IT(&hcan1, CAN_IT_FOV0 | CAN_IT_FMP0);
}