rcbench
Servo and ESC test bench: panel, IOmcu and shared C
Loading...
Searching...
No Matches
sense_bus.h
Go to the documentation of this file.
1
45
46#pragma once
47
48#include <stdbool.h>
49#include <stddef.h>
50#include <stdint.h>
51
52#ifdef __cplusplus
53extern "C" {
54#endif
55
72
77typedef struct {
78 sense_err_t (*read)(void *ctx, uint8_t addr, uint8_t reg,
79 uint8_t *buf, size_t n);
80 sense_err_t (*write)(void *ctx, uint8_t addr, uint8_t reg,
81 const uint8_t *buf, size_t n);
82 void *ctx;
84
85#define SENSE_STUCK_TIMEOUTS 2u
86#define SENSE_RECOVER_MS 100u
88#define SENSE_FAILS 3u
89#define SENSE_RETRY_MS 1000u
90
91typedef struct {
92 sense_i2c_t io;
93 uint16_t errors;
94 uint8_t timeouts;
95 bool stuck;
96 bool trial;
97 bool recovered;
98 uint32_t recover_at;
99 uint32_t recoveries;
101
108void sense_bus_init(sense_bus_t *b, const sense_i2c_t *io);
109
120sense_err_t sense_bus_read(sense_bus_t *b, uint8_t addr, uint8_t reg,
121 uint8_t *buf, size_t n);
122
132sense_err_t sense_bus_write16(sense_bus_t *b, uint8_t addr, uint8_t reg,
133 uint16_t value);
134
149
159bool sense_bus_recovery_due(const sense_bus_t *b, uint32_t now_ms);
160
170void sense_bus_recovered(sense_bus_t *b, uint32_t now_ms);
171
172/* ----------------------------------------------------------- one part */
173
186
190typedef struct {
191 sense_bus_t *bus;
192 uint8_t addr;
193 sense_state_t state;
194 uint8_t fails;
196 uint16_t id_maker;
197 uint16_t id_device;
198 uint32_t now;
199 uint32_t probe_at;
201
209void sense_part_init(sense_part_t *p, sense_bus_t *bus, uint8_t addr);
210
218bool sense_part_probe_due(sense_part_t *p, uint32_t now_ms);
219
228
241 uint8_t *buf, size_t n);
250sense_err_t sense_part_write16(sense_part_t *p, uint8_t reg, uint16_t value);
251
262void sense_part_sample_failed(sense_part_t *p, uint8_t before);
263
273
274/* ------------------------------------------------------------- codec */
275
282uint16_t sense_be16(const uint8_t *b);
289uint32_t sense_be24(const uint8_t *b);
296uint64_t sense_be40(const uint8_t *b);
297
305int32_t sense_sext32(uint32_t v, unsigned bits);
313int64_t sense_sext64(uint64_t v, unsigned bits);
314
326int64_t sense_muldiv(int64_t a, uint32_t mul, uint64_t div);
327
329typedef enum {
331 SENSE_CLIP_NONE = 0,
334
341typedef struct {
342 int32_t value;
343 sense_clip_t clip;
345
346#ifdef __cplusplus
347}
348#endif
void sense_part_probed(sense_part_t *p, sense_state_t state)
The probe's result; a part left anything but online is probed again SENSE_RETRY_MS on.
Definition sense_bus.c:151
uint64_t sense_be40(const uint8_t *b)
Read a big-endian 40-bit value.
Definition sense_bus.c:232
sense_clip_t
A reading that may be at the end of its register's range.
Definition sense_bus.h:329
@ SENSE_CLIP_HIGH
Definition sense_bus.h:332
@ SENSE_CLIP_LOW
Definition sense_bus.h:330
bool sense_part_probe_due(sense_part_t *p, uint32_t now_ms)
Every tick, with the time: whether the part is to be probed now.
Definition sense_bus.c:135
void sense_bus_recovered(sense_bus_t *b, uint32_t now_ms)
The caller has run a recovery.
Definition sense_bus.c:114
void sense_bus_init(sense_bus_t *b, const sense_i2c_t *io)
Set up a bus over a wire.
Definition sense_bus.c:14
void sense_bus_note(sense_bus_t *b, sense_err_t e)
A transaction made on the wire without this module – an address scan – and what it came to,...
Definition sense_bus.c:89
sense_err_t sense_bus_read(sense_bus_t *b, uint8_t addr, uint8_t reg, uint8_t *buf, size_t n)
A register read: n bytes, most significant first, into buf.
Definition sense_bus.c:70
bool sense_bus_recovery_due(const sense_bus_t *b, uint32_t now_ms)
Whether a recovery is to be run now: the bus is stuck, and none has run since, or the last ran SENSE_...
Definition sense_bus.c:106
sense_err_t sense_bus_write16(sense_bus_t *b, uint8_t addr, uint8_t reg, uint16_t value)
A 16-bit register write.
Definition sense_bus.c:79
sense_err_t
What a transaction came to.
Definition sense_bus.h:62
@ SENSE_BUS_LOW
Definition sense_bus.h:66
@ SENSE_BAD_ARG
Definition sense_bus.h:70
@ SENSE_OFFLINE
Definition sense_bus.h:69
@ SENSE_TIMEOUT
Definition sense_bus.h:65
@ SENSE_NACK
Definition sense_bus.h:64
@ SENSE_STUCK
Definition sense_bus.h:68
uint16_t sense_be16(const uint8_t *b)
Big-endian register contents, most significant byte first.
Definition sense_bus.c:222
int32_t sense_sext32(uint32_t v, unsigned bits)
The low bits of v as a two's complement number (1 to 32 bits).
Definition sense_bus.c:242
sense_err_t sense_part_write16(sense_part_t *p, uint8_t reg, uint16_t value)
Write a 16-bit register of a part, counted towards taking it offline.
Definition sense_bus.c:189
void sense_part_lost(sense_part_t *p)
An online part that answers and no longer holds the set-up written to it: offline,...
Definition sense_bus.c:210
void sense_part_init(sense_part_t *p, sense_bus_t *bus, uint8_t addr)
Set up a part record on a bus.
Definition sense_bus.c:127
sense_state_t
A part on the bus, as far as it is known.
Definition sense_bus.h:175
@ SENSE_PART_OFFLINE
Definition sense_bus.h:183
@ SENSE_PART_WRONG_ID
Definition sense_bus.h:179
@ SENSE_PART_REFUSED
Definition sense_bus.h:180
@ SENSE_PART_UNPROBED
Definition sense_bus.h:177
@ SENSE_PART_ABSENT
Definition sense_bus.h:178
@ SENSE_PART_UNSET
Definition sense_bus.h:176
int64_t sense_sext64(uint64_t v, unsigned bits)
The same for up to 64 bits.
Definition sense_bus.c:253
void sense_part_sample_failed(sense_part_t *p, uint8_t before)
After a sample of two reads whose second failed: the failure run is before plus one,...
Definition sense_bus.c:197
uint32_t sense_be24(const uint8_t *b)
Read a big-endian 24-bit value.
Definition sense_bus.c:227
sense_err_t sense_part_read(sense_part_t *p, uint8_t reg, uint8_t *buf, size_t n)
A read or write of an online part, counted towards taking it offline.
Definition sense_bus.c:180
int64_t sense_muldiv(int64_t a, uint32_t mul, uint64_t div)
a × mul / div without the product overflowing, truncated towards zero.
Definition sense_bus.c:264
Definition sense_bus.h:91
bool stuck
Definition sense_bus.h:95
uint16_t errors
Definition sense_bus.h:93
uint32_t recoveries
Definition sense_bus.h:99
uint32_t recover_at
Definition sense_bus.h:98
bool recovered
Definition sense_bus.h:97
uint8_t timeouts
Definition sense_bus.h:94
bool trial
Definition sense_bus.h:96
The wire. n is the register width in bytes: 2, 3 or 5 for a read (INA228 SLYS021A Table 7-3,...
Definition sense_bus.h:77
A part on the bus: its address, state and last probe.
Definition sense_bus.h:190
uint8_t fails
Definition sense_bus.h:194
uint32_t probe_at
Definition sense_bus.h:199
uint16_t id_device
Definition sense_bus.h:197
sense_err_t last_err
Definition sense_bus.h:195
uint32_t now
Definition sense_bus.h:198
uint16_t id_maker
Definition sense_bus.h:196
A measurement, or a clip.
Definition sense_bus.h:341