Skip to content

sx126x: remove machine dependency, fix bugs, add features and tests - #954

Open
deadprogram wants to merge 8 commits into
sx127x-improvementsfrom
sx126x-improvements
Open

deadprogram wants to merge 8 commits into
sx127x-improvementsfrom
sx126x-improvements

Conversation

@deadprogram

Copy link
Copy Markdown
Member

This PR improves the sx126x driver. It is stacked on #953.

Changes:

  • Remove the dependency on the machine package. NewRadioControl takes pin interfaces, and the STM32WL radio control takes an RFSwitch function. Helpers are included for the Nucleo-WL55JC, GNSE and LoRa-E5 boards
  • Fix SetCrc off, the device error and stats decoding, the 863 MHz image calibration values, and the Rx buffer read
  • Return the SetupInterrupts error from SetRadioController
  • Eliminate heap allocations
  • Add CAD, packet status, instant RSSI, stats reset, random number, Rx duty cycle, DIO2 RF switch control, DIO3 TCXO control and the symbol timeout
  • Add unit tests with a fake radio, including allocation checks

API change: NewRadioControl now takes pin interfaces, or an RFSwitch on STM32WL.

Tested with make unit-test and the smoke tests.

@deadprogram

Copy link
Copy Markdown
Member Author

Hardware test results on a LoRa-E5 board, flashed with ST-Link.

examples/sx126x/lora_rxtx

Tested against a PyBadge with a LoRa FeatherWing running examples/sx127x/lora_rxtx built from this branch. Both use 868.1 MHz, SF9, BW 125 kHz, CR 4/7, private sync word.

Board Driver TX RX Errors
LoRa-E5 sx126x 4 3 none
PyBadge + FeatherWing sx127x 4 4 none

Each board received Hello TinyGO (len 12) from the other in every RX window that followed a transmit.

examples/sx126x/lora_continuous

Runs without panics or hangs. Timing matches the example.

10:05:11 2 seconds in Continuous Preamble
10:05:13 Continuous Preamble Stopped
10:05:23 2 seconds in Continuous Wave
10:05:25  Continuous Wave Stopped
10:06:25 2 seconds in Continuous Preamble

RF output in continuous modes was not measured.

Not tested on hardware: CAD, packet status, instant RSSI, random number, Rx duty cycle.

@deadprogram

Copy link
Copy Markdown
Member Author

Hardware test of the new features on a LoRa-E5, with a PyBadge and LoRa FeatherWing sending Hello TinyGO every 10 seconds.

Feature Result
RandomU32 Varied values, no repeats
GetRssiInst -110 to -115 dBm noise floor
ResetStats, GetLoraStats Counts match packets received, 0 CRC and header errors
GetPacketStatus RSSI -15 to -30 dBm, SNR 10 to 11 dB at close range
Cad Bursts of detections every 10.4 s that match the PyBadge packets
SetRxDutyCycle RxDone with the 12 byte packet
GetDeviceErrors 0

Two commits were added from this testing.

  • 3a839df: Rx returned a slice of the shared SPI buffer, so GetPacketStatus overwrote the first bytes of the packet. Rx now copies the packet into its own buffer. The dev branch has the same behavior.
  • 54f3777: SetLoraSymbNumTimeout now encodes the value and writes register 0x0706 the same way as SX126xSetLoRaSymbNumTimeout in Semtech LoRaMac-node.

Note on the symbol timeout. With 8 or 16 symbols, Rx(0) and Rx(3000) did not end early on an empty channel, both before and after 54f3777. Each waited for the next packet or the full timeout. The command and register values now match Semtech's code, but an early timeout was not seen on hardware.

Not tested: SetDio2AsRfSwitchCtrl and SetDio3AsTcxoCtrl, since the LoRa-E5 does not use DIO2 or DIO3 for these.

@deadprogram

Copy link
Copy Markdown
Member Author

Hardware test of 29ea5df on a LoRa-E5, with a PyBadge and LoRa FeatherWing running examples/sx127x/lora_rxtx as the transmitter.

This applies the same interrupt fix as ba5472e in #953. HandleInterrupt used SPI from the radio interrupt. The interrupt now only signals, and Tx, Rx and Cad read the IRQ status. On STM32WL the radio interrupt is disabled in the handler and enabled again after the status is cleared, since the radio IRQ stays active until then.

Interrupt during SPI

GetRssiInst in a loop for 25 seconds in continuous Rx with the RxDone interrupt on.

event at ms: 5226
event at ms: 15469
done, reads: 407774 events: 2

Both packets were handled while SPI was busy, with no hang.

Old and new driver with the same test program

Before 29ea5df After 29ea5df
First 25 s after reset 3 packets, stats 3, 0 errors 3 packets, stats 3, 0 errors
Later 25 s windows 2 packets each 3 packets each
Status, packet type, sync word, IQ, Rx gain, 0x0706 same same

Other features after 29ea5df

Rx, GetPacketStatus and GetLoraStats gave Hello TinyGO intact with matching stats. Cad gave 89 to 107 detections in 25 seconds. SetRxDutyCycle with the new PollEvent got RxDone with the 12 byte packet. GetDeviceErrors stayed 0.

API change: events reach GetRadioEventChan() only while Tx, Rx or Cad waits. After SetRx, SetRxDutyCycle or SetCad, use PollEvent.

@deadprogram

Copy link
Copy Markdown
Member Author

The commits were squashed, so the hashes in my earlier comments changed. The code at the tip is the same.

Use pin.Output, pin.Input and pin.InterruptFunc for the radio control
pins. The STM32WL radio control now takes an RFSwitch, with helpers
for the Nucleo WL55JC1, GNSE and LoRa-E5 boards. Add SetResetPin since
the reset pin could never be set before.
…uffer read

SetCrc(false) now disables CRC. GetDeviceErrors and GetLoraStats no
longer lose the high byte. Use the 863 MHz image calibration values
from the datasheet. Rx now reads the packet from the offset reported by
GetRxBufferStatus.
Keep the SPI buffers inside Device, write buffer data straight into the
Tx buffer and use arrays for command parameters. Tx now rejects packets
longer than 255 bytes instead of growing the buffer.
Add Cad, SetCad and SetCadParams for channel activity detection, with
HandleInterrupt now sending CAD done events. Add GetPacketStatus,
GetRssiInst, ResetStats, RandomU32, SetRxDutyCycle,
SetDio2AsRfSwitchCtrl, SetDio3AsTcxoCtrl and SetLoraSymbNumTimeout.
Test the driver against a fake radio that emulates the SPI commands,
including Tx, Rx, CAD, register access, RF switch helpers and that the
main operations do not allocate.
HandleInterrupt used SPI from the interrupt. If it ran during another SPI transfer, the bus could lock up. The interrupt now only signals, and Tx, Rx and Cad read the IRQ status. On STM32WL the radio interrupt is disabled until the status is cleared. PollEvent waits for events after SetRx, SetRxDutyCycle or SetCad.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant