Skip to content

Commit 0d65289

Browse files
committed
Update getRawAccelData so it can read 8-bit buffer data if required
1 parent 7c599e0 commit 0d65289

File tree

1 file changed

+65
-17
lines changed

1 file changed

+65
-17
lines changed

src/SparkFun_Qwiic_KX13X.cpp

Lines changed: 65 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,7 +1153,10 @@ bool QwDevKX13X::setBufferResolution(bool sixteenBit)
11531153
if (retVal != 0)
11541154
return false;
11551155

1156-
tempVal = tempVal | ((uint8_t)sixteenBit << 6);
1156+
sfe_kx13x_buf_cntl2_bitfield_t bufCntl2;
1157+
bufCntl2.all = tempVal;
1158+
bufCntl2.bits.bres = sixteenBit; // This is a long winded but definitive way of setting/clearing the resolution bit
1159+
tempVal = bufCntl2.all;
11571160

11581161
retVal = writeRegisterByte(SFE_KX13X_BUF_CNTL2, tempVal);
11591162

@@ -1181,7 +1184,10 @@ bool QwDevKX13X::enableBufferInt(bool enable)
11811184
if (retVal != 0)
11821185
return false;
11831186

1184-
tempVal = tempVal | (enable << 5);
1187+
sfe_kx13x_buf_cntl2_bitfield_t bufCntl2;
1188+
bufCntl2.all = tempVal;
1189+
bufCntl2.bits.bfie = enable; // This is a long winded but definitive way of setting/clearing the buffer interrupt enable bit
1190+
tempVal = bufCntl2.all;
11851191

11861192
retVal = writeRegisterByte(SFE_KX13X_BUF_CNTL2, tempVal);
11871193

@@ -1209,7 +1215,10 @@ bool QwDevKX13X::enableSampleBuffer(bool enable)
12091215
if (retVal != 0)
12101216
return false;
12111217

1212-
tempVal = tempVal | ((uint8_t)enable << 7);
1218+
sfe_kx13x_buf_cntl2_bitfield_t bufCntl2;
1219+
bufCntl2.all = tempVal;
1220+
bufCntl2.bits.bufe = enable; // This is a long winded but definitive way of setting/clearing the buffer enable bit
1221+
tempVal = bufCntl2.all;
12131222

12141223
retVal = writeRegisterByte(SFE_KX13X_BUF_CNTL2, tempVal);
12151224

@@ -1236,7 +1245,7 @@ uint16_t QwDevKX13X::getSampleLevel()
12361245
return 0;
12371246

12381247
numSamples = tempVal[0];
1239-
numSamples = numSamples | ((tempVal[1] & 0x03) << 8);
1248+
numSamples = numSamples | (((uint16_t)tempVal[1] & 0x03) << 8);
12401249

12411250
return numSamples;
12421251
}
@@ -1276,7 +1285,10 @@ bool QwDevKX13X::runCommandTest()
12761285
if (retVal != 0)
12771286
return false;
12781287

1279-
tempVal = tempVal | 0x40;
1288+
sfe_kx13x_cntl2_bitfield_t cntl2;
1289+
cntl2.all = tempVal;
1290+
cntl2.bits.cotc = 1; // This is a long winded, but definitive way of setting the COTC bit
1291+
tempVal = cntl2.all;
12801292

12811293
// Going to assume that communication is working at this point.
12821294
writeRegisterByte(SFE_KX13X_CNTL2, tempVal);
@@ -1288,7 +1300,8 @@ bool QwDevKX13X::runCommandTest()
12881300

12891301
readRegisterRegion(SFE_KX13X_CNTL2, &tempVal, 1);
12901302

1291-
if (tempVal != 0)
1303+
cntl2.all = tempVal;
1304+
if (cntl2.bits.cotc != 0)
12921305
return false;
12931306

12941307
readRegisterRegion(SFE_KX13X_COTR, &tempVal, 1);
@@ -1314,26 +1327,61 @@ bool QwDevKX13X::getRawAccelData(rawOutputData *rawAccelData)
13141327
uint8_t tempVal;
13151328
uint8_t tempRegData[6] = {0};
13161329

1317-
// Check if buffer is enabled
1318-
retVal = readRegisterRegion(SFE_KX13X_INC4, &tempVal, 1);
1330+
// Check if the buffer full interrupt is activated
1331+
//retVal = readRegisterRegion(SFE_KX13X_INC4, &tempVal, 1); // inc4.bfi1 indicates if the buffer full interrupt is reported on INT1
1332+
retVal = readRegisterRegion(SFE_KX13X_BUF_CNTL2, &tempVal, 1); // bufCntl2.bits.bufe indicates if the buffer is enabled
13191333

13201334
if (retVal != 0)
13211335
return false;
13221336

1323-
if (tempVal & 0x40) // If Buffer is enabled, read there.
1324-
retVal = readRegisterRegion(SFE_KX13X_BUF_READ, tempRegData, 6);
1337+
sfe_kx13x_buf_cntl2_bitfield_t bufCntl2;
1338+
bufCntl2.all = tempVal;
1339+
1340+
bool is16bit = true;
1341+
1342+
if (bufCntl2.bits.bufe) // If Buffer is enabled, read there.
1343+
{
1344+
if (getSampleLevel() > 0) // Check the buffer contains data
1345+
{
1346+
if (bufCntl2.bits.bres) // If the buffer contains 16-bit samples
1347+
retVal = readRegisterRegion(SFE_KX13X_BUF_READ, tempRegData, 6); // Read 3 * 16-bit
1348+
else
1349+
{
1350+
retVal = readRegisterRegion(SFE_KX13X_BUF_READ, tempRegData, 3); // Read 3 * 8-bit
1351+
is16bit = false;
1352+
}
1353+
}
1354+
else
1355+
// No buffer data to read!
1356+
// We can either:
1357+
//return false;
1358+
// Or, be kind and read the normal registers
1359+
retVal = readRegisterRegion(SFE_KX13X_XOUT_L, tempRegData, 6); // Read 3 * 16-bit
1360+
}
13251361
else
1326-
retVal = readRegisterRegion(SFE_KX13X_XOUT_L, tempRegData, 6);
1362+
retVal = readRegisterRegion(SFE_KX13X_XOUT_L, tempRegData, 6); // Read 3 * 16-bit
13271363

13281364
if (retVal != 0)
13291365
return false;
13301366

1331-
rawAccelData->xData = tempRegData[XLSB];
1332-
rawAccelData->xData |= (uint16_t)((tempRegData[XMSB]) << 8);
1333-
rawAccelData->yData = tempRegData[YLSB];
1334-
rawAccelData->yData |= (uint16_t)((tempRegData[YMSB]) << 8);
1335-
rawAccelData->zData = tempRegData[ZLSB];
1336-
rawAccelData->zData |= ((uint16_t)(tempRegData[ZMSB]) << 8);
1367+
if (is16bit) // Process buffer 8-bit samples
1368+
{
1369+
rawAccelData->xData = tempRegData[XLSB];
1370+
rawAccelData->xData |= (uint16_t)tempRegData[XMSB] << 8;
1371+
rawAccelData->yData = tempRegData[YLSB];
1372+
rawAccelData->yData |= (uint16_t)tempRegData[YMSB] << 8;
1373+
rawAccelData->zData = tempRegData[ZLSB];
1374+
rawAccelData->zData |= (uint16_t)tempRegData[ZMSB] << 8;
1375+
}
1376+
else
1377+
{
1378+
rawAccelData->xData = 0;
1379+
rawAccelData->xData |= (uint16_t)tempRegData[0] << 8; // Convert 8-bit signed to 16-bit signed
1380+
rawAccelData->yData = 0;
1381+
rawAccelData->yData |= (uint16_t)tempRegData[1] << 8;
1382+
rawAccelData->zData = 0;
1383+
rawAccelData->zData |= (uint16_t)tempRegData[2] << 8;
1384+
}
13371385

13381386
return true;
13391387
}

0 commit comments

Comments
 (0)