Skip to content

Commit da40f27

Browse files
committed
Removing blocking SPI waits from main functions.
1 parent 3e89187 commit da40f27

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

examples/SPI/Example1-RotationVector/Example1-RotationVector.ino

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
7 = !RST
3434
3.3V = 3V3
3535
GND = GND
36-
3736
*/
3837

3938
#include <SPI.h>
@@ -47,9 +46,12 @@ byte imuWAKPin = 9;
4746
byte imuINTPin = 8;
4847
byte imuRSTPin = 7;
4948

49+
unsigned long startTime; //Used for calc'ing Hz
50+
long measurements = 0; //Used for calc'ing Hz
51+
5052
void setup()
5153
{
52-
Serial.begin(9600);
54+
Serial.begin(115200);
5355
Serial.println();
5456
Serial.println("BNO080 SPI Read Example");
5557

@@ -72,10 +74,15 @@ void setup()
7274

7375
Serial.println(F("Rotation vector enabled"));
7476
Serial.println(F("Output in form i, j, k, real, accuracy"));
77+
78+
startTime = millis();
7579
}
7680

7781
void loop()
7882
{
83+
Serial.println("Doing other things");
84+
delay(10); //You can do many other things. We spend most of our time printing and delaying.
85+
7986
//Look for reports from the IMU
8087
if (myIMU.dataAvailable() == true)
8188
{
@@ -84,6 +91,7 @@ void loop()
8491
float quatK = myIMU.getQuatK();
8592
float quatReal = myIMU.getQuatReal();
8693
float quatRadianAccuracy = myIMU.getQuatRadianAccuracy();
94+
measurements++;
8795

8896
Serial.print(quatI, 2);
8997
Serial.print(F(","));
@@ -95,7 +103,10 @@ void loop()
95103
Serial.print(F(","));
96104
Serial.print(quatRadianAccuracy, 2);
97105
Serial.print(F(","));
106+
Serial.print((float)measurements / ((millis() - startTime) / 1000.0), 2);
107+
Serial.print(F("Hz"));
98108

99109
Serial.println();
100110
}
111+
101112
}

src/SparkFun_BNO080_Arduino_Library.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,12 @@ boolean BNO080::beginSPI(uint8_t user_CSPin, uint8_t user_WAKPin, uint8_t user_I
102102
//host. It must not send any other data until this step is complete.
103103
//When BNO080 first boots it broadcasts big startup packet
104104
//Read it and dump it
105+
waitForSPI();
105106
receivePacket();
106107

107108
//The BNO080 will then transmit an unsolicited Initialize Response (see 6.4.5.2)
108109
//Read it and dump it
110+
waitForSPI();
109111
receivePacket();
110112

111113
//Check communication with device
@@ -116,6 +118,7 @@ boolean BNO080::beginSPI(uint8_t user_CSPin, uint8_t user_WAKPin, uint8_t user_I
116118
sendPacket(CHANNEL_CONTROL, 2);
117119

118120
//Now we wait for response
121+
waitForSPI();
119122
if (receivePacket() == true)
120123
{
121124
if (shtpData[0] == SHTP_REPORT_PRODUCT_ID_RESPONSE)
@@ -950,6 +953,7 @@ boolean BNO080::waitForSPI()
950953
for (uint8_t counter = 0 ; counter < 125 ; counter++) //Don't got more than 255
951954
{
952955
if (digitalRead(_int) == LOW) return (true);
956+
if (_printDebug == true) _debugPort->println(F("SPI Wait"));
953957
delay(1);
954958
}
955959

@@ -963,7 +967,9 @@ boolean BNO080::receivePacket(void)
963967
{
964968
if (_i2cPort == NULL) //Do SPI
965969
{
966-
if (waitForSPI() == false) return (false); //Something went wrong
970+
if (digitalRead(_int) == HIGH) return (false); //Data is not available
971+
972+
//Old way: if (waitForSPI() == false) return (false); //Something went wrong
967973

968974
//Get first four bytes to find out how much data we need to read
969975

0 commit comments

Comments
 (0)