1 //////////////////////////////////////////////////////////////////////////////////////
2 // The MIT License (MIT)
3 //
4 // Submit Date: 03/09/2015
5 // Author: Juan Jose Chong <juanjchong@gmail.com>
6 // Copyright (c) 2015 Juan Jose Chong
7 //
8 //////////////////////////////////////////////////////////////////////////////////////
9 // adis16448.h
10 //////////////////////////////////////////////////////////////////////////////////////
11 //
12 // This library runs on an Intel Edison and uses mraa to acquire data
13 // from an ADIS16448. This data is then scaled and printed onto the terminal.
14 //
15 // This software has been tested to connect to an ADIS16448 through a level shifter
16 // such as the TI TXB0104. The SPI lines (DIN, DOUT, SCLK, /CS) are all wired through
17 // the level shifter and the ADIS16448 is also being powered by the Intel Edison.
18 //
19 // Permission is hereby granted, free of charge, to any person obtaining
20 // a copy of this software and associated documentation files (the
21 // "Software"), to deal in the Software without restriction, including
22 // without limitation the rights to use, copy, modify, merge, publish,
23 // distribute, sublicense, and/or sell copies of the Software, and to
24 // permit persons to whom the Software is furnished to do so, subject to
25 // the following conditions:
26 //
27 // The above copyright notice and this permission notice shall be
28 // included in all copies or substantial portions of the Software.
29 //
30 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
31 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
32 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
33 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
34 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
35 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
36 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 //
38 //////////////////////////////////////////////////////////////////////////////////////
39 #include <string>
40 #include <mraa/spi.h>
41 #include <mraa/gpio.h>
42 
43 // User Register Memory Map from Table 6 of the Datasheet
44 #define FLASH_CNT 0x00 //Flash memory write count
45 #define XGYRO_OUT 0x04 //X-axis gyroscope output
46 #define YGYRO_OUT 0x06 //Y-axis gyroscope output
47 #define ZGYRO_OUT 0x08 //Z-axis gyroscope output
48 #define XACCL_OUT 0x0A //X-axis accelerometer output
49 #define YACCL_OUT 0x0C //Y-axis accelerometer output
50 #define ZACCL_OUT 0x0E //Z-axis accelerometer output
51 #define XMAGN_OUT 0X10 //X-axis magnetometer output
52 #define YMAGN_OUT 0x12 //Y-axis magnetometer output
53 #define ZMAGN_OUT 0x14 //Z-axis magnetometer output
54 #define BARO_OUT 0x16 //Barometer pressure measurement, high word
55 #define TEMP_OUT 0x18 //Temperature output
56 #define XGYRO_OFF 0x1A //X-axis gyroscope bias offset factor
57 #define YGYRO_OFF 0x1C //Y-axis gyroscope bias offset factor
58 #define ZGYRO_OFF 0x1E //Z-axis gyroscope bias offset factor
59 #define XACCL_OFF 0x20 //X-axis acceleration bias offset factor
60 #define YACCL_OFF 0x22 //Y-axis acceleration bias offset factor
61 #define ZACCL_OFF 0x24 //Z-axis acceleration bias offset factor
62 #define XMAGN_HIC 0x26 //X-axis magnetometer, hard iron factor
63 #define YMAGN_HIC 0x28 //Y-axis magnetometer, hard iron factor
64 #define ZMAGN_HIC 0x2A //Z-axis magnetometer, hard iron factor
65 #define XMAGN_SIC 0x2C //X-axis magnetometer, soft iron factor
66 #define YMAGN_SIC 0x2E //Y-axis magnetometer, soft iron factor
67 #define ZMAGN_SIC 0x30 //Z-axis magnetometer, soft iron factor
68 #define GPIO_CTRL 0x32 //GPIO control
69 #define MSC_CTRL 0x34 //Misc. control
70 #define SMPL_PRD 0x36 //Sample clock/Decimation filter control
71 #define SENS_AVG 0x38 //Digital filter control
72 #define SEQ_CNT 0x3A //xMAGN_OUT and BARO_OUT counter
73 #define DIAG_STAT 0x3C //System status
74 #define GLOB_CMD 0x3E //System command
75 #define ALM_MAG1 0x40 //Alarm 1 amplitude threshold
76 #define ALM_MAG2 0x42 //Alarm 2 amplitude threshold
77 #define ALM_SMPL1 0x44 //Alarm 1 sample size
78 #define ALM_SMPL2 0x46 //Alarm 2 sample size
79 #define ALM_CTRL 0x48 //Alarm control
80 #define LOT_ID1 0x52 //Lot identification number
81 #define LOT_ID2 0x54 //Lot identification number
82 #define PROD_ID 0x56 //Product identifier
83 #define SERIAL_NUM 0x58 //Lot-specific serial number
84 
85 namespace upm {
86  /**
87   * @brief ADIS16448 Accelerometer library
88   * @defgroup adis16448 libupm-adis16448
89   * @ingroup generic spi accelerometer
90   */
91 
92  /**
93   * @library adis16448
94   * @sensor adis16448
95   * @comname ADIS16448 Accelerometer
96   * @type accelerometer
97   * @man generic
98   * @web http://www.analog.com/en/products/sensors/isensor-mems-inertial-measurement-units/adis16448.html
99   * @con spi
100   *
101   * @brief API for the Analog Devices ADIS16448 Accelerometer
102   *
103   * This is an industrial-grade accelerometer by Analog Devices.
104   *
105   * @snippet adis16448.cxx Interesting
106   */
107     class ADIS16448{
108 
109         public:
110 
111         /**
112          * Constructor with configurable HW Reset
113          */
114         ADIS16448(int bus, int rst);
115 
116         /**
117          * Destructor
118          */
119         ~ADIS16448();
120 
121         /**
122          * Performs hardware reset by sending the specified pin low for 2 seconds
123          */
124         void resetDUT();
125 
126         /**
127          * Sets SPI frequency, mode, and bits/word
128          */
129         void configSPI();
130 
131         /**
132          * Reads a specified register and returns data
133          */
134         int16_t regRead(uint8_t regAddr);
135 
136         /**
137          * Writes to a specified register
138          */
139         void regWrite(uint8_t regAddr, uint16_t regData);
140 
141         /**
142          * Scales accelerometer data
143          */
144         float accelScale(int16_t sensorData);
145 
146         /**
147          * Scales gyro data
148          */
149         float gyroScale(int16_t sensorData);
150 
151         /**
152          * Scales temperature data
153          */
154         float tempScale(int16_t sensorData);
155 
156         /**
157          * Scales pressure data
158          */
159         float pressureScale(int16_t sensorData);
160 
161         /**
162          * Scales magnetometer data
163          */
164         float magnetometerScale(int16_t sensorData);
165 
166         private:
167 
168         mraa_spi_context _spi;
169         mraa_gpio_context _rst;
170 
171     };
172 }
173 
174