Commit Graph

89 Commits

Author SHA1 Message Date
Kumar Gala 998c03a603 drivers: lora: sx126x: Replace use of DT_INST_LABEL
As we work to phase out devicetree 'label' properties, convert
driver to just use sx126x instead of DT_INST_LABEL for logging
purposes.

Signed-off-by: Kumar Gala <galak@kernel.org>
2022-07-02 18:11:10 -05:00
Jordan Yates f12d36a51e lorawan: move `REQUIRES_FULL_LIBC` dependency
Move the `REQUIRES_FULL_LIBC` dependency from `config LORA` to
`config LORAWAN`. The commit that added the `select` (f590d4fa) mentions
that this is required by `loramac-node`, which is only used by LoRaWAN,
not the base LoRa code.

This results in small FLASH savings when compiling the samples, but can
result in larger savings in more complex applications:
```
// With REQUIRES_FULL_LIBC
west build -b 96b_wistrio zephyr/samples/drivers/lora/send/
[162/162] Linking C executable zephyr/zephyr.elf
Memory region         Used Size  Region Size  %age Used
           FLASH:       37708 B       128 KB     28.77%
            SRAM:        8832 B        32 KB     26.95%
        IDT_LIST:          0 GB         2 KB      0.00%

// Without REQUIRES_FULL_LIBC
[181/181] Linking C executable zephyr/zephyr.elf
Memory region         Used Size  Region Size  %age Used
           FLASH:       37444 B       128 KB     28.57%
            SRAM:        8832 B        32 KB     26.95%
        IDT_LIST:          0 GB         2 KB      0.00%
```

Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
2022-07-02 14:16:18 +02:00
Kumar Gala 7bb17e6fb7 lora: shell: Remove use of DT_LABEL
Remove DT_LABEL usage that is needed for device_get_binding, replace
this with DEVICE_DT_GET.

Signed-off-by: Kumar Gala <galak@kernel.org>
2022-06-09 22:01:00 -05:00
Gerard Marull-Paretas fb60aab245 drivers: migrate includes to <zephyr/...>
In order to bring consistency in-tree, migrate all drivers to the new
prefix <zephyr/...>. Note that the conversion has been scripted, refer
to #45388 for more details.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-05-06 19:58:21 +02:00
Adrien Rouault bc47f66062 drivers: lora: sx127x: fix missing reset gpio instantiation
Instantiate the value of `sx127x` reset GPIO

Signed-off-by: Adrien Rouault <adrien.rouault@nemeus.fr>
2022-04-29 15:30:10 +02:00
Henrik Brix Andersen d4023b3c1b drivers: gpio: move non-standard dts flags to be soc specific
Reserve the upper 8 bits of gpio_dt_flags_t for SoC specific flags and
move the non-standard, hardware-specific GPIO devicetree flags (IO
voltage level, drive strength, debounce filter) from the generic
dt-bindings/gpio/gpio.h header to SoC specific dt-bindings headers.

Some of the SoC specific dt-bindings flags take up more bits than
necessary in order to retain backwards compatibility with the deprecated
GPIO flags. The width of these fields can be reduced/optimized once the
deprecated flags are removed.

Remove hardcoded use of GPIO_INT_DEBOUNCE in GPIO client drivers. This
flag can now be set in the devicetree for boards/SoCs with debounce
filter support. The SoC specific debounce flags have had the _INT part
of their name removed since these flag must be passed to
gpio_pin_configure(), not gpio_pin_interrupt_configure().

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-03-10 13:46:34 -05:00
Gerard Marull-Paretas 95fb0ded6b kconfig: remove Enable from boolean prompts
According to Kconfig guidelines, boolean prompts must not start with
"Enable...". The following command has been used to automate the changes
in this patch:

sed -i "s/bool \"[Ee]nables\? \(\w\)/bool \"\U\1/g" **/Kconfig*

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-03-09 15:35:54 +01:00
Krzysztof Chruscinski 47ae656cc1 all: Deprecate UTIL_LISTIFY and replace with LISTIFY
UTIL_LISTIFY is deprecated. Replacing it with LISTIFY.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2022-03-08 11:03:30 +01:00
Jordan Yates d9a74dda54 lora: sx126x: fix missing GPIO instantiation
Instantiate the values of `sx126x` GPIO's when they are present in
devicetree. This was missed in #42230.

Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
2022-02-28 10:52:23 +01:00
Gerard Marull-Paretas ca7f291dd4 drivers: lora: sx12xx: use gpio_dt_spec
Simplify driver implementation by using gpio_dt_spec.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-02-21 22:16:10 -05:00
Gerard Marull-Paretas dc2c889948 drivers: lora: sx127x: use spi_dt_spec
Simplify driver implementation by using spi_dt_spec.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-02-21 22:16:10 -05:00
Gerard Marull-Paretas 4bcbbe571e drivers: lora: sx126x: use spi_dt_spec
Simplify driver implementation by using spi_dt_spec.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-02-21 22:16:10 -05:00
Marcin Niestroj becc1efe51 drivers: lora: shell: fix 'long' specifier in shell_error()
Replace '%s' with '%ld', so that 'long' value is properly printed with
shell_error(). This suppresses following warning:

  zephyr/drivers/lora/shell.c:181:23: warning: format '%s' expects \
      argument of type 'char *', but argument 4 has type 'long int' \
      [-Wformat=]
    181 |    shell_error(shell, "Invalid bandwidth: %s", lval);
        |                       ^~~~~~~~~~~~~~~~~~~~~~~  ~~~~
        |                                                |
        |                                                long int

Signed-off-by: Marcin Niestroj <m.niestroj@grinn-global.com>
2022-02-01 14:25:38 -06:00
Jordan Yates 1bb7aa21b8 lora: sx126x: don't re-enable interrupt in sleep
Don't re-enable the DIO1 interrupt when the modem is in sleep mode. This
fixes the power regression introduced in Zephyr v2.7.0 #41026.

Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
2022-01-04 10:02:24 -05:00
Gerard Marull-Paretas c759a35d08 drivers: use DT_INST_ENUM_IDX(_OR) macros
Replace `DT_ENUM_IDX(_OR)(DT_DRV_INST(...),` pattern with
`DT_INST_ENUM_IDX(_OR)(...,`.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2021-11-18 19:12:07 -05:00
Jordan Yates 526b3e9605 lora: asynchronous packet reception
Adds functionality to receive LoRa packets asynchronously. Reception
runs continuously until cancelled by another call to `lora_recv_async`.

Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
2021-10-26 15:48:09 +02:00
Torsten Rasmussen bd61122aa2 kconfig: drivers: experimental settings now uses select EXPERIMENTAL
With the introduction of `EXPERIMENTAL` and `WARN_EXPERIMENTAL` in
Zephyr all drivers settings having `[EXPERIMENTAL]` in their
prompt has has been updated to include `select EXPERIMENTAL` so that
developers can enable warnings when experimental features are enabled.

Signed-off-by: Torsten Rasmussen <Torsten.Rasmussen@nordicsemi.no>
2021-10-25 10:46:48 +02:00
Marcin Niestroj 47e10a6830 drivers: lora: fix RtcGetCalendarTime()
Uptime in milliseconds is assigned to uint32_t variable, which results
in integer overflow after enough time has expired. Additionally
milliseconds part (which should be 0-999) is assigned directly from
uptime, without subtracting full seconds.

Fix both issues by using int64_t variable and calculating milliseconds
with modulo.

Signed-off-by: Marcin Niestroj <m.niestroj@grinn-global.com>
2021-10-05 19:24:58 -04:00
Fabio Baltieri 23fbb19ed5 drivers: sx126x_stm32wl: clear the radio IRQ before reenabling it
The stm32wl version of the sx126x driver disables the NVIC interrupt in
the radio isr to prevent retriggering while the event gets handled in
the workqueue. Since the interrupt condition is still present while the
line is disabled, the interrupt pending bit remains set in the NVIC, so
after the handler finished, when irq_enable() gets called, the interrupt
fires immediately again with no status bit set in the radio registers.

Apart from the no-op interrupt, this has the side effect of bringing
the radio out of sleep as soon as the interrupt bit are read and
cleared, which increases the idle state power consumption.

Adding a NVIC_ClearPendingIRQ() before irq_enable() seems to fix the
problem. It should not cause any issue with missing interrupt events, as
if there are pending bit on the Radio, the NVIC pending bit would be
re-set immediately.

Signed-off-by: Fabio Baltieri <fabio.baltieri@gmail.com>
2021-09-22 21:10:05 -05:00
Marcin Niestroj c910138ade drivers: lora: sx12xx: fix atomic include
Include sys/atomic.h instead of sys/atomic_builtin.h, so that build for
platforms with non-default atomic implementation will still succeed.

Signed-off-by: Marcin Niestroj <m.niestroj@emb.dev>
2021-08-20 11:32:45 -04:00
Jordan Yates 3cec3ac2a4 lora: detect failed transmissions
Detect failed transmissions using `lora_send` by only waiting for some
multiple of the actual on-air time before giving up.

We use this instead of the inbuilt TxTimeout functionality because the
value of this timeout is set by `lora_configure`, and therefore doesn't
change with different packet lengths. This is a limitation of the
underlying `RadioSend` function, not the Zephyr driver.

Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
2021-08-18 05:16:46 -04:00
Jordan Yates 93b4dbcc19 lora: `lora_send` blocks until completion
Change the behaviour of `lora_send` to block until the transmission
completes. The current asynchronous behaviour is exposed through the
new function `lora_send_async`. This naming convention brings LoRa in
line with other asynchronous subsystems.

Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
2021-08-18 05:16:46 -04:00
Fabio Baltieri d6731819e3 drivers: sx126x_standalone: use the correct dev_data for dio1 callback
The current code uses dev->data for finding back the instance data
structure in various places, but for the dio1 irq callback, dev refers
to the GPIO device node (not the LORA radio one), so dev->data returns a
pointer to the GPIO data rather than a "struct sx126x_data".

Fix that by using CONTAINER_OF to find back the correct structure from
the callback pointer.

The bug was introduced in:

  74efaa920a drivers: sx126x: refactor few functions for stm32wl support

Signed-off-by: Fabio Baltieri <fabio.baltieri@gmail.com>
2021-08-16 04:57:41 -04:00
Jordan Yates d2363be091 lora: sx12xx_common: thread safe RX
Make the `lora_recv` operation thread safe by copying memory directly
in the callback instead of deferring copying to the original caller.

To ensure pointer validity, this requires performing operations "inside"
the `modem_release` context.

Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
2021-08-08 04:19:27 -04:00
Jordan Yates 586a4bfc7d lora: sx12xx_common: thread-safe API
Ensure that the modem is not asked to perform new operations before the
previous operation completes.

An atomic variable is used instead of a mutex as multiple threads need
to release the lock. A semaphore isn't used as there is no indication
whether `k_sem_give` gives the semaphore or not, which is required to
determine if `Radio.Sleep()` should be run.

`Radio.Sleep()` is only ever run by the context that successfully
releases the modem usage, to guard against double calls.

Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
2021-08-08 04:19:27 -04:00
Jordan Yates 4b370fb5e3 lora: sx12xx_common: transition to k_poll_signal
Transition the receive synchronisation method from a single driver
semaphore to a function specific k_poll_signal. This is required to
allow the modem to be released without introducing race conditions on
the signalling mechanism.

Without this change, the RX can either be signalled before the modem is
released, unblocking the calling thread before the modem is put back to
sleep, or after the modem is released, in which case a second thread
could start using the semaphore before the original thread is signalled.

Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
2021-08-08 04:19:27 -04:00
Jordan Yates f397361c8a lora: sx12xx_common: sleep after RX timeout
If no packet was received in the provided timeout, manually transition
to sleep mode. If a packet was received, the rx_done callback
automatically transitions the modem to sleep.

Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
2021-08-08 04:19:27 -04:00
Fabio Baltieri accb71ec9e drivers: sx126x: add support for the stm32wl chip
Add support for the STM32WL integrated radio, based on the sx1262. The
STM32WL implementation does not use any GPIO, and the signals are routed
to other units of the SoC and accessed with the ST HAL.

Signed-off-by: Fabio Baltieri <fabio.baltieri@gmail.com>
2021-07-15 21:30:28 -04:00
Fabio Baltieri 2916133b06 drivers: sx126x_standalone: convert to gpio_dt_spec APIs
Convert the standalone part of the sx126x driver to the new gpio_dt_spec
APIs. This allows moving the specific GPIOs in that part of the driver
and out of struct sx126x_data.

Signed-off-by: Fabio Baltieri <fabio.baltieri@gmail.com>
2021-07-15 21:30:28 -04:00
Fabio Baltieri 74efaa920a drivers: sx126x: refactor few functions for stm32wl support
Move few functions around in preparation of adding the STM32WL variant
of the driver.

Signed-off-by: Fabio Baltieri <fabio.baltieri@gmail.com>
2021-07-15 21:30:28 -04:00
Ilya Tagunov 63dbc1f4e0 drivers: lora: sx126x: cosmetic fixes
Fix minor cosmetic issues discovered while updating driver.

Signed-off-by: Ilya Tagunov <tagunil@gmail.com>
2021-06-18 11:22:03 +02:00
Ilya Tagunov 05957b66cb lorawan: update LoRaMac-node and move CMakeLists.txt to the main repo
Update the LoRaMac-node library to the last stable release and fix
the Zephyr glue code to match it. Move CMakeLists.txt to the main
Zephyr repository to simplify loramac-node module maintenance.

Signed-off-by: Ilya Tagunov <tagunil@gmail.com>
2021-06-18 11:22:03 +02:00
Gerard Marull-Paretas 496cdac71d drivers: lora: remove usage of device_pm_control_nop
device_pm_control_nop is now deprecated in favour of NULL.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2021-04-28 11:25:42 -04:00
James Harris b10428163a kernel: sem: add K_SEM_MAX_LIMIT
Currently there is no way to distinguish between a caller
explicitly asking for a semaphore with a limit that
happens to be `UINT_MAX` and a semaphore that just
has a limit "as large as possible".

Add `K_SEM_MAX_LIMIT`, currently defined to `UINT_MAX`, and akin
to `K_FOREVER` versus just passing some very large wait time.

In addition, the `k_sem_*` APIs were type-confused, where
the internal data structure was `uint32_t`, but the APIs took
and returned `unsigned int`. This changes the underlying data
structure to also use `unsigned int`, as changing the APIs
would be a (potentially) breaking change.

These changes are backwards-compatible, but it is strongly suggested
to take a quick scan for `k_sem_init` and `K_SEM_DEFINE` calls with
`UINT_MAX` (or `UINT32_MAX`) and replace them with `K_SEM_MAX_LIMIT`
where appropriate.

Signed-off-by: James Harris <james.harris@intel.com>
2021-03-05 08:13:53 -06:00
Ilya Tagunov 62d4c6947c drivers: lora: sx127x: rework SX1276 driver to add SX1272 support
SX1272 chip is very similar to the already supported SX1276;
unfortunately, Semtech provides a completely separate driver
for this one. This commit marks an attempt to avoid code
duplication by means of some not-very-pretty ifdefery.

Signed-off-by: Ilya Tagunov <tagunil@gmail.com>
2021-02-28 16:36:35 -05:00
Jordan Yates e756ad6c0f lora: sx126x: disable DIO1 in sleep mode
Disable the DIO1 interrupt when the sx126x modem is in sleep mode.
On nRF hardware, this lets the `GPIOTE` hardware be switched off, saving
15uA.

Fixes #31569.

Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
2021-02-10 08:55:15 -05:00
Jordan Yates 6776535322 lora: sx126x: update modem state on transitions
Update the global state of the modem on calls to `SX126xReset` and
`SX126xWakeup`. This stops `SX126xCheckDeviceReady` sending spurious SPI
commands on every transieve before `SX126xSetOperatingMode` is called.

Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
2021-02-10 08:55:15 -05:00
Jordan Yates fb71ad5bb6 lora: sx12xx_common: force sleep on boot
Force sx12xx modems into sleep mode on boot. This is the default state
of the modem when not being used due to the TX/RX complete callbacks.
This reduces current consumption by 600uA before the modems are used
for the first time.

As sleep is the normal state after TX/RX when using the LoRa API, it is
garaunteed that this will not change the behaviour of application code.
LoRaWAN starts by calling Radio.Init(), which resets the modem, so any
sleep configuration we do here is discarded.

Fixes #31567.

Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
2021-01-31 10:20:53 -05:00
Kumar Gala f19e36f4a6 drivers: lora: Convert drivers to new DT device macros
Convert lora drivers from:

        DEVICE_AND_API_INIT -> DEVICE_DT_INST_DEFINE

Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2020-12-15 15:17:59 -06:00
Kuba Sanak 52a8020398 drivers: lora: Add CheckRfFrequency callback to SX1276 driver
CheckRfFrequency was not assigned correctly to the Radio. This lead
to the system hang when the loramac library tries to call a relevant
Radio method when processing a join-accept message

Signed-off-by: Kuba Sanak <contact@kuba.fyi>
Signed-off-by: Manivannan Sadhasivam <mani@kernel.org>
2020-10-08 12:15:38 +02:00
Manivannan Sadhasivam 4d1d1bb330 drivers: lora: Add missing function definitions & callbacks for LoRaWAN
For preparing the radio drivers (specifically SX1276) for LoRaWAN
support, let's add missing function definitions and callbacks required
by the stack.

Signed-off-by: Manivannan Sadhasivam <mani@kernel.org>
2020-10-08 12:15:38 +02:00
Manivannan Sadhasivam 47a262f28b drivers: lora: hal_common: Offload calling TimerIrqHandler to work queue
Since TimerIrqHandler() API does SPI transactions, it is not advised to
call from an IRQ context. Hence, offload it to a work queue.

Reported-by: Andreas Sandberg <andreas@sandberg.pp.se>
Signed-off-by: Manivannan Sadhasivam <mani@kernel.org>
2020-10-08 12:15:38 +02:00
Manivannan Sadhasivam 0668e2b795 drivers: lora: Only create loramac-node library if not done before
The loramac-node library definition will also be created by
'subsys/lorawan' for the LoRaWAN support. Hence, just add the source
files if the previous declaration of the library is found else create
a new one.

Signed-off-by: Manivannan Sadhasivam <mani@kernel.org>
2020-10-08 12:15:38 +02:00
Andreas Sandberg 78cc7df60c drivers: lora: sx126x: Support software-managed RF switches
The SX126x supports using DIO2 to control an TX switch, but only if
the switch can be operated using a single control signal. Add support
for RF switches that are wired to a GPIO instead of the radio chip
itself. This makes it possible to use RF switches that require two
control signals (one for the RX port and one for the TX port) by
wiring them to two GPIOs on the MCU.

Signed-off-by: Andreas Sandberg <andreas@sandberg.pp.se>
2020-09-04 12:36:53 +02:00
Tomasz Bursztyka e18fcbba5a device: Const-ify all device driver instance pointers
Now that device_api attribute is unmodified at runtime, as well as all
the other attributes, it is possible to switch all device driver
instance to be constant.

A coccinelle rule is used for this:

@r_const_dev_1
  disable optional_qualifier
@
@@
-struct device *
+const struct device *

@r_const_dev_2
 disable optional_qualifier
@
@@
-struct device * const
+const struct device *

Fixes #27399

Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
2020-09-02 13:48:13 +02:00
Andreas Sandberg b5bff2cf48 drivers: lora: sx126x: Use the new pin helpers
User the new pin configuration helpers to reduce boilerplate.

Signed-off-by: Andreas Sandberg <andreas@sandberg.pp.se>
2020-07-13 13:11:59 +02:00
Andreas Sandberg 2ebd80cd0e drivers: lora: sx126x: Don't initialize control pin to active
The antenna control GPIO was incorrectly initialized to active. This
is unnecessary since the HAL will activate the antenna switch when the
chip enters an active state.

Signed-off-by: Andreas Sandberg <andreas@sandberg.pp.se>
2020-07-13 13:11:59 +02:00
Andreas Sandberg 23e4a8b64e drivers: lora: Move sx1276 pin helpers to sx12xx common
The new sx1276 pin configuration helpers can be used by the sx126x
driver as well. Move them to sx126xx_common.{c,h} to facilitate reuse.

Signed-off-by: Andreas Sandberg <andreas@sandberg.pp.se>
2020-07-13 13:11:59 +02:00
Marcin Niestroj 559cf02107 drivers: lora: sx1276: configure reset gpio only once
Reset gpio is configured during driver initialization. There is no
reason to do it repeat that process later, so replace
gpio_pin_configure() with gpio_pin_set().

Signed-off-by: Marcin Niestroj <m.niestroj@grinn-global.com>
2020-07-09 13:47:42 +02:00
Marcin Niestroj 71bdb35191 drivers: lora: sx1276: rework gpio configuration
Introduce sx1276_configure_pin() helper macro, which hides lots of
boilerplate preprocessor and C code. Doing so allows to uncover
inconsistent gpio initialization flow and prevent such in future. Also
add error log whenever gpio_pin_configure() fails.

It seems like output of gpio_pin_configure() for several
gpios (antenna_enable, rfi_enable, rfo_enable) was assigned to variable,
but its values was never checked. Return value from gpio_pin_configure()
of tcxo_power gpio was even not catched. The only output gpio
configuration that was handled properly was actually reset gpio.

Fix that inconsistent behavior by always checking return value of
sx1276_configure_pin().

Signed-off-by: Marcin Niestroj <m.niestroj@grinn-global.com>
2020-07-09 13:47:42 +02:00