En DM00300436
En DM00300436
En DM00300436
User manual
STM32 LoRa® software expansion for STM32Cube™
Introduction
This user manual describes the LoRa® Expansion Package implementation on the
STM32Lx Series; this software is called I-CUBE-LRWAN. This document also explains how
to interface with the LoRaWAN™ to manage the LoRa® wireless link.
LoRa® is a type of wireless telecommunication network designed to allow long range
communications at a very low bit-rate and enabling long-life battery operated sensors.
LoRaWAN™ defines the communication and security protocol that ensures the
interoperability with the LoRa® network.
The LoRa® Expansion Package is compliant with the LoRa Alliance™ specification protocol
named LoRaWAN™.
The I-CUBE-LRWAN main features are the following:
• Application integration ready
• Easy add-on of the low-power LoRa® solution
• Extremely low CPU load
• No latency requirements
• Small STM32 memory footprint
• Low-power timing services provided
The I-CUBE-LRWAN Expansion Package is based on the STM32Cube™ HAL drivers (see
Section 2).
This user manual provides customer application examples on NUCLEO-L053R8,
NUCLEO-L152RE and NUCLEOL476RG using Semtech expansion boards
SX1276MB1MAS, SX1276MB1LAS, SX1272MB2DAS, SX1262DVK1DAS,
SX1262DVK1CAS and SX1262DVK1BAS.
This document targets the following tools:
• P-NUCLEO-LRWAN1, STM32 Nucleo pack for LoRa® technology
• B-L072Z-LRWAN1, STM32 Discovery kit embedding the CMWX1ZZABZ-091 LoRa®
module (Murata)
• I-NUCLEO-LRWAN1, LoRa® expansion board for STM32 Nucleo, based on the
WM-SG-SM-42 LPWAN module (USI®)
• LRWAN-NS1, expansion board featuring the RiSiNGHF® modem RHF0M003 available in
P-NUCLEO-LRWAN3
Contents
1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.1 Acronyms and abbreviations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.2 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
5 Example description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
5.1 Single MCU end-device hardware description . . . . . . . . . . . . . . . . . . . . . 39
5.2 Split end-device hardware description (two-MCUs solution) . . . . . . . . . . 40
5.3 Package description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
5.4 End_Node application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
5.4.1 Activation methods and keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
5.4.2 Debug switch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
5.4.3 Sensor switch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
5.5 PingPong application description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
5.6 AT_Slave application description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
5.7 AT_Master application description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
6 System performances . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
6.1 Memory footprints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
6.2 Real-time constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
6.3 Power consumption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
7 Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
List of tables
List of figures
1 Overview
a. Arm is a registered trademark of Arm Limited (or its subsidiaries) in the US and/or elsewhere.
1.2 References
• LoRa Alliance specification protocol named LoRaWAN version V1.0.3 - 2018, March -
Final - Released
• IEEE Std 802.15.4TM - 2011. Low-Rate Wireless Personal Area Networks (LR-
WPANs)
• LoRaWAN version 1.1 Regional Parameters - RevB - 2018, January - Released
2.1 Overview
This section provides a general overview of the LoRa and LoRaWAN recommendations,
focusing in particular on the LoRa end-device which is the core subject of this user manual.
LoRa is a type of wireless telecommunication network designed to allow long range
communication at a very low bit-rate and enabling long-life battery operated sensors.
LoRaWAN defines the communication and security protocol ensuring the interoperability
with the LoRa network.
The LoRa Expansion Package is compliant with the LoRa Alliance specification protocol
named LoRaWAN.
Table 2 shows the LoRa classes usage definition. Refer to Section 2.2.2 for further details
on these classes.
Note: While the physical layer of LoRa is proprietary, the rest of the protocol stack (LoRaWAN) is
kept open and its development is carried out by the LoRa Alliance.
3HWWUDFNLQJ
6PRNH
DODUP
:DWHUPHWHU
7UDVK
FRQWDLQHU
9HQGLQJPDFKLQH
*DV
PRQLWRULQJ
06Y9
7[ 5[ 5[
5['HOD\
5['HOD\
06Y9
5['HOD\
3HULRG3LQJ
5['HOD\
%HDFRQ3HULRG
06Y9
5['HOD\
5['HOD\
([WHQGV5[XQWLOQH[W7[
06Y9
8SSHUOD\HU
0$&
3+<
3K\VLFDOPHGLXP DLULQWHUIDFH
06Y9
0/0(5HT
MRLQUHTXHVW
-RLQUHTXHVW 0/0(,QG
MRLQUHTXHVW
0$&:DLW7LPHU
0/0(5HVS
MRLQDFFHSW
-RLQUHVSRQVH DVN
0/0(&RQI
MRLQDFFHSW
0/0(,QG
MRLQDFFHSW
(QGGHYLFH $SSVHUYHU
06Y9
0&365HT
GDWDUHTXHVW
'DWD $FNUHT
$FN:DLW'XUDWLRQ
$FNQRZOHGJPHQWIUDPH
0&36,QG GDWD
0&36&RQI
(QGGHYLFH $SS6HUYHU
06Y9
0&36&RQI
(QGGHYLFH $SSVHUYHU
06Y9
$SSOLFDWLRQGDWD
$SSOLFDWLRQGDWD
1ZN6.H\ SD\ORDGRI0$&GDWD
$SS6.H\ SD\ORDGRIDSSOLFDWLRQPHVVDJH
06Y9
The NwkSKey is shared between the end-device and the network server. The NwkSKey
provides message integrity for the communication and provides security for the end-device
towards the network server communication.
The AppSKey is shared between the end-device and the application server. The AppSKey
is used to encrypt/decrypt the application data. In other words, the AppSKey provides
security for the application’s payload. In this way, the application data sent by an end-device
can not be interpreted by the network server.
3.1 Overview
This I-CUBE-LRWAN package offers a LoRa stack middleware for STM32 microcontrollers.
This middleware is split into several modules:
• LoRaMac layer module
• LoRa utilities module
• LoRa crypto module
• LoRa core module
The LoRa core module implements a LoRa state machine coming on top of the LoRaMac
layer. The LoRa stack module interfaces with the BSP Semtech radio driver module.
This middleware is provided in source-code format and is compliant which the
STM32Cube™ HAL driver.
Refer to Figure 10 for the structure of the project files.
%63GULYHUV/R5D70IRU
'LVFRYHU\ERDUG
%63GULYHUVIRU
&0:;==$%=PRGXOH
%63GULYHUV,18&/(2
/5:$1PRGHPERDUG
%63GULYHUV671XFOHR
6HQVRUPRGHPERDUG
%63GULYHUV1XFOHRERDUGV
%63GULYHUVIRU6HPWHFK
5DGLRERDUGV
^WĚƌŝǀĞƌƐĨŽƌ^ĞŶƐŽƌďŽĂƌĚ
0LGGOHZDUH/R5D70FRUH
0LGGOHZDUH/R5D70FU\SWR
HQJLQH
0LGGOHZDUH/R5D700$&
OD\HU
0LGGOHZDUH/R5D705DGLR
LQWHUIDFH
/R5D70DSSOLFDWLRQV
06Y9
3.2 Features
• Compliant with the specification for the LoRa Alliance protocol named LoRaWAN
• On-board LoRaWAN class A, class B and class C protocol stack
• EU 868MHz ISM band ETSI compliant
• EU 433MHz ISM band ETSI compliant
• US 915MHz ISM band FCC compliant
• KR 920Mhz ISM band defined by Korean Government
• RU 864Mhz ISM band defined by Russian regulation
• End-device activation either through over-the-air activation (OTAA) or through
activation-by-personalization (ABP)
• Adaptive data rate support
• LoRaWAN test application for certification tests included
• Low-power optimized
3.3 Architecture
Figure 11 describes the main design of the firmware for the I-CUBE-LRWAN application.
$SSOLFDWLRQ XVHU
0$& 8SSHU/D\HU
0$& /RZHU/D\HU
/R5D0LGGOHZDUH
8WLOLWLHV
WH[W
7LPHUVHUYHU 6HQVRU
/RZ3RZHU GULYHU
5DQGJHQ
&U\SWR
6RIWZDUH
5DGLRGULYHU
6HFXUH
6;[[GULYHU
(OHPHQW
+$/
676(07(&+ 3URYLGHGE\6(07(&+ 67
06Y9
The HAL uses Cube APIs to drive the MCU hardware required by the application. Only
specific hardware is included in the LoRa middleware as it is mandatory to run a LoRa
application.
The RTC provides a centralized time unit which continues to run even in Low-power mode
(Stop mode). The RTC alarm is used to wake up the system at specific timings managed by
the timer server.
The radio driver uses the SPI and the GPIO hardware to control the radio (see Figure 11).
The radio driver also provides a set of APIs to be used by higher level software.
The LoRa radio is provided by Semtech, though the APIs have been slightly modified to
interface with the STM32Cube™ HAL.
The radio driver is split in two parts:
• The sx1276.c, sx1272.c and sx126x.c contain all functions which are radio dependent
only.
• The sx1276mb1mas.c, sx1276mb1las, sx1272mb2das, sx1262dvk1das,
sx1262dvk1cas and sx1262dvk1bas contain all the radio board dependent functions.
The MAC controls the PHY using 802.15.4 model. The MAC interfaces with the PHY driver
and uses the timer server to add or remove timed tasks and to take care of the
'Tx time on air'. This action ensures that the duty-cycle limitation mandated by the ETSI is
respected and also carries out AES encryption/decryption algorithm to cypher the MAC
header and the payload.
Since the state machine, that controls the LoRa class A, is sensitive, an intermediate level
of software has been inserted (lora.c) between the MAC and the application
(refer to MAC’s “upper layer” on Figure 11). With a set of APIs limited as of now, the user is
free to implement the class A state machine at application level.
The application, built around an infinite loop, manages the low-power, runs the interrupt
handlers (alarm or GPIO) and calls the LoRa class A if any task must be done. This
application also implements the sensor read access.
3.4.2 SPI
The sx127x or sx126x radio commands and registers are accessed through the SPI bus at
1 Mbit/s (refer to Table 40 and to Section 5.1).
3.4.3 RTC
The RTC calendar is used as a timer engine running in all power modes from the 32 kHz
external oscillator. By default, the RTC is programed to provide 1024 ticks (sub-seconds)
per second. The RTC is programed once at initialization of the hardware when the MCU
starts for the first time. The RTC output is limited to a 32-bit timer that is around a 48 days
period.
If the user needs to change the tick duration, note that the tick duration should remain below
1 ms.
This section gives a description of the LoRaMac layer APIs. The PHY layer being
proprietary (see Section 2.1: Overview) is out of the scope of this user manual and must be
viewed as a black box.
LoRaMacStatus_t LoRaMacInitialization
Do initialization of the LoRaMac layer module
(LoRAMacPrimitives_t *primitives,
(see Section 4.3: Middleware MAC layer
LoRaMacCallback_t *callback,
callbacks)
LoRaMacRegion_t region)
LoRaMacStatus_t LoRaMacMcpsRequest
Requests to send Tx data
(McpsReq_t *mcpsRequest)
LoRaMacStatus_t LoRaMacMibSetRequestConfirm
To set attributes of the LoRaMac layer
(MibRequestConfirm_t *mibSet)
LoRaMacStatus_t LoRaMacMibGetRequestConfirm
To get attributes of the LoRaMac layer
(MibRequestConfirm_t *mibGet)
4.3.1 MCPS
In general, the LoRaMAC layer uses the MCPS services for data transmission and data
reception (see Table 8).
4.3.2 MLME
The LoRaMAC layer uses the MLME services to manage the LoRaWAN network
(see Table 9).
4.3.3 MIB
N/A
Note: SysTimeMkTime and SysTimeLocalTime are also provided in order to convert epoch into tm
structure as specified by time.h interface.
To convert UNIX time to your local time, time zone need to be added and leap seconds need
to be removed. In 2018, 18 leap seconds need to be removed. In Paris summer time, there
are two hours difference from Greenwich time, assuming time has been set, local time can
be printed on terminal:
{
SysTime_t UnixEpoch = SysTimeGet();
struct tm localtime;
Note: It can be used in polling mode when no real time constraints apply, typically during
application initialization:
#define PPRINTF(...) do{} while (0!= TraceSend (__VA_ARGS__)) //Polling Mode.
It can be used in real time mode. In this case, when there is not space left in the circular
queue, the string is not added and will not be printed out in com port
#define PRINTF(...) do {TraceSend (__VA_ARGS__);} while(0)
Note: The queue is filled with elements. Each element is composed of buffer length field (2 bytes)
and the buffer. When an element is too large to fit at the end of the queue, it is fragmented
into two elements.
06Y9
Standard mode
In standard mode, an interface file (see MAC upper layer in Figure 11) is provided to let the
user start without worrying about the LoRa state machine. The interface file is located in
Middlewares\Third_Party\Lora\Core\lora.c.
The interface file implements:
• a set of APIs allowing to access to the LoRaMAC services
• the LoRa certification test cases that are not visible to the application layer
Advanced mode
In this mode, the user accesses directly the MAC layer by including the MAC in the user file.
Operation model
The operation model proposed for this LoRa End_Node (see Figure 13) is based on ‘event-
driven’ paradigms including ‘time-driven’. The behavior of the system LoRa is triggered
either by a timer event or by a radio event plus a guard transition.
ZĞƐĞƚ
+$/,QLWLDOL]DWLRQ
+:,QLWLDOL]DWLRQ
/R5DVWDFN,QLWLDOL]DWLRQ
/R5D-RLQ6WDUW
/R5D,QLW7[(YHQW
3URFHVV(YHQW ',6$%/(B,54
(1$%/(B,54 /RZ3RZHU0RGH
ĞǀĞŶƚ
WƌŽĐĞƐƐǀĞŶƚ
WƌŽĐĞƐƐǀĞŶƚ
zĞƐ EŽ
ZĂĚŝŽŽƌ
ůĂƐƐǀƚ
WƌŽĐĞƐƐddžŽƌZdž
ǀĞŶƚ
WƌŽĐĞƐƐdŝŵĞƌ
ǀĞŶƚ
WƌŽĐĞƐƐůĂƐƐ
ǀĞŶƚ
06Y9
6OHHS
1:.B-2,1('7LPHU(YHQW
5HVHW
1:.B-2,1('7LPHU(YHQW
-RLQHG ,QLW
7[1H[W7[1H[W3DFNHW 27$$-RLQ7LPHU
7UXHSULQWI 'XW\&\FOH(QDEOH
1:.B-2,1('2Q6HQG(YHQW
-RLQ
6HQG
$%39RLG
06Y9
application server must use End-Device's Class A uplink to send back a downlink
frame to the application layer.
On MLME Beacon_Acquisition_req the End-Device LoRa Class B system state goes into a
BEACON_STATE_ACQUISITION.
The LoRa End-Device starts the beacon acquisition. When the MAC layer has received a
beacon in function RxBeacon successfully, the next state is BEACON_STATE_LOCKED.
The LoRa End-Device has received a beacon. Acquisition is no longer pending then the
MAC layer goes in BEACON_STATE_IDLE.
In BEACON_STATE_IDLE, the MAC layer compares the BeaconEventTime with the current
End-Device Time. If the beaconEventTime is less than current End-Device Time the MAC
layer goes in BEACON_STATE_REACQUISITION else it goes in
BEACON_STATE_GUARD. Then it performs a new Beacon Acquisition.
If the MAC layer does not find a beacon, the state machine stays in state
BEACON_STATE_ACQUISITION. This state detects that an acquisition was pending
previously and changes the next state to BEACON_STATE_LOST.
When the MAC layer receives a bad beacon format, it must go to
BEACON_STATE_TIMEOUT. It enlarges window timeouts to increase the chance to receive
the next beacon and goes in BEACON_STATE_REACQUISITION.
%HDFRQ)RUPDW 2.
0/0(B%HDFRQB$FTXLVLWLRQBUHT
%HDFRQ
7LPH2XW
%HDFRQQRWUHFHLYHG (QODUJH:LQGRZ7PHRXW
%HDFRQ)RUPDW 2.
%HDFRQ
$FTXLVLWLRQ
0/0(B%HDFRQB$FTXLVLWLRQB,QG &XUUHQW7LPH!%HDFRQB/HVVB3HULRG
%HDFRQ
/RFNHG %HDFRQ$FT3HQGLQJ %HDFRQ
5HDFTXLVLWLRQ
%HDFRQ
0/0(B%HDFRQB$FTXLVLWLRQBFQI /RVW
&XUUHQW7LPH%HDFRQB/HVVB3HULRG
%HDFRQ6HWXS
%HDFRQ
,GOH
%HDFRQ(YHQ7LPH&XUUHQW7LPH
%HDFRQ
*XDUG
%HDFRQ(YHQ7LPH!&XUUHQW7LPH
06Y9
5 Example description
The I-CUBE-LRWAN Expansion Package can easily be tailored to any other supported
device and development board.
The main characteristics of the LoRa radio expansion board are described in Table 40.
SX1276MB1MAS 868 MHz (HF) at 14 dBm and 433 MHz (LF) at 14 dBm
SX1276MB1LAS 915 MHz (HF) at 20 dBm and 433 MHz (LF) at 14 dBm
SX1272MB2DAS 915 MHz and 868 MHz at 14 dBm
SX1261DVK1BAS E406V03A sx1261, 14 dBm, 868 MHz, XTAL
SX1262DVK1CAS E428V03A sx1262, 22 dBm, 915 MHz, XTAL
SX1262DVK1DAS E449V01A sx1262, 22 dBm, 860-930 MHz, TCXO
Interrupts
Table 41 shows the interrupt priorities level applicable for the Cortex system processor
exception and for the STM32L0 Series LoRa application-specific interrupt (IRQ).
RTC 0 NA
EXTI2_3 0 NA
EXTI4_15 0 NA
18&/(2/5 /RUDH[SDQVLRQERDUG
,18&/(2/5:$1
$7B0$67(5 /RUD0RGHP
DSSOLFDWLRQ $7FRPPDQG
/RUD:DQ 6WDFN
70
LQFOXGHGLQ RYHU8$57
5DGLR
,&8%(/5:$1
06Y9
The interface between the LoRa modem and the LoRa host is a UART running AT
commands.
06Y9
Note: The maximum payload length allowed depends on both the region and the selected data
rate, so the payload format must be carefully designed according to these parameters.
/R5DHQGGHYLFH /R5DHQGGHYLFH
&RP3RUW &RP3RUW
67018&/(25DGLR 67018&/(25DGLR
H[SDQVLRQERDUG H[SDQVLRQERDUG
06Y9
For more details, refer to the application note AN4967 which gives the list of AT commands
and their description.
Note: The NUCLEO board communicates with expansion board via UART (PA2,PA3). The
following modifications must be applied: (cf. chapter 5.8 of UM1724).
• SB62 and SB63 should be closed
• SB13 and SB14 should be opened to disconnect STM32 UART from STLINK
6 System performances
0&8
',2
7['RQH 5['RQH
06Y9
Measurements setup:
• No DEBUG
• No TRACE
• No SENSOR_ENABLED
Measurements results:
• Typical consumption in stop mode: 1.3 µA
• Typical consumption in run mode: 8.0 mA
Measurements figures:
• Instantaneous consumption over 30 seconds
The Figure 20 shows an example of the current consumption against time on a
microcontroller of the STM32L0 Series.
7 Revision history
Added:
– New modem reference: expansion board featuring the
RiSiNGHF® modem RHF0M003
14-Dec-2017 6 Updated:
– New architecture design (LoRa FSM removed)
– Figure 10: Project files structure
– Figure 13: Operation model
Added:
– New expansion boards
4-Jul-2018 7 – introduction of LoRaWAN Class B mode
Updated:
– Figure 10 to Figure 17, Table 4, Table 10 to Table 45
Removed:
13-Dec-2018 8 – Class B restriction regarding AT commands in
Section 5.6: AT_Slave application description
STMicroelectronics NV and its subsidiaries (“ST”) reserve the right to make changes, corrections, enhancements, modifications, and
improvements to ST products and/or to this document at any time without notice. Purchasers should obtain the latest relevant information on
ST products before placing orders. ST products are sold pursuant to ST’s terms and conditions of sale in place at the time of order
acknowledgement.
Purchasers are solely responsible for the choice, selection, and use of ST products and ST assumes no liability for application assistance or
the design of Purchasers’ products.
Resale of ST products with provisions different from the information set forth herein shall void any warranty granted by ST for such product.
ST and the ST logo are trademarks of ST. All other product or service names are the property of their respective owners.
Information in this document supersedes and replaces information previously supplied in any prior versions of this document.