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

Cycle counts and divider of the PIO pin driver for the KST wire. More...

#include "kst_pio.h"
#include <stddef.h>
#include <string.h>

Functions

static uint32_t cycles_of (uint32_t hz, uint32_t ns)
 ns as cycles of hz, rounded to nearest.
static uint32_t ns_of (uint32_t hz, uint64_t cycles)
 cycles of hz as ns, rounded to nearest.
bool kst_pio_clock (uint32_t sys_hz, kst_pio_clock_t *out)
 The counts for a system clock of sys_hz.
uint32_t kst_pio_half_cell_ns (const kst_pio_clock_t *clock)
 The transmit half-cell of clock in ns, rounded to nearest.
uint32_t kst_pio_tick_ns (const kst_pio_clock_t *clock)
 One tick of clock in ns, rounded up.
static uint32_t first_sample_cycles (const kst_pio_clock_t *clock)
 Cycles from the last edge to the first sample of the pin, as the pin was when the sample was taken.
unsigned kst_pio_tx (const kst_pio_clock_t *clock, const kst_frame_t *frame, uint32_t window_ns, uint32_t *words, kst_pio_run_t *run)
 The word stream for frame and a capture until window_ns after its last edge.
void kst_pio_capture (const kst_pio_clock_t *clock, const kst_pio_run_t *run, const uint32_t *stamps, unsigned n, bool lost, kst_capture_t *out)
 The capture from the n stamps of a transaction, in the order the program gave them.

Detailed Description

Cycle counts and divider of the PIO pin driver for the KST wire.

SPDX-License-Identifier: MIT

Function Documentation

◆ kst_pio_capture()

void kst_pio_capture ( const kst_pio_clock_t * clock,
const kst_pio_run_t * run,
const uint32_t * stamps,
unsigned n,
bool lost,
kst_capture_t * out )

The capture from the n stamps of a transaction, in the order the program gave them.

lost says the program gave more stamps than were kept. A first stamp equal to the window count is a line that was high at the first sample: it sets level0 and is no edge. More than KST_CAP_MAX_EDGES edges, lost, and a stamp no count of the stream can give set overflow.

Parameters
clockCounts of the clock.
runLayout of the transaction.
stampsStamps the program gave, in order.
nNumber of stamps.
losttrue when the program gave more stamps than were kept.
outReceives the capture.

◆ kst_pio_clock()

bool kst_pio_clock ( uint32_t sys_hz,
kst_pio_clock_t * out )

The counts for a system clock of sys_hz.

False when a tick is longer than KST_PIO_MAX_TICK_NS, which it is below 8 MHz, and when the half-cell the clock gives is more than 0.2% from KST_HALF_CELL_NS. Every clock from 9.9MHz up gives one within 0.2%, and a multiple of 5MHz gives it exactly.

Parameters
sys_hzSystem clock, in Hz.
outReceives the counts; left unchanged when false is returned.
Returns
true when the clock gives usable counts.

◆ kst_pio_half_cell_ns()

uint32_t kst_pio_half_cell_ns ( const kst_pio_clock_t * clock)

The transmit half-cell of clock in ns, rounded to nearest.

Parameters
clockCounts of the clock.
Returns
The transmit half-cell in ns, rounded to nearest.

◆ kst_pio_tick_ns()

uint32_t kst_pio_tick_ns ( const kst_pio_clock_t * clock)

One tick of clock in ns, rounded up.

Parameters
clockCounts of the clock.
Returns
One tick in ns, rounded up.

◆ kst_pio_tx()

unsigned kst_pio_tx ( const kst_pio_clock_t * clock,
const kst_frame_t * frame,
uint32_t window_ns,
uint32_t * words,
kst_pio_run_t * run )

The word stream for frame and a capture until window_ns after its last edge.

Returns the number of words written to words, at most KST_PIO_TX_WORDS, or 0 for a frame without half-cells, one longer than KST_FRAME_MAX_HALF_CELLS, or a window that ends before the release.

Parameters
clockCounts of the clock.
frameFrame to send.
window_nsCapture end, in ns after the last edge.
wordsReceives the word stream.
runReceives the layout of the transaction.
Returns
Number of words written, or 0 for a frame that cannot be sent.