STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
board_spi.h
Go to the documentation of this file.
1#ifndef STAR_BOARD_SPI_H
2#define STAR_BOARD_SPI_H
3
4/*
5 * SPI bus layer: turns "talk to chip X" into chip-select + HAL_SPI calls on
6 * the right bus, under a per-bus lock and a deadline.
7 *
8 * SPI1 SX1262 radio (mode 0, ~8 MHz)
9 * SPI2 W5500 Ethernet (mode 0, ~16 MHz)
10 * SPI3 IMU, high-g, baro (mode 3, ~8 MHz) shared: the lock matters here
11 * SPI4 SD NAND (mode 0, 250 kHz for SD init)
12 *
13 * Every call takes an absolute deadline (board_now_ms() based) and releases
14 * chip-select and the bus lock before it returns, on every path.
15 */
16
17#include <stdbool.h>
18#include <stdint.h>
19
20#include "self_test_devices.h"
21
22#ifdef __cplusplus
23extern "C" {
24#endif
25
26typedef enum {
27 BOARD_SPI_IMU = 0, /* ISM330DHCX, U4 */
28 BOARD_SPI_HIGH_G, /* H3LIS100DL, U3 */
29 BOARD_SPI_BARO, /* MS5611, U5 */
30 BOARD_SPI_ETH, /* W5500, U6 */
31 BOARD_SPI_RADIO, /* Wio-SX1262, U8 */
32 BOARD_SPI_NAND, /* ZDSD01GLGEAG SD NAND, U10 */
35
36typedef enum {
38 BOARD_SPI_TIMEOUT, /* lock, BUSY or transfer missed the deadline */
39 BOARD_SPI_ERROR, /* HAL reported a bus error */
40 BOARD_SPI_NOT_CONFIGURED, /* device not routed, or bus mode/clock unfit */
42
43/* Create the bus locks. Call once, after osKernelInitialize(). */
44bool board_spi_init(void);
45
46/* True if the device has a bus and chip-select in the board table. */
48
49/* SPI mode (0..3) and actual SCK frequency of the device's bus. */
52
53/* Lock the device's bus until deadline_ms. Pair with board_spi_unlock(). */
55 uint32_t deadline_ms);
57
58/*
59 * One chip-select frame on a bus the caller has locked: (radio: wait BUSY
60 * low) CS low, len bytes full duplex, CS high. tx == NULL clocks out 0xFF;
61 * rx == NULL discards the reply.
62 */
64 uint8_t* rx, uint16_t len,
65 uint32_t deadline_ms);
66
67/* Lock, one frame, unlock. */
69 const uint8_t* tx, uint8_t* rx,
70 uint16_t len, uint32_t deadline_ms);
71
72/*
73 * Manual chip-select, for sequences that must hold CS across a wait (MS5611
74 * reset: CSB low through the 2.8 ms PROM reload). Caller holds the lock and
75 * must deselect on every path. board_spi_shift() moves bytes without touching
76 * CS (tx == NULL sends 0xFF, rx == NULL discards).
77 */
81 uint8_t* rx, uint16_t len,
82 uint32_t deadline_ms);
83
84/* Clock out len bytes of 0xFF with chip-select HIGH (SD card wake-up).
85 * Caller holds the lock. */
87 uint16_t len, uint32_t deadline_ms);
88
89/* self_test_devices_t.exchange: maps self-test device IDs to this layer. */
91 void* context, uint16_t device_id, uint8_t mode, uint32_t max_hz,
92 self_test_spi_frame_t* frames, uint8_t count, uint32_t budget_ms);
93
94#ifdef __cplusplus
95}
96#endif
97#endif
board_spi_status_t board_spi_idle_clocks(board_spi_device_t device, uint16_t len, uint32_t deadline_ms)
Definition board_spi.c:210
board_spi_device_t
Definition board_spi.h:26
@ BOARD_SPI_COUNT
Definition board_spi.h:33
@ BOARD_SPI_ETH
Definition board_spi.h:30
@ BOARD_SPI_NAND
Definition board_spi.h:32
@ BOARD_SPI_BARO
Definition board_spi.h:29
@ BOARD_SPI_IMU
Definition board_spi.h:27
@ BOARD_SPI_HIGH_G
Definition board_spi.h:28
@ BOARD_SPI_RADIO
Definition board_spi.h:31
self_test_reply_t board_spi_self_test_exchange(void *context, uint16_t device_id, uint8_t mode, uint32_t max_hz, self_test_spi_frame_t *frames, uint8_t count, uint32_t budget_ms)
Definition board_spi.c:243
bool board_spi_init(void)
Definition board_spi.c:56
board_spi_status_t board_spi_frame(board_spi_device_t device, const uint8_t *tx, uint8_t *rx, uint16_t len, uint32_t deadline_ms)
Definition board_spi.c:163
void board_spi_deselect(board_spi_device_t device)
Definition board_spi.c:195
board_spi_status_t board_spi_lock(board_spi_device_t device, uint32_t deadline_ms)
Definition board_spi.c:94
board_spi_status_t board_spi_transfer(board_spi_device_t device, const uint8_t *tx, uint8_t *rx, uint16_t len, uint32_t deadline_ms)
Definition board_spi.c:178
void board_spi_unlock(board_spi_device_t device)
Definition board_spi.c:108
bool board_spi_routed(board_spi_device_t device)
Definition board_spi.c:69
board_spi_status_t board_spi_shift(board_spi_device_t device, const uint8_t *tx, uint8_t *rx, uint16_t len, uint32_t deadline_ms)
Definition board_spi.c:201
board_spi_status_t
Definition board_spi.h:36
@ BOARD_SPI_TIMEOUT
Definition board_spi.h:38
@ BOARD_SPI_NOT_CONFIGURED
Definition board_spi.h:40
@ BOARD_SPI_OK
Definition board_spi.h:37
@ BOARD_SPI_ERROR
Definition board_spi.h:39
uint32_t board_spi_clock_hz(board_spi_device_t device)
Definition board_spi.c:82
uint8_t board_spi_mode(board_spi_device_t device)
Definition board_spi.c:73
void board_spi_select(board_spi_device_t device)
Definition board_spi.c:189
size_type count(const key_type &key) const
Definition json.hpp:19264