fix:[CAN][STM32]open时立刻启动can_start,还未完成其他配置,可能导致异常

This commit is contained in:
wdfk-prog 2024-11-13 09:18:59 +08:00 committed by Meco Man
parent e763e51e49
commit 4c18fa7e21
3 changed files with 20 additions and 10 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2006-2023, RT-Thread Development Team * Copyright (c) 2006-2024 RT-Thread Development Team
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
* *
@ -174,8 +174,6 @@ static rt_err_t _can_config(struct rt_can_device *can, struct can_configure *cfg
/* default filter config */ /* default filter config */
HAL_CAN_ConfigFilter(&drv_can->CanHandle, &drv_can->FilterConfig); HAL_CAN_ConfigFilter(&drv_can->CanHandle, &drv_can->FilterConfig);
/* can start */
HAL_CAN_Start(&drv_can->CanHandle);
return RT_EOK; return RT_EOK;
} }
@ -321,7 +319,7 @@ static rt_err_t _can_control(struct rt_can_device *can, int cmd, void *arg)
rt_uint32_t id_l = 0; rt_uint32_t id_l = 0;
rt_uint32_t mask_h = 0; rt_uint32_t mask_h = 0;
rt_uint32_t mask_l = 0; rt_uint32_t mask_l = 0;
rt_uint32_t mask_l_tail = 0; //CAN_FxR2 bit [2:0] rt_uint32_t mask_l_tail = 0; /*CAN_FxR2 bit [2:0]*/
if (RT_NULL == arg) if (RT_NULL == arg)
{ {
@ -458,7 +456,6 @@ static rt_err_t _can_control(struct rt_can_device *can, int cmd, void *arg)
} }
break; break;
case RT_CAN_CMD_GET_STATUS: case RT_CAN_CMD_GET_STATUS:
{
rt_uint32_t errtype; rt_uint32_t errtype;
errtype = drv_can->CanHandle.Instance->ESR; errtype = drv_can->CanHandle.Instance->ESR;
drv_can->device.status.rcverrcnt = errtype >> 24; drv_can->device.status.rcverrcnt = errtype >> 24;
@ -467,8 +464,19 @@ static rt_err_t _can_control(struct rt_can_device *can, int cmd, void *arg)
drv_can->device.status.errcode = errtype & 0x07; drv_can->device.status.errcode = errtype & 0x07;
rt_memcpy(arg, &drv_can->device.status, sizeof(drv_can->device.status)); rt_memcpy(arg, &drv_can->device.status, sizeof(drv_can->device.status));
} break;
break; case RT_CAN_CMD_START:
argval = (rt_uint32_t) arg;
if (argval == 0)
{
HAL_CAN_Stop(&drv_can->CanHandle);
}
else
{
HAL_CAN_Start(&drv_can->CanHandle);
}
break;
} }
return RT_EOK; return RT_EOK;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2006-2023, RT-Thread Development Team * Copyright (c) 2006-2024 RT-Thread Development Team
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
* *
@ -435,7 +435,7 @@ static rt_err_t rt_can_close(struct rt_device *dev)
} }
can->ops->control(can, RT_DEVICE_CTRL_CLR_INT, (void *)RT_DEVICE_CAN_INT_ERR); can->ops->control(can, RT_DEVICE_CTRL_CLR_INT, (void *)RT_DEVICE_CAN_INT_ERR);
can->ops->control(can, RT_CAN_CMD_START, RT_FALSE);
CAN_UNLOCK(can); CAN_UNLOCK(can);
return RT_EOK; return RT_EOK;

View File

@ -111,7 +111,8 @@ enum CANBAUD
* res = rt_device_control(can_dev, RT_CAN_CMD_SET_FILTER, &cfg); * res = rt_device_control(can_dev, RT_CAN_CMD_SET_FILTER, &cfg);
* RT_ASSERT(res == RT_EOK); * RT_ASSERT(res == RT_EOK);
* #endif * #endif
* * res = RT_TRUE;
* res = rt_device_control(can_dev, RT_CAN_CMD_START, &res);
* while (1) * while (1)
* { * {
* // hdr 值为 - 1表示直接从 uselist 链表读取数据 * // hdr 值为 - 1表示直接从 uselist 链表读取数据
@ -345,6 +346,7 @@ struct rt_can_ops;
#define RT_CAN_CMD_SET_CANFD 0x1A #define RT_CAN_CMD_SET_CANFD 0x1A
#define RT_CAN_CMD_SET_BAUD_FD 0x1B #define RT_CAN_CMD_SET_BAUD_FD 0x1B
#define RT_CAN_CMD_SET_BITTIMING 0x1C #define RT_CAN_CMD_SET_BITTIMING 0x1C
#define RT_CAN_CMD_START 0x1D
#define RT_DEVICE_CAN_INT_ERR 0x1000 #define RT_DEVICE_CAN_INT_ERR 0x1000