Skip to content

Commit 4c18fa7

Browse files
wdfk-progmysterywolf
authored andcommitted
fix:[CAN][STM32]open时立刻启动can_start,还未完成其他配置,可能导致异常
1 parent e763e51 commit 4c18fa7

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

bsp/stm32/libraries/HAL_Drivers/drivers/drv_can.c

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2006-2023, RT-Thread Development Team
2+
* Copyright (c) 2006-2024 RT-Thread Development Team
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*
@@ -174,8 +174,6 @@ static rt_err_t _can_config(struct rt_can_device *can, struct can_configure *cfg
174174

175175
/* default filter config */
176176
HAL_CAN_ConfigFilter(&drv_can->CanHandle, &drv_can->FilterConfig);
177-
/* can start */
178-
HAL_CAN_Start(&drv_can->CanHandle);
179177

180178
return RT_EOK;
181179
}
@@ -321,7 +319,7 @@ static rt_err_t _can_control(struct rt_can_device *can, int cmd, void *arg)
321319
rt_uint32_t id_l = 0;
322320
rt_uint32_t mask_h = 0;
323321
rt_uint32_t mask_l = 0;
324-
rt_uint32_t mask_l_tail = 0; //CAN_FxR2 bit [2:0]
322+
rt_uint32_t mask_l_tail = 0; /*CAN_FxR2 bit [2:0]*/
325323

326324
if (RT_NULL == arg)
327325
{
@@ -458,7 +456,6 @@ static rt_err_t _can_control(struct rt_can_device *can, int cmd, void *arg)
458456
}
459457
break;
460458
case RT_CAN_CMD_GET_STATUS:
461-
{
462459
rt_uint32_t errtype;
463460
errtype = drv_can->CanHandle.Instance->ESR;
464461
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)
467464
drv_can->device.status.errcode = errtype & 0x07;
468465

469466
rt_memcpy(arg, &drv_can->device.status, sizeof(drv_can->device.status));
470-
}
471-
break;
467+
break;
468+
case RT_CAN_CMD_START:
469+
argval = (rt_uint32_t) arg;
470+
if (argval == 0)
471+
{
472+
HAL_CAN_Stop(&drv_can->CanHandle);
473+
}
474+
else
475+
{
476+
HAL_CAN_Start(&drv_can->CanHandle);
477+
}
478+
479+
break;
472480
}
473481

474482
return RT_EOK;

components/drivers/can/dev_can.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2006-2023, RT-Thread Development Team
2+
* Copyright (c) 2006-2024 RT-Thread Development Team
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*
@@ -435,7 +435,7 @@ static rt_err_t rt_can_close(struct rt_device *dev)
435435
}
436436

437437
can->ops->control(can, RT_DEVICE_CTRL_CLR_INT, (void *)RT_DEVICE_CAN_INT_ERR);
438-
438+
can->ops->control(can, RT_CAN_CMD_START, RT_FALSE);
439439
CAN_UNLOCK(can);
440440

441441
return RT_EOK;

components/drivers/include/drivers/dev_can.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ enum CANBAUD
111111
* res = rt_device_control(can_dev, RT_CAN_CMD_SET_FILTER, &cfg);
112112
* RT_ASSERT(res == RT_EOK);
113113
* #endif
114-
*
114+
* res = RT_TRUE;
115+
* res = rt_device_control(can_dev, RT_CAN_CMD_START, &res);
115116
* while (1)
116117
* {
117118
* // hdr 值为 - 1,表示直接从 uselist 链表读取数据
@@ -345,6 +346,7 @@ struct rt_can_ops;
345346
#define RT_CAN_CMD_SET_CANFD 0x1A
346347
#define RT_CAN_CMD_SET_BAUD_FD 0x1B
347348
#define RT_CAN_CMD_SET_BITTIMING 0x1C
349+
#define RT_CAN_CMD_START 0x1D
348350

349351
#define RT_DEVICE_CAN_INT_ERR 0x1000
350352

0 commit comments

Comments
 (0)