From 9ad3ef6afa2ff2f6447b4c223ea01e08e16828c7 Mon Sep 17 00:00:00 2001 From: Alan Carvalho de Assis Date: Sun, 4 Jan 2026 10:24:39 -0300 Subject: [PATCH 1/3] drivers/sx127x: Move Kconfig and add bitrate options This commit moves sx127x Kconfig to lpwan/sx127x/ to follow the same logic used by sx126x and adds the configuration to support different bitrate (the default 4800 is fine tuned for LORA). Signed-off-by: Alan C. Assis --- drivers/wireless/lpwan/Kconfig | 72 +------------------ drivers/wireless/lpwan/sx127x/Kconfig | 95 ++++++++++++++++++++++++++ drivers/wireless/lpwan/sx127x/sx127x.c | 38 +++++++++++ 3 files changed, 134 insertions(+), 71 deletions(-) create mode 100644 drivers/wireless/lpwan/sx127x/Kconfig diff --git a/drivers/wireless/lpwan/Kconfig b/drivers/wireless/lpwan/Kconfig index 8348e4cfc20e8..0b3a9fc4379e5 100644 --- a/drivers/wireless/lpwan/Kconfig +++ b/drivers/wireless/lpwan/Kconfig @@ -28,76 +28,6 @@ config LPWAN_SX127X ---help--- This options adds driver support for the Samtech SX127X chip. -if LPWAN_SX127X - -config LPWAN_SX127X_RFFREQ_DEFAULT - int "SX127X default RF frequency" - default 433000000 - -config LPWAN_SX127X_SPIFREQ - int "SX127X SPI frequency" - default 1000000 - ---help--- - SX127X SPI frequency up to 10MHz - -config LPWAN_SX127X_TXPOWER_DEFAULT - int "SX127X default TX power" - default 14 - -config LPWAN_SX127X_PREAMBLE_DEFAULT - int "SX127X default preamble length" - default 8 - -config LPWAN_SX127X_MODULATION_DEFAULT - int "SX127X default modulation scheme" - default 3 if LPWAN_SX127X_LORA - default 1 if LPWAN_SX127X_FSKOOK - range 1 3 - ---help--- - 1 - FSK, 2 - OOK, 3 - LORA - -config LPWAN_SX127X_CRCON - int "SX127X CRC ON" - range 0 1 - default 0 - -config LPWAN_SX127X_RXSUPPORT - bool "SX127X RX support" - default n - -if LPWAN_SX127X_RXSUPPORT - -config LPWAN_SX127X_RXFIFO_LEN - int "SX127X RX FIFO length" - default 5 - -config LPWAN_SX127X_RXFIFO_DATA_LEN - int "SX127X RX FIFO data length" - default 64 - -endif #LPWAN_SX127X_RXSUPPORT - -config LPWAN_SX127X_TXSUPPORT - bool "SX127X TX support" - default n - -config LPWAN_SX127X_LORA - bool "SX127X LORA support" - default y - -if LPWAN_SX127X_LORA - -config LPWAN_SX127X_LORA_IMPHEADER - int "SX127X LORA implicit header ON" - range 0 1 - default 0 - -endif # LPWAN_SX127X_LORA - -config LPWAN_SX127X_FSKOOK - bool "SX127X FSK/OOK support" - default n - -endif # WL_SX127X +source "drivers/wireless/lpwan/sx127x/Kconfig" endif # DRIVERS_LPWAN diff --git a/drivers/wireless/lpwan/sx127x/Kconfig b/drivers/wireless/lpwan/sx127x/Kconfig new file mode 100644 index 0000000000000..4b73457b36c0a --- /dev/null +++ b/drivers/wireless/lpwan/sx127x/Kconfig @@ -0,0 +1,95 @@ +# +# For a description of the syntax of this configuration file, +# see the file kconfig-language.txt in the NuttX tools repository. +# + +if LPWAN_SX127X + +config LPWAN_SX127X_RFFREQ_DEFAULT + int "SX127X default RF frequency" + default 433000000 + +config LPWAN_SX127X_SPIFREQ + int "SX127X SPI frequency" + default 1000000 + ---help--- + SX127X SPI frequency up to 10MHz + +config LPWAN_SX127X_TXPOWER_DEFAULT + int "SX127X default TX power" + default 14 + +config LPWAN_SX127X_PREAMBLE_DEFAULT + int "SX127X default preamble length" + default 8 + +config LPWAN_SX127X_MODULATION_DEFAULT + int "SX127X default modulation scheme" + default 3 if LPWAN_SX127X_LORA + default 1 if LPWAN_SX127X_FSKOOK + range 1 3 + ---help--- + 1 - FSK, 2 - OOK, 3 - LORA + +config LPWAN_SX127X_CRCON + int "SX127X CRC ON" + range 0 1 + default 0 + +config LPWAN_SX127X_RXSUPPORT + bool "SX127X RX support" + default n + +if LPWAN_SX127X_RXSUPPORT + +config LPWAN_SX127X_RXFIFO_LEN + int "SX127X RX FIFO length" + default 5 + +config LPWAN_SX127X_RXFIFO_DATA_LEN + int "SX127X RX FIFO data length" + default 64 + +endif #LPWAN_SX127X_RXSUPPORT + +config LPWAN_SX127X_TXSUPPORT + bool "SX127X TX support" + default n + +config LPWAN_SX127X_LORA + bool "SX127X LORA support" + default y + +if LPWAN_SX127X_LORA + +config LPWAN_SX127X_LORA_IMPHEADER + int "SX127X LORA implicit header ON" + range 0 1 + default 0 + +endif # LPWAN_SX127X_LORA + +config LPWAN_SX127X_FSKOOK + bool "SX127X FSK/OOK support" + default n + +if LPWAN_SX127X_FSKOOK + +choice + prompt "SX127X Bitrate" + default LPWAN_SX127X_FSKOOK_4800 + +config LPWAN_SX127X_FSKOOK_4800 + bool "4800 BPS" + +config LPWAN_SX127X_FSKOOK_38400 + bool "38400 BPS" + +config LPWAN_SX127X_FSKOOK_76800 + bool "76800 BPS" + +endchoice + +endif # LPWAN_SX127X_FSKOOK + +endif # WL_SX127X diff --git a/drivers/wireless/lpwan/sx127x/sx127x.c b/drivers/wireless/lpwan/sx127x/sx127x.c index 635af9f8dbf78..8acf14b26c3af 100644 --- a/drivers/wireless/lpwan/sx127x/sx127x.c +++ b/drivers/wireless/lpwan/sx127x/sx127x.c @@ -77,6 +77,8 @@ #define SX127X_FREQ_CALIBRATION (CONFIG_LPWAN_SX127X_RFFREQ_DEFAULT) +#ifdef CONFIG_LPWAN_SX127X_FSKOOK_4800 + /* FSK default frequency deviation is 5kHz */ #define SX127X_FDEV_DEFAULT (5000) @@ -90,6 +92,42 @@ #define SX127X_FSKOOK_RXBW_DEFAULT FSKOOK_BANDWIDTH_15P6KHZ #define SX127X_FSKOOK_AFCBW_DEFAULT FSKOOK_BANDWIDTH_20P8KHZ +#endif /* CONFIG_LPWAN_SX127X_FSKOOK_4800 */ + +#ifdef CONFIG_LPWAN_SX127X_FSKOOK_38400 + +/* FSK frequency deviation for 38400 is 20kHz */ + +#define SX127X_FDEV_DEFAULT (20000) + +/* FSK/OOK bitrate default */ + +#define SX127X_FOM_BITRATE_DEFAULT (38400) + +/* FSK/OOK bandwidth for 38400 */ + +#define SX127X_FSKOOK_RXBW_DEFAULT FSKOOK_BANDWIDTH_250KHZ +#define SX127X_FSKOOK_AFCBW_DEFAULT FSKOOK_BANDWIDTH_250KHZ + +#endif /* CONFIG_LPWAN_SX127X_FSKOOK_38400 */ + +#ifdef CONFIG_LPWAN_SX127X_FSKOOK_76800 + +/* FSK frequency deviation for 76800 is 75kHz */ + +#define SX127X_FDEV_DEFAULT (75000) + +/* FSK/OOK bitrate default */ + +#define SX127X_FOM_BITRATE_DEFAULT (76800) + +/* FSK/OOK bandwidth for 76800 */ + +#define SX127X_FSKOOK_RXBW_DEFAULT FSKOOK_BANDWIDTH_250KHZ +#define SX127X_FSKOOK_AFCBW_DEFAULT FSKOOK_BANDWIDTH_250KHZ + +#endif /* CONFIG_LPWAN_SX127X_FSKOOK_76800 */ + /* Default LORA bandwidth */ #define SX127X_LRM_BW_DEFAULT LORA_BANDWIDTH_7P8KHZ From 8dbb108c759cf0bca667e5b6f02a3fd601810e77 Mon Sep 17 00:00:00 2001 From: Alan Carvalho de Assis Date: Sun, 4 Jan 2026 11:10:50 -0300 Subject: [PATCH 2/3] drivers/sx127x: Add RX Timeout support This commit adds support to fix RX timeout for SX127x, it is done leaving the RX mode, entering in stanby mode and returning to RX mode. It is enough to fix the AFC and get communication working again. Signed-off-by: Alan C. Assis --- drivers/wireless/lpwan/sx127x/Kconfig | 12 +++++ drivers/wireless/lpwan/sx127x/sx127x.c | 73 +++++++++++++++++++++++++- 2 files changed, 84 insertions(+), 1 deletion(-) diff --git a/drivers/wireless/lpwan/sx127x/Kconfig b/drivers/wireless/lpwan/sx127x/Kconfig index 4b73457b36c0a..b787792da93a0 100644 --- a/drivers/wireless/lpwan/sx127x/Kconfig +++ b/drivers/wireless/lpwan/sx127x/Kconfig @@ -50,6 +50,18 @@ config LPWAN_SX127X_RXFIFO_DATA_LEN int "SX127X RX FIFO data length" default 64 +config LPWAN_SX127X_RX_TIMEOUT + int "SX127X RX Timeout in Milliseconds" + default 10000 + ---help--- + If device stopped receiving data for more than + LPWAN_SX127X_RX_TIMEOUT milliseconds it means the + frequency drift is too high that AFC failed to fix + the reception frequency. So if we leave the RX mode, + entering in standby and returning will clear the + AFC and fix the communication again. + If you don't want to use RX timeout set it to -1. + endif #LPWAN_SX127X_RXSUPPORT config LPWAN_SX127X_TXSUPPORT diff --git a/drivers/wireless/lpwan/sx127x/sx127x.c b/drivers/wireless/lpwan/sx127x/sx127x.c index 8acf14b26c3af..8d4e1181bda7d 100644 --- a/drivers/wireless/lpwan/sx127x/sx127x.c +++ b/drivers/wireless/lpwan/sx127x/sx127x.c @@ -316,10 +316,12 @@ struct sx127x_dev_s bool pa_force; /* Force PA BOOST pin select */ #endif #ifdef CONFIG_LPWAN_SX127X_RXSUPPORT - uint32_t rx_timeout; /* RX timeout (not supported) */ + uint32_t rx_timeout; /* RX timeout in milliseconds */ uint16_t rx_fifo_len; /* Number of bytes stored in fifo */ uint16_t nxt_read; /* Next read index */ uint16_t nxt_write; /* Next write index */ + clock_t last_rx_tick; /* Ticks since last RX event to detect timeout */ + struct work_s rx_watchdog; /* Watchdog to detect timeout */ /* Circular RX packet buffer */ @@ -1276,6 +1278,50 @@ static int sx127x_poll(FAR struct file *filep, FAR struct pollfd *fds, #endif } +/**************************************************************************** + * Name: sx127x_rx_watchdog + * + * Description: + * Watchdog to detect SX127x RX communication stall + * + ****************************************************************************/ + +#ifdef CONFIG_LPWAN_SX127X_RXSUPPORT +static void sx127x_rx_watchdog(FAR void *arg) +{ + FAR struct sx127x_dev_s *dev = (FAR struct sx127x_dev_s *)arg; + clock_t now = clock_systime_ticks(); + + if (dev->opmode == SX127X_OPMODE_RX && + (now - dev->last_rx_tick) > MSEC2TICK(dev->rx_timeout)) + { + wlerr("RX stall detected, restarting RX\n"); + + /* Leave RX mode to clear AFC + bit sync */ + + sx127x_opmode_set(dev, SX127X_OPMODE_STANDBY); + + /* datasheet-safe delay */ + + nxsched_usleep(100); + + /* Re-enter RX */ + + sx127x_opmode_set(dev, SX127X_OPMODE_RX); + + /* Avoid using old RX tick, otherwise it always will fail */ + + dev->last_rx_tick = now; + } + + /* Reschedule watchdog */ + + work_queue(LPWORK, &dev->rx_watchdog, + sx127x_rx_watchdog, dev, + MSEC2TICK(dev->rx_timeout)); +} +#endif + /**************************************************************************** * Name: sx127x_lora_isr0_process * @@ -1471,6 +1517,21 @@ static int sx127x_fskook_isr0_process(FAR struct sx127x_dev_s *dev) ret = sx127x_fskook_rxhandle(dev); if (ret > 0) { + /* Should we take care of RX timeout? */ + + if (dev->rx_timeout > 0) + { + /* Keep a track of last RX time to detect timeout */ + + dev->last_rx_tick = clock_systime_ticks(); + + /* Prepare the work queue to take care of RX timeout */ + + work_queue(LPWORK, &dev->rx_watchdog, + sx127x_rx_watchdog, dev, + MSEC2TICK(dev->rx_timeout)); + } + if (dev->pfd) { /* Data available for input */ @@ -4204,6 +4265,12 @@ static int sx127x_deinit(FAR struct sx127x_dev_s *dev) { wlinfo("Deinit sx127x dev\n"); +#ifdef CONFIG_LPWAN_SX127X_RXSUPPORT + /* Cancel any running watchdog */ + + work_cancel(LPWORK, &dev->rx_watchdog); +#endif + /* Enter SLEEP mode */ sx127x_opmode_set(dev, SX127X_OPMODE_SLEEP); @@ -4559,6 +4626,7 @@ static int sx127x_unregister(FAR struct sx127x_dev_s *dev) nxsem_destroy(&dev->tx_sem); #endif #ifdef CONFIG_LPWAN_SX127X_RXSUPPORT + work_cancel(LPWORK, &dev->rx_watchdog); nxsem_destroy(&dev->rx_sem); nxmutex_destroy(&dev->rx_buffer_lock); #endif @@ -4614,6 +4682,9 @@ int sx127x_register(FAR struct spi_dev_s *spi, dev->crcon = CONFIG_LPWAN_SX127X_CRCON; #ifdef CONFIG_LPWAN_SX127X_FSKOOK dev->fskook.fixlen = false; +# ifdef CONFIG_LPWAN_SX127X_RXSUPPORT + dev->rx_timeout = CONFIG_LPWAN_SX127X_RX_TIMEOUT; +# endif #endif #ifdef CONFIG_LPWAN_SX127X_LORA dev->lora.invert_iq = false; From 36c09feb8144582f9211fc8be74626fb5a693d33 Mon Sep 17 00:00:00 2001 From: Alan Carvalho de Assis Date: Sun, 4 Jan 2026 11:55:25 -0300 Subject: [PATCH 3/3] drivers/sx126x: Fix typo in the Kconfig This is a small fix just to true the github getting the new commits after running git push -f Signed-off-by: Alan C. Assis --- drivers/wireless/lpwan/sx126x/Kconfig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/wireless/lpwan/sx126x/Kconfig b/drivers/wireless/lpwan/sx126x/Kconfig index 6a04e1952c034..822c40267d0b4 100644 --- a/drivers/wireless/lpwan/sx126x/Kconfig +++ b/drivers/wireless/lpwan/sx126x/Kconfig @@ -25,4 +25,4 @@ config LPWAN_SX126X_MAX_DEVICES int "SX126X maximum devices" default 1 -endif # DRIVERS_LPWAN +endif # LPWAN_SX126X