Skip to content

Commit 04a895f

Browse files
authored
Merge pull request stm32duino#11 from cparata/master
Improve STM32duinoBLE robustness and flexibility
2 parents 495b941 + bd1b0e0 commit 04a895f

27 files changed

+637
-855
lines changed

.github/workflows/compile-examples.yml

+3-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ jobs:
77
strategy:
88
matrix:
99
fqbn: [
10-
"arduino:samd:mkrwifi1010",
11-
"arduino:samd:nano_33_iot",
12-
"arduino:megaavr:uno2018:mode=on",
13-
"arduino:mbed:nano33ble"
10+
'"STM32:stm32:Eval:pnum=STEVAL_MKSBOX1V1,usb=CDCgen" "https://github.com/stm32duino/BoardManagerFiles/raw/master/STM32/package_stm_index.json"',
11+
'"STM32:stm32:Nucleo_64:pnum=NUCLEO_L476RG" "https://github.com/stm32duino/BoardManagerFiles/raw/master/STM32/package_stm_index.json"',
12+
'"STM32:stm32:Disco:pnum=DISCO_L475VG_IOT" "https://github.com/stm32duino/BoardManagerFiles/raw/master/STM32/package_stm_index.json"'
1413
]
1514

1615
steps:

README.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
# STM32duinoBLE
22

33
This library is a fork of ArduinoBLE library to add the support of SPBTLE-RF and SPBTLE-1S BLE modules.
4-
It was successfully tested with the X-NUCLEO-IDB05A1 expansion board and a NUCLEO-F401RE or NUCLEO-L476RG or
5-
NUCLEO-L053R8, with B-L475E-IOT01A and with STEVAL-MKSBOX1V1.
4+
It was successfully tested with the X-NUCLEO-IDB05A1 or X-NUCLEO-BNRG2A1 expansion board and a NUCLEO-F401RE
5+
or NUCLEO-L476RG or NUCLEO-L053R8, with B-L475E-IOT01A and with STEVAL-MKSBOX1V1.
66
In order to use this library with STEVAL-MKSBOX1V1, you need to update the firmware of the SPBTLE-1S BLE module
77
mounted on that board as described in the following wiki page:
88

99
https://github.com/stm32duino/wiki/wiki/STM32duinoBLE-with-STEVAL_MKSBOX1V1
1010

11-
You can find the official Pull Request at the following link:
11+
In order to use this library with X-NUCLEO-BNRG2A1, you need to update the firmware of the BLUENRG-M2SP BLE module
12+
mounted on that expansion board as described in the following wiki page:
1213

13-
https://github.com/arduino-libraries/ArduinoBLE/pull/26
14+
https://github.com/stm32duino/wiki/wiki/STM32duinoBLE-with-X_NUCLEO_BNRG2A1
1415

1516
For more information about ArduinoBLE library please visit the official web page at:
1617
https://github.com/arduino-libraries/ArduinoBLE

examples/Central/LedControl/LedControl.ino

+73-9
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
it will remotely control the BLE Peripheral's LED, when the button is pressed or released.
77
88
The circuit:
9-
- Arduino MKR WiFi 1010, Arduino Uno WiFi Rev2 board, Arduino Nano 33 IoT,
10-
Arduino Nano 33 BLE, or Arduino Nano 33 BLE Sense board.
11-
- Button with pull-up resistor connected to pin 2.
9+
- STEVAL-MKSBOX1V1, B-L475E-IOT01A1, or a Nucleo board plus the X-NUCLEO-IDB05A1 or the X-NUCLEO-BNRG2A1
1210
1311
You can use it with another board that is compatible with this library and the
1412
Peripherals -> LED example.
@@ -18,12 +16,47 @@
1816

1917
#include <ArduinoBLE.h>
2018

19+
#if defined(ARDUINO_STEVAL_MKSBOX1V1)
20+
/* STEVAL-MKSBOX1V1 */
21+
SPIClass SpiHCI(PC3, PD3, PD1);
22+
HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_1S, PD0, PD4, PA8, 1000000, SPI_MODE1);
23+
BLELocalDevice BLE(&HCISpiTransport);
24+
const int buttonPin = PG1; // set buttonPin to digital pin PG1
25+
#elif defined(ARDUINO_DISCO_L475VG_IOT)
26+
/* B-L475E-IOT01A1 */
27+
SPIClass SpiHCI(PC12, PC11, PC10);
28+
HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, PD13, PE6, PA8, 8000000, SPI_MODE0);
29+
BLELocalDevice BLE(&HCISpiTransport);
30+
const int buttonPin = PC13; // set buttonPin to digital pin PC13
31+
#else
32+
/* Shield IDB05A1 with SPI clock on D3 */
33+
SPIClass SpiHCI(D11, D12, D3);
34+
HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, A1, A0, D7, 8000000, SPI_MODE0);
35+
BLELocalDevice BLE(&HCISpiTransport);
36+
const int buttonPin = PC13; // set buttonPin to digital pin PC13
37+
/* Shield IDB05A1 with SPI clock on D13 */
38+
/*#define SpiHCI SPI
39+
HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, A1, A0, D7, 8000000, SPI_MODE0);
40+
BLELocalDevice BLE(&HCISpiTransport);
41+
const int buttonPin = PC13; // set buttonPin to digital pin PC13 */
42+
/* Shield BNRG2A1 with SPI clock on D3 */
43+
/*SPIClass SpiHCI(D11, D12, D3);
44+
HCISpiTransportClass HCISpiTransport(SpiHCI, BLUENRG_M2SP, A1, A0, D7, 1000000, SPI_MODE1);
45+
BLELocalDevice BLE(&HCISpiTransport);
46+
const int buttonPin = PC13; // set buttonPin to digital pin PC13 */
47+
/* Shield BNRG2A1 with SPI clock on D13 */
48+
/*#define SpiHCI SPI
49+
HCISpiTransportClass HCISpiTransport(SpiHCI, BLUENRG_M2SP, A1, A0, D7, 1000000, SPI_MODE1);
50+
BLELocalDevice BLE(&HCISpiTransport);
51+
const int buttonPin = PC13; // set buttonPin to digital pin PC13 */
52+
#endif
53+
2154
// variables for button
22-
const int buttonPin = 2;
2355
int oldButtonState = LOW;
56+
int initialButtonState = LOW;
2457

2558
void setup() {
26-
Serial.begin(9600);
59+
Serial.begin(115200);
2760
while (!Serial);
2861

2962
// configure the button pin as input
@@ -32,10 +65,23 @@ void setup() {
3265
// initialize the BLE hardware
3366
BLE.begin();
3467

68+
// Get initial button state
69+
initialButtonState = digitalRead(buttonPin);
70+
oldButtonState = initialButtonState;
71+
3572
Serial.println("BLE Central - LED control");
3673

3774
// start scanning for peripherals
38-
BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214");
75+
int ret = 1;
76+
do
77+
{
78+
ret = BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214");
79+
if (ret == 0)
80+
{
81+
BLE.end();
82+
BLE.begin();
83+
}
84+
} while(ret == 0);
3985
}
4086

4187
void loop() {
@@ -57,12 +103,30 @@ void loop() {
57103
}
58104

59105
// stop scanning
60-
BLE.stopScan();
106+
int ret = 1;
107+
do
108+
{
109+
ret = BLE.stopScan();
110+
if (ret == 0)
111+
{
112+
BLE.end();
113+
BLE.begin();
114+
}
115+
} while(ret == 0);
61116

62117
controlLed(peripheral);
63118

64119
// peripheral disconnected, start scanning again
65-
BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214");
120+
ret = 1;
121+
do
122+
{
123+
ret = BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214");
124+
if (ret == 0)
125+
{
126+
BLE.end();
127+
BLE.begin();
128+
}
129+
} while(ret == 0);
66130
}
67131
}
68132

@@ -110,7 +174,7 @@ void controlLed(BLEDevice peripheral) {
110174
// button changed
111175
oldButtonState = buttonState;
112176

113-
if (buttonState) {
177+
if (buttonState != initialButtonState) {
114178
Serial.println("button pressed");
115179

116180
// button is pressed, write 0x01 to turn the LED on

examples/Central/PeripheralExplorer/PeripheralExplorer.ino

+51-5
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
is found. Then connects, and discovers + prints all the peripheral's attributes.
66
77
The circuit:
8-
- Arduino MKR WiFi 1010, Arduino Uno WiFi Rev2 board, Arduino Nano 33 IoT,
9-
Arduino Nano 33 BLE, or Arduino Nano 33 BLE Sense board.
8+
- STEVAL-MKSBOX1V1, B-L475E-IOT01A1, or a Nucleo board plus the X-NUCLEO-IDB05A1 or the X-NUCLEO-BNRG2A1
109
1110
You can use it with another board that is compatible with this library and the
1211
Peripherals -> LED example.
@@ -16,8 +15,37 @@
1615

1716
#include <ArduinoBLE.h>
1817

18+
#if defined(ARDUINO_STEVAL_MKSBOX1V1)
19+
/* STEVAL-MKSBOX1V1 */
20+
SPIClass SpiHCI(PC3, PD3, PD1);
21+
HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_1S, PD0, PD4, PA8, 1000000, SPI_MODE1);
22+
BLELocalDevice BLE(&HCISpiTransport);
23+
#elif defined(ARDUINO_DISCO_L475VG_IOT)
24+
/* B-L475E-IOT01A1 */
25+
SPIClass SpiHCI(PC12, PC11, PC10);
26+
HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, PD13, PE6, PA8, 8000000, SPI_MODE0);
27+
BLELocalDevice BLE(&HCISpiTransport);
28+
#else
29+
/* Shield IDB05A1 with SPI clock on D3 */
30+
SPIClass SpiHCI(D11, D12, D3);
31+
HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, A1, A0, D7, 8000000, SPI_MODE0);
32+
BLELocalDevice BLE(&HCISpiTransport);
33+
/* Shield IDB05A1 with SPI clock on D13 */
34+
/*#define SpiHCI SPI
35+
HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, A1, A0, D7, 8000000, SPI_MODE0);
36+
BLELocalDevice BLE(&HCISpiTransport); */
37+
/* Shield BNRG2A1 with SPI clock on D3 */
38+
/*SPIClass SpiHCI(D11, D12, D3);
39+
HCISpiTransportClass HCISpiTransport(SpiHCI, BLUENRG_M2SP, A1, A0, D7, 1000000, SPI_MODE1);
40+
BLELocalDevice BLE(&HCISpiTransport); */
41+
/* Shield BNRG2A1 with SPI clock on D13 */
42+
/*#define SpiHCI SPI
43+
HCISpiTransportClass HCISpiTransport(SpiHCI, BLUENRG_M2SP, A1, A0, D7, 1000000, SPI_MODE1);
44+
BLELocalDevice BLE(&HCISpiTransport); */
45+
#endif
46+
1947
void setup() {
20-
Serial.begin(9600);
48+
Serial.begin(115200);
2149
while (!Serial);
2250

2351
// begin initialization
@@ -30,7 +58,16 @@ void setup() {
3058
Serial.println("BLE Central - Peripheral Explorer");
3159

3260
// start scanning for peripherals
33-
BLE.scan();
61+
int ret = 1;
62+
do
63+
{
64+
ret = BLE.scan();
65+
if (ret == 0)
66+
{
67+
BLE.end();
68+
BLE.begin();
69+
}
70+
} while(ret == 0);
3471
}
3572

3673
void loop() {
@@ -50,7 +87,16 @@ void loop() {
5087
// see if peripheral is a LED
5188
if (peripheral.localName() == "LED") {
5289
// stop scanning
53-
BLE.stopScan();
90+
int ret = 1;
91+
do
92+
{
93+
ret = BLE.stopScan();
94+
if (ret == 0)
95+
{
96+
BLE.end();
97+
BLE.begin();
98+
}
99+
} while(ret == 0);
54100

55101
explorerPeripheral(peripheral);
56102

examples/Central/Scan/Scan.ino

+42-5
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,44 @@
55
address, local name, adverised service UUID's.
66
77
The circuit:
8-
- Arduino MKR WiFi 1010, Arduino Uno WiFi Rev2 board, Arduino Nano 33 IoT,
9-
Arduino Nano 33 BLE, or Arduino Nano 33 BLE Sense board.
8+
- STEVAL-MKSBOX1V1, B-L475E-IOT01A1, or a Nucleo board plus the X-NUCLEO-IDB05A1 or the X-NUCLEO-BNRG2A1
109
1110
This example code is in the public domain.
1211
*/
1312

1413
#include <ArduinoBLE.h>
1514

15+
#if defined(ARDUINO_STEVAL_MKSBOX1V1)
16+
/* STEVAL-MKSBOX1V1 */
17+
SPIClass SpiHCI(PC3, PD3, PD1);
18+
HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_1S, PD0, PD4, PA8, 1000000, SPI_MODE1);
19+
BLELocalDevice BLE(&HCISpiTransport);
20+
#elif defined(ARDUINO_DISCO_L475VG_IOT)
21+
/* B-L475E-IOT01A1 */
22+
SPIClass SpiHCI(PC12, PC11, PC10);
23+
HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, PD13, PE6, PA8, 8000000, SPI_MODE0);
24+
BLELocalDevice BLE(&HCISpiTransport);
25+
#else
26+
/* Shield IDB05A1 with SPI clock on D3 */
27+
SPIClass SpiHCI(D11, D12, D3);
28+
HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, A1, A0, D7, 8000000, SPI_MODE0);
29+
BLELocalDevice BLE(&HCISpiTransport);
30+
/* Shield IDB05A1 with SPI clock on D13 */
31+
/*#define SpiHCI SPI
32+
HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, A1, A0, D7, 8000000, SPI_MODE0);
33+
BLELocalDevice BLE(&HCISpiTransport); */
34+
/* Shield BNRG2A1 with SPI clock on D3 */
35+
/*SPIClass SpiHCI(D11, D12, D3);
36+
HCISpiTransportClass HCISpiTransport(SpiHCI, BLUENRG_M2SP, A1, A0, D7, 1000000, SPI_MODE1);
37+
BLELocalDevice BLE(&HCISpiTransport); */
38+
/* Shield BNRG2A1 with SPI clock on D13 */
39+
/*#define SpiHCI SPI
40+
HCISpiTransportClass HCISpiTransport(SpiHCI, BLUENRG_M2SP, A1, A0, D7, 1000000, SPI_MODE1);
41+
BLELocalDevice BLE(&HCISpiTransport); */
42+
#endif
43+
1644
void setup() {
17-
Serial.begin(9600);
45+
Serial.begin(115200);
1846
while (!Serial);
1947

2048
// begin initialization
@@ -26,8 +54,17 @@ void setup() {
2654

2755
Serial.println("BLE Central scan");
2856

29-
// start scanning for peripheral
30-
BLE.scan();
57+
// start scanning for peripherals
58+
int ret = 1;
59+
do
60+
{
61+
ret = BLE.scan();
62+
if (ret == 0)
63+
{
64+
BLE.end();
65+
BLE.begin();
66+
}
67+
} while(ret == 0);
3168
}
3269

3370
void loop() {

examples/Central/ScanCallback/ScanCallback.ino

+41-4
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,44 @@
77
reported for every single advertisement it makes.
88
99
The circuit:
10-
- Arduino MKR WiFi 1010, Arduino Uno WiFi Rev2 board, Arduino Nano 33 IoT,
11-
Arduino Nano 33 BLE, or Arduino Nano 33 BLE Sense board.
10+
- STEVAL-MKSBOX1V1, B-L475E-IOT01A1, or a Nucleo board plus the X-NUCLEO-IDB05A1 or the X-NUCLEO-BNRG2A1
1211
1312
This example code is in the public domain.
1413
*/
1514

1615
#include <ArduinoBLE.h>
1716

17+
#if defined(ARDUINO_STEVAL_MKSBOX1V1)
18+
/* STEVAL-MKSBOX1V1 */
19+
SPIClass SpiHCI(PC3, PD3, PD1);
20+
HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_1S, PD0, PD4, PA8, 1000000, SPI_MODE1);
21+
BLELocalDevice BLE(&HCISpiTransport);
22+
#elif defined(ARDUINO_DISCO_L475VG_IOT)
23+
/* B-L475E-IOT01A1 */
24+
SPIClass SpiHCI(PC12, PC11, PC10);
25+
HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, PD13, PE6, PA8, 8000000, SPI_MODE0);
26+
BLELocalDevice BLE(&HCISpiTransport);
27+
#else
28+
/* Shield IDB05A1 with SPI clock on D3 */
29+
SPIClass SpiHCI(D11, D12, D3);
30+
HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, A1, A0, D7, 8000000, SPI_MODE0);
31+
BLELocalDevice BLE(&HCISpiTransport);
32+
/* Shield IDB05A1 with SPI clock on D13 */
33+
/*#define SpiHCI SPI
34+
HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, A1, A0, D7, 8000000, SPI_MODE0);
35+
BLELocalDevice BLE(&HCISpiTransport); */
36+
/* Shield BNRG2A1 with SPI clock on D3 */
37+
/*SPIClass SpiHCI(D11, D12, D3);
38+
HCISpiTransportClass HCISpiTransport(SpiHCI, BLUENRG_M2SP, A1, A0, D7, 1000000, SPI_MODE1);
39+
BLELocalDevice BLE(&HCISpiTransport); */
40+
/* Shield BNRG2A1 with SPI clock on D13 */
41+
/*#define SpiHCI SPI
42+
HCISpiTransportClass HCISpiTransport(SpiHCI, BLUENRG_M2SP, A1, A0, D7, 1000000, SPI_MODE1);
43+
BLELocalDevice BLE(&HCISpiTransport); */
44+
#endif
45+
1846
void setup() {
19-
Serial.begin(9600);
47+
Serial.begin(115200);
2048
while (!Serial);
2149

2250
// begin initialization
@@ -32,7 +60,16 @@ void setup() {
3260
BLE.setEventHandler(BLEDiscovered, bleCentralDiscoverHandler);
3361

3462
// start scanning for peripherals with duplicates
35-
BLE.scan(true);
63+
int ret = 1;
64+
do
65+
{
66+
ret = BLE.scan(true);
67+
if (ret == 0)
68+
{
69+
BLE.end();
70+
BLE.begin();
71+
}
72+
} while(ret == 0);
3673
}
3774

3875
void loop() {

0 commit comments

Comments
 (0)