From c4f359a90e97b3574dc524b2821f35eb9ac32806 Mon Sep 17 00:00:00 2001 From: Carlo Parata Date: Fri, 7 Feb 2020 11:20:49 +0100 Subject: [PATCH 01/22] Improve STM32duinoBLE robustness and flexibility --- README.md | 9 +- examples/Central/LedControl/LedControl.ino | 82 +++++- .../PeripheralExplorer/PeripheralExplorer.ino | 56 +++- examples/Central/Scan/Scan.ino | 47 ++- .../Central/ScanCallback/ScanCallback.ino | 45 ++- .../SensorTagButton/SensorTagButton.ino | 69 ++++- .../BatteryMonitor/BatteryMonitor.ino | 106 ------- examples/Peripheral/ButtonLED/ButtonLED.ino | 42 ++- .../Peripheral/CallbackLED/CallbackLED.ino | 34 ++- examples/Peripheral/LED/LED.ino | 35 ++- keywords.txt | 1 + src/ArduinoBLE.h | 1 + src/local/BLELocalDevice.cpp | 53 +--- src/local/BLELocalDevice.h | 10 +- src/utility/GAP.cpp | 20 +- src/utility/GAP.h | 2 +- src/utility/HCI.cpp | 54 ++-- src/utility/HCI.h | 5 + src/utility/HCICordioTransport.cpp | 271 ----------------- src/utility/HCICordioTransport.h | 54 ---- src/utility/HCISpiTransport.cpp | 276 +++++++++++------- src/utility/HCISpiTransport.h | 17 +- src/utility/HCIUartTransport.cpp | 103 ------- src/utility/HCIUartTransport.h | 46 --- 24 files changed, 603 insertions(+), 835 deletions(-) delete mode 100644 examples/Peripheral/BatteryMonitor/BatteryMonitor.ino delete mode 100644 src/utility/HCICordioTransport.cpp delete mode 100644 src/utility/HCICordioTransport.h delete mode 100644 src/utility/HCIUartTransport.cpp delete mode 100644 src/utility/HCIUartTransport.h diff --git a/README.md b/README.md index 0376b8e4..437e1ae3 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,17 @@ # STM32duinoBLE This library is a fork of ArduinoBLE library to add the support of SPBTLE-RF and SPBTLE-1S BLE modules. -It was successfully tested with the X-NUCLEO-IDB05A1 expansion board and a NUCLEO-F401RE or NUCLEO-L476RG or -NUCLEO-L053R8, with B-L475E-IOT01A and with STEVAL-MKSBOX1V1. +It was successfully tested with the X-NUCLEO-IDB05A1 or X-NUCLEO-BNRG2A1 expansion board and a NUCLEO-F401RE +or NUCLEO-L476RG or NUCLEO-L053R8, with B-L475E-IOT01A and with STEVAL-MKSBOX1V1. In order to use this library with STEVAL-MKSBOX1V1, you need to update the firmware of the SPBTLE-1S BLE module mounted on that board as described in the following wiki page: https://github.com/stm32duino/wiki/wiki/STM32duinoBLE-with-STEVAL_MKSBOX1V1 -You can find the official Pull Request at the following link: +In order to use this library with X-NUCLEO-BNRG2A1, you need to update the firmware of the BLUENRG-M2SP BLE module +mounted on that expansion board as described in the following wiki page: -https://github.com/arduino-libraries/ArduinoBLE/pull/26 +https://github.com/stm32duino/wiki/wiki/STM32duinoBLE-with-X_NUCLEO_BNRG2A1 For more information about ArduinoBLE library please visit the official web page at: https://github.com/arduino-libraries/ArduinoBLE diff --git a/examples/Central/LedControl/LedControl.ino b/examples/Central/LedControl/LedControl.ino index 8301a311..af082e00 100644 --- a/examples/Central/LedControl/LedControl.ino +++ b/examples/Central/LedControl/LedControl.ino @@ -6,9 +6,7 @@ it will remotely control the BLE Peripheral's LED, when the button is pressed or released. The circuit: - - Arduino MKR WiFi 1010, Arduino Uno WiFi Rev2 board, Arduino Nano 33 IoT, - Arduino Nano 33 BLE, or Arduino Nano 33 BLE Sense board. - - Button with pull-up resistor connected to pin 2. + - STEVAL-MKSBOX1V1, B-L475E-IOT01A1, or a Nucleo board plus the X-NUCLEO-IDB05A1 or the X-NUCLEO-BNRG2A1 You can use it with another board that is compatible with this library and the Peripherals -> LED example. @@ -18,12 +16,47 @@ #include +#if defined(ARDUINO_STEVAL_MKSBOX1V1) +/* STEVAL-MKSBOX1V1 */ +SPIClass SpiHCI(PC3, PD3, PD1); +HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_1S, PD0, PD4, PA8, 1000000, SPI_MODE1); +BLELocalDevice BLE(&HCISpiTransport); +const int buttonPin = PG1; // set buttonPin to digital pin PG1 +#elif defined(ARDUINO_DISCO_L475VG_IOT) +/* B-L475E-IOT01A1 */ +SPIClass SpiHCI(PC12, PC11, PC10); +HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, PD13, PE6, PA8, 8000000, SPI_MODE0); +BLELocalDevice BLE(&HCISpiTransport); +const int buttonPin = PC13; // set buttonPin to digital pin PC13 +#else +/* Shield IDB05A1 with SPI clock on D3 */ +SPIClass SpiHCI(D11, D12, D3); +HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, A1, A0, D7, 8000000, SPI_MODE0); +BLELocalDevice BLE(&HCISpiTransport); +const int buttonPin = PC13; // set buttonPin to digital pin PC13 +/* Shield IDB05A1 with SPI clock on D13 */ +/*SPIClass SpiHCI(D11, D12, D13); +HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, A1, A0, D7, 8000000, SPI_MODE0); +BLELocalDevice BLE(&HCISpiTransport); +const int buttonPin = PC13; // set buttonPin to digital pin PC13 */ +/* Shield BNRG2A1 with SPI clock on D3 */ +/*SPIClass SpiHCI(D11, D12, D3); +HCISpiTransportClass HCISpiTransport(SpiHCI, BLUENRG_M2SP, A1, A0, D7, 1000000, SPI_MODE1); +BLELocalDevice BLE(&HCISpiTransport); +const int buttonPin = PC13; // set buttonPin to digital pin PC13 */ +/* Shield BNRG2A1 with SPI clock on D13 */ +/*SPIClass SpiHCI(D11, D12, D13); +HCISpiTransportClass HCISpiTransport(SpiHCI, BLUENRG_M2SP, A1, A0, D7, 1000000, SPI_MODE1); +BLELocalDevice BLE(&HCISpiTransport); +const int buttonPin = PC13; // set buttonPin to digital pin PC13 */ +#endif + // variables for button -const int buttonPin = 2; int oldButtonState = LOW; +int initialButtonState = LOW; void setup() { - Serial.begin(9600); + Serial.begin(115200); while (!Serial); // configure the button pin as input @@ -32,10 +65,23 @@ void setup() { // initialize the BLE hardware BLE.begin(); + // Get initial button state + initialButtonState = digitalRead(buttonPin); + oldButtonState = initialButtonState; + Serial.println("BLE Central - LED control"); // start scanning for peripherals - BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214"); + int ret = 1; + do + { + ret = BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214"); + if (ret == 0) + { + BLE.end(); + BLE.begin(); + } + } while(ret == 0); } void loop() { @@ -57,12 +103,30 @@ void loop() { } // stop scanning - BLE.stopScan(); + int ret = 1; + do + { + ret = BLE.stopScan(); + if (ret == 0) + { + BLE.end(); + BLE.begin(); + } + } while(ret == 0); controlLed(peripheral); // peripheral disconnected, start scanning again - BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214"); + ret = 1; + do + { + ret = BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214"); + if (ret == 0) + { + BLE.end(); + BLE.begin(); + } + } while(ret == 0); } } @@ -110,7 +174,7 @@ void controlLed(BLEDevice peripheral) { // button changed oldButtonState = buttonState; - if (buttonState) { + if (buttonState != initialButtonState) { Serial.println("button pressed"); // button is pressed, write 0x01 to turn the LED on diff --git a/examples/Central/PeripheralExplorer/PeripheralExplorer.ino b/examples/Central/PeripheralExplorer/PeripheralExplorer.ino index 100225d6..357a8ef4 100644 --- a/examples/Central/PeripheralExplorer/PeripheralExplorer.ino +++ b/examples/Central/PeripheralExplorer/PeripheralExplorer.ino @@ -5,8 +5,7 @@ is found. Then connects, and discovers + prints all the peripheral's attributes. The circuit: - - Arduino MKR WiFi 1010, Arduino Uno WiFi Rev2 board, Arduino Nano 33 IoT, - Arduino Nano 33 BLE, or Arduino Nano 33 BLE Sense board. + - STEVAL-MKSBOX1V1, B-L475E-IOT01A1, or a Nucleo board plus the X-NUCLEO-IDB05A1 or the X-NUCLEO-BNRG2A1 You can use it with another board that is compatible with this library and the Peripherals -> LED example. @@ -16,8 +15,37 @@ #include +#if defined(ARDUINO_STEVAL_MKSBOX1V1) +/* STEVAL-MKSBOX1V1 */ +SPIClass SpiHCI(PC3, PD3, PD1); +HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_1S, PD0, PD4, PA8, 1000000, SPI_MODE1); +BLELocalDevice BLE(&HCISpiTransport); +#elif defined(ARDUINO_DISCO_L475VG_IOT) +/* B-L475E-IOT01A1 */ +SPIClass SpiHCI(PC12, PC11, PC10); +HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, PD13, PE6, PA8, 8000000, SPI_MODE0); +BLELocalDevice BLE(&HCISpiTransport); +#else +/* Shield IDB05A1 with SPI clock on D3 */ +SPIClass SpiHCI(D11, D12, D3); +HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, A1, A0, D7, 8000000, SPI_MODE0); +BLELocalDevice BLE(&HCISpiTransport); +/* Shield IDB05A1 with SPI clock on D13 */ +/*SPIClass SpiHCI(D11, D12, D13); +HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, A1, A0, D7, 8000000, SPI_MODE0); +BLELocalDevice BLE(&HCISpiTransport); */ +/* Shield BNRG2A1 with SPI clock on D3 */ +/*SPIClass SpiHCI(D11, D12, D3); +HCISpiTransportClass HCISpiTransport(SpiHCI, BLUENRG_M2SP, A1, A0, D7, 1000000, SPI_MODE1); +BLELocalDevice BLE(&HCISpiTransport); */ +/* Shield BNRG2A1 with SPI clock on D13 */ +/*SPIClass SpiHCI(D11, D12, D13); +HCISpiTransportClass HCISpiTransport(SpiHCI, BLUENRG_M2SP, A1, A0, D7, 1000000, SPI_MODE1); +BLELocalDevice BLE(&HCISpiTransport); */ +#endif + void setup() { - Serial.begin(9600); + Serial.begin(115200); while (!Serial); // begin initialization @@ -30,7 +58,16 @@ void setup() { Serial.println("BLE Central - Peripheral Explorer"); // start scanning for peripherals - BLE.scan(); + int ret = 1; + do + { + ret = BLE.scan(); + if (ret == 0) + { + BLE.end(); + BLE.begin(); + } + } while(ret == 0); } void loop() { @@ -50,7 +87,16 @@ void loop() { // see if peripheral is a LED if (peripheral.localName() == "LED") { // stop scanning - BLE.stopScan(); + int ret = 1; + do + { + ret = BLE.stopScan(); + if (ret == 0) + { + BLE.end(); + BLE.begin(); + } + } while(ret == 0); explorerPeripheral(peripheral); diff --git a/examples/Central/Scan/Scan.ino b/examples/Central/Scan/Scan.ino index 784f3058..5326514f 100644 --- a/examples/Central/Scan/Scan.ino +++ b/examples/Central/Scan/Scan.ino @@ -5,16 +5,44 @@ address, local name, adverised service UUID's. The circuit: - - Arduino MKR WiFi 1010, Arduino Uno WiFi Rev2 board, Arduino Nano 33 IoT, - Arduino Nano 33 BLE, or Arduino Nano 33 BLE Sense board. + - STEVAL-MKSBOX1V1, B-L475E-IOT01A1, or a Nucleo board plus the X-NUCLEO-IDB05A1 or the X-NUCLEO-BNRG2A1 This example code is in the public domain. */ #include +#if defined(ARDUINO_STEVAL_MKSBOX1V1) +/* STEVAL-MKSBOX1V1 */ +SPIClass SpiHCI(PC3, PD3, PD1); +HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_1S, PD0, PD4, PA8, 1000000, SPI_MODE1); +BLELocalDevice BLE(&HCISpiTransport); +#elif defined(ARDUINO_DISCO_L475VG_IOT) +/* B-L475E-IOT01A1 */ +SPIClass SpiHCI(PC12, PC11, PC10); +HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, PD13, PE6, PA8, 8000000, SPI_MODE0); +BLELocalDevice BLE(&HCISpiTransport); +#else +/* Shield IDB05A1 with SPI clock on D3 */ +SPIClass SpiHCI(D11, D12, D3); +HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, A1, A0, D7, 8000000, SPI_MODE0); +BLELocalDevice BLE(&HCISpiTransport); +/* Shield IDB05A1 with SPI clock on D13 */ +/*SPIClass SpiHCI(D11, D12, D13); +HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, A1, A0, D7, 8000000, SPI_MODE0); +BLELocalDevice BLE(&HCISpiTransport); */ +/* Shield BNRG2A1 with SPI clock on D3 */ +/*SPIClass SpiHCI(D11, D12, D3); +HCISpiTransportClass HCISpiTransport(SpiHCI, BLUENRG_M2SP, A1, A0, D7, 1000000, SPI_MODE1); +BLELocalDevice BLE(&HCISpiTransport); */ +/* Shield BNRG2A1 with SPI clock on D13 */ +/*SPIClass SpiHCI(D11, D12, D13); +HCISpiTransportClass HCISpiTransport(SpiHCI, BLUENRG_M2SP, A1, A0, D7, 1000000, SPI_MODE1); +BLELocalDevice BLE(&HCISpiTransport); */ +#endif + void setup() { - Serial.begin(9600); + Serial.begin(115200); while (!Serial); // begin initialization @@ -26,8 +54,17 @@ void setup() { Serial.println("BLE Central scan"); - // start scanning for peripheral - BLE.scan(); + // start scanning for peripherals + int ret = 1; + do + { + ret = BLE.scan(); + if (ret == 0) + { + BLE.end(); + BLE.begin(); + } + } while(ret == 0); } void loop() { diff --git a/examples/Central/ScanCallback/ScanCallback.ino b/examples/Central/ScanCallback/ScanCallback.ino index 9583079c..f84b88e1 100644 --- a/examples/Central/ScanCallback/ScanCallback.ino +++ b/examples/Central/ScanCallback/ScanCallback.ino @@ -7,16 +7,44 @@ reported for every single advertisement it makes. The circuit: - - Arduino MKR WiFi 1010, Arduino Uno WiFi Rev2 board, Arduino Nano 33 IoT, - Arduino Nano 33 BLE, or Arduino Nano 33 BLE Sense board. + - STEVAL-MKSBOX1V1, B-L475E-IOT01A1, or a Nucleo board plus the X-NUCLEO-IDB05A1 or the X-NUCLEO-BNRG2A1 This example code is in the public domain. */ #include +#if defined(ARDUINO_STEVAL_MKSBOX1V1) +/* STEVAL-MKSBOX1V1 */ +SPIClass SpiHCI(PC3, PD3, PD1); +HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_1S, PD0, PD4, PA8, 1000000, SPI_MODE1); +BLELocalDevice BLE(&HCISpiTransport); +#elif defined(ARDUINO_DISCO_L475VG_IOT) +/* B-L475E-IOT01A1 */ +SPIClass SpiHCI(PC12, PC11, PC10); +HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, PD13, PE6, PA8, 8000000, SPI_MODE0); +BLELocalDevice BLE(&HCISpiTransport); +#else +/* Shield IDB05A1 with SPI clock on D3 */ +SPIClass SpiHCI(D11, D12, D3); +HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, A1, A0, D7, 8000000, SPI_MODE0); +BLELocalDevice BLE(&HCISpiTransport); +/* Shield IDB05A1 with SPI clock on D13 */ +/*SPIClass SpiHCI(D11, D12, D13); +HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, A1, A0, D7, 8000000, SPI_MODE0); +BLELocalDevice BLE(&HCISpiTransport); */ +/* Shield BNRG2A1 with SPI clock on D3 */ +/*SPIClass SpiHCI(D11, D12, D3); +HCISpiTransportClass HCISpiTransport(SpiHCI, BLUENRG_M2SP, A1, A0, D7, 1000000, SPI_MODE1); +BLELocalDevice BLE(&HCISpiTransport); */ +/* Shield BNRG2A1 with SPI clock on D13 */ +/*SPIClass SpiHCI(D11, D12, D13); +HCISpiTransportClass HCISpiTransport(SpiHCI, BLUENRG_M2SP, A1, A0, D7, 1000000, SPI_MODE1); +BLELocalDevice BLE(&HCISpiTransport); */ +#endif + void setup() { - Serial.begin(9600); + Serial.begin(115200); while (!Serial); // begin initialization @@ -32,7 +60,16 @@ void setup() { BLE.setEventHandler(BLEDiscovered, bleCentralDiscoverHandler); // start scanning for peripherals with duplicates - BLE.scan(true); + int ret = 1; + do + { + ret = BLE.scan(true); + if (ret == 0) + { + BLE.end(); + BLE.begin(); + } + } while(ret == 0); } void loop() { diff --git a/examples/Central/SensorTagButton/SensorTagButton.ino b/examples/Central/SensorTagButton/SensorTagButton.ino index 72dad90b..dd8d8ade 100644 --- a/examples/Central/SensorTagButton/SensorTagButton.ino +++ b/examples/Central/SensorTagButton/SensorTagButton.ino @@ -8,8 +8,7 @@ outputted to the Serial Monitor when one is pressed. The circuit: - - Arduino MKR WiFi 1010, Arduino Uno WiFi Rev2 board, Arduino Nano 33 IoT, - Arduino Nano 33 BLE, or Arduino Nano 33 BLE Sense board. + - STEVAL-MKSBOX1V1, B-L475E-IOT01A1, or a Nucleo board plus the X-NUCLEO-IDB05A1 or the X-NUCLEO-BNRG2A1 - TI SensorTag This example code is in the public domain. @@ -17,8 +16,37 @@ #include +#if defined(ARDUINO_STEVAL_MKSBOX1V1) +/* STEVAL-MKSBOX1V1 */ +SPIClass SpiHCI(PC3, PD3, PD1); +HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_1S, PD0, PD4, PA8, 1000000, SPI_MODE1); +BLELocalDevice BLE(&HCISpiTransport); +#elif defined(ARDUINO_DISCO_L475VG_IOT) +/* B-L475E-IOT01A1 */ +SPIClass SpiHCI(PC12, PC11, PC10); +HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, PD13, PE6, PA8, 8000000, SPI_MODE0); +BLELocalDevice BLE(&HCISpiTransport); +#else +/* Shield IDB05A1 with SPI clock on D3 */ +SPIClass SpiHCI(D11, D12, D3); +HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, A1, A0, D7, 8000000, SPI_MODE0); +BLELocalDevice BLE(&HCISpiTransport); +/* Shield IDB05A1 with SPI clock on D13 */ +/*SPIClass SpiHCI(D11, D12, D13); +HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, A1, A0, D7, 8000000, SPI_MODE0); +BLELocalDevice BLE(&HCISpiTransport); */ +/* Shield BNRG2A1 with SPI clock on D3 */ +/*SPIClass SpiHCI(D11, D12, D3); +HCISpiTransportClass HCISpiTransport(SpiHCI, BLUENRG_M2SP, A1, A0, D7, 1000000, SPI_MODE1); +BLELocalDevice BLE(&HCISpiTransport); */ +/* Shield BNRG2A1 with SPI clock on D13 */ +/*SPIClass SpiHCI(D11, D12, D13); +HCISpiTransportClass HCISpiTransport(SpiHCI, BLUENRG_M2SP, A1, A0, D7, 1000000, SPI_MODE1); +BLELocalDevice BLE(&HCISpiTransport); */ +#endif + void setup() { - Serial.begin(9600); + Serial.begin(115200); while (!Serial); // begin initialization @@ -31,8 +59,17 @@ void setup() { Serial.println("BLE Central - SensorTag button"); Serial.println("Make sure to turn on the device."); - // start scanning for peripheral - BLE.scan(); + // start scanning for peripherals + int ret = 1; + do + { + ret = BLE.scan(); + if (ret == 0) + { + BLE.end(); + BLE.begin(); + } + } while(ret == 0); } void loop() { @@ -53,12 +90,30 @@ void loop() { // "CC2650 SensorTag" if (peripheral.localName() == "CC2650 SensorTag") { // stop scanning - BLE.stopScan(); + int ret = 1; + do + { + ret = BLE.stopScan(); + if (ret == 0) + { + BLE.end(); + BLE.begin(); + } + } while(ret == 0); monitorSensorTagButtons(peripheral); // peripheral disconnected, start scanning again - BLE.scan(); + ret = 1; + do + { + ret = BLE.scan(); + if (ret == 0) + { + BLE.end(); + BLE.begin(); + } + } while(ret == 0); } } } diff --git a/examples/Peripheral/BatteryMonitor/BatteryMonitor.ino b/examples/Peripheral/BatteryMonitor/BatteryMonitor.ino deleted file mode 100644 index 6c5d3d30..00000000 --- a/examples/Peripheral/BatteryMonitor/BatteryMonitor.ino +++ /dev/null @@ -1,106 +0,0 @@ -/* - Battery Monitor - - This example creates a BLE peripheral with the standard battery service and - level characteristic. The A0 pin is used to calculate the battery level. - - The circuit: - - Arduino MKR WiFi 1010, Arduino Uno WiFi Rev2 board, Arduino Nano 33 IoT, - Arduino Nano 33 BLE, or Arduino Nano 33 BLE Sense board. - - You can use a generic BLE central app, like LightBlue (iOS and Android) or - nRF Connect (Android), to interact with the services and characteristics - created in this sketch. - - This example code is in the public domain. -*/ - -#include - - // BLE Battery Service -BLEService batteryService("180F"); - -// BLE Battery Level Characteristic -BLEUnsignedCharCharacteristic batteryLevelChar("2A19", // standard 16-bit characteristic UUID - BLERead | BLENotify); // remote clients will be able to get notifications if this characteristic changes - -int oldBatteryLevel = 0; // last battery level reading from analog input -long previousMillis = 0; // last time the battery level was checked, in ms - -void setup() { - Serial.begin(9600); // initialize serial communication - while (!Serial); - - pinMode(LED_BUILTIN, OUTPUT); // initialize the built-in LED pin to indicate when a central is connected - - // begin initialization - if (!BLE.begin()) { - Serial.println("starting BLE failed!"); - - while (1); - } - - /* Set a local name for the BLE device - This name will appear in advertising packets - and can be used by remote devices to identify this BLE device - The name can be changed but maybe be truncated based on space left in advertisement packet - */ - BLE.setLocalName("BatteryMonitor"); - BLE.setAdvertisedService(batteryService); // add the service UUID - batteryService.addCharacteristic(batteryLevelChar); // add the battery level characteristic - BLE.addService(batteryService); // Add the battery service - batteryLevelChar.writeValue(oldBatteryLevel); // set initial value for this characteristic - - /* Start advertising BLE. It will start continuously transmitting BLE - advertising packets and will be visible to remote BLE central devices - until it receives a new connection */ - - // start advertising - BLE.advertise(); - - Serial.println("Bluetooth device active, waiting for connections..."); -} - -void loop() { - // wait for a BLE central - BLEDevice central = BLE.central(); - - // if a central is connected to the peripheral: - if (central) { - Serial.print("Connected to central: "); - // print the central's BT address: - Serial.println(central.address()); - // turn on the LED to indicate the connection: - digitalWrite(LED_BUILTIN, HIGH); - - // check the battery level every 200ms - // while the central is connected: - while (central.connected()) { - long currentMillis = millis(); - // if 200ms have passed, check the battery level: - if (currentMillis - previousMillis >= 200) { - previousMillis = currentMillis; - updateBatteryLevel(); - } - } - // when the central disconnects, turn off the LED: - digitalWrite(LED_BUILTIN, LOW); - Serial.print("Disconnected from central: "); - Serial.println(central.address()); - } -} - -void updateBatteryLevel() { - /* Read the current voltage level on the A0 analog input pin. - This is used here to simulate the charge level of a battery. - */ - int battery = analogRead(A0); - int batteryLevel = map(battery, 0, 1023, 0, 100); - - if (batteryLevel != oldBatteryLevel) { // if the battery level has changed - Serial.print("Battery Level % is now: "); // print it - Serial.println(batteryLevel); - batteryLevelChar.writeValue(batteryLevel); // and update the battery level characteristic - oldBatteryLevel = batteryLevel; // save the level for next comparison - } -} diff --git a/examples/Peripheral/ButtonLED/ButtonLED.ino b/examples/Peripheral/ButtonLED/ButtonLED.ino index bd373731..9b0dfb0b 100644 --- a/examples/Peripheral/ButtonLED/ButtonLED.ino +++ b/examples/Peripheral/ButtonLED/ButtonLED.ino @@ -6,9 +6,7 @@ represents the state of the button. The circuit: - - Arduino MKR WiFi 1010, Arduino Uno WiFi Rev2 board, Arduino Nano 33 IoT, - Arduino Nano 33 BLE, or Arduino Nano 33 BLE Sense board. - - Button connected to pin 4 + - STEVAL-MKSBOX1V1, B-L475E-IOT01A1, or a Nucleo board plus the X-NUCLEO-IDB05A1 or the X-NUCLEO-BNRG2A1 You can use a generic BLE central app, like LightBlue (iOS and Android) or nRF Connect (Android), to interact with the services and characteristics @@ -19,8 +17,42 @@ #include +#if defined(ARDUINO_STEVAL_MKSBOX1V1) +/* STEVAL-MKSBOX1V1 */ +SPIClass SpiHCI(PC3, PD3, PD1); +HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_1S, PD0, PD4, PA8, 1000000, SPI_MODE1); +BLELocalDevice BLE(&HCISpiTransport); +const int buttonPin = PG1; // set buttonPin to digital pin PG1 +#elif defined(ARDUINO_DISCO_L475VG_IOT) +/* B-L475E-IOT01A1 */ +SPIClass SpiHCI(PC12, PC11, PC10); +HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, PD13, PE6, PA8, 8000000, SPI_MODE0); +BLELocalDevice BLE(&HCISpiTransport); +const int buttonPin = PC13; // set buttonPin to digital pin PC13 +#else +/* Shield IDB05A1 with SPI clock on D3 */ +SPIClass SpiHCI(D11, D12, D3); +HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, A1, A0, D7, 8000000, SPI_MODE0); +BLELocalDevice BLE(&HCISpiTransport); +const int buttonPin = PC13; // set buttonPin to digital pin PC13 +/* Shield IDB05A1 with SPI clock on D13 */ +/*SPIClass SpiHCI(D11, D12, D13); +HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, A1, A0, D7, 8000000, SPI_MODE0); +BLELocalDevice BLE(&HCISpiTransport); +const int buttonPin = PC13; // set buttonPin to digital pin PC13 */ +/* Shield BNRG2A1 with SPI clock on D3 */ +/*SPIClass SpiHCI(D11, D12, D3); +HCISpiTransportClass HCISpiTransport(SpiHCI, BLUENRG_M2SP, A1, A0, D7, 1000000, SPI_MODE1); +BLELocalDevice BLE(&HCISpiTransport); +const int buttonPin = PC13; // set buttonPin to digital pin PC13 */ +/* Shield BNRG2A1 with SPI clock on D13 */ +/*SPIClass SpiHCI(D11, D12, D13); +HCISpiTransportClass HCISpiTransport(SpiHCI, BLUENRG_M2SP, A1, A0, D7, 1000000, SPI_MODE1); +BLELocalDevice BLE(&HCISpiTransport); +const int buttonPin = PC13; // set buttonPin to digital pin PC13 */ +#endif + const int ledPin = LED_BUILTIN; // set ledPin to on-board LED -const int buttonPin = 4; // set buttonPin to digital pin 4 BLEService ledService("19B10010-E8F2-537E-4F6C-D104768A1214"); // create service @@ -30,7 +62,7 @@ BLEByteCharacteristic ledCharacteristic("19B10011-E8F2-537E-4F6C-D104768A1214", BLEByteCharacteristic buttonCharacteristic("19B10012-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify); void setup() { - Serial.begin(9600); + Serial.begin(115200); while (!Serial); pinMode(ledPin, OUTPUT); // use the LED as an output diff --git a/examples/Peripheral/CallbackLED/CallbackLED.ino b/examples/Peripheral/CallbackLED/CallbackLED.ino index a874a77b..c794856e 100644 --- a/examples/Peripheral/CallbackLED/CallbackLED.ino +++ b/examples/Peripheral/CallbackLED/CallbackLED.ino @@ -6,8 +6,7 @@ library are used. The circuit: - - Arduino MKR WiFi 1010, Arduino Uno WiFi Rev2 board, Arduino Nano 33 IoT, - Arduino Nano 33 BLE, or Arduino Nano 33 BLE Sense board. + - STEVAL-MKSBOX1V1, B-L475E-IOT01A1, or a Nucleo board plus the X-NUCLEO-IDB05A1 or the X-NUCLEO-BNRG2A1 You can use a generic BLE central app, like LightBlue (iOS and Android) or nRF Connect (Android), to interact with the services and characteristics @@ -18,6 +17,35 @@ #include +#if defined(ARDUINO_STEVAL_MKSBOX1V1) +/* STEVAL-MKSBOX1V1 */ +SPIClass SpiHCI(PC3, PD3, PD1); +HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_1S, PD0, PD4, PA8, 1000000, SPI_MODE1); +BLELocalDevice BLE(&HCISpiTransport); +#elif defined(ARDUINO_DISCO_L475VG_IOT) +/* B-L475E-IOT01A1 */ +SPIClass SpiHCI(PC12, PC11, PC10); +HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, PD13, PE6, PA8, 8000000, SPI_MODE0); +BLELocalDevice BLE(&HCISpiTransport); +#else +/* Shield IDB05A1 with SPI clock on D3 */ +SPIClass SpiHCI(D11, D12, D3); +HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, A1, A0, D7, 8000000, SPI_MODE0); +BLELocalDevice BLE(&HCISpiTransport); +/* Shield IDB05A1 with SPI clock on D13 */ +/*SPIClass SpiHCI(D11, D12, D13); +HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, A1, A0, D7, 8000000, SPI_MODE0); +BLELocalDevice BLE(&HCISpiTransport); */ +/* Shield BNRG2A1 with SPI clock on D3 */ +/*SPIClass SpiHCI(D11, D12, D3); +HCISpiTransportClass HCISpiTransport(SpiHCI, BLUENRG_M2SP, A1, A0, D7, 1000000, SPI_MODE1); +BLELocalDevice BLE(&HCISpiTransport); */ +/* Shield BNRG2A1 with SPI clock on D13 */ +/*SPIClass SpiHCI(D11, D12, D13); +HCISpiTransportClass HCISpiTransport(SpiHCI, BLUENRG_M2SP, A1, A0, D7, 1000000, SPI_MODE1); +BLELocalDevice BLE(&HCISpiTransport); */ +#endif + BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // create service // create switch characteristic and allow remote device to read and write @@ -26,7 +54,7 @@ BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214 const int ledPin = LED_BUILTIN; // pin to use for the LED void setup() { - Serial.begin(9600); + Serial.begin(115200); while (!Serial); pinMode(ledPin, OUTPUT); // use the LED pin as an output diff --git a/examples/Peripheral/LED/LED.ino b/examples/Peripheral/LED/LED.ino index 1ede6535..a6da2dca 100644 --- a/examples/Peripheral/LED/LED.ino +++ b/examples/Peripheral/LED/LED.ino @@ -5,8 +5,7 @@ characteristic to control an LED. The circuit: - - Arduino MKR WiFi 1010, Arduino Uno WiFi Rev2 board, Arduino Nano 33 IoT, - Arduino Nano 33 BLE, or Arduino Nano 33 BLE Sense board. + - STEVAL-MKSBOX1V1, B-L475E-IOT01A1, or a Nucleo board plus the X-NUCLEO-IDB05A1 or the X-NUCLEO-BNRG2A1 You can use a generic BLE central app, like LightBlue (iOS and Android) or nRF Connect (Android), to interact with the services and characteristics @@ -14,9 +13,37 @@ This example code is in the public domain. */ - #include +#if defined(ARDUINO_STEVAL_MKSBOX1V1) +/* STEVAL-MKSBOX1V1 */ +SPIClass SpiHCI(PC3, PD3, PD1); +HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_1S, PD0, PD4, PA8, 1000000, SPI_MODE1); +BLELocalDevice BLE(&HCISpiTransport); +#elif defined(ARDUINO_DISCO_L475VG_IOT) +/* B-L475E-IOT01A1 */ +SPIClass SpiHCI(PC12, PC11, PC10); +HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, PD13, PE6, PA8, 8000000, SPI_MODE0); +BLELocalDevice BLE(&HCISpiTransport); +#else +/* Shield IDB05A1 with SPI clock on D3 */ +SPIClass SpiHCI(D11, D12, D3); +HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, A1, A0, D7, 8000000, SPI_MODE0); +BLELocalDevice BLE(&HCISpiTransport); +/* Shield IDB05A1 with SPI clock on D13 */ +/*SPIClass SpiHCI(D11, D12, D13); +HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, A1, A0, D7, 8000000, SPI_MODE0); +BLELocalDevice BLE(&HCISpiTransport); */ +/* Shield BNRG2A1 with SPI clock on D3 */ +/*SPIClass SpiHCI(D11, D12, D3); +HCISpiTransportClass HCISpiTransport(SpiHCI, BLUENRG_M2SP, A1, A0, D7, 1000000, SPI_MODE1); +BLELocalDevice BLE(&HCISpiTransport); */ +/* Shield BNRG2A1 with SPI clock on D13 */ +/*SPIClass SpiHCI(D11, D12, D13); +HCISpiTransportClass HCISpiTransport(SpiHCI, BLUENRG_M2SP, A1, A0, D7, 1000000, SPI_MODE1); +BLELocalDevice BLE(&HCISpiTransport); */ +#endif + BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service // BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central @@ -25,7 +52,7 @@ BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214 const int ledPin = LED_BUILTIN; // pin to use for the LED void setup() { - Serial.begin(9600); + Serial.begin(115200); while (!Serial); // set LED pin to output mode diff --git a/keywords.txt b/keywords.txt index 464cac72..12f67785 100644 --- a/keywords.txt +++ b/keywords.txt @@ -80,6 +80,7 @@ setConnectable KEYWORD2 setTimeout KEYWORD2 debug KEYWORD2 noDebug KEYWORD2 +setTransport KEYWORD2 properties KEYWORD2 valueSize KEYWORD2 diff --git a/src/ArduinoBLE.h b/src/ArduinoBLE.h index dc6e8188..41c11111 100644 --- a/src/ArduinoBLE.h +++ b/src/ArduinoBLE.h @@ -24,5 +24,6 @@ #include "BLEProperty.h" #include "BLEStringCharacteristic.h" #include "BLETypedCharacteristics.h" +#include "utility/HCISpiTransport.h" #endif diff --git a/src/local/BLELocalDevice.cpp b/src/local/BLELocalDevice.cpp index d2c8a1b3..51e48b7f 100644 --- a/src/local/BLELocalDevice.cpp +++ b/src/local/BLELocalDevice.cpp @@ -18,14 +18,13 @@ */ #include "utility/ATT.h" -#include "utility/HCI.h" #include "utility/GAP.h" #include "utility/GATT.h" #include "utility/L2CAPSignaling.h" - #include "BLELocalDevice.h" -BLELocalDevice::BLELocalDevice() +BLELocalDevice::BLELocalDevice(HCITransportInterface *HCITransport) : + _HCITransport(HCITransport) { } @@ -35,39 +34,7 @@ BLELocalDevice::~BLELocalDevice() int BLELocalDevice::begin() { -#if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_AVR_UNO_WIFI_REV2) || defined(ARDUINO_SAMD_NANO_33_IOT) - // reset the NINA in BLE mode - pinMode(SPIWIFI_SS, OUTPUT); - pinMode(NINA_RESETN, OUTPUT); - - digitalWrite(SPIWIFI_SS, LOW); -#endif - -#if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_AVR_UNO_WIFI_REV2) - digitalWrite(NINA_RESETN, HIGH); - delay(100); - digitalWrite(NINA_RESETN, LOW); - delay(750); -#elif defined(ARDUINO_SAMD_NANO_33_IOT) - // inverted reset - digitalWrite(NINA_RESETN, LOW); - delay(100); - digitalWrite(NINA_RESETN, HIGH); - delay(750); -#endif - - -#ifdef ARDUINO_AVR_UNO_WIFI_REV2 - // set SS HIGH - digitalWrite(SPIWIFI_SS, HIGH); - - // set RTS HIGH - pinMode(NINA_RTS, OUTPUT); - digitalWrite(NINA_RTS, HIGH); - - // set CTS as input - pinMode(NINA_CTS, INPUT); -#endif + HCI.setTransport(_HCITransport); if (!HCI.begin()) { end(); @@ -116,14 +83,6 @@ void BLELocalDevice::end() GATT.end(); HCI.end(); - -#if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_AVR_UNO_WIFI_REV2) - // disable the NINA - digitalWrite(NINA_RESETN, HIGH); -#elif defined(ARDUINO_SAMD_NANO_33_IOT) - // disable the NINA - digitalWrite(NINA_RESETN, LOW); -#endif } void BLELocalDevice::poll() @@ -240,9 +199,9 @@ int BLELocalDevice::scanForAddress(String address, bool withDuplicates) return GAP.scanForAddress(address, withDuplicates); } -void BLELocalDevice::stopScan() +int BLELocalDevice::stopScan() { - GAP.stopScan(); + return GAP.stopScan(); } BLEDevice BLELocalDevice::central() @@ -297,5 +256,3 @@ void BLELocalDevice::noDebug() { HCI.noDebug(); } - -BLELocalDevice BLE; diff --git a/src/local/BLELocalDevice.h b/src/local/BLELocalDevice.h index 20837c14..27f001fb 100644 --- a/src/local/BLELocalDevice.h +++ b/src/local/BLELocalDevice.h @@ -20,12 +20,13 @@ #ifndef _BLE_LOCAL_DEVICE_H_ #define _BLE_LOCAL_DEVICE_H_ +#include "utility/HCI.h" #include "BLEDevice.h" #include "BLEService.h" class BLELocalDevice { public: - BLELocalDevice(); + BLELocalDevice(HCITransportInterface *HCITransport); virtual ~BLELocalDevice(); int begin(); @@ -39,7 +40,7 @@ class BLELocalDevice { String address() const; - int rssi(); + int rssi(); void setAdvertisedServiceUuid(const char* advertisedServiceUuid); void setAdvertisedService(const BLEService& service); @@ -59,7 +60,7 @@ class BLELocalDevice { int scanForName(String name, bool withDuplicates = false); int scanForUuid(String uuid, bool withDuplicates = false); int scanForAddress(String address, bool withDuplicates = false); - void stopScan(); + int stopScan(); BLEDevice central(); BLEDevice available(); @@ -76,8 +77,7 @@ class BLELocalDevice { void noDebug(); private: + HCITransportInterface *_HCITransport; }; -extern BLELocalDevice BLE; - #endif diff --git a/src/utility/GAP.cpp b/src/utility/GAP.cpp index ae2e90cd..2948a38a 100644 --- a/src/utility/GAP.cpp +++ b/src/utility/GAP.cpp @@ -153,7 +153,7 @@ int GAPClass::advertise() return 0; } - _advertising = false; + _advertising = true; return 1; } @@ -167,7 +167,12 @@ void GAPClass::stopAdvertise() int GAPClass::scan(bool withDuplicates) { - HCI.leSetScanEnable(false, true); + if(_scanning) { + // Check if the HCI command fails + if (HCI.leSetScanEnable(false, true) != 0) { + return 0; + } + } // active scan, 10 ms scan interval (N * 0.625), 10 ms scan window (N * 0.625), public own address type, no filter if (HCI.leSetScanParameters(0x01, 0x0010, 0x0010, 0x00, 0x00) != 0) { @@ -210,9 +215,14 @@ int GAPClass::scanForAddress(String address, bool withDuplicates) return scan(withDuplicates); } -void GAPClass::stopScan() +int GAPClass::stopScan() { - HCI.leSetScanEnable(false, false); + if(_scanning) { + // Check if the HCI command fails + if (HCI.leSetScanEnable(false, false) != 0) { + return 0; + } + } _scanning = false; @@ -223,6 +233,8 @@ void GAPClass::stopScan() } _discoveredDevices.clear(); + + return 1; } BLEDevice GAPClass::available() diff --git a/src/utility/GAP.h b/src/utility/GAP.h index c2a99daa..b1b1cf48 100644 --- a/src/utility/GAP.h +++ b/src/utility/GAP.h @@ -42,7 +42,7 @@ class GAPClass { int scanForName(String name, bool withDuplicates); int scanForUuid(String uuid, bool withDuplicates); int scanForAddress(String address, bool withDuplicates); - void stopScan(); + int stopScan(); BLEDevice available(); void setAdvertisingInterval(uint16_t advertisingInterval); diff --git a/src/utility/HCI.cpp b/src/utility/HCI.cpp index 0234803f..f1bc1552 100644 --- a/src/utility/HCI.cpp +++ b/src/utility/HCI.cpp @@ -19,7 +19,6 @@ #include "ATT.h" #include "GAP.h" -#include "HCITransport.h" #include "L2CAPSignaling.h" #include "HCI.h" @@ -75,8 +74,10 @@ HCIClass::HCIClass() : _debug(NULL), _recvIndex(0), - _pendingPkt(0) + _pendingPkt(0), + _HCITransport(0) { + } HCIClass::~HCIClass() @@ -87,12 +88,12 @@ int HCIClass::begin() { _recvIndex = 0; - return HCITransport.begin(); + return _HCITransport->begin(); } void HCIClass::end() { - HCITransport.end(); + _HCITransport->end(); } void HCIClass::poll() @@ -102,16 +103,12 @@ void HCIClass::poll() void HCIClass::poll(unsigned long timeout) { -#ifdef ARDUINO_AVR_UNO_WIFI_REV2 - digitalWrite(NINA_RTS, LOW); -#endif - if (timeout) { - HCITransport.wait(timeout); + _HCITransport->wait(timeout); } - while (HCITransport.available()) { - byte b = HCITransport.read(); + while (_HCITransport->available()) { + byte b = _HCITransport->read(); _recvBuffer[_recvIndex++] = b; @@ -120,35 +117,23 @@ void HCIClass::poll(unsigned long timeout) if (_debug) { dumpPkt("HCI ACLDATA RX <- ", _recvIndex, _recvBuffer); } -#ifdef ARDUINO_AVR_UNO_WIFI_REV2 - digitalWrite(NINA_RTS, HIGH); -#endif + int pktLen = _recvIndex - 1; _recvIndex = 0; handleAclDataPkt(pktLen, &_recvBuffer[1]); - -#ifdef ARDUINO_AVR_UNO_WIFI_REV2 - digitalWrite(NINA_RTS, LOW); -#endif } } else if (_recvBuffer[0] == HCI_EVENT_PKT) { if (_recvIndex > 3 && _recvIndex >= (3 + _recvBuffer[2])) { if (_debug) { dumpPkt("HCI EVENT RX <- ", _recvIndex, _recvBuffer); } -#ifdef ARDUINO_AVR_UNO_WIFI_REV2 - digitalWrite(NINA_RTS, HIGH); -#endif + // received full event int pktLen = _recvIndex - 1; _recvIndex = 0; handleEventPkt(pktLen, &_recvBuffer[1]); - -#ifdef ARDUINO_AVR_UNO_WIFI_REV2 - digitalWrite(NINA_RTS, LOW); -#endif } } else { _recvIndex = 0; @@ -158,10 +143,6 @@ void HCIClass::poll(unsigned long timeout) } } } - -#ifdef ARDUINO_AVR_UNO_WIFI_REV2 - digitalWrite(NINA_RTS, HIGH); -#endif } int HCIClass::reset() @@ -240,9 +221,7 @@ int HCIClass::readLeBufferSize(uint16_t& pktLen, uint8_t& maxPkt) pktLen = leBufferSize->pktLen; _maxPkt = maxPkt = leBufferSize->maxPkt; -#ifndef __AVR__ ATT.setMaxMtu(pktLen - 9); // max pkt len - ACL header size -#endif } return result; @@ -436,7 +415,7 @@ int HCIClass::sendAclPkt(uint16_t handle, uint8_t cid, uint8_t plen, void* data) } _pendingPkt++; - HCITransport.write(txBuffer, sizeof(aclHdr) + plen); + _HCITransport->write(txBuffer, sizeof(aclHdr) + plen); return 0; } @@ -477,7 +456,7 @@ int HCIClass::sendCommand(uint16_t opcode, uint8_t plen, void* parameters) dumpPkt("HCI COMMAND TX -> ", sizeof(pktHdr) + plen, txBuffer); } - HCITransport.write(txBuffer, sizeof(pktHdr) + plen); + _HCITransport->write(txBuffer, sizeof(pktHdr) + plen); _cmdCompleteOpcode = 0xffff; _cmdCompleteStatus = -1; @@ -571,7 +550,9 @@ void HCIClass::handleEventPkt(uint8_t /*plen*/, uint8_t pdata[]) ATT.removeConnection(disconnComplete->handle, disconnComplete->reason); L2CAPSignaling.removeConnection(disconnComplete->handle, disconnComplete->reason); - HCI.leSetAdvertiseEnable(0x01); + if(GAP.advertising()) { + HCI.leSetAdvertiseEnable(0x01); + } } else if (eventHdr->evt == EVT_CMD_COMPLETE) { struct __attribute__ ((packed)) CmdComplete { uint8_t ncmd; @@ -686,4 +667,9 @@ void HCIClass::dumpPkt(const char* prefix, uint8_t plen, uint8_t pdata[]) } } +void HCIClass::setTransport(HCITransportInterface *HCITransport) +{ + _HCITransport = HCITransport; +} + HCIClass HCI; diff --git a/src/utility/HCI.h b/src/utility/HCI.h index 86ec679c..01923e3a 100644 --- a/src/utility/HCI.h +++ b/src/utility/HCI.h @@ -21,6 +21,7 @@ #define _HCI_H_ #include +#include "HCITransport.h" class HCIClass { public: @@ -71,6 +72,8 @@ class HCIClass { void debug(Stream& stream); void noDebug(); + void setTransport(HCITransportInterface *HCITransport); + private: int sendCommand(uint16_t opcode, uint8_t plen = 0, void* parameters = NULL); @@ -94,6 +97,8 @@ class HCIClass { uint8_t _pendingPkt; uint8_t _aclPktBuffer[255]; + + HCITransportInterface *_HCITransport; }; extern HCIClass HCI; diff --git a/src/utility/HCICordioTransport.cpp b/src/utility/HCICordioTransport.cpp deleted file mode 100644 index f2a83d97..00000000 --- a/src/utility/HCICordioTransport.cpp +++ /dev/null @@ -1,271 +0,0 @@ -/* - This file is part of the ArduinoBLE library. - Copyright (c) 2019 Arduino SA. All rights reserved. - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -*/ - -#ifdef ARDUINO_ARCH_MBED - -#include -#include - -#include - -// Parts of this file are based on: https://github.com/ARMmbed/mbed-os-cordio-hci-passthrough/pull/2 -// With permission from the Arm Mbed team to re-license - -#if CORDIO_ZERO_COPY_HCI -#include -#include -#include -#include -#include -#include - -/* avoid many small allocs (and WSF doesn't have smaller buffers) */ -#define MIN_WSF_ALLOC (16) -#endif //CORDIO_ZERO_COPY_HCI - -#include "HCICordioTransport.h" - -extern ble::vendor::cordio::CordioHCIDriver& ble_cordio_get_hci_driver(); - -namespace ble { - namespace vendor { - namespace cordio { - struct CordioHCIHook { - static CordioHCIDriver& getDriver() { - return ble_cordio_get_hci_driver(); - } - - static CordioHCITransportDriver& getTransportDriver() { - return getDriver()._transport_driver; - } - - static void setDataReceivedHandler(void (*handler)(uint8_t*, uint8_t)) { - getTransportDriver().set_data_received_handler(handler); - } - }; - } - } -} - -using ble::vendor::cordio::CordioHCIHook; - -#if CORDIO_ZERO_COPY_HCI -extern uint8_t *SystemHeapStart; -extern uint32_t SystemHeapSize; - -void init_wsf(ble::vendor::cordio::buf_pool_desc_t& buf_pool_desc) { - static bool init = false; - - if (init) { - return; - } - init = true; - - // use the buffer for the WSF heap - SystemHeapStart = buf_pool_desc.buffer_memory; - SystemHeapSize = buf_pool_desc.buffer_size; - - // Initialize buffers with the ones provided by the HCI driver - uint16_t bytes_used = WsfBufInit( - buf_pool_desc.pool_count, - (wsfBufPoolDesc_t*)buf_pool_desc.pool_description - ); - - // Raise assert if not enough memory was allocated - MBED_ASSERT(bytes_used != 0); - - SystemHeapStart += bytes_used; - SystemHeapSize -= bytes_used; - - WsfTimerInit(); -} - - -extern "C" void wsf_mbed_ble_signal_event(void) -{ - // do nothing -} -#endif //CORDIO_ZERO_COPY_HCI - -static void bleLoop() -{ -#if CORDIO_ZERO_COPY_HCI - uint64_t last_update_us = 0; - mbed::LowPowerTimer timer; - - timer.start(); - - while (true) { - last_update_us += (uint64_t) timer.read_high_resolution_us(); - timer.reset(); - - uint64_t last_update_ms = (last_update_us / 1000); - wsfTimerTicks_t wsf_ticks = (last_update_ms / WSF_MS_PER_TICK); - - if (wsf_ticks > 0) { - WsfTimerUpdate(wsf_ticks); - last_update_us -= (last_update_ms * 1000); - } - - wsfOsDispatcher(); - - bool sleep = false; - { - /* call needs interrupts disabled */ - mbed::CriticalSectionLock critical_section; - if (wsfOsReadyToSleep()) { - sleep = true; - } - } - - uint64_t time_spent = (uint64_t) timer.read_high_resolution_us(); - - /* don't bother sleeping if we're already past tick */ - if (sleep && (WSF_MS_PER_TICK * 1000 > time_spent)) { - /* sleep to maintain constant tick rate */ - uint64_t wait_time_us = WSF_MS_PER_TICK * 1000 - time_spent; - uint64_t wait_time_ms = wait_time_us / 1000; - - wait_time_us = wait_time_us % 1000; - - if (wait_time_ms) { - rtos::ThisThread::sleep_for(wait_time_ms); - } - - if (wait_time_us) { - wait_us(wait_time_us); - } - } - } -#else - while(true) { - rtos::Thread::wait(osWaitForever); - } -#endif // CORDIO_ZERO_COPY_HCI -} - -static rtos::EventFlags bleEventFlags; -static rtos::Thread bleLoopThread; - - -HCICordioTransportClass::HCICordioTransportClass() : - _begun(false) -{ -} - -HCICordioTransportClass::~HCICordioTransportClass() -{ -} - -int HCICordioTransportClass::begin() -{ - _rxBuf.clear(); - -#if CORDIO_ZERO_COPY_HCI - ble::vendor::cordio::buf_pool_desc_t bufPoolDesc = CordioHCIHook::getDriver().get_buffer_pool_description(); - init_wsf(bufPoolDesc); -#endif - - CordioHCIHook::getDriver().initialize(); - bleLoopThread.start(bleLoop); - - CordioHCIHook::setDataReceivedHandler(HCICordioTransportClass::onDataReceived); - - _begun = true; - - return 1; -} - -void HCICordioTransportClass::end() -{ - bleLoopThread.terminate(); - - CordioHCIHook::getDriver().terminate(); - - _begun = false; -} - -void HCICordioTransportClass::wait(unsigned long timeout) -{ - if (available()) { - return; - } - - // wait for handleRxData to signal - bleEventFlags.wait_all(0x01, timeout, true); -} - -int HCICordioTransportClass::available() -{ - return _rxBuf.available(); -} - -int HCICordioTransportClass::peek() -{ - return _rxBuf.peek(); -} - -int HCICordioTransportClass::read() -{ - return _rxBuf.read_char(); -} - -size_t HCICordioTransportClass::write(const uint8_t* data, size_t length) -{ - if (!_begun) { - return 0; - } - - uint8_t packetLength = length - 1; - uint8_t packetType = data[0]; - -#if CORDIO_ZERO_COPY_HCI - uint8_t* packet = (uint8_t*)WsfMsgAlloc(max(packetLength, MIN_WSF_ALLOC)); - - memcpy(packet, &data[1], packetLength); - - return CordioHCIHook::getTransportDriver().write(packetType, packetLength, packet); -#else - return CordioHCIHook::.getTransportDriver().write(packetType, packetLength, &data[1]); -#endif -} - -void HCICordioTransportClass::handleRxData(uint8_t* data, uint8_t len) -{ - if (_rxBuf.availableForStore() < len) { - // drop! - return; - } - - for (int i = 0; i < len; i++) { - _rxBuf.store_char(data[i]); - } - - bleEventFlags.set(0x01); -} - -HCICordioTransportClass HCICordioTransport; -HCITransportInterface& HCITransport = HCICordioTransport; - -void HCICordioTransportClass::onDataReceived(uint8_t* data, uint8_t len) -{ - HCICordioTransport.handleRxData(data, len); -} - -#endif diff --git a/src/utility/HCICordioTransport.h b/src/utility/HCICordioTransport.h deleted file mode 100644 index b8d0596a..00000000 --- a/src/utility/HCICordioTransport.h +++ /dev/null @@ -1,54 +0,0 @@ -/* - This file is part of the ArduinoBLE library. - Copyright (c) 2019 Arduino SA. All rights reserved. - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -*/ - -#ifndef _HCI_CORDIO_TRANSPORT_H_ -#define _HCI_CORDIO_TRANSPORT_H_ - -#include - -#include "api/RingBuffer.h" - -#include "HCITransport.h" - -class HCICordioTransportClass : public HCITransportInterface { -public: - HCICordioTransportClass(); - virtual ~HCICordioTransportClass(); - - virtual int begin(); - virtual void end(); - - virtual void wait(unsigned long timeout); - - virtual int available(); - virtual int peek(); - virtual int read(); - - virtual size_t write(const uint8_t* data, size_t length); - -private: - static void onDataReceived(uint8_t* data, uint8_t len); - void handleRxData(uint8_t* data, uint8_t len); - -private: - bool _begun; - RingBufferN<256> _rxBuf; -}; - -#endif diff --git a/src/utility/HCISpiTransport.cpp b/src/utility/HCISpiTransport.cpp index ab9eeaea..08e3f12b 100644 --- a/src/utility/HCISpiTransport.cpp +++ b/src/utility/HCISpiTransport.cpp @@ -17,21 +17,13 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#ifdef ARDUINO_ARCH_STM32 - #include "HCISpiTransport.h" -#if defined(ARDUINO_STEVAL_MKSBOX1V1) -SPIClass SpiHCI(PC3, PD3, PD1); /* STEVAL_MKSBOX1V1 */ -#elif defined(ARDUINO_DISCO_L475VG_IOT) -SPIClass SpiHCI(PC12, PC11, PC10); /* B-L475E-IOT01A1 */ -#else -SPIClass SpiHCI(D11, D12, D3); /* Shield IDB05A1 */ -#endif /* ARDUINO_STEVAL_MKSBOX1V1 */ volatile int data_avail = 0; -HCISpiTransportClass::HCISpiTransportClass(SPIClass& spi, uint8_t cs_pin, uint8_t spi_irq, uint8_t ble_rst, unsigned long frequency, int spi_mode) : +HCISpiTransportClass::HCISpiTransportClass(SPIClass& spi, BLEChip_t ble_chip, uint8_t cs_pin, uint8_t spi_irq, uint8_t ble_rst, unsigned long frequency, int spi_mode) : _spi(&spi), + _ble_chip(ble_chip), _cs_pin(cs_pin), _spi_irq(spi_irq), _ble_rst(ble_rst), @@ -75,15 +67,18 @@ int HCISpiTransportClass::begin() digitalWrite(_ble_rst, HIGH); delay(5); -#if defined(SPBTLE_RF) - // Wait for Blue Initialize - wait_for_blue_initialize(); -#endif /* SPBTLE_RF */ + if (_ble_chip == SPBTLE_RF) + { + // Wait for Blue Initialize + wait_for_blue_initialize(); + } else if (_ble_chip == SPBTLE_1S || _ble_chip == BLUENRG_M2SP) + { + // Wait a while for the reset of the BLE module + delay(300); + } else + { -#if defined(SPBTLE_1S) - // Wait a while for the reset of the BLE module - delay(300); -#endif /* SPBTLE_1S */ + } return 1; } @@ -125,6 +120,11 @@ int HCISpiTransportClass::available() { uint8_t header_master[5] = {0x0b, 0x00, 0x00, 0x00, 0x00}; + if(_ble_chip == SPBTLE_1S || _ble_chip == BLUENRG_M2SP) + { + detachInterrupt(_spi_irq); + } + _spi->beginTransaction(SPISettings(_frequency, MSBFIRST, _spi_mode)); digitalWrite(_cs_pin, LOW); @@ -132,11 +132,65 @@ int HCISpiTransportClass::available() /* Write the header */ _spi->transfer(header_master, 5); -#if defined(SPBTLE_RF) - /* device is ready */ - if(header_master[0] == 0x02) + if (_ble_chip == SPBTLE_RF) + { + /* device is ready */ + if(header_master[0] == 0x02) + { + uint16_t byte_count = (header_master[4] << 8) | header_master[3]; + + if(byte_count > 0) + { + if(_initial_phase) + { + /* avoid to read more data that available size of the buffer */ + if(byte_count > (BLE_MODULE_SPI_BUFFER_SIZE - _write_index_initial)) + { + byte_count = (BLE_MODULE_SPI_BUFFER_SIZE - _write_index_initial); + } + + /* Read the response */ + for(int j=0; j < byte_count; j++) + { + _rxbuff[_write_index_initial] = _spi->transfer(0xFF); + _write_index_initial++; + } + + /* Check if the message is a Blue Initialize */ + /* If so we need to send the command to enable LL_ONLY */ + if(byte_count == 6) + { + if(_rxbuff[_write_index_initial - 6] == 0x04 && + _rxbuff[_write_index_initial - 5] == 0xFF && + _rxbuff[_write_index_initial - 4] == 0x03 && + _rxbuff[_write_index_initial - 3] == 0x01 && + _rxbuff[_write_index_initial - 2] == 0x00 && + _rxbuff[_write_index_initial - 1] == 0x01) + { + ble_reset = 1; + } + } + } else + { + /* avoid to read more data that available size of the buffer */ + if(byte_count > (BLE_MODULE_SPI_BUFFER_SIZE - _write_index)) + { + byte_count = (BLE_MODULE_SPI_BUFFER_SIZE - _write_index); + /* SPI buffer is full but we still have data to store, so we set the data_avail flag to true */ + data_avail = 1; + } + + /* Read the response */ + for(int j=0; j < byte_count; j++) + { + _rxbuff[_write_index] = _spi->transfer(0xFF); + _write_index++; + } + } + } + } + } else if (_ble_chip == SPBTLE_1S || _ble_chip == BLUENRG_M2SP) { -#endif /* SPBTLE_RF */ uint16_t byte_count = (header_master[4] << 8) | header_master[3]; if(byte_count > 0) @@ -156,23 +210,6 @@ int HCISpiTransportClass::available() _write_index_initial++; } -#if defined(SPBTLE_RF) - /* Check if the message is a Blue Initialize */ - /* If so we need to send the command to enable LL_ONLY */ - if(byte_count == 6) - { - if(_rxbuff[_write_index_initial - 6] == 0x04 && - _rxbuff[_write_index_initial - 5] == 0xFF && - _rxbuff[_write_index_initial - 4] == 0x03 && - _rxbuff[_write_index_initial - 3] == 0x01 && - _rxbuff[_write_index_initial - 2] == 0x00 && - _rxbuff[_write_index_initial - 1] == 0x01) - { - ble_reset = 1; - } - } -#endif /* SPBTLE_RF */ -#if defined(SPBTLE_1S) /* Check if the message is a CMD_COMPLETE */ /* We suppose that the first CMD is always a HCI_RESET */ if(byte_count == 7) @@ -188,7 +225,6 @@ int HCISpiTransportClass::available() ble_reset = 1; } } -#endif /* SPBTLE_1S */ } else { /* avoid to read more data that available size of the buffer */ @@ -207,26 +243,36 @@ int HCISpiTransportClass::available() } } } -#if defined(SPBTLE_RF) + } else + { + } -#endif /* SPBTLE_RF */ digitalWrite(_cs_pin, HIGH); _spi->endTransaction(); + + if(_ble_chip == SPBTLE_1S || _ble_chip == BLUENRG_M2SP) + { + attachInterrupt(_spi_irq, SPI_Irq_Callback, RISING); + } } if(ble_reset) { -#if defined(SPBTLE_RF) - /* BLE chip was reset: we need to enable LL_ONLY */ - enable_ll_only(); - wait_for_enable_ll_only(); -#endif /* SPBTLE_RF */ -#if defined(SPBTLE_1S) - /* BLE chip was reset: we need to wait for a while */ - delay(300); -#endif /* SPBTLE_1S */ + if (_ble_chip == SPBTLE_RF) + { + /* BLE chip was reset: we need to enable LL_ONLY */ + enable_ll_only(); + wait_for_enable_ll_only(); + } else if (_ble_chip == SPBTLE_1S || _ble_chip == BLUENRG_M2SP) + { + /* BLE chip was reset: we need to wait for a while */ + delay(300); + } else + { + + } /* Now we can update the write index and close the initial phase */ _write_index = _write_index_initial; @@ -287,49 +333,78 @@ size_t HCISpiTransportClass::write(const uint8_t* data, size_t length) do { -#if defined(SPBTLE_1S) - uint32_t tickstart_data_available = millis(); -#endif /* SPBTLE_1S */ - result = 0; + if (_ble_chip == SPBTLE_RF) + { + result = 0; - _spi->beginTransaction(SPISettings(_frequency, MSBFIRST, _spi_mode)); + _spi->beginTransaction(SPISettings(_frequency, MSBFIRST, _spi_mode)); - digitalWrite(_cs_pin, LOW); + digitalWrite(_cs_pin, LOW); -#if defined(SPBTLE_1S) - /* - * Wait until BlueNRG-1 is ready. - * When ready it will raise the IRQ pin. - */ - while(!(digitalRead(_spi_irq) == 1)) - { - if((millis() - tickstart_data_available) > 1000) + /* Write the header */ + _spi->transfer(header_master, 5); + + /* device is ready */ + if(header_master[0] == 0x02) { - result = -3; - break; + if(header_master[1] >= length) + { + /* Write the data */ + _spi->transfer(my_data, length); + } else + { + result = -2; + } + } else + { + result = -1; } - } - if(result == -3) - { digitalWrite(_cs_pin, HIGH); - _spi->endTransaction(); - break; - } -#endif /* SPBTLE_1S */ - /* Write the header */ - _spi->transfer(header_master, 5); + _spi->endTransaction(); -#if defined(SPBTLE_RF) - /* device is ready */ - if(header_master[0] == 0x02) + if((millis() - tickstart) > 1000) + { + result = -3; + break; + } + } else if (_ble_chip == SPBTLE_1S || _ble_chip == BLUENRG_M2SP) { - if(header_master[1] >= length) -#endif /* SPBTLE_RF */ -#if defined(SPBTLE_1S) + uint32_t tickstart_data_available = millis(); + result = 0; + + detachInterrupt(_spi_irq); + + _spi->beginTransaction(SPISettings(_frequency, MSBFIRST, _spi_mode)); + + digitalWrite(_cs_pin, LOW); + + /* + * Wait until BlueNRG-1 is ready. + * When ready it will raise the IRQ pin. + */ + while(!(digitalRead(_spi_irq) == 1)) + { + if((millis() - tickstart_data_available) > 1000) + { + result = -3; + break; + } + } + + if(result == -3) + { + digitalWrite(_cs_pin, HIGH); + _spi->endTransaction(); + attachInterrupt(_spi_irq, SPI_Irq_Callback, RISING); + break; + } + + /* Write the header */ + _spi->transfer(header_master, 5); + if((int)((((uint16_t)header_master[2])<<8) | ((uint16_t)header_master[1])) >= (int)length) -#endif /* SPBTLE_1S */ { /* Write the data */ _spi->transfer(my_data, length); @@ -337,21 +412,21 @@ size_t HCISpiTransportClass::write(const uint8_t* data, size_t length) { result = -2; } -#if defined(SPBTLE_RF) - } else - { - result = -1; - } -#endif /* SPBTLE_RF */ - digitalWrite(_cs_pin, HIGH); + digitalWrite(_cs_pin, HIGH); - _spi->endTransaction(); + _spi->endTransaction(); + + attachInterrupt(_spi_irq, SPI_Irq_Callback, RISING); - if((millis() - tickstart) > 1000) + if((millis() - tickstart) > 1000) + { + result = -3; + break; + } + } else { - result = -3; - break; + } } while(result < 0); @@ -364,7 +439,6 @@ size_t HCISpiTransportClass::write(const uint8_t* data, size_t length) } } -#if defined(SPBTLE_RF) void HCISpiTransportClass::wait_for_blue_initialize() { int event_blue_initialize = 0; @@ -535,15 +609,3 @@ void HCISpiTransportClass::enable_ll_only() _spi->endTransaction(); } while (result < 0); } -#endif /* SPBTLE_RF */ - -#if defined(ARDUINO_STEVAL_MKSBOX1V1) -HCISpiTransportClass HCISpiTransport(SpiHCI, PD0, PD4, PA8, 1000000, SPI_MODE1); /* STEVAL_MKSBOX1V1 */ -#elif defined(ARDUINO_DISCO_L475VG_IOT) -HCISpiTransportClass HCISpiTransport(SpiHCI, PD13, PE6, PA8, 8000000, SPI_MODE0); /* B-L475E-IOT01A1 */ -#else -HCISpiTransportClass HCISpiTransport(SpiHCI, A1, A0, D7, 8000000, SPI_MODE0); /* Shield IDB05A1 */ -#endif /* ARDUINO_STEVAL_MKSBOX1V1 */ -HCITransportInterface& HCITransport = HCISpiTransport; - -#endif /* ARDUINO_ARCH_STM32 */ diff --git a/src/utility/HCISpiTransport.h b/src/utility/HCISpiTransport.h index 70f03684..0318ebd5 100644 --- a/src/utility/HCISpiTransport.h +++ b/src/utility/HCISpiTransport.h @@ -23,19 +23,17 @@ #include "HCITransport.h" #include "SPI.h" -#if defined(ARDUINO_STEVAL_MKSBOX1V1) -#define SPBTLE_1S /* STEVAL_MKSBOX1V1 */ -#elif defined(ARDUINO_DISCO_L475VG_IOT) -#define SPBTLE_RF /* B-L475E-IOT01A1 */ -#else -#define SPBTLE_RF /* Shield IDB05A1 */ -#endif /* ARDUINO_STEVAL_MKSBOX1V1 */ +typedef enum BLEChip_s { + SPBTLE_RF, + SPBTLE_1S, + BLUENRG_M2SP +} BLEChip_t; #define BLE_MODULE_SPI_BUFFER_SIZE 128 class HCISpiTransportClass : public HCITransportInterface { public: - HCISpiTransportClass(SPIClass& spi, uint8_t cs_pin, uint8_t spi_irq, uint8_t ble_rst, unsigned long frequency, int spi_mode); + HCISpiTransportClass(SPIClass& spi, BLEChip_t ble_chip, uint8_t cs_pin, uint8_t spi_irq, uint8_t ble_rst, unsigned long frequency, int spi_mode); virtual ~HCISpiTransportClass(); virtual int begin(); @@ -50,12 +48,11 @@ class HCISpiTransportClass : public HCITransportInterface { virtual size_t write(const uint8_t* data, size_t length); private: -#if defined(SPBTLE_RF) void wait_for_blue_initialize(); void wait_for_enable_ll_only(); void enable_ll_only(); -#endif /* SPBTLE_RF */ SPIClass* _spi; + BLEChip_t _ble_chip; uint8_t _cs_pin; uint8_t _spi_irq; uint8_t _ble_rst; diff --git a/src/utility/HCIUartTransport.cpp b/src/utility/HCIUartTransport.cpp deleted file mode 100644 index 48578766..00000000 --- a/src/utility/HCIUartTransport.cpp +++ /dev/null @@ -1,103 +0,0 @@ -/* - This file is part of the ArduinoBLE library. - Copyright (c) 2018 Arduino SA. All rights reserved. - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -*/ - -#ifndef ARDUINO_ARCH_STM32 - -#ifndef ARDUINO_ARCH_MBED - -#include "HCIUartTransport.h" - -#if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_AVR_UNO_WIFI_REV2) -#define SerialHCI Serial2 -#elif defined(ARDUINO_SAMD_NANO_33_IOT) -// SerialHCI is already defined in the variant -#else -#error "Unsupported board selected!" -#endif - -HCIUartTransportClass::HCIUartTransportClass(HardwareSerial& uart, unsigned long baudrate) : - _uart(&uart), - _baudrate(baudrate) -{ -} - -HCIUartTransportClass::~HCIUartTransportClass() -{ -} - -int HCIUartTransportClass::begin() -{ - _uart->begin(_baudrate); - - return 1; -} - -void HCIUartTransportClass::end() -{ - _uart->end(); -} - -void HCIUartTransportClass::wait(unsigned long timeout) -{ - for (unsigned long start = millis(); (millis() - start) < timeout;) { - if (available()) { - break; - } - } -} - -int HCIUartTransportClass::available() -{ - return _uart->available(); -} - -int HCIUartTransportClass::peek() -{ - return _uart->peek(); -} - -int HCIUartTransportClass::read() -{ - return _uart->read(); -} - -size_t HCIUartTransportClass::write(const uint8_t* data, size_t length) -{ -#ifdef ARDUINO_AVR_UNO_WIFI_REV2 - // wait while the CTS pin is low - while (digitalRead(NINA_CTS) == HIGH); -#endif - - size_t result = _uart->write(data, length); - - _uart->flush(); - - return result; -} - -#ifdef ARDUINO_AVR_UNO_WIFI_REV2 -HCIUartTransportClass HCIUartTransport(SerialHCI, 119600); -#else -HCIUartTransportClass HCIUartTransport(SerialHCI, 912600); -#endif -HCITransportInterface& HCITransport = HCIUartTransport; - -#endif - -#endif /* ARDUINO_ARCH_STM32 */ diff --git a/src/utility/HCIUartTransport.h b/src/utility/HCIUartTransport.h deleted file mode 100644 index ba70dff4..00000000 --- a/src/utility/HCIUartTransport.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - This file is part of the ArduinoBLE library. - Copyright (c) 2018 Arduino SA. All rights reserved. - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -*/ - -#ifndef _HCI_UART_TRANSPORT_H_ -#define _HCI_UART_TRANSPORT_H_ - -#include "HCITransport.h" - -class HCIUartTransportClass : public HCITransportInterface { -public: - HCIUartTransportClass(HardwareSerial& uart, unsigned long baudrate); - virtual ~HCIUartTransportClass(); - - virtual int begin(); - virtual void end(); - - virtual void wait(unsigned long timeout); - - virtual int available(); - virtual int peek(); - virtual int read(); - - virtual size_t write(const uint8_t* data, size_t length); - -private: - HardwareSerial* _uart; - unsigned long _baudrate; -}; - -#endif From 02ff3a64f54b21ce815460964c8ec508e6f15ac4 Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Mon, 17 Feb 2020 09:00:30 +0100 Subject: [PATCH 02/22] SPISettings managements Signed-off-by: Frederic Pillon --- src/utility/HCISpiTransport.cpp | 19 +++++++++---------- src/utility/HCISpiTransport.h | 8 +++++--- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/utility/HCISpiTransport.cpp b/src/utility/HCISpiTransport.cpp index 08e3f12b..6f86f86b 100644 --- a/src/utility/HCISpiTransport.cpp +++ b/src/utility/HCISpiTransport.cpp @@ -21,15 +21,14 @@ volatile int data_avail = 0; -HCISpiTransportClass::HCISpiTransportClass(SPIClass& spi, BLEChip_t ble_chip, uint8_t cs_pin, uint8_t spi_irq, uint8_t ble_rst, unsigned long frequency, int spi_mode) : +HCISpiTransportClass::HCISpiTransportClass(SPIClass& spi, BLEChip_t ble_chip, uint8_t cs_pin, uint8_t spi_irq, uint8_t ble_rst, uint32_t frequency, uint8_t spi_mode) : _spi(&spi), _ble_chip(ble_chip), _cs_pin(cs_pin), _spi_irq(spi_irq), - _ble_rst(ble_rst), - _frequency(frequency), - _spi_mode(spi_mode) + _ble_rst(ble_rst) { + _spiSettings = SPISettings(frequency, (BitOrder)BLE_SPI_BYTE_ORDER, spi_mode); _read_index = 0; _write_index = 0; _write_index_initial = 0; @@ -125,7 +124,7 @@ int HCISpiTransportClass::available() detachInterrupt(_spi_irq); } - _spi->beginTransaction(SPISettings(_frequency, MSBFIRST, _spi_mode)); + _spi->beginTransaction(_spiSettings); digitalWrite(_cs_pin, LOW); @@ -337,7 +336,7 @@ size_t HCISpiTransportClass::write(const uint8_t* data, size_t length) { result = 0; - _spi->beginTransaction(SPISettings(_frequency, MSBFIRST, _spi_mode)); + _spi->beginTransaction(_spiSettings); digitalWrite(_cs_pin, LOW); @@ -376,7 +375,7 @@ size_t HCISpiTransportClass::write(const uint8_t* data, size_t length) detachInterrupt(_spi_irq); - _spi->beginTransaction(SPISettings(_frequency, MSBFIRST, _spi_mode)); + _spi->beginTransaction(_spiSettings); digitalWrite(_cs_pin, LOW); @@ -458,7 +457,7 @@ void HCISpiTransportClass::wait_for_blue_initialize() { uint8_t header_master[5] = {0x0b, 0x00, 0x00, 0x00, 0x00}; - _spi->beginTransaction(SPISettings(_frequency, MSBFIRST, _spi_mode)); + _spi->beginTransaction(_spiSettings); digitalWrite(_cs_pin, LOW); @@ -526,7 +525,7 @@ void HCISpiTransportClass::wait_for_enable_ll_only() { uint8_t header_master[5] = {0x0b, 0x00, 0x00, 0x00, 0x00}; - _spi->beginTransaction(SPISettings(_frequency, MSBFIRST, _spi_mode)); + _spi->beginTransaction(_spiSettings); digitalWrite(_cs_pin, LOW); @@ -580,7 +579,7 @@ void HCISpiTransportClass::enable_ll_only() { result = 0; - _spi->beginTransaction(SPISettings(_frequency, MSBFIRST, _spi_mode)); + _spi->beginTransaction(_spiSettings); digitalWrite(_cs_pin, LOW); diff --git a/src/utility/HCISpiTransport.h b/src/utility/HCISpiTransport.h index 0318ebd5..aeb509d1 100644 --- a/src/utility/HCISpiTransport.h +++ b/src/utility/HCISpiTransport.h @@ -29,11 +29,14 @@ typedef enum BLEChip_s { BLUENRG_M2SP } BLEChip_t; +#ifndef BLE_SPI_BYTE_ORDER +#define BLE_SPI_BYTE_ORDER MSBFIRST +#endif #define BLE_MODULE_SPI_BUFFER_SIZE 128 class HCISpiTransportClass : public HCITransportInterface { public: - HCISpiTransportClass(SPIClass& spi, BLEChip_t ble_chip, uint8_t cs_pin, uint8_t spi_irq, uint8_t ble_rst, unsigned long frequency, int spi_mode); + HCISpiTransportClass(SPIClass& spi, BLEChip_t ble_chip, uint8_t cs_pin, uint8_t spi_irq, uint8_t ble_rst, uint32_t frequency, uint8_t spi_mode); virtual ~HCISpiTransportClass(); virtual int begin(); @@ -52,12 +55,11 @@ class HCISpiTransportClass : public HCITransportInterface { void wait_for_enable_ll_only(); void enable_ll_only(); SPIClass* _spi; + SPISettings _spiSettings; BLEChip_t _ble_chip; uint8_t _cs_pin; uint8_t _spi_irq; uint8_t _ble_rst; - unsigned long _frequency; - int _spi_mode; uint8_t _rxbuff[BLE_MODULE_SPI_BUFFER_SIZE]; uint16_t _read_index; uint16_t _write_index; From 2f22ce95979cc609a23dcfac3bd67650cef763e9 Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Tue, 19 Nov 2019 12:07:36 +0100 Subject: [PATCH 03/22] Replace boolean with standard bool https://github.com/arduino/Arduino/issues/4673 Signed-off-by: Frederic Pillon --- examples/Peripheral/ButtonLED/ButtonLED.ino | 2 +- src/BLETypedCharacteristics.h | 2 +- src/utility/ATT.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/Peripheral/ButtonLED/ButtonLED.ino b/examples/Peripheral/ButtonLED/ButtonLED.ino index 9b0dfb0b..ea14b7c8 100644 --- a/examples/Peripheral/ButtonLED/ButtonLED.ino +++ b/examples/Peripheral/ButtonLED/ButtonLED.ino @@ -104,7 +104,7 @@ void loop() { char buttonValue = digitalRead(buttonPin); // has the value changed since the last read - boolean buttonChanged = (buttonCharacteristic.value() != buttonValue); + bool buttonChanged = (buttonCharacteristic.value() != buttonValue); if (buttonChanged) { // button state changed, update characteristics diff --git a/src/BLETypedCharacteristics.h b/src/BLETypedCharacteristics.h index 465fc046..8bd98c78 100644 --- a/src/BLETypedCharacteristics.h +++ b/src/BLETypedCharacteristics.h @@ -27,7 +27,7 @@ class BLEBoolCharacteristic : public BLETypedCharacteristic { BLEBoolCharacteristic(const char* uuid, unsigned char properties); }; -class BLEBooleanCharacteristic : public BLETypedCharacteristic { +class BLEBooleanCharacteristic : public BLETypedCharacteristic { public: BLEBooleanCharacteristic(const char* uuid, unsigned char properties); }; diff --git a/src/utility/ATT.cpp b/src/utility/ATT.cpp index 2696960e..6605a8a3 100644 --- a/src/utility/ATT.cpp +++ b/src/utility/ATT.cpp @@ -1142,7 +1142,7 @@ void ATTClass::readByTypeResp(uint16_t connectionHandle, uint8_t dlen, uint8_t d void ATTClass::writeReqOrCmd(uint16_t connectionHandle, uint16_t mtu, uint8_t op, uint8_t dlen, uint8_t data[]) { - boolean withResponse = (op == ATT_OP_WRITE_REQ); + bool withResponse = (op == ATT_OP_WRITE_REQ); if (dlen < sizeof(uint16_t)) { if (withResponse) { From 7e7cf887cf24410e50ce25d55bb2a3c312687ea1 Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Mon, 17 Feb 2020 10:08:13 +0100 Subject: [PATCH 04/22] Fix workflow to build ST targets Signed-off-by: Frederic Pillon --- .github/workflows/compile-examples.yml | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/.github/workflows/compile-examples.yml b/.github/workflows/compile-examples.yml index 884f11d6..baeeeec6 100644 --- a/.github/workflows/compile-examples.yml +++ b/.github/workflows/compile-examples.yml @@ -7,10 +7,9 @@ jobs: strategy: matrix: fqbn: [ - "arduino:samd:mkrwifi1010", - "arduino:samd:nano_33_iot", - "arduino:megaavr:uno2018:mode=on", - "arduino:mbed:nano33ble" + '"STM32:stm32:Eval:pnum=STEVAL_MKSBOX1V1,usb=CDCgen" "https://github.com/stm32duino/BoardManagerFiles/raw/master/STM32/package_stm_index.json"', + '"STM32:stm32:Nucleo_64:pnum=NUCLEO_L476RG" "https://github.com/stm32duino/BoardManagerFiles/raw/master/STM32/package_stm_index.json"', + '"STM32:stm32:Disco:pnum=DISCO_L475VG_IOT" "https://github.com/stm32duino/BoardManagerFiles/raw/master/STM32/package_stm_index.json"' ] steps: From ab1df57890c5e6d94e989c1efc17244c9782f3de Mon Sep 17 00:00:00 2001 From: Carlo Parata Date: Mon, 17 Feb 2020 10:38:38 +0100 Subject: [PATCH 05/22] Update examples/Central/PeripheralExplorer/PeripheralExplorer.ino Co-Authored-By: Frederic Pillon --- examples/Central/PeripheralExplorer/PeripheralExplorer.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/Central/PeripheralExplorer/PeripheralExplorer.ino b/examples/Central/PeripheralExplorer/PeripheralExplorer.ino index 357a8ef4..b32185cd 100644 --- a/examples/Central/PeripheralExplorer/PeripheralExplorer.ino +++ b/examples/Central/PeripheralExplorer/PeripheralExplorer.ino @@ -31,7 +31,7 @@ SPIClass SpiHCI(D11, D12, D3); HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, A1, A0, D7, 8000000, SPI_MODE0); BLELocalDevice BLE(&HCISpiTransport); /* Shield IDB05A1 with SPI clock on D13 */ -/*SPIClass SpiHCI(D11, D12, D13); +/*#define SpiHCI SPI HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, A1, A0, D7, 8000000, SPI_MODE0); BLELocalDevice BLE(&HCISpiTransport); */ /* Shield BNRG2A1 with SPI clock on D3 */ From 19ce6152cd33e9201f9704a6fdfcf6e3cc1ed9ed Mon Sep 17 00:00:00 2001 From: Carlo Parata Date: Mon, 17 Feb 2020 10:38:49 +0100 Subject: [PATCH 06/22] Update examples/Central/PeripheralExplorer/PeripheralExplorer.ino Co-Authored-By: Frederic Pillon --- examples/Central/PeripheralExplorer/PeripheralExplorer.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/Central/PeripheralExplorer/PeripheralExplorer.ino b/examples/Central/PeripheralExplorer/PeripheralExplorer.ino index b32185cd..82e1cc60 100644 --- a/examples/Central/PeripheralExplorer/PeripheralExplorer.ino +++ b/examples/Central/PeripheralExplorer/PeripheralExplorer.ino @@ -39,7 +39,7 @@ BLELocalDevice BLE(&HCISpiTransport); */ HCISpiTransportClass HCISpiTransport(SpiHCI, BLUENRG_M2SP, A1, A0, D7, 1000000, SPI_MODE1); BLELocalDevice BLE(&HCISpiTransport); */ /* Shield BNRG2A1 with SPI clock on D13 */ -/*SPIClass SpiHCI(D11, D12, D13); +/*#define SpiHCI SPI HCISpiTransportClass HCISpiTransport(SpiHCI, BLUENRG_M2SP, A1, A0, D7, 1000000, SPI_MODE1); BLELocalDevice BLE(&HCISpiTransport); */ #endif From 81ff00b5579f7bb3583fd4de23afb3a1d59c1eef Mon Sep 17 00:00:00 2001 From: Carlo Parata Date: Mon, 17 Feb 2020 10:38:57 +0100 Subject: [PATCH 07/22] Update examples/Central/LedControl/LedControl.ino Co-Authored-By: Frederic Pillon --- examples/Central/LedControl/LedControl.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/Central/LedControl/LedControl.ino b/examples/Central/LedControl/LedControl.ino index af082e00..5770c7cf 100644 --- a/examples/Central/LedControl/LedControl.ino +++ b/examples/Central/LedControl/LedControl.ino @@ -35,7 +35,7 @@ HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, A1, A0, D7, 8000000, SPI BLELocalDevice BLE(&HCISpiTransport); const int buttonPin = PC13; // set buttonPin to digital pin PC13 /* Shield IDB05A1 with SPI clock on D13 */ -/*SPIClass SpiHCI(D11, D12, D13); +/*#define SpiHCI SPI HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, A1, A0, D7, 8000000, SPI_MODE0); BLELocalDevice BLE(&HCISpiTransport); const int buttonPin = PC13; // set buttonPin to digital pin PC13 */ From 9f4679794b399a99b9cfd2077cd342ab774d80f5 Mon Sep 17 00:00:00 2001 From: Carlo Parata Date: Mon, 17 Feb 2020 10:39:06 +0100 Subject: [PATCH 08/22] Update examples/Central/LedControl/LedControl.ino Co-Authored-By: Frederic Pillon --- examples/Central/LedControl/LedControl.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/Central/LedControl/LedControl.ino b/examples/Central/LedControl/LedControl.ino index 5770c7cf..a3115f53 100644 --- a/examples/Central/LedControl/LedControl.ino +++ b/examples/Central/LedControl/LedControl.ino @@ -45,7 +45,7 @@ HCISpiTransportClass HCISpiTransport(SpiHCI, BLUENRG_M2SP, A1, A0, D7, 1000000, BLELocalDevice BLE(&HCISpiTransport); const int buttonPin = PC13; // set buttonPin to digital pin PC13 */ /* Shield BNRG2A1 with SPI clock on D13 */ -/*SPIClass SpiHCI(D11, D12, D13); +/*#define SpiHCI SPI HCISpiTransportClass HCISpiTransport(SpiHCI, BLUENRG_M2SP, A1, A0, D7, 1000000, SPI_MODE1); BLELocalDevice BLE(&HCISpiTransport); const int buttonPin = PC13; // set buttonPin to digital pin PC13 */ From 775a60aaaec40cf98bcaf354e4baaa965123a361 Mon Sep 17 00:00:00 2001 From: Carlo Parata Date: Mon, 17 Feb 2020 10:39:13 +0100 Subject: [PATCH 09/22] Update examples/Central/Scan/Scan.ino Co-Authored-By: Frederic Pillon --- examples/Central/Scan/Scan.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/Central/Scan/Scan.ino b/examples/Central/Scan/Scan.ino index 5326514f..650798ac 100644 --- a/examples/Central/Scan/Scan.ino +++ b/examples/Central/Scan/Scan.ino @@ -28,7 +28,7 @@ SPIClass SpiHCI(D11, D12, D3); HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, A1, A0, D7, 8000000, SPI_MODE0); BLELocalDevice BLE(&HCISpiTransport); /* Shield IDB05A1 with SPI clock on D13 */ -/*SPIClass SpiHCI(D11, D12, D13); +/*#define SpiHCI SPI HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, A1, A0, D7, 8000000, SPI_MODE0); BLELocalDevice BLE(&HCISpiTransport); */ /* Shield BNRG2A1 with SPI clock on D3 */ From f7d45fad296b21b29e087affbf47592b4ccb41e4 Mon Sep 17 00:00:00 2001 From: Carlo Parata Date: Mon, 17 Feb 2020 10:39:25 +0100 Subject: [PATCH 10/22] Update examples/Central/Scan/Scan.ino Co-Authored-By: Frederic Pillon --- examples/Central/Scan/Scan.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/Central/Scan/Scan.ino b/examples/Central/Scan/Scan.ino index 650798ac..74ce1a71 100644 --- a/examples/Central/Scan/Scan.ino +++ b/examples/Central/Scan/Scan.ino @@ -36,7 +36,7 @@ BLELocalDevice BLE(&HCISpiTransport); */ HCISpiTransportClass HCISpiTransport(SpiHCI, BLUENRG_M2SP, A1, A0, D7, 1000000, SPI_MODE1); BLELocalDevice BLE(&HCISpiTransport); */ /* Shield BNRG2A1 with SPI clock on D13 */ -/*SPIClass SpiHCI(D11, D12, D13); +/*#define SpiHCI SPI HCISpiTransportClass HCISpiTransport(SpiHCI, BLUENRG_M2SP, A1, A0, D7, 1000000, SPI_MODE1); BLELocalDevice BLE(&HCISpiTransport); */ #endif From c9f4239e46a53a7b9e40705b2065ef1d14bef8d6 Mon Sep 17 00:00:00 2001 From: Carlo Parata Date: Mon, 17 Feb 2020 10:39:37 +0100 Subject: [PATCH 11/22] Update examples/Central/ScanCallback/ScanCallback.ino Co-Authored-By: Frederic Pillon --- examples/Central/ScanCallback/ScanCallback.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/Central/ScanCallback/ScanCallback.ino b/examples/Central/ScanCallback/ScanCallback.ino index f84b88e1..e88f8789 100644 --- a/examples/Central/ScanCallback/ScanCallback.ino +++ b/examples/Central/ScanCallback/ScanCallback.ino @@ -30,7 +30,7 @@ SPIClass SpiHCI(D11, D12, D3); HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, A1, A0, D7, 8000000, SPI_MODE0); BLELocalDevice BLE(&HCISpiTransport); /* Shield IDB05A1 with SPI clock on D13 */ -/*SPIClass SpiHCI(D11, D12, D13); +/*#define SpiHCI SPI HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, A1, A0, D7, 8000000, SPI_MODE0); BLELocalDevice BLE(&HCISpiTransport); */ /* Shield BNRG2A1 with SPI clock on D3 */ From b598a3fa8d0fa3d8c180e9625add9db3409aa775 Mon Sep 17 00:00:00 2001 From: Carlo Parata Date: Mon, 17 Feb 2020 10:39:48 +0100 Subject: [PATCH 12/22] Update examples/Central/ScanCallback/ScanCallback.ino Co-Authored-By: Frederic Pillon --- examples/Central/ScanCallback/ScanCallback.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/Central/ScanCallback/ScanCallback.ino b/examples/Central/ScanCallback/ScanCallback.ino index e88f8789..3c927140 100644 --- a/examples/Central/ScanCallback/ScanCallback.ino +++ b/examples/Central/ScanCallback/ScanCallback.ino @@ -38,7 +38,7 @@ BLELocalDevice BLE(&HCISpiTransport); */ HCISpiTransportClass HCISpiTransport(SpiHCI, BLUENRG_M2SP, A1, A0, D7, 1000000, SPI_MODE1); BLELocalDevice BLE(&HCISpiTransport); */ /* Shield BNRG2A1 with SPI clock on D13 */ -/*SPIClass SpiHCI(D11, D12, D13); +/*#define SpiHCI SPI HCISpiTransportClass HCISpiTransport(SpiHCI, BLUENRG_M2SP, A1, A0, D7, 1000000, SPI_MODE1); BLELocalDevice BLE(&HCISpiTransport); */ #endif From aa6e1b30e8a3ebe426231107c4bc0b1a3f15c538 Mon Sep 17 00:00:00 2001 From: Carlo Parata Date: Mon, 17 Feb 2020 10:40:00 +0100 Subject: [PATCH 13/22] Update examples/Central/SensorTagButton/SensorTagButton.ino Co-Authored-By: Frederic Pillon --- examples/Central/SensorTagButton/SensorTagButton.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/Central/SensorTagButton/SensorTagButton.ino b/examples/Central/SensorTagButton/SensorTagButton.ino index dd8d8ade..cb43ae7d 100644 --- a/examples/Central/SensorTagButton/SensorTagButton.ino +++ b/examples/Central/SensorTagButton/SensorTagButton.ino @@ -32,7 +32,7 @@ SPIClass SpiHCI(D11, D12, D3); HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, A1, A0, D7, 8000000, SPI_MODE0); BLELocalDevice BLE(&HCISpiTransport); /* Shield IDB05A1 with SPI clock on D13 */ -/*SPIClass SpiHCI(D11, D12, D13); +/*#define SpiHCI SPI HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, A1, A0, D7, 8000000, SPI_MODE0); BLELocalDevice BLE(&HCISpiTransport); */ /* Shield BNRG2A1 with SPI clock on D3 */ From 18e0ca187283face9db991e610113bbdc0ed3ecb Mon Sep 17 00:00:00 2001 From: Carlo Parata Date: Mon, 17 Feb 2020 10:40:13 +0100 Subject: [PATCH 14/22] Update examples/Central/SensorTagButton/SensorTagButton.ino Co-Authored-By: Frederic Pillon --- examples/Central/SensorTagButton/SensorTagButton.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/Central/SensorTagButton/SensorTagButton.ino b/examples/Central/SensorTagButton/SensorTagButton.ino index cb43ae7d..b17f0766 100644 --- a/examples/Central/SensorTagButton/SensorTagButton.ino +++ b/examples/Central/SensorTagButton/SensorTagButton.ino @@ -40,7 +40,7 @@ BLELocalDevice BLE(&HCISpiTransport); */ HCISpiTransportClass HCISpiTransport(SpiHCI, BLUENRG_M2SP, A1, A0, D7, 1000000, SPI_MODE1); BLELocalDevice BLE(&HCISpiTransport); */ /* Shield BNRG2A1 with SPI clock on D13 */ -/*SPIClass SpiHCI(D11, D12, D13); +/*#define SpiHCI SPI HCISpiTransportClass HCISpiTransport(SpiHCI, BLUENRG_M2SP, A1, A0, D7, 1000000, SPI_MODE1); BLELocalDevice BLE(&HCISpiTransport); */ #endif From fafde937b1b5322af7433dbf25ab21754d8207c2 Mon Sep 17 00:00:00 2001 From: Carlo Parata Date: Mon, 17 Feb 2020 10:40:23 +0100 Subject: [PATCH 15/22] Update examples/Peripheral/ButtonLED/ButtonLED.ino Co-Authored-By: Frederic Pillon --- examples/Peripheral/ButtonLED/ButtonLED.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/Peripheral/ButtonLED/ButtonLED.ino b/examples/Peripheral/ButtonLED/ButtonLED.ino index ea14b7c8..9dd04044 100644 --- a/examples/Peripheral/ButtonLED/ButtonLED.ino +++ b/examples/Peripheral/ButtonLED/ButtonLED.ino @@ -36,7 +36,7 @@ HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, A1, A0, D7, 8000000, SPI BLELocalDevice BLE(&HCISpiTransport); const int buttonPin = PC13; // set buttonPin to digital pin PC13 /* Shield IDB05A1 with SPI clock on D13 */ -/*SPIClass SpiHCI(D11, D12, D13); +/*#define SpiHCI SPI HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, A1, A0, D7, 8000000, SPI_MODE0); BLELocalDevice BLE(&HCISpiTransport); const int buttonPin = PC13; // set buttonPin to digital pin PC13 */ From b95942fe54cc07f5e41e57a34eaf048036483b4a Mon Sep 17 00:00:00 2001 From: Carlo Parata Date: Mon, 17 Feb 2020 10:40:33 +0100 Subject: [PATCH 16/22] Update examples/Peripheral/ButtonLED/ButtonLED.ino Co-Authored-By: Frederic Pillon --- examples/Peripheral/ButtonLED/ButtonLED.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/Peripheral/ButtonLED/ButtonLED.ino b/examples/Peripheral/ButtonLED/ButtonLED.ino index 9dd04044..0e93f03b 100644 --- a/examples/Peripheral/ButtonLED/ButtonLED.ino +++ b/examples/Peripheral/ButtonLED/ButtonLED.ino @@ -46,7 +46,7 @@ HCISpiTransportClass HCISpiTransport(SpiHCI, BLUENRG_M2SP, A1, A0, D7, 1000000, BLELocalDevice BLE(&HCISpiTransport); const int buttonPin = PC13; // set buttonPin to digital pin PC13 */ /* Shield BNRG2A1 with SPI clock on D13 */ -/*SPIClass SpiHCI(D11, D12, D13); +/*#define SpiHCI SPI HCISpiTransportClass HCISpiTransport(SpiHCI, BLUENRG_M2SP, A1, A0, D7, 1000000, SPI_MODE1); BLELocalDevice BLE(&HCISpiTransport); const int buttonPin = PC13; // set buttonPin to digital pin PC13 */ From da2c4fdad68d4963f049efefd6f126c17036afa5 Mon Sep 17 00:00:00 2001 From: Carlo Parata Date: Mon, 17 Feb 2020 10:40:44 +0100 Subject: [PATCH 17/22] Update examples/Peripheral/CallbackLED/CallbackLED.ino Co-Authored-By: Frederic Pillon --- examples/Peripheral/CallbackLED/CallbackLED.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/Peripheral/CallbackLED/CallbackLED.ino b/examples/Peripheral/CallbackLED/CallbackLED.ino index c794856e..6b9e499d 100644 --- a/examples/Peripheral/CallbackLED/CallbackLED.ino +++ b/examples/Peripheral/CallbackLED/CallbackLED.ino @@ -37,7 +37,7 @@ BLELocalDevice BLE(&HCISpiTransport); HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, A1, A0, D7, 8000000, SPI_MODE0); BLELocalDevice BLE(&HCISpiTransport); */ /* Shield BNRG2A1 with SPI clock on D3 */ -/*SPIClass SpiHCI(D11, D12, D3); +/*#define SpiHCI SPI HCISpiTransportClass HCISpiTransport(SpiHCI, BLUENRG_M2SP, A1, A0, D7, 1000000, SPI_MODE1); BLELocalDevice BLE(&HCISpiTransport); */ /* Shield BNRG2A1 with SPI clock on D13 */ From 11bb9499910485b2f8b6cec100cfafb0f832fd42 Mon Sep 17 00:00:00 2001 From: Carlo Parata Date: Mon, 17 Feb 2020 10:40:55 +0100 Subject: [PATCH 18/22] Update examples/Peripheral/CallbackLED/CallbackLED.ino Co-Authored-By: Frederic Pillon --- examples/Peripheral/CallbackLED/CallbackLED.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/Peripheral/CallbackLED/CallbackLED.ino b/examples/Peripheral/CallbackLED/CallbackLED.ino index 6b9e499d..24929e5e 100644 --- a/examples/Peripheral/CallbackLED/CallbackLED.ino +++ b/examples/Peripheral/CallbackLED/CallbackLED.ino @@ -41,7 +41,7 @@ BLELocalDevice BLE(&HCISpiTransport); */ HCISpiTransportClass HCISpiTransport(SpiHCI, BLUENRG_M2SP, A1, A0, D7, 1000000, SPI_MODE1); BLELocalDevice BLE(&HCISpiTransport); */ /* Shield BNRG2A1 with SPI clock on D13 */ -/*SPIClass SpiHCI(D11, D12, D13); +/*#define SpiHCI SPI HCISpiTransportClass HCISpiTransport(SpiHCI, BLUENRG_M2SP, A1, A0, D7, 1000000, SPI_MODE1); BLELocalDevice BLE(&HCISpiTransport); */ #endif From d4bb5d87ce8f0cc48b44a0f97b2db0fb24471359 Mon Sep 17 00:00:00 2001 From: Carlo Parata Date: Mon, 17 Feb 2020 10:41:08 +0100 Subject: [PATCH 19/22] Update examples/Peripheral/LED/LED.ino Co-Authored-By: Frederic Pillon --- examples/Peripheral/LED/LED.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/Peripheral/LED/LED.ino b/examples/Peripheral/LED/LED.ino index a6da2dca..a23fe1a4 100644 --- a/examples/Peripheral/LED/LED.ino +++ b/examples/Peripheral/LED/LED.ino @@ -31,7 +31,7 @@ SPIClass SpiHCI(D11, D12, D3); HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, A1, A0, D7, 8000000, SPI_MODE0); BLELocalDevice BLE(&HCISpiTransport); /* Shield IDB05A1 with SPI clock on D13 */ -/*SPIClass SpiHCI(D11, D12, D13); +/*#define SpiHCI SPI HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, A1, A0, D7, 8000000, SPI_MODE0); BLELocalDevice BLE(&HCISpiTransport); */ /* Shield BNRG2A1 with SPI clock on D3 */ From e53282b2c1b537e098c107b97424f504a57c19f2 Mon Sep 17 00:00:00 2001 From: Carlo Parata Date: Mon, 17 Feb 2020 10:41:21 +0100 Subject: [PATCH 20/22] Update examples/Peripheral/LED/LED.ino Co-Authored-By: Frederic Pillon --- examples/Peripheral/LED/LED.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/Peripheral/LED/LED.ino b/examples/Peripheral/LED/LED.ino index a23fe1a4..c1a1ed58 100644 --- a/examples/Peripheral/LED/LED.ino +++ b/examples/Peripheral/LED/LED.ino @@ -39,7 +39,7 @@ BLELocalDevice BLE(&HCISpiTransport); */ HCISpiTransportClass HCISpiTransport(SpiHCI, BLUENRG_M2SP, A1, A0, D7, 1000000, SPI_MODE1); BLELocalDevice BLE(&HCISpiTransport); */ /* Shield BNRG2A1 with SPI clock on D13 */ -/*SPIClass SpiHCI(D11, D12, D13); +/*#define SpiHCI SPI HCISpiTransportClass HCISpiTransport(SpiHCI, BLUENRG_M2SP, A1, A0, D7, 1000000, SPI_MODE1); BLELocalDevice BLE(&HCISpiTransport); */ #endif From 5d789c63edcc99c463714a75fbe5ae1dc414e98a Mon Sep 17 00:00:00 2001 From: Carlo Parata Date: Mon, 17 Feb 2020 11:11:51 +0100 Subject: [PATCH 21/22] Improve check when BLE chip is not supported --- src/utility/HCISpiTransport.cpp | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/utility/HCISpiTransport.cpp b/src/utility/HCISpiTransport.cpp index 6f86f86b..486df38a 100644 --- a/src/utility/HCISpiTransport.cpp +++ b/src/utility/HCISpiTransport.cpp @@ -76,7 +76,8 @@ int HCISpiTransportClass::begin() delay(300); } else { - + // BLE chip not supported + return 0; } return 1; @@ -101,6 +102,11 @@ void HCISpiTransportClass::wait(unsigned long timeout) int HCISpiTransportClass::available() { + if(_ble_chip != SPBTLE_RF && _ble_chip !=SPBTLE_1S && _ble_chip !=BLUENRG_M2SP) + { + return 0; + } + if(_read_index != _write_index) { return 1; @@ -242,9 +248,6 @@ int HCISpiTransportClass::available() } } } - } else - { - } digitalWrite(_cs_pin, HIGH); @@ -268,9 +271,6 @@ int HCISpiTransportClass::available() { /* BLE chip was reset: we need to wait for a while */ delay(300); - } else - { - } /* Now we can update the write index and close the initial phase */ @@ -330,6 +330,11 @@ size_t HCISpiTransportClass::write(const uint8_t* data, size_t length) int result = 0; uint32_t tickstart = millis(); + if(_ble_chip != SPBTLE_RF && _ble_chip !=SPBTLE_1S && _ble_chip !=BLUENRG_M2SP) + { + return 0; + } + do { if (_ble_chip == SPBTLE_RF) @@ -423,9 +428,6 @@ size_t HCISpiTransportClass::write(const uint8_t* data, size_t length) result = -3; break; } - } else - { - } } while(result < 0); From bd1b0e0eb5c82e9ad0072cadd340ac1c2841de61 Mon Sep 17 00:00:00 2001 From: Carlo Parata Date: Mon, 17 Feb 2020 11:34:49 +0100 Subject: [PATCH 22/22] Update keywords.txt with new classes, methods and constants --- keywords.txt | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/keywords.txt b/keywords.txt index 12f67785..e7f1b228 100644 --- a/keywords.txt +++ b/keywords.txt @@ -30,6 +30,9 @@ BLEFloatCharacteristic KEYWORD1 BLEDoubleCharacteristic KEYWORD1 BLEStringCharacteristic KEYWORD1 +HCISpiTransportClass KEYWORD1 +HCITransportInterface KEYWORD1 + ####################################### # Methods and Functions (KEYWORD2) ####################################### @@ -112,7 +115,11 @@ setValueBE KEYWORD2 valueBE KEYWORD2 uuid KEYWORD2 -addCharacteristic KEYWORD +addCharacteristic KEYWORD2 + +wait KEYWORD2 +peek KEYWORD2 +write KEYWORD2 ####################################### # Constants (LITERAL1) @@ -132,5 +139,10 @@ BLEIndicate LITERAL1 BLESubscribed LITERAL1 BLEUnsubscribed LITERAL1 BLEWritten LITERAL1 -BLEUpdated LITERAL1 +BLEUpdated LITERAL1 + +SPBTLE_RF LITERAL1 +SPBTLE_1S LITERAL1 +BLUENRG_M2SP LITERAL1 +BLEChip_t LITERAL1