1 /*
2  * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
3  * Copyright (c) 2014 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 #pragma once
25 
26 #include <string>
27 #include <mraa/i2c.hpp>
28 
29 #define ADDR               0x1D // device address
30 
31 // Register names according to the datasheet.
32 // Register 0x1C is sometimes called 'PW', and sometimes 'PD'.
33 // The two reserved registers cannot be used.
34 #define MMA7455_XOUTL 0x00      // Read only, Output Value X LSB
35 #define MMA7455_XOUTH 0x01      // Read only, Output Value X MSB
36 #define MMA7455_YOUTL 0x02      // Read only, Output Value Y LSB
37 #define MMA7455_YOUTH 0x03      // Read only, Output Value Y MSB
38 #define MMA7455_ZOUTL 0x04      // Read only, Output Value Z LSB
39 #define MMA7455_ZOUTH 0x05      // Read only, Output Value Z MSB
40 #define MMA7455_XOUT8 0x06      // Read only, Output Value X 8 bits
41 #define MMA7455_YOUT8 0x07      // Read only, Output Value Y 8 bits
42 #define MMA7455_ZOUT8 0x08      // Read only, Output Value Z 8 bits
43 #define MMA7455_STATUS 0x09     // Read only, Status Register
44 #define MMA7455_DETSRC 0x0A     // Read only, Detection Source Register
45 #define MMA7455_TOUT 0x0B       // Temperature Output Value (Optional)
46 #define MMA7455_RESERVED1 0x0C  // Reserved
47 #define MMA7455_I2CAD 0x0D      // Read/Write, I2C Device Address
48 #define MMA7455_USRINF 0x0E     // Read only, User Information (Optional)
49 #define MMA7455_WHOAMI 0x0F     // Read only, "Who am I" value (Optional)
50 #define MMA7455_XOFFL 0x10      // Read/Write, Offset Drift X LSB
51 #define MMA7455_XOFFH 0x11      // Read/Write, Offset Drift X MSB
52 #define MMA7455_YOFFL 0x12      // Read/Write, Offset Drift Y LSB
53 #define MMA7455_YOFFH 0x13      // Read/Write, Offset Drift Y MSB
54 #define MMA7455_ZOFFL 0x14      // Read/Write, Offset Drift Z LSB
55 #define MMA7455_ZOFFH 0x15      // Read/Write, Offset Drift Z MSB
56 #define MMA7455_MCTL 0x16       // Read/Write, Mode Control Register
57 #define MMA7455_INTRST 0x17     // Read/Write, Interrupt Latch Reset
58 #define MMA7455_CTL1 0x18       // Read/Write, Control 1 Register
59 #define MMA7455_CTL2 0x19       // Read/Write, Control 2 Register
60 #define MMA7455_LDTH 0x1A       // Read/Write, Level Detection Threshold Limit Value
61 #define MMA7455_PDTH 0x1B       // Read/Write, Pulse Detection Threshold Limit Value
62 #define MMA7455_PD 0x1C         // Read/Write, Pulse Duration Value
63 #define MMA7455_LT 0x1D         // Read/Write, Latency Time Value (between pulses)
64 #define MMA7455_TW 0x1E         // Read/Write, Time Window for Second Pulse Value
65 #define MMA7455_RESERVED2 0x1F  // Reserved
66 
67 // Defines for the bits to be able to change
68 // between the bit number and the binary definition.
69 // By using the bit number, programming MMA7455
70 // is like programming an AVR microcontroller.
71 // But instead of using "(1<<X)", or "_BV(X)",
72 // the Arduino "bit(X)" is used.
73 #define MMA7455_D0 0
74 #define MMA7455_D1 1
75 #define MMA7455_D2 2
76 #define MMA7455_D3 3
77 #define MMA7455_D4 4
78 #define MMA7455_D5 5
79 #define MMA7455_D6 6
80 #define MMA7455_D7 7
81 
82 // Status Register
83 #define MMA7455_DRDY MMA7455_D0
84 #define MMA7455_DOVR MMA7455_D1
85 #define MMA7455_PERR MMA7455_D2
86 
87 // Mode Control Register
88 #define MMA7455_MODE0 MMA7455_D0
89 #define MMA7455_MODE1 MMA7455_D1
90 #define MMA7455_GLVL0 MMA7455_D2
91 #define MMA7455_GLVL1 MMA7455_D3
92 #define MMA7455_STON MMA7455_D4
93 #define MMA7455_SPI3W MMA7455_D5
94 #define MMA7455_DRPD MMA7455_D6
95 
96 // Control 1 Register
97 #define MMA7455_INTPIN MMA7455_D0
98 #define MMA7455_INTREG0 MMA7455_D1
99 #define MMA7455_INTREG1 MMA7455_D2
100 #define MMA7455_XDA MMA7455_D3
101 #define MMA7455_YDA MMA7455_D4
102 #define MMA7455_ZDA MMA7455_D5
103 #define MMA7455_THOPT MMA7455_D6
104 #define MMA7455_DFBW MMA7455_D7
105 
106 // Control 2 Register
107 #define MMA7455_LDPL MMA7455_D0
108 #define MMA7455_PDPL MMA7455_D1
109 #define MMA7455_DRVO MMA7455_D2
110 
111 // Interrupt Latch Reset Register
112 #define MMA7455_CLR_INT1 MMA7455_D0
113 #define MMA7455_CLR_INT2 MMA7455_D1
114 
115 // Detection Source Register
116 #define MMA7455_INT1 MMA7455_D0
117 #define MMA7455_INT2 MMA7455_D1
118 #define MMA7455_PDZ MMA7455_D2
119 #define MMA7455_PDY MMA7455_D3
120 #define MMA7455_PDX MMA7455_D4
121 #define MMA7455_LDZ MMA7455_D5
122 #define MMA7455_LDY MMA7455_D6
123 #define MMA7455_LDX MMA7455_D7
124 
125 // I2C Device Address Register
126 #define MMA7455_I2CDIS MMA7455_D7
127 
128 #define HIGH               1
129 #define LOW                0
130 
131 namespace upm {
132 
133 typedef union {
134     struct {
135         unsigned char x_lsb;
136         unsigned char x_msb;
137         unsigned char y_lsb;
138         unsigned char y_msb;
139         unsigned char z_lsb;
140         unsigned char z_msb;
141     } reg;
142 
143     struct {
144         short x;
145         short y;
146         short z;
147     } value;
148 } accelData;
149 
150 #define BIT(n) (1<<n)
151 
152 /**
153  * @brief MMA7455 Accelerometer library
154  * @defgroup mma7455 libupm-mma7455
155  * @ingroup generic i2c accelerometer
156  */
157 /**
158  * @library mma7455
159  * @sensor mma7455
160  * @comname MMA7455 Accelerometer Sensor
161  * @type accelerometer
162  * @man generic
163  * @con i2c
164  *
165  * @brief API for the MMA7455 Accelerometer
166  *
167  * This module defines the MMA7455 interface for libmma7455
168  *
169  * @image html mma7455.jpg
170  * @snippet mma7455.cxx Interesting
171  */
172 class MMA7455 {
173     public:
174         /**
175          * Instantiates an MMA7455 object
176          *
177          * @param bus Number of the used bus
178          * @param devAddr Address of the used I2C device
179          */
180         MMA7455 (int bus=0, int devAddr=0x1D);
181 
182         /**
183          * Returns the name of the component
184          *
185          * @return Name of the component
186          */
name()187         std::string name()
188         {
189             return m_name;
190         }
191 
192         /**
193          * Calibrates the sensor
194          *
195          * @return 0 (MRAA_SUCCESS) if successful; non-zero otherwise
196          */
197         mraa::Result calibrate ();
198 
199         /**
200          * Reads X-axis, Y-axis, and Z-axis acceleration data
201          *
202          * @param ptrX X-axis
203          * @param ptrY Y-axis
204          * @param ptrZ Z-axis
205          *
206          * @return 0 (MRAA_SUCCESS) if successful; non-zero otherwise
207          */
208         mraa::Result readData (short * ptrX, short * ptrY, short * ptrZ);
209 
210 #ifdef SWIGJAVA
211         /**
212          * Reads X-axis, Y-axis, and Z-axis acceleration data
213          *
214          * @return Array containing X, Y, Z acceleration data
215          */
216         short *readData ();
217 #endif
218         /**
219          * Internal function for reading I2C data
220          *
221          * @param reg Register address
222          * @param buffer Register data buffer
223          * @param len Buffer size
224          */
225         int i2cReadReg (unsigned char reg, uint8_t *buffer, int len);
226 
227         /**
228          * Internal function for writing I2C data
229          *
230          * @param reg Register address
231          * @param buffer Register data buffer
232          * @param len Buffer size
233          */
234         mraa::Result i2cWriteReg (unsigned char reg, uint8_t *buffer, int len);
235 
236     private:
237         std::string m_name;
238         int              m_controlAddr;
239         int              m_bus;
240         mraa::I2c  m_i2ControlCtx;
241 };
242 
243 }
244