Jasper Swallen / KX134-1211 Driver

Dependents:   KX134-1211 Examples

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers KX134.h Source File

KX134.h

00001 /**
00002  * @author Jasper Swallen
00003  * @filename KX134.h
00004  *
00005  * @section DESCRIPTION
00006  *
00007  * Software Driver for KX134-1211 accelerometer
00008  *
00009  * Datasheets:
00010  * http://kionixfs.kionix.com/en/document/AN101-Getting-Started.pdf
00011  * https://d10bqar0tuhard.cloudfront.net/en/document/KX134-1211-Technical-Reference-Manual-Rev-1.0.pdf
00012  * https://d10bqar0tuhard.cloudfront.net/en/document/TN027-Power-On-Procedure.pdf
00013  * https://d10bqar0tuhard.cloudfront.net/en/datasheet/KX134-1211-Specifications-Rev-1.0.pdf
00014  */
00015 
00016 #ifndef KX134_H
00017 #define KX134_H
00018 
00019 #include "mbed.h"
00020 
00021 class KX134
00022 {
00023   public:
00024     enum class Register : uint8_t
00025     {
00026         MAN_ID = 0x00,
00027         PART_ID = 0x01,
00028         XADP_L = 0x02,
00029         XADP_H = 0x03,
00030         YADP_L = 0x04,
00031         YADP_H = 0x05,
00032         ZADP_L = 0x06,
00033         ZADP_H = 0x07,
00034         XOUT_L = 0x08,
00035         XOUT_H = 0x09,
00036         YOUT_L = 0x0A,
00037         YOUT_H = 0x0B,
00038         ZOUT_L = 0x0C,
00039         ZOUT_H = 0x0D,
00040         COTR = 0x12,
00041         WHO_AM_I = 0x13,
00042         TSCP = 0x14,
00043         TSPP = 0x15,
00044         INS1 = 0x16,
00045         INS2 = 0x17,
00046         INS3 = 0x18,
00047         STATUS_REG = 0x19,
00048         INT_REL = 0x1A,
00049         CNTL1 = 0x1B,
00050         CNTL2 = 0x1C,
00051         CNTL3 = 0x1D,
00052         CNTL4 = 0x1E,
00053         CNTL5 = 0x1F,
00054         CNTL6 = 0x20,
00055         ODCNTL = 0x21,
00056         INC1 = 0x22,
00057         INC2 = 0x23,
00058         INC3 = 0x24,
00059         INC4 = 0x25,
00060         INC5 = 0x26,
00061         INC6 = 0x27,
00062         TILT_TIMER = 0x29,
00063         TDTRC = 0x2A,
00064         TDTC = 0x2B,
00065         TTH = 0x2C,
00066         TTL = 0x2D,
00067         FTD = 0x2E,
00068         STD = 0x2F,
00069         TLT = 0x30,
00070         TWS = 0x31,
00071         FFTH = 0x32,
00072         FFC = 0x33,
00073         FFCNTL = 0x34,
00074         TILT_ANGLE_LL = 0x37,
00075         TILT_ANGLE_HL = 0x38,
00076         HYST_SET = 0x39,
00077         LP_CNTL1 = 0x3A,
00078         LP_CNTL2 = 0x3B,
00079         WUFTH = 0x49,
00080         BTSWUFTH = 0x4A,
00081         BTSTH = 0x4B,
00082         BTSC = 0x4C,
00083         WUFC = 0x4D,
00084         SELF_TEST = 0x5D,
00085         BUF_CNTL1 = 0x5E,
00086         BUF_CNTL2 = 0x5F,
00087         BUF_STATUS_1 = 0x60,
00088         BUF_STATUS_2 = 0x61,
00089         BUF_CLEAR = 0x62,
00090         BUF_READ = 0x63,
00091         ADP_CNTL1 = 0x64,
00092         ADP_CNTL2 = 0x65,
00093         ADP_CNTL3 = 0x66,
00094         ADP_CNTL4 = 0x67,
00095         ADP_CNTL5 = 0x68,
00096         ADP_CNTL6 = 0x69,
00097         ADP_CNTL7 = 0x6A,
00098         ADP_CNTL8 = 0x6B,
00099         ADP_CNTL9 = 0x6C,
00100         ADP_CNTL10 = 0x6D,
00101         ADP_CNTL11 = 0x6E,
00102         ADP_CNTL12 = 0x6F,
00103         ADP_CNTL13 = 0x70,
00104         ADP_CNTL14 = 0x71,
00105         ADP_CNTL15 = 0x72,
00106         ADP_CNTL16 = 0x73,
00107         ADP_CNTL17 = 0x74,
00108         ADP_CNTL18 = 0x75,
00109         ADP_CNTL19 = 0x76,
00110         INTERNAL_0X7F = 0x7F
00111     };
00112 
00113     enum class Range : uint8_t
00114     {
00115         RANGE_8G = 0b00,
00116         RANGE_16G = 0b01,
00117         RANGE_32G = 0b10,
00118         RANGE_64G = 0b11
00119     };
00120 
00121     KX134(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName int1,
00122           PinName int2, PinName rst);
00123 
00124     bool init();
00125 
00126     /* Converts a LSB value to gravs
00127      * To convert to m/s^2, multiply by 1G (~9.8m/s^2)
00128      *
00129      * Note:
00130      * +-64g: 1LSB = 0.00195g
00131      * +-32g: 1LSB = 0.00098g
00132      * +-16g: 1LSB = 0.00049g
00133      * +-8g: 1LSB = 0.00024g
00134      */
00135     float convertRawToGravs(int16_t lsbValue);
00136 
00137     /* Changes the value of output[3] as follows:
00138      * output[0] is X acceleration
00139      * output[1] is Y accel
00140      * output[2] is Z accel
00141      *
00142      * Results are in LSB format, to convert call convertRawToGravs() on each
00143      * output
00144      */
00145     void getAccelerations(int16_t *output);
00146 
00147     /* Verifies the KX134-1211 unit is connected and functioning normally.
00148      * If it returns false, call reset() and check again.
00149      */
00150     bool checkExistence();
00151 
00152     /* To enable writing to settings registers, this function must be called.
00153      * After writing settings, register writing is automatically disabled, and
00154      * this function must be called again to enable it.
00155      */
00156     void enableRegisterWriting();
00157 
00158     /* Saves settings as currently set and disables register writing.
00159      * Useful for state changes
00160      */
00161     void disableRegisterWriting();
00162 
00163     // Set acceleration range (8, 16, 32, or 64 gs)
00164     void setAccelRange(Range range);
00165 
00166     // Set Output Data Rate Bitwise
00167     void setOutputDataRateBytes(uint8_t byteHz);
00168 
00169     // Set Output Data Rate from Hz
00170     void setOutputDataRateHz(uint32_t hz);
00171 
00172     bool dataReady();
00173 
00174   private:
00175     // Mbed pin identities
00176     SPI _spi;
00177     PinName _int1, _int2;
00178     DigitalOut _cs;
00179     DigitalOut _rst;
00180 
00181     /* Reset function
00182      * Should be called on initial start (init()) and every software reset
00183      */
00184     bool reset();
00185 
00186     /* Deselect (push high) _cs
00187      */
00188     void deselect();
00189 
00190     /* Select (push low) _cs
00191      */
00192     void select();
00193 
00194     /* Read a given register a given number of bytes
00195      *
00196      * Note: the first byte read should return 0x0, so the data begins at
00197      * rx_buf[1]
00198      */
00199     void readRegister(Register addr, uint8_t *rx_buf, int size = 2);
00200 
00201     /* Writes a given register a given number of bytes
00202      *
00203      * Note: the first byte read should return 0x0, so the data begins at
00204      * rx_buf[1]
00205      */
00206     void writeRegister(Register addr, uint8_t *data, uint8_t *rx_buf,
00207                        int size = 1);
00208 
00209     /* Writes a given register 1 byte (convenience function, calls
00210      * writeRegister())
00211      *
00212      * Note: the first byte read should return 0x0, so the data begins at
00213      * rx_buf[1]
00214      */
00215     void writeRegisterOneByte(Register addr, uint8_t data, uint8_t *buf);
00216 
00217     /* Reads a value from a low and high address and combines them to create a
00218      * signed (2s complement) 16-bit integer
00219      */
00220     int16_t read16BitValue(Register lowAddr, Register highAddr);
00221 
00222     /* Converts 2 8-bit unsigned integers to a single signed 16-bit (2s
00223      * complement) integer
00224      */
00225     int16_t convertTo16BitValue(uint8_t low, uint8_t high);
00226 
00227     // Settings variables
00228 
00229     // CNTL1 vars
00230     bool resStatus;
00231     bool drdyeStatus;
00232     bool gsel1Status;
00233     bool gsel0Status;
00234     bool tdteStatus;
00235     bool tpeStatus;
00236 
00237     // ODCNTL vars
00238     bool iirBypass;
00239     bool lpro;
00240     bool fstup;
00241     bool osa3;
00242     bool osa2;
00243     bool osa1;
00244     bool osa0;
00245 
00246     bool registerWritingEnabled;
00247 };
00248 
00249 #endif // KX134_H