Skip to content

Commit 699aa8e

Browse files
committed
Adds SPI example, rolls version
1 parent 6ddb182 commit 699aa8e

File tree

4 files changed

+116
-40
lines changed

4 files changed

+116
-40
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*********************************************************
2+
Author: Elias Santistevan @ SparkFun Electronics
3+
Date: 5/2021
4+
Library repository can be found here:
5+
https://github.com/sparkfun/SparkFun_KX13X_Arduino_Library
6+
7+
This example routes the accelerometer data to the buffer and then reads it when
8+
its full.
9+
10+
This code is released under the [MIT License](http://opensource.org/licenses/MIT).
11+
12+
Please review the LICENSE.md file included with this example. If you have any questions
13+
or concerns with licensing, please contact [email protected].
14+
15+
Distributed as-is; no warranty is given.
16+
*******************************************************/
17+
#include <SPI.h>
18+
#include "SparkFun_Qwiic_KX13X.h"
19+
20+
QwiicKX132 kxAccel;
21+
//QwiicKX134 kxAccel; // Uncomment this if using the KX134 - check your board
22+
//if unsure.
23+
outputData myData; // This will hold the accelerometer's output.
24+
int kxChipSelect = D1;
25+
26+
void setup() {
27+
28+
pinMode(kxChipSelect, OUTPUT);
29+
digitalWrite(kxChipSelect, HIGH);
30+
31+
Serial.begin(115200);
32+
Serial.println("Welcome.");
33+
34+
while(!Serial)
35+
delay(50);
36+
37+
SPI.begin();
38+
39+
if( !kxAccel.beginSPI(kxChipSelect) ){
40+
Serial.println("Could not communicate with the the KX13X. Freezing.");
41+
while(1);
42+
}
43+
else
44+
Serial.println("Ready.");
45+
46+
if( !kxAccel.initialize(DEFAULT_SETTINGS)){ //Load preset buffer settings.
47+
Serial.println("Could not initialize the chip. Freezing.");
48+
while(1);
49+
}
50+
else
51+
Serial.println("Initialized...");
52+
53+
//kxAccel.setRange(KX132_RANGE16G);
54+
//kxAccel.setRange(KX134_RANGE32G); // For a larger range uncomment
55+
56+
}
57+
58+
void loop()
59+
{
60+
61+
myData = kxAccel.getAccelData();
62+
Serial.print("X: ");
63+
Serial.print(myData.xData, 4);
64+
Serial.print(" Y: ");
65+
Serial.print(myData.yData, 4);
66+
Serial.print(" Z: ");
67+
Serial.print(myData.zData, 4);
68+
Serial.println();
69+
70+
delay(20);
71+
}

library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=SparkFun KX13X Arduino Library
2-
version=1.0.7
2+
version=1.0.8
33
author=SparkFun Electronics <[email protected]>
44
maintainer=Elias Santistevan @ SparkFun Electronics
55
sentence=Communicates and configures the SparkFun KX132/KX134 Accelerometer.

src/SparkFun_Qwiic_KX13X.cpp

+43-38
Original file line numberDiff line numberDiff line change
@@ -33,37 +33,32 @@ uint8_t QwiicKX13xCore::beginCore(uint8_t deviceAddress, TwoWire &i2cPort)
3333

3434
uint8_t QwiicKX13xCore::beginSPICore(uint8_t CSPin, uint32_t spiPortSpeed, SPIClass &spiPort)
3535
{
36+
uint8_t partID;
37+
3638
_i2cPort = NULL;
37-
_spiPortSpeed = spiPortSpeed;
3839
_spiPort = &spiPort;
40+
_cs = CSPin;
3941

4042
if( _spiPortSpeed > 10000000 )
4143
_spiPortSpeed = 10000000;
4244

43-
_cs = CSPin;
44-
pinMode(_cs, OUTPUT);
45-
digitalWrite(_cs, HIGH);
46-
//writeBit(INC1, SPI3E, 1); //Enable SPI
47-
//_i2cPort->end();
45+
_spiPortSpeed = spiPortSpeed;
46+
4847

49-
// CPOL and CPHA are demonstrated on pg 25 of Specification Data sheet
50-
// CPOL = 0, CPHA = 0 SPI_MODE0
48+
// CPOL and CPHA are demonstrated on pg 25 of Specification Data sheet
49+
// CPOL = 0, CPHA = 0 SPI_MODE0
5150
#ifdef ESP32
5251
kxSPISettings = SPISettings(spiPortSpeed, SPI_MSBFIRST, SPI_MODE0);
5352
#else
5453
kxSPISettings = SPISettings(spiPortSpeed, MSBFIRST, SPI_MODE0);
5554
#endif
56-
//#ifdef _MK20DX256_ //Teensy
57-
// kxSPISettings = SPISettings(spiPortSpeed, MSBFIRST, SPI_MODE0)
58-
//#endif
5955

60-
uint8_t partID;
6156
KX13X_STATUS_t status = readRegister(&partID, KX13X_WHO_AM_I);
57+
6258
if( status != KX13X_SUCCESS )
6359
return status;
64-
else
65-
return partID;
66-
60+
61+
return partID;
6762
}
6863

6964
// This function sets various register with regards to these pre-determined
@@ -411,31 +406,36 @@ KX13X_STATUS_t QwiicKX13xCore::readRegister(uint8_t *dataPointer, uint8_t reg)
411406
{
412407

413408
if( _i2cPort == NULL ) {
409+
414410
_spiPort->beginTransaction(kxSPISettings);
415411
digitalWrite(_cs, LOW);
416-
reg |= SPI_READ;
412+
413+
reg = (reg | SPI_READ);
417414
_spiPort->transfer(reg);
418-
*dataPointer = _spiPort->transfer(0x00);
415+
416+
*dataPointer = _spiPort->transfer(0x00);
417+
419418
digitalWrite(_cs, HIGH);
420419
_spiPort->endTransaction();
420+
421421
return KX13X_SUCCESS;
422422
}
423423

424-
else {
425424

426-
uint8_t i2cResult;
427-
_i2cPort->beginTransmission(_deviceAddress);
428-
_i2cPort->write(reg);
429-
i2cResult = _i2cPort->endTransmission(false);
430-
if( i2cResult != 0 )
431-
return KX13X_I2C_ERROR; //Error: Sensor did not ack
432-
i2cResult = _i2cPort->requestFrom(static_cast<uint8_t>(_deviceAddress), static_cast<uint8_t>(1)); //returns number of bytes
433-
if( i2cResult != 0) {
434-
*dataPointer = _i2cPort->read();
435-
return KX13X_SUCCESS;
436-
}
437-
return KX13X_I2C_ERROR; //Error: Sensor did not ack
438-
}
425+
uint8_t i2cResult;
426+
427+
_i2cPort->beginTransmission(_deviceAddress);
428+
_i2cPort->write(reg);
429+
i2cResult = _i2cPort->endTransmission(false);
430+
431+
if( i2cResult != 0 )
432+
return KX13X_I2C_ERROR; //Error: Sensor did not ack
433+
//
434+
_i2cPort->requestFrom(static_cast<uint8_t>(_deviceAddress), static_cast<uint8_t>(1)); //returns number of bytes
435+
//
436+
*dataPointer = _i2cPort->read();
437+
return KX13X_SUCCESS;
438+
439439
}
440440

441441
//Sends a request to read a number of registers
@@ -445,7 +445,7 @@ KX13X_STATUS_t QwiicKX13xCore::readMultipleRegisters(uint8_t reg, uint8_t dataBu
445445
if( _i2cPort == NULL ) {
446446
_spiPort->beginTransaction(kxSPISettings);
447447
digitalWrite(_cs, LOW);
448-
reg |= SPI_READ;
448+
reg = (reg | SPI_READ);
449449
_spiPort->transfer(reg);
450450
dataBuffer[0] = _spiPort->transfer(0x00); //first byte on transfer of address and read bit
451451
for(size_t i = 1; i < numBytes; i++) {
@@ -581,11 +581,14 @@ bool QwiicKX132::begin(uint8_t kxAddress, TwoWire &i2cPort){
581581
// register matches the correct value. Uses SPI for data transfer.
582582
bool QwiicKX132::beginSPI(uint8_t csPin, uint32_t spiPortSpeed, SPIClass &spiPort){
583583

584-
uint8_t partID = beginSPICore(csPin, spiPortSpeed, spiPort);
584+
uint8_t partID;
585+
586+
partID = beginSPICore(csPin, spiPortSpeed, spiPort);
587+
585588
if( partID == KX132_WHO_AM_I )
586589
return true;
587-
else
588-
return false;
590+
591+
return false;
589592
}
590593

591594
// Grabs raw accel data and passes it to the following function to be
@@ -668,11 +671,13 @@ bool QwiicKX134::begin(uint8_t kxAddress, TwoWire &i2cPort){
668671
// register matches the correct value. Uses SPI for data transfer.
669672
bool QwiicKX134::beginSPI(uint8_t csPin, uint32_t spiPortSpeed, SPIClass &spiPort){
670673

671-
uint8_t partID = beginSPICore(csPin, spiPortSpeed, spiPort);
674+
uint8_t partID;
675+
partID = beginSPICore(csPin, spiPortSpeed, spiPort);
676+
672677
if( partID == KX134_WHO_AM_I )
673678
return true;
674-
else
675-
return false;
679+
680+
return false;
676681
}
677682

678683
// Grabs raw accel data and passes it to the following function to be

src/SparkFun_Qwiic_KX13X.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ class QwiicKX132 : public QwiicKX13xCore
240240

241241
QwiicKX132();
242242
bool begin(uint8_t kxAddress = KX13X_DEFAULT_ADDRESS, TwoWire &i2cPort = Wire);
243-
bool beginSPI(uint8_t, uint32_t spiPortSpeed = 10000000, SPIClass &spiPort = SPI);
243+
bool beginSPI(uint8_t, uint32_t spiPortSpeed = 2000000, SPIClass &spiPort = SPI);
244244
outputData getAccelData();
245245
bool convAccelData(outputData*, rawOutputData*);
246246

0 commit comments

Comments
 (0)