ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
nema_ts2.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 nema_ts2.h
6 * @brief NEMA TS 2 traffic-cabinet SDLC frame codec (PROTOCORE_ENABLE_NEMA_TS2).
7 *
8 * NEMA TS 2 links the devices inside a traffic-signal control cabinet - the controller, the Malfunction
9 * Management Unit (MMU), the Bus Interface Units (BIUs) and detector racks - over a synchronous SDLC
10 * bus. Each transaction is an SDLC frame:
11 *
12 * [address][control][frame-type][data...][FCS-16]
13 *
14 * where the FCS is the HDLC/X.25 CRC-16 (CRC-16/X-25: reflected poly 0x1021, init 0xFFFF, xorout 0xFFFF)
15 * over address..last-data, transmitted low byte first. This builds and validates those frames (the
16 * frame-type identifies command / status / detector frames per the TS 2 frame set); the synchronous
17 * serial PHY and the BIU/detector timing are the hardware-gated part. Pure, zero heap, host-testable.
18 */
19
20#ifndef PROTOCORE_NEMA_TS2_H
21#define PROTOCORE_NEMA_TS2_H
22
23#include "protocore_config.h" // the entry point: protocore_types.h for the widths
24
25#if PROTOCORE_ENABLE_NEMA_TS2
26
28
29/** @brief Common TS 2 frame types (the third octet). */
30// NEMA TS2 frame types: wire values compared, so integer constants in a namespacing struct.
31typedef struct
32{
33#define NEMA_TS2_FT_CMD_LOADSWITCH 0 ///< controller -> BIU: load-switch (signal-head) drivers.
34#define NEMA_TS2_FT_STATUS_LOADSWITCH 128 ///< BIU -> controller: load-switch status (frame type + 128).
35#define NEMA_TS2_FT_CMD_MMU 3 ///< controller <-> MMU status frame.
36#define NEMA_TS2_FT_DETECTOR 9 ///< detector BIU -> controller: detector call/status.
37} NemaTs2;
38
39/** @brief HDLC/X.25 CRC-16 (CRC-16/X-25) over @p len bytes. */
40uint16_t protocore_nema_ts2_crc(const uint8_t *bytes, size_t len);
41
42/**
43 * @brief Build a TS 2 SDLC frame: [address][control][frame_type][data...][FCS lo][FCS hi].
44 * @return the frame length (3 + data_len + 2), or 0 on overflow / bad args.
45 */
46size_t protocore_nema_ts2_build(uint8_t address, uint8_t control, uint8_t frame_type, const uint8_t *data,
47 size_t data_len, uint8_t *out, size_t cap);
48
49/** @brief A parsed TS 2 frame (data points into the input). */
50typedef struct
51{
52 uint8_t address;
53 uint8_t control;
54 uint8_t frame_type;
55 const uint8_t *data;
56 size_t data_len;
57} NemaTs2Frame;
58
59/** @brief Validate the FCS and parse a TS 2 frame. @return true if the CRC matches and it is well-formed. */
60proto_bool protocore_nema_ts2_parse(const uint8_t *frame, size_t len, NemaTs2Frame *out);
61
63
64#endif // PROTOCORE_ENABLE_NEMA_TS2
65
66#endif // PROTOCORE_NEMA_TS2_H
#define PROTOCORE_BEGIN_DECLS
Give a header's declarations C linkage, so their symbol names carry no parameter types.
Definition types.h:96
_Bool proto_bool
The truth value.
Definition types.h:64
#define PROTOCORE_END_DECLS
Definition types.h:97