ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
cia402.h
Go to the documentation of this file.
1// ProtoCore v1.0.16 - Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file cia402.h
6 * @brief CiA 402 / IEC 61800-7-201 drive + motion profile (PROTOCORE_ENABLE_CIA402) over CANopen.
7 *
8 * The standardized servo / stepper drive profile: the power state machine (Controlword 0x6040 /
9 * Statusword 0x6041), the Modes of Operation, and the target/actual position-velocity-torque
10 * objects. This is the pure profile layer - the state decode + controlword commands are just
11 * value logic, and the setters/getters wrap the shipped `services/canopen` SDO / PDO codec, so
12 * the CAN stack (ESP32 TWAI or an MCP2515) becomes a motion master. Close the loop with a
13 * `services/control` PID.
14 *
15 * Statusword state masks, Controlword command values, and the object indices are verified against
16 * IEC 61800-7-201 (CiA 402) and multiple drive vendors' state-machine tables. Pure, host-tested.
17 *
18 * @author Douglas Quigg (dstroy0)
19 * @date 2026
20 */
21
22#ifndef PROTOCORE_CIA402_H
23#define PROTOCORE_CIA402_H
24
25#include "protocore_config.h" // the entry point: protocore_types.h for the widths
26#include "shared/can/can.h" // CanFrame: the type a parameter points at
27
28#if PROTOCORE_ENABLE_CIA402
29
30EMBED_BEGIN_DECLS
31
32// This module holds nothing between calls, so it carves no borrow and states none. An entry
33// takes one all the same, and never reads it, so every namespace in the tree is invoked the
34// same way.
35
36// --- object dictionary indices (sub-index 0 unless noted); the comment gives the CANopen type ---
37#define CIA402_OD_ERROR_CODE 0x603Fu ///< u16 last error code
38#define CIA402_OD_CONTROLWORD 0x6040u ///< u16 command word (drives the state machine)
39#define CIA402_OD_STATUSWORD 0x6041u ///< u16 status word (reports the state)
40#define CIA402_OD_QUICK_STOP_OPTION 0x605Au ///< i16 quick-stop option code
41#define CIA402_OD_MODES_OF_OPERATION 0x6060u ///< i8 requested mode
42#define CIA402_OD_MODES_DISPLAY 0x6061u ///< i8 active mode (read-back)
43#define CIA402_OD_POSITION_ACTUAL 0x6064u ///< i32 position actual value
44#define CIA402_OD_VELOCITY_ACTUAL 0x606Cu ///< i32 velocity actual value
45#define CIA402_OD_TARGET_TORQUE 0x6071u ///< i16 target torque (per-mille of rated)
46#define CIA402_OD_TORQUE_ACTUAL 0x6077u ///< i16 torque actual value
47#define CIA402_OD_TARGET_POSITION 0x607Au ///< i32 target position (PP / CSP)
48#define CIA402_OD_PROFILE_VELOCITY 0x6081u ///< u32 profile velocity (PP)
49#define CIA402_OD_PROFILE_ACCEL 0x6083u ///< u32 profile acceleration
50#define CIA402_OD_PROFILE_DECEL 0x6084u ///< u32 profile deceleration
51#define CIA402_OD_TARGET_VELOCITY 0x60FFu ///< i32 target velocity (PV / CSV)
52#define CIA402_OD_SUPPORTED_MODES 0x6502u ///< u32 supported drive modes bitfield
53
54/// Controlword bit masks (object 0x6040).
55#define CIA402_CW_SWITCH_ON 0x0001
56#define CIA402_CW_ENABLE_VOLTAGE 0x0002
57#define CIA402_CW_QUICK_STOP 0x0004 ///< active-low: 0 requests quick stop
58#define CIA402_CW_ENABLE_OPERATION 0x0008
59#define CIA402_CW_FAULT_RESET 0x0080 ///< acts on the rising edge
60#define CIA402_CW_HALT 0x0100
61
62/// Statusword bit masks (object 0x6041).
63#define CIA402_SW_READY_TO_SWITCH_ON 0x0001
64#define CIA402_SW_SWITCHED_ON 0x0002
65#define CIA402_SW_OPERATION_ENABLED 0x0004
66#define CIA402_SW_FAULT 0x0008
67#define CIA402_SW_VOLTAGE_ENABLED 0x0010
68#define CIA402_SW_QUICK_STOP 0x0020 ///< 0 = quick stop active
69#define CIA402_SW_SWITCH_ON_DISABLED 0x0040
70#define CIA402_SW_WARNING 0x0080
71#define CIA402_SW_REMOTE 0x0200
72#define CIA402_SW_TARGET_REACHED 0x0400
73#define CIA402_SW_INTERNAL_LIMIT 0x0800
74
75/// @return true if the Statusword's Target Reached flag (bit 10) is set.
76static inline embed_bool protocore_cia402_target_reached(uint16_t sw)
77{
78 return (sw & CIA402_SW_TARGET_REACHED) != 0;
79}
80/// @return true if the drive reports a fault (bit 3).
81static inline embed_bool protocore_cia402_has_fault(uint16_t sw)
82{
83 return (sw & CIA402_SW_FAULT) != 0;
84}
85/// @return true if a warning is present (bit 7).
86static inline embed_bool protocore_cia402_warning(uint16_t sw)
87{
88 return (sw & CIA402_SW_WARNING) != 0;
89}
90/// @return true if the drive's power stage voltage is applied (bit 4).
91static inline embed_bool protocore_cia402_voltage_enabled(uint16_t sw)
92{
93 return (sw & CIA402_SW_VOLTAGE_ENABLED) != 0;
94}
95/// @return true if the drive follows the Controlword (bit 9 remote).
96static inline embed_bool protocore_cia402_remote(uint16_t sw)
97{
98 return (sw & CIA402_SW_REMOTE) != 0;
99}
100/// @return true if a set-point was internally limited (bit 11).
101static inline embed_bool protocore_cia402_internal_limit(uint16_t sw)
102{
103 return (sw & CIA402_SW_INTERNAL_LIMIT) != 0;
104}
105
106EMBED_ENUM_BYTE(Cia402Mode, CIA402_MODE_NO_MODE = 0,
107 CIA402_MODE_PROFILE_POSITION = 1, ///< PP
108 CIA402_MODE_VELOCITY = 2, ///< VL (frequency-converter CIA402_MODE_VELOCITY)
109 CIA402_MODE_PROFILE_VELOCITY = 3, ///< PV
110 CIA402_MODE_PROFILE_TORQUE = 4, ///< TQ
111 CIA402_MODE_HOMING = 6, ///< HM
112 CIA402_MODE_INTERPOLATED_POSITION = 7, ///< IP
113 CIA402_MODE_CYCLIC_SYNC_POSITION = 8, ///< CSP
114 CIA402_MODE_CYCLIC_SYNC_VELOCITY = 9, ///< CSV
115 CIA402_MODE_CYCLIC_SYNC_TORQUE = 10, ///< CST
116);
117
118EMBED_ENUM_BYTE(Cia402State, CIA402_STATE_NOT_READY_TO_SWITCH_ON, CIA402_STATE_SWITCH_ON_DISABLED,
119 CIA402_STATE_READY_TO_SWITCH_ON, CIA402_STATE_SWITCHED_ON, CIA402_STATE_OPERATION_ENABLED,
120 CIA402_STATE_QUICK_STOP_ACTIVE, CIA402_STATE_FAULT_REACTION_ACTIVE, CIA402_STATE_FAULT,
121 CIA402_STATE_UNKNOWN, ///< Statusword matched no defined state
122);
123
124EMBED_ENUM_BYTE(Cia402Command,
125 CIA402_COMMAND_SHUTDOWN, ///< -> Ready to switch on
126 CIA402_COMMAND_SWITCH_ON, ///< -> Switched on
127 CIA402_COMMAND_ENABLE_OPERATION, ///< -> Operation enabled
128 CIA402_COMMAND_DISABLE_VOLTAGE, ///< -> Switch on disabled
129 CIA402_COMMAND_QUICK_STOP, ///< -> Quick stop active
130 CIA402_COMMAND_DISABLE_OPERATION, ///< -> Switched on
131 CIA402_COMMAND_FAULT_RESET, ///< clear a fault (rising edge of bit 7)
132);
133
134/** @brief What state takes: statusword. */
135typedef struct
136{
137 uint16_t statusword;
138} Cia402StateArgs;
139
140/** @brief What controlword takes: cmd. */
141typedef struct
142{
143 Cia402Command cmd;
144} Cia402ControlwordArgs;
145
146/** @brief What enable_sequence takes: state. */
147typedef struct
148{
149 Cia402State state;
150} Cia402EnableSequenceArgs;
151
152/** @brief What sdo_set_controlword takes: out, node, controlword. */
153typedef struct
154{
155 CanFrame *out;
156 uint8_t node;
157 uint16_t controlword;
158} Cia402SdoSetControlwordArgs;
159
160/** @brief What sdo_set_mode takes: out, node, mode. */
161typedef struct
162{
163 CanFrame *out;
164 uint8_t node;
165 Cia402Mode mode;
166} Cia402SdoSetModeArgs;
167
168/** @brief What sdo_set_target_position takes: out, node, position. */
169typedef struct
170{
171 CanFrame *out;
172 uint8_t node;
173 int32_t position;
174} Cia402SdoSetTargetPositionArgs;
175
176/** @brief What sdo_set_target_velocity takes: out, node, velocity. */
177typedef struct
178{
179 CanFrame *out;
180 uint8_t node;
181 int32_t velocity;
182} Cia402SdoSetTargetVelocityArgs;
183
184/** @brief What sdo_set_target_torque takes: out, node, torque. */
185typedef struct
186{
187 CanFrame *out;
188 uint8_t node;
189 int16_t torque;
190} Cia402SdoSetTargetTorqueArgs;
191
192/** @brief What sdo_read takes: out, node, index, sub. */
193typedef struct
194{
195 CanFrame *out;
196 uint8_t node;
197 uint16_t index;
198 uint8_t sub;
199} Cia402SdoReadArgs;
200
201/** @brief What sdo_get_u16 takes: f, want_index, value. */
202typedef struct
203{
204 const CanFrame *f;
205 uint16_t want_index;
206 uint16_t *value;
207} Cia402SdoGetU16Args;
208
209/** @brief What sdo_get_i32 takes: f, want_index, value. */
210typedef struct
211{
212 const CanFrame *f;
213 uint16_t want_index;
214 int32_t *value;
215} Cia402SdoGetI32Args;
216
217/** @brief What pack_command takes: buf, cap, controlword, target. */
218typedef struct
219{
220 uint8_t *buf;
221 size_t cap;
222 uint16_t controlword;
223 int32_t target;
224} Cia402PackCommandArgs;
225
226/** @brief What unpack_status takes: buf, len, statusword, actual. */
227typedef struct
228{
229 const uint8_t *buf;
230 size_t len;
231 uint16_t *statusword;
232 int32_t *actual;
233} Cia402UnpackStatusArgs;
234
235/**
236 * @brief CiA 402 / IEC 61800-7-201 drive + motion profile (PROTOCORE_ENABLE_CIA402) over CANopen.
237 *
238 * A caller sets the members a call takes, invokes it through ::Cia402 with the bytes it runs
239 * out of, and reads the outcome off the same handle.
240 *
241 * Cia402.state_args.statusword = ...;
242 * Cia402.state(work);
243 * // Cia402.value is what the call reports
244 *
245 * @var Cia402Ns::state_args what state takes: statusword
246 * @var Cia402Ns::controlword_args what controlword takes: cmd
247 * @var Cia402Ns::enable_sequence_args what enable_sequence takes: state
248 * @var Cia402Ns::sdo_set_controlword_args what sdo_set_controlword takes: out, node, controlword
249 * @var Cia402Ns::sdo_set_mode_args what sdo_set_mode takes: out, node, mode
250 * @var Cia402Ns::sdo_set_target_position_args what sdo_set_target_position takes: out, node, position
251 * @var Cia402Ns::sdo_set_target_velocity_args what sdo_set_target_velocity takes: out, node, velocity
252 * @var Cia402Ns::sdo_set_target_torque_args what sdo_set_target_torque takes: out, node, torque
253 * @var Cia402Ns::sdo_read_args what sdo_read takes: out, node, index, sub
254 * @var Cia402Ns::sdo_get_u16_args what sdo_get_u16 takes: f, want_index, value
255 * @var Cia402Ns::sdo_get_i32_args what sdo_get_i32 takes: f, want_index, value
256 * @var Cia402Ns::pack_command_args what pack_command takes: buf, cap, controlword, target
257 * @var Cia402Ns::unpack_status_args what unpack_status takes: buf, len, statusword, actual
258 * @var Cia402Ns::ok a call's true/false outcome
259 * @var Cia402Ns::value the value a call reports
260 * @var Cia402Ns::u16 what a call reports
261 * @var Cia402Ns::n the count a call reports
262 * @var Cia402Ns::state state
263 * @var Cia402Ns::controlword controlword
264 * @var Cia402Ns::enable_sequence enable_sequence
265 * @var Cia402Ns::sdo_set_controlword sdo_set_controlword
266 * @var Cia402Ns::sdo_set_mode sdo_set_mode
267 * @var Cia402Ns::sdo_set_target_position sdo_set_target_position
268 * @var Cia402Ns::sdo_set_target_velocity sdo_set_target_velocity
269 * @var Cia402Ns::sdo_set_target_torque sdo_set_target_torque
270 * @var Cia402Ns::sdo_read sdo_read
271 * @var Cia402Ns::sdo_get_u16 sdo_get_u16
272 * @var Cia402Ns::sdo_get_i32 sdo_get_i32
273 * @var Cia402Ns::pack_command pack_command
274 * @var Cia402Ns::unpack_status unpack_status
275 *
276 * @c work is bytes the CALLER holds. This module reads none of them: it carries nothing
277 * between calls, so there is no state to keep and nothing to wipe. The parameter is there so
278 * a caller drives every namespace the same way.
279 */
280typedef struct
281{
282 Cia402StateArgs state_args;
283 Cia402ControlwordArgs controlword_args;
284 Cia402EnableSequenceArgs enable_sequence_args;
285 Cia402SdoSetControlwordArgs sdo_set_controlword_args;
286 Cia402SdoSetModeArgs sdo_set_mode_args;
287 Cia402SdoSetTargetPositionArgs sdo_set_target_position_args;
288 Cia402SdoSetTargetVelocityArgs sdo_set_target_velocity_args;
289 Cia402SdoSetTargetTorqueArgs sdo_set_target_torque_args;
290 Cia402SdoReadArgs sdo_read_args;
291 Cia402SdoGetU16Args sdo_get_u16_args;
292 Cia402SdoGetI32Args sdo_get_i32_args;
293 Cia402PackCommandArgs pack_command_args;
294 Cia402UnpackStatusArgs unpack_status_args;
295 embed_bool ok;
296 Cia402State value;
297 uint16_t u16;
298 size_t n;
299} Cia402Vars;
300
301/** @brief The operands and the outcome. */
302extern Cia402Vars Cia402V;
303
304/** @brief The entries. */
305typedef struct
306{
307 void (*const state)(const uint8_t *work);
308 void (*const controlword)(const uint8_t *work);
309 void (*const enable_sequence)(const uint8_t *work);
310 void (*const sdo_set_controlword)(const uint8_t *work);
311 void (*const sdo_set_mode)(const uint8_t *work);
312 void (*const sdo_set_target_position)(const uint8_t *work);
313 void (*const sdo_set_target_velocity)(const uint8_t *work);
314 void (*const sdo_set_target_torque)(const uint8_t *work);
315 void (*const sdo_read)(const uint8_t *work);
316 void (*const sdo_get_u16)(uint8_t *work);
317 void (*const sdo_get_i32)(uint8_t *work);
318 void (*const pack_command)(const uint8_t *work);
319 void (*const unpack_status)(const uint8_t *work);
320} Cia402Ns;
321
322// What the table binds, defined once in the .c and taking one parameter each: everything
323// else an entry needs is an operand in Cia402V or a region of the borrow at a fixed offset.
324void protocore_cia402_state(const uint8_t *work);
325void protocore_cia402_controlword(const uint8_t *work);
326void protocore_cia402_enable_sequence(const uint8_t *work);
327void protocore_cia402_sdo_set_controlword(const uint8_t *work);
328void protocore_cia402_sdo_set_mode(const uint8_t *work);
329void protocore_cia402_sdo_set_target_position(const uint8_t *work);
330void protocore_cia402_sdo_set_target_velocity(const uint8_t *work);
331void protocore_cia402_sdo_set_target_torque(const uint8_t *work);
332void protocore_cia402_sdo_read(const uint8_t *work);
333void protocore_cia402_sdo_get_u16(uint8_t *work);
334void protocore_cia402_sdo_get_i32(uint8_t *work);
335void protocore_cia402_pack_command(const uint8_t *work);
336void protocore_cia402_unpack_status(const uint8_t *work);
337
338// `static const`, initialised HERE rather than `extern` against a definition in the .c: a
339// const object whose initializer every translation unit can see is a COMPILE-TIME FACT, so
340// `Cia402.state(work)` resolves to a named function and becomes a DIRECT call. An extern table
341// leaves the call indirect and the symbol live at every level, -O2 -flto included.
342static const Cia402Ns Cia402 __attribute__((unused)) = {
343 .state = protocore_cia402_state,
344 .controlword = protocore_cia402_controlword,
345 .enable_sequence = protocore_cia402_enable_sequence,
346 .sdo_set_controlword = protocore_cia402_sdo_set_controlword,
347 .sdo_set_mode = protocore_cia402_sdo_set_mode,
348 .sdo_set_target_position = protocore_cia402_sdo_set_target_position,
349 .sdo_set_target_velocity = protocore_cia402_sdo_set_target_velocity,
350 .sdo_set_target_torque = protocore_cia402_sdo_set_target_torque,
351 .sdo_read = protocore_cia402_sdo_read,
352 .sdo_get_u16 = protocore_cia402_sdo_get_u16,
353 .sdo_get_i32 = protocore_cia402_sdo_get_i32,
354 .pack_command = protocore_cia402_pack_command,
355 .unpack_status = protocore_cia402_unpack_status,
356};
357
358EMBED_END_DECLS
359
360#endif // PROTOCORE_ENABLE_CIA402
361
362#endif // PROTOCORE_CIA402_H
EMBED_ENUM_BYTE(ProtoConn, PROTO_NONE=0, PROTO_HTTP=1, PROTO_TELNET=2, PROTO_SSH=3, PROTO_MODBUS=4, PROTO_OPCUA=5, PROTO_SSH_RFWD=6, PROTO_RELAY=7, PROTO_BRIDGE=8, PROTO_NTRIP_CASTER=9, PROTO_MESH=10, PROTO_UDP=11,)
Application protocol spoken on a listener port or connection slot.
Shared CAN 2.0 frame type for the CAN-based industrial codecs (one source of truth).
Definition can.h:44