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

The KST wire on one RP2350 pin: a kst_driver_t made of one PIO (programmable input/output) state machine (kst_line.pio) and two DMA (direct memory access) channels, one for the words of a frame and one for the stamps of its reply window. More...

#include <stdbool.h>
#include <stdint.h>
#include "kst_pio.h"
#include "kst_session.h"

Go to the source code of this file.

Data Structures

struct  kst_line_t
 One line. More...

Macros

#define KST_LINE_GRACE_US   1000u
 The driver ends a transaction whose state machine still runs this long after the end of the window.

Enumerations

enum  kst_line_err_t {
  KST_LINE_OK = 0 , KST_LINE_ERR_ARG , KST_LINE_ERR_CLOCK , KST_LINE_ERR_PIO ,
  KST_LINE_ERR_DMA
}
 Error codes of the KST line driver. More...

Functions

void kst_line_init (kst_line_t *line)
 Mark line closed.
kst_line_err_t kst_line_open (kst_line_t *line, unsigned pin)
 Claim the resources and take pin: from the return on the pad drives low.
void kst_line_close (kst_line_t *line)
 Stop the state machine, give back what the open claimed and leave the pad an input with its pull-down on.
bool kst_line_is_open (const kst_line_t *line)
 Check whether a line is open.
bool kst_line_busy (const kst_line_t *line)
 A transaction runs: between a start() that returned true and the poll() that returns true.
uint32_t kst_line_half_cell_ns (const kst_line_t *line)
 The transmit half-cell the system clock gives, in ns; 0 on a closed line.
bool kst_line_start (void *ctx, const kst_frame_t *frame, uint32_t window_ns)
 The three functions of kst_driver_t; ctx is the kst_line_t.
bool kst_line_poll (void *ctx, kst_capture_t *out)
 Poll a transaction; the second of the three driver functions.
uint32_t kst_line_now_us (void *ctx)
 Read the microsecond clock; the third of the three driver functions.
kst_driver_t kst_line_driver (kst_line_t *line)
 The driver for a session on line.

Detailed Description

The KST wire on one RP2350 pin: a kst_driver_t made of one PIO (programmable input/output) state machine (kst_line.pio) and two DMA (direct memory access) channels, one for the words of a frame and one for the stamps of its reply window.

Implements the programming protocol of KST and Chaservo servos. KST and Chaservo are names or trademarks of their respective owners; they are used here only to say which servos this module works with (README.md, "Legal notice and disclaimer").

Resources. kst_line_open() claims 1 state machine, 24 words of program space in a PIO block that reaches the pin, and 2 DMA channels; kst_line_close() gives all of them back. A second line on a second pin claims its own and shares the program when its state machine is in the same block. No interrupt and no PIO interrupt flag is used.

Clock. The counts are computed from clock_get_hz(clk_sys) at the open. The open is refused when the half-cell that clock gives is more than 0.2% from 25.40us. At 150MHz the half-cell is 3810 cycles and a stamp resolves 26.7ns. The system clock must not change while the line is open.

Pad. While the line is open and no transaction runs, the pad drives low. In a transaction it is push-pull for the frame, driven low for 50us after the frame's last edge, then an input. It drives low again 165us after the last rising edge of a reply, or at the end of the window when no edge came. The pad's pull-down is on and its pull-up off from the open on, and the close leaves the pad an input with the pull-down on. A pull-down on the board is expected: the internal one is 50 to 80kOhm. A level change before the reply starts the 165us early, so the pad may drive low into a reply; the capture then holds the edges up to that point and the decoder refuses it.

Every function is called from one context. kst_line_poll() ends a transaction whose state machine still runs 1ms after the window: the state machine is restarted with the pad driven low and the capture is marked as overflowed.

Not measured: no frame of this driver has been on a pin.

SPDX-License-Identifier: MIT

Enumeration Type Documentation

◆ kst_line_err_t

Error codes of the KST line driver.

Enumerator
KST_LINE_OK 

no error

KST_LINE_ERR_ARG 

no line object, or a pin the chip does not have

KST_LINE_ERR_CLOCK 

the system clock gives no 25.40us half-cell

KST_LINE_ERR_PIO 

no state machine or no program space

KST_LINE_ERR_DMA 

fewer than 2 free DMA channels

Function Documentation

◆ kst_line_busy()

bool kst_line_busy ( const kst_line_t * line)

A transaction runs: between a start() that returned true and the poll() that returns true.

Parameters
lineLine object.
Returns
true between a start that returned true and the poll that returns true.

◆ kst_line_close()

void kst_line_close ( kst_line_t * line)

Stop the state machine, give back what the open claimed and leave the pad an input with its pull-down on.

A transaction on the wire is cut. Safe on a closed line.

Parameters
lineLine object.

◆ kst_line_driver()

kst_driver_t kst_line_driver ( kst_line_t * line)

The driver for a session on line.

Parameters
lineLine object.
Returns
The driver for a session on the line.

◆ kst_line_half_cell_ns()

uint32_t kst_line_half_cell_ns ( const kst_line_t * line)

The transmit half-cell the system clock gives, in ns; 0 on a closed line.

Parameters
lineLine object.
Returns
The transmit half-cell in ns; 0 on a closed line.

◆ kst_line_init()

void kst_line_init ( kst_line_t * line)

Mark line closed.

Call once, before the first open.

Parameters
lineLine object to mark closed.

◆ kst_line_is_open()

bool kst_line_is_open ( const kst_line_t * line)

Check whether a line is open.

Parameters
lineLine object.
Returns
true when the line is open.

◆ kst_line_now_us()

uint32_t kst_line_now_us ( void * ctx)

Read the microsecond clock; the third of the three driver functions.

Parameters
ctxThe kst_line_t.
Returns
The microsecond clock.

◆ kst_line_open()

kst_line_err_t kst_line_open ( kst_line_t * line,
unsigned pin )

Claim the resources and take pin: from the return on the pad drives low.

The caller has stopped every other use of the pin. On an error nothing stays claimed and the pin is not touched. An open line is closed first.

Parameters
lineLine object.
pinGPIO number.
Returns
KST_LINE_OK, or the error that refused the open.

◆ kst_line_poll()

bool kst_line_poll ( void * ctx,
kst_capture_t * out )

Poll a transaction; the second of the three driver functions.

Parameters
ctxThe kst_line_t.
outReceives the capture when the transaction has ended.
Returns
true once, when the transaction has ended.

◆ kst_line_start()

bool kst_line_start ( void * ctx,
const kst_frame_t * frame,
uint32_t window_ns )

The three functions of kst_driver_t; ctx is the kst_line_t.

Parameters
ctxThe kst_line_t.
frameFrame to send.
window_nsCapture end, in ns after the last edge.
Returns
true when the transaction started.