rcbench
Servo and ESC test bench: panel, IOmcu and shared C
Loading...
Searching...
No Matches
can_selftest.h
1/*
2 * The CAN (Controller Area Network) echo self-test: the first check when two
3 * nodes are wired together.
4 *
5 * It answers one question: do frames cross this bus intact? The initiator
6 * sends a frame with a sequence number and a payload, the responder sends it
7 * back unchanged, and the initiator compares it byte for byte. No page map,
8 * no registers, no state machine above it. A passing test with a link that
9 * does not work places the fault above the wire. A wrong bit timing, a
10 * missing terminator, a transceiver in the wrong mode and a dispatcher bug
11 * all present as a panel that shows no numbers; this test separates the
12 * first three from the fourth.
13 *
14 * The payloads exercise bit stuffing. CAN stuffs a complementary bit after
15 * five bits of the same polarity, so a marginal bus fails on long runs of one
16 * level. The first two payload bytes carry the sequence number; the other
17 * six carry one of four patterns selected by it (0x00, 0xFF, 0x55, 0xAA),
18 * and the echo is checked against the pattern it was sent with.
19 *
20 * The test runs at the lowest arbitration priority on a page the map does
21 * not use, so it can run beside normal traffic without delaying it.
22 *
23 * SPDX-License-Identifier: MIT
24 */
25
26#ifndef RCBENCH_CAN_SELFTEST_H
27#define RCBENCH_CAN_SELFTEST_H
28
29#include <stdbool.h>
30#include <stdint.h>
31
32#include "link_can.h"
33
34#ifdef __cplusplus
35extern "C" {
36#endif
37
40#define CAN_SELFTEST_PAGE 0x7Eu
41
43#define CAN_SELFTEST_MIN_PROBES 8u
44
45typedef enum {
46 CAN_SELFTEST_RUNNING = 0,
47 CAN_SELFTEST_OK,
49 CAN_SELFTEST_SILENT,
51 CAN_SELFTEST_CORRUPT,
54 CAN_SELFTEST_LOSSY,
63 CAN_SELFTEST_DROPPED,
64} can_selftest_verdict_t;
65
66typedef struct {
67 uint32_t sent;
68 uint32_t echoed;
69 uint32_t timed_out;
70 uint32_t corrupt;
71 uint32_t stale;
72
73 uint16_t seq;
74 uint16_t outstanding;
75 bool waiting;
76 uint32_t sent_ms;
77 uint32_t timeout_ms;
78
79 uint32_t rtt_min_us;
80 uint32_t rtt_max_us;
81
88 uint32_t bus_errors;
90
91void can_selftest_init(can_selftest_t *st, uint32_t timeout_ms);
92
100bool can_selftest_probe(can_selftest_t *st, uint32_t now_ms,
101 link_can_frame_t *out);
102
108bool can_selftest_rx(can_selftest_t *st, const link_can_frame_t *f,
109 uint32_t rtt_us);
110
112bool can_selftest_tick(can_selftest_t *st, uint32_t now_ms);
113
114can_selftest_verdict_t can_selftest_verdict(const can_selftest_t *st);
115const char *can_selftest_text(can_selftest_verdict_t v);
116const char *can_selftest_hint(can_selftest_verdict_t v);
117
118/* ----------------------------------------------- the far end's own numbers */
119
120/*
121 * The IOmcu's view of the bus, carried across the bus in one 8-byte
122 * frame on the self-test page at the lowest priority, so one console shows
123 * both ends of a fault.
124 *
125 * It is polled: the IOmcu transmits only in answer to a request.
126 */
127typedef struct {
128 bool up;
130 uint8_t tx_errors;
131 uint8_t rx_errors;
132 uint8_t flags;
133 uint16_t echoes;
134 uint16_t overflows;
136
138bool can_selftest_status_request(link_can_frame_t *out);
139
141bool can_selftest_status_reply(const link_can_frame_t *in,
142 const can_remote_status_t *st,
143 link_can_frame_t *out);
144
146bool can_selftest_status_parse(const link_can_frame_t *in,
148
164uint16_t can_selftest_return_loss(uint16_t before, uint16_t after,
165 uint32_t heard);
166
167/* ------------------------------------------------------------- responder */
168
174bool can_selftest_echo(const link_can_frame_t *in, link_can_frame_t *out);
175
176#ifdef __cplusplus
177}
178#endif
179
180#endif /* RCBENCH_CAN_SELFTEST_H */
Definition can_selftest.h:127
bool up
Definition can_selftest.h:128
uint8_t rx_errors
Definition can_selftest.h:131
uint16_t echoes
Definition can_selftest.h:133
uint8_t tx_errors
Definition can_selftest.h:130
uint16_t overflows
Definition can_selftest.h:134
uint8_t flags
Definition can_selftest.h:132
Definition can_selftest.h:66
uint32_t bus_errors
Definition can_selftest.h:88
uint16_t outstanding
Definition can_selftest.h:74
uint16_t seq
Definition can_selftest.h:73
uint32_t corrupt
Definition can_selftest.h:70
uint32_t stale
Definition can_selftest.h:71
uint32_t echoed
Definition can_selftest.h:68