ACwing FC firmware
STM32WB55: attitude IEKF, altitude Kalman filter, flight core, protocol v2
Loading...
Searching...
No Matches
usb_link.c File Reference

USB byte-stream link (see usb_link.h). More...

#include "usb_link.h"
#include "proto_v2.h"
#include "telemetry.h"
#include "spsc.h"
#include <string.h>
Include dependency graph for usb_link.c:

Go to the source code of this file.

Data Structures

struct  ring_t
 

Functions

void usbl_reset (void)
 
void usbl_set_configured (uint8_t on)
 host state: configured by a host, DTR set (any context, single writer each)
 
void usbl_set_dtr (uint8_t on)
 
uint8_t usbl_host (void)
 a USB host has configured the device (cable to a PC): ARM is refused
 
uint8_t usbl_open (uint32_t now)
 a program has the port open: host and (DTR or a command in the last USBL_OPEN_MS)
 
void usbl_rx_isr (const uint8_t *p, uint32_t n)
 bytes that do not fit are dropped
 
static int rx_byte (uint8_t *b)
 
uint32_t usbl_rx_errors (void)
 frames with a bad CRC
 
static void resync (void)
 
static int frame_ready (void)
 
uint32_t usbl_poll (uint8_t *cmd, uint32_t cap, uint32_t now)
 next CMD frame payload; returns its length (>= 2), 0 if none.
 
uint32_t usbl_tx_free_src (uint8_t src)
 
uint32_t usbl_tx_dropped (uint8_t src)
 frames that did not fit
 
int usbl_put_frame (uint8_t src, const uint8_t *f, uint32_t n)
 a frame already encoded; 0 = queued whole
 
int usbl_send_src (uint8_t src, uint8_t type, const void *payload, uint8_t len)
 a whole v2 frame built from type and payload; 0 = queued whole
 
static void choose (void)
 
uint32_t usbl_tx_peek (uint8_t *buf, uint32_t cap)
 
void usbl_tx_consume (uint32_t n)
 
void usbl_tx_flush (void)
 nobody listening: empties every ring
 

Variables

static uint8_t s_rx [USBL_RX_SIZE]
 
static volatile uint32_t s_rx_h
 
static volatile uint32_t s_rx_t
 
static ring_t s_tx [USBL_SRC_COUNT]
 
static uint8_t s_cur
 
static uint32_t s_cur_left
 
static volatile uint8_t s_cfgd
 
static volatile uint8_t s_dtr
 
static volatile uint8_t s_ever
 
static volatile uint32_t s_cmd_ms
 
static uint8_t s_acc [4+255+2]
 
static uint32_t s_na
 
static uint32_t s_err
 

Detailed Description

USB byte-stream link (see usb_link.h).

Definition in file usb_link.c.

Function Documentation

◆ usbl_reset()

void usbl_reset ( void  )

Definition at line 35 of file usb_link.c.

References s_cfgd, s_cmd_ms, s_cur, s_cur_left, s_dtr, s_err, s_ever, s_na, s_rx_h, s_rx_t, and s_tx.

Referenced by log_task_init().

Here is the caller graph for this function:

◆ usbl_set_configured()

void usbl_set_configured ( uint8_t  on)

host state: configured by a host, DTR set (any context, single writer each)

Definition at line 45 of file usb_link.c.

References on, s_cfgd, and s_dtr.

Referenced by usb_cdc_pump().

Here is the caller graph for this function:

◆ usbl_set_dtr()

void usbl_set_dtr ( uint8_t  on)

Definition at line 46 of file usb_link.c.

References on, and s_dtr.

Referenced by itf_control().

Here is the caller graph for this function:

◆ usbl_host()

uint8_t usbl_host ( void  )

a USB host has configured the device (cable to a PC): ARM is refused

Definition at line 47 of file usb_link.c.

References s_cfgd.

Referenced by app_start(), and build_checks().

Here is the caller graph for this function:

◆ usbl_open()

uint8_t usbl_open ( uint32_t  now)

a program has the port open: host and (DTR or a command in the last USBL_OPEN_MS)

Definition at line 49 of file usb_link.c.

References s_cfgd, s_cmd_ms, s_dtr, s_ever, and USBL_OPEN_MS.

Referenced by app_step(), LogTask(), and tx_frame().

Here is the caller graph for this function:

◆ usbl_rx_isr()

void usbl_rx_isr ( const uint8_t *  p,
uint32_t  n 
)

bytes that do not fit are dropped

Definition at line 57 of file usb_link.c.

References n, s_rx, s_rx_h, s_rx_t, SPSC_BARRIER, and USBL_RX_SIZE.

Referenced by itf_receive().

Here is the caller graph for this function:

◆ rx_byte()

static int rx_byte ( uint8_t *  b)
static

Definition at line 70 of file usb_link.c.

References b, s_rx, s_rx_h, s_rx_t, SPSC_BARRIER, and USBL_RX_SIZE.

Referenced by usbl_poll().

Here is the caller graph for this function:

◆ usbl_rx_errors()

uint32_t usbl_rx_errors ( void  )

frames with a bad CRC

Definition at line 80 of file usb_link.c.

References s_err.

◆ resync()

static void resync ( void  )
static

Definition at line 83 of file usb_link.c.

References s_acc, and s_na.

Referenced by frame_ready().

Here is the caller graph for this function:

◆ frame_ready()

static int frame_ready ( void  )
static

Definition at line 95 of file usb_link.c.

References resync(), s_acc, s_err, s_na, and tlm_crc16().

Referenced by usbl_poll().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ usbl_poll()

uint32_t usbl_poll ( uint8_t *  cmd,
uint32_t  cap,
uint32_t  now_ms 
)

next CMD frame payload; returns its length (>= 2), 0 if none.

Records the arrival time.

Definition at line 105 of file usb_link.c.

References b, cap, cmd(), frame_ready(), n, PV2_FT_CMD, rx_byte(), s_acc, s_cmd_ms, s_ever, and s_na.

Referenced by log_task_step().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ usbl_tx_free_src()

uint32_t usbl_tx_free_src ( uint8_t  src)

Definition at line 134 of file usb_link.c.

References ring_t::h, s_tx, ring_t::t, USBL_SRC_COUNT, and USBL_TX_SIZE.

Referenced by list_pace(), usbl_put_frame(), and usbl_tx_free().

Here is the caller graph for this function:

◆ usbl_tx_dropped()

uint32_t usbl_tx_dropped ( uint8_t  src)

frames that did not fit

Definition at line 140 of file usb_link.c.

References ring_t::dropped, s_tx, and USBL_SRC_COUNT.

◆ usbl_put_frame()

int usbl_put_frame ( uint8_t  src,
const uint8_t *  f,
uint32_t  n 
)

a frame already encoded; 0 = queued whole

Definition at line 142 of file usb_link.c.

References ring_t::b, ring_t::dropped, ring_t::h, n, s_tx, SPSC_BARRIER, USBL_SRC_COUNT, usbl_tx_free_src(), and USBL_TX_SIZE.

Referenced by tx(), tx_frame(), and usbl_send_src().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ usbl_send_src()

int usbl_send_src ( uint8_t  src,
uint8_t  type,
const void *  payload,
uint8_t  len 
)

a whole v2 frame built from type and payload; 0 = queued whole

Definition at line 154 of file usb_link.c.

References n, pv2_frame(), and usbl_put_frame().

Referenced by usbl_send().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ choose()

static void choose ( void  )
static

Definition at line 164 of file usb_link.c.

References ring_t::h, n, s_cur, s_cur_left, s_tx, SPSC_BARRIER, ring_t::t, and USBL_SRC_COUNT.

Referenced by usbl_tx_peek().

Here is the caller graph for this function:

◆ usbl_tx_peek()

uint32_t usbl_tx_peek ( uint8_t *  buf,
uint32_t  cap 
)

Definition at line 174 of file usb_link.c.

References ring_t::b, cap, choose(), n, s_cur, s_cur_left, s_tx, ring_t::t, and USBL_TX_SIZE.

Referenced by usb_cdc_pump().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ usbl_tx_consume()

void usbl_tx_consume ( uint32_t  n)

Definition at line 185 of file usb_link.c.

References n, s_cur, s_cur_left, s_tx, and ring_t::t.

Referenced by usb_cdc_pump().

Here is the caller graph for this function:

◆ usbl_tx_flush()

void usbl_tx_flush ( void  )

nobody listening: empties every ring

Definition at line 192 of file usb_link.c.

References s_cur_left, s_tx, and USBL_SRC_COUNT.

Referenced by usb_cdc_pump().

Here is the caller graph for this function:

Variable Documentation

◆ s_rx

uint8_t s_rx[USBL_RX_SIZE]
static

Definition at line 11 of file usb_link.c.

Referenced by rx_byte(), and usbl_rx_isr().

◆ s_rx_h

volatile uint32_t s_rx_h
static

Definition at line 12 of file usb_link.c.

Referenced by rx_byte(), usbl_reset(), and usbl_rx_isr().

◆ s_rx_t

volatile uint32_t s_rx_t
static

Definition at line 12 of file usb_link.c.

Referenced by rx_byte(), usbl_reset(), and usbl_rx_isr().

◆ s_tx

◆ s_cur

uint8_t s_cur
static

Definition at line 23 of file usb_link.c.

Referenced by choose(), usbl_reset(), usbl_tx_consume(), and usbl_tx_peek().

◆ s_cur_left

uint32_t s_cur_left
static

Definition at line 24 of file usb_link.c.

Referenced by choose(), usbl_reset(), usbl_tx_consume(), usbl_tx_flush(), and usbl_tx_peek().

◆ s_cfgd

volatile uint8_t s_cfgd
static

Definition at line 27 of file usb_link.c.

Referenced by usbl_host(), usbl_open(), usbl_reset(), and usbl_set_configured().

◆ s_dtr

volatile uint8_t s_dtr
static

Definition at line 27 of file usb_link.c.

Referenced by usbl_open(), usbl_reset(), usbl_set_configured(), and usbl_set_dtr().

◆ s_ever

volatile uint8_t s_ever
static

Definition at line 27 of file usb_link.c.

Referenced by usbl_open(), usbl_poll(), and usbl_reset().

◆ s_cmd_ms

volatile uint32_t s_cmd_ms
static

Definition at line 28 of file usb_link.c.

Referenced by usbl_open(), usbl_poll(), and usbl_reset().

◆ s_acc

uint8_t s_acc[4+255+2]
static

Definition at line 31 of file usb_link.c.

Referenced by frame_ready(), resync(), and usbl_poll().

◆ s_na

uint32_t s_na
static

Definition at line 32 of file usb_link.c.

Referenced by frame_ready(), resync(), usbl_poll(), and usbl_reset().

◆ s_err

uint32_t s_err
static

Definition at line 33 of file usb_link.c.

Referenced by frame_ready(), usbl_reset(), and usbl_rx_errors().