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

Byte-stream link over USB CDC: protocol v2 frames in both directions (A5 5A | TYPE | LEN | PAYLOAD | CRC16). More...

#include <stdint.h>
#include <stddef.h>
Include dependency graph for usb_link.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define USBL_RX_SIZE   1024u
 
#define USBL_TX_SIZE   4096u
 per producer
 
#define USBL_CMD_MAX   32u
 
#define USBL_OPEN_MS   3000u
 without DTR, the link stays open this long after a command
 

Enumerations

enum  { USBL_SRC_LOG = 0 , USBL_SRC_APP = 1 , USBL_SRC_TLM = 2 , USBL_SRC_COUNT }
 producers of the TX rings More...
 

Functions

void usbl_reset (void)
 
void usbl_rx_isr (const uint8_t *p, uint32_t n)
 bytes that do not fit are dropped
 
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_ms)
 a program has the port open: host and (DTR or a command in the last USBL_OPEN_MS)
 
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.
 
uint32_t usbl_rx_errors (void)
 frames with a bad CRC
 
uint32_t usbl_tx_free_src (uint8_t 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
 
int usbl_put_frame (uint8_t src, const uint8_t *frame, uint32_t n)
 a frame already encoded; 0 = queued whole
 
static uint32_t usbl_tx_free (void)
 the LogTask's ring (log answers)
 
static int usbl_send (uint8_t type, const void *payload, uint8_t len)
 
uint32_t usbl_tx_dropped (uint8_t src)
 frames that did not fit
 
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
 

Detailed Description

Byte-stream link over USB CDC: protocol v2 frames in both directions (A5 5A | TYPE | LEN | PAYLOAD | CRC16).

The USB link is a ground link: telemetry, parameters, calibrations and the flight log. It never flies the drone (ADR 0009): the AppTask refuses the flight commands that arrive this way, and the flight core refuses ARM, TAKEOFF and MOTOR_TEST while a USB host is connected.

  • RX: the USB interrupt hands the received bytes to usbl_rx_isr() (SPSC byte ring); the LogTask calls usbl_poll(), which resynchronises on A5 5A, checks the CRC and returns the payload of every CMD frame (type 0x20: op, seq, args, the same bytes as a BLE CMD write).
  • TX: one SPSC ring per producing task (LogTask, AppTask, TelemetryTask), each holding whole frames. The board binding (called by the LogTask) drains them with usbl_tx_peek()/usbl_tx_consume(), one ring at a time and only at frame boundaries, so frames of different tasks never interleave.
  • Host state: the binding reports whether a host has configured the device and whether it holds DTR (port open); usbl_open() adds "a command arrived in the last USBL_OPEN_MS", for hosts that do not set DTR.

Transport-agnostic and host-testable: the same code would carry a UART.

Definition in file usb_link.h.

Macro Definition Documentation

◆ USBL_RX_SIZE

#define USBL_RX_SIZE   1024u

Definition at line 31 of file usb_link.h.

◆ USBL_TX_SIZE

#define USBL_TX_SIZE   4096u

per producer

Definition at line 32 of file usb_link.h.

◆ USBL_CMD_MAX

#define USBL_CMD_MAX   32u

Definition at line 33 of file usb_link.h.

◆ USBL_OPEN_MS

#define USBL_OPEN_MS   3000u

without DTR, the link stays open this long after a command

Definition at line 34 of file usb_link.h.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum

producers of the TX rings

Enumerator
USBL_SRC_LOG 
USBL_SRC_APP 
USBL_SRC_TLM 
USBL_SRC_COUNT 

Definition at line 37 of file usb_link.h.

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_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:

◆ 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_ms)

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_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_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.

◆ 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_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:

◆ usbl_put_frame()

int usbl_put_frame ( uint8_t  src,
const uint8_t *  frame,
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_tx_free()

static uint32_t usbl_tx_free ( void  )
inlinestatic

the LogTask's ring (log answers)

Definition at line 65 of file usb_link.h.

References USBL_SRC_LOG, and usbl_tx_free_src().

Referenced by port_room(), and port_room_ack().

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

◆ usbl_send()

static int usbl_send ( uint8_t  type,
const void *  payload,
uint8_t  len 
)
inlinestatic

Definition at line 66 of file usb_link.h.

References usbl_send_src(), and USBL_SRC_LOG.

Referenced by cmd_list(), and port_send().

Here is the call graph for this function:
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_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: