rcbench
Servo and ESC test bench: panel, IOmcu and shared C
Loading...
Searching...
No Matches
pdmini.h
Go to the documentation of this file.
1
123
124#pragma once
125
126#include <stdbool.h>
127#include <stddef.h>
128#include <stdint.h>
129
130#ifdef __cplusplus
131extern "C" {
132#endif
133
135#define PDMINI_V_MIN_MV 1000u
136#define PDMINI_V_MAX_MV 20000u
137#define PDMINI_I_MIN_MA 50u
138#define PDMINI_I_MAX_MA 3000u
150#define PDMINI_HEADROOM_MV 500u
162#define PDMINI_CAP_MV 700u
163
164#ifdef __cplusplus
165static_assert(PDMINI_CAP_MV > PDMINI_HEADROOM_MV,
166 "a set point at the cap lies above the sag cut line");
167#else
168_Static_assert(PDMINI_CAP_MV > PDMINI_HEADROOM_MV,
169 "a set point at the cap lies above the sag cut line");
170#endif
171
178#define PDMINI_WHO "PD Power Mini"
179
181enum {
191};
192
194enum { PDMINI_MODE_NORMAL = 0, PDMINI_MODE_CC = 1, PDMINI_MODE_OC = 2 };
195
197#define PDMINI_ATTACH_MS 5u
198#define PDMINI_REPLY_MS 400u
199#define PDMINI_BYTE_MS 60u
200#define PDMINI_TXN_MS 600u
202#define PDMINI_CONFIRM_MS 250u
203#define PDMINI_WATCH_MS 1000u
205#define PDMINI_GAP_MS 10u
206#define PDMINI_DISPLAY_MS 100u
207#define PDMINI_POLL_LATE_MS 400u
213#define PDMINI_STATE_MS 500u
214#define PDMINI_IDENTIFY_MS 1000u
215#define PDMINI_SLOT_MS 1000u
216#define PDMINI_RETRY_MS 2000u
217#define PDMINI_FAILS 3u
218#define PDMINI_ABSENT_TRIES 10u
221#define PDMINI_INPUT_MISSES 3u
226#define PDMINI_INPUT_RETRY_MS 5000u
227#define PDMINI_SAG_READS 2u
231
239uint8_t pdmini_crc8(const uint8_t *p, size_t n);
240
249
258pdmini_frame_t pdmini_reply_frame(uint8_t cmd, const uint8_t *buf, size_t n);
259
261typedef struct {
262 void (*attach)(void *ctx);
263 void (*detach)(void *ctx);
264 void (*send)(void *ctx, const uint8_t *p, size_t n);
265 void *ctx;
267
269typedef struct {
270 bool online;
271 bool output;
272 uint8_t mode;
273 uint16_t v_mv,
275 uint16_t set_mv;
276 uint16_t set_ma;
277 uint8_t in_state;
278 uint16_t vin_mv;
279 bool stuck;
281 bool tripped;
282 bool sagged;
284 uint32_t samples;
285 uint32_t errors;
287
290typedef struct {
291 pdmini_io_t io;
293
294 /* What is asked for. */
295 bool want_output;
296 bool want_set;
297 uint16_t want_mv, want_ma;
298
299 /* What is known. */
300 bool identified;
301 bool answered;
302 bool restored;
303 uint8_t who_misses;
304 uint16_t who_failed; /* WHO_AM_I without a valid answer: a count
305 to compare for a change, wrapping from
306 65535 to 0 */
308 int slot;
310 uint8_t on_value;
311 uint8_t en_value;
312 bool en_for;
313 bool on_seen;
315 bool on_sent;
317 bool held_off; /* switched off for set points that would not
318 take; until an OFF is asked */
319 bool off_owed;
321 uint8_t en_tries;
323 uint32_t en_at;
325 uint8_t data_tries;
326 uint32_t data_at;
327 uint8_t fails;
328 uint8_t state_fails;
329 uint8_t input_misses;
332 uint8_t on_step; /* reads before an ON: 1 data asked, 2 data
333 seen, 3 slot asked, 4 slot seen */
334 bool polled_late; /* the last job a reading that went before
335 the set points: an overdue one, or the
336 input read before a set point or an ON */
337 uint8_t sag_reads; /* input reads in a row under a live output's
338 set point and the headroom */
339 uint8_t check;
340
341 /* The transaction under way. */
342 enum { PD_IDLE, PD_ATTACH, PD_WAIT, PD_GAP } phase;
343 uint8_t req[8];
344 size_t req_n;
345 uint8_t cmd;
346 bool write;
347 uint8_t rx[72];
348 size_t rx_n;
349 uint32_t t;
350 uint32_t deadline;
351
352 uint32_t last_display, last_state, last_input, last_identify, last_slot;
353} pdmini_t;
354
362void pdmini_init(pdmini_t *d, const pdmini_io_t *io, uint32_t now_ms);
363
375
386
394
409void pdmini_want(pdmini_t *d, bool output, uint16_t set_mv, uint16_t set_ma);
410
420
428void pdmini_reset(pdmini_t *d);
429
437
445void pdmini_rx(pdmini_t *d, uint8_t byte, uint32_t now_ms);
446
455void pdmini_step(pdmini_t *d, uint32_t now_ms);
456
463const pdmini_status_t *pdmini_status(const pdmini_t *d);
464
476bool pdmini_may_be_on(const pdmini_t *d);
477
489uint16_t pdmini_cap_mv(uint32_t vin_mv);
490
491#ifdef __cplusplus
492}
493#endif
pdmini_check_t
What a state read asked for with pdmini_check_off() has shown.
Definition pdmini.h:365
@ PDMINI_CHECK_NOT_OFF
Definition pdmini.h:371
@ PDMINI_CHECK_ASKED
Definition pdmini.h:367
@ PDMINI_CHECK_OFF
Definition pdmini.h:369
@ PDMINI_CHECK_NONE
Definition pdmini.h:366
@ PDMINI_CHECK_SENT
Definition pdmini.h:368
bool pdmini_may_be_on(const pdmini_t *d)
Whether the output is on or may be: it reads on, an ON waits to be sent or confirmed or went out unse...
Definition pdmini.c:122
pdmini_frame_t pdmini_reply_frame(uint8_t cmd, const uint8_t *buf, size_t n)
Checks the bytes received so far against a reply to cmd.
Definition pdmini_codec.c:52
void pdmini_want_off(pdmini_t *d)
The output off, and no set points asked: the module's are left as they are until pdmini_want() names ...
Definition pdmini.c:90
pdmini_frame_t
Whether buf (n bytes received) holds a whole reply to cmd.
Definition pdmini.h:244
@ PDMINI_MORE
Definition pdmini.h:245
@ PDMINI_BAD
Definition pdmini.h:247
@ PDMINI_DONE
Definition pdmini.h:246
uint8_t pdmini_crc8(const uint8_t *p, size_t n)
CRC8, polynomial 0x31, initial 0xFF, over n bytes.
Definition pdmini_codec.c:13
void pdmini_check_off(pdmini_t *d)
A state read sent from now on, ahead of the other readings: the answer is in pdmini_checked().
Definition pdmini.c:105
void pdmini_step(pdmini_t *d, uint32_t now_ms)
Run one pass of the driver.
Definition pdmini_jobs.c:440
void pdmini_init(pdmini_t *d, const pdmini_io_t *io, uint32_t now_ms)
Set up a driver with nothing asked and nothing known.
Definition pdmini.c:15
#define PDMINI_CAP_MV
A voltage set point is kept this far under the input the module reports, rounded down to 10mV,...
Definition pdmini.h:162
void pdmini_restored(pdmini_t *d)
Attached to a module whose state is not known – wiring restored after a restart, or newly given: it m...
Definition pdmini.c:76
void pdmini_rx(pdmini_t *d, uint8_t byte, uint32_t now_ms)
A byte from the module.
Definition pdmini.c:531
uint16_t pdmini_cap_mv(uint32_t vin_mv)
The highest voltage set point written for an input of vin_mv, as read.
Definition pdmini.c:288
pdmini_check_t pdmini_checked(const pdmini_t *d)
The answer to the last pdmini_check_off().
Definition pdmini.c:112
@ PDMINI_READ_STATE
Definition pdmini.h:186
@ PDMINI_READ_DATA
Definition pdmini.h:188
@ PDMINI_READ_ID
Definition pdmini.h:187
@ PDMINI_WHO_AM_I
Definition pdmini.h:185
@ PDMINI_READ_INPUT
Definition pdmini.h:190
@ PDMINI_READ_DISPLAY
Definition pdmini.h:189
@ PDMINI_SYSTEM_RESET
Definition pdmini.h:184
@ PDMINI_OUTPUT_EN
Definition pdmini.h:182
@ PDMINI_OUTPUT_DATA
Definition pdmini.h:183
const pdmini_status_t * pdmini_status(const pdmini_t *d)
The driver's status, read back from the module.
Definition pdmini.c:117
void pdmini_reset(pdmini_t *d)
Restart the module with SYSTEM_RESET: sent once it has answered, its output reads off and nothing els...
Definition pdmini.c:83
#define PDMINI_HEADROOM_MV
The margin between the input the module reports and a live output's set point that the buck is taken ...
Definition pdmini.h:150
void pdmini_want(pdmini_t *d, bool output, uint16_t set_mv, uint16_t set_ma)
What the output is to be.
Definition pdmini.c:47
What the driver needs from the hardware.
Definition pdmini.h:261
void(*) attach(void *ctx)
Definition pdmini.h:262
void(*) send(void *ctx, const uint8_t *p, size_t n)
Definition pdmini.h:264
void(*) detach(void *ctx)
Definition pdmini.h:263
void * ctx
Definition pdmini.h:265
What the module has said, read back rather than assumed.
Definition pdmini.h:269
uint32_t errors
Definition pdmini.h:285
uint16_t i_ma
Definition pdmini.h:274
bool online
Definition pdmini.h:270
uint16_t set_mv
Definition pdmini.h:275
bool tripped
Definition pdmini.h:281
uint16_t v_mv
Definition pdmini.h:273
bool sagged
Definition pdmini.h:282
uint16_t vin_mv
Definition pdmini.h:278
uint8_t mode
Definition pdmini.h:272
uint16_t set_ma
Definition pdmini.h:276
uint32_t samples
Definition pdmini.h:284
uint8_t in_state
Definition pdmini.h:277
bool set_stuck
Definition pdmini.h:280
bool output
Definition pdmini.h:271
bool stuck
Definition pdmini.h:279
Driver state: what is asked, what is known, and the transaction under way.
Definition pdmini.h:290
bool want_set
Definition pdmini.h:296
bool write
Definition pdmini.h:346
uint8_t check
Definition pdmini.h:339
uint8_t data_tries
Definition pdmini.h:325
bool blind_due
Definition pdmini.h:320
uint8_t on_value
Definition pdmini.h:310
uint8_t who_misses
Definition pdmini.h:303
uint8_t input_misses
Definition pdmini.h:329
bool input_seen
Definition pdmini.h:331
uint8_t state_fails
Definition pdmini.h:328
bool on_sent
Definition pdmini.h:315
bool on_seen
Definition pdmini.h:313
bool state_known
Definition pdmini.h:307
uint8_t en_value
Definition pdmini.h:311
int slot
Definition pdmini.h:308
bool data_known
Definition pdmini.h:309
bool data_pending
Definition pdmini.h:324
bool en_for
Definition pdmini.h:312
bool off_owed
Definition pdmini.h:319
bool en_pending
Definition pdmini.h:322
uint32_t t
Definition pdmini.h:349
uint8_t en_tries
Definition pdmini.h:321
bool restored
Definition pdmini.h:302
bool input_known
Definition pdmini.h:330
uint8_t fails
Definition pdmini.h:327
bool answered
Definition pdmini.h:301
bool on_confirmed
Definition pdmini.h:314
bool reset_owed
Definition pdmini.h:316