Skip to content

Commit 1e82368

Browse files
GuEe-GUIRbb666
authored andcommitted
[DM/CAN] Update CAN for DM
1. Kconfig import wtih DM 2. Add DM API for Drivers Signed-off-by: GuEe-GUI <[email protected]>
1 parent 4337d7f commit 1e82368

File tree

4 files changed

+120
-2
lines changed

4 files changed

+120
-2
lines changed

components/drivers/can/Kconfig

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
config RT_USING_CAN
1+
menuconfig RT_USING_CAN
22
bool "Using CAN device drivers"
33
default n
44
help
@@ -73,4 +73,8 @@ if RT_USING_CAN
7373
consumes static RAM but guarantees the memory is always available
7474
and avoids heap fragmentation.
7575

76-
endif
76+
endif
77+
78+
if RT_USING_DM && RT_USING_CAN
79+
osource "$(SOC_DM_CAN_DIR)/Kconfig"
80+
endif

components/drivers/can/SConscript

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ from building import *
33
cwd = GetCurrentDir()
44
src = Glob('*.c')
55
CPPPATH = [cwd + '/../include']
6+
7+
if not GetDepend('RT_USING_DM'):
8+
SrcRemove(src, ['can_dm.c'])
9+
610
group = DefineGroup('DeviceDrivers', src, depend = ['RT_USING_CAN'], CPPPATH = CPPPATH)
711

812
Return('group')

components/drivers/can/can_dm.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2006-2022, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2022-11-26 GuEe-GUI first version
9+
*/
10+
11+
#include "can_dm.h"
12+
13+
static const rt_uint8_t dlc2len[] =
14+
{
15+
0, 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 20, 24, 32, 48, 64
16+
};
17+
18+
rt_uint8_t can_dlc2len(rt_uint8_t can_dlc)
19+
{
20+
return dlc2len[can_dlc & 0x0F];
21+
}
22+
23+
static const rt_uint8_t len2dlc[] =
24+
{
25+
0, 1, 2, 3, 4, 5, 6, 7, 8, /* 0 - 8 */
26+
9, 9, 9, 9, /* 9 - 12 */
27+
10, 10, 10, 10, /* 13 - 16 */
28+
11, 11, 11, 11, /* 17 - 20 */
29+
12, 12, 12, 12, /* 21 - 24 */
30+
13, 13, 13, 13, 13, 13, 13, 13, /* 25 - 32 */
31+
14, 14, 14, 14, 14, 14, 14, 14, /* 33 - 40 */
32+
14, 14, 14, 14, 14, 14, 14, 14, /* 41 - 48 */
33+
15, 15, 15, 15, 15, 15, 15, 15, /* 49 - 56 */
34+
15, 15, 15, 15, 15, 15, 15, 15, /* 57 - 64 */
35+
};
36+
37+
rt_uint8_t can_len2dlc(rt_uint8_t len)
38+
{
39+
if (len <= 64)
40+
{
41+
return len2dlc[len];
42+
}
43+
44+
return 0xf;
45+
}

components/drivers/can/can_dm.h

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright (c) 2006-2022, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2022-11-26 GuEe-GUI first version
9+
*/
10+
11+
#ifndef __CAN_DM_H__
12+
#define __CAN_DM_H__
13+
14+
#include <drivers/misc.h>
15+
16+
/* Special address description flags for the CAN_ID */
17+
#define CAN_EFF_FLAG 0x80000000U /* EFF/SFF is set in the MSB */
18+
#define CAN_RTR_FLAG 0x40000000U /* Remote transmission request */
19+
#define CAN_ERR_FLAG 0x20000000U /* Error message frame */
20+
21+
/* Valid bits in CAN ID for frame formats */
22+
#define CAN_SFF_MASK 0x000007ffU /* Standard frame format (SFF) */
23+
#define CAN_EFF_MASK 0x1fffffffU /* Extended frame format (EFF) */
24+
#define CAN_ERR_MASK 0x1fffffffU /* Omit EFF, RTR, ERR flags */
25+
#define CANXL_PRIO_MASK CAN_SFF_MASK /* 11 bit priority mask */
26+
27+
/* CAN payload length and DLC definitions according to ISO 11898-1 */
28+
#define CAN_MAX_DLC 8
29+
#define CAN_MAX_RAW_DLC 15
30+
#define CAN_MAX_DLEN 8
31+
32+
/* CAN FD payload length and DLC definitions according to ISO 11898-7 */
33+
#define CANFD_MAX_DLC 15
34+
#define CANFD_MAX_DLEN 64
35+
36+
/*
37+
* To be used in the CAN netdriver receive path to ensure conformance with
38+
* ISO 11898-1 Chapter 8.4.2.3 (DLC field)
39+
*/
40+
#define can_get_dlc(v) (rt_min_t(rt_uint8_t, (v), CAN_MAX_DLC))
41+
#define canfd_get_dlc(v) (rt_min_t(rt_uint8_t, (v), CANFD_MAX_DLC))
42+
43+
/**
44+
* @brief Convert CAN DLC value to actual data length
45+
*
46+
* Converts a CAN Data Length Code (DLC) to the actual number of data bytes
47+
* according to ISO 11898-1 and ISO 11898-7 (CAN FD) specifications.
48+
*
49+
* @param can_dlc The DLC value (0-15)
50+
* @return The actual data length in bytes
51+
*/
52+
rt_uint8_t can_dlc2len(rt_uint8_t can_dlc);
53+
54+
/**
55+
* @brief Convert data length to CAN DLC value
56+
*
57+
* Converts a data length in bytes to the appropriate CAN Data Length Code (DLC)
58+
* according to ISO 11898-1 and ISO 11898-7 (CAN FD) specifications.
59+
*
60+
* @param len The data length in bytes (0-64)
61+
* @return The corresponding DLC value (0-15)
62+
*/
63+
rt_uint8_t can_len2dlc(rt_uint8_t len);
64+
65+
#endif /* __CAN_DM_H__ */

0 commit comments

Comments
 (0)