1 /*
2  * Copyright (c) 2019 Vestas Wind Systems A/S
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @defgroup CAN CAN BUS
9  * @{
10  * @}
11  */
12 
13 /**
14  * @brief CANopen Network Stack
15  * @defgroup canopen CANopen Network Stack
16  * @ingroup CAN
17  * @{
18  */
19 
20 #ifndef ZEPHYR_MODULES_CANOPENNODE_CANOPENNODE_H_
21 #define ZEPHYR_MODULES_CANOPENNODE_CANOPENNODE_H_
22 
23 #include <CANopen.h>
24 #include <CO_Emergency.h>
25 #include <CO_SDO.h>
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 /**
32  * @brief CANopen object dictionary storage types.
33  */
34 enum canopen_storage {
35 	CANOPEN_STORAGE_RAM,
36 	CANOPEN_STORAGE_ROM,
37 	CANOPEN_STORAGE_EEPROM,
38 };
39 
40 struct canopen_context {
41 	const struct device *dev;
42 };
43 
44 /**
45  * @brief Attach CANopen object dictionary storage handlers.
46  *
47  * Attach CANopen storage handler functions to object dictionary
48  * indexes 0x1010 (Store parameters) and 0x1011 (Restore default
49  * parameters). This function must be called after calling CANopenNode
50  * `CO_init()`.
51  *
52  * The handlers will save object dictionary entries of type @ref
53  * CANOPEN_STORAGE_ROM to non-volatile storage when a CANopen SDO
54  * client writes 0x65766173 ('s', 'a', 'v', 'e' from LSB to MSB) to
55  * object dictionary index 0x1010 sub-index 1.
56  *
57  * Object dictionary entries of types @ref CANOPEN_STORAGE_ROM (and
58  * optionally @ref CANOPEN_STORAGE_EEPROM) will be deleted from
59  * non-volatile storage when a CANopen SDO client writes 0x64616F6C
60  * ('l', 'o', 'a', 'd' from LSB to MSB) to object dictionary index
61  * 0x1011 sub-index 1.
62  *
63  * Object dictionary entries of type @ref CANOPEN_STORAGE_EEPROM may be
64  * saved by the application by periodically calling @ref
65  * canopen_storage_save().
66  *
67  * Object dictionary entries of type @ref CANOPEN_STORAGE_RAM are
68  * never saved to non-volatile storage.
69  *
70  * @param sdo CANopenNode SDO server object
71  * @param em  CANopenNode Emergency object
72  */
73 void canopen_storage_attach(CO_SDO_t *sdo, CO_EM_t *em);
74 
75 /**
76  * @brief Save CANopen object dictionary entries to non-volatile storage.
77  *
78  * Save object dictionary entries of a given type to non-volatile
79  * storage.
80  *
81  * @param storage CANopen object dictionary entry type
82  *
83  * @return 0 if successful, negative errno code if failure
84  */
85 int canopen_storage_save(enum canopen_storage storage);
86 
87 /**
88  * @brief Erase CANopen object dictionary entries from non-volatile storage.
89  *
90  * Erase object dictionary entries of a given type from non-volatile
91  * storage.
92  *
93  * @param storage CANopen object dictionary entry type
94  *
95  * @return 0 if successful, negative errno code if failure
96  */
97 int canopen_storage_erase(enum canopen_storage storage);
98 
99 /**
100  * @brief Attach CANopen object dictionary program download handlers.
101  *
102  * Attach CANopen program download functions to object dictionary
103  * indexes 0x1F50, 0x1F51, 0x1F56, and 0x1F57. This function must be
104  * called after calling CANopenNode `CO_init()`.
105  *
106  * @param nmt CANopenNode NMT object
107  * @param sdo CANopenNode SDO server object
108  * @param em  CANopenNode Emergency object
109  */
110 void canopen_program_download_attach(CO_NMT_t *nmt, CO_SDO_t *sdo, CO_EM_t *em);
111 
112 /**
113  * @typedef canopen_led_callback_t
114  * @brief CANopen LED indicator callback function signature.
115  *
116  * @param value true if the LED indicator shall be turned on, false otherwise.
117  * @param arg argument that was passed when LEDs were initialized.
118  */
119 typedef void (*canopen_led_callback_t)(bool value, void *arg);
120 
121 /**
122  * @brief Initialize CANopen LED indicators.
123  *
124  * Initialize CANopen LED indicators and attach callbacks for setting
125  * their state. Two LED indicators, a red and a green, are supported
126  * according to CiA 303-3.
127  *
128  * @param nmt CANopenNode NMT object.
129  * @param green_cb callback for changing state on the green LED indicator.
130  * @param green_arg argument to pass to the green LED indicator callback.
131  * @param red_cb callback for changing state on the red LED indicator.
132  * @param red_arg argument to pass to the red LED indicator callback.
133  */
134 void canopen_leds_init(CO_NMT_t *nmt,
135 		       canopen_led_callback_t green_cb, void *green_arg,
136 		       canopen_led_callback_t red_cb, void *red_arg);
137 
138 /**
139  * @brief Indicate CANopen program download in progress
140  *
141  * Indicate that a CANopen program download is in progress.
142  *
143  * @param in_progress true if program download is in progress, false otherwise
144  */
145 void canopen_leds_program_download(bool in_progress);
146 
147 #ifdef __cplusplus
148 }
149 #endif
150 
151 /**
152  * @}
153  */
154 
155 #endif /* ZEPHYR_MODULES_CANOPENNODE_CANOPENNODE_H_ */
156