drivers: lora: Get rid of counter support

The RTC/Counter implementation doesn't fit for the upcoming LoRaWAN
as most of the Counter drivers in Zephyr works with 1s granularity
which is not enough for LoRaWAN stack. So, k_timer calls are used in
place of Counter's alarm and k_uptime_get() APIs are used in place of
Counter's time keeping.

While at it, lets also fixup the broken alarm implementation.

Signed-off-by: Manivannan Sadhasivam <mani@kernel.org>
This commit is contained in:
Manivannan Sadhasivam 2020-04-23 22:48:33 +05:30 committed by Carles Cufí
commit e177ab6c42
2 changed files with 40 additions and 38 deletions

View file

@ -10,7 +10,6 @@ menuconfig LORA_SX1276
select HAS_SEMTECH_LORAMAC
select HAS_SEMTECH_SX1276
depends on SPI
depends on COUNTER
help
Enable LoRa driver for Semtech SX1276.

View file

@ -6,13 +6,14 @@
#define DT_DRV_COMPAT semtech_sx1276
#include <drivers/counter.h>
#include <drivers/gpio.h>
#include <drivers/lora.h>
#include <drivers/spi.h>
#include <zephyr.h>
/* LoRaMac-node specific includes */
#include <sx1276/sx1276.h>
#include <timer.h>
#define LOG_LEVEL CONFIG_LORA_LOG_LEVEL
#include <logging/log.h>
@ -26,6 +27,7 @@ LOG_MODULE_REGISTER(sx1276);
#define SX1276_REG_PA_DAC 0x4d
#define SX1276_REG_VERSION 0x42
static u32_t saved_time;
extern DioIrqHandler *DioIrq[];
struct sx1276_dio {
@ -50,13 +52,13 @@ static const struct sx1276_dio sx1276_dios[] = { SX1276_DIO_GPIO_INIT(0) };
#define SX1276_MAX_DIO ARRAY_SIZE(sx1276_dios)
struct sx1276_data {
struct device *counter;
struct device *reset;
struct device *spi;
struct spi_config spi_cfg;
struct device *dio_dev[SX1276_MAX_DIO];
struct k_work dio_work[SX1276_MAX_DIO];
struct k_sem data_sem;
struct k_timer timer;
RadioEvents_t sx1276_event;
u8_t *rx_buf;
u8_t rx_len;
@ -70,11 +72,6 @@ bool SX1276CheckRfFrequency(uint32_t frequency)
return true;
}
void RtcStopAlarm(void)
{
counter_stop(dev_data.counter);
}
void SX1276SetAntSwLowPower(bool status)
{
/* TODO */
@ -112,51 +109,61 @@ void BoardCriticalSectionEnd(uint32_t *mask)
irq_unlock(*mask);
}
uint32_t RtcGetTimerElapsedTime(void)
u32_t RtcGetTimerValue(void)
{
u32_t ticks;
int err;
return k_uptime_get_32();
}
err = counter_get_value(dev_data.counter, &ticks);
if (err) {
LOG_ERR("Failed to read counter value (err %d)", err);
return 0;
}
return ticks;
u32_t RtcGetTimerElapsedTime(void)
{
return (k_uptime_get_32() - saved_time);
}
u32_t RtcGetMinimumTimeout(void)
{
/* TODO: Get this value from counter driver */
return 3;
return 1;
}
void RtcSetAlarm(uint32_t timeout)
void RtcStopAlarm(void)
{
struct counter_alarm_cfg alarm_cfg;
alarm_cfg.flags = 0;
alarm_cfg.ticks = timeout;
counter_set_channel_alarm(dev_data.counter, 0, &alarm_cfg);
k_timer_stop(&dev_data.timer);
}
uint32_t RtcSetTimerContext(void)
static void timer_callback(struct k_timer *_timer)
{
return 0;
ARG_UNUSED(_timer);
TimerIrqHandler();
}
uint32_t RtcMs2Tick(uint32_t milliseconds)
void RtcSetAlarm(u32_t timeout)
{
return counter_us_to_ticks(dev_data.counter, (milliseconds / 1000));
k_timer_start(&dev_data.timer, timeout, K_NO_WAIT);
}
void DelayMsMcu(uint32_t ms)
u32_t RtcSetTimerContext(void)
{
saved_time = k_uptime_get_32();
return saved_time;
}
/* For us, 1 tick = 1 milli second. So no need to do any conversion here */
u32_t RtcGetTimerContext(void)
{
return saved_time;
}
void DelayMsMcu(u32_t ms)
{
k_sleep(ms);
}
u32_t RtcMs2Tick(uint32_t milliseconds)
{
return milliseconds;
}
static void sx1276_dio_work_handle(struct k_work *work)
{
int dio = work - dev_data.dio_work;
@ -514,14 +521,10 @@ static int sx1276_lora_init(struct device *dev)
return -EIO;
}
dev_data.counter = device_get_binding(DT_RTC_0_NAME);
if (!dev_data.counter) {
LOG_ERR("Cannot get pointer to %s device", DT_RTC_0_NAME);
return -EIO;
}
k_sem_init(&dev_data.data_sem, 0, UINT_MAX);
k_timer_init(&dev_data.timer, timer_callback, NULL);
dev_data.sx1276_event.TxDone = sx1276_tx_done;
dev_data.sx1276_event.RxDone = sx1276_rx_done;
Radio.Init(&dev_data.sx1276_event);