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

The sensor bus's sampling schedule. More...

#include "sense_sched_int.h"
#include <string.h>

Enumerations

enum  rot_t {
  ROT_NONE = 0 , ROT_TEMP , ROT_DIAG , ROT_ENERGY ,
  ROT_CHARGE , ROT_ADC , ROT_FLAGS0 , ROT_CONFIG0 = ROT_FLAGS0 + SENSE_I3221_PARTS
}
 What a rotation item reads. More...

Functions

static void acc_clear (sense_acc_t *a)
 Clear a window accumulator.
static void acc_current (sense_acc_t *a, sense_value_t v)
 Add one current sample to a window accumulator.
static void acc_voltage (sense_acc_t *a, int32_t uv)
 Add one bus voltage sample to a window accumulator.
static void acc_close (const sense_acc_t *a, uint64_t number, sense_window_t *w)
 Close a window: write its totals from an accumulator.
static void ring_close (int32_t top, int32_t bottom, const sense_acc_t *a, sense_ring_win_t *e)
 FAST's window into a ring entry: every sample counted, a clipped one as the end of the range it read. top and bottom are the ends of FAST's range.
static void ring_push (sense_sched_t *s, uint64_t number, const sense_acc_t *a)
 Window number has closed with FAST's a, empty with no FAST: the ring moves on by as many places as numbers have passed, and the places of skipped numbers read not closed.
static void init_part (sense_sched_t *s, unsigned a)
 Part a's set-up from the map: its channels with a shunt.
static bool ch_read (const sense_sched_t *s, unsigned c)
 Whether channel c is read: its part set up and the channel on.
void sense_sched_init (sense_sched_t *s, const sense_sched_io_t *io, const sense_sched_cfg_t *cfg)
 A schedule over io for the parts cfg enables.
const ina3221_t * sense_sched_fast_part (const sense_sched_t *s)
 FAST's INA3221, or NULL with no FAST channel.
void sense_sched_arm (sense_sched_t *s)
 The edge into driving: the run's peaks start again and the INA228's totals are cleared on the next tick.
bool sense_sched_window (const sense_sched_t *s, unsigned src, sense_window_t *out)
 The last complete window of source src (a channel, or SENSE_SRC_INA228); false before one has closed.
bool sense_sched_ring (const sense_sched_t *s, unsigned back, sense_ring_win_t *out)
 FAST's window numbered back before the newest complete one, back under SENSE_WIN_RING; false, and out not closed, for a number that was skipped, lies before the first window, or before one has closed.
static bool i228_ok (sense_sched_t *s, sense_err_t e)
 An INA228 transaction's result: the first failure after the clear ends the run's totals.
static void roll_windows (sense_sched_t *s, uint64_t now_ms)
 The windows: the one being filled closes when the clock passes its end, and the next one starts empty.
static uint64_t stamp (sense_sched_t *s)
 A read is done: its time, µs, with the windows rolled to it, so what it read goes into the window it was read in.
static bool read_ch_current (sense_sched_t *s, unsigned c, sense_value_t *v, uint64_t *at_us)
 Channel c's current into its window; true with a sample. at_us, when given, is when the read was done, sample or not.
static void read_ch_bus (sense_sched_t *s, unsigned c)
 Read channel c's bus voltage into its window.
static void read_other (sense_sched_t *s)
 The next channel beside FAST, in turn.
static void read_volt (sense_sched_t *s)
 The next channel's bus voltage: every channel in turn, one that is not read leaving its turn empty, so each is read every SENSE_CHANNELS even ticks.
static void read_i228_current (sense_sched_t *s)
 Read the INA228's current into its window and the run's peaks.
static void read_i228_vbus (sense_sched_t *s)
 Read the INA228's bus voltage into its window and the run's minimum.
static void read_i228 (sense_sched_t *s, uint32_t n)
 The INA228's slot n: CURRENT on even ones, VBUS on odd ones.
static void verify_i228 (sense_sched_t *s)
 The INA228's ADC_CONFIG read back.
static void verify_i3221 (sense_sched_t *s, unsigned a)
 Part a's Configuration read back.
static void read_flags (sense_sched_t *s, unsigned a)
 Read part a's Mask/Enable flags; kept when the read succeeds.
static void read_rotation (sense_sched_t *s)
 The next rotation item.
uint16_t sense_sched_enc_still_ms (const sense_sched_t *s)
 How long the encoder's angle has stayed within SENSE_ENC_STILL_TOL counts of its anchor, ms, saturating at 65535; 0 with no anchor.
static void enc_anchor (sense_enc_t *e, uint16_t raw, uint64_t at_ms)
 An angle sample at at_ms: the anchor moves when the angle has left its tolerance.
static void read_enc (sense_sched_t *s)
 The encoder's slot: STATUS and RAW ANGLE, and every SENSE_ENC_MAG_EVERY-th time the field's AGC and MAGNITUDE as well, in place of STATUS.
static void totals (sense_sched_t *s)
 The totals: cleared once the arm owes it.
void sense_sched_tick (sense_sched_t *s)
 One tick: every 1ms.

Variables

static const uint8_t k_rotation [SENSE_ROTATION]
 The INA228's eight slots where they always were; each INA3221's Mask/Enable and Configuration 20ms apart, part 0's in the slots one part had before; four slots empty.

Detailed Description

The sensor bus's sampling schedule.

See sense_sched.h.

SPDX-License-Identifier: MIT

Enumeration Type Documentation

◆ rot_t

enum rot_t

What a rotation item reads.

An INA3221 item names its part.

Function Documentation

◆ acc_clear()

void acc_clear ( sense_acc_t * a)
static

Clear a window accumulator.

Parameters
aThe accumulator.

◆ acc_close()

void acc_close ( const sense_acc_t * a,
uint64_t number,
sense_window_t * w )
static

Close a window: write its totals from an accumulator.

Parameters
aThe accumulator.
numberNumber of the window.
wReceives the window.

◆ acc_current()

void acc_current ( sense_acc_t * a,
sense_value_t v )
static

Add one current sample to a window accumulator.

Parameters
aThe accumulator.
vThe sample.

◆ acc_voltage()

void acc_voltage ( sense_acc_t * a,
int32_t uv )
static

Add one bus voltage sample to a window accumulator.

Parameters
aThe accumulator.
uvVoltage, in µV.

◆ ch_read()

bool ch_read ( const sense_sched_t * s,
unsigned c )
static

Whether channel c is read: its part set up and the channel on.

Parameters
sThe schedule.
cChannel index, 0 to 11.
Returns
true when the channel is read.

◆ enc_anchor()

void enc_anchor ( sense_enc_t * e,
uint16_t raw,
uint64_t at_ms )
static

An angle sample at at_ms: the anchor moves when the angle has left its tolerance.

Parameters
eThe encoder state.
rawThe angle, 0 to 4095 counts.
at_msThe time of the sample, in ms.

◆ i228_ok()

bool i228_ok ( sense_sched_t * s,
sense_err_t e )
static

An INA228 transaction's result: the first failure after the clear ends the run's totals.

Parameters
sThe schedule.
eThe result of the transaction.
Returns
true when the transaction succeeded.

◆ init_part()

void init_part ( sense_sched_t * s,
unsigned a )
static

Part a's set-up from the map: its channels with a shunt.

Parameters
sThe schedule.
aPart index, 0 to 3.

◆ read_ch_bus()

void read_ch_bus ( sense_sched_t * s,
unsigned c )
static

Read channel c's bus voltage into its window.

Parameters
sThe schedule.
cChannel index, 0 to 11.

◆ read_ch_current()

bool read_ch_current ( sense_sched_t * s,
unsigned c,
sense_value_t * v,
uint64_t * at_us )
static

Channel c's current into its window; true with a sample. at_us, when given, is when the read was done, sample or not.

Parameters
sThe schedule.
cChannel index, 0 to 11.
vReceives the current.
at_usReceives the time of the read, in µs; may be NULL.
Returns
true with a sample.

◆ read_enc()

void read_enc ( sense_sched_t * s)
static

The encoder's slot: STATUS and RAW ANGLE, and every SENSE_ENC_MAG_EVERY-th time the field's AGC and MAGNITUDE as well, in place of STATUS.

The angle is read in every slot, 500Hz. Each register is a transaction of its own (as5600.h).

Parameters
sThe schedule.

◆ read_flags()

void read_flags ( sense_sched_t * s,
unsigned a )
static

Read part a's Mask/Enable flags; kept when the read succeeds.

Parameters
sThe schedule.
aPart index, 0 to 3.

◆ read_i228()

void read_i228 ( sense_sched_t * s,
uint32_t n )
static

The INA228's slot n: CURRENT on even ones, VBUS on odd ones.

Parameters
sThe schedule.
nThe slot number.

◆ read_i228_current()

void read_i228_current ( sense_sched_t * s)
static

Read the INA228's current into its window and the run's peaks.

Parameters
sThe schedule.

◆ read_i228_vbus()

void read_i228_vbus ( sense_sched_t * s)
static

Read the INA228's bus voltage into its window and the run's minimum.

Parameters
sThe schedule.

◆ read_other()

void read_other ( sense_sched_t * s)
static

The next channel beside FAST, in turn.

Parameters
sThe schedule.

◆ read_rotation()

void read_rotation ( sense_sched_t * s)
static

The next rotation item.

Parameters
sThe schedule.

◆ read_volt()

void read_volt ( sense_sched_t * s)
static

The next channel's bus voltage: every channel in turn, one that is not read leaving its turn empty, so each is read every SENSE_CHANNELS even ticks.

Parameters
sThe schedule.

◆ ring_close()

void ring_close ( int32_t top,
int32_t bottom,
const sense_acc_t * a,
sense_ring_win_t * e )
static

FAST's window into a ring entry: every sample counted, a clipped one as the end of the range it read. top and bottom are the ends of FAST's range.

Parameters
topThe current at the top code, in µA.
bottomThe current at the bottom code, in µA.
aThe accumulator.
eReceives the ring entry.

◆ ring_push()

void ring_push ( sense_sched_t * s,
uint64_t number,
const sense_acc_t * a )
static

Window number has closed with FAST's a, empty with no FAST: the ring moves on by as many places as numbers have passed, and the places of skipped numbers read not closed.

Parameters
sThe schedule.
numberNumber of the window.
aFAST's accumulator, or the empty one with no FAST.

◆ roll_windows()

void roll_windows ( sense_sched_t * s,
uint64_t now_ms )
static

The windows: the one being filled closes when the clock passes its end, and the next one starts empty.

In 64 bits, so the numbering runs on past the 32-bit millisecond count's wrap at 49.7 days.

Parameters
sThe schedule.
now_msThe current time, in ms.

◆ sense_sched_arm()

void sense_sched_arm ( sense_sched_t * s)

The edge into driving: the run's peaks start again and the INA228's totals are cleared on the next tick.

Parameters
sThe schedule.

◆ sense_sched_enc_still_ms()

uint16_t sense_sched_enc_still_ms ( const sense_sched_t * s)

How long the encoder's angle has stayed within SENSE_ENC_STILL_TOL counts of its anchor, ms, saturating at 65535; 0 with no anchor.

Parameters
sThe schedule.
Returns
Time since the encoder angle was last anchored, in ms; 0 before an anchor, 65535 at most.

◆ sense_sched_fast_part()

const ina3221_t * sense_sched_fast_part ( const sense_sched_t * s)

FAST's INA3221, or NULL with no FAST channel.

Parameters
sThe schedule.
Returns
FAST's INA3221, or NULL with no FAST channel.

◆ sense_sched_init()

void sense_sched_init ( sense_sched_t * s,
const sense_sched_io_t * io,
const sense_sched_cfg_t * cfg )

A schedule over io for the parts cfg enables.

A part whose set-up the driver refuses is left unset, never addressed, and its error kept in i228_setup or i3221_setup[a]; a FAST that names a channel off is taken as none. The bus is the caller's to open at 400kHz.

Parameters
sThe schedule to set up.
ioThe callbacks the schedule calls.
cfgThe parts to enable.

◆ sense_sched_ring()

bool sense_sched_ring ( const sense_sched_t * s,
unsigned back,
sense_ring_win_t * out )

FAST's window numbered back before the newest complete one, back under SENSE_WIN_RING; false, and out not closed, for a number that was skipped, lies before the first window, or before one has closed.

Parameters
sThe schedule.
backHow many windows before the newest complete one.
outReceives the window.
Returns
true with a window; false for a number that was skipped, lies before the first window, or before one has closed.

◆ sense_sched_tick()

void sense_sched_tick ( sense_sched_t * s)

One tick: every 1ms.

Parameters
sThe schedule.

◆ sense_sched_window()

bool sense_sched_window ( const sense_sched_t * s,
unsigned src,
sense_window_t * out )

The last complete window of source src (a channel, or SENSE_SRC_INA228); false before one has closed.

Parameters
sThe schedule.
srcThe source: a channel, or SENSE_SRC_INA228.
outReceives the window.
Returns
true with a window; false before one has closed.

◆ stamp()

uint64_t stamp ( sense_sched_t * s)
static

A read is done: its time, µs, with the windows rolled to it, so what it read goes into the window it was read in.

Parameters
sThe schedule.
Returns
The time of the read, in µs.

◆ totals()

void totals ( sense_sched_t * s)
static

The totals: cleared once the arm owes it.

Parameters
sThe schedule.

◆ verify_i228()

void verify_i228 ( sense_sched_t * s)
static

The INA228's ADC_CONFIG read back.

A part found reset is offline from here. The read-back is stamped as any read: the windows are rolled to the time it was done, and the one being filled then is emptied – it holds samples at the reset range. The voltage of the last slot pairs with no current, and the totals are no longer the run's.

Parameters
sThe schedule.

◆ verify_i3221()

void verify_i3221 ( sense_sched_t * s,
unsigned a )
static

Part a's Configuration read back.

A part found reset is offline from here, which ends a capture on it as lost (sense_cap_step()); the windows being filled from it at the time the read-back was done are emptied.

Parameters
sThe schedule.
aPart index, 0 to 3.

Variable Documentation

◆ k_rotation

const uint8_t k_rotation[SENSE_ROTATION]
static
Initial value:
= {
ROT_FLAGS0 + 1, ROT_TEMP, ROT_NONE, ROT_DIAG,
ROT_FLAGS0 + 2, ROT_ENERGY, ROT_FLAGS0 + 3, ROT_CHARGE,
ROT_NONE, ROT_FLAGS0,
ROT_CONFIG0 + 1, ROT_ADC, ROT_NONE, ROT_DIAG,
ROT_CONFIG0 + 2, ROT_ENERGY, ROT_CONFIG0 + 3, ROT_CHARGE,
ROT_NONE, ROT_CONFIG0,
}

The INA228's eight slots where they always were; each INA3221's Mask/Enable and Configuration 20ms apart, part 0's in the slots one part had before; four slots empty.