From eb633e5dfd52c6cf491d73605170f799453dd2dc Mon Sep 17 00:00:00 2001 From: Giulia Cioffi Date: Fri, 24 Apr 2020 11:26:53 +0200 Subject: [PATCH 1/3] ST fw update for band selection at runtime Command "+BAND" added to the predefined set of AT commands. It allows to select the frequency band at runtime. The initial band is set to the European one. If the user selects a different band, the Lora state machine is re-initialized and the desired band of frequency is enabled. In the compiler builIn options, the symbols of the frequency bands of all the regions are set to False. An interactive test is available here: https://github.com/giulcioffi/tests_MKRWAN/blob/master/band_test.ino It continuously asks the user to select a new region. After the user inserts one, the value of the frequency on the RX channel is printed. In case this value is different from the one that should be set for that region, an error message is displayed. --- Middlewares/Third_Party/LoRaWAN/Mac/LoRaMac.h | 97 ++++++++-------- .../LoRa/AT_Slave/LoRaWAN/App/inc/at.h | 15 +++ .../LoRa/AT_Slave/LoRaWAN/App/inc/lora.h | 9 +- .../LoRa/AT_Slave/LoRaWAN/App/src/at.c | 21 ++++ .../LoRa/AT_Slave/LoRaWAN/App/src/command.c | 13 +++ .../LoRa/AT_Slave/LoRaWAN/App/src/lora.c | 106 ++++++------------ .../LoRa/AT_Slave/LoRaWAN/App/src/main.c | 4 +- .../AT_Slave/SW4STM32/mlm32l07x01/.cproject | 9 ++ 8 files changed, 156 insertions(+), 118 deletions(-) diff --git a/Middlewares/Third_Party/LoRaWAN/Mac/LoRaMac.h b/Middlewares/Third_Party/LoRaWAN/Mac/LoRaMac.h index 06d26bad..c5d0455b 100644 --- a/Middlewares/Third_Party/LoRaWAN/Mac/LoRaMac.h +++ b/Middlewares/Third_Party/LoRaWAN/Mac/LoRaMac.h @@ -172,6 +172,53 @@ */ #define LORAMAC_CRYPTO_MULTICAST_KEYS 127 +/*! + * LoRaMAC region enumeration + */ +typedef enum eLoRaMacRegion_t +{ + /*! + * AS band on 923MHz + */ + LORAMAC_REGION_AS923, + /*! + * Australian band on 915MHz + */ + LORAMAC_REGION_AU915, + /*! + * Chinese band on 470MHz + */ + LORAMAC_REGION_CN470, + /*! + * Chinese band on 779MHz + */ + LORAMAC_REGION_CN779, + /*! + * European band on 433MHz + */ + LORAMAC_REGION_EU433, + /*! + * European band on 868MHz + */ + LORAMAC_REGION_EU868, + /*! + * South korean band on 920MHz + */ + LORAMAC_REGION_KR920, + /*! + * India band on 865MHz + */ + LORAMAC_REGION_IN865, + /*! + * North american band on 915MHz + */ + LORAMAC_REGION_US915, + /*! + * Russia band on 864MHz + */ + LORAMAC_REGION_RU864, +}LoRaMacRegion_t; + /*! * End-Device activation type */ @@ -915,6 +962,10 @@ typedef struct sMcpsIndication * Set if a DeviceTimeAns MAC command was received. */ bool DeviceTimeAnsReceived; + /*! + * Set the region currently in use + */ + LoRaMacRegion_t Region; }McpsIndication_t; /*! @@ -2244,52 +2295,6 @@ typedef enum eLoRaMacStatus LORAMAC_STATUS_ERROR }LoRaMacStatus_t; -/*! - * LoRaMAC region enumeration - */ -typedef enum eLoRaMacRegion_t -{ - /*! - * AS band on 923MHz - */ - LORAMAC_REGION_AS923, - /*! - * Australian band on 915MHz - */ - LORAMAC_REGION_AU915, - /*! - * Chinese band on 470MHz - */ - LORAMAC_REGION_CN470, - /*! - * Chinese band on 779MHz - */ - LORAMAC_REGION_CN779, - /*! - * European band on 433MHz - */ - LORAMAC_REGION_EU433, - /*! - * European band on 868MHz - */ - LORAMAC_REGION_EU868, - /*! - * South korean band on 920MHz - */ - LORAMAC_REGION_KR920, - /*! - * India band on 865MHz - */ - LORAMAC_REGION_IN865, - /*! - * North american band on 915MHz - */ - LORAMAC_REGION_US915, - /*! - * Russia band on 864MHz - */ - LORAMAC_REGION_RU864, -}LoRaMacRegion_t; /*! * Enumeration of modules which have a context diff --git a/Projects/B-L072Z-LRWAN1/Applications/LoRa/AT_Slave/LoRaWAN/App/inc/at.h b/Projects/B-L072Z-LRWAN1/Applications/LoRa/AT_Slave/LoRaWAN/App/inc/at.h index 40b15a98..c0bfc77d 100644 --- a/Projects/B-L072Z-LRWAN1/Applications/LoRa/AT_Slave/LoRaWAN/App/inc/at.h +++ b/Projects/B-L072Z-LRWAN1/Applications/LoRa/AT_Slave/LoRaWAN/App/inc/at.h @@ -55,6 +55,7 @@ typedef enum eATEerror /* AT Command strings. Commands start with AT */ #define AT_RESET "Z" +#define AT_BAND "+BAND" #define AT_DEUI "+DEUI" #define AT_DADDR "+DADDR" #define AT_APPKEY "+APPKEY" @@ -131,6 +132,20 @@ ATEerror_t at_return_error(const char *param); */ ATEerror_t at_reset(const char *param); +/** + * @brief Print RF Band in use + * @param Param string of the AT command + * @retval AT_OK if OK, or an appropriate AT_xxx error code + */ +ATEerror_t at_Band_get(const char *param); + +/** + * @brief Set RF Band in use + * @param Param string of the AT command + * @retval AT_OK if OK, or an appropriate AT_xxx error code + */ +ATEerror_t at_Band_set(const char *param); + /** * @brief Print Device EUI * @param Param string of the AT command - unused diff --git a/Projects/B-L072Z-LRWAN1/Applications/LoRa/AT_Slave/LoRaWAN/App/inc/lora.h b/Projects/B-L072Z-LRWAN1/Applications/LoRa/AT_Slave/LoRaWAN/App/inc/lora.h index d1252483..61b3b091 100644 --- a/Projects/B-L072Z-LRWAN1/Applications/LoRa/AT_Slave/LoRaWAN/App/inc/lora.h +++ b/Projects/B-L072Z-LRWAN1/Applications/LoRa/AT_Slave/LoRaWAN/App/inc/lora.h @@ -222,7 +222,7 @@ typedef struct sLoRaMainCallback * @param [IN] application parmaters * @retval none */ -void LORA_Init(LoRaMainCallback_t *callbacks, LoRaParam_t *LoRaParam); +void LORA_Init(LoRaMainCallback_t *callbacks, LoRaParam_t *LoRaParam, LoRaMacRegion_t region); /** * @brief run Lora classA state Machine @@ -388,6 +388,13 @@ int8_t lora_config_tx_datarate_get(void); */ LoRaMacRegion_t lora_region_get(void); +/** + * @brief triggers a reinit when band gets changed + * @param none + * @retval none + */ +void TriggerReinit( LoRaMacRegion_t region ); + #ifdef __cplusplus } #endif diff --git a/Projects/B-L072Z-LRWAN1/Applications/LoRa/AT_Slave/LoRaWAN/App/src/at.c b/Projects/B-L072Z-LRWAN1/Applications/LoRa/AT_Slave/LoRaWAN/App/src/at.c index 0d49f139..208ee88f 100644 --- a/Projects/B-L072Z-LRWAN1/Applications/LoRa/AT_Slave/LoRaWAN/App/src/at.c +++ b/Projects/B-L072Z-LRWAN1/Applications/LoRa/AT_Slave/LoRaWAN/App/src/at.c @@ -410,6 +410,27 @@ ATEerror_t at_AppKey_set(const char *param) return AT_OK; } +extern LoRaMacRegion_t globalRegion; +ATEerror_t at_Band_get(const char *param) +{ + print_d(globalRegion); + return AT_OK; +} + +ATEerror_t at_Band_set(const char *param) +{ + LoRaMacRegion_t region; + if (tiny_sscanf(param, "%hhu", ®ion) != 1) + { + return AT_PARAM_ERROR; + } + if (region != globalRegion) { + globalRegion = region; + TriggerReinit(globalRegion); + } + return AT_OK; +} + ATEerror_t at_NwkSKey_get(const char *param) { MibRequestConfirm_t mib; diff --git a/Projects/B-L072Z-LRWAN1/Applications/LoRa/AT_Slave/LoRaWAN/App/src/command.c b/Projects/B-L072Z-LRWAN1/Applications/LoRa/AT_Slave/LoRaWAN/App/src/command.c index 365fd65e..c3e3b438 100644 --- a/Projects/B-L072Z-LRWAN1/Applications/LoRa/AT_Slave/LoRaWAN/App/src/command.c +++ b/Projects/B-L072Z-LRWAN1/Applications/LoRa/AT_Slave/LoRaWAN/App/src/command.c @@ -109,6 +109,19 @@ static const struct ATCommand_s ATCommand[] = }, #endif +#ifndef NO_BAND_RUNTIME_SWITCH + { + .string = AT_BAND, + .size_string = sizeof(AT_BAND) - 1, +#ifndef NO_HELP + .help_string = "AT"AT_BAND ": Get or Set the Regional Band\r\n", +#endif + .get = at_Band_get, + .set = at_Band_set, + .run = at_return_error, + }, +#endif + #ifndef NO_KEY_ADDR_EUI { .string = AT_APPKEY, diff --git a/Projects/B-L072Z-LRWAN1/Applications/LoRa/AT_Slave/LoRaWAN/App/src/lora.c b/Projects/B-L072Z-LRWAN1/Applications/LoRa/AT_Slave/LoRaWAN/App/src/lora.c index c8784465..9a2fadbc 100644 --- a/Projects/B-L072Z-LRWAN1/Applications/LoRa/AT_Slave/LoRaWAN/App/src/lora.c +++ b/Projects/B-L072Z-LRWAN1/Applications/LoRa/AT_Slave/LoRaWAN/App/src/lora.c @@ -53,11 +53,7 @@ typedef struct static lora_configuration_t lora_config = { .otaa = LORA_ENABLE, -#if defined( REGION_EU868 ) - .duty_cycle = LORA_ENABLE, -#else .duty_cycle = LORA_DISABLE, -#endif .DevEui = LORAWAN_DEVICE_EUI, .JoinEui = LORAWAN_JOIN_EUI, .AppKey = LORAWAN_APP_KEY, @@ -87,7 +83,6 @@ static lora_configuration_t lora_config = #include "LoRaMacTest.h" -#if defined( REGION_EU868 ) || defined( REGION_RU864 ) || defined( REGION_CN779 ) || defined( REGION_EU433 ) /*! * LoRaWAN ETSI duty cycle control enable/disable * @@ -95,7 +90,6 @@ static lora_configuration_t lora_config = */ #define LORAWAN_DUTYCYCLE_ON true -#endif #ifdef LORAMAC_CLASSB_ENABLED /*! @@ -331,11 +325,11 @@ static void MlmeConfirm(MlmeConfirm_t *mlmeConfirm) mibReq.Param.Class = CLASS_B; LoRaMacMibSetRequestConfirm(&mibReq); -#if defined( REGION_AU915 ) || defined( REGION_US915 ) - mibReq.Type = MIB_PING_SLOT_DATARATE; - mibReq.Param.PingSlotDatarate = DR_8; - LoRaMacMibSetRequestConfirm(&mibReq); -#endif + if ((LoRaRegion == LORAMAC_REGION_AU915) || (LoRaRegion == LORAMAC_REGION_US915)) { + mibReq.Type = MIB_PING_SLOT_DATARATE; + mibReq.Param.PingSlotDatarate = DR_8; + LoRaMacMibSetRequestConfirm(&mibReq); + } /*notify upper layer*/ LoRaMainCallbacks->LORA_ConfirmClass(CLASS_B); @@ -413,7 +407,7 @@ static void MlmeIndication(MlmeIndication_t *MlmeIndication) /** * lora Init */ -void LORA_Init(LoRaMainCallback_t *callbacks, LoRaParam_t *LoRaParam) +void LORA_Init(LoRaMainCallback_t *callbacks, LoRaParam_t *LoRaParam, LoRaMacRegion_t region) { /* init the Tx Duty Cycle*/ LoRaParamInit = LoRaParam; @@ -439,59 +433,28 @@ void LORA_Init(LoRaMainCallback_t *callbacks, LoRaParam_t *LoRaParam) LoRaMacCallbacks.GetBatteryLevel = LoRaMainCallbacks->BoardGetBatteryLevel; LoRaMacCallbacks.GetTemperatureLevel = LoRaMainCallbacks->BoardGetTemperatureLevel; LoRaMacCallbacks.MacProcessNotify = LoRaMainCallbacks->MacProcessNotify; -#if defined( REGION_AS923 ) - LoRaMacInitialization(&LoRaMacPrimitives, &LoRaMacCallbacks, LORAMAC_REGION_AS923); - LoRaRegion = LORAMAC_REGION_AS923; -#elif defined( REGION_AU915 ) - LoRaMacInitialization(&LoRaMacPrimitives, &LoRaMacCallbacks, LORAMAC_REGION_AU915); - LoRaRegion = LORAMAC_REGION_AU915; -#elif defined( REGION_CN470 ) - LoRaMacInitialization(&LoRaMacPrimitives, &LoRaMacCallbacks, LORAMAC_REGION_CN470); - LoRaRegion = LORAMAC_REGION_CN470; -#elif defined( REGION_CN779 ) - LoRaMacInitialization(&LoRaMacPrimitives, &LoRaMacCallbacks, LORAMAC_REGION_CN779); - LoRaRegion = LORAMAC_REGION_CN779; -#elif defined( REGION_EU433 ) - LoRaMacInitialization(&LoRaMacPrimitives, &LoRaMacCallbacks, LORAMAC_REGION_EU433); - LoRaRegion = LORAMAC_REGION_EU433; -#elif defined( REGION_IN865 ) - LoRaMacInitialization(&LoRaMacPrimitives, &LoRaMacCallbacks, LORAMAC_REGION_IN865); - LoRaRegion = LORAMAC_REGION_IN865; -#elif defined( REGION_EU868 ) - LoRaMacInitialization(&LoRaMacPrimitives, &LoRaMacCallbacks, LORAMAC_REGION_EU868); - LoRaRegion = LORAMAC_REGION_EU868; -#elif defined( REGION_KR920 ) - LoRaMacInitialization(&LoRaMacPrimitives, &LoRaMacCallbacks, LORAMAC_REGION_KR920); - LoRaRegion = LORAMAC_REGION_KR920; -#elif defined( REGION_US915 ) - LoRaMacInitialization(&LoRaMacPrimitives, &LoRaMacCallbacks, LORAMAC_REGION_US915); - LoRaRegion = LORAMAC_REGION_US915; -#elif defined( REGION_RU864 ) - LoRaMacInitialization(&LoRaMacPrimitives, &LoRaMacCallbacks, LORAMAC_REGION_RU864); - LoRaRegion = LORAMAC_REGION_RU864; -#else -#error "Please define a region in the compiler options." -#endif + LoRaMacInitialization(&LoRaMacPrimitives, &LoRaMacCallbacks, region); + LoRaRegion = region; #if defined( HYBRID ) -#if defined( REGION_US915 ) || defined( REGION_AU915 ) - uint16_t channelMask[] = { 0x00FF, 0x0000, 0x0000, 0x0000, 0x0001, 0x0000}; - mibReq.Type = MIB_CHANNELS_MASK; - mibReq.Param.ChannelsMask = channelMask; - LoRaMacMibSetRequestConfirm(&mibReq); - mibReq.Type = MIB_CHANNELS_DEFAULT_MASK; - mibReq.Param.ChannelsDefaultMask = channelMask; - LoRaMacMibSetRequestConfirm(&mibReq); -#endif -#if defined( REGION_CN470 ) - uint16_t channelMask[] = { 0x00FF, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000}; - mibReq.Type = MIB_CHANNELS_MASK; - mibReq.Param.ChannelsMask = channelMask; - LoRaMacMibSetRequestConfirm(&mibReq); - mibReq.Type = MIB_CHANNELS_DEFAULT_MASK; - mibReq.Param.ChannelsDefaultMask = channelMask; - LoRaMacMibSetRequestConfirm(&mibReq); -#endif + if ((LoRaRegion == LORAMAC_REGION_US915) || (LoRaRegion == LORAMAC_REGION_AU915)) { + uint16_t channelMask[] = { 0x00FF, 0x0000, 0x0000, 0x0000, 0x0001, 0x0000}; + mibReq.Type = MIB_CHANNELS_MASK; + mibReq.Param.ChannelsMask = channelMask; + LoRaMacMibSetRequestConfirm(&mibReq); + mibReq.Type = MIB_CHANNELS_DEFAULT_MASK; + mibReq.Param.ChannelsDefaultMask = channelMask; + LoRaMacMibSetRequestConfirm(&mibReq); + } + if (LoRaRegion == LORAMAC_REGION_CN470) { + uint16_t channelMask[] = { 0x00FF, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000}; + mibReq.Type = MIB_CHANNELS_MASK; + mibReq.Param.ChannelsMask = channelMask; + LoRaMacMibSetRequestConfirm(&mibReq); + mibReq.Type = MIB_CHANNELS_DEFAULT_MASK; + mibReq.Param.ChannelsDefaultMask = channelMask; + LoRaMacMibSetRequestConfirm(&mibReq); + } #endif lora_config_otaa_set(LORA_ENABLE); @@ -524,13 +487,14 @@ void LORA_Init(LoRaMainCallback_t *callbacks, LoRaParam_t *LoRaParam) mibReq.Param.Class = CLASS_A; LoRaMacMibSetRequestConfirm(&mibReq); -#if defined( REGION_EU868 ) || defined( REGION_RU864 ) || defined( REGION_CN779 ) || defined( REGION_EU433 ) - LoRaMacTestSetDutyCycleOn(LORAWAN_DUTYCYCLE_ON); + if ((LoRaRegion == LORAMAC_REGION_EU868) || (LoRaRegion == LORAMAC_REGION_RU864) || (LoRaRegion == LORAMAC_REGION_CN779) || (LoRaRegion == LORAMAC_REGION_EU433)) { + LoRaMacTestSetDutyCycleOn(LORAWAN_DUTYCYCLE_ON); - lora_config.duty_cycle = LORA_ENABLE; -#else - lora_config.duty_cycle = LORA_DISABLE; -#endif + lora_config.duty_cycle = LORA_ENABLE; + } + else { + lora_config.duty_cycle = LORA_DISABLE; + } mibReq.Type = MIB_SYSTEM_MAX_RX_ERROR; mibReq.Param.SystemMaxRxError = 20; @@ -970,6 +934,10 @@ void lora_wan_certif(void) } +void TriggerReinit( LoRaMacRegion_t region ) { + LORA_Init(LoRaMainCallbacks, LoRaParamInit, region); +} + LoRaMacRegion_t lora_region_get(void) { return LoRaRegion; diff --git a/Projects/B-L072Z-LRWAN1/Applications/LoRa/AT_Slave/LoRaWAN/App/src/main.c b/Projects/B-L072Z-LRWAN1/Applications/LoRa/AT_Slave/LoRaWAN/App/src/main.c index b51ea1cc..c4c13cf5 100644 --- a/Projects/B-L072Z-LRWAN1/Applications/LoRa/AT_Slave/LoRaWAN/App/src/main.c +++ b/Projects/B-L072Z-LRWAN1/Applications/LoRa/AT_Slave/LoRaWAN/App/src/main.c @@ -102,7 +102,7 @@ static LoRaParam_t LoRaParamInit = {LORAWAN_ADR_ON, LORAWAN_PUBLIC_NETWORK }; - +LoRaMacRegion_t globalRegion = LORAMAC_REGION_EU868; /* Private functions ---------------------------------------------------------*/ @@ -134,7 +134,7 @@ int main(void) /* USER CODE END 1 */ /* Configure the Lora Stack*/ - LORA_Init(&LoRaMainCallbacks, &LoRaParamInit); + LORA_Init(&LoRaMainCallbacks, &LoRaParamInit, globalRegion); /* main loop*/ while (1) diff --git a/Projects/B-L072Z-LRWAN1/Applications/LoRa/AT_Slave/SW4STM32/mlm32l07x01/.cproject b/Projects/B-L072Z-LRWAN1/Applications/LoRa/AT_Slave/SW4STM32/mlm32l07x01/.cproject index 458316b2..270bcc13 100644 --- a/Projects/B-L072Z-LRWAN1/Applications/LoRa/AT_Slave/SW4STM32/mlm32l07x01/.cproject +++ b/Projects/B-L072Z-LRWAN1/Applications/LoRa/AT_Slave/SW4STM32/mlm32l07x01/.cproject @@ -58,6 +58,15 @@ + + + + + + + + +