Skip to content

Commit f9e3096

Browse files
authored
Merge pull request #18 from elle-bi/main
Add support for X-NUCLEO-BNRG2A1
2 parents 9b1dd2f + 0ee06d6 commit f9e3096

File tree

2 files changed

+49
-25
lines changed

2 files changed

+49
-25
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ to SensiBLE and see sensors data. The data are also printed to the serial port.
2525
X-NUCLEO-IKS01A2 or X-NUCLEO-IKS01A3, X-NUCLEO-IDB05A1 and X-NUCLEO-53L1A1 Expansion Boards. The application gather data from the sensor mounted and then communicates
2626
them via bluetooth to a suitable Android app (such as ST BLE Sensor). The data gathered include environmental data (temperature, pression, humidity),
2727
distance, gesture recognition (tap and directional swipe), acceleration (with accelerometer hardware events) and a gyroscope.
28-
* Flight1v2: This application provides an example of usage of a NUCLEO board (it was tested with NUCLEO-F401RE) with
29-
X-NUCLEO-IKS01A3, X-NUCLEO-IDB05A2 and X-NUCLEO-53L1A1 Expansion Boards. The application gathers data from the sensors mounted and then communicates
28+
* Flight1v2: This application provides an example of usage of a NUCLEO board (it was tested with a NUCLEO-F401RE and a NUCLEO-L476RG) with
29+
X-NUCLEO-IKS01A3, X-NUCLEO-IDB05A2, X-NUCLEO-BNRG2A1 and X-NUCLEO-53L1A1 Expansion Boards. The application gathers data from the sensors mounted and then communicates
3030
them via bluetooth to a suitable Android or iOS app (such as ST BLE Sensor). The data gathered include environmental data (temperature, pression, humidity),
3131
distance, gesture recognition (tap and directional swipe), acceleration (with accelerometer hardware events), a gyroscope and a magnetometer. In addition, 9x sensor fusion functionality for accurate orientation detection is also available. Note that
3232
unlike the Flight1 application, this one makes use of the STM32duinoBLE API instead of SPBTLE-RF, and does not support X-NUCLEO-IKS01A2 boards. In addition, an up-to-date version of the STM32duinoBLE library is required, as this application uses some of the newest features.

examples/Flight1v2/Flight1v2.ino

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
******************************************************************************
33
@file Flight1v2.ino
44
@author STMicroelectronics
5-
@version V1.1.0
6-
@date 23 March 2023
5+
@version V1.2.0
6+
@date 30 March 2023
77
@brief Arduino demo application for the STMicrolectronics
8-
X-NUCLEO-IKS01A3, X-NUCLEO-53L1A1
8+
X-NUCLEO-IKS01A3, X-NUCLEO-53L1A1, X-NUCLEO-BNRG2A1
99
and X-NUCLEO-IDB05A1
1010
******************************************************************************
1111
@attention
@@ -70,19 +70,32 @@
7070
#define DEV_I2C Wire
7171
#define SerialPort Serial
7272

73-
//#define DEBUG_MODE
74-
7573
#define INT_1 4
7674
#define INT_2 5
7775

76+
//#define DEBUG_MODE
77+
78+
//#define USE_BNRG2A1 // By default, the IDB05A2 is used. Uncomment this to use the BNRG2A1 instead.
79+
7880
// BLE boards
7981
/* Shield IDB05A2 with SPI clock on D3 */
82+
#ifndef USE_BNRG2A1
8083
SPIClass SpiHCI(D11, D12, D3);
8184
HCISpiTransportClass HCISpiTransport(SpiHCI, BLUENRG_M0, A1, A0, D7, 8000000, SPI_MODE0);
8285
#if !defined(FAKE_BLELOCALDEVICE)
8386
BLELocalDevice BLEObj(&HCISpiTransport);
8487
BLELocalDevice &BLE = BLEObj;
8588
#endif
89+
#endif
90+
91+
#ifdef USE_BNRG2A1
92+
SPIClass SpiHCI(D11, D12, D3);
93+
HCISpiTransportClass HCISpiTransport(SpiHCI, BLUENRG_M2SP, A1, A0, D7, 1000000, SPI_MODE1);
94+
#if !defined(FAKE_BLELOCALDEVICE)
95+
BLELocalDevice BLEObj(&HCISpiTransport);
96+
BLELocalDevice &BLE = BLEObj;
97+
#endif
98+
#endif
8699

87100
// Interrupts
88101
volatile int mems_event = 0;
@@ -153,7 +166,7 @@ volatile int mems_event = 0;
153166
#define GBIAS_MAG_TH_SC (2.0f*0.001500f)
154167

155168
#define DECIMATION 1U
156-
#define FUSION_FRAME 30 // Approximately 60 fps/ups
169+
#define FUSION_FRAME 30 // Approximately 30 fps/ups
157170

158171
/* Private variables ---------------------------------------------------------*/
159172
#if !(__CORTEX_M == 0U)
@@ -388,21 +401,21 @@ class Flight1Service {
388401
return ret;
389402
}
390403

391-
int AccEvent_Notify(uint16_t steps, uint8_t event)
404+
int AccEvent_Notify(uint8_t event)
392405
{
393406
uint8_t buff_2[2 + 1]; // Only event
394407
uint8_t buff_4[5]; // Event and pedometer
395408

396409
int ret = 0;
397410

398411
if (shortMode) {
399-
STORE_LE_16(buff_4, millis());
412+
STORE_LE_16(buff_2, millis());
400413
buff_2[2] = event;
401414
ret += accEventC.writeValue(buff_2, 2 + 1);
402415
} else {
403416
STORE_LE_16(buff_4, millis());
404417
buff_4[2] = event;
405-
STORE_LE_16(buff_4 + 3, steps);
418+
STORE_LE_16(buff_4 + 3, globalSteps);
406419
ret += accEventC.writeValue(buff_4, LEN_ACCEVENT);
407420
}
408421
return ret;
@@ -527,11 +540,9 @@ void fusion_update(void)
527540
fusion_flag = 1;
528541
}
529542

530-
void configCB(BLEDevice unused1, BLECharacteristic unused2)
543+
void configCB(BLEDevice, BLECharacteristic)
531544
{
532545
uint8_t buf[LEN_CFG];
533-
(void)unused1;
534-
(void)unused2;
535546

536547
configC.readValue(buf, LEN_CFG);
537548
char command = buf[4];
@@ -564,7 +575,7 @@ void configCB(BLEDevice unused1, BLECharacteristic unused2)
564575
break;
565576
case 'o':
566577
data ? Flight1.shortMode = true : Flight1.shortMode = false;
567-
data ? AccGyr.Enable_6D_Orientation(LSM6DSO_INT1_PIN) : AccGyr.Disable_6D_Orientation();;
578+
data ? AccGyr.Enable_6D_Orientation(LSM6DSO_INT1_PIN) : AccGyr.Disable_6D_Orientation();
568579
break;
569580
}
570581
}
@@ -579,7 +590,6 @@ int gestureGuard = 0;
579590

580591
bool envEnable = false;
581592
bool proxEnable = false;
582-
bool eventEnable = false;
583593
bool accEnable = false;
584594
bool gyroEnable = false;
585595
bool magEnable = false;
@@ -589,6 +599,8 @@ long fusionTime = 0;
589599
long currTime = 0;
590600
long lastTime = 0;
591601

602+
long upsTiming = 0;
603+
592604
void setup()
593605
{
594606
// Initialize serial port
@@ -746,13 +758,24 @@ void loop()
746758

747759
BLE.poll();
748760

749-
envEnable = pressC.subscribed() || tempC.subscribed() || humC.subscribed();
750-
accEnable = accC.subscribed();
751-
gyroEnable = gyroC.subscribed();
752-
magEnable = magC.subscribed();
753-
eventEnable = accEventC.subscribed();
754-
proxEnable = distanceC.subscribed() || gestureC.subscribed();
755-
fusionEnable = fusionC.subscribed();
761+
// If using the slower bluetooth chip BNRG2A1, we slow down the update rate to 30 ups
762+
#ifdef USE_BNRG2A1
763+
// Calculate how much time passed since the last update
764+
bool newUpdate = (millis() - upsTiming) > 33 ; // If it is over 33 milliseconds, activate the update flag
765+
if (millis() - upsTiming > 33) {
766+
upsTiming = millis(); // Also update the time of last update to current time
767+
}
768+
// Otherwise, we place no limit on update rate
769+
#else
770+
bool newUpdate = true;
771+
#endif
772+
773+
envEnable = (pressC.subscribed() || tempC.subscribed() || humC.subscribed()) && newUpdate;
774+
accEnable = accC.subscribed() && newUpdate;
775+
gyroEnable = gyroC.subscribed() && newUpdate;
776+
magEnable = magC.subscribed() && newUpdate;
777+
proxEnable = (distanceC.subscribed() || gestureC.subscribed()) && newUpdate;
778+
fusionEnable = fusionC.subscribed() && newUpdate;
756779

757780
if (fusionEnable) {
758781
if (!mag_calibrated) {
@@ -809,7 +832,7 @@ void loop()
809832
/* Disable magnetometer calibration */
810833
MotionFX_MagCal_init(ALGO_PERIOD, 0);
811834
digitalWrite(LED_BUILTIN, HIGH);
812-
Serial.println("Magnetomer calibration done!");
835+
SerialPort.println("Magnetomer calibration done!");
813836
}
814837
#endif
815838
}
@@ -919,6 +942,7 @@ void loop()
919942
Mag.GetAxes(magnetometer);
920943
}
921944

945+
// Do note that this is interrupt-based: it does NOT follow the timing rules of all other processes
922946
if (mems_event) {
923947
mems_event = 0;
924948
LSM6DSO_Event_Status_t Astatus;
@@ -987,7 +1011,7 @@ void loop()
9871011
FLIGHT1_PRINTF("Wake Up\n");
9881012
stat = stat | 0x80u;
9891013
}
990-
Flight1.AccEvent_Notify(Flight1.globalSteps, stat);
1014+
Flight1.AccEvent_Notify(stat);
9911015
}
9921016

9931017
if (proxEnable) {

0 commit comments

Comments
 (0)