From 69fac5c49863d071a46a33ca95c4016ce1df2fbd Mon Sep 17 00:00:00 2001 From: Andreas Sandberg Date: Mon, 4 May 2020 18:23:59 +0100 Subject: [PATCH] drivers: lora: Move board support to a separate file The implementation of the board support routines is shared between all LoRa drivers that use LoRaMAC-Node. Move them from the SX1276 implementation to a separate source file to facilitate reuse. Make this source file conditional on CONFIG_HAS_SEMTECH_RADIO_DRIVERS since it will be used by all LoRaMAC-Node-based drivers. Signed-off-by: Andreas Sandberg --- drivers/lora/CMakeLists.txt | 1 + drivers/lora/hal_common.c | 83 +++++++++++++++++++++++++++++++++++++ drivers/lora/sx1276.c | 75 --------------------------------- 3 files changed, 84 insertions(+), 75 deletions(-) create mode 100644 drivers/lora/hal_common.c diff --git a/drivers/lora/CMakeLists.txt b/drivers/lora/CMakeLists.txt index 1a359072b4f..ea4c43e9be0 100644 --- a/drivers/lora/CMakeLists.txt +++ b/drivers/lora/CMakeLists.txt @@ -3,4 +3,5 @@ zephyr_library_named(loramac-node) zephyr_library_sources_ifdef(CONFIG_LORA_SHELL shell.c) +zephyr_library_sources_ifdef(CONFIG_HAS_SEMTECH_RADIO_DRIVERS hal_common.c) zephyr_library_sources_ifdef(CONFIG_LORA_SX1276 sx1276.c) diff --git a/drivers/lora/hal_common.c b/drivers/lora/hal_common.c new file mode 100644 index 00000000000..a909bf2c615 --- /dev/null +++ b/drivers/lora/hal_common.c @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2019 Manivannan Sadhasivam + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +#include + +static uint32_t saved_time; + +static void timer_callback(struct k_timer *_timer) +{ + ARG_UNUSED(_timer); + + TimerIrqHandler(); +} + +K_TIMER_DEFINE(lora_timer, timer_callback, NULL); + +uint32_t RtcGetTimerValue(void) +{ + return k_uptime_get_32(); +} + +uint32_t RtcGetTimerElapsedTime(void) +{ + return (k_uptime_get_32() - saved_time); +} + +uint32_t RtcGetMinimumTimeout(void) +{ + return 1; +} + +void RtcStopAlarm(void) +{ + k_timer_stop(&lora_timer); +} + +void RtcSetAlarm(uint32_t timeout) +{ + k_timer_start(&lora_timer, K_MSEC(timeout), K_NO_WAIT); +} + +uint32_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 */ +uint32_t RtcGetTimerContext(void) +{ + return saved_time; +} + +void DelayMsMcu(uint32_t ms) +{ + k_sleep(K_MSEC(ms)); +} + +uint32_t RtcMs2Tick(uint32_t milliseconds) +{ + return milliseconds; +} + +uint32_t RtcTick2Ms(uint32_t tick) +{ + return tick; +} + +void BoardCriticalSectionBegin(uint32_t *mask) +{ + *mask = irq_lock(); +} + +void BoardCriticalSectionEnd(uint32_t *mask) +{ + irq_unlock(*mask); +} diff --git a/drivers/lora/sx1276.c b/drivers/lora/sx1276.c index 45fa1d19071..af2355f49db 100644 --- a/drivers/lora/sx1276.c +++ b/drivers/lora/sx1276.c @@ -13,7 +13,6 @@ /* LoRaMac-node specific includes */ #include -#include #define LOG_LEVEL CONFIG_LORA_LOG_LEVEL #include @@ -27,7 +26,6 @@ LOG_MODULE_REGISTER(sx1276); #define SX1276_REG_PA_DAC 0x4d #define SX1276_REG_VERSION 0x42 -static uint32_t saved_time; extern DioIrqHandler *DioIrq[]; struct sx1276_dio { @@ -58,7 +56,6 @@ struct sx1276_data { 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; uint8_t *rx_buf; uint8_t rx_len; @@ -99,76 +96,6 @@ void SX1276Reset(void) k_sleep(K_MSEC(6)); } -void BoardCriticalSectionBegin(uint32_t *mask) -{ - *mask = irq_lock(); -} - -void BoardCriticalSectionEnd(uint32_t *mask) -{ - irq_unlock(*mask); -} - -uint32_t RtcGetTimerValue(void) -{ - return k_uptime_get_32(); -} - -uint32_t RtcGetTimerElapsedTime(void) -{ - return (k_uptime_get_32() - saved_time); -} - -uint32_t RtcGetMinimumTimeout(void) -{ - return 1; -} - -void RtcStopAlarm(void) -{ - k_timer_stop(&dev_data.timer); -} - -static void timer_callback(struct k_timer *_timer) -{ - ARG_UNUSED(_timer); - - TimerIrqHandler(); -} - -void RtcSetAlarm(uint32_t timeout) -{ - k_timer_start(&dev_data.timer, K_MSEC(timeout), K_NO_WAIT); -} - -uint32_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 */ -uint32_t RtcGetTimerContext(void) -{ - return saved_time; -} - -void DelayMsMcu(uint32_t ms) -{ - k_sleep(K_MSEC(ms)); -} - -uint32_t RtcMs2Tick(uint32_t milliseconds) -{ - return milliseconds; -} - -uint32_t RtcTick2Ms(uint32_t tick) -{ - return tick; -} - static void sx1276_dio_work_handle(struct k_work *work) { int dio = work - dev_data.dio_work; @@ -531,8 +458,6 @@ static int sx1276_lora_init(struct device *dev) 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);