Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e7be519

Browse files
committedOct 12, 2022
Places new documentation in documentation folder
* Replaces old references to ISM part * Updates SPI MODE * Updates a few bit fields that have changed in the v4.0 reference document * Adds a few tap related functions * Adds a software reset function * Updates all examples with correct output data rate comment * Also updates all examples with sofware reset function and additional sensor class
1 parent 38a94af commit e7be519

15 files changed

+136
-38
lines changed
 
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

‎examples/example1_basic_readings/example1_basic_readings.ino

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "SparkFun_KX13X.h"
2525

2626
SparkFun_KX132 kxAccel;
27+
// SparkFun_KX134 kxAccel; // For the KX134, uncomment this and comment line above
2728

2829
outputData myData; // Struct for the accelerometer's data
2930

@@ -40,14 +41,17 @@ void setup()
4041
delay(50);
4142

4243

43-
if( !kxAccel.begin(chipSelect) )
44+
if( !kxAccel.begin() )
4445
{
4546
Serial.println("Could not communicate with the the KX13X. Freezing.");
4647
while(1);
4748
}
4849

4950
Serial.println("Ready.");
5051

52+
if( kxAccel.softwareReset() )
53+
Serial.println("Reset.");
54+
5155
// Many settings for KX13X can only be
5256
// applied when the accelerometer is powered down.
5357
// However there are many that can be changed "on-the-fly"
@@ -57,7 +61,7 @@ void setup()
5761

5862
kxAccel.setRange(0x18); // 16g Range
5963
kxAccel.enableDataEngine(); // Enables the bit that indicates data is ready.
60-
// kxAccel.setOutputDataRate(); // Default is 400Hz
64+
// kxAccel.setOutputDataRate(); // Default is 50Hz
6165
kxAccel.enableAccel();
6266

6367

‎examples/example2_interrupts/example2_interrupts.ino

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "SparkFun_KX13X.h"
2626

2727
SparkFun_KX132 kxAccel;
28+
// SparkFun_KX134 kxAccel; // For the KX134, uncomment this and comment line above
2829

2930
outputData myData; // Struct for the accelerometer's data
3031
byte dataReadyPin = 2; // Change to fit your project.
@@ -50,6 +51,10 @@ void setup()
5051

5152
Serial.println("Ready.");
5253

54+
// Reset the chip so that old settings don't apply to new setups.
55+
if( kxAccel.softwareReset() )
56+
Serial.println("Reset.");
57+
5358
// Many settings for KX13X can only be
5459
// applied when the accelerometer is powered down.
5560
// However there are many that can be changed "on-the-fly"

‎examples/example3_buffer/example3_buffer.ino

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "SparkFun_Qwiic_KX13X.h"
2626

2727
SparkFun_KX132 kxAccel;
28+
// SparkFun_KX134 kxAccel; // For the KX134, uncomment this and comment line above
2829

2930
outputData myData; // Struct for the accelerometer's data
3031
byte dataReadyPin = 2; // Change to fit your project.
@@ -50,6 +51,10 @@ void setup()
5051

5152
Serial.println("Ready.");
5253

54+
// Reset the chip so that old settings don't apply to new setups.
55+
if( kxAccel.softwareReset() )
56+
Serial.println("Reset.");
57+
5358
// Many settings for KX13X can only be
5459
// applied when the accelerometer is powered down.
5560
// However there are many that can be changed "on-the-fly"
@@ -77,7 +82,7 @@ void setup()
7782
// Changes depending on the resolution, see datasheet for more info.
7883

7984
kxAccel.setRange(0x18); // 16g Range
80-
//kxAccel.setOutputDataRate(); // Default is 400Hz
85+
//kxAccel.setOutputDataRate(); // Default is 50Hz
8186
kxAccel.enableAccel();
8287

8388

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
example6-tap
33
4-
This example shows the how to enable the tap interrupts
4+
This example shows the how to enable the tap interrupts.
55
66
Please refer to the header file for more possible settings, found here:
77
..\SparkFun_KX13X_Arduino_Library\src\sfe_kx13x_defs.h
@@ -20,22 +20,18 @@
2020
License (http://opensource.org/licenses/MIT).
2121
*/
2222

23-
#include <SPI.h>
2423
#include <Wire.h>
2524
#include "SparkFun_KX13X.h"
2625

27-
SparkFun_KX132_SPI kxAccel;
26+
SparkFun_KX132 kxAccel;
27+
// SparkFun_KX134 kxAccel; // For the KX134, uncomment this and comment line above
2828

29-
byte chipSelect = 1; // Change to fit your project.
29+
byte direction = 0;
3030

3131
void setup()
3232
{
3333

34-
// Get the chip select pin ready.
35-
pinMode(chipSelect, OUTPUT);
36-
digitalWrite(chipSelect, HIGH);
37-
38-
SPI.begin();
34+
Wire.begin();
3935

4036
Serial.begin(115200);
4137
Serial.println("Welcome.");
@@ -45,24 +41,29 @@ void setup()
4541
delay(50);
4642

4743

48-
if( !kxAccel.begin(chipSelect) )
44+
if( !kxAccel.begin() )
4945
{
5046
Serial.println("Could not communicate with the the KX13X. Freezing.");
5147
while(1);
5248
}
5349

5450
Serial.println("Ready.");
5551

52+
// Reset the chip so that old settings don't apply to new setups.
53+
if( kxAccel.softwareReset() )
54+
Serial.println("Reset.");
55+
5656
// Many settings for KX13X can only be
5757
// applied when the accelerometer is powered down.
5858
// However there are many that can be changed "on-the-fly"
5959
// check datasheet for more info, or the comments in the
6060
// "...regs.h" file which specify which can be changed when.
6161
kxAccel.enableAccel(false);
6262

63-
kxAccel.setRange(0x18); // 16g Range
64-
kxAccel.enableTapEngine(); // Enable tap Engine
65-
// kxAccel.setOutputDataRate(); // Default is 400Hz
63+
kxAccel.setRange(0x18); // 16g Range
64+
kxAccel.enableTapEngine(); // Enable tap Engine
65+
kxAccel.enableDirecTapInterupt(); // This enables checking the direction of the interrupt
66+
//kxAccel.setTapDataRate(uint8_t rate); // Default is 400Hz
6667
kxAccel.enableAccel();
6768

6869

@@ -73,14 +74,14 @@ void loop()
7374
// Check if tap was detected
7475
if( kxAccel.tapDetected() )
7576
{
76-
Serial.println("Tap Detected.");
77+
Serial.print("Tap Detected: ");
78+
Serial.println(kxAccel.getDirection(), HEX);
7779
kxAccel.clearInterrupt();
78-
while(1);
7980
}
8081

81-
if( kxAccel.unknownTap() || kxAccel.doubleTapDetected() )
82+
if( kxAccel.unknownTap() || kxAccel.doubleTapDetected() ) // These all share the same bit space
8283
kxAccel.clearInterrupt();
8384

84-
delay(30); // Delay should be 1/ODR (Output Data Rate), default tap ODR is 400Hz
85+
delay(25); // Delay should be 1/ODR (Output Data Rate), default tap ODR is 400Hz
8586

8687
}

‎examples/example5_spi/example5_spi.ino

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "SparkFun_KX13X.h"
2525

2626
SparkFun_KX132_SPI kxAccel;
27+
// SparkFun_KX134_SPI kxAccel; // For the KX134, uncomment this and comment line above
2728

2829
outputData myData; // Struct for the accelerometer's data
2930
byte chipSelect = 1; // Change to fit your project.
@@ -53,6 +54,10 @@ void setup()
5354

5455
Serial.println("Ready.");
5556

57+
// Reset the chip so that old settings don't apply to new setups.
58+
if( kxAccel.softwareReset() )
59+
Serial.println("Reset.");
60+
5661
// Many settings for KX13X can only be
5762
// applied when the accelerometer is powered down.
5863
// However there are many that can be changed "on-the-fly"
@@ -62,7 +67,7 @@ void setup()
6267

6368
kxAccel.setRange(0x18); // 16g Range
6469
kxAccel.enableDataEngine(); // Enables the bit that indicates data is ready.
65-
// kxAccel.setOutputDataRate(); // Default is 400Hz
70+
// kxAccel.setOutputDataRate(); // Default is 50Hz
6671
kxAccel.enableAccel();
6772

6873

‎src/SparkFun_KX13X.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,13 @@ class SparkFun_KX132_SPI : public QwDevKX132
105105
return this->QwDevKX132::init();
106106
}
107107

108-
bool begin(SPIClass &spiPort, SPISettings ismSettings, uint8_t cs)
108+
bool begin(SPIClass &spiPort, SPISettings kxSettings, uint8_t cs)
109109
{
110110
// Setup a SPI object and pass into the superclass
111111
setCommunicationBus(_spiBus);
112112

113113
// Initialize the SPI bus class with provided SPI port, SPI setttings, and chip select pin.
114-
_spiBus.init(spiPort, ismSettings, cs, true);
114+
_spiBus.init(spiPort, kxSettings, cs, true);
115115

116116
// Initialize the system - return results
117117
return this->QwDevKX132::init();
@@ -226,13 +226,13 @@ class SparkFun_KX134_SPI : public QwDevKX134
226226
return this->QwDevKX134::init();
227227
}
228228

229-
bool begin(SPIClass &spiPort, SPISettings ismSettings, uint8_t cs)
229+
bool begin(SPIClass &spiPort, SPISettings kxSettings, uint8_t cs)
230230
{
231231
// Setup a SPI object and pass into the superclass
232232
setCommunicationBus(_spiBus);
233233

234234
// Initialize the SPI bus class with provided SPI port, SPI setttings, and chip select pin.
235-
_spiBus.init(spiPort, ismSettings, cs, true);
235+
_spiBus.init(spiPort, kxSettings, cs, true);
236236

237237
// Initialize the system - return results
238238
return this->QwDevKX134::init();

‎src/SparkFun_KX13X_regs.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,10 @@ typedef struct
268268
// To change these settings make sure IC is in "stand-by" mode: PC1 bit in CNTL1.
269269
typedef struct
270270
{
271-
uint8_t iir_bypass : 1; // 0 - not bypassed : 1 - bypassed
271+
uint8_t reserved_one : 1;
272272
uint8_t lpro : 1; // 0 - Filter ODR/9 : 1 - Filter ODR/2
273273
uint8_t fstup : 1;
274-
uint8_t reserved_one : 1;
274+
uint8_t reserved_two : 1;
275275
uint8_t osa : 4;
276276
} sfe_kx13x_odcntl_t;
277277

@@ -310,7 +310,7 @@ typedef struct
310310
typedef struct
311311
{
312312
uint8_t undefined : 1;
313-
uint8_t tmen : 1;
313+
uint8_t tmen : 1; // Alternate masking scheme
314314
uint8_t tlem : 1; //X-
315315
uint8_t trim : 1; //X+
316316
uint8_t tdom : 1; //Y-

‎src/SparkFun_Qwiic_KX13X.cpp

Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ uint8_t QwDevKX13X::getUniqueID()
2222
{
2323
uint8_t tempVal;
2424
int retVal = readRegisterRegion(SFE_KX13X_WHO_AM_I, &tempVal, 1);
25+
Serial.println(tempVal);
2526

2627
if( retVal != 0 )
2728
return 0;
@@ -98,6 +99,21 @@ bool QwDevKX13X::initialize(uint8_t settings)
9899
return true;
99100
}
100101

102+
bool QwDevKX13X::softwareReset()
103+
{
104+
105+
uint8_t reset = 0x80;
106+
int retVal;
107+
108+
retVal = writeRegisterByte(SFE_KX13X_CNTL2, reset);
109+
110+
if( retVal != 0 )
111+
return true;
112+
113+
return false;
114+
115+
}
116+
101117
bool QwDevKX13X::enableAccel(bool enable)
102118
{
103119

@@ -245,6 +261,29 @@ bool QwDevKX13X::setOutputDataRate(uint8_t rate)
245261
return true;
246262
}
247263

264+
bool QwDevKX13X::setTapDataRate(uint8_t rate)
265+
{
266+
267+
if( rate > 7 )
268+
return false;
269+
270+
uint8_t tempVal;
271+
int retVal;
272+
273+
retVal = readRegisterRegion(SFE_KX13X_CNTL3, &tempVal, 1);
274+
275+
if( retVal != 0 )
276+
return false;
277+
278+
tempVal = tempVal | (rate << 3);
279+
280+
retVal = writeRegisterByte(SFE_KX13X_CNTL3, tempVal);
281+
282+
if( retVal != 0 )
283+
return false;
284+
285+
return true;
286+
}
248287
// Address:0x21 , bit[3:0]: default value is: 0x06 (50Hz)
249288
// Gets the accelerometer's output data rate.
250289
float QwDevKX13X::getOutputDataRate()
@@ -334,7 +373,7 @@ bool QwDevKX13X::setPinMode(bool activeLow)
334373
return true;
335374
}
336375

337-
bool QwDevKX13X::setLatchControl(bool pulse)
376+
bool QwDevKX13X::setLatchControl(bool latch)
338377
{
339378
int retVal;
340379
uint8_t tempVal;
@@ -344,7 +383,7 @@ bool QwDevKX13X::setLatchControl(bool pulse)
344383
if( retVal != 0 )
345384
return false;
346385

347-
tempVal = tempVal | (pulse << 4);
386+
tempVal = tempVal | (latch << 3);
348387

349388
retVal = writeRegisterByte(SFE_KX13X_INC1, tempVal);
350389

@@ -427,7 +466,7 @@ bool QwDevKX13X::clearInterrupt()
427466
return true;
428467
}
429468

430-
bool QwDevKX13X::enableTapInterupt(bool enable)
469+
bool QwDevKX13X::enableDirecTapInterupt(bool enable)
431470
{
432471
int retVal;
433472
uint8_t tempVal;
@@ -447,6 +486,26 @@ bool QwDevKX13X::enableTapInterupt(bool enable)
447486
return true;
448487
}
449488

489+
bool QwDevKX13X::enableDoubleTapInterupt(bool enable)
490+
{
491+
int retVal;
492+
uint8_t tempVal;
493+
494+
retVal = readRegisterRegion(SFE_KX13X_TDTRC, &tempVal, 1);
495+
496+
if( retVal != 0 )
497+
return false;
498+
499+
tempVal = tempVal | (enable << 1);
500+
501+
retVal = writeRegisterByte(SFE_KX13X_TDTRC, tempVal);
502+
503+
if( retVal != 0 )
504+
return false;
505+
506+
return true;
507+
}
508+
450509
bool QwDevKX13X::dataReady()
451510
{
452511

@@ -526,6 +585,7 @@ bool QwDevKX13X::tapDetected()
526585
if( retVal != 0 )
527586
return false;
528587

588+
529589
tempVal = tempVal & 0x0C; // Three states of interest: single tap detected
530590
// undefined, and no tap.
531591

@@ -535,6 +595,20 @@ bool QwDevKX13X::tapDetected()
535595
return false;
536596
}
537597

598+
int8_t QwDevKX13X::getDirection()
599+
{
600+
601+
int retVal;
602+
uint8_t tempVal;
603+
604+
retVal = readRegisterRegion(SFE_KX13X_INS1, &tempVal, 1);
605+
606+
if( retVal != 0 )
607+
return retVal;
608+
609+
return tempVal;
610+
}
611+
538612
bool QwDevKX13X::unknownTap()
539613
{
540614

@@ -927,7 +1001,7 @@ bool QwDevKX134::init(void)
9271001
if( !_sfeBus->ping(_i2cAddress) )
9281002
return false;
9291003

930-
if( getUniqueID() != KX132_WHO_AM_I )
1004+
if( getUniqueID() != KX134_WHO_AM_I )
9311005
return false;
9321006

9331007
return true;

‎src/SparkFun_Qwiic_KX13X.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,17 +144,21 @@ class QwDevKX13X
144144
bool runCommandTest();
145145
uint8_t readAccelState();
146146
bool getRawAccelData(rawOutputData*);
147+
bool softwareReset();
148+
bool setTapDataRate(uint8_t rate);
147149

148150
// Interrupt Settings
149151
bool configureInterruptPin(uint8_t pinVal);
150152
bool enablePhysInterrupt(bool enable = true, uint8_t pin = 1);
151-
bool enableTapInterupt(bool enable);
153+
bool enableDirecTapInterupt(bool enable = true);
154+
bool enableDoubleTapInterupt(bool enable = true);
152155
bool setPinMode(bool activeLow = true);
153-
bool setLatchControl(bool pulse = true);
156+
bool setLatchControl(bool latch = true);
154157
bool setPulseWidth(uint8_t width);
155158
float readOutputDataRate();
156159
bool clearInterrupt();
157160
bool tapDetected();
161+
int8_t getDirection();
158162
bool unknownTap();
159163
bool waterMarkReached();
160164
bool bufferFull();

‎src/sfe_bus.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ SfeSPI::SfeSPI(void) : _spiPort{nullptr}
218218
// will use the default
219219

220220

221-
bool SfeSPI::init(SPIClass &spiPort, SPISettings& ismSPISettings, uint8_t cs, bool bInit)
221+
bool SfeSPI::init(SPIClass &spiPort, SPISettings& kxSettings, uint8_t cs, bool bInit)
222222
{
223223

224224
// if we don't have a SPI port already
@@ -232,7 +232,7 @@ bool SfeSPI::init(SPIClass &spiPort, SPISettings& ismSPISettings, uint8_t cs, b
232232

233233

234234
// SPI settings are needed for every transaction
235-
_sfeSPISettings = ismSPISettings;
235+
_sfeSPISettings = kxSettings;
236236

237237
// The chip select pin can vary from platform to platform and project to project
238238
// and so it must be given by the user.
@@ -253,7 +253,7 @@ bool SfeSPI::init(uint8_t cs, bool bInit)
253253
{
254254

255255
//If the transaction settings are not provided by the user they are built here.
256-
SPISettings spiSettings = SPISettings(3000000, MSBFIRST, SPI_MODE3);
256+
SPISettings spiSettings = SPISettings(2000000, MSBFIRST, SPI_MODE0);
257257

258258
//In addition of the port is not provided by the user, it defaults to SPI here.
259259
return init(SPI, spiSettings, cs, bInit);

‎src/sfe_bus.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class SfeSPI : public QwIDeviceBus
105105

106106
bool init(uint8_t cs, bool bInit=false);
107107

108-
bool init(SPIClass& spiPort, SPISettings& ismSPISettings, uint8_t cs, bool bInit=false);
108+
bool init(SPIClass& spiPort, SPISettings& kxSettings, uint8_t cs, bool bInit=false);
109109

110110
bool ping(uint8_t address);
111111

0 commit comments

Comments
 (0)
Please sign in to comment.