• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  *
4  * HDF is dual licensed: you can use it either under the terms of
5  * the GPL, or the BSD license, at your option.
6  * See the LICENSE file in the root of this repository for complete details.
7  */
8 
9 #ifndef CAN_IF_H
10 #define CAN_IF_H
11 
12 #include "platform_if.h"
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif /* __cplusplus */
17 
18 #define CAN_DATA_LEN 8
19 
20 enum CanBusState {
21     CAN_BUS_RESET,
22     CAN_BUS_READY,
23     CAN_BUS_BUSY,
24     CAN_BUS_STOP,
25     CAN_BUS_SLEEP,
26     CAN_BUS_ERROR,
27     CAN_BUS_INVALID,
28 };
29 
30 enum CanBusMode {
31     CAN_BUS_NORMAL,
32     CAN_BUS_LOOPBACK,
33 };
34 
35 struct CanMsg {
36     union {
37         uint32_t id11 : 11;
38         uint32_t id29 : 29;
39         uint32_t id : 29;
40     };
41     uint32_t ide : 1;
42     uint32_t rtr : 1;
43     uint32_t padding : 1;
44     uint8_t dlc;
45     uint8_t data[CAN_DATA_LEN];
46     int32_t error;
47     const struct CanFilter *filter;
48 };
49 
50 enum CanEvent {
51     CAN_EVENT_MSG_RECEIVED,
52     CAN_EVENT_MSG_SENT,
53     CAN_EVENT_ERROR,
54 };
55 
56 enum CanFilterType {
57     CAN_FILTER_HW = 1,
58 };
59 
60 struct CanFilter {
61     uint32_t rtr : 1;
62     uint32_t ide : 1;
63     uint32_t id : 29;
64     uint32_t rtrMask : 1;
65     uint32_t ideMask : 1;
66     uint32_t idMask : 29;
67     uint32_t type : 2;
68 };
69 
70 struct CanConfig {
71     uint32_t speed;
72     uint8_t mode;
73 };
74 
75 int32_t CanBusOpen(int32_t number, DevHandle *handle);
76 
77 void CanBusClose(DevHandle handle);
78 
79 int32_t CanBusSendMsg(DevHandle handle, const struct CanMsg *msg);
80 
81 int32_t CanBusReadMsg(DevHandle handle, struct CanMsg *msg, uint32_t tms);
82 
83 int32_t CanBusAddFilter(DevHandle handle, const struct CanFilter *filter);
84 
85 int32_t CanBusDelFilter(DevHandle handle, const struct CanFilter *filter);
86 
87 int32_t CanBusSetCfg(DevHandle handle, const struct CanConfig *cfg);
88 
89 int32_t CanBusGetCfg(DevHandle handle, struct CanConfig *cfg);
90 
91 int32_t CanBusGetState(DevHandle handle);
92 
93 #ifdef __cplusplus
94 }
95 #endif /* __cplusplus */
96 
97 #endif /* CAN_IF_H */
98