rcbench
Servo and ESC test bench: panel, IOmcu and shared C
Loading...
Searching...
No Matches
sense_map.c File Reference

The SENSE_MAP, SENSE_WIN_A and SENSE_WIN_B link pages. More...

#include "sense_map.h"
#include <stddef.h>
#include <string.h>
#include "ina3221.h"
#include "link_msg.h"
#include "link_pages.h"
#include "sense_page_int.h"

Functions

static bool fitted (const uint16_t *m, unsigned a)
 Whether INA3221 part a is fitted: its bit in ENABLE is set.
static void derive (const sense_page_t *p, uint16_t *m)
 The map SENSE's one part makes: what a read shows and a first write starts from.
static const uint16_t * kept_regs (const sense_page_t *p, uint16_t *buf)
 The kept registers in force, or derived into buf.
static bool watch_ok (uint16_t w)
 Whether a WATCH value is one the map accepts.
bool sense_map_on_addr (const uint16_t *map, uint16_t addr)
 Whether the map map fits an INA3221 at addr.
static bool map_ok (const uint16_t *m, const uint16_t *sense)
 The map's own rules, whatever the bus.
static void mirror (const uint16_t *m, const uint16_t *sense, uint16_t *next)
 SENSE's set-up with the map mirrored into ENABLE bit 1 and registers 8 to 10.
static bool bus_differs (const uint16_t *a, const uint16_t *b)
 Whether two maps differ in what core 1 reads: ENABLE, FAST, a shunt.
void sense_map_read (const sense_page_t *p, uint8_t off, uint8_t n, uint16_t *out)
 A SENSE_MAP read.
uint8_t sense_map_write (sense_page_t *p, uint8_t off, uint8_t n, const uint16_t *in, const outputs_t *o, uint64_t taken)
 A SENSE_MAP write, merged into the map in force (or the derived one) and the whole map judged before any of it is stored.
void sense_xwin_read (const sense_page_t *p, unsigned page, uint8_t off, uint8_t n, uint16_t *out)
 A SENSE_WIN_A (page 0) or SENSE_WIN_B (1) read; no write is taken.
bool sense_map_watches (const sense_page_t *p)
 Whether a channel the map reads watches an output channel or a programming port: the map in force's LINK_CAP_SERVO_SENSE.
void sense_map_kept (const sense_page_t *p, uint16_t *kept, uint16_t *set)
 What is kept: registers 0 to 25 (LINK_SM_KEPT) into kept, and 1 into set while the map is in force, else 0 (the registers then the derived ones).
uint8_t sense_map_restore (sense_page_t *p, const uint16_t *kept, uint16_t set, const outputs_t *o, uint64_t taken)
 A kept map back in force at boot, through sense_map_write()'s checks, after SENSE's restore: nothing when set is 0.
void sense_map_cfg (const uint16_t *map, sense_i3221_cfg_t *out)
 The map's kept registers as the schedule's INA3221 set-up.
static uint16_t entry_flags (const sense_window_t *w)
 The flags of one SENSE_WIN entry: which samples it holds and which clip.
void sense_map_publish (sense_page_t *p, const sense_snap_t *s)
 The snapshot's per-part state into SENSE_MAP's read-only registers, and every channel's window into SENSE_WIN_A and SENSE_WIN_B.

Detailed Description

The SENSE_MAP, SENSE_WIN_A and SENSE_WIN_B link pages.

See sense_map.h.

SPDX-License-Identifier: MIT

Function Documentation

◆ bus_differs()

bool bus_differs ( const uint16_t * a,
const uint16_t * b )
static

Whether two maps differ in what core 1 reads: ENABLE, FAST, a shunt.

Parameters
aThe first map.
bThe second map.
Returns
true when the first LINK_SM_WATCH_0 registers differ, false when they are equal.

◆ derive()

void derive ( const sense_page_t * p,
uint16_t * m )
static

The map SENSE's one part makes: what a read shows and a first write starts from.

Parameters
pThe SENSE page state. Its set-up registers are read.
mReceives LINK_SM_KEPT registers. An address outside the INA3221 range leaves them at the defaults of FAST and zero for the rest.

◆ entry_flags()

uint16_t entry_flags ( const sense_window_t * w)
static

The flags of one SENSE_WIN entry: which samples it holds and which clip.

Parameters
wThe channel's window.
Returns
LINK_SX_E_CURRENT when n_i is above 0, LINK_SX_E_VOLTAGE when n_v is above 0, and LINK_SX_E_CLIP_HI or LINK_SX_E_CLIP_LO when the window clipped high or low.

◆ fitted()

bool fitted ( const uint16_t * m,
unsigned a )
static

Whether INA3221 part a is fitted: its bit in ENABLE is set.

Parameters
mThe map registers.
aPart index, 0 to LINK_SM_PARTS - 1.
Returns
true when the bit is set, false otherwise.

◆ kept_regs()

const uint16_t * kept_regs ( const sense_page_t * p,
uint16_t * buf )
static

The kept registers in force, or derived into buf.

Parameters
pThe SENSE page state.
bufReceives the derived map when the map is not in force.
Returns
p->map when the map is in force, otherwise buf.

◆ map_ok()

bool map_ok ( const uint16_t * m,
const uint16_t * sense )
static

The map's own rules, whatever the bus.

Parameters
mThe map to judge, LINK_SM_KEPT registers.
senseThe SENSE page's set-up registers, read for the INA228 check.
Returns
true when the map passes, false when any rule is broken.

◆ mirror()

void mirror ( const uint16_t * m,
const uint16_t * sense,
uint16_t * next )
static

SENSE's set-up with the map mirrored into ENABLE bit 1 and registers 8 to 10.

With no part fitted only the bit clears.

Parameters
mThe map in force.
senseThe SENSE page's set-up registers.
nextReceives the mirrored set-up, LINK_SN_CONFIG_COUNT registers.

◆ sense_map_cfg()

void sense_map_cfg ( const uint16_t * map,
sense_i3221_cfg_t * out )

The map's kept registers as the schedule's INA3221 set-up.

Parameters
mapThe map's registers.
outReceives the INA3221 set-up of the schedule.

◆ sense_map_kept()

void sense_map_kept ( const sense_page_t * p,
uint16_t * kept,
uint16_t * set )

What is kept: registers 0 to 25 (LINK_SM_KEPT) into kept, and 1 into set while the map is in force, else 0 (the registers then the derived ones).

Nothing is done when any pointer is NULL.

Parameters
pThe SENSE page state.
keptReceives LINK_SM_KEPT registers.
setReceives 1 while the map is in force, 0 otherwise.

◆ sense_map_on_addr()

bool sense_map_on_addr ( const uint16_t * map,
uint16_t addr )

Whether the map map fits an INA3221 at addr.

Parameters
mapThe map's registers.
addrThe INA3221 address.
Returns
true when the map fits an INA3221 at addr.

◆ sense_map_publish()

void sense_map_publish ( sense_page_t * p,
const sense_snap_t * s )

The snapshot's per-part state into SENSE_MAP's read-only registers, and every channel's window into SENSE_WIN_A and SENSE_WIN_B.

Parameters
pThe page, updated in place.
sThe snapshot.

◆ sense_map_read()

void sense_map_read ( const sense_page_t * p,
uint8_t off,
uint8_t n,
uint16_t * out )

A SENSE_MAP read.

Registers 0 to 25 are the map in force, or the one derived from SENSE; 26 to 30 what core 1 last read; 31 reads 0.

Nothing is done when p or out is NULL, or when off and n reach past LINK_SM_COUNT.

Parameters
pThe SENSE page state.
offFirst register, 0 to LINK_SM_COUNT - 1.
nNumber of registers to read.
outReceives the n register values.

◆ sense_map_restore()

uint8_t sense_map_restore ( sense_page_t * p,
const uint16_t * kept,
uint16_t set,
const outputs_t * o,
uint64_t taken )

A kept map back in force at boot, through sense_map_write()'s checks, after SENSE's restore: nothing when set is 0.

0, or the refusal, which leaves the map derived.

Parameters
pThe SENSE page state.
keptThe kept registers, LINK_SM_KEPT of them.
set1 when the kept map was in force, 0 for nothing to restore.
oThe outputs bank, as sense_map_write() takes it.
takenPassed to sense_map_write() unchanged.
Returns
0 when set is 0 or the map is taken. Otherwise the refusal of sense_map_write().

◆ sense_map_watches()

bool sense_map_watches ( const sense_page_t * p)

Whether a channel the map reads watches an output channel or a programming port: the map in force's LINK_CAP_SERVO_SENSE.

Parameters
pThe page.
Returns
true when a channel the map reads watches an output channel or a programming port.

◆ sense_map_write()

uint8_t sense_map_write ( sense_page_t * p,
uint8_t off,
uint8_t n,
const uint16_t * in,
const outputs_t * o,
uint64_t taken )

A SENSE_MAP write, merged into the map in force (or the derived one) and the whole map judged before any of it is stored.

Refused: off the page (BAD_RANGE); a register from 26 on (READ_ONLY); ENABLE above 15, FAST not 0xFF and past 11, on a part not fitted or on a channel whose shunt is 0, a shunt not 0 outside 50 to 10000, a WATCH value outside the list, a fitted part with no channel, a fitted part on the INA228's address while SENSE enables it, the mirrored SENSE set-up refused as a SENSE write would be (its pins, while the map opens the bus), and any change while o drives (BAD_VALUE). A map taken is in force from then on; a change of ENABLE, FAST or a shunt is a new set-up (the generation moves, the readings go), a change of WATCH alone is not.

Parameters
pThe SENSE page state, changed only when the write is taken.
offFirst register, 0 to LINK_SM_COUNT - 1.
nNumber of registers to write. 0 is taken with no change.
inThe n new register values.
oThe outputs bank, checked for a change while it drives.
takenPassed to sense_cfg_check() unchanged.
Returns
0 when taken, or when the write changes nothing. LINK_NACK_BAD_RANGE for a NULL pointer or a range past LINK_SM_COUNT. LINK_NACK_READ_ONLY when the range reaches register 26 or above. LINK_NACK_BAD_VALUE for a change while o drives, or a map that fails the rules above. The refusal of sense_cfg_check() when the mirrored SENSE set-up is refused.

◆ sense_xwin_read()

void sense_xwin_read ( const sense_page_t * p,
unsigned page,
uint8_t off,
uint8_t n,
uint16_t * out )

A SENSE_WIN_A (page 0) or SENSE_WIN_B (1) read; no write is taken.

Nothing is done when p or out is NULL, page is above 1, or off and n reach past LINK_SX_COUNT.

Parameters
pThe SENSE page state.
page0 for SENSE_WIN_A, 1 for SENSE_WIN_B.
offFirst register, 0 to LINK_SX_COUNT - 1.
nNumber of registers to read.
outReceives the n register values.

◆ watch_ok()

bool watch_ok ( uint16_t w)
static

Whether a WATCH value is one the map accepts.

Accepted: none, an output channel from OUT(0) to OUT(7), a programming port from PORT(0) below LINK_PG_PORTS_MAX, and the supply.

Parameters
wThe WATCH register value.
Returns
true for an accepted value, false otherwise.