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