mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-17 13:23:26 +00:00
sx126x: refactor to RadioController interface to more easily handle non-STM32WL boards and remove duplicated code
Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
//go:build gnse
|
||||
|
||||
/*
|
||||
Generic Node Sensor Edition
|
||||
RFSwitch
|
||||
|
||||
Disable Switch : PB8=OFF PA0=OFF PA1=OFF
|
||||
Enable RX : PB8=ON PA0=ON PA1=OFF
|
||||
Enable TX RFO LP : PB8=ON PA0=ON PA1=ON
|
||||
Enable TX RFO HP : PB8=ON PA0=OFF PA1=ON
|
||||
*/
|
||||
package sx126x
|
||||
|
||||
import (
|
||||
"machine"
|
||||
)
|
||||
|
||||
// RadioControl for GNSE board.
|
||||
type RadioControl struct {
|
||||
STM32RadioControl
|
||||
}
|
||||
|
||||
func NewRadioControl() *RadioControl {
|
||||
return &RadioControl{STM32RadioControl{}}
|
||||
}
|
||||
|
||||
// Init pins needed for controlling rx/tx
|
||||
func (rc *RadioControl) Init() error {
|
||||
machine.RF_FE_CTRL1.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
machine.RF_FE_CTRL2.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
machine.RF_FE_CTRL3.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (rc *RadioControl) SetRfSwitchMode(mode int) error {
|
||||
switch mode {
|
||||
|
||||
case RFSWITCH_TX_HP:
|
||||
machine.RF_FE_CTRL1.Set(false)
|
||||
machine.RF_FE_CTRL2.Set(true)
|
||||
machine.RF_FE_CTRL3.Set(true)
|
||||
|
||||
case RFSWITCH_TX_LP:
|
||||
machine.RF_FE_CTRL1.Set(true)
|
||||
machine.RF_FE_CTRL2.Set(true)
|
||||
machine.RF_FE_CTRL3.Set(true)
|
||||
|
||||
case RFSWITCH_RX:
|
||||
machine.RF_FE_CTRL1.Set(true)
|
||||
machine.RF_FE_CTRL2.Set(false)
|
||||
machine.RF_FE_CTRL3.Set(true)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
//go:build lorae5
|
||||
|
||||
/*
|
||||
LoRa-E5 module ONLY transmits through RFO_HP:
|
||||
|
||||
Receive: PA4=1, PA5=0
|
||||
Transmit(high output power, SMPS mode): PA4=0, PA5=1
|
||||
*/
|
||||
|
||||
package sx126x
|
||||
|
||||
import (
|
||||
"machine"
|
||||
)
|
||||
|
||||
// RadioControl for LoRa-E5 board.
|
||||
type RadioControl struct {
|
||||
STM32RadioControl
|
||||
}
|
||||
|
||||
func NewRadioControl() *RadioControl {
|
||||
return &RadioControl{STM32RadioControl: STM32RadioControl{}}
|
||||
}
|
||||
|
||||
// Init pins needed for controlling rx/tx
|
||||
func (rc *RadioControl) Init() error {
|
||||
machine.PA4.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
machine.PB5.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (rc *RadioControl) SetRfSwitchMode(mode int) error {
|
||||
switch mode {
|
||||
|
||||
case RFSWITCH_RX:
|
||||
machine.PA4.Set(true)
|
||||
machine.PB5.Set(false)
|
||||
case RFSWITCH_TX_LP:
|
||||
return errLowPowerTxNotSupported
|
||||
case RFSWITCH_TX_HP:
|
||||
machine.PA4.Set(false)
|
||||
machine.PB5.Set(true)
|
||||
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
//go:build !stm32wlx
|
||||
|
||||
package sx126x
|
||||
|
||||
import (
|
||||
"machine"
|
||||
|
||||
"time"
|
||||
)
|
||||
|
||||
type RadioControl struct {
|
||||
nssPin, busyPin, dio0Pin, dio1Pin machine.Pin
|
||||
rxPin, txLowPin, txHighPin machine.Pin
|
||||
}
|
||||
|
||||
func NewRadioControl(nssPin, busyPin, dio0Pin, dio1Pin,
|
||||
rxPin, txLowPin, txHighPin machine.Pin) *RadioControl {
|
||||
return &RadioControl{
|
||||
nssPin: nssPin,
|
||||
busyPin: busyPin,
|
||||
dio0Pin: dio0Pin,
|
||||
dio1Pin: dio1Pin,
|
||||
rxPin: rxPin,
|
||||
txLowPin: txLowPin,
|
||||
txHighPin: txHighPin,
|
||||
}
|
||||
}
|
||||
|
||||
// SetNss sets the NSS line aka chip select for SPI.
|
||||
func (rc *RadioControl) SetNss(state bool) error {
|
||||
rc.nssPin.Set(state)
|
||||
return nil
|
||||
}
|
||||
|
||||
// WaitWhileBusy wait until the radio is no longer busy
|
||||
func (rc *RadioControl) WaitWhileBusy() error {
|
||||
count := 100
|
||||
for count > 0 {
|
||||
if !rc.busyPin.Get() {
|
||||
return nil
|
||||
}
|
||||
count--
|
||||
time.Sleep(time.Millisecond)
|
||||
}
|
||||
return errWaitWhileBusyTimeout
|
||||
}
|
||||
|
||||
// Init() configures whatever needed for sx126x radio control
|
||||
func (rc *RadioControl) Init() error {
|
||||
rc.nssPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
rc.busyPin.Configure(machine.PinConfig{Mode: machine.PinInputPulldown})
|
||||
return nil
|
||||
}
|
||||
|
||||
// add interrupt handler for Radio IRQs for pins
|
||||
func (rc *RadioControl) SetupInterrupts(handler func()) error {
|
||||
irqHandler = handler
|
||||
|
||||
rc.dio0Pin.Configure(machine.PinConfig{Mode: machine.PinInputPulldown})
|
||||
if err := rc.dio0Pin.SetInterrupt(machine.PinRising, handleInterrupt); err != nil {
|
||||
return errRadioNotFound
|
||||
}
|
||||
|
||||
rc.dio1Pin.Configure(machine.PinConfig{Mode: machine.PinInputPulldown})
|
||||
if err := rc.dio1Pin.SetInterrupt(machine.PinRising, handleInterrupt); err != nil {
|
||||
return errRadioNotFound
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
var irqHandler func()
|
||||
|
||||
func handleInterrupt(machine.Pin) {
|
||||
irqHandler()
|
||||
}
|
||||
|
||||
func (rc *RadioControl) SetRfSwitchMode(mode int) error {
|
||||
switch mode {
|
||||
|
||||
case RFSWITCH_RX:
|
||||
rc.rxPin.Set(true)
|
||||
rc.txLowPin.Set(false)
|
||||
rc.txHighPin.Set(false)
|
||||
case RFSWITCH_TX_LP:
|
||||
rc.rxPin.Set(false)
|
||||
rc.txLowPin.Set(true)
|
||||
rc.txHighPin.Set(false)
|
||||
case RFSWITCH_TX_HP:
|
||||
rc.rxPin.Set(false)
|
||||
rc.txLowPin.Set(false)
|
||||
rc.txHighPin.Set(true)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -4,40 +4,28 @@ package sx126x
|
||||
|
||||
import (
|
||||
"device/stm32"
|
||||
"errors"
|
||||
"machine"
|
||||
"tinygo.org/x/drivers"
|
||||
"tinygo.org/x/drivers/lora"
|
||||
|
||||
"runtime/interrupt"
|
||||
)
|
||||
|
||||
// New creates a new SX126x connection.
|
||||
func New(spi drivers.SPI) *Device {
|
||||
c := make(chan lora.RadioEvent, 10)
|
||||
d := Device{
|
||||
spi: spi,
|
||||
radioEventChan: c,
|
||||
}
|
||||
if d.spi == machine.SPI3 {
|
||||
d.SubGhzInit()
|
||||
d.SetDeviceType(DEVICE_TYPE_SX1262)
|
||||
} else {
|
||||
panic("Driver only support SUBGHZSPI (SPI3) on stm32wlx targets")
|
||||
}
|
||||
return &d
|
||||
// STM32RadioControl helps implement the RadioController interface
|
||||
type STM32RadioControl struct {
|
||||
irqHandler func()
|
||||
}
|
||||
|
||||
// SpiSetNss Sets the NSS line
|
||||
func (d *Device) SpiSetNss(state bool) {
|
||||
// SetNss sets the NSS line aka chip select for SPI.
|
||||
func (rc *STM32RadioControl) SetNss(state bool) error {
|
||||
if state {
|
||||
stm32.PWR.SUBGHZSPICR.SetBits(stm32.PWR_SUBGHZSPICR_NSS)
|
||||
} else {
|
||||
stm32.PWR.SUBGHZSPICR.ClearBits(stm32.PWR_SUBGHZSPICR_NSS)
|
||||
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// WaitBusy sleep until all busy flags clears
|
||||
func (d *Device) WaitBusy() error {
|
||||
// WaitWhileBusy wait until the radio is no longer busy
|
||||
func (rc *STM32RadioControl) WaitWhileBusy() error {
|
||||
count := 100
|
||||
var rfbusyms, rfbusys bool
|
||||
for count > 0 {
|
||||
@@ -49,12 +37,11 @@ func (d *Device) WaitBusy() error {
|
||||
}
|
||||
count--
|
||||
}
|
||||
return errors.New("WaitBusy Timeout")
|
||||
return errWaitWhileBusyTimeout
|
||||
}
|
||||
|
||||
// SubGhzInit() configures internal SX1262's SPI bus.
|
||||
func (d *Device) SubGhzInit() {
|
||||
|
||||
// init() configures whatever needed for sx126x radio control
|
||||
func init() {
|
||||
// Enable APB3 Periph clock and delay
|
||||
stm32.RCC.APB3ENR.SetBits(stm32.RCC_APB3ENR_SUBGHZSPIEN)
|
||||
_ = stm32.RCC.APB3ENR.Get()
|
||||
@@ -81,5 +68,18 @@ func (d *Device) SubGhzInit() {
|
||||
stm32.SPI3.CR1.Set(stm32.SPI_CR1_MSTR | stm32.SPI_CR1_SSI | (0b010 << 3) | stm32.SPI_CR1_SSM)
|
||||
stm32.SPI3.CR2.Set(stm32.SPI_CR2_FRXTH | (0b111 << 8))
|
||||
stm32.SPI3.CR1.SetBits(stm32.SPI_CR1_SPE)
|
||||
|
||||
}
|
||||
|
||||
func (rc *STM32RadioControl) SetupInterrupts(handler func()) error {
|
||||
irqHandler = handler
|
||||
intr := interrupt.New(stm32.IRQ_Radio_IRQ_Busy, handleInterrupt)
|
||||
intr.Enable()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
var irqHandler func()
|
||||
|
||||
func handleInterrupt(interrupt.Interrupt) {
|
||||
irqHandler()
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
//go:build nucleowl55jc
|
||||
|
||||
/*
|
||||
Nucleo WL55JC1
|
||||
RFSwitch
|
||||
|
||||
+-----------+---------+------------+------------+
|
||||
| | FE_CTRL1 | FE_CTRL2 | FE_CTRL3 |
|
||||
| | (PC4) | (PC5) | (PC3) |
|
||||
+-----------+----------+-----------+------------+
|
||||
| TX_HP | LOW | HIGH | HIGH |
|
||||
| TX_LP | HIGH | HIGH | HIGH |
|
||||
| RX | HIGH | LOW | HIGH |
|
||||
+-----------+----------+-----------+------------+
|
||||
*/
|
||||
package sx126x
|
||||
|
||||
import (
|
||||
"machine"
|
||||
)
|
||||
|
||||
// RadioControl for Nucleo WL55JC1 board.
|
||||
type RadioControl struct {
|
||||
STM32RadioControl
|
||||
}
|
||||
|
||||
func NewRadioControl() *RadioControl {
|
||||
return &RadioControl{STM32RadioControl{}}
|
||||
}
|
||||
|
||||
// Init pins needed for controlling rx/tx
|
||||
func (rc *RadioControl) Init() error {
|
||||
machine.PC4.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
machine.PC5.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
machine.PC3.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (rc *RadioControl) SetRfSwitchMode(mode int) error {
|
||||
switch mode {
|
||||
|
||||
case RFSWITCH_TX_HP:
|
||||
machine.PC4.Set(false)
|
||||
machine.PC5.Set(true)
|
||||
machine.PC3.Set(true)
|
||||
|
||||
case RFSWITCH_TX_LP:
|
||||
machine.PC4.Set(true)
|
||||
machine.PC5.Set(true)
|
||||
machine.PC3.Set(true)
|
||||
|
||||
case RFSWITCH_RX:
|
||||
machine.PC4.Set(true)
|
||||
machine.PC5.Set(false)
|
||||
machine.PC3.Set(true)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
+59
-34
@@ -13,12 +13,22 @@ import (
|
||||
"tinygo.org/x/drivers/lora"
|
||||
)
|
||||
|
||||
// SX126X radio transceiver RF_IN and RF_OUT may be connected
|
||||
// to RF Switch. This interface allows the creation of struct
|
||||
var (
|
||||
errWaitWhileBusyTimeout = errors.New("WaitBusy Timeout")
|
||||
errLowPowerTxNotSupported = errors.New("RFSWITCH_TX_LP not supported")
|
||||
errRadioNotFound = errors.New("LoRa radio not found")
|
||||
)
|
||||
|
||||
// SX126X radio transceiver has several pins that control
|
||||
// RF_IN, RF_OUT, NSS, and BUSY.
|
||||
// This interface allows the creation of struct
|
||||
// that can drive the RF Switch (Used in Lora RX and Lora Tx)
|
||||
type RFSwitch interface {
|
||||
InitRFSwitch()
|
||||
type RadioController interface {
|
||||
Init() error
|
||||
SetRfSwitchMode(mode int) error
|
||||
SetNss(state bool) error
|
||||
WaitWhileBusy() error
|
||||
SetupInterrupts(handler func()) error
|
||||
}
|
||||
|
||||
const (
|
||||
@@ -38,18 +48,28 @@ const (
|
||||
SPI_BUFFER_SIZE = 256
|
||||
)
|
||||
|
||||
// Device wraps an SPI connection to a SX127x device.
|
||||
// Device wraps an SPI connection to a SX126x device.
|
||||
type Device struct {
|
||||
spi drivers.SPI // SPI bus for module communication
|
||||
rstPin, csPin machine.Pin // GPIOs for reset and chip select
|
||||
rstPin machine.Pin // GPIOs for reset, chip select, and busy pin
|
||||
radioEventChan chan lora.RadioEvent // Channel for Receiving events
|
||||
loraConf lora.Config // Current Lora configuration
|
||||
rfswitch RFSwitch // RF Switch, if any
|
||||
controller RadioController // to manage interactions with the radio
|
||||
deepSleep bool // Internal Sleep state
|
||||
deviceType int // sx1261,sx1262,sx1268 (defaults sx1261)
|
||||
spiBuffer [SPI_BUFFER_SIZE]uint8
|
||||
}
|
||||
|
||||
// New creates a new SX126x connection.
|
||||
func New(spi drivers.SPI) *Device {
|
||||
c := make(chan lora.RadioEvent, 10)
|
||||
d := Device{
|
||||
spi: spi,
|
||||
radioEventChan: c,
|
||||
}
|
||||
return &d
|
||||
}
|
||||
|
||||
const (
|
||||
SX126X_RTC_FREQ_IN_HZ uint32 = 64000
|
||||
)
|
||||
@@ -79,10 +99,15 @@ func (d *Device) SetDeviceType(devType int) {
|
||||
d.deviceType = devType
|
||||
}
|
||||
|
||||
// SetRfSwitch let you define a custom RF Switch driver if needed
|
||||
func (d *Device) SetRfSwitch(rfswitch RFSwitch) {
|
||||
d.rfswitch = rfswitch
|
||||
d.rfswitch.InitRFSwitch()
|
||||
// SetRadioControl let you define the RadioController
|
||||
func (d *Device) SetRadioController(rc RadioController) error {
|
||||
d.controller = rc
|
||||
if err := d.controller.Init(); err != nil {
|
||||
return err
|
||||
}
|
||||
d.controller.SetupInterrupts(d.HandleInterrupt)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// --------------------------------------------------
|
||||
@@ -126,8 +151,8 @@ func (d *Device) SetFs() {
|
||||
|
||||
// SetTxContinuousWave set device in test mode to generate a continuous wave (RF tone)
|
||||
func (d *Device) SetTxContinuousWave() {
|
||||
if d.rfswitch != nil {
|
||||
d.rfswitch.SetRfSwitchMode(RFSWITCH_TX_HP)
|
||||
if d.controller != nil {
|
||||
d.controller.SetRfSwitchMode(RFSWITCH_TX_HP)
|
||||
}
|
||||
d.ExecSetCommand(SX126X_CMD_SET_TX_CONTINUOUS_WAVE, []uint8{})
|
||||
}
|
||||
@@ -136,8 +161,8 @@ func (d *Device) SetTxContinuousWave() {
|
||||
// Take care to initialize all Lora settings like it's done in Tx before calling this function
|
||||
// If you don't init properly all the settings, it'll fail
|
||||
func (d *Device) SetTxContinuousPreamble() {
|
||||
if d.rfswitch != nil {
|
||||
d.rfswitch.SetRfSwitchMode(RFSWITCH_TX_HP)
|
||||
if d.controller != nil {
|
||||
d.controller.SetRfSwitchMode(RFSWITCH_TX_HP)
|
||||
}
|
||||
d.ExecSetCommand(SX126X_CMD_SET_TX_INFINITE_PREAMBLE, []uint8{})
|
||||
}
|
||||
@@ -235,25 +260,25 @@ func (d *Device) SetRxTxFallbackMode(fallbackMode uint8) {
|
||||
// ReadRegister reads register value
|
||||
func (d *Device) ReadRegister(addr, size uint16) ([]uint8, error) {
|
||||
d.CheckDeviceReady()
|
||||
d.SpiSetNss(false)
|
||||
d.controller.SetNss(false)
|
||||
// Send command
|
||||
cmd := []uint8{SX126X_CMD_READ_REGISTER, uint8((addr & 0xFF00) >> 8), uint8(addr & 0x00FF), 0x00}
|
||||
d.spi.Tx(cmd, nil)
|
||||
ret := d.spiBuffer[0:size]
|
||||
d.spi.Tx(nil, ret)
|
||||
d.SpiSetNss(true)
|
||||
d.WaitBusy()
|
||||
d.controller.SetNss(true)
|
||||
d.controller.WaitWhileBusy()
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
// WriteRegister writes value to register
|
||||
func (d *Device) WriteRegister(addr uint16, data []uint8) {
|
||||
d.CheckDeviceReady()
|
||||
d.SpiSetNss(false)
|
||||
d.controller.SetNss(false)
|
||||
cmd := []uint8{SX126X_CMD_WRITE_REGISTER, uint8((addr & 0xFF00) >> 8), uint8(addr & 0x00FF)}
|
||||
d.spi.Tx(append(cmd, data...), nil)
|
||||
d.SpiSetNss(true)
|
||||
d.WaitBusy()
|
||||
d.controller.SetNss(true)
|
||||
d.controller.WaitWhileBusy()
|
||||
}
|
||||
|
||||
// WriteBuffer write data from current buffer position
|
||||
@@ -466,12 +491,12 @@ func (d *Device) SetModulationParams(spreadingFactor, bandwidth, codingRate, low
|
||||
// CheckDeviceReady sleep until all busy flags clears
|
||||
func (d *Device) CheckDeviceReady() error {
|
||||
if d.deepSleep == true {
|
||||
d.SpiSetNss(false)
|
||||
d.controller.SetNss(false)
|
||||
time.Sleep(time.Millisecond)
|
||||
d.SpiSetNss(true)
|
||||
d.controller.SetNss(true)
|
||||
d.deepSleep = false
|
||||
}
|
||||
return d.WaitBusy()
|
||||
return d.controller.WaitWhileBusy()
|
||||
}
|
||||
|
||||
// ExecSetCommand send a command to configure the peripheral
|
||||
@@ -482,24 +507,24 @@ func (d *Device) ExecSetCommand(cmd uint8, buf []uint8) {
|
||||
} else {
|
||||
d.deepSleep = false
|
||||
}
|
||||
d.SpiSetNss(false)
|
||||
d.controller.SetNss(false)
|
||||
// Send command and params
|
||||
d.spi.Tx(append([]uint8{cmd}, buf...), nil)
|
||||
d.SpiSetNss(true)
|
||||
d.controller.SetNss(true)
|
||||
if cmd != SX126X_CMD_SET_SLEEP {
|
||||
d.WaitBusy()
|
||||
d.controller.WaitWhileBusy()
|
||||
}
|
||||
}
|
||||
|
||||
// ExecGetCommand queries the peripheral the peripheral
|
||||
func (d *Device) ExecGetCommand(cmd uint8, size uint8) []uint8 {
|
||||
d.CheckDeviceReady()
|
||||
d.SpiSetNss(false)
|
||||
d.controller.SetNss(false)
|
||||
// Send the command and flush first status byte (as not used)
|
||||
d.spi.Tx([]uint8{cmd, 0x00}, nil)
|
||||
d.spi.Tx(nil, d.spiBuffer[:size])
|
||||
d.SpiSetNss(true)
|
||||
d.WaitBusy()
|
||||
d.controller.SetNss(true)
|
||||
d.controller.WaitWhileBusy()
|
||||
return d.spiBuffer[:size]
|
||||
}
|
||||
|
||||
@@ -582,8 +607,8 @@ func (d *Device) Tx(pkt []uint8, timeoutMs uint32) error {
|
||||
return lora.ErrUndefinedLoraConf
|
||||
}
|
||||
|
||||
if d.rfswitch != nil {
|
||||
err := d.rfswitch.SetRfSwitchMode(RFSWITCH_TX_HP)
|
||||
if d.controller != nil {
|
||||
err := d.controller.SetRfSwitchMode(RFSWITCH_TX_HP)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -616,8 +641,8 @@ func (d *Device) Rx(timeoutMs uint32) ([]uint8, error) {
|
||||
return nil, lora.ErrUndefinedLoraConf
|
||||
}
|
||||
|
||||
if d.rfswitch != nil {
|
||||
err := d.rfswitch.SetRfSwitchMode(RFSWITCH_RX)
|
||||
if d.controller != nil {
|
||||
err := d.controller.SetRfSwitchMode(RFSWITCH_RX)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user