From 8cfe5ebc4a6e9d958084a2da239c46d3b6c92812 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Sat, 7 Jan 2023 13:10:44 +0100 Subject: [PATCH] examples: lora atcmd minimal rx and tx Signed-off-by: deadprogram --- examples/lora/atcmd/commands.go | 38 +++++++++++++++++++++++++++------ examples/lora/atcmd/main.go | 12 +++++------ examples/lora/atcmd/parser.go | 4 ++++ examples/lora/atcmd/radio.go | 1 + examples/lora/atcmd/sim.go | 18 ++++++++++------ examples/lora/atcmd/sx126x.go | 6 +++++- examples/lora/atcmd/sx127x.go | 25 ++++++++++++++-------- examples/lora/atcmd/version.go | 2 +- 8 files changed, 76 insertions(+), 30 deletions(-) diff --git a/examples/lora/atcmd/commands.go b/examples/lora/atcmd/commands.go index f06bda6..de58128 100644 --- a/examples/lora/atcmd/commands.go +++ b/examples/lora/atcmd/commands.go @@ -11,7 +11,7 @@ func quicktest() { // Check firmware version. func version() { - writeCommandOutput("VER", currentVersion()+" "+firmwareVersion()) + writeCommandOutput("VER", currentVersion()+" ("+firmwareVersion()+")") } // Use to check the ID of the LoRaWAN module, or change the ID. @@ -172,7 +172,7 @@ func power(setting string) error { return nil } -// Unconfirmed message repeats times. +// Unconfirmed message repeats times. func rept(setting string) error { cmd := "REPT" writeCommandOutput(cmd, setting) @@ -180,9 +180,9 @@ func rept(setting string) error { return nil } -// Confirmed message retry times. Valid range 0~254, +// Confirmed message retry times. Valid range 0~254, // if retry times is less than 2, only one message will -// be sent. Random delay 3 - 10s between each retry +// be sent. Random delay 3 - 10s between each retry // (band duty cycle limitation has the priority) func retry(setting string) error { cmd := "RETRY" @@ -329,12 +329,38 @@ func log(setting string) error { return nil } -func crlf() { +func recv(setting string) error { + cmd := "RECV" + + data, err := lorarx() + if err != nil { + writeCommandOutput(cmd, "ERROR "+err.Error()) + return err + } + writeCommandOutput(cmd, string(data)) + + return nil +} + +func recvhex(setting string) error { + cmd := "RECVHEX" + + data, err := lorarx() + if err != nil { + writeCommandOutput(cmd, "ERROR "+err.Error()) + return err + } + writeCommandOutput(cmd, string(data)) + + return nil +} + +func crlf() { uart.Write([]byte("\r\n")) } func writeCommandOutput(cmd, data string) { - uart.Write([]byte("+"+cmd+": ")) + uart.Write([]byte("+" + cmd + ": ")) uart.Write([]byte(data)) crlf() } diff --git a/examples/lora/atcmd/main.go b/examples/lora/atcmd/main.go index a817e03..2f53840 100644 --- a/examples/lora/atcmd/main.go +++ b/examples/lora/atcmd/main.go @@ -15,12 +15,12 @@ import ( // change these to test a different UART or pins if available var ( - uart = machine.Serial - tx = machine.UART_TX_PIN - rx = machine.UART_RX_PIN + uart = machine.Serial + tx = machine.UART_TX_PIN + rx = machine.UART_RX_PIN input = make([]byte, 0, 64) - radio LoraRadio + radio LoraRadio defaultTimeout uint32 = 1000 ) @@ -55,11 +55,11 @@ func main() { } } -func fail(msg string) { +func fail(msg string) { for { uart.Write([]byte(msg)) crlf() time.Sleep(time.Minute) } -} \ No newline at end of file +} diff --git a/examples/lora/atcmd/parser.go b/examples/lora/atcmd/parser.go index 5bee414..e2d2cd7 100644 --- a/examples/lora/atcmd/parser.go +++ b/examples/lora/atcmd/parser.go @@ -99,6 +99,10 @@ func parseCommand(cmd, args string) error { test(args) case "LOG": log(args) + case "RECV": + recv(args) + case "RECVHEX": + recvhex(args) default: return errInvalidCommand } diff --git a/examples/lora/atcmd/radio.go b/examples/lora/atcmd/radio.go index 15cc4db..eda7106 100644 --- a/examples/lora/atcmd/radio.go +++ b/examples/lora/atcmd/radio.go @@ -6,6 +6,7 @@ import ( var ( errRadioNotFound = errors.New("radio not found") + errRxTimeout = errors.New("radio RX timeout") ) type LoraRadio interface { diff --git a/examples/lora/atcmd/sim.go b/examples/lora/atcmd/sim.go index 57f8b9a..c2c252e 100644 --- a/examples/lora/atcmd/sim.go +++ b/examples/lora/atcmd/sim.go @@ -4,7 +4,7 @@ package main // do simulator setup here func setupLora() (LoraRadio, error) { - return &SimLoraRadio{}, nil + return &SimLoraRadio{}, nil } type SimLoraRadio struct { @@ -21,13 +21,17 @@ func (sr *SimLoraRadio) LoraRx(timeoutMs uint32) ([]uint8, error) { return nil, nil } -func (sr *SimLoraRadio) SetLoraFrequency(freq uint32) {} -func (sr *SimLoraRadio) SetLoraIqMode(mode uint8) {} -func (sr *SimLoraRadio) SetLoraCodingRate(cr uint8) {} -func (sr *SimLoraRadio) SetLoraBandwidth(bw uint8) {} -func (sr *SimLoraRadio) SetLoraCrc(enable bool) {} +func (sr *SimLoraRadio) SetLoraFrequency(freq uint32) {} +func (sr *SimLoraRadio) SetLoraIqMode(mode uint8) {} +func (sr *SimLoraRadio) SetLoraCodingRate(cr uint8) {} +func (sr *SimLoraRadio) SetLoraBandwidth(bw uint8) {} +func (sr *SimLoraRadio) SetLoraCrc(enable bool) {} func (sr *SimLoraRadio) SetLoraSpreadingFactor(sf uint8) {} func firmwareVersion() string { - return "Simulator "+currentVersion() + return "Simulator " + currentVersion() +} + +func lorarx() ([]byte, error) { + return nil, nil } diff --git a/examples/lora/atcmd/sx126x.go b/examples/lora/atcmd/sx126x.go index 41add5c..9367839 100644 --- a/examples/lora/atcmd/sx126x.go +++ b/examples/lora/atcmd/sx126x.go @@ -52,7 +52,7 @@ func setupLora() (LoraRadio, error) { loraRadio.LoraConfig(loraConf) - return loraRadio, nil + return loraRadio, nil } // radioIntHandler will take care of radio interrupts @@ -63,3 +63,7 @@ func radioIntHandler(intr interrupt.Interrupt) { func firmwareVersion() string { return "sx126x" } + +func lorarx() ([]byte, error) { + return loraRadio.LoraRx(LORA_DEFAULT_RXTIMEOUT_MS) +} diff --git a/examples/lora/atcmd/sx127x.go b/examples/lora/atcmd/sx127x.go index 986119c..c89f108 100644 --- a/examples/lora/atcmd/sx127x.go +++ b/examples/lora/atcmd/sx127x.go @@ -11,15 +11,19 @@ import ( "tinygo.org/x/drivers/sx127x" ) -const FREQ = 868100000 +const ( + FREQ = 868100000 + LORA_DEFAULT_RXTIMEOUT_MS = 1000 + LORA_DEFAULT_TXTIMEOUT_MS = 5000 +) var ( // We assume LoRa Featherwing module is connected to PyBadge: - rstPin = machine.D11 - csPin = machine.D10 - dio0Pin = machine.D6 - dio1Pin = machine.D9 - spi = machine.SPI0 + rstPin = machine.D11 + csPin = machine.D10 + dio0Pin = machine.D6 + dio1Pin = machine.D9 + spi = machine.SPI0 loraRadio *sx127x.Device ) @@ -64,15 +68,18 @@ func setupLora() (LoraRadio, error) { loraRadio.LoraConfig(loraConf) - return loraRadio, nil + return loraRadio, nil } - func dioIrqHandler(machine.Pin) { loraRadio.HandleInterrupt() } func firmwareVersion() string { v := loraRadio.GetVersion() - return "sx127x v"+strconv.Itoa(int(v)) + return "sx127x v" + strconv.Itoa(int(v)) +} + +func lorarx() ([]byte, error) { + return loraRadio.LoraRx(LORA_DEFAULT_RXTIMEOUT_MS) } diff --git a/examples/lora/atcmd/version.go b/examples/lora/atcmd/version.go index d1ee0b9..d6c882f 100644 --- a/examples/lora/atcmd/version.go +++ b/examples/lora/atcmd/version.go @@ -4,4 +4,4 @@ const VERSION = "0.0.1" func currentVersion() string { return VERSION -} \ No newline at end of file +}