Files
heltec-repeater/firmware/main/sx126x.h
T

66 lines
2.7 KiB
C

// Minimal SX1262 driver for wM-Bus C1 on Heltec WiFi LoRa 32 V4.3.
// GFSK packet mode only. No LoRa support.
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
// IRQ bits (SX126x datasheet 13.3.1)
#define SX126X_IRQ_TX_DONE (1u << 0)
#define SX126X_IRQ_RX_DONE (1u << 1)
#define SX126X_IRQ_PREAMBLE_DETECTED (1u << 2)
#define SX126X_IRQ_SYNCWORD_VALID (1u << 3)
#define SX126X_IRQ_CRC_ERR (1u << 6)
#define SX126X_IRQ_TIMEOUT (1u << 9)
#define SX126X_IRQ_ALL 0x03FFu
typedef struct {
bool spi_ok; // sync-word register readback matched
bool reset_ok; // BUSY went low after reset
bool busy_ok; // BUSY currently low
bool tcxo_ok; // XOSC start + calibrate succeeded (no device errors)
uint16_t device_errors;
uint8_t chip_mode; // from GetStatus, 3-bit chip mode field
} sx126x_diag_t;
// Fills diag during bring-up. Returns true if the radio is usable.
bool sx126x_init(sx126x_diag_t *diag);
// Configure GFSK modem for wM-Bus C1: 100 kbps, +/-45 kHz, sync 0x543D.
// rx_payload_len is the fixed RX capture length (we truncate in software).
bool sx126x_config_wmbus_c1(uint32_t freq_hz, uint8_t rx_payload_len);
bool sx126x_set_tx_power(int8_t dbm); // -9 .. +14 (PA config capped)
bool sx126x_set_rx(void); // continuous RX, FEM to RX path
bool sx126x_standby(void);
// Transmit buf as GFSK payload (sync word prepended by radio).
// Handles FEM CTX switching. Blocking until commands issued; TX-done is
// signalled via DIO1/IRQ. timeout handled by caller.
bool sx126x_start_tx(const uint8_t *buf, uint8_t len);
// Restore fixed RX payload length after a TX (packet params are shared).
bool sx126x_restore_rx_payload_len(void);
uint16_t sx126x_get_irq(void);
bool sx126x_clear_irq(uint16_t mask);
// Read last received packet. Returns actual bytes copied (<= maxlen).
int sx126x_read_rx(uint8_t *out, size_t maxlen, int8_t *rssi_dbm);
int8_t sx126x_get_rssi_inst(void);
uint16_t sx126x_get_device_errors(void);
uint8_t sx126x_get_status(void);
// Raw command exchange for diagnostics: sends op + n_tx arg bytes, captures
// the full-duplex MISO bytes into rx (same length). Returns false on error.
bool sx126x_raw(uint8_t op, const uint8_t *args, size_t n_tx,
uint8_t *rx, size_t rx_len);
// FEM control (exposed for diagnostics)
void fem_set_tx(bool tx);
void fem_power(bool on);
void fem_set_rx_csd(bool en); // LNA experiment: FEM CSD level in RX
void sx126x_set_pa_low(bool low); // PA table: true = lowest documented output
// Registered by caller before init; invoked from DIO1 GPIO ISR.
typedef void (*sx126x_irq_cb_t)(int64_t timestamp_us);
void sx126x_set_irq_callback(sx126x_irq_cb_t cb);