Skip to content

Commit e116a20

Browse files
[STM32WLx] Add examples
Co-Authored-By: jgromes <jan.gromes@gmail.com>
1 parent e52ffb0 commit e116a20

6 files changed

Lines changed: 693 additions & 0 deletions

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
RadioLib STM32WLx Channel Activity Detection Example
3+
4+
This example uses STM32WLx to scan the current LoRa
5+
channel and detect ongoing LoRa transmissions.
6+
Unlike SX127x CAD, SX126x/STM32WLx can detect any part
7+
of LoRa transmission, not just the preamble.
8+
9+
For default module settings, see the wiki page
10+
https://github.com/jgromes/RadioLib/wiki/Default-configuration#sx126x---lora-modem
11+
12+
For full API reference, see the GitHub Pages
13+
https://jgromes.github.io/RadioLib/
14+
*/
15+
16+
// include the library
17+
#include <RadioLib.h>
18+
19+
// no need to configure pins, signals are routed to the radio internally
20+
STM32WLx radio = new STM32WLx_Module();
21+
22+
23+
void setup() {
24+
Serial.begin(9600);
25+
26+
// initialize STM32WLx with default settings
27+
Serial.print(F("[STM32WLx] Initializing ... "));
28+
int state = radio.begin(868.0);
29+
if (state == RADIOLIB_ERR_NONE) {
30+
Serial.println(F("success!"));
31+
} else {
32+
Serial.print(F("failed, code "));
33+
Serial.println(state);
34+
while (true);
35+
}
36+
}
37+
38+
void loop() {
39+
Serial.print(F("[STM32WLx] Scanning channel for LoRa transmission ... "));
40+
41+
// start scanning current channel
42+
int state = radio.scanChannel();
43+
44+
if (state == RADIOLIB_LORA_DETECTED) {
45+
// LoRa preamble was detected
46+
Serial.println(F("detected!"));
47+
48+
} else if (state == RADIOLIB_CHANNEL_FREE) {
49+
// no preamble was detected, channel is free
50+
Serial.println(F("channel is free!"));
51+
52+
} else {
53+
// some other error occurred
54+
Serial.print(F("failed, code "));
55+
Serial.println(state);
56+
57+
}
58+
59+
// wait 100 ms before new scan
60+
delay(100);
61+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
RadioLib STM32WLx Channel Activity Detection Example
3+
4+
This example uses STM32WLx to scan the current LoRa
5+
channel and detect ongoing LoRa transmissions.
6+
Unlike SX127x CAD, SX126x/STM32WLx can detect any part
7+
of LoRa transmission, not just the preamble.
8+
9+
For default module settings, see the wiki page
10+
https://github.com/jgromes/RadioLib/wiki/Default-configuration#sx126x---lora-modem
11+
12+
For full API reference, see the GitHub Pages
13+
https://jgromes.github.io/RadioLib/
14+
*/
15+
16+
// include the library
17+
#include <RadioLib.h>
18+
19+
// no need to configure pins, signals are routed to the radio internally
20+
STM32WLx radio = new STM32WLx_Module();
21+
22+
23+
void setup() {
24+
Serial.begin(9600);
25+
26+
// initialize STM32WLx with default settings
27+
Serial.print(F("[STM32WLx] Initializing ... "));
28+
int state = radio.begin(868.0);
29+
if (state == RADIOLIB_ERR_NONE) {
30+
Serial.println(F("success!"));
31+
} else {
32+
Serial.print(F("failed, code "));
33+
Serial.println(state);
34+
while (true);
35+
}
36+
37+
// set the function that will be called
38+
// when LoRa packet or timeout is detected
39+
radio.setDio1Action(setFlag);
40+
41+
// start scanning the channel
42+
Serial.print(F("[STM32WLx] Starting scan for LoRa preamble ... "));
43+
state = radio.startChannelScan();
44+
if (state == RADIOLIB_ERR_NONE) {
45+
Serial.println(F("success!"));
46+
} else {
47+
Serial.print(F("failed, code "));
48+
Serial.println(state);
49+
}
50+
}
51+
52+
// flag to indicate that a packet was detected or CAD timed out
53+
volatile bool scanFlag = false;
54+
55+
// this function is called when a complete packet
56+
// is received by the module
57+
// IMPORTANT: this function MUST be 'void' type
58+
// and MUST NOT have any arguments!
59+
void setFlag(void) {
60+
// something happened, set the flag
61+
scanFlag = true;
62+
}
63+
64+
void loop() {
65+
// check if the flag is set
66+
if(scanFlag) {
67+
// reset flag
68+
scanFlag = false;
69+
70+
// check CAD result
71+
int state = radio.getChannelScanResult();
72+
73+
if (state == RADIOLIB_LORA_DETECTED) {
74+
// LoRa packet was detected
75+
Serial.println(F("[STM32WLx] Packet detected!"));
76+
77+
} else if (state == RADIOLIB_CHANNEL_FREE) {
78+
// channel is free
79+
Serial.println(F("[STM32WLx] Channel is free!"));
80+
81+
} else {
82+
// some other error occurred
83+
Serial.print(F("[STM32WLx] Failed, code "));
84+
Serial.println(state);
85+
86+
}
87+
88+
// start scanning the channel again
89+
Serial.print(F("[STM32WLx] Starting scan for LoRa preamble ... "));
90+
state = radio.startChannelScan();
91+
if (state == RADIOLIB_ERR_NONE) {
92+
Serial.println(F("success!"));
93+
} else {
94+
Serial.print(F("failed, code "));
95+
Serial.println(state);
96+
}
97+
}
98+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
RadioLib STM32WLx Receive Example
3+
4+
This example listens for LoRa transmissions using STM32WL MCU with
5+
integrated (SX126x) LoRa radio.
6+
7+
To successfully receive data, the following settings have to be the same
8+
on both transmitter and receiver:
9+
- carrier frequency
10+
- bandwidth
11+
- spreading factor
12+
- coding rate
13+
- sync word
14+
- preamble length
15+
16+
This example assumes Nucleo WL55JC1 is used. For other Nucleo boards
17+
or standalone STM32WL, some configuration such as TCXO voltage and
18+
RF switch control may have to be adjusted.
19+
20+
For default module settings, see the wiki page
21+
https://github.com/jgromes/RadioLib/wiki/Default-configuration#sx126x---lora-modem
22+
23+
For full API reference, see the GitHub Pages
24+
https://jgromes.github.io/RadioLib/
25+
*/
26+
27+
// include the library
28+
#include <RadioLib.h>
29+
30+
// no need to configure pins, signals are routed to the radio internally
31+
STM32WLx radio = new STM32WLx_Module();
32+
33+
// set RF switch configuration for Nucleo WL55JC1
34+
// NOTE: other boards may be different!
35+
static const RADIOLIB_PIN_TYPE rfswitch_pins[] =
36+
{PC3, PC4, PC5};
37+
static const Module::RfSwitchMode_t rfswitch_table[] = {
38+
{STM32WLx::MODE_IDLE, {LOW, LOW, LOW}},
39+
{STM32WLx::MODE_RX, {HIGH, HIGH, LOW}},
40+
{STM32WLx::MODE_TX_LP, {HIGH, HIGH, HIGH}},
41+
{STM32WLx::MODE_TX_HP, {HIGH, LOW, HIGH}},
42+
STM32WLx::END_OF_MODE_TABLE,
43+
};
44+
45+
void setup() {
46+
Serial.begin(9600);
47+
48+
// set RF switch control configuration
49+
// this has to be done prior to calling begin()
50+
radio.setRfSwitchTable(rfswitch_pins, rfswitch_table);
51+
52+
// initialize STM32WL with default settings, except frequency
53+
Serial.print(F("[STM32WL] Initializing ... "));
54+
int state = radio.begin(868.0);
55+
if (state == RADIOLIB_ERR_NONE) {
56+
Serial.println(F("success!"));
57+
} else {
58+
Serial.print(F("failed, code "));
59+
Serial.println(state);
60+
while (true);
61+
}
62+
63+
// set appropriate TCXO voltage for Nucleo WL55JC1
64+
state = radio.setTCXO(1.7);
65+
if (state == RADIOLIB_ERR_NONE) {
66+
Serial.println(F("success!"));
67+
} else {
68+
Serial.print(F("failed, code "));
69+
Serial.println(state);
70+
while (true);
71+
}
72+
}
73+
74+
void loop() {
75+
Serial.print(F("[STM32WL] Waiting for incoming transmission ... "));
76+
77+
// you can receive data as an Arduino String
78+
// NOTE: receive() is a blocking method!
79+
// See example ReceiveInterrupt for details
80+
// on non-blocking reception method.
81+
String str;
82+
int state = radio.receive(str);
83+
84+
// you can also receive data as byte array
85+
/*
86+
byte byteArr[8];
87+
int state = radio.receive(byteArr, 8);
88+
*/
89+
90+
if (state == RADIOLIB_ERR_NONE) {
91+
// packet was successfully received
92+
Serial.println(F("success!"));
93+
94+
// print the data of the packet
95+
Serial.print(F("[STM32WL] Data:\t\t"));
96+
Serial.println(str);
97+
98+
// print the RSSI (Received Signal Strength Indicator)
99+
// of the last received packet
100+
Serial.print(F("[STM32WL] RSSI:\t\t"));
101+
Serial.print(radio.getRSSI());
102+
Serial.println(F(" dBm"));
103+
104+
// print the SNR (Signal-to-Noise Ratio)
105+
// of the last received packet
106+
Serial.print(F("[STM32WL] SNR:\t\t"));
107+
Serial.print(radio.getSNR());
108+
Serial.println(F(" dB"));
109+
110+
} else if (state == RADIOLIB_ERR_RX_TIMEOUT) {
111+
// timeout occurred while waiting for a packet
112+
Serial.println(F("timeout!"));
113+
114+
} else if (state == RADIOLIB_ERR_CRC_MISMATCH) {
115+
// packet was received, but is malformed
116+
Serial.println(F("CRC error!"));
117+
118+
} else {
119+
// some other error occurred
120+
Serial.print(F("failed, code "));
121+
Serial.println(state);
122+
123+
}
124+
}

0 commit comments

Comments
 (0)