From 05957b66cb9d4646173b9b04a92f02cf80cab82b Mon Sep 17 00:00:00 2001 From: Ilya Tagunov Date: Thu, 3 Jun 2021 01:54:54 +0300 Subject: [PATCH] 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 --- drivers/lora/Kconfig.sx12xx | 1 - drivers/lora/sx126x.c | 5 + drivers/lora/sx127x.c | 22 ++++- modules/Kconfig | 4 +- modules/loramac-node/CMakeLists.txt | 91 +++++++++++++++++++ .../Kconfig} | 25 +++-- subsys/lorawan/lorawan.c | 19 +++- subsys/lorawan/lw_priv.c | 3 - west.yml | 2 +- 9 files changed, 149 insertions(+), 23 deletions(-) create mode 100644 modules/loramac-node/CMakeLists.txt rename modules/{Kconfig.loramac-node => loramac-node/Kconfig} (86%) diff --git a/drivers/lora/Kconfig.sx12xx b/drivers/lora/Kconfig.sx12xx index ba07747278ca7a..8ec17c08a893a7 100644 --- a/drivers/lora/Kconfig.sx12xx +++ b/drivers/lora/Kconfig.sx12xx @@ -12,7 +12,6 @@ DT_COMPAT_SEMTECH_SX1262 := semtech,sx1262 menuconfig LORA_SX12XX bool "Semtech SX-series driver" select HAS_SEMTECH_RADIO_DRIVERS - select HAS_SEMTECH_LORAMAC depends on SPI help Enable LoRa driver for Semtech SX12xx. diff --git a/drivers/lora/sx126x.c b/drivers/lora/sx126x.c index 7d376d5b29cc75..5d15b66440f65d 100644 --- a/drivers/lora/sx126x.c +++ b/drivers/lora/sx126x.c @@ -443,6 +443,11 @@ void SX126xWakeup(void) dev_data.mode = MODE_STDBY_RC; } +uint32_t SX126xGetDio1PinState(void) +{ + return gpio_pin_get(dev_data.dio1, GPIO_DIO1_PIN) > 0 ? 1U : 0U; +} + static void sx126x_dio1_irq_work_handler(struct k_work *work) { LOG_DBG("Processing DIO1 interrupt"); diff --git a/drivers/lora/sx127x.c b/drivers/lora/sx127x.c index b664e1d950236a..5cac0bab4b48e4 100644 --- a/drivers/lora/sx127x.c +++ b/drivers/lora/sx127x.c @@ -31,6 +31,7 @@ LOG_MODULE_REGISTER(sx127x, CONFIG_LORA_LOG_LEVEL); #define SX127xWriteBuffer SX1272WriteBuffer #define SX127xReadBuffer SX1272ReadBuffer #define SX127xSetRfTxPower SX1272SetRfTxPower +#define SX127xGetDio1PinState SX1272GetDio1PinState #define SX127xInit SX1272Init #define SX127xGetStatus SX1272GetStatus #define SX127xSetModem SX1272SetModem @@ -67,6 +68,7 @@ LOG_MODULE_REGISTER(sx127x, CONFIG_LORA_LOG_LEVEL); #define SX127xWriteBuffer SX1276WriteBuffer #define SX127xReadBuffer SX1276ReadBuffer #define SX127xSetRfTxPower SX1276SetRfTxPower +#define SX127xGetDio1PinState SX1276GetDio1PinState #define SX127xInit SX1276Init #define SX127xGetStatus SX1276GetStatus #define SX127xSetModem SX1276SetModem @@ -160,6 +162,9 @@ struct sx127x_dio { }; /* Helper macro that UTIL_LISTIFY can use and produces an element with comma */ +#define SX127X_DIO_GPIO_LEN(inst) \ + DT_INST_PROP_LEN(inst, dio_gpios) + #define SX127X_DIO_GPIO_ELEM(idx, inst) \ { \ DT_INST_GPIO_LABEL_BY_IDX(inst, dio_gpios, idx), \ @@ -168,7 +173,7 @@ struct sx127x_dio { }, #define SX127X_DIO_GPIO_INIT(n) \ - UTIL_LISTIFY(DT_INST_PROP_LEN(n, dio_gpios), SX127X_DIO_GPIO_ELEM, n) + UTIL_LISTIFY(SX127X_DIO_GPIO_LEN(n), SX127X_DIO_GPIO_ELEM, n) static const struct sx127x_dio sx127x_dios[] = { SX127X_DIO_GPIO_INIT(0) }; @@ -427,7 +432,7 @@ int sx127x_write(uint8_t reg_addr, uint8_t *data, uint8_t len) return sx127x_transceive(reg_addr | BIT(7), true, data, len); } -void SX127xWriteBuffer(uint16_t addr, uint8_t *buffer, uint8_t size) +void SX127xWriteBuffer(uint32_t addr, uint8_t *buffer, uint8_t size) { int ret; @@ -437,7 +442,7 @@ void SX127xWriteBuffer(uint16_t addr, uint8_t *buffer, uint8_t size) } } -void SX127xReadBuffer(uint16_t addr, uint8_t *buffer, uint8_t size) +void SX127xReadBuffer(uint32_t addr, uint8_t *buffer, uint8_t size) { int ret; @@ -511,6 +516,17 @@ void SX127xSetRfTxPower(int8_t power) } } +uint32_t SX127xGetDio1PinState(void) +{ +#if SX127X_DIO_GPIO_LEN(0) >= 2 + if (gpio_pin_get(dev_data.dio_dev[1], sx127x_dios[1].pin) > 0) { + return 1U; + } +#endif + + return 0U; +} + /* Initialize Radio driver callbacks */ const struct Radio_s Radio = { .Init = SX127xInit, diff --git a/modules/Kconfig b/modules/Kconfig index 30943f1430077c..5144af370321e8 100644 --- a/modules/Kconfig +++ b/modules/Kconfig @@ -17,7 +17,6 @@ source "modules/Kconfig.eos_s3" source "modules/Kconfig.imx" source "modules/Kconfig.infineon" source "modules/Kconfig.libmetal" -source "modules/Kconfig.loramac-node" source "modules/Kconfig.mcux" source "modules/Kconfig.microchip" source "modules/Kconfig.nuvoton" @@ -62,6 +61,9 @@ comment "Nanopb module not available." comment "Lz4 module not available." depends on !ZEPHYR_LZ4_MODULE +comment "loramac-node module not available." + depends on !ZEPHYR_LORAMAC_NODE_MODULE + # This ensures that symbols are available in Kconfig for dependency checking # and referencing, while keeping the settings themselves unavailable when the # modules are not present in the workspace diff --git a/modules/loramac-node/CMakeLists.txt b/modules/loramac-node/CMakeLists.txt new file mode 100644 index 00000000000000..71fc06661e9c4e --- /dev/null +++ b/modules/loramac-node/CMakeLists.txt @@ -0,0 +1,91 @@ +if(${CONFIG_HAS_SEMTECH_RADIO_DRIVERS}) + +set(ZEPHYR_CURRENT_LIBRARY loramac-node) + +zephyr_library_include_directories( + ${ZEPHYR_LORAMAC_NODE_MODULE_DIR}/src/boards + ${ZEPHYR_LORAMAC_NODE_MODULE_DIR}/src/system + ${ZEPHYR_LORAMAC_NODE_MODULE_DIR}/src/radio +) + +zephyr_library_sources( + ${ZEPHYR_LORAMAC_NODE_MODULE_DIR}/src/boards/mcu/utilities.c + ${ZEPHYR_LORAMAC_NODE_MODULE_DIR}/src/system/systime.c + ${ZEPHYR_LORAMAC_NODE_MODULE_DIR}/src/system/timer.c + ${ZEPHYR_LORAMAC_NODE_MODULE_DIR}/src/system/delay.c +) + +zephyr_library_sources_ifdef(CONFIG_HAS_SEMTECH_SX1272 + ${ZEPHYR_LORAMAC_NODE_MODULE_DIR}/src/radio/sx1272/sx1272.c +) +zephyr_library_sources_ifdef(CONFIG_HAS_SEMTECH_SX1276 + ${ZEPHYR_LORAMAC_NODE_MODULE_DIR}/src/radio/sx1276/sx1276.c +) +zephyr_library_sources_ifdef(CONFIG_HAS_SEMTECH_SX126X + ${ZEPHYR_LORAMAC_NODE_MODULE_DIR}/src/radio/sx126x/sx126x.c + ${ZEPHYR_LORAMAC_NODE_MODULE_DIR}/src/radio/sx126x/radio.c +) + +if(${CONFIG_HAS_SEMTECH_LORAMAC}) + zephyr_library_include_directories( + ${ZEPHYR_LORAMAC_NODE_MODULE_DIR}/src/mac + ${ZEPHYR_LORAMAC_NODE_MODULE_DIR}/src/mac/region + ) +endif() + +zephyr_library_compile_definitions_ifdef(CONFIG_HAS_SEMTECH_SOFT_SE SOFT_SE) + +zephyr_library_sources_ifdef(CONFIG_HAS_SEMTECH_SOFT_SE + ${ZEPHYR_LORAMAC_NODE_MODULE_DIR}/src/peripherals/soft-se/aes.c + ${ZEPHYR_LORAMAC_NODE_MODULE_DIR}/src/peripherals/soft-se/cmac.c + ${ZEPHYR_LORAMAC_NODE_MODULE_DIR}/src/peripherals/soft-se/soft-se.c + ${ZEPHYR_LORAMAC_NODE_MODULE_DIR}/src/peripherals/soft-se/soft-se-hal.c +) + +zephyr_library_sources_ifdef(CONFIG_HAS_SEMTECH_LORAMAC + ${ZEPHYR_LORAMAC_NODE_MODULE_DIR}/src/mac/LoRaMac.c + ${ZEPHYR_LORAMAC_NODE_MODULE_DIR}/src/mac/LoRaMacAdr.c + ${ZEPHYR_LORAMAC_NODE_MODULE_DIR}/src/mac/LoRaMacClassB.c + ${ZEPHYR_LORAMAC_NODE_MODULE_DIR}/src/mac/LoRaMacCommands.c + ${ZEPHYR_LORAMAC_NODE_MODULE_DIR}/src/mac/LoRaMacConfirmQueue.c + ${ZEPHYR_LORAMAC_NODE_MODULE_DIR}/src/mac/LoRaMacCrypto.c + ${ZEPHYR_LORAMAC_NODE_MODULE_DIR}/src/mac/LoRaMacParser.c + ${ZEPHYR_LORAMAC_NODE_MODULE_DIR}/src/mac/LoRaMacSerializer.c +) + +zephyr_library_sources_ifdef(CONFIG_HAS_SEMTECH_LORAMAC + ${ZEPHYR_LORAMAC_NODE_MODULE_DIR}/src/mac/region/Region.c + ${ZEPHYR_LORAMAC_NODE_MODULE_DIR}/src/mac/region/RegionCommon.c +) +zephyr_library_sources_ifdef(CONFIG_LORAMAC_REGION_EU868 + ${ZEPHYR_LORAMAC_NODE_MODULE_DIR}/src/mac/region/RegionEU868.c +) +zephyr_library_sources_ifdef(CONFIG_LORAMAC_REGION_US915 + ${ZEPHYR_LORAMAC_NODE_MODULE_DIR}/src/mac/region/RegionUS915.c +) +zephyr_library_sources_ifdef(CONFIG_LORAMAC_REGION_CN779 + ${ZEPHYR_LORAMAC_NODE_MODULE_DIR}/src/mac/region/RegionCN779.c +) +zephyr_library_sources_ifdef(CONFIG_LORAMAC_REGION_EU433 + ${ZEPHYR_LORAMAC_NODE_MODULE_DIR}/src/mac/region/RegionEU433.c +) +zephyr_library_sources_ifdef(CONFIG_LORAMAC_REGION_AU915 + ${ZEPHYR_LORAMAC_NODE_MODULE_DIR}/src/mac/region/RegionAU915.c +) +zephyr_library_sources_ifdef(CONFIG_LORAMAC_REGION_AS923 + ${ZEPHYR_LORAMAC_NODE_MODULE_DIR}/src/mac/region/RegionAS923.c +) +zephyr_library_sources_ifdef(CONFIG_LORAMAC_REGION_CN470 + ${ZEPHYR_LORAMAC_NODE_MODULE_DIR}/src/mac/region/RegionCN470.c +) +zephyr_library_sources_ifdef(CONFIG_LORAMAC_REGION_KR920 + ${ZEPHYR_LORAMAC_NODE_MODULE_DIR}/src/mac/region/RegionKR920.c +) +zephyr_library_sources_ifdef(CONFIG_LORAMAC_REGION_IN865 + ${ZEPHYR_LORAMAC_NODE_MODULE_DIR}/src/mac/region/RegionIN865.c +) +zephyr_library_sources_ifdef(CONFIG_LORAMAC_REGION_RU864 + ${ZEPHYR_LORAMAC_NODE_MODULE_DIR}/src/mac/region/RegionRU864.c +) + +endif() diff --git a/modules/Kconfig.loramac-node b/modules/loramac-node/Kconfig similarity index 86% rename from modules/Kconfig.loramac-node rename to modules/loramac-node/Kconfig index 9cdcfee40745bf..e4c2eaaed352ec 100644 --- a/modules/Kconfig.loramac-node +++ b/modules/loramac-node/Kconfig @@ -4,16 +4,8 @@ # SPDX-License-Identifier: Apache-2.0 # -config HAS_SEMTECH_LORAMAC - bool "Semtech LoRaMac Stack" - help - This option enables the use of Semtech's LoRaMac stack - -config HAS_SEMTECH_SOFT_SE - bool "Semtech Secure Element software implementation" - help - This option enables the use of Semtech's Secure Element - software implementation +config ZEPHYR_LORAMAC_NODE_MODULE + bool config HAS_SEMTECH_RADIO_DRIVERS bool "Semtech LoRa Radio Drivers" @@ -31,3 +23,16 @@ config HAS_SEMTECH_SX1276 config HAS_SEMTECH_SX126X bool depends on HAS_SEMTECH_RADIO_DRIVERS + +config HAS_SEMTECH_LORAMAC + bool "Semtech LoRaMac Stack" + depends on HAS_SEMTECH_RADIO_DRIVERS + help + This option enables the use of Semtech's LoRaMac stack + +config HAS_SEMTECH_SOFT_SE + bool "Semtech Secure Element software implementation" + depends on HAS_SEMTECH_LORAMAC + help + This option enables the use of Semtech's Secure Element + software implementation diff --git a/subsys/lorawan/lorawan.c b/subsys/lorawan/lorawan.c index 53af1405b885bb..8faafb76abe524 100644 --- a/subsys/lorawan/lorawan.c +++ b/subsys/lorawan/lorawan.c @@ -12,6 +12,7 @@ #include "lw_priv.h" #include +#include BUILD_ASSERT(!IS_ENABLED(CONFIG_LORAMAC_REGION_UNKNOWN), "Unknown region specified for LoRaWAN in Kconfig"); @@ -59,7 +60,6 @@ K_MUTEX_DEFINE(lorawan_send_mutex); */ static enum lorawan_datarate default_datarate; static enum lorawan_datarate current_datarate; -static uint8_t lorawan_conf_msg_tries = 1; static bool lorawan_adr_enable; static sys_slist_t dl_callbacks; @@ -75,6 +75,11 @@ static LoRaMacEventInfoStatus_t last_mlme_indication_status; static uint8_t (*getBatteryLevelUser)(void); static void (*dr_change_cb)(enum lorawan_datarate dr); +void BoardGetUniqueId(uint8_t *id) +{ + /* Do not change the default value */ +} + static uint8_t getBatteryLevelLocal(void) { if (getBatteryLevelUser != NULL) { @@ -205,6 +210,7 @@ static LoRaMacStatus_t lorawan_join_otaa( mlme_req.Type = MLME_JOIN; mlme_req.Req.Join.Datarate = default_datarate; + mlme_req.Req.Join.NetworkActivation = ACTIVATION_TYPE_OTAA; mib_req.Type = MIB_DEV_EUI; mib_req.Param.DevEui = join_cfg->dev_eui; @@ -431,7 +437,13 @@ void lorawan_enable_adr(bool enable) int lorawan_set_conf_msg_tries(uint8_t tries) { - lorawan_conf_msg_tries = tries; + MibRequestConfirm_t mib_req; + + mib_req.Type = MIB_CHANNELS_NB_TRANS; + mib_req.Param.ChannelsNbTrans = tries; + if (LoRaMacMibSetRequestConfirm(&mib_req) != LORAMAC_STATUS_OK) { + return -EINVAL; + } return 0; } @@ -473,7 +485,6 @@ int lorawan_send(uint8_t port, uint8_t *data, uint8_t len, uint8_t flags) mcpsReq.Req.Confirmed.fPort = port; mcpsReq.Req.Confirmed.fBuffer = data; mcpsReq.Req.Confirmed.fBufferSize = len; - mcpsReq.Req.Confirmed.NbTrials = lorawan_conf_msg_tries; mcpsReq.Req.Confirmed.Datarate = current_datarate; } else { /* default message type */ @@ -577,7 +588,7 @@ static int lorawan_init(const struct device *dev) macPrimitives.MacMlmeIndication = MlmeIndication; macCallbacks.GetBatteryLevel = getBatteryLevelLocal; macCallbacks.GetTemperatureLevel = NULL; - macCallbacks.NvmContextChange = NULL; + macCallbacks.NvmDataChange = NULL; macCallbacks.MacProcessNotify = OnMacProcessNotify; status = LoRaMacInitialization(&macPrimitives, &macCallbacks, diff --git a/subsys/lorawan/lw_priv.c b/subsys/lorawan/lw_priv.c index 7bedc466629d5d..4e8837b38d35e6 100644 --- a/subsys/lorawan/lw_priv.c +++ b/subsys/lorawan/lw_priv.c @@ -61,8 +61,6 @@ static const char *const eventinfo2str[] = { [LORAMAC_EVENT_INFO_STATUS_DOWNLINK_REPEATED] = "Downlink repeated", [LORAMAC_EVENT_INFO_STATUS_TX_DR_PAYLOAD_SIZE_ERROR] = "Tx DR payload size error", - [LORAMAC_EVENT_INFO_STATUS_DOWNLINK_TOO_MANY_FRAMES_LOSS] = - "Downlink too many frames loss", [LORAMAC_EVENT_INFO_STATUS_ADDRESS_FAIL] = "Address fail", [LORAMAC_EVENT_INFO_STATUS_MIC_FAIL] = "MIC fail", [LORAMAC_EVENT_INFO_STATUS_MULTICAST_FAIL] = "Multicast fail", @@ -131,7 +129,6 @@ static const int eventinfo2errno[] = { [LORAMAC_EVENT_INFO_STATUS_JOIN_FAIL] = -EINVAL, [LORAMAC_EVENT_INFO_STATUS_DOWNLINK_REPEATED] = -ECONNRESET, [LORAMAC_EVENT_INFO_STATUS_TX_DR_PAYLOAD_SIZE_ERROR] = -EMSGSIZE, - [LORAMAC_EVENT_INFO_STATUS_DOWNLINK_TOO_MANY_FRAMES_LOSS] = -ECONNRESET, [LORAMAC_EVENT_INFO_STATUS_ADDRESS_FAIL] = -EACCES, [LORAMAC_EVENT_INFO_STATUS_MIC_FAIL] = -EACCES, [LORAMAC_EVENT_INFO_STATUS_MULTICAST_FAIL] = -EINVAL, diff --git a/west.yml b/west.yml index 487f17d57a7e19..c0f575b3290d58 100644 --- a/west.yml +++ b/west.yml @@ -106,7 +106,7 @@ manifest: revision: 6010f0523cbc75f551d9256cf782f173177acdef path: modules/lib/open-amp - name: loramac-node - revision: 0a5dfdbc00d6c8bd1ea3acb76941521d09aa3eff + revision: 12019623bbad9eb54fe51066847a7cbd4b4eac57 path: modules/lib/loramac-node - name: openthread revision: f460532d4afa5d49feba241e5dc31c56123d31a8