DeterministicESPAsyncWebServer v6.28.0
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
cclink.h
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 cclink.h
6 * @brief CC-Link (CLPA) cyclic fieldbus frame codec (DETWS_ENABLE_CCLINK).
7 *
8 * CC-Link is Mitsubishi's (CLPA) factory fieldbus. The classic CC-Link master polls remote stations
9 * over RS-485 exchanging a cyclic process image split into bit devices (RX/RY - remote input/output
10 * bits) and word devices (RWr/RWw - remote registers). This codec builds/validates the cyclic frame a
11 * master/station exchanges:
12 *
13 * [station][command][RX/RY bit data...][RWr/RWw word data...][sum-checksum]
14 *
15 * A station's process image is a fixed BSS block; this frames it. The checksum is the low byte of the
16 * arithmetic sum of the framed bytes. The RS-485 timing and the CC-Link IE Field (Gigabit) PHY are the
17 * hardware-gated part; this is the frame + process-image accessors. Pure, zero heap, host-testable.
18 */
19
20#ifndef DETERMINISTICESPASYNCWEBSERVER_CCLINK_H
21#define DETERMINISTICESPASYNCWEBSERVER_CCLINK_H
22
23#include "ServerConfig.h"
24#include <stddef.h>
25#include <stdint.h>
26
27#if DETWS_ENABLE_CCLINK
28
29// CC-Link command bytes: wire values compared/emitted, so integer constants in a namespacing struct.
30struct CclinkCmd
31{
32 static constexpr uint8_t CCLINK_CMD_REFRESH = 0x01; ///< cyclic refresh (master <-> station process image).
33 static constexpr uint8_t CCLINK_CMD_POLL = 0x02; ///< poll a station.
34 static constexpr uint8_t CCLINK_CMD_TEST = 0x0F; ///< line test.
35};
36
37/** @brief Arithmetic-sum checksum: low byte of the sum of @p len bytes. */
38uint8_t detws_cclink_sum(const uint8_t *bytes, size_t len);
39
40/**
41 * @brief Build a CC-Link cyclic frame: [station][command][bit_data...][word_data...][checksum].
42 * @param station station number 0..63.
43 * @param command CCLINK_CMD_*.
44 * @param bits the RX/RY bit-device bytes (may be null if bit_len == 0).
45 * @param bit_len number of bit-device bytes.
46 * @param words the RWr/RWw word-device bytes (little-endian words; may be null if word_len == 0).
47 * @param word_len number of word-device bytes.
48 * @return the frame length (2 + bit_len + word_len + 1), or 0 on overflow / bad args.
49 */
50size_t detws_cclink_build(uint8_t station, uint8_t command, const uint8_t *bits, size_t bit_len, const uint8_t *words,
51 size_t word_len, uint8_t *out, size_t cap);
52
53/** @brief A parsed CC-Link frame (payload points into the input; caller knows the bit/word split). */
54struct CcLinkFrame
55{
56 uint8_t station;
57 uint8_t command;
58 const uint8_t *payload; ///< the bit+word data region.
59 size_t payload_len;
60};
61
62/** @brief Validate the checksum and parse a CC-Link frame. @return true if the checksum matches. */
63bool detws_cclink_parse(const uint8_t *frame, size_t len, CcLinkFrame *out);
64
65/** @brief Read bit @p index (0-based) from a bit-device byte array. */
66bool detws_cclink_get_bit(const uint8_t *bits, size_t bit_len, size_t index);
67
68/** @brief Set/clear bit @p index in a bit-device byte array (no-op if out of range). */
69void detws_cclink_set_bit(uint8_t *bits, size_t bit_len, size_t index, bool value);
70
71/** @brief Read word @p index (0-based, little-endian) from a word-device byte array. */
72uint16_t detws_cclink_get_word(const uint8_t *words, size_t word_len, size_t index);
73
74#endif // DETWS_ENABLE_CCLINK
75#endif // DETERMINISTICESPASYNCWEBSERVER_CCLINK_H
User-facing configuration for DeterministicESPAsyncWebServer.