Midnight Sun Firmware
Loading...
Searching...
No Matches
Telemetry

telemetry Firmware More...

Classes

struct  CanMessageCache
 Per-message cache entry. More...
 
struct  Datagram
 Datagram storage class. More...
 
struct  SdSpiSettings
 Configuration settings for initializing an SD SPI port. More...
 
struct  TelemetryConfig
 Telemetry configuration data. More...
 
struct  TelemetryStorage
 Telemetry storage. More...
 

Macros

#define TELEMETRY_STATS_CAN_ID   2020U
 Synthetic XBee-only ID for the telemetry bus-load stats datagram.
 
#define TELEMETRY_STATS_DLC   6U
 DLC of the telemetry stats datagram (3 × uint16_t).
 
#define WS22_TELEMETRY_STATUS_ID   2039U
 Synthetic XBee-only IDs for WS22 re-encoded telemetry (floats → scaled integers). 2039–2043 sit above the highest system CAN ID (AFE_temperature, 2035). More...
 
#define WS22_TELEMETRY_BUS_ID   2040U
 
#define WS22_TELEMETRY_VELOCITY_ID   2041U
 
#define WS22_TELEMETRY_TEMP_ID   2042U
 
#define WS22_TELEMETRY_DRIVE_CMD_ID   2043U
 
#define WS22_TELEMETRY_DLC   4U
 
#define DATAGRAM_BUFFER_SIZE   64U
 Datagram buffer size.
 
#define DATAGRAM_START_FRAME   0xAAU
 Start of frame indicator.
 
#define DATAGRAM_END_FRAME   0xBBU
 End of frame indicator.
 
#define DATAGRAM_METADATA_SIZE   5U
 Datagram metadata size. More...
 

Enumerations

enum  CanMsgPriority { CAN_MSG_PRIORITY_HIGH = 0 , CAN_MSG_PRIORITY_MEDIUM , CAN_MSG_PRIORITY_LOW , NUM_CAN_MSG_PRIORITIES }
 Transmission priority tier for a cached CAN message. More...
 
enum  SdSpiBaudrate {
  SD_SPI_BAUDRATE_312_5KHZ = 0 , SD_SPI_BAUDRATE_625KHZ , SD_SPI_BAUDRATE_1_25MHZ , SD_SPI_BAUDRATE_2_5MHZ ,
  SD_SPI_BAUDRATE_5MHZ , SD_SPI_BAUDRATE_10MHZ , SD_SPI_BAUDRATE_20MHZ , SD_SPI_BAUDRATE_40MHZ ,
  NUM_SD_SPI_BAUDRATES
}
 SD Card SPI baudrate options. More...
 
enum  SdSpiMode {
  SD_SPI_MODE_0 = 0 , SD_SPI_MODE_1 , SD_SPI_MODE_2 , SD_SPI_MODE_3 ,
  NUM_SD_SPI_MODES
}
 SPI modes (CPOL and CPHA settings) More...
 
enum  SdSpiPort { SD_SPI_PORT_1 = 0 , SD_SPI_PORT_2 , SD_SPI_PORT_3 , NUM_SD_SPI_PORTS }
 SD SPI port options.
 

Functions

StatusCode decode_can_message (Datagram *datagram, CanMessage *msg)
 Decode CAN message and update a datagram. More...
 
StatusCode encode_datagram (Datagram *datagram, uint32_t id, uint8_t dlc, const uint8_t *data)
 Encode id/dlc/data directly into a datagram (no CanMessage wrapper). More...
 
void log_decoded_message (Datagram *datagram)
 Log the decoded datagram for debug purposes. More...
 
StatusCode sd_card_link_driver (SdSpiPort spi, SdSpiSettings *settings)
 Link the SD card driver with the FATFs API and initialize it. More...
 
StatusCode sd_spi_init (SdSpiPort spi, const SdSpiSettings *settings)
 Initialize SD SPI port with given settings. More...
 
StatusCode sd_spi_tx (SdSpiPort spi, uint8_t *tx_data, size_t tx_len)
 Transmit data to SD card via SPI. More...
 
StatusCode sd_spi_rx (SdSpiPort spi, uint8_t *rx_data, size_t rx_len, uint8_t placeholder)
 Receive data from SD card via SPI. More...
 
StatusCode sd_spi_exchange (SdSpiPort spi, uint8_t *tx_data, size_t tx_len, uint8_t *rx_data, size_t rx_len)
 Exchange data over SPI. More...
 
StatusCode sd_spi_cs_set_state (SdSpiPort spi, GpioState state)
 Set the CS (chip select) GPIO state. More...
 
GpioState sd_spi_cs_get_state (SdSpiPort spi)
 Get the current state of the CS (chip select) pin. More...
 
StatusCode sd_spi_set_frequency (SdSpiPort spi, SdSpiBaudrate baudrate)
 Change the baudrate of an initialized SPI port. More...
 
StatusCode telemetry_init (TelemetryStorage *telemetry_storage, TelemetryConfig *config, Bmi323Storage *bmi323_storage, CanStorage *can_storage)
 Initialize the telemetry interface. More...
 
StatusCode xb_transmit_init (TelemetryStorage *storage, TelemetryConfig *config)
 Initializes the xb_transmit component. More...
 

Variables

CanMessageCache g_can_cache_high []
 High-priority (fast-cycle) message cache. More...
 
const size_t g_can_cache_high_size
 
CanMessageCache g_can_cache_medium []
 Medium-priority (medium-cycle) message cache. More...
 
const size_t g_can_cache_medium_size
 
CanMessageCache g_can_cache_low []
 Low-priority (slow-cycle) message cache. More...
 
const size_t g_can_cache_low_size
 

Detailed Description

telemetry Firmware

Macro Definition Documentation

◆ DATAGRAM_METADATA_SIZE

#define DATAGRAM_METADATA_SIZE   5U

Datagram metadata size.

1 byte for start frame 2 bytes for ID 1 byte for DLC 1 byte for end frame

◆ WS22_TELEMETRY_BUS_ID

#define WS22_TELEMETRY_BUS_ID   2040U

bus_voltage_cV (u16) + bus_current_cA (i16)

◆ WS22_TELEMETRY_DLC

#define WS22_TELEMETRY_DLC   4U

DLC for all WS22 synthetic entries (2 × uint16_t)

◆ WS22_TELEMETRY_DRIVE_CMD_ID

#define WS22_TELEMETRY_DRIVE_CMD_ID   2043U

drive_current×10000 (u16) + drive_velocity_rpm (i16)

◆ WS22_TELEMETRY_STATUS_ID

#define WS22_TELEMETRY_STATUS_ID   2039U

Synthetic XBee-only IDs for WS22 re-encoded telemetry (floats → scaled integers). 2039–2043 sit above the highest system CAN ID (AFE_temperature, 2035).

error_flags (u16) + limit_flags (u16)

◆ WS22_TELEMETRY_TEMP_ID

#define WS22_TELEMETRY_TEMP_ID   2042U

motor_temp_°C×10 (i16) + heatsink_temp_°C×10 (i16)

◆ WS22_TELEMETRY_VELOCITY_ID

#define WS22_TELEMETRY_VELOCITY_ID   2041U

motor_velocity_rpm×10 (i16) + vehicle_kph×100 (u16)

Enumeration Type Documentation

◆ CanMsgPriority

Transmission priority tier for a cached CAN message.

Derived from the message's cycle rate in the board YAML: fast-cycle → HIGH, medium-cycle → MEDIUM, slow-cycle → LOW

◆ SdSpiBaudrate

SD Card SPI baudrate options.

These prescaler values map to SPI peripheral baudrate divisors. The actual baudrate is calculated as APB_Frequency / Prescaler. On STM32, prescalers must be powers of 2.

Enumerator
SD_SPI_BAUDRATE_312_5KHZ 

312.5 kHz

SD_SPI_BAUDRATE_625KHZ 

625 kHz

SD_SPI_BAUDRATE_1_25MHZ 

1.25 MHz

SD_SPI_BAUDRATE_2_5MHZ 

2.5 MHz

SD_SPI_BAUDRATE_5MHZ 

5 MHz

SD_SPI_BAUDRATE_10MHZ 

10 MHz

SD_SPI_BAUDRATE_20MHZ 

20 MHz

SD_SPI_BAUDRATE_40MHZ 

40 MHz

NUM_SD_SPI_BAUDRATES 

Number of baudrate options

◆ SdSpiMode

enum SdSpiMode

SPI modes (CPOL and CPHA settings)

Enumerator
SD_SPI_MODE_0 

CPOL: 0, CPHA: 0 — idle low, sample rising edge

SD_SPI_MODE_1 

CPOL: 0, CPHA: 1 — idle low, sample falling edge

SD_SPI_MODE_2 

CPOL: 1, CPHA: 0 — idle high, sample falling edge

SD_SPI_MODE_3 

CPOL: 1, CPHA: 1 — idle high, sample rising edge

NUM_SD_SPI_MODES 

Number of SPI modes

Function Documentation

◆ decode_can_message()

StatusCode decode_can_message ( Datagram datagram,
CanMessage msg 
)

Decode CAN message and update a datagram.

Parameters
datagramPointer to the datagram to be updated
msgPointer to the new CAN data

◆ encode_datagram()

StatusCode encode_datagram ( Datagram datagram,
uint32_t  id,
uint8_t  dlc,
const uint8_t *  data 
)

Encode id/dlc/data directly into a datagram (no CanMessage wrapper).

Parameters
datagramPointer to the datagram to populate
idRaw CAN ID
dlcPayload length in bytes
dataPointer to payload

◆ log_decoded_message()

void log_decoded_message ( Datagram datagram)

Log the decoded datagram for debug purposes.

Parameters
datagramPointer to the datagram to be debugged

◆ sd_card_link_driver()

StatusCode sd_card_link_driver ( SdSpiPort  spi,
SdSpiSettings settings 
)

Link the SD card driver with the FATFs API and initialize it.

Parameters
spiwhich SPI port to use for the SD card
settingspointer to your pre-configured SdSpiSettings
Returns
STATUS_CODE_OK on success, or an error code.

◆ sd_spi_cs_get_state()

GpioState sd_spi_cs_get_state ( SdSpiPort  spi)

Get the current state of the CS (chip select) pin.

Parameters
spiSPI port
Returns
GPIO_STATE_HIGH if pin is high GPIO_STATE_LOW if pin is low

◆ sd_spi_cs_set_state()

StatusCode sd_spi_cs_set_state ( SdSpiPort  spi,
GpioState  state 
)

Set the CS (chip select) GPIO state.

Parameters
spiSPI port
stateDesired CS state (GPIO_STATE_LOW to select)
Returns
STATUS_CODE_OK on success STATUS_CODE_INVALID_ARGS if port is invalid STATUS_CODE_UNINITIALIZED if SPI not initialized

◆ sd_spi_exchange()

StatusCode sd_spi_exchange ( SdSpiPort  spi,
uint8_t *  tx_data,
size_t  tx_len,
uint8_t *  rx_data,
size_t  rx_len 
)

Exchange data over SPI.

Simultaneously transmit and receive data. Lengths may differ. Transmits 0xFF if no TX data

Parameters
spiSPI port to use
tx_dataPointer to data to transmit (can be NULL)
tx_lenNumber of bytes to transmit
rx_dataPointer to buffer for received data (can be NULL)
rx_lenNumber of bytes to receive
Returns
STATUS_CODE_OK on success STATUS_CODE_INVALID_ARGS if parameters are invalid STATUS_CODE_UNINITIALIZED if SPI not initialized STATUS_CODE_INTERNAL_ERROR if SPI transfer fails

◆ sd_spi_init()

StatusCode sd_spi_init ( SdSpiPort  spi,
const SdSpiSettings settings 
)

Initialize SD SPI port with given settings.

Note that STM32 SPI baudrate prescalers are powers of 2, so the resulting baudrate may not match the requested value exactly. Check resulting timing if critical

Parameters
spiSPI port to initialize
settingsPointer to configuration settings
Returns
STATUS_CODE_OK on success STATUS_CODE_INVALID_ARGS if parameters are invalid STATUS_CODE_RESOURCE_EXHAUSTED if already initialized STATUS_CODE_INTERNAL_ERROR if HAL initialization fails

◆ sd_spi_rx()

StatusCode sd_spi_rx ( SdSpiPort  spi,
uint8_t *  rx_data,
size_t  rx_len,
uint8_t  placeholder 
)

Receive data from SD card via SPI.

Reads |rx_len| bytes into |rx_data|. Sends the specified |placeholder| byte while receiving (commonly 0xFF)

Parameters
spiSPI port to use
rx_dataPointer to buffer to receive data
rx_lenNumber of bytes to receive
placeholderByte to transmit during receive
Returns
STATUS_CODE_OK on success STATUS_CODE_INVALID_ARGS if inputs are invalid STATUS_CODE_UNINITIALIZED if SPI is not initialized STATUS_CODE_INTERNAL_ERROR if HAL receive fails

◆ sd_spi_set_frequency()

StatusCode sd_spi_set_frequency ( SdSpiPort  spi,
SdSpiBaudrate  baudrate 
)

Change the baudrate of an initialized SPI port.

Reconfigures the SPI peripheral to operate at a different baudrate

Parameters
spiSPI port
baudrateDesired new baudrate
Returns
STATUS_CODE_OK on success STATUS_CODE_INVALID_ARGS if arguments are invalid STATUS_CODE_UNINITIALIZED if SPI not initialized

◆ sd_spi_tx()

StatusCode sd_spi_tx ( SdSpiPort  spi,
uint8_t *  tx_data,
size_t  tx_len 
)

Transmit data to SD card via SPI.

Sends |tx_len| bytes from |tx_data| over SPI without changing the CS line. Received bytes are discarded

Parameters
spiSPI port to use
tx_dataPointer to data to transmit
tx_lenNumber of bytes to transmit
Returns
STATUS_CODE_OK on success STATUS_CODE_INVALID_ARGS if inputs are invalid STATUS_CODE_UNINITIALIZED if SPI is not initialized STATUS_CODE_INTERNAL_ERROR if HAL transmit fails

◆ telemetry_init()

StatusCode telemetry_init ( TelemetryStorage telemetry_storage,
TelemetryConfig config,
Bmi323Storage bmi323_storage,
CanStorage can_storage 
)

Initialize the telemetry interface.

Parameters
telemetry_storagePointer to the telemetry storage struct
configPointer to the telemetry config
bmi323_storagepointer to bmi323_storage struct
can_storagepointer to can_storage struct
Returns
STATUS_CODE_OK if telemetry initialization succeeded STATUS_CODE_INVALID_ARGS if one of the parameters are NULL/incorrect

◆ xb_transmit_init()

StatusCode xb_transmit_init ( TelemetryStorage storage,
TelemetryConfig config 
)

Initializes the xb_transmit component.

Returns
STATUS_CODE_OK if initialization is successful STATUS_CODE_INVALID_ARGS if storage is null

Variable Documentation

◆ g_can_cache_high

CanMessageCache g_can_cache_high[]
extern

High-priority (fast-cycle) message cache.

Currently empty — no fast-cycle messages target telemetry.

◆ g_can_cache_low

CanMessageCache g_can_cache_low[]
extern

Low-priority (slow-cycle) message cache.

Contains 3 front_controller power group entries plus the telemetry-internal stats datagram and 5 WS22 synthetic entries.

◆ g_can_cache_medium

CanMessageCache g_can_cache_medium[]
extern

Medium-priority (medium-cycle) message cache.

Contains 23 entries from rear_controller, steering, front_controller, and IMU.