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 #include <zephyr/dsp/types.h> /* float32_t, float64_t */
26 
27 /* Use static variables instead of calloc() */
28 #define CO_USE_GLOBALS
29 
30 /* Use Zephyr provided crc16 implementation */
31 #define CO_USE_OWN_CRC16
32 
33 /* Use SDO buffer size from Kconfig */
34 #define CO_SDO_BUFFER_SIZE CONFIG_CANOPENNODE_SDO_BUFFER_SIZE
35 
36 /* Use trace buffer size from Kconfig */
37 #define CO_TRACE_BUFFER_SIZE_FIXED CONFIG_CANOPENNODE_TRACE_BUFFER_SIZE
38 
39 #ifdef CONFIG_CANOPENNODE_LEDS
40 #define CO_USE_LEDS 1
41 #endif
42 
43 #ifdef CONFIG_LITTLE_ENDIAN
44 #define CO_LITTLE_ENDIAN
45 #else
46 #define CO_BIG_ENDIAN
47 #endif
48 
49 typedef bool          bool_t;
50 typedef char          char_t;
51 typedef unsigned char oChar_t;
52 typedef unsigned char domain_t;
53 
54 BUILD_ASSERT(sizeof(float32_t) >= 4);
55 BUILD_ASSERT(sizeof(float64_t) >= 8);
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 	uint16_t mask;
72 #ifdef CONFIG_CAN_ACCEPT_RTR
73 	bool rtr;
74 #endif /* CONFIG_CAN_ACCEPT_RTR */
75 } CO_CANrx_t;
76 
77 typedef struct canopen_tx {
78 	uint8_t data[8];
79 	uint16_t ident;
80 	uint8_t DLC;
81 	bool_t rtr : 1;
82 	bool_t bufferFull : 1;
83 	bool_t syncFlag : 1;
84 } CO_CANtx_t;
85 
86 typedef struct canopen_module {
87 	const struct device *dev;
88 	CO_CANrx_t *rx_array;
89 	CO_CANtx_t *tx_array;
90 	uint16_t rx_size;
91 	uint16_t tx_size;
92 	uint32_t errors;
93 	void *em;
94 	bool_t configured : 1;
95 	bool_t CANnormal : 1;
96 	bool_t first_tx_msg : 1;
97 } CO_CANmodule_t;
98 
99 void canopen_send_lock(void);
100 void canopen_send_unlock(void);
101 #define CO_LOCK_CAN_SEND()   canopen_send_lock()
102 #define CO_UNLOCK_CAN_SEND() canopen_send_unlock()
103 
104 void canopen_emcy_lock(void);
105 void canopen_emcy_unlock(void);
106 #define CO_LOCK_EMCY()   canopen_emcy_lock()
107 #define CO_UNLOCK_EMCY() canopen_emcy_unlock()
108 
109 void canopen_od_lock(void);
110 void canopen_od_unlock(void);
111 #define CO_LOCK_OD()   canopen_od_lock()
112 #define CO_UNLOCK_OD() canopen_od_unlock()
113 
114 /*
115  * CANopenNode RX callbacks run in interrupt context, no memory
116  * barrier needed.
117  */
118 #define CANrxMemoryBarrier()
119 #define IS_CANrxNew(rxNew) ((uintptr_t)rxNew)
120 #define SET_CANrxNew(rxNew) { CANrxMemoryBarrier(); rxNew = (void *)1L; }
121 #define CLEAR_CANrxNew(rxNew) { CANrxMemoryBarrier(); rxNew = (void *)0L; }
122 
123 #ifdef __cplusplus
124 }
125 #endif
126 
127 #endif /* ZEPHYR_MODULES_CANOPENNODE_CO_DRIVER_H */
128