ISSDK  1.7
IoT Sensing Software Development Kit
driver_MMA845X.c
Go to the documentation of this file.
1 /*
2  * The Clear BSD License
3  * Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
4  * Copyright 2016-2017 NXP
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without modification,
8  * are permitted (subject to the limitations in the disclaimer below) provided
9  * that the following conditions are met:
10  *
11  * o Redistributions of source code must retain the above copyright notice, this list
12  * of conditions and the following disclaimer.
13  *
14  * o Redistributions in binary form must reproduce the above copyright notice, this
15  * list of conditions and the following disclaimer in the documentation and/or
16  * other materials provided with the distribution.
17  *
18  * o Neither the name of the copyright holder nor the names of its
19  * contributors may be used to endorse or promote products derived from this
20  * software without specific prior written permission.
21  *
22  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
27  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /*! \file driver_MMA845x.c
36  \brief Provides init() and read() functions for the MMA845x 3-axis accel family.
37 
38  Supports MMA8451Q, MMA8452Q and MMA8453Q. Key differences which are applicable
39  to this driver are shown in the table below.
40 
41  Feature | MMA8451Q | MMA8452Q | MMA8453Q
42  --------|----------|----------|---------
43  # Bits | 14 | 12 | 10
44  FIFO | 32-deep | NONE | NONE
45 
46  All three have the MSB of the result registers located in the same location,
47  so if we read as one 16-bit value, the only thing that should change g/count
48  will be which range (+/- 2/4/8g) we are on.
49 */
50 
51 #include "board.h" // generated by Kinetis Expert. Long term - merge sensor_board.h into this file
52 #include "sensor_fusion.h" // Sensor fusion structures and types
53 #include "sensor_drv.h"
54 #include "sensor_io_i2c.h" // Required for registerreadlist_t / registerwritelist_t declarations
55 #include "MMA845x.h" // describes the MMA845x register definition and its bit mask
56 #include "drivers.h" // Device specific drivers supplied by NXP (can be replaced with user drivers)
57 #define MMA845x_COUNTSPERG 8192.0
58 #define MMA8451_ACCEL_FIFO_SIZE 32
59 
60 #if F_USING_ACCEL
61 
62 // Command definition to read the WHO_AM_I value.
63 const registerreadlist_t MMA845x_WHO_AM_I_READ[] =
64 {
65  { .readFrom = MMA845x_WHO_AM_I, .numBytes = 1 }, __END_READ_DATA__
66 };
67 
68 // Command definition to read the number of entries in the accel FIFO.
69 const registerreadlist_t MMA845x_F_STATUS_READ[] =
70 {
71  { .readFrom = MMA845x_STATUS, .numBytes = 1 }, __END_READ_DATA__
72 };
73 
74 // Command definition to read the number of entries in the accel FIFO.
75 registerreadlist_t MMA845x_DATA_READ[] =
76 {
77  { .readFrom = MMA845x_OUT_X_MSB, .numBytes = 6 }, __END_READ_DATA__
78 };
79 
80 // Each entry in a RegisterWriteList is composed of: register address, value to write, bit-mask to apply to write (0 enables)
81 const registerwritelist_t MMA8451_Initialization[] =
82 {
83  // OK: write 0000 0000 = 0x00 to CTRL_REG1 to place MMA845x into standby
84  // [7-1] = 0000 000
85  // [0]: active=0
86  { MMA845x_CTRL_REG1, 0x00, 0x00 },
87 
88  // Not applicable to the 8452/3.
89  // OK: write 0100 0000 = 0x40 to F_SETUP to enable FIFO in continuous (circular) mode
90  // [7-6]: F_MODE[1-0]=01 for FIFO continuous mode
91  // [5-0]: F_WMRK[5-0]=000000 for no FIFO watermark
92  { MMA845x_F_SETUP, 0x40, 0x00 },
93 
94  // OK: write 0000 0001 = 0x01 to XYZ_DATA_CFG register to set g range
95  // [7-5]: reserved=000
96  // [4]: HPF_OUT=0
97  // [3-2]: reserved=00
98  // [1-0]: FS=01 for +/-4g: 512 counts / g = 8192 counts / g after 4 bit left shift
99  { MMA845x_XYZ_DATA_CFG, 0x01, 0x00 },
100 
101  // OK: write 0000 0010 = 0x02 to CTRL_REG2 to set MODS bits
102  // [7]: ST=0: self test disabled
103  // [6]: RST=0: reset disabled
104  // [5]: unused
105  // [4-3]: SMODS=00
106  // [2]: SLPE=0: auto sleep disabled
107  // [1-0]: mods=10 for high resolution (maximum over sampling)
108  { MMA845x_CTRL_REG2, 0x02, 0x00 },
109 
110  // write 00XX X001 to CTRL_REG1 to set data rate and exit standby
111  // [7-6]: aslp_rate=00
112  // [5-3]: dr=111 for 1.56Hz data rate giving 0x39
113  // [5-3]: dr=110 for 6.25Hz data rate giving 0x31
114  // [5-3]: dr=101 for 12.5Hz data rate giving 0x29
115  // [5-3]: dr=100 for 50Hz data rate giving 0x21
116  // [5-3]: dr=011 for 100Hz data rate giving 0x19
117  // [5-3]: dr=010 for 200Hz data rate giving 0x11
118  // [5-3]: dr=001 for 400Hz data rate giving 0x09
119  // [5-3]: dr=000 for 800Hz data rate giving 0x01
120  // [2]: unused=0
121  // [1]: f_read=0 for normal 16 bit reads
122  // [0]: active=1 to take the part out of standby and enable sampling
123 #if (ACCEL_ODR_HZ <= 1)
124  { MMA845x_CTRL_REG1, 0x39, 0x00 }, // select 1.5625Hz ODR,
125 #elif (ACCEL_ODR_HZ <= 6)
126  { MMA845x_CTRL_REG1, 0x31, 0x00 }, // select 6.25Hz ODR
127 #elif (ACCEL_ODR_HZ <= 12)
128  { MMA845x_CTRL_REG1, 0x29, 0x00 }, // select 12.5Hz ODR
129 #elif (ACCEL_ODR_HZ <= 50)
130  { MMA845x_CTRL_REG1, 0x21, 0x00 }, // select 50Hz ODR
131 #elif (ACCEL_ODR_HZ <= 100)
132  { MMA845x_CTRL_REG1, 0x19, 0x00 }, // select 100Hz ODR
133 #elif (ACCEL_ODR_HZ <= 200)
134  { MMA845x_CTRL_REG1, 0x11, 0x00 }, // select 200Hz ODR
135 #elif (ACCEL_ODR_HZ <= 400)
136  { MMA845x_CTRL_REG1, 0x09, 0x00 }, // select 400Hz ODR
137 #else
138  { MMA845x_CTRL_REG1, 0x01, 0x00 }, // select 800Hz ODR
139 #endif
141 };
142 
143 // MMA845x_Initialization is exactly the same as the above, but without
144 // the FIFO initialization.
145 const registerwritelist_t MMA845x_Initialization[] =
146 {
147  { MMA845x_CTRL_REG1, 0x00, 0x00 },
148  { MMA845x_XYZ_DATA_CFG, 0x01, 0x00 },
149  { MMA845x_CTRL_REG2, 0x02, 0x00 },
150 #if (ACCEL_ODR_HZ <= 1)
151  { MMA845x_CTRL_REG1, 0x39, 0x00 }, // select 1.5625Hz ODR,
152 #elif (ACCEL_ODR_HZ <= 6)
153  { MMA845x_CTRL_REG1, 0x31, 0x00 }, // select 6.25Hz ODR
154 #elif (ACCEL_ODR_HZ <= 12)
155  { MMA845x_CTRL_REG1, 0x29, 0x00 }, // select 12.5Hz ODR
156 #elif (ACCEL_ODR_HZ <= 50)
157  { MMA845x_CTRL_REG1, 0x21, 0x00 }, // select 50Hz ODR
158 #elif (ACCEL_ODR_HZ <= 100)
159  { MMA845x_CTRL_REG1, 0x19, 0x00 }, // select 100Hz ODR
160 #elif (ACCEL_ODR_HZ <= 200)
161  { MMA845x_CTRL_REG1, 0x11, 0x00 }, // select 200Hz ODR
162 #elif (ACCEL_ODR_HZ <= 400)
163  { MMA845x_CTRL_REG1, 0x09, 0x00 }, // select 400Hz ODR
164 #else
165  { MMA845x_CTRL_REG1, 0x01, 0x00 }, // select 800Hz ODR
166 #endif
168 };
169 
170 // All sensor drivers and initialization functions have the same prototype.
171 // sfg is a pointer to the master "global" sensor fusion structure.
172 // sensor = pointer to linked list element used by the sensor fusion subsystem to specify required sensors
173 
174 // sfg = pointer to top level (generally global) data structure for sensor fusion
175 int8_t MMA845x_Init(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
176 {
177  int32_t status;
178  uint8_t reg;
179  status = Register_I2C_Read(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, MMA845x_WHO_AM_I, 1, &reg);
180  if (status==SENSOR_ERROR_NONE) {
181  sfg->Accel.iWhoAmI = reg;
182  switch (reg) {
185  case MMA8453_WHO_AM_I_WHOAMI_VALUE: break;
186  default: return(SENSOR_ERROR_INIT);
187  }
188  } else {
189  return(status);
190  }
191 
192  // Configure and start the MMA845x sensor. This does multiple register writes
193  // (see MMA845x_Initialization definition above)
195  status = Sensor_I2C_Write(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, MMA8451_Initialization );
196  } else {
197  status = Sensor_I2C_Write(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, MMA845x_Initialization );
198  }
199 
200  // Stash some needed constants in the SF data structure for this sensor
201  sfg->Accel.iCountsPerg = MMA845x_COUNTSPERG;
202  sfg->Accel.fgPerCount = 1.0F / MMA845x_COUNTSPERG;
203  sfg->Accel.fgPerCount = 1.0F / MMA845x_COUNTSPERG;
204 
205  sensor->isInitialized = F_USING_ACCEL;
206  sfg->Accel.isEnabled = true;
207 
208  return (status);
209 }
210 
211 int8_t MMA845x_Read(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
212 {
213  uint8_t I2C_Buffer[6 * MMA8451_ACCEL_FIFO_SIZE]; // I2C read buffer
214  int8_t status; // I2C transaction status
215  int8_t j; // scratch
216  uint8_t sensor_fifo_count = 1;
217  int16_t sample[3];
218 
219  if(sensor->isInitialized != F_USING_ACCEL)
220  {
221  return SENSOR_ERROR_INIT;
222  }
223 
224  // read the F_STATUS register (mapped to STATUS) and extract number of measurements available (lower 6 bits)
225  status = Sensor_I2C_Read(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, MMA845x_F_STATUS_READ, I2C_Buffer );
226  if (status==SENSOR_ERROR_NONE) {
227  if (sfg->Accel.iWhoAmI==MMA8451_WHO_AM_I_WHOAMI_VALUE) {
228  sensor_fifo_count = I2C_Buffer[0] & 0x3F;
229  } else {
230  sensor_fifo_count = (I2C_Buffer[0] & 0x08)>>3;
231  }
232  // return if there are no measurements in the sensor FIFO.
233  // this will only occur when the FAST_LOOP_HZ equals or exceeds ACCEL_ODR_HZ
234  if (sensor_fifo_count == 0) return SENSOR_ERROR_READ;
235  } else {
236  return(status);
237  }
238 
239  MMA845x_DATA_READ[0].readFrom = MMA845x_OUT_X_MSB;
240  MMA845x_DATA_READ[0].numBytes = 6 * sensor_fifo_count;
241  status = Sensor_I2C_Read(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, MMA845x_DATA_READ, I2C_Buffer );
242 
243  if (status==SENSOR_ERROR_NONE) {
244  for (j = 0; j < sensor_fifo_count; j++)
245  {
246  sample[CHX] = (I2C_Buffer[6 * j ] << 8) | I2C_Buffer[6 * j + 1];
247  sample[CHY] = (I2C_Buffer[6 * j + 2] << 8) | I2C_Buffer[6 * j + 3];
248  sample[CHZ] = (I2C_Buffer[6 * j + 4] << 8) | I2C_Buffer[6 * j + 5];
249  conditionSample(sample); // truncate negative values to -32767
250  addToFifo((union FifoSensor*) &(sfg->Accel), ACCEL_FIFO_SIZE, sample);
251  }
252  }
253 
254  return (status);
255 }
256 
257 
258 // Each entry in a RegisterWriteList is composed of: register address, value to write, bit-mask to apply to write (0 enables)
259 const registerwritelist_t MMA845x_IDLE[] =
260 {
261  // Set ACTIVE = other bits unchanged
262  { MMA845x_CTRL_REG1, 0x00, 0x01 },
264 };
265 
266 // MMA845x_Idle places the sensor into Standby mode (wakeup time = 2/ODR+1ms)
267 int8_t MMA845x_Idle(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
268 {
269  int32_t status;
270  if(sensor->isInitialized == F_USING_ACCEL) {
271  status = Sensor_I2C_Write(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, MMA845x_IDLE );
272  sensor->isInitialized = 0;
273  sfg->Accel.isEnabled = false;
274  } else {
275  return SENSOR_ERROR_INIT;
276  }
277  return status;
278 }
279 #endif // F_USING_ACCEL
The sensor_drv.h file contains sensor state and error definitions.
#define __END_WRITE_DATA__
Definition: sensor_drv.h:71
void * bus_driver
should be of type (ARM_DRIVER_I2C* for I2C-based sensors, ARM_DRIVER_SPI* for SPI) ...
#define CHY
Used to access Y-channel entries in various data data structures.
Definition: sensor_fusion.h:87
SensorFusionGlobals sfg
int32_t status
int32_t Sensor_I2C_Read(ARM_DRIVER_I2C *pCommDrv, registerDeviceInfo_t *devInfo, uint16_t slaveAddress, const registerreadlist_t *pReadList, uint8_t *pOutBuffer)
Read register data from a sensor.
#define ACCEL_FIFO_SIZE
FXOS8700 (accel), MMA8652, FXLS8952 all have 32 element FIFO.
#define __END_READ_DATA__
Definition: sensor_drv.h:77
The top level fusion structure.
#define MMA8452_WHO_AM_I_WHOAMI_VALUE
Definition: mma845x.h:509
uint16_t addr
I2C address if applicable.
void addToFifo(union FifoSensor *sensor, uint16_t maxFifoSize, int16_t sample[3])
addToFifo is called from within sensor driver read functions
The sensor_fusion.h file implements the top level programming interface.
The FifoSensor union allows us to use common pointers for Accel, Mag & Gyro logical sensor structures...
#define CHZ
Used to access Z-channel entries in various data data structures.
Definition: sensor_fusion.h:88
uint16_t isInitialized
Bitfields to indicate sensor is active (use SensorBitFields from build.h)
registerDeviceInfo_t deviceInfo
I2C device context.
Provides function prototypes for driver level interfaces.
#define CHX
Used to access X-channel entries in various data data structures.
Definition: sensor_fusion.h:86
void conditionSample(int16_t sample[3])
conditionSample ensures that we never encounter the maximum negative two&#39;s complement value for a 16-...
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
The mma845x.h contains the MMA845x sensor register definitions and its bit mask.
#define MMA845x_COUNTSPERG
int32_t Register_I2C_Read(ARM_DRIVER_I2C *pCommDrv, registerDeviceInfo_t *devInfo, uint16_t slaveAddress, uint8_t offset, uint8_t length, uint8_t *pOutBuffer)
The interface function to read a sensor register.
#define MMA8453_WHO_AM_I_WHOAMI_VALUE
Definition: mma845x.h:510
This structure defines the Write command List.
Definition: sensor_drv.h:94
This structure defines the Read command List.
Definition: sensor_drv.h:104
#define F_USING_ACCEL
nominally 0x0001 if an accelerometer is to be used, 0x0000 otherwise
#define MMA8451_ACCEL_FIFO_SIZE
An instance of PhysicalSensor structure type should be allocated for each physical sensors (combo dev...
The sensor_io_i2c.h file declares low-level interface functions for reading and writing sensor regist...
#define MMA8451_WHO_AM_I_WHOAMI_VALUE
Definition: mma845x.h:508
int32_t Sensor_I2C_Write(ARM_DRIVER_I2C *pCommDrv, registerDeviceInfo_t *devInfo, uint16_t slaveAddress, const registerwritelist_t *pRegWriteList)
Write register data to a sensor.
Definition: sensor_io_i2c.c:97