1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-17 05:12:57 +01:00

drivers/sx127x: migrate to ztimer

This commit is contained in:
Alexandre Abadie 2020-07-17 15:35:29 +02:00
parent 6bf6b6be6c
commit 6199ada6b5
No known key found for this signature in database
GPG Key ID: 1C919A403CAE1405
6 changed files with 41 additions and 25 deletions

View File

@ -60,7 +60,8 @@
#ifndef SX127X_H
#define SX127X_H
#include "xtimer.h"
#include "timex.h"
#include "ztimer.h"
#include "net/netdev.h"
#include "periph/gpio.h"
#include "periph/spi.h"
@ -78,9 +79,9 @@ extern "C" {
#define SX127X_HF_CHANNEL_DEFAULT (868000000UL) /**< Use to calibrate RX chain for LF and HF bands */
#define SX127X_RF_MID_BAND_THRESH (525000000UL) /**< Mid-band threshold */
#define SX127X_XTAL_FREQ (32000000UL) /**< Internal oscillator frequency, 32MHz */
#define SX127X_RADIO_WAKEUP_TIME (1000U) /**< In microseconds [us] */
#define SX127X_RADIO_WAKEUP_TIME (1U) /**< In milliseconds [ms] */
#define SX127X_TX_TIMEOUT_DEFAULT (1000U * 1000U * 30UL) /**< TX timeout, 30s */
#define SX127X_TX_TIMEOUT_DEFAULT (30 * MS_PER_SEC) /**< TX timeout, 30s */
#define SX127X_RX_SINGLE (false) /**< Single byte receive mode => continuous by default */
#define SX127X_RX_BUFFER_SIZE (256) /**< RX buffer size */
#define SX127X_RADIO_TX_POWER (14U) /**< Radio power in dBm */
@ -183,8 +184,8 @@ typedef struct {
uint8_t coderate; /**< Error coding rate */
uint8_t freq_hop_period; /**< Frequency hop period */
uint8_t flags; /**< Boolean flags */
uint32_t rx_timeout; /**< RX timeout in microseconds */
uint32_t tx_timeout; /**< TX timeout in microseconds */
uint32_t rx_timeout; /**< RX timeout in milliseconds */
uint32_t tx_timeout; /**< TX timeout in milliseconds */
} sx127x_lora_settings_t;
/**
@ -202,8 +203,8 @@ typedef struct {
*/
typedef struct {
/* Data that will be passed to events handler in application */
xtimer_t tx_timeout_timer; /**< TX operation timeout timer */
xtimer_t rx_timeout_timer; /**< RX operation timeout timer */
ztimer_t tx_timeout_timer; /**< TX operation timeout timer */
ztimer_t rx_timeout_timer; /**< RX operation timeout timer */
uint32_t last_channel; /**< Last channel in frequency hopping sequence */
bool is_last_cad_success; /**< Sign of success of last CAD operation (activity detected) */
} sx127x_internal_t;

View File

@ -3,7 +3,15 @@ FEATURES_REQUIRED += periph_gpio_irq
FEATURES_REQUIRED += periph_spi
FEATURES_OPTIONAL += periph_spi_gpio_mode
USEMODULE += iolist
USEMODULE += xtimer
USEMODULE += ztimer_usec
USEMODULE += ztimer_msec
# If RTT feature is available use the RTT backend of ztimer
FEATURES_OPTIONAL += periph_rtt
ifneq (,$(filter periph_rtt,$(FEATURES_USED)))
USEMODULE += ztimer_periph_rtt
endif
USEMODULE += netif
USEMODULE += lora

View File

@ -24,7 +24,8 @@
#include <string.h>
#include <stdlib.h>
#include "xtimer.h"
#include "timex.h"
#include "ztimer.h"
#include "thread.h"
#include "periph/gpio.h"
@ -110,12 +111,12 @@ int sx127x_reset(const sx127x_t *dev)
/* set reset pin to the state that triggers manual reset */
gpio_write(dev->params.reset_pin, SX127X_POR_ACTIVE_LOGIC_LEVEL);
xtimer_usleep(SX127X_MANUAL_RESET_SIGNAL_LEN_US);
ztimer_sleep(ZTIMER_USEC, SX127X_MANUAL_RESET_SIGNAL_LEN_US);
/* Put reset pin in High-Z */
gpio_init(dev->params.reset_pin, GPIO_IN);
xtimer_usleep(SX127X_MANUAL_RESET_WAIT_FOR_READY_US);
ztimer_sleep(ZTIMER_USEC, SX127X_MANUAL_RESET_WAIT_FOR_READY_US);
}
return 0;
@ -147,7 +148,7 @@ int sx127x_init(sx127x_t *dev)
}
/* wait for the device to become ready */
xtimer_usleep(SX127X_POR_WAIT_FOR_READY_US);
ztimer_sleep(ZTIMER_USEC, SX127X_POR_WAIT_FOR_READY_US);
sx127x_reset(dev);
@ -205,7 +206,7 @@ uint32_t sx127x_random(sx127x_t *dev)
sx127x_set_op_mode(dev, SX127X_RF_OPMODE_RECEIVER);
for (unsigned i = 0; i < 32; i++) {
xtimer_usleep(1000); /* wait for the chaos */
ztimer_sleep(ZTIMER_MSEC, 1); /* wait one millisecond */
/* Non-filtered RSSI value reading. Only takes the LSB value */
rnd |= ((uint32_t) sx127x_reg_read(dev, SX127X_REG_LR_RSSIWIDEBAND) & 0x01) << i;

View File

@ -26,6 +26,8 @@
#include <stdbool.h>
#include <inttypes.h>
#include "ztimer.h"
#include "net/lora.h"
#include "sx127x.h"
@ -200,8 +202,8 @@ void sx127x_set_sleep(sx127x_t *dev)
DEBUG("[sx127x] Set sleep\n");
/* Disable running timers */
xtimer_remove(&dev->_internal.tx_timeout_timer);
xtimer_remove(&dev->_internal.rx_timeout_timer);
ztimer_remove(ZTIMER_MSEC, &dev->_internal.tx_timeout_timer);
ztimer_remove(ZTIMER_MSEC, &dev->_internal.rx_timeout_timer);
/* Put chip into sleep */
sx127x_set_op_mode(dev, SX127X_RF_OPMODE_SLEEP);
@ -213,8 +215,8 @@ void sx127x_set_standby(sx127x_t *dev)
DEBUG("[sx127x] Set standby\n");
/* Disable running timers */
xtimer_remove(&dev->_internal.tx_timeout_timer);
xtimer_remove(&dev->_internal.rx_timeout_timer);
ztimer_remove(ZTIMER_MSEC, &dev->_internal.tx_timeout_timer);
ztimer_remove(ZTIMER_MSEC, &dev->_internal.rx_timeout_timer);
sx127x_set_op_mode(dev, SX127X_RF_OPMODE_STANDBY);
sx127x_set_state(dev, SX127X_RF_IDLE);
@ -322,7 +324,7 @@ void sx127x_set_rx(sx127x_t *dev)
sx127x_set_state(dev, SX127X_RF_RX_RUNNING);
if (dev->settings.lora.rx_timeout != 0) {
xtimer_set(&(dev->_internal.rx_timeout_timer), dev->settings.lora.rx_timeout);
ztimer_set(ZTIMER_MSEC, &(dev->_internal.rx_timeout_timer), dev->settings.lora.rx_timeout);
}
@ -395,7 +397,7 @@ void sx127x_set_tx(sx127x_t *dev)
/* Start TX timeout timer */
if (dev->settings.lora.tx_timeout != 0) {
xtimer_set(&(dev->_internal.tx_timeout_timer), dev->settings.lora.tx_timeout);
ztimer_set(ZTIMER_MSEC, &(dev->_internal.tx_timeout_timer), dev->settings.lora.tx_timeout);
}
/* Put chip into transfer mode */

View File

@ -25,6 +25,8 @@
#include <stdbool.h>
#include <inttypes.h>
#include "ztimer.h"
#include "net/lora.h"
#include "sx127x.h"
@ -231,7 +233,7 @@ bool sx127x_is_channel_free(sx127x_t *dev, uint32_t freq, int16_t rssi_threshold
sx127x_set_channel(dev, freq);
sx127x_set_op_mode(dev, SX127X_RF_OPMODE_RECEIVER);
xtimer_usleep(1000); /* wait 1 millisecond */
ztimer_sleep(ZTIMER_MSEC, 1); /* wait 1 millisecond */
rssi = sx127x_read_rssi(dev);
sx127x_set_sleep(dev);

View File

@ -21,6 +21,8 @@
#include <string.h>
#include <errno.h>
#include "ztimer.h"
#include "net/netopt.h"
#include "net/netdev.h"
#include "net/netdev/lora.h"
@ -77,7 +79,7 @@ static int _send(netdev_t *netdev, const iolist_t *iolist)
* So wake up the chip */
if (sx127x_get_op_mode(dev) == SX127X_RF_OPMODE_SLEEP) {
sx127x_set_standby(dev);
xtimer_usleep(SX127X_RADIO_WAKEUP_TIME); /* wait for chip wake up */
ztimer_sleep(ZTIMER_MSEC, SX127X_RADIO_WAKEUP_TIME); /* wait for chip wake up */
}
/* Write payload buffer */
@ -122,7 +124,7 @@ static int _recv(netdev_t *netdev, void *buf, size_t len, void *info)
sx127x_set_state(dev, SX127X_RF_IDLE);
}
xtimer_remove(&dev->_internal.rx_timeout_timer);
ztimer_remove(ZTIMER_MSEC, &dev->_internal.rx_timeout_timer);
netdev->event_callback(netdev, NETDEV_EVENT_CRC_ERROR);
return -EBADMSG;
}
@ -180,7 +182,7 @@ static int _recv(netdev_t *netdev, void *buf, size_t len, void *info)
sx127x_set_state(dev, SX127X_RF_IDLE);
}
xtimer_remove(&dev->_internal.rx_timeout_timer);
ztimer_remove(ZTIMER_MSEC, &dev->_internal.rx_timeout_timer);
/* Read the last packet from FIFO */
uint8_t last_rx_addr = sx127x_reg_read(dev, SX127X_REG_LR_FIFORXCURRENTADDR);
sx127x_reg_write(dev, SX127X_REG_LR_FIFOADDRPTR, last_rx_addr);
@ -565,7 +567,7 @@ void _on_dio0_irq(void *arg)
netdev->event_callback(netdev, NETDEV_EVENT_RX_COMPLETE);
break;
case SX127X_RF_TX_RUNNING:
xtimer_remove(&dev->_internal.tx_timeout_timer);
ztimer_remove(ZTIMER_MSEC, &dev->_internal.tx_timeout_timer);
switch (dev->settings.modem) {
case SX127X_MODEM_LORA:
/* Clear IRQ */
@ -602,7 +604,7 @@ void _on_dio1_irq(void *arg)
/* todo */
break;
case SX127X_MODEM_LORA:
xtimer_remove(&dev->_internal.rx_timeout_timer);
ztimer_remove(ZTIMER_MSEC, &dev->_internal.rx_timeout_timer);
/* Clear Irq */
sx127x_reg_write(dev, SX127X_REG_LR_IRQFLAGS, SX127X_RF_LORA_IRQFLAGS_RXTIMEOUT);
sx127x_set_state(dev, SX127X_RF_IDLE);