summaryrefslogtreecommitdiff
path: root/components/drivers/can/can_dm.c
blob: a61cf8e6aa741b5614649c16ec5adb1d4a077179 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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;
}