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.h>
22 #include <zephyr/types.h>
23 #include <device.h>
24 #include <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 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
43 #define CO_LITTLE_ENDIAN
44 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
45 #define CO_BIG_ENDIAN
46 #else
47 #error "Unsupported endianness"
48 #endif
49 
50 typedef bool          bool_t;
51 typedef float         float32_t;
52 typedef long double   float64_t;
53 typedef char          char_t;
54 typedef unsigned char oChar_t;
55 typedef unsigned char domain_t;
56 
57 typedef struct canopen_rx_msg {
58 	uint8_t data[8];
59 	uint16_t ident;
60 	uint8_t DLC;
61 } CO_CANrxMsg_t;
62 
63 typedef void (*CO_CANrxBufferCallback_t)(void *object,
64 					 const CO_CANrxMsg_t *message);
65 
66 typedef struct canopen_rx {
67 	int filter_id;
68 	void *object;
69 	CO_CANrxBufferCallback_t pFunct;
70 	uint16_t ident;
71 } CO_CANrx_t;
72 
73 typedef struct canopen_tx {
74 	uint8_t data[8];
75 	uint16_t ident;
76 	uint8_t DLC;
77 	bool_t rtr : 1;
78 	bool_t bufferFull : 1;
79 	bool_t syncFlag : 1;
80 } CO_CANtx_t;
81 
82 typedef struct canopen_module {
83 	const struct device *dev;
84 	CO_CANrx_t *rx_array;
85 	CO_CANtx_t *tx_array;
86 	uint16_t rx_size;
87 	uint16_t tx_size;
88 	uint32_t errors;
89 	void *em;
90 	bool_t configured : 1;
91 	bool_t CANnormal : 1;
92 	bool_t first_tx_msg : 1;
93 } CO_CANmodule_t;
94 
95 void canopen_send_lock(void);
96 void canopen_send_unlock(void);
97 #define CO_LOCK_CAN_SEND()   canopen_send_lock()
98 #define CO_UNLOCK_CAN_SEND() canopen_send_unlock()
99 
100 void canopen_emcy_lock(void);
101 void canopen_emcy_unlock(void);
102 #define CO_LOCK_EMCY()   canopen_emcy_lock()
103 #define CO_UNLOCK_EMCY() canopen_emcy_unlock()
104 
105 void canopen_od_lock(void);
106 void canopen_od_unlock(void);
107 #define CO_LOCK_OD()   canopen_od_lock()
108 #define CO_UNLOCK_OD() canopen_od_unlock()
109 
110 /*
111  * CANopenNode RX callbacks run in interrupt context, no memory
112  * barrier needed.
113  */
114 #define CANrxMemoryBarrier()
115 #define IS_CANrxNew(rxNew) ((uintptr_t)rxNew)
116 #define SET_CANrxNew(rxNew) { CANrxMemoryBarrier(); rxNew = (void *)1L; }
117 #define CLEAR_CANrxNew(rxNew) { CANrxMemoryBarrier(); rxNew = (void *)0L; }
118 
119 #ifdef __cplusplus
120 }
121 #endif
122 
123 #endif /* ZEPHYR_MODULES_CANOPENNODE_CO_DRIVER_H */
124