Skip to content

Commit eb633e5

Browse files
committed
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.
1 parent 2b551f1 commit eb633e5

File tree

8 files changed

+156
-118
lines changed

8 files changed

+156
-118
lines changed

Middlewares/Third_Party/LoRaWAN/Mac/LoRaMac.h

Lines changed: 51 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,53 @@
172172
*/
173173
#define LORAMAC_CRYPTO_MULTICAST_KEYS 127
174174

175+
/*!
176+
* LoRaMAC region enumeration
177+
*/
178+
typedef enum eLoRaMacRegion_t
179+
{
180+
/*!
181+
* AS band on 923MHz
182+
*/
183+
LORAMAC_REGION_AS923,
184+
/*!
185+
* Australian band on 915MHz
186+
*/
187+
LORAMAC_REGION_AU915,
188+
/*!
189+
* Chinese band on 470MHz
190+
*/
191+
LORAMAC_REGION_CN470,
192+
/*!
193+
* Chinese band on 779MHz
194+
*/
195+
LORAMAC_REGION_CN779,
196+
/*!
197+
* European band on 433MHz
198+
*/
199+
LORAMAC_REGION_EU433,
200+
/*!
201+
* European band on 868MHz
202+
*/
203+
LORAMAC_REGION_EU868,
204+
/*!
205+
* South korean band on 920MHz
206+
*/
207+
LORAMAC_REGION_KR920,
208+
/*!
209+
* India band on 865MHz
210+
*/
211+
LORAMAC_REGION_IN865,
212+
/*!
213+
* North american band on 915MHz
214+
*/
215+
LORAMAC_REGION_US915,
216+
/*!
217+
* Russia band on 864MHz
218+
*/
219+
LORAMAC_REGION_RU864,
220+
}LoRaMacRegion_t;
221+
175222
/*!
176223
* End-Device activation type
177224
*/
@@ -915,6 +962,10 @@ typedef struct sMcpsIndication
915962
* Set if a DeviceTimeAns MAC command was received.
916963
*/
917964
bool DeviceTimeAnsReceived;
965+
/*!
966+
* Set the region currently in use
967+
*/
968+
LoRaMacRegion_t Region;
918969
}McpsIndication_t;
919970

920971
/*!
@@ -2244,52 +2295,6 @@ typedef enum eLoRaMacStatus
22442295
LORAMAC_STATUS_ERROR
22452296
}LoRaMacStatus_t;
22462297

2247-
/*!
2248-
* LoRaMAC region enumeration
2249-
*/
2250-
typedef enum eLoRaMacRegion_t
2251-
{
2252-
/*!
2253-
* AS band on 923MHz
2254-
*/
2255-
LORAMAC_REGION_AS923,
2256-
/*!
2257-
* Australian band on 915MHz
2258-
*/
2259-
LORAMAC_REGION_AU915,
2260-
/*!
2261-
* Chinese band on 470MHz
2262-
*/
2263-
LORAMAC_REGION_CN470,
2264-
/*!
2265-
* Chinese band on 779MHz
2266-
*/
2267-
LORAMAC_REGION_CN779,
2268-
/*!
2269-
* European band on 433MHz
2270-
*/
2271-
LORAMAC_REGION_EU433,
2272-
/*!
2273-
* European band on 868MHz
2274-
*/
2275-
LORAMAC_REGION_EU868,
2276-
/*!
2277-
* South korean band on 920MHz
2278-
*/
2279-
LORAMAC_REGION_KR920,
2280-
/*!
2281-
* India band on 865MHz
2282-
*/
2283-
LORAMAC_REGION_IN865,
2284-
/*!
2285-
* North american band on 915MHz
2286-
*/
2287-
LORAMAC_REGION_US915,
2288-
/*!
2289-
* Russia band on 864MHz
2290-
*/
2291-
LORAMAC_REGION_RU864,
2292-
}LoRaMacRegion_t;
22932298

22942299
/*!
22952300
* Enumeration of modules which have a context

Projects/B-L072Z-LRWAN1/Applications/LoRa/AT_Slave/LoRaWAN/App/inc/at.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ typedef enum eATEerror
5555

5656
/* AT Command strings. Commands start with AT */
5757
#define AT_RESET "Z"
58+
#define AT_BAND "+BAND"
5859
#define AT_DEUI "+DEUI"
5960
#define AT_DADDR "+DADDR"
6061
#define AT_APPKEY "+APPKEY"
@@ -131,6 +132,20 @@ ATEerror_t at_return_error(const char *param);
131132
*/
132133
ATEerror_t at_reset(const char *param);
133134

135+
/**
136+
* @brief Print RF Band in use
137+
* @param Param string of the AT command
138+
* @retval AT_OK if OK, or an appropriate AT_xxx error code
139+
*/
140+
ATEerror_t at_Band_get(const char *param);
141+
142+
/**
143+
* @brief Set RF Band in use
144+
* @param Param string of the AT command
145+
* @retval AT_OK if OK, or an appropriate AT_xxx error code
146+
*/
147+
ATEerror_t at_Band_set(const char *param);
148+
134149
/**
135150
* @brief Print Device EUI
136151
* @param Param string of the AT command - unused

Projects/B-L072Z-LRWAN1/Applications/LoRa/AT_Slave/LoRaWAN/App/inc/lora.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ typedef struct sLoRaMainCallback
222222
* @param [IN] application parmaters
223223
* @retval none
224224
*/
225-
void LORA_Init(LoRaMainCallback_t *callbacks, LoRaParam_t *LoRaParam);
225+
void LORA_Init(LoRaMainCallback_t *callbacks, LoRaParam_t *LoRaParam, LoRaMacRegion_t region);
226226

227227
/**
228228
* @brief run Lora classA state Machine
@@ -388,6 +388,13 @@ int8_t lora_config_tx_datarate_get(void);
388388
*/
389389
LoRaMacRegion_t lora_region_get(void);
390390

391+
/**
392+
* @brief triggers a reinit when band gets changed
393+
* @param none
394+
* @retval none
395+
*/
396+
void TriggerReinit( LoRaMacRegion_t region );
397+
391398
#ifdef __cplusplus
392399
}
393400
#endif

Projects/B-L072Z-LRWAN1/Applications/LoRa/AT_Slave/LoRaWAN/App/src/at.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,27 @@ ATEerror_t at_AppKey_set(const char *param)
410410
return AT_OK;
411411
}
412412

413+
extern LoRaMacRegion_t globalRegion;
414+
ATEerror_t at_Band_get(const char *param)
415+
{
416+
print_d(globalRegion);
417+
return AT_OK;
418+
}
419+
420+
ATEerror_t at_Band_set(const char *param)
421+
{
422+
LoRaMacRegion_t region;
423+
if (tiny_sscanf(param, "%hhu", &region) != 1)
424+
{
425+
return AT_PARAM_ERROR;
426+
}
427+
if (region != globalRegion) {
428+
globalRegion = region;
429+
TriggerReinit(globalRegion);
430+
}
431+
return AT_OK;
432+
}
433+
413434
ATEerror_t at_NwkSKey_get(const char *param)
414435
{
415436
MibRequestConfirm_t mib;

Projects/B-L072Z-LRWAN1/Applications/LoRa/AT_Slave/LoRaWAN/App/src/command.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,19 @@ static const struct ATCommand_s ATCommand[] =
109109
},
110110
#endif
111111

112+
#ifndef NO_BAND_RUNTIME_SWITCH
113+
{
114+
.string = AT_BAND,
115+
.size_string = sizeof(AT_BAND) - 1,
116+
#ifndef NO_HELP
117+
.help_string = "AT"AT_BAND ": Get or Set the Regional Band\r\n",
118+
#endif
119+
.get = at_Band_get,
120+
.set = at_Band_set,
121+
.run = at_return_error,
122+
},
123+
#endif
124+
112125
#ifndef NO_KEY_ADDR_EUI
113126
{
114127
.string = AT_APPKEY,

Projects/B-L072Z-LRWAN1/Applications/LoRa/AT_Slave/LoRaWAN/App/src/lora.c

Lines changed: 37 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,7 @@ typedef struct
5353
static lora_configuration_t lora_config =
5454
{
5555
.otaa = LORA_ENABLE,
56-
#if defined( REGION_EU868 )
57-
.duty_cycle = LORA_ENABLE,
58-
#else
5956
.duty_cycle = LORA_DISABLE,
60-
#endif
6157
.DevEui = LORAWAN_DEVICE_EUI,
6258
.JoinEui = LORAWAN_JOIN_EUI,
6359
.AppKey = LORAWAN_APP_KEY,
@@ -87,15 +83,13 @@ static lora_configuration_t lora_config =
8783

8884
#include "LoRaMacTest.h"
8985

90-
#if defined( REGION_EU868 ) || defined( REGION_RU864 ) || defined( REGION_CN779 ) || defined( REGION_EU433 )
9186
/*!
9287
* LoRaWAN ETSI duty cycle control enable/disable
9388
*
9489
* \remark Please note that ETSI mandates duty cycled transmissions. Use only for test purposes
9590
*/
9691
#define LORAWAN_DUTYCYCLE_ON true
9792

98-
#endif
9993

10094
#ifdef LORAMAC_CLASSB_ENABLED
10195
/*!
@@ -331,11 +325,11 @@ static void MlmeConfirm(MlmeConfirm_t *mlmeConfirm)
331325
mibReq.Param.Class = CLASS_B;
332326
LoRaMacMibSetRequestConfirm(&mibReq);
333327

334-
#if defined( REGION_AU915 ) || defined( REGION_US915 )
335-
mibReq.Type = MIB_PING_SLOT_DATARATE;
336-
mibReq.Param.PingSlotDatarate = DR_8;
337-
LoRaMacMibSetRequestConfirm(&mibReq);
338-
#endif
328+
if ((LoRaRegion == LORAMAC_REGION_AU915) || (LoRaRegion == LORAMAC_REGION_US915)) {
329+
mibReq.Type = MIB_PING_SLOT_DATARATE;
330+
mibReq.Param.PingSlotDatarate = DR_8;
331+
LoRaMacMibSetRequestConfirm(&mibReq);
332+
}
339333

340334
/*notify upper layer*/
341335
LoRaMainCallbacks->LORA_ConfirmClass(CLASS_B);
@@ -413,7 +407,7 @@ static void MlmeIndication(MlmeIndication_t *MlmeIndication)
413407
/**
414408
* lora Init
415409
*/
416-
void LORA_Init(LoRaMainCallback_t *callbacks, LoRaParam_t *LoRaParam)
410+
void LORA_Init(LoRaMainCallback_t *callbacks, LoRaParam_t *LoRaParam, LoRaMacRegion_t region)
417411
{
418412
/* init the Tx Duty Cycle*/
419413
LoRaParamInit = LoRaParam;
@@ -439,59 +433,28 @@ void LORA_Init(LoRaMainCallback_t *callbacks, LoRaParam_t *LoRaParam)
439433
LoRaMacCallbacks.GetBatteryLevel = LoRaMainCallbacks->BoardGetBatteryLevel;
440434
LoRaMacCallbacks.GetTemperatureLevel = LoRaMainCallbacks->BoardGetTemperatureLevel;
441435
LoRaMacCallbacks.MacProcessNotify = LoRaMainCallbacks->MacProcessNotify;
442-
#if defined( REGION_AS923 )
443-
LoRaMacInitialization(&LoRaMacPrimitives, &LoRaMacCallbacks, LORAMAC_REGION_AS923);
444-
LoRaRegion = LORAMAC_REGION_AS923;
445-
#elif defined( REGION_AU915 )
446-
LoRaMacInitialization(&LoRaMacPrimitives, &LoRaMacCallbacks, LORAMAC_REGION_AU915);
447-
LoRaRegion = LORAMAC_REGION_AU915;
448-
#elif defined( REGION_CN470 )
449-
LoRaMacInitialization(&LoRaMacPrimitives, &LoRaMacCallbacks, LORAMAC_REGION_CN470);
450-
LoRaRegion = LORAMAC_REGION_CN470;
451-
#elif defined( REGION_CN779 )
452-
LoRaMacInitialization(&LoRaMacPrimitives, &LoRaMacCallbacks, LORAMAC_REGION_CN779);
453-
LoRaRegion = LORAMAC_REGION_CN779;
454-
#elif defined( REGION_EU433 )
455-
LoRaMacInitialization(&LoRaMacPrimitives, &LoRaMacCallbacks, LORAMAC_REGION_EU433);
456-
LoRaRegion = LORAMAC_REGION_EU433;
457-
#elif defined( REGION_IN865 )
458-
LoRaMacInitialization(&LoRaMacPrimitives, &LoRaMacCallbacks, LORAMAC_REGION_IN865);
459-
LoRaRegion = LORAMAC_REGION_IN865;
460-
#elif defined( REGION_EU868 )
461-
LoRaMacInitialization(&LoRaMacPrimitives, &LoRaMacCallbacks, LORAMAC_REGION_EU868);
462-
LoRaRegion = LORAMAC_REGION_EU868;
463-
#elif defined( REGION_KR920 )
464-
LoRaMacInitialization(&LoRaMacPrimitives, &LoRaMacCallbacks, LORAMAC_REGION_KR920);
465-
LoRaRegion = LORAMAC_REGION_KR920;
466-
#elif defined( REGION_US915 )
467-
LoRaMacInitialization(&LoRaMacPrimitives, &LoRaMacCallbacks, LORAMAC_REGION_US915);
468-
LoRaRegion = LORAMAC_REGION_US915;
469-
#elif defined( REGION_RU864 )
470-
LoRaMacInitialization(&LoRaMacPrimitives, &LoRaMacCallbacks, LORAMAC_REGION_RU864);
471-
LoRaRegion = LORAMAC_REGION_RU864;
472-
#else
473-
#error "Please define a region in the compiler options."
474-
#endif
436+
LoRaMacInitialization(&LoRaMacPrimitives, &LoRaMacCallbacks, region);
437+
LoRaRegion = region;
475438

476439
#if defined( HYBRID )
477-
#if defined( REGION_US915 ) || defined( REGION_AU915 )
478-
uint16_t channelMask[] = { 0x00FF, 0x0000, 0x0000, 0x0000, 0x0001, 0x0000};
479-
mibReq.Type = MIB_CHANNELS_MASK;
480-
mibReq.Param.ChannelsMask = channelMask;
481-
LoRaMacMibSetRequestConfirm(&mibReq);
482-
mibReq.Type = MIB_CHANNELS_DEFAULT_MASK;
483-
mibReq.Param.ChannelsDefaultMask = channelMask;
484-
LoRaMacMibSetRequestConfirm(&mibReq);
485-
#endif
486-
#if defined( REGION_CN470 )
487-
uint16_t channelMask[] = { 0x00FF, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000};
488-
mibReq.Type = MIB_CHANNELS_MASK;
489-
mibReq.Param.ChannelsMask = channelMask;
490-
LoRaMacMibSetRequestConfirm(&mibReq);
491-
mibReq.Type = MIB_CHANNELS_DEFAULT_MASK;
492-
mibReq.Param.ChannelsDefaultMask = channelMask;
493-
LoRaMacMibSetRequestConfirm(&mibReq);
494-
#endif
440+
if ((LoRaRegion == LORAMAC_REGION_US915) || (LoRaRegion == LORAMAC_REGION_AU915)) {
441+
uint16_t channelMask[] = { 0x00FF, 0x0000, 0x0000, 0x0000, 0x0001, 0x0000};
442+
mibReq.Type = MIB_CHANNELS_MASK;
443+
mibReq.Param.ChannelsMask = channelMask;
444+
LoRaMacMibSetRequestConfirm(&mibReq);
445+
mibReq.Type = MIB_CHANNELS_DEFAULT_MASK;
446+
mibReq.Param.ChannelsDefaultMask = channelMask;
447+
LoRaMacMibSetRequestConfirm(&mibReq);
448+
}
449+
if (LoRaRegion == LORAMAC_REGION_CN470) {
450+
uint16_t channelMask[] = { 0x00FF, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000};
451+
mibReq.Type = MIB_CHANNELS_MASK;
452+
mibReq.Param.ChannelsMask = channelMask;
453+
LoRaMacMibSetRequestConfirm(&mibReq);
454+
mibReq.Type = MIB_CHANNELS_DEFAULT_MASK;
455+
mibReq.Param.ChannelsDefaultMask = channelMask;
456+
LoRaMacMibSetRequestConfirm(&mibReq);
457+
}
495458
#endif
496459

497460
lora_config_otaa_set(LORA_ENABLE);
@@ -524,13 +487,14 @@ void LORA_Init(LoRaMainCallback_t *callbacks, LoRaParam_t *LoRaParam)
524487
mibReq.Param.Class = CLASS_A;
525488
LoRaMacMibSetRequestConfirm(&mibReq);
526489

527-
#if defined( REGION_EU868 ) || defined( REGION_RU864 ) || defined( REGION_CN779 ) || defined( REGION_EU433 )
528-
LoRaMacTestSetDutyCycleOn(LORAWAN_DUTYCYCLE_ON);
490+
if ((LoRaRegion == LORAMAC_REGION_EU868) || (LoRaRegion == LORAMAC_REGION_RU864) || (LoRaRegion == LORAMAC_REGION_CN779) || (LoRaRegion == LORAMAC_REGION_EU433)) {
491+
LoRaMacTestSetDutyCycleOn(LORAWAN_DUTYCYCLE_ON);
529492

530-
lora_config.duty_cycle = LORA_ENABLE;
531-
#else
532-
lora_config.duty_cycle = LORA_DISABLE;
533-
#endif
493+
lora_config.duty_cycle = LORA_ENABLE;
494+
}
495+
else {
496+
lora_config.duty_cycle = LORA_DISABLE;
497+
}
534498

535499
mibReq.Type = MIB_SYSTEM_MAX_RX_ERROR;
536500
mibReq.Param.SystemMaxRxError = 20;
@@ -970,6 +934,10 @@ void lora_wan_certif(void)
970934

971935
}
972936

937+
void TriggerReinit( LoRaMacRegion_t region ) {
938+
LORA_Init(LoRaMainCallbacks, LoRaParamInit, region);
939+
}
940+
973941
LoRaMacRegion_t lora_region_get(void)
974942
{
975943
return LoRaRegion;

0 commit comments

Comments
 (0)