diff --git a/components/drivers/can/Kconfig b/components/drivers/can/Kconfig index ccb4348db28..3bdc212acdc 100644 --- a/components/drivers/can/Kconfig +++ b/components/drivers/can/Kconfig @@ -1,4 +1,4 @@ -config RT_USING_CAN +menuconfig RT_USING_CAN bool "Using CAN device drivers" default n help @@ -73,4 +73,8 @@ if RT_USING_CAN consumes static RAM but guarantees the memory is always available and avoids heap fragmentation. -endif \ No newline at end of file +endif + +if RT_USING_DM && RT_USING_CAN + osource "$(SOC_DM_CAN_DIR)/Kconfig" +endif diff --git a/components/drivers/can/SConscript b/components/drivers/can/SConscript index 84ae2a10daa..28d45d016d9 100644 --- a/components/drivers/can/SConscript +++ b/components/drivers/can/SConscript @@ -3,6 +3,10 @@ from building import * cwd = GetCurrentDir() src = Glob('*.c') CPPPATH = [cwd + '/../include'] + +if not GetDepend('RT_USING_DM'): + SrcRemove(src, ['can_dm.c']) + group = DefineGroup('DeviceDrivers', src, depend = ['RT_USING_CAN'], CPPPATH = CPPPATH) Return('group') diff --git a/components/drivers/can/can_dm.c b/components/drivers/can/can_dm.c new file mode 100644 index 00000000000..a61cf8e6aa7 --- /dev/null +++ b/components/drivers/can/can_dm.c @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2006-2022, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2022-11-26 GuEe-GUI first version + */ + +#include "can_dm.h" + +static const rt_uint8_t dlc2len[] = +{ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 20, 24, 32, 48, 64 +}; + +rt_uint8_t can_dlc2len(rt_uint8_t can_dlc) +{ + return dlc2len[can_dlc & 0x0F]; +} + +static const rt_uint8_t len2dlc[] = +{ + 0, 1, 2, 3, 4, 5, 6, 7, 8, /* 0 - 8 */ + 9, 9, 9, 9, /* 9 - 12 */ + 10, 10, 10, 10, /* 13 - 16 */ + 11, 11, 11, 11, /* 17 - 20 */ + 12, 12, 12, 12, /* 21 - 24 */ + 13, 13, 13, 13, 13, 13, 13, 13, /* 25 - 32 */ + 14, 14, 14, 14, 14, 14, 14, 14, /* 33 - 40 */ + 14, 14, 14, 14, 14, 14, 14, 14, /* 41 - 48 */ + 15, 15, 15, 15, 15, 15, 15, 15, /* 49 - 56 */ + 15, 15, 15, 15, 15, 15, 15, 15, /* 57 - 64 */ +}; + +rt_uint8_t can_len2dlc(rt_uint8_t len) +{ + if (len <= 64) + { + return len2dlc[len]; + } + + return 0xf; +} diff --git a/components/drivers/can/can_dm.h b/components/drivers/can/can_dm.h new file mode 100644 index 00000000000..52caa1e4948 --- /dev/null +++ b/components/drivers/can/can_dm.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2006-2022, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2022-11-26 GuEe-GUI first version + */ + +#ifndef __CAN_DM_H__ +#define __CAN_DM_H__ + +#include + +/* Special address description flags for the CAN_ID */ +#define CAN_EFF_FLAG 0x80000000U /* EFF/SFF is set in the MSB */ +#define CAN_RTR_FLAG 0x40000000U /* Remote transmission request */ +#define CAN_ERR_FLAG 0x20000000U /* Error message frame */ + +/* Valid bits in CAN ID for frame formats */ +#define CAN_SFF_MASK 0x000007ffU /* Standard frame format (SFF) */ +#define CAN_EFF_MASK 0x1fffffffU /* Extended frame format (EFF) */ +#define CAN_ERR_MASK 0x1fffffffU /* Omit EFF, RTR, ERR flags */ +#define CANXL_PRIO_MASK CAN_SFF_MASK /* 11 bit priority mask */ + +/* CAN payload length and DLC definitions according to ISO 11898-1 */ +#define CAN_MAX_DLC 8 +#define CAN_MAX_RAW_DLC 15 +#define CAN_MAX_DLEN 8 + +/* CAN FD payload length and DLC definitions according to ISO 11898-7 */ +#define CANFD_MAX_DLC 15 +#define CANFD_MAX_DLEN 64 + +/* + * To be used in the CAN netdriver receive path to ensure conformance with + * ISO 11898-1 Chapter 8.4.2.3 (DLC field) + */ +#define can_get_dlc(v) (rt_min_t(rt_uint8_t, (v), CAN_MAX_DLC)) +#define canfd_get_dlc(v) (rt_min_t(rt_uint8_t, (v), CANFD_MAX_DLC)) + +rt_uint8_t can_dlc2len(rt_uint8_t can_dlc); +rt_uint8_t can_len2dlc(rt_uint8_t len); + +#endif /* __CAN_DM_H__ */