13#ifndef CANOPEN_CLIENT_PROTOCOL_HPP
14#define CANOPEN_CLIENT_PROTOCOL_HPP
37constexpr bool IsNodeId(uint8_t node)
noexcept {
73constexpr uint16_t
EmcyId(uint8_t node)
noexcept {
74 return static_cast<uint16_t
>(detail::Widen(
kCobIdEmcyBase) + detail::Widen(node));
125 using detail::operator
""_u8;
126 const auto raw =
static_cast<uint8_t
>(command);
144 using detail::operator
""_u8;
145 if (counter > 240_u8) {
150 if (counter != 0_u8) {
184 f.dlc =
static_cast<uint8_t
>(data.size());
185 for (std::size_t i = 0; i < data.size(); ++i) {
194constexpr CanFrame SdoRequestBase(uint8_t node, uint8_t command, uint16_t index,
195 uint8_t sub)
noexcept {
196 using detail::operator
""_u8;
201 f.data[1] =
static_cast<uint8_t
>(Widen(index) & 0xFFU);
202 f.data[2] =
static_cast<uint8_t
>(Widen(index) >> 8U);
217 uint8_t sub)
noexcept {
218 using detail::operator
""_u8;
235 std::span<const uint8_t> data)
noexcept {
236 if (!
IsNodeId(node) || data.empty() || (data.size() > 4U)) {
239 const auto unused =
static_cast<uint32_t
>(4U - data.size());
240 const auto command =
static_cast<uint8_t
>(0x23U | (unused << 2U));
241 CanFrame f = detail::SdoRequestBase(node, command, index, sub);
242 for (std::size_t i = 0; i < data.size(); ++i) {
243 f.
data[4U + i] = data[i];
257 uint32_t size)
noexcept {
258 using detail::operator
""_u8;
259 if (!
IsNodeId(node) || (size == 0U)) {
262 CanFrame f = detail::SdoRequestBase(node, 0x21_u8, index, sub);
263 detail::StoreLe32At4(f.
data, size);
276 std::span<const uint8_t> data,
277 bool last)
noexcept {
278 using detail::operator
""_u8;
279 if (!
IsNodeId(node) || data.empty() || (data.size() > 7U)) {
282 const uint32_t unused = 7U -
static_cast<uint32_t
>(data.size());
283 const uint32_t command = (toggle ? 0x10U : 0U) | (unused << 1U) | (last ? 1U : 0U);
287 f.data[0] =
static_cast<uint8_t
>(command);
288 for (std::size_t i = 0; i < data.size(); ++i) {
289 f.data[1U + i] = data[i];
301 using detail::operator
""_u8;
308 f.data[0] = toggle ? 0x70_u8 : 0x60_u8;
321 uint32_t code)
noexcept {
322 using detail::operator
""_u8;
326 CanFrame f = detail::SdoRequestBase(node, 0x80_u8, index, sub);
327 detail::StoreLe32At4(f.
data, code);
336constexpr Result<SdoReply> ParseInitiateUpload(
const CanFrame& f, SdoReply r,
337 bool strict)
noexcept {
338 using detail::operator
""_u8;
339 const uint32_t b0 = Widen(f.data[0]);
340 const bool expedited = TestBit(b0, 1U);
341 const bool sized = TestBit(b0, 0U);
342 r.expedited = expedited;
343 r.size_indicated = sized;
345 const uint32_t len = sized ? (4U - ((b0 >> 2U) & 3U)) : 4U;
346 if ((Widen(f.dlc) < (4U + len)) || (strict && (f.dlc != 8_u8))) {
349 r.data_len =
static_cast<uint8_t
>(len);
350 for (uint32_t i = 0U; i < len; ++i) {
351 r.data[i] = f.data[4U + i];
355 const uint32_t need = sized ? 8U : 4U;
356 if ((Widen(f.dlc) < need) || (strict && (f.dlc != 8_u8))) {
359 r.size = sized ? LoadLe32At4(f.data) : 0U;
364constexpr Result<SdoReply> ParseUploadSegment(
const CanFrame& f, SdoReply r,
bool strict)
noexcept {
365 using detail::operator
""_u8;
366 const uint32_t b0 = Widen(f.data[0]);
367 const uint32_t len = 7U - ((b0 >> 1U) & 7U);
368 if ((Widen(f.dlc) < (1U + len)) || (strict && (f.dlc != 8_u8))) {
371 r.toggle = TestBit(b0, 4U);
372 r.last = TestBit(b0, 0U);
373 r.data_len =
static_cast<uint8_t
>(len);
374 for (uint32_t i = 0U; i < len; ++i) {
375 r.data[i] = f.data[1U + i];
381constexpr Result<SdoReply> ParseDownloadSegment(
const CanFrame& f, SdoReply r,
382 bool strict)
noexcept {
383 using detail::operator
""_u8;
385 r.toggle = TestBit(Widen(f.data[0]), 4U);
387 : Result<SdoReply>::Success(r);
391constexpr Result<SdoReply> ParseAbort(
const CanFrame& f, SdoReply r, uint32_t scs)
noexcept {
392 using detail::operator
""_u8;
393 if ((scs == 4U) && (f.dlc == 8_u8)) {
395 r.size = LoadLe32At4(f.data);
416 using detail::operator
""_u8;
417 if (frame.rtr || (frame.dlc < 1_u8) || (frame.dlc >
kMaxDlc)) {
421 const uint32_t b0 = detail::Widen(frame.data[0]);
422 const uint32_t scs = b0 >> 5U;
423 const bool header_ok = (frame.dlc >= 4_u8);
425 return detail::ParseDownloadSegment(frame, r, strict);
429 return detail::ParseUploadSegment(frame, r, strict);
431 if (!header_ok || ((scs != 4U) && strict && (frame.dlc != 8_u8))) {
434 r.index = detail::Le16(frame.data[1], frame.data[2]);
435 r.sub = frame.data[3];
442 return detail::ParseInitiateUpload(frame, r, strict);
444 return detail::ParseAbort(frame, r, scs);
454 using detail::operator
""_u8;
455 const uint32_t
id = detail::Widen(frame.id);
457 if (frame.rtr || (frame.dlc != 1_u8) || (
id <= base) ||
462 h.
node =
static_cast<uint8_t
>(
id - base);
463 h.toggle = detail::TestBit(detail::Widen(frame.data[0]), 7U);
464 const auto raw =
static_cast<uint8_t
>(detail::Widen(frame.data[0]) & 0x7FU);
478 using detail::operator
""_u8;
479 const uint32_t
id = detail::Widen(frame.id);
481 if (frame.rtr || (frame.dlc != 8_u8) || (
id <= base) ||
486 e.
node =
static_cast<uint8_t
>(
id - base);
487 e.code = detail::Le16(frame.data[0], frame.data[1]);
488 e.error_register = frame.data[2];
489 for (std::size_t i = 0; i < e.manufacturer.size(); ++i) {
490 e.manufacturer[i] = frame.data[3U + i];
504 using detail::operator
""_u16;
505 if ((number < 1_u16) || (number > 512_u16)) {
532 return (detail::Widen(entry.index) << 16U) | (detail::Widen(entry.sub) << 8U) |
533 detail::Widen(entry.bits);
543 e.
index =
static_cast<uint16_t
>(record >> 16U);
544 e.sub =
static_cast<uint8_t
>((record >> 8U) & 0xFFU);
545 e.bits =
static_cast<uint8_t
>(record & 0xFFU);
557 using detail::operator
""_u8;
558 using detail::operator
""_u16;
563 if ((config.transmission_type >= 241_u8) && (config.transmission_type <= 251_u8)) {
567 for (std::size_t i = 0; i < config.entry_count; ++i) {
569 if ((e.
index == 0_u16) || (e.
bits < 1_u8) || (e.
bits > 64_u8)) {
572 total += detail::Widen(e.
bits);
584 for (std::size_t i = 0; (i < config.entry_count) && (i < config.entries.size()); ++i) {
585 total += detail::Widen(config.entries[i].bits);
600 uint32_t bits)
noexcept {
601 const uint64_t end =
static_cast<uint64_t
>(offset) + bits;
602 if ((bits < 1U) || (bits > 64U) || (end > (
static_cast<uint64_t
>(data.size()) * 8U))) {
606 for (uint32_t i = 0U; i < bits; ++i) {
607 const uint32_t bit = offset + i;
608 const uint32_t b = (detail::Widen(data[bit / 8U]) >> (bit % 8U)) & 1U;
609 value |=
static_cast<uint64_t
>(b) << i;
624 uint64_t value)
noexcept {
625 const uint64_t end =
static_cast<uint64_t
>(offset) + bits;
626 if ((bits < 1U) || (bits > 64U) || (end > (
static_cast<uint64_t
>(data.size()) * 8U))) {
629 for (uint32_t i = 0U; i < bits; ++i) {
630 const uint32_t bit = offset + i;
631 const uint32_t mask = 1U << (bit % 8U);
632 const uint32_t old = detail::Widen(data[bit / 8U]);
633 const bool set = ((value >> i) & 1U) != 0U;
634 data[bit / 8U] =
static_cast<uint8_t
>(set ? (old | mask) : (old & ~mask));
648 const uint32_t sw = detail::Widen(statusword);
649 const uint32_t low = sw & 0x4FU;
650 const uint32_t quick = sw & 0x6FU;
652 return DriveState::NotReadyToSwitchOn;
655 return DriveState::SwitchOnDisabled;
658 return DriveState::FaultReactionActive;
661 return DriveState::Fault;
663 if (quick == 0x21U) {
664 return DriveState::ReadyToSwitchOn;
666 if (quick == 0x23U) {
667 return DriveState::SwitchedOn;
669 if (quick == 0x27U) {
670 return DriveState::OperationEnabled;
672 if (quick == 0x07U) {
673 return DriveState::QuickStopActive;
689 case DriveState::SwitchOnDisabled:
692 case DriveState::ReadyToSwitchOn:
695 case DriveState::SwitchedOn:
698 case DriveState::QuickStopActive:
701 case DriveState::NotReadyToSwitchOn:
702 case DriveState::OperationEnabled:
703 case DriveState::FaultReactionActive:
704 case DriveState::Fault:
Compile-time helpers of the CANOPEN_CLIENT driver: typed integer literals, exact hook matching,...
Public types of the CANopen client driver: errors, Result<T>, the CAN frame, the protocol vocabulary ...
constexpr uint32_t PdoBitLength(const PdoConfig &config) noexcept
The bits the entries of a PDO occupy in all.
Definition canopen_client_protocol.hpp:582
constexpr Result< CanFrame > MakeSyncFrame(uint8_t counter) noexcept
The SYNC frame: identifier 0x080, no data, or one counter byte.
Definition canopen_client_protocol.hpp:143
constexpr uint16_t SdoRequestId(uint8_t node) noexcept
The identifier of the SDO request frames to node.
Definition canopen_client_protocol.hpp:46
constexpr Result< CanFrame > MakeSdoAbort(uint8_t node, uint16_t index, uint8_t sub, uint32_t code) noexcept
The abort frame the client sends to end a transfer: command byte 0x80.
Definition canopen_client_protocol.hpp:320
constexpr Result< SdoReply > ParseSdoReply(const CanFrame &frame, bool strict) noexcept
Decode a frame from an SDO server (identifier 0x580 + node).
Definition canopen_client_protocol.hpp:415
constexpr uint16_t EmcyId(uint8_t node) noexcept
The identifier of the emergency frames of node.
Definition canopen_client_protocol.hpp:73
constexpr Result< CanFrame > MakeSdoSegmentedDownload(uint8_t node, uint16_t index, uint8_t sub, uint32_t size) noexcept
The segmented initiate-download request announcing size bytes: command byte 0x21.
Definition canopen_client_protocol.hpp:256
constexpr bool EnableStepControlword(DriveState state, uint16_t &word) noexcept
The controlword that moves a drive one step toward Operation enabled.
Definition canopen_client_protocol.hpp:687
constexpr uint32_t EncodePdoMapEntry(const PdoMapEntry &entry) noexcept
The 32-bit mapping record: index << 16 | sub-index << 8 | length in bits.
Definition canopen_client_protocol.hpp:531
constexpr Result< CanFrame > MakeSdoExpeditedDownload(uint8_t node, uint16_t index, uint8_t sub, std::span< const uint8_t > data) noexcept
The expedited initiate-download request (write 1 to 4 bytes): command byte 0x23 | (4 - size) << 2.
Definition canopen_client_protocol.hpp:234
constexpr Result< CanFrame > MakeNmtFrame(NmtCommand command, uint8_t node) noexcept
The NMT command frame: identifier 0x000, two data bytes.
Definition canopen_client_protocol.hpp:124
constexpr Result< CanFrame > MakeSdoUploadSegmentRequest(uint8_t node, bool toggle) noexcept
The request for the next upload segment: command byte 0x60 | toggle << 4.
Definition canopen_client_protocol.hpp:300
constexpr Result< CanFrame > MakeGuardRequest(uint8_t node) noexcept
The node-guarding request: a remote frame on 0x700 + node.
Definition canopen_client_protocol.hpp:162
constexpr Result< uint16_t > PdoMappingIndex(PdoKind kind, uint16_t number) noexcept
The index of the mapping parameter record of a PDO.
Definition canopen_client_protocol.hpp:518
constexpr bool IsNodeId(uint8_t node) noexcept
Whether a node identifier is in 1 to 127.
Definition canopen_client_protocol.hpp:37
constexpr uint16_t HeartbeatId(uint8_t node) noexcept
The identifier of the heartbeat and node-guarding frames of node.
Definition canopen_client_protocol.hpp:64
constexpr Result< uint16_t > PdoCommunicationIndex(PdoKind kind, uint16_t number) noexcept
The index of the communication parameter record of a PDO.
Definition canopen_client_protocol.hpp:503
constexpr PdoMapEntry DecodePdoMapEntry(uint32_t record) noexcept
Split a 32-bit mapping record into its object and length.
Definition canopen_client_protocol.hpp:541
constexpr uint16_t SdoResponseId(uint8_t node) noexcept
The identifier of the SDO reply frames from node.
Definition canopen_client_protocol.hpp:55
constexpr Result< HeartbeatInfo > ParseHeartbeat(const CanFrame &frame) noexcept
Decode a heartbeat, boot-up or node-guarding reply: identifier 0x701 to 0x77F, one byte.
Definition canopen_client_protocol.hpp:453
constexpr Result< CanFrame > MakeSdoDownloadSegment(uint8_t node, bool toggle, std::span< const uint8_t > data, bool last) noexcept
One download segment: command byte toggle << 4 | (7 - length) << 1 | last.
Definition canopen_client_protocol.hpp:275
constexpr Result< CanFrame > MakePdoFrame(uint16_t cob_id, std::span< const uint8_t > data) noexcept
A PDO frame: a valid identifier and 0 to 8 data bytes.
Definition canopen_client_protocol.hpp:178
constexpr Result< void > InsertBits(std::span< uint8_t > data, uint32_t offset, uint32_t bits, uint64_t value) noexcept
Write the low bits bits of value at bit offset of a PDO payload; the other bits are unchanged.
Definition canopen_client_protocol.hpp:623
constexpr Result< EmcyEvent > ParseEmcy(const CanFrame &frame) noexcept
Decode an emergency message: identifier 0x081 to 0x0FF, eight bytes (error code, error register,...
Definition canopen_client_protocol.hpp:477
constexpr bool DecodeNmtState(uint8_t raw, NmtState &state) noexcept
Decode the state byte of a heartbeat (bit 7 already cleared).
Definition canopen_client_protocol.hpp:96
constexpr Error ValidatePdoConfig(const PdoConfig &config) noexcept
Check a PDO configuration: identifier range, a transmission type that is not reserved (241 to 251),...
Definition canopen_client_protocol.hpp:556
constexpr Result< uint64_t > ExtractBits(std::span< const uint8_t > data, uint32_t offset, uint32_t bits) noexcept
Read bits bits starting at bit offset of a PDO payload (Intel bit order: bit 0 is the least significa...
Definition canopen_client_protocol.hpp:599
constexpr bool IsNmtCommand(uint8_t command) noexcept
Whether a command byte is one of the five NMT commands.
Definition canopen_client_protocol.hpp:82
constexpr Result< CanFrame > MakeSdoUploadRequest(uint8_t node, uint16_t index, uint8_t sub) noexcept
The initiate-upload request (read an object): command byte 0x40.
Definition canopen_client_protocol.hpp:216
constexpr DriveState DecodeStatusword(uint16_t statusword) noexcept
Decode the drive state from a statusword (object 0x6041): the masks 0x4F and 0x6F of the state machin...
Definition canopen_client_protocol.hpp:647
constexpr uint8_t kMinNodeId
Lowest valid node identifier.
Definition canopen_client_types.hpp:218
NmtCommand
NMT master command (the first data byte of the frame on identifier 0x000).
Definition canopen_client_types.hpp:263
constexpr uint16_t kPdoCobIdMax
Highest identifier the driver accepts for a PDO (the last pre-defined RPDO4 identifier).
Definition canopen_client_types.hpp:237
DriveState
The drive state machine states, decoded from the statusword.
Definition canopen_client_types.hpp:498
constexpr uint8_t kMaxDlc
Longest classic CAN payload in bytes.
Definition canopen_client_types.hpp:182
constexpr uint16_t kControlEnableOperation
Controlword: enable operation (to Operation enabled).
Definition canopen_client_types.hpp:573
constexpr uint16_t kCobIdEmcyBase
EMCY identifiers are this plus the node identifier.
Definition canopen_client_types.hpp:227
constexpr uint16_t kPdoCobIdMin
Lowest identifier the driver accepts for a PDO (the first pre-defined TPDO1 of node 1).
Definition canopen_client_types.hpp:235
constexpr uint16_t kCobIdSdoResponseBase
SDO response (server to client) identifiers are this plus the node identifier.
Definition canopen_client_types.hpp:229
NmtState
NMT state a node reports in its heartbeat or guarding reply (bit 7 masked off).
Definition canopen_client_types.hpp:272
constexpr uint8_t kMaxNodeId
Highest valid node identifier.
Definition canopen_client_types.hpp:220
constexpr uint16_t kControlDisableVoltage
Controlword: disable voltage (to Switch on disabled).
Definition canopen_client_types.hpp:569
constexpr uint16_t kControlSwitchOn
Controlword: switch on (to Switched on).
Definition canopen_client_types.hpp:567
constexpr uint16_t kCobIdSync
SYNC identifier.
Definition canopen_client_types.hpp:225
constexpr uint16_t kCobIdHeartbeatBase
Heartbeat and node-guarding identifiers are this plus the node identifier.
Definition canopen_client_types.hpp:233
constexpr uint16_t kControlShutdown
Controlword: shutdown (to Ready to switch on).
Definition canopen_client_types.hpp:565
constexpr uint16_t kCobIdSdoRequestBase
SDO request (client to server) identifiers are this plus the node identifier.
Definition canopen_client_types.hpp:231
PdoKind
Whether a PDO is a receive PDO (client to node) or a transmit PDO (node to client).
Definition canopen_client_types.hpp:471
constexpr uint16_t kCobIdNmt
NMT command identifier (pre-defined connection set).
Definition canopen_client_types.hpp:223
Error
Why a call failed. Values 0 to 9 are the shared base set; client-specific values start at 16.
Definition canopen_client_types.hpp:36
constexpr std::size_t kMaxPdoEntries
Entries of one PDO mapping the helpers handle.
Definition canopen_client_types.hpp:248
@ PreOperational
Go to Pre-operational.
@ Start
Go to Operational.
@ ResetNode
Reset the whole application and communication.
@ ResetCommunication
Reset the communication parameters only.
@ Unknown
The statusword matches no state.
@ BootUp
The boot-up message; the node continues to Pre-operational.
@ Operational
SDO and PDO are on.
@ PreOperational
SDO is on, PDO is off.
@ Stopped
Only NMT and heartbeat; SDO and PDO are off.
@ InitiateDownload
Response to an initiate download request (scs 3).
@ Abort
Abort transfer (scs 4).
@ SegmentDownload
Response to a download segment (scs 1).
@ SegmentUpload
A segment of upload data (scs 0).
@ InitiateUpload
Response to an initiate upload request (scs 2).
@ Receive
RPDO: the node consumes it.
@ InvalidArgument
An argument or a Config field is out of range; nothing was sent.
@ InvalidFrame
A frame fails validation (identifier, length or content).
The CANopen client driver: everything it declares lives here; internals in detail.
Definition canopen_client.hpp:28
One classic CAN frame with an 11-bit identifier.
Definition canopen_client_types.hpp:190
std::array< uint8_t, 8 > data
The payload; bytes at and past dlc are not meaningful.
Definition canopen_client_types.hpp:194
uint16_t id
The 11-bit identifier (COB-ID).
Definition canopen_client_types.hpp:191
One emergency message (EMCY).
Definition canopen_client_types.hpp:433
uint8_t node
The node that sent it.
Definition canopen_client_types.hpp:434
A decoded heartbeat or node-guarding reply.
Definition canopen_client_types.hpp:416
uint8_t node
The node (identifier minus 0x700).
Definition canopen_client_types.hpp:417
The communication and mapping parameters of one PDO.
Definition canopen_client_types.hpp:484
One mapped object of a PDO.
Definition canopen_client_types.hpp:477
uint8_t bits
Length in bits, 1 to 64.
Definition canopen_client_types.hpp:480
uint16_t index
Object index.
Definition canopen_client_types.hpp:478
The value of a call, or why there is none.
Definition canopen_client_types.hpp:106
static constexpr Result Success(T v) noexcept
A successful result.
Definition canopen_client_types.hpp:123
static constexpr Result Failure(Error e) noexcept
A failed result.
Definition canopen_client_types.hpp:131
A decoded SDO server reply.
Definition canopen_client_types.hpp:399