mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-19 14:14:00 +00:00
lora: created shared RadioEvent
Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
@@ -0,0 +1,23 @@
|
|||||||
|
package lora
|
||||||
|
|
||||||
|
const (
|
||||||
|
RadioEventRxDone = iota
|
||||||
|
RadioEventTxDone
|
||||||
|
RadioEventTimeout
|
||||||
|
RadioEventWatchdog
|
||||||
|
RadioEventCrcError
|
||||||
|
RadioEventUnhandled
|
||||||
|
)
|
||||||
|
|
||||||
|
// RadioEvent are used for communicating in the radio Event Channel
|
||||||
|
type RadioEvent struct {
|
||||||
|
EventType int
|
||||||
|
IRQStatus uint16
|
||||||
|
EventData []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewRadioEvent() returns a new RadioEvent that can be used in the RadioChannel
|
||||||
|
func NewRadioEvent(eType int, irqStatus uint16, eData []byte) RadioEvent {
|
||||||
|
r := RadioEvent{EventType: eType, IRQStatus: irqStatus, EventData: eData}
|
||||||
|
return r
|
||||||
|
}
|
||||||
@@ -7,11 +7,12 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"machine"
|
"machine"
|
||||||
"tinygo.org/x/drivers"
|
"tinygo.org/x/drivers"
|
||||||
|
"tinygo.org/x/drivers/lora"
|
||||||
)
|
)
|
||||||
|
|
||||||
// New creates a new SX126x connection.
|
// New creates a new SX126x connection.
|
||||||
func New(spi drivers.SPI) *Device {
|
func New(spi drivers.SPI) *Device {
|
||||||
c := make(chan RadioEvent, 10)
|
c := make(chan lora.RadioEvent, 10)
|
||||||
d := Device{
|
d := Device{
|
||||||
spi: spi,
|
spi: spi,
|
||||||
radioEventChan: c,
|
radioEventChan: c,
|
||||||
|
|||||||
+28
-34
@@ -31,21 +31,21 @@ const (
|
|||||||
RFSWITCH_TX_HP = iota
|
RFSWITCH_TX_HP = iota
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
// const (
|
||||||
RadioEventRxDone = iota
|
// RadioEventRxDone = iota
|
||||||
RadioEventTxDone = iota
|
// RadioEventTxDone = iota
|
||||||
RadioEventTimeout = iota
|
// RadioEventTimeout = iota
|
||||||
RadioEventWatchdog = iota
|
// RadioEventWatchdog = iota
|
||||||
RadioEventCrcError = iota
|
// RadioEventCrcError = iota
|
||||||
RadioEventUnhandled = iota
|
// RadioEventUnhandled = iota
|
||||||
)
|
// )
|
||||||
|
|
||||||
// RadioEvent are used for communicating in the radio Event Channel
|
// // RadioEvent are used for communicating in the radio Event Channel
|
||||||
type RadioEvent struct {
|
// type RadioEvent struct {
|
||||||
EventType int
|
// EventType int
|
||||||
IRQStatus uint16
|
// IRQStatus uint16
|
||||||
EventData []byte
|
// EventData []byte
|
||||||
}
|
// }
|
||||||
|
|
||||||
const (
|
const (
|
||||||
PERIOD_PER_SEC = (uint32)(1000000 / 15.625) // SX1261 DS 13.1.4
|
PERIOD_PER_SEC = (uint32)(1000000 / 15.625) // SX1261 DS 13.1.4
|
||||||
@@ -54,12 +54,12 @@ const (
|
|||||||
|
|
||||||
// Device wraps an SPI connection to a SX127x device.
|
// Device wraps an SPI connection to a SX127x device.
|
||||||
type Device struct {
|
type Device struct {
|
||||||
spi drivers.SPI // SPI bus for module communication
|
spi drivers.SPI // SPI bus for module communication
|
||||||
radioEventChan chan RadioEvent // Channel for Receiving events
|
radioEventChan chan lora.RadioEvent // Channel for Receiving events
|
||||||
loraConf lora.Config // Current Lora configuration
|
loraConf lora.Config // Current Lora configuration
|
||||||
rfswitch RFSwitch // RF Switch, if any
|
rfswitch RFSwitch // RF Switch, if any
|
||||||
deepSleep bool // Internal Sleep state
|
deepSleep bool // Internal Sleep state
|
||||||
deviceType int // sx1261,sx1262,sx1268 (defaults sx1261)
|
deviceType int // sx1261,sx1262,sx1268 (defaults sx1261)
|
||||||
spiBuffer [SPI_BUFFER_SIZE]uint8
|
spiBuffer [SPI_BUFFER_SIZE]uint8
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,14 +82,8 @@ func timeoutMsToRtcSteps(timeoutMs uint32) uint32 {
|
|||||||
// Channel and events
|
// Channel and events
|
||||||
//
|
//
|
||||||
// --------------------------------------------------
|
// --------------------------------------------------
|
||||||
// NewRadioEvent() returns a new RadioEvent that can be used in the RadioChannel
|
|
||||||
func NewRadioEvent(eType int, irqStatus uint16, eData []byte) RadioEvent {
|
|
||||||
r := RadioEvent{EventType: eType, IRQStatus: irqStatus, EventData: eData}
|
|
||||||
return r
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get the RadioEvent channel of the device
|
// Get the RadioEvent channel of the device
|
||||||
func (d *Device) GetRadioEventChan() chan RadioEvent {
|
func (d *Device) GetRadioEventChan() chan lora.RadioEvent {
|
||||||
return d.radioEventChan
|
return d.radioEventChan
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -615,7 +609,7 @@ func (d *Device) LoraTx(pkt []uint8, timeoutMs uint32) error {
|
|||||||
d.SetTx(timeoutMsToRtcSteps(timeoutMs))
|
d.SetTx(timeoutMsToRtcSteps(timeoutMs))
|
||||||
|
|
||||||
msg := <-d.GetRadioEventChan()
|
msg := <-d.GetRadioEventChan()
|
||||||
if msg.EventType != RadioEventTxDone {
|
if msg.EventType != lora.RadioEventTxDone {
|
||||||
return errors.New("Unexpected Radio Event while TX")
|
return errors.New("Unexpected Radio Event while TX")
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@@ -644,9 +638,9 @@ func (d *Device) LoraRx(timeoutMs uint32) ([]uint8, error) {
|
|||||||
|
|
||||||
msg := <-d.GetRadioEventChan()
|
msg := <-d.GetRadioEventChan()
|
||||||
|
|
||||||
if msg.EventType == RadioEventTimeout {
|
if msg.EventType == lora.RadioEventTimeout {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
} else if msg.EventType != RadioEventRxDone {
|
} else if msg.EventType != lora.RadioEventRxDone {
|
||||||
return nil, errors.New("Unexpected Radio Event while RX")
|
return nil, errors.New("Unexpected Radio Event while RX")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -666,19 +660,19 @@ func (d *Device) HandleInterrupt() {
|
|||||||
rChan := d.GetRadioEventChan()
|
rChan := d.GetRadioEventChan()
|
||||||
|
|
||||||
if (st & SX126X_IRQ_RX_DONE) > 0 {
|
if (st & SX126X_IRQ_RX_DONE) > 0 {
|
||||||
rChan <- NewRadioEvent(RadioEventRxDone, st, nil)
|
rChan <- lora.NewRadioEvent(lora.RadioEventRxDone, st, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (st & SX126X_IRQ_TX_DONE) > 0 {
|
if (st & SX126X_IRQ_TX_DONE) > 0 {
|
||||||
rChan <- NewRadioEvent(RadioEventTxDone, st, nil)
|
rChan <- lora.NewRadioEvent(lora.RadioEventTxDone, st, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (st & SX126X_IRQ_TIMEOUT) > 0 {
|
if (st & SX126X_IRQ_TIMEOUT) > 0 {
|
||||||
rChan <- NewRadioEvent(RadioEventTimeout, st, nil)
|
rChan <- lora.NewRadioEvent(lora.RadioEventTimeout, st, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (st & SX126X_IRQ_CRC_ERR) > 0 {
|
if (st & SX126X_IRQ_CRC_ERR) > 0 {
|
||||||
rChan <- NewRadioEvent(RadioEventCrcError, st, nil)
|
rChan <- lora.NewRadioEvent(lora.RadioEventCrcError, st, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+15
-37
@@ -13,35 +13,19 @@ import (
|
|||||||
"tinygo.org/x/drivers/lora"
|
"tinygo.org/x/drivers/lora"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
RadioEventRxDone = iota
|
|
||||||
RadioEventTxDone = iota
|
|
||||||
RadioEventTimeout = iota
|
|
||||||
RadioEventWatchdog = iota
|
|
||||||
RadioEventCrcError = iota
|
|
||||||
RadioEventUnhandled = iota
|
|
||||||
)
|
|
||||||
|
|
||||||
// So we can keep track of the origin of interruption
|
// So we can keep track of the origin of interruption
|
||||||
const (
|
const (
|
||||||
SPI_BUFFER_SIZE = 256
|
SPI_BUFFER_SIZE = 256
|
||||||
)
|
)
|
||||||
|
|
||||||
// RadioEvent are used for communicating in the radio Event Channel
|
|
||||||
type RadioEvent struct {
|
|
||||||
EventType int
|
|
||||||
IRQStatus uint8
|
|
||||||
EventData []byte
|
|
||||||
}
|
|
||||||
|
|
||||||
// Device wraps an SPI connection to a SX127x device.
|
// Device wraps an SPI connection to a SX127x device.
|
||||||
type Device struct {
|
type Device struct {
|
||||||
spi drivers.SPI // SPI bus for module communication
|
spi drivers.SPI // SPI bus for module communication
|
||||||
rstPin, csPin machine.Pin // GPIOs for reset and chip select
|
rstPin, csPin machine.Pin // GPIOs for reset and chip select
|
||||||
radioEventChan chan RadioEvent // Channel for Receiving events
|
radioEventChan chan lora.RadioEvent // Channel for Receiving events
|
||||||
loraConf lora.Config // Current Lora configuration
|
loraConf lora.Config // Current Lora configuration
|
||||||
deepSleep bool // Internal Sleep state
|
deepSleep bool // Internal Sleep state
|
||||||
deviceType int // sx1261,sx1262,sx1268 (defaults sx1261)
|
deviceType int // sx1261,sx1262,sx1268 (defaults sx1261)
|
||||||
spiBuffer [SPI_BUFFER_SIZE]uint8
|
spiBuffer [SPI_BUFFER_SIZE]uint8
|
||||||
packetIndex uint8 // FIXME ... useless ?
|
packetIndex uint8 // FIXME ... useless ?
|
||||||
}
|
}
|
||||||
@@ -51,14 +35,8 @@ type Device struct {
|
|||||||
// Channel and events
|
// Channel and events
|
||||||
//
|
//
|
||||||
// --------------------------------------------------
|
// --------------------------------------------------
|
||||||
// NewRadioEvent() returns a new RadioEvent that can be used in the RadioChannel
|
|
||||||
func NewRadioEvent(eType int, irqStatus uint8, eData []byte) RadioEvent {
|
|
||||||
r := RadioEvent{EventType: eType, IRQStatus: irqStatus, EventData: eData}
|
|
||||||
return r
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get the RadioEvent channel of the device
|
// Get the RadioEvent channel of the device
|
||||||
func (d *Device) GetRadioEventChan() chan RadioEvent {
|
func (d *Device) GetRadioEventChan() chan lora.RadioEvent {
|
||||||
return d.radioEventChan
|
return d.radioEventChan
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,7 +46,7 @@ func New(spi machine.SPI, csPin machine.Pin, rstPin machine.Pin) *Device {
|
|||||||
spi: spi,
|
spi: spi,
|
||||||
csPin: csPin,
|
csPin: csPin,
|
||||||
rstPin: rstPin,
|
rstPin: rstPin,
|
||||||
radioEventChan: make(chan RadioEvent, 10),
|
radioEventChan: make(chan lora.RadioEvent, 10),
|
||||||
}
|
}
|
||||||
return &k
|
return &k
|
||||||
}
|
}
|
||||||
@@ -401,7 +379,7 @@ func (d *Device) LoraTx(pkt []uint8, timeoutMs uint32) error {
|
|||||||
d.SetOpMode(SX127X_OPMODE_TX)
|
d.SetOpMode(SX127X_OPMODE_TX)
|
||||||
|
|
||||||
msg := <-d.GetRadioEventChan()
|
msg := <-d.GetRadioEventChan()
|
||||||
if msg.EventType != RadioEventTxDone {
|
if msg.EventType != lora.RadioEventTxDone {
|
||||||
return errors.New("Unexpected Radio Event while TX " + string(0x30+msg.EventType))
|
return errors.New("Unexpected Radio Event while TX " + string(0x30+msg.EventType))
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@@ -447,9 +425,9 @@ func (d *Device) LoraRx(timeoutMs uint32) ([]uint8, error) {
|
|||||||
radioCh := d.GetRadioEventChan()
|
radioCh := d.GetRadioEventChan()
|
||||||
|
|
||||||
msg := <-radioCh
|
msg := <-radioCh
|
||||||
if msg.EventType == RadioEventTimeout {
|
if msg.EventType == lora.RadioEventTimeout {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
} else if msg.EventType != RadioEventRxDone {
|
} else if msg.EventType != lora.RadioEventRxDone {
|
||||||
return nil, errors.New("Unexpected Radio Event while RX " + string(0x30+msg.EventType))
|
return nil, errors.New("Unexpected Radio Event while RX " + string(0x30+msg.EventType))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -504,19 +482,19 @@ func (d *Device) HandleInterrupt() {
|
|||||||
rChan := d.GetRadioEventChan()
|
rChan := d.GetRadioEventChan()
|
||||||
|
|
||||||
if (st & SX127X_IRQ_LORA_RXDONE_MASK) > 0 {
|
if (st & SX127X_IRQ_LORA_RXDONE_MASK) > 0 {
|
||||||
rChan <- NewRadioEvent(RadioEventRxDone, st, nil)
|
rChan <- lora.NewRadioEvent(lora.RadioEventRxDone, uint16(st), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (st & SX127X_IRQ_LORA_TXDONE_MASK) > 0 {
|
if (st & SX127X_IRQ_LORA_TXDONE_MASK) > 0 {
|
||||||
rChan <- NewRadioEvent(RadioEventTxDone, st, nil)
|
rChan <- lora.NewRadioEvent(lora.RadioEventTxDone, uint16(st), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (st & SX127X_IRQ_LORA_RXTOUT_MASK) > 0 {
|
if (st & SX127X_IRQ_LORA_RXTOUT_MASK) > 0 {
|
||||||
rChan <- NewRadioEvent(RadioEventTimeout, st, nil)
|
rChan <- lora.NewRadioEvent(lora.RadioEventTimeout, uint16(st), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (st & SX127X_IRQ_LORA_CRCERR_MASK) > 0 {
|
if (st & SX127X_IRQ_LORA_CRCERR_MASK) > 0 {
|
||||||
rChan <- NewRadioEvent(RadioEventCrcError, st, nil)
|
rChan <- lora.NewRadioEvent(lora.RadioEventCrcError, uint16(st), nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user