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_CORE_H
10 #define CAN_CORE_H
11
12 #include "can_if.h"
13 #include "can_manager.h"
14 #include "can_msg.h"
15 #include "can_service.h"
16 #include "hdf_base.h"
17 #include "hdf_dlist.h"
18 #include "hdf_sref.h"
19 #include "osal_mutex.h"
20 #include "platform_core.h"
21
22 #ifdef __cplusplus
23 extern "C" {
24 #endif /* __cplusplus */
25
26 struct CanCntlr;
27 struct CanRxBox;
28
29 struct CanCntlrMethod {
30 int32_t (*open)(struct CanCntlr *cntlr);
31 int32_t (*close)(struct CanCntlr *cntlr);
32 int32_t (*lock)(struct CanCntlr *cntlr);
33 int32_t (*unlock)(struct CanCntlr *cntlr);
34 int32_t (*sendMsg)(struct CanCntlr *cntlr, const struct CanMsg *msg);
35 int32_t (*setCfg)(struct CanCntlr *cntlr, const struct CanConfig *cfg);
36 int32_t (*getCfg)(struct CanCntlr *cntlr, struct CanConfig *cfg);
37 int32_t (*getState)(struct CanCntlr *cntlr);
38 };
39
40 struct CanCntlr {
41 int32_t number; // bus number
42 int32_t speed; // bit rate
43 int32_t state; // bus status
44 int32_t mode; // work mode
45 size_t msgPoolSize; // size of msg pool
46 /*
47 * the members below are used by framework
48 * don't init or modify them
49 */
50 struct CanCntlrMethod *ops;
51 struct PlatformDevice device;
52 struct OsalMutex lock;
53 struct DListHead filters;
54 struct DListHead rxBoxList;
55 struct OsalMutex rboxListLock;
56 struct CanMsgPool *msgPool;
57 };
58
59 enum CanErrState {
60 CAN_ERR_ACTIVE, // error active CAN_ERR_PASSIVE, // error passive
61 CAN_ERR_BUS_OFF, // error bus off
62 };
63
64 struct CanTiming {
65 uint32_t brp; // prescaler
66 uint32_t sjw; // sync jump width
67 uint32_t seg1; // phase segment 1
68 uint32_t seg2; // phase segment 2
69 };
70
71 // HDF dev relationship manage
CanCntlrSetHdfDev(struct CanCntlr * cntlr,struct HdfDeviceObject * device)72 static inline int32_t CanCntlrSetHdfDev(struct CanCntlr *cntlr, struct HdfDeviceObject *device)
73 {
74 return PlatformDeviceSetHdfDev(&cntlr->device, device);
75 }
76
CanCntlrFromHdfDev(const struct HdfDeviceObject * device)77 static inline struct CanCntlr *CanCntlrFromHdfDev(const struct HdfDeviceObject *device)
78 {
79 struct PlatformDevice *pdevice = PlatformDeviceFromHdfDev(device);
80 return (pdevice == NULL) ? NULL : CONTAINER_OF(pdevice, struct CanCntlr, device);
81 }
82
83 // CAN BUS operations
84 int32_t CanCntlrWriteMsg(struct CanCntlr *cntlr, const struct CanMsg *msg);
85
86 int32_t CanCntlrSetCfg(struct CanCntlr *cntlr, const struct CanConfig *cfg);
87
88 int32_t CanCntlrGetCfg(struct CanCntlr *cntlr, struct CanConfig *cfg);
89
90 int32_t CanCntlrGetState(struct CanCntlr *cntlr);
91
92 int32_t CanCntlrOnNewMsg(struct CanCntlr *cntlr, const struct CanMsg *msg);
93
94 int32_t CanCntlrAddRxBox(struct CanCntlr *cntlr, struct CanRxBox *rxBox);
95
96 int32_t CanCntlrDelRxBox(struct CanCntlr *cntlr, struct CanRxBox *rxBox);
97
98 #ifdef __cplusplus
99 }
100 #endif /* __cplusplus */
101
102 #endif
103