Skip to content

sx128x: add LoRa radio interface, ranging, fixes and tests - #955

Open
deadprogram wants to merge 5 commits into
sx126x-improvementsfrom
sx128x-improvements
Open

deadprogram wants to merge 5 commits into
sx126x-improvementsfrom
sx128x-improvements

Conversation

@deadprogram

Copy link
Copy Markdown
Member

This PR improves the sx128x driver. It is stacked on #954.

Changes:

  • Fix the RSSI sign for raw values above 127
  • Eliminate heap allocations
  • Implement the lora.Radio interface with Tx, Rx and CAD, using DIO1 interrupts or polling
  • Add ranging, firmware version, LoRa sync word and frequency error helpers
  • Write the spreading factor registers required by the datasheet (section 14.4.1)
  • Add unit tests with a fake radio, including allocation checks

Tested with make unit-test and the smoke tests.

@deadprogram

Copy link
Copy Markdown
Member Author

Rebased on the current #954 and added 494a68c. These are the same fixes found while testing #953 and #954 on hardware.

  • HandleInterrupt used SPI and WaitWhileBusy from the DIO1 interrupt. On the sx127x this locked up the bus when the interrupt ran during another SPI transfer. The interrupt now only signals, and waitIrq reads and clears the IRQ status. Polling mode without DIO1 is unchanged.
  • Rx returned a slice of the shared SPI buffer, so GetPacketStatusLoRa after Rx overwrote the packet. Rx now copies the packet into its own buffer.

Tested with unit tests only, since I do not have sx128x hardware. The new tests fail before 494a68c and pass after it. The same interrupt design was tested on a PyBadge with an RFM95W and on a LoRa-E5.

API change: events reach GetRadioEventChan() only while Tx, Rx or Cad waits.

@deadprogram
deadprogram force-pushed the sx128x-improvements branch 2 times, most recently from 494a68c to cb07cf5 Compare December 3, 2026 10:58
@deadprogram

Copy link
Copy Markdown
Member Author

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

  • 494a68c (IRQ status read in main code and Rx copies the packet) is now part of a48d394, with its tests in cb07cf5.

@deadprogram

Copy link
Copy Markdown
Member Author

@jwetzell if you can please take a look at this PR and test since you have this radio, I would be very greatful... 😸

@jwetzell

jwetzell commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

I can pull them out and give it a try. Is there an example somewhere?

@deadprogram

Copy link
Copy Markdown
Member Author

I can pull them out and give it a try. Is there an example somewhere?

tinygo-org/wireless#16

The raw RSSI byte is unsigned and the value is -raw/2 dBm. Converting
it to int8 first returned positive values for weak signals.
Keep the SPI buffers inside Device and size them for the largest
command. WriteBuffer with 256 bytes and ReadBuffer with 255 bytes grew
the old 256 byte buffers.
Device now implements lora.Radio with LoraConfig, Tx and Rx, and adds
Cad. These use DIO1 interrupts when SetupInterrupts is called and poll
the IRQ status otherwise. HandleInterrupt sends lora.RadioEvent values.
Add ranging role, address, calibration and result functions, plus
ReadRegisters, GetFirmwareVersion, SetLoRaSyncWord and
GetFrequencyError. SetModulationParamsLoRa now also sets the spreading
factor dependent registers from the datasheet.
Test the driver against a fake radio that emulates the SPI commands,
including LoRa Tx, Rx and CAD with and without interrupts, ranging,
register access, RSSI and that the main operations do not allocate.
The sx128x only works at 2.4 GHz, where LoRaWAN uses 0x21 for public networks (SYNC_WORD_PUBLIC_WW_2G4 in Semtech LoRa Basics Modem). 0x34 is the sub-GHz value.
@jwetzell

jwetzell commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

Ok @deadprogram the basic_demo from the wireless repo flashed and booted successfully. I don't have a LoRaWAN network to join but I can see the attempts at joining via serial monitoring

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.

2 participants