Driver for KX134-1211 Accelerometer

Dependents:   KX134-1211 Examples

Files at this revision

API Documentation at this revision

Comitter:
Jasper Swallen
Date:
Sun Oct 25 15:47:36 2020 -0400
Parent:
0:01d5616ba355
Commit message:
make synchronous & read all accel data at once

Changed in this revision

KX134.cpp Show annotated file Show diff for this revision Revisions of this file
KX134.h Show annotated file Show diff for this revision Revisions of this file
diff -r 01d5616ba355 -r c6e2a348da09 KX134.cpp
--- a/KX134.cpp	Tue Sep 22 19:36:06 2020 -0400
+++ b/KX134.cpp	Sun Oct 25 15:47:36 2020 -0400
@@ -3,13 +3,13 @@
 
 #define SPI_FREQ 100000
 
-char defaultBuffer[2] = {0}; // allows calling writeRegisterOneByte
-                             // without buf argument
+static uint8_t defaultBuffer[2] = {0}; // allows calling writeRegisterOneByte
+                                       // without buf argument
 
 /* Writes one byte to a register
  */
 void KX134::writeRegisterOneByte(Register addr, uint8_t data,
-                                 char *buf = defaultBuffer)
+                                 uint8_t *buf = defaultBuffer)
 {
     uint8_t _data[1] = {data};
     writeRegister(addr, _data, buf);
@@ -21,7 +21,7 @@
 {
     // set default values for settings variables
     resStatus = 1;   // high performance mode
-    drdyeStatus = 0; // Data Ready Engine disabled
+    drdyeStatus = 1; // Data Ready Engine enabled
     gsel1Status = 0; // +-8g bit 1
     gsel0Status = 0; // +-8g bit 0
     tdteStatus = 0;  // Tap/Double-Tap engine disabled
@@ -78,7 +78,7 @@
     _cs.write(0);
 }
 
-void KX134::readRegister(Register addr, char *rx_buf, int size)
+void KX134::readRegister(Register addr, uint8_t *rx_buf, int size)
 {
     select();
 
@@ -92,7 +92,8 @@
     deselect();
 }
 
-void KX134::writeRegister(Register addr, uint8_t *data, char *rx_buf, int size)
+void KX134::writeRegister(Register addr, uint8_t *data, uint8_t *rx_buf,
+                          int size)
 {
     select();
 
@@ -105,19 +106,20 @@
     deselect();
 }
 
-/* Returns a 16 bit signed integer representation of a 2 address read
- * Assumes 2s Complement
- */
 int16_t KX134::read16BitValue(Register lowAddr, Register highAddr)
 {
     // get contents of register
-    char lowWord[2], highWord[2];
+    uint8_t lowWord[2], highWord[2];
     readRegister(lowAddr, lowWord);
     readRegister(highAddr, highWord);
 
+    return convertTo16BitValue(lowWord[1], highWord[1]);
+}
+
+int16_t KX134::convertTo16BitValue(uint8_t low, uint8_t high)
+{
     // combine low & high words
-    uint16_t val2sComplement =
-        (static_cast<uint16_t>(highWord[1] << 8)) | lowWord[1];
+    uint16_t val2sComplement = (static_cast<uint16_t>(high << 8)) | low;
     int16_t value = static_cast<int16_t>(val2sComplement);
 
     return value;
@@ -153,16 +155,22 @@
 
 void KX134::getAccelerations(int16_t *output)
 {
-    // read X, Y, and Z
-    output[0] = read16BitValue(Register::XOUT_L, Register::XOUT_H);
-    output[1] = read16BitValue(Register::YOUT_L, Register::YOUT_H);
-    output[2] = read16BitValue(Register::ZOUT_L, Register::ZOUT_H);
+    uint8_t words[7];
+
+    // this was the recommended method by Kionix
+    // for some reason, this has *significantly* less noise than reading
+    // one-by-one
+    readRegister(Register::XOUT_L, words, 7);
+
+    output[0] = convertTo16BitValue(words[1], words[2]);
+    output[1] = convertTo16BitValue(words[3], words[4]);
+    output[2] = convertTo16BitValue(words[5], words[6]);
 }
 
 bool KX134::checkExistence()
 {
     // verify WHO_I_AM
-    char whoami[5];
+    uint8_t whoami[2];
     readRegister(Register::WHO_AM_I, whoami);
 
     if(whoami[1] != 0x46)
@@ -171,7 +179,7 @@
     }
 
     // verify COTR
-    char cotr[2];
+    uint8_t cotr[2];
     readRegister(Register::COTR, cotr);
     if(cotr[1] != 0x55)
     {
@@ -250,3 +258,11 @@
 
     registerWritingEnabled = 0;
 }
+
+bool KX134::dataReady()
+{
+    uint8_t buf[2];
+    readRegister(Register::INS2, buf);
+
+    return (buf[1] == 0x10);
+}
diff -r 01d5616ba355 -r c6e2a348da09 KX134.h
--- a/KX134.h	Tue Sep 22 19:36:06 2020 -0400
+++ b/KX134.h	Sun Oct 25 15:47:36 2020 -0400
@@ -169,6 +169,8 @@
     // Set Output Data Rate from Hz
     void setOutputDataRateHz(uint32_t hz);
 
+    bool dataReady();
+
   private:
     // Mbed pin identities
     SPI _spi;
@@ -194,14 +196,14 @@
      * Note: the first byte read should return 0x0, so the data begins at
      * rx_buf[1]
      */
-    void readRegister(Register addr, char *rx_buf, int size = 2);
+    void readRegister(Register addr, uint8_t *rx_buf, int size = 2);
 
     /* Writes a given register a given number of bytes
      *
      * Note: the first byte read should return 0x0, so the data begins at
      * rx_buf[1]
      */
-    void writeRegister(Register addr, uint8_t *data, char *rx_buf,
+    void writeRegister(Register addr, uint8_t *data, uint8_t *rx_buf,
                        int size = 1);
 
     /* Writes a given register 1 byte (convenience function, calls
@@ -210,13 +212,18 @@
      * Note: the first byte read should return 0x0, so the data begins at
      * rx_buf[1]
      */
-    void writeRegisterOneByte(Register addr, uint8_t data, char *buf);
+    void writeRegisterOneByte(Register addr, uint8_t data, uint8_t *buf);
 
     /* Reads a value from a low and high address and combines them to create a
      * signed (2s complement) 16-bit integer
      */
     int16_t read16BitValue(Register lowAddr, Register highAddr);
 
+    /* Converts 2 8-bit unsigned integers to a single signed 16-bit (2s
+     * complement) integer
+     */
+    int16_t convertTo16BitValue(uint8_t low, uint8_t high);
+
     // Settings variables
 
     // CNTL1 vars
@@ -239,4 +246,4 @@
     bool registerWritingEnabled;
 };
 
-#endif // KX134_H
+#endif // KX134_H
\ No newline at end of file