DeterministicESPAsyncWebServer v6.28.0
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
sercos.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file sercos.cpp
6 * @brief SERCOS III telegram + IDN codec (see sercos.h).
7 */
8
10
11#if DETWS_ENABLE_SERCOS
12
13#include <string.h>
14
15uint16_t detws_sercos_idn(bool is_product, uint8_t param_set, uint16_t data_block)
16{
17 return (uint16_t)(((is_product ? 1u : 0u) << 15) | ((uint32_t)(param_set & 0x7) << 12) | (data_block & 0x0FFF));
18}
19
20void detws_sercos_idn_parse(uint16_t idn, bool *is_product, uint8_t *param_set, uint16_t *data_block)
21{
22 if (is_product)
23 *is_product = (idn & 0x8000) != 0;
24 if (param_set)
25 *param_set = (uint8_t)((idn >> 12) & 0x7);
26 if (data_block)
27 *data_block = (uint16_t)(idn & 0x0FFF);
28}
29
30size_t detws_sercos_build(uint8_t type, uint8_t phase, uint16_t cycle, const uint8_t *data, size_t data_len,
31 uint8_t *out, size_t cap)
32{
33 if (!out || (data_len && !data) || (type != Sercos::SERCOS_TEL_MDT && type != Sercos::SERCOS_TEL_AT))
34 return 0;
35 size_t n = Sercos::SERCOS_HDR_LEN + data_len;
36 if (n > cap)
37 return 0;
38 out[0] = type;
39 out[1] = phase;
40 out[2] = (uint8_t)cycle; // little-endian cycle count
41 out[3] = (uint8_t)(cycle >> 8);
42 if (data_len)
43 memcpy(out + Sercos::SERCOS_HDR_LEN, data, data_len);
44 return n;
45}
46
47bool detws_sercos_parse(const uint8_t *frame, size_t len, SercosTelegram *out)
48{
49 if (!frame || !out || len < Sercos::SERCOS_HDR_LEN)
50 return false;
51 if (frame[0] != Sercos::SERCOS_TEL_MDT && frame[0] != Sercos::SERCOS_TEL_AT)
52 return false;
53 out->type = frame[0];
54 out->phase = frame[1];
55 out->cycle = (uint16_t)(frame[2] | (frame[3] << 8));
56 out->data = (len > Sercos::SERCOS_HDR_LEN) ? (frame + Sercos::SERCOS_HDR_LEN) : nullptr;
57 out->data_len = len - Sercos::SERCOS_HDR_LEN;
58 return true;
59}
60
61#endif // DETWS_ENABLE_SERCOS
SERCOS III motion-bus telegram + IDN codec (DETWS_ENABLE_SERCOS).