1 /*
2  * Copyright (c) 2019 Vestas Wind Systems A/S
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_MODULES_CANOPENNODE_CO_DRIVER_H
8 #define ZEPHYR_MODULES_CANOPENNODE_CO_DRIVER_H
9 
10 /*
11  * Zephyr RTOS CAN driver interface and configuration for CANopenNode
12  * CANopen protocol stack.
13  *
14  * See CANopenNode/stack/drvTemplate/CO_driver.h for API description.
15  */
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20 
21 #include <zephyr/kernel.h>
22 #include <zephyr/types.h>
23 #include <zephyr/device.h>
24 #include <zephyr/toolchain.h>
25 
26 /* Use static variables instead of calloc() */
27 #define CO_USE_GLOBALS
28 
29 /* Use Zephyr provided crc16 implementation */
30 #define CO_USE_OWN_CRC16
31 
32 /* Use SDO buffer size from Kconfig */
33 #define CO_SDO_BUFFER_SIZE CONFIG_CANOPENNODE_SDO_BUFFER_SIZE
34 
35 /* Use trace buffer size from Kconfig */
36 #define CO_TRACE_BUFFER_SIZE_FIXED CONFIG_CANOPENNODE_TRACE_BUFFER_SIZE
37 
38 #ifdef CONFIG_CANOPENNODE_LEDS
39 #define CO_USE_LEDS 1
40 #endif
41 
42 #ifdef CONFIG_LITTLE_ENDIAN
43 #define CO_LITTLE_ENDIAN
44 #else
45 #define CO_BIG_ENDIAN
46 #endif
47 
48 typedef bool          bool_t;
49 typedef float         float32_t;
50 typedef long double   float64_t;
51 typedef char          char_t;
52 typedef unsigned char oChar_t;
53 typedef unsigned char domain_t;
54 
55 typedef struct canopen_rx_msg {
56 	uint8_t data[8];
57 	uint16_t ident;
58 	uint8_t DLC;
59 } CO_CANrxMsg_t;
60 
61 typedef void (*CO_CANrxBufferCallback_t)(void *object,
62 					 const CO_CANrxMsg_t *message);
63 
64 typedef struct canopen_rx {
65 	int filter_id;
66 	void *object;
67 	CO_CANrxBufferCallback_t pFunct;
68 	uint16_t ident;
69 	uint16_t mask;
70 } CO_CANrx_t;
71 
72 typedef struct canopen_tx {
73 	uint8_t data[8];
74 	uint16_t ident;
75 	uint8_t DLC;
76 	bool_t rtr : 1;
77 	bool_t bufferFull : 1;
78 	bool_t syncFlag : 1;
79 } CO_CANtx_t;
80 
81 typedef struct canopen_module {
82 	const struct device *dev;
83 	CO_CANrx_t *rx_array;
84 	CO_CANtx_t *tx_array;
85 	uint16_t rx_size;
86 	uint16_t tx_size;
87 	uint32_t errors;
88 	void *em;
89 	bool_t configured : 1;
90 	bool_t CANnormal : 1;
91 	bool_t first_tx_msg : 1;
92 } CO_CANmodule_t;
93 
94 void canopen_send_lock(void);
95 void canopen_send_unlock(void);
96 #define CO_LOCK_CAN_SEND()   canopen_send_lock()
97 #define CO_UNLOCK_CAN_SEND() canopen_send_unlock()
98 
99 void canopen_emcy_lock(void);
100 void canopen_emcy_unlock(void);
101 #define CO_LOCK_EMCY()   canopen_emcy_lock()
102 #define CO_UNLOCK_EMCY() canopen_emcy_unlock()
103 
104 void canopen_od_lock(void);
105 void canopen_od_unlock(void);
106 #define CO_LOCK_OD()   canopen_od_lock()
107 #define CO_UNLOCK_OD() canopen_od_unlock()
108 
109 /*
110  * CANopenNode RX callbacks run in interrupt context, no memory
111  * barrier needed.
112  */
113 #define CANrxMemoryBarrier()
114 #define IS_CANrxNew(rxNew) ((uintptr_t)rxNew)
115 #define SET_CANrxNew(rxNew) { CANrxMemoryBarrier(); rxNew = (void *)1L; }
116 #define CLEAR_CANrxNew(rxNew) { CANrxMemoryBarrier(); rxNew = (void *)0L; }
117 
118 #ifdef __cplusplus
119 }
120 #endif
121 
122 #endif /* ZEPHYR_MODULES_CANOPENNODE_CO_DRIVER_H */
123