CAN

STM32F107 CAN2 BUG

发布于 2020-06-20 19:21:05
原驱动使用CAN2时可以发送无法接收,需修改配置里面如下两片,有说F4只修改上面一处的,但测试不可以

static rt_err_t drv_configure(struct rt_can_device *dev_can,
struct can_configure *cfg)
{
struct stm32_drv_can *drv_can;
rt_uint32_t baud_index;
CAN_InitTypeDef *drv_init;
CAN_FilterConfTypeDef *filterConf;

RT_ASSERT(dev_can);
RT_ASSERT(cfg);

drv_can = (struct stm32_drv_can *)dev_can->parent.user_data;
drv_init = &drv_can->CanHandle.Init;

drv_init->TTCM = DISABLE;
drv_init->ABOM = ENABLE;
drv_init->AWUM = DISABLE;
drv_init->NART = DISABLE;
drv_init->RFLM = DISABLE;
drv_init->TXFP = DISABLE;

switch (cfg->mode)
{
case RT_CAN_MODE_NORMAL:
drv_init->Mode = CAN_MODE_NORMAL;
break;
case RT_CAN_MODE_LISEN:
drv_init->Mode = CAN_MODE_SILENT;
break;
case RT_CAN_MODE_LOOPBACK:
drv_init->Mode = CAN_MODE_LOOPBACK;
break;
case RT_CAN_MODE_LOOPBACKANLISEN:
drv_init->Mode = CAN_MODE_SILENT_LOOPBACK;
break;
}

baud_index = get_can_baud_index(cfg->baud_rate);
drv_init->SJW = BAUD_DATA(SJW, baud_index);
drv_init->BS1 = BAUD_DATA(BS1, baud_index);
drv_init->BS2 = BAUD_DATA(BS2, baud_index);
drv_init->Prescaler = BAUD_DATA(RRESCL, baud_index);
if (HAL_CAN_Init(&drv_can->CanHandle) != HAL_OK)
{
return RT_ERROR;
}

/* Filter conf */
filterConf = &drv_can->FilterConfig;
filterConf->FilterNumber = 0;
if(drv_can->CanHandle.Instance==CAN2)
filterConf->FilterNumber = 14;
else
filterConf->FilterNumber = 0;
filterConf->FilterMode = CAN_FILTERMODE_IDMASK;
filterConf->FilterScale = CAN_FILTERSCALE_32BIT;
filterConf->FilterIdHigh = 0x0000;
filterConf->FilterIdLow = 0x0000;
filterConf->FilterMaskIdHigh = 0x0000;
filterConf->FilterMaskIdLow = 0x0000;
filterConf->FilterFIFOAssignment = CAN_FIFO0;
filterConf->FilterActivation = ENABLE;
filterConf->BankNumber = 14;
if(drv_can->CanHandle.Instance==CAN2)
filterConf->BankNumber = 14;
else
filterConf->BankNumber = 0;
HAL_CAN_ConfigFilter(&drv_can->CanHandle, filterConf);
return RT_EOK;
}

查看更多

关注者
0
被浏览
148
1 个回答
whj467467222
whj467467222 2020-06-20
确实是这样的,F1 F4的CAN2的是从14开始的。
另外楼主要确认一下是不是HAL库升级导致的,我记得去年有一波HAL库集体升级都是对CAN的结构体做了修改。

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友