芯片:STM32F429IGTx
开发板:野火挑战者F429开发板
开发环境:KEIL 5.24.2
CUBEMX库:STM32F4 1.16.0
现象:配置的CAN1可以和周立功的USBCAN-I-MINI进行正常的收发,CAN2在发送的时候只有很短的一小段波形,而且只在上电后第一次发送有波形,后边在怎么发送都没有波形(通过示波器直接观察PB13引脚).两个CAN外设波特率设置都是500k.
CAN_FilterConfTypeDef sFilterConfig;
static CanTxMsgTypeDef TxMessage1;
static CanRxMsgTypeDef RxMessage1;
static CanTxMsgTypeDef TxMessage2;
static CanRxMsgTypeDef RxMessage2;
void MX_CAN1_Init(void)
{
hcan1.Instance = CAN1;
hcan1.Init.Prescaler = 10;
hcan1.Init.Mode = CAN_MODE_NORMAL;
hcan1.Init.SJW = CAN_SJW_2TQ;
hcan1.Init.BS1 = CAN_BS1_5TQ;
hcan1.Init.BS2 = CAN_BS2_3TQ;
hcan1.Init.TTCM = DISABLE;
hcan1.Init.ABOM = DISABLE;
hcan1.Init.AWUM = DISABLE;
hcan1.Init.NART = DISABLE;
hcan1.Init.RFLM = DISABLE;
hcan1.Init.TXFP = DISABLE;
if (HAL_CAN_Init(&hcan1) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
}
/* CAN2 init function */
void MX_CAN2_Init(void)
{
hcan2.Instance = CAN2;
hcan2.Init.Prescaler = 10;
hcan2.Init.Mode = CAN_MODE_NORMAL;
hcan2.Init.SJW = CAN_SJW_2TQ;
hcan2.Init.BS1 = CAN_BS1_5TQ;
hcan2.Init.BS2 = CAN_BS2_3TQ;
hcan2.Init.TTCM = DISABLE;
hcan2.Init.ABOM = DISABLE;
hcan2.Init.AWUM = DISABLE;
hcan2.Init.NART = DISABLE;
hcan2.Init.RFLM = DISABLE;
hcan2.Init.TXFP = DISABLE;
if (HAL_CAN_Init(&hcan2) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
}
hcan1.pTxMsg = &TxMessage1;
hcan1.pRxMsg = &RxMessage1;
hcan2.pTxMsg = &TxMessage2;
hcan2.pRxMsg = &RxMessage2;
sFilterConfig.FilterNumber = 0;
sFilterConfig.FilterMode = CAN_FILTERMODE_IDLIST;
sFilterConfig.FilterScale = CAN_FILTERSCALE_16BIT;
sFilterConfig.FilterIdHigh = (uint32_t)0x0123<<5;
sFilterConfig.FilterIdLow = CAN_ID_STD;
sFilterConfig.FilterMaskIdHigh = 0xFFFF;
sFilterConfig.FilterMaskIdLow = 0xFFFF;
sFilterConfig.FilterFIFOAssignment = CAN_FILTER_FIFO0;
sFilterConfig.FilterActivation = ENABLE;
sFilterConfig.BankNumber = 14;
if(HAL_CAN_ConfigFilter(&hcan1, &sFilterConfig) != HAL_OK)
{
DEBUG("HAL_CAN_ConfigFilter ERR\r\n");
}
sFilterConfig.FilterNumber = 14;
sFilterConfig.FilterFIFOAssignment = CAN_FILTER_FIFO1;
if(HAL_CAN_ConfigFilter(&hcan2, &sFilterConfig) != HAL_OK)
{
DEBUG("HAL_CAN_ConfigFilter ERR\r\n");
}
hcan1.pTxMsg->StdId = 0x123;
hcan1.pTxMsg->RTR = CAN_RTR_DATA;
hcan1.pTxMsg->IDE = CAN_ID_STD;
hcan1.pTxMsg->DLC = 8;
hcan1.pTxMsg->Data[0] = 0x01;
hcan1.pTxMsg->Data[1] = 0x02;
hcan1.pTxMsg->Data[2] = 0x03;
hcan1.pTxMsg->Data[3] = 0x04;
hcan1.pTxMsg->Data[4] = 0x05;
hcan1.pTxMsg->Data[5] = 0x06;
hcan1.pTxMsg->Data[6] = 0x07;
hcan1.pTxMsg->Data[7] = 0x08;
hcan2.pTxMsg->StdId = 0x123;
hcan2.pTxMsg->RTR = CAN_RTR_DATA;
hcan2.pTxMsg->IDE = CAN_ID_STD;
hcan2.pTxMsg->DLC = 8;
hcan2.pTxMsg->Data[0] = 0x01;
hcan2.pTxMsg->Data[1] = 0x02;
hcan2.pTxMsg->Data[2] = 0x03;
hcan2.pTxMsg->Data[3] = 0x04;
hcan2.pTxMsg->Data[4] = 0x05;
hcan2.pTxMsg->Data[5] = 0x06;
hcan2.pTxMsg->Data[6] = 0x07;
hcan2.pTxMsg->Data[7] = 0x08;
if(HAL_CAN_Receive_IT(&hcan1, CAN_FIFO0) != HAL_OK)
{
DEBUG("HAL_CAN1_Receive_IT ERR\r\n");
}
if(HAL_CAN_Receive_IT(&hcan2, CAN_FIFO1) != HAL_OK)
{
DEBUG("HAL_CAN2_Receive_IT ERR\r\n");
}
/*******************************
main循环
*******************************/
CAN2_FLAG = HAL_CAN_Transmit(&hcan2, 10);
if(CAN2_FLAG == HAL_OK)
{
DEBUG("5AL_CAN_Transmit HAL_OK\r\n");
}
else if(CAN2_FLAG == HAL_ERROR)
{
DEBUG("6AL_CAN_Transmit HAL_ERROR\r\n");
}
else if(CAN2_FLAG == HAL_BUSY)
{
DEBUG("7AL_CAN_Transmit HAL_BUSY\r\n");
}
else if(CAN2_FLAG == HAL_TIMEOUT)
{
DEBUG("8AL_CAN_Transmit HAL_TIMEOUT\r\n");
}
CAN2_FLAG = HAL_OK;
CAN1_FLAG = HAL_CAN_Transmit(&hcan1, 10);
if(CAN1_FLAG == HAL_OK)
{
DEBUG("1AL_CAN_Transmit HAL_OK\r\n");
}
else if(CAN1_FLAG == HAL_ERROR)
{
DEBUG("2AL_CAN_Transmit HAL_ERROR\r\n");
}
else if(CAN1_FLAG == HAL_BUSY)
{
DEBUG("3AL_CAN_Transmit HAL_BUSY\r\n");
}
else if(CAN1_FLAG == HAL_TIMEOUT)
{
DEBUG("4AL_CAN_Transmit HAL_TIMEOUT\r\n");
}
CAN1_FLAG = HAL_OK;
void HAL_CAN_RxCpltCallback(CAN_HandleTypeDef* CanHandle)
{
int i=0;
if(CanHandle ==&hcan1)
{
__HAL_CAN_ENABLE_IT(&hcan1,CAN_IT_FMP0);
printf("1pRxMsg->StdId : 0x%x\r\n",hcan1.pRxMsg->StdId);
printf("1pRxMsg->DLC : 0x%x\r\n",hcan1.pRxMsg->DLC);
printf("1DATA :");
for(i=0;i<hcan1.pRxMsg->DLC;i++)
{
printf(" 0x%x ",hcan1.pRxMsg->Data);
}
printf("\r\n\r\n");
}
if(CanHandle ==&hcan2)
{
__HAL_CAN_ENABLE_IT(&hcan2,CAN_IT_FMP1);
printf("2pRxMsg->StdId : 0x%x\r\n",hcan2.pRxMsg->StdId);
printf("2pRxMsg->DLC : 0x%x\r\n",hcan2.pRxMsg->DLC);
printf("2DATA :");
for(i=0;i<hcan2.pRxMsg->DLC;i++)
{
printf(" 0x%x ",hcan2.pRxMsg->Data);
}
printf("\r\n\r\n");
}
}
当更改sFilterConfig.FilterNumber = 14;时网上又说仍然要执行HAL_CAN_ConfigFilter(&hcan1, &sFilterConfig)而不能执行HAL_CAN_ConfigFilter(&hcan2, &sFilterConfig),这种方法我也试过,仍旧不行,并且同样尝试过两个CAN使用同一个CAN_FILTER_FIFO0的情况,依然不可行.
示波器上能看到CAN2发送管脚PB13发出的波形大概只有两三位然后就没有了,后边在怎么执行CAN2发送命令也不会再有任何波形.请各位帮忙看一下究竟是哪里出了问题?困扰我三天了死活找不到结症所在