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_MMA8652.c
10 \brief Provides init() and read() functions for the MMA8652 3-axis accel family.
11
12 1G*/
13
14 #include "board.h" // generated by Kinetis Expert. Long term - merge sensor_board.h into this file
15 #include "sensor_fusion.h" // Sensor fusion structures and types
16 #include "sensor_drv.h"
17 #include "sensor_io_i2c.h" // Required for registerreadlist_t / registerwritelist_t declarations
18 #include "mma865x.h"
19 #include "drivers.h" // Device specific drivers supplied by NXP (can be replaced with user drivers)
20 #define MMA8652_COUNTSPERG 8192.0
21 #define MMA8652_ACCEL_FIFO_SIZE 32
22
23 #if F_USING_ACCEL
24
25 // Command definition to read the WHO_AM_I value.
26 const registerreadlist_t MMA8652_WHO_AM_I_READ[] =
27 {
28 { .readFrom = MMA865x_WHO_AM_I, .numBytes = 1 }, __END_READ_DATA__
29 };
30
31 // Command definition to read the number of entries in the accel FIFO.
32 const registerreadlist_t MMA8652_F_STATUS_READ[] =
33 {
34 { .readFrom = MMA865x_STATUS, .numBytes = 1 }, __END_READ_DATA__
35 };
36
37 // Command definition to read the number of entries in the accel FIFO.
38 registerreadlist_t MMA8652_DATA_READ[] =
39 {
40 { .readFrom = MMA865x_OUT_X_MSB, .numBytes = 6 }, __END_READ_DATA__
41 };
42
43 // Each entry in a RegisterWriteList is composed of: register address, value to write, bit-mask to apply to write (0 enables)
44 const registerwritelist_t MMA8652_Initialization[] =
45 {
46 // write 0000 0000 = 0x00 to CTRL_REG1 to place MMA8652 into standby
47 // [7-1] = 0000 000
48 // [0]: active=0
49 { MMA865x_CTRL_REG1, 0x00, 0x00 },
50
51 // write 0000 0000 = 0x00 to CTRL_REG1 to place MMA8652 into standby
52 // [7-1] = 0000 000
53 // [0]: active=0
54 { MMA865x_F_SETUP, 0x40, 0x00 },
55
56 // OK: write 0000 0001 = 0x01 to XYZ_DATA_CFG register to set g range
57 // [7-5]: reserved=000
58 // [4]: HPF_OUT=0
59 // [3-2]: reserved=00
60 // [1-0]: FS=01 for +/-4g: 512 counts / g = 8192 counts / g after 4 bit left shift
61 { MMA865x_XYZ_DATA_CFG, 0x01, 0x00 },
62
63 // OK: write 0000 0010 = 0x02 to CTRL_REG2 to set MODS bits
64 // [7]: ST=0: self test disabled
65 // [6]: RST=0: reset disabled
66 // [5]: unused
67 // [4-3]: SMODS=00
68 // [2]: SLPE=0: auto sleep disabled
69 // [1-0]: mods=10 for high resolution (maximum over sampling)
70 { MMA865x_CTRL_REG2, 0x02, 0x00 },
71
72 // write 00XX X001 to CTRL_REG1 to set data rate and exit standby
73 // [7-6]: aslp_rate=00
74 // [5-3]: dr=111 for 1.56Hz data rate giving 0x39
75 // [5-3]: dr=110 for 6.25Hz data rate giving 0x31
76 // [5-3]: dr=101 for 12.5Hz data rate giving 0x29
77 // [5-3]: dr=100 for 50Hz data rate giving 0x21
78 // [5-3]: dr=011 for 100Hz data rate giving 0x19
79 // [5-3]: dr=010 for 200Hz data rate giving 0x11
80 // [5-3]: dr=001 for 400Hz data rate giving 0x09
81 // [5-3]: dr=000 for 800Hz data rate giving 0x01
82 // [2]: unused=0
83 // [1]: f_read=0 for normal 16 bit reads
84 // [0]: active=1 to take the part out of standby and enable sampling
85 #if (ACCEL_ODR_HZ <= 1)
86 { MMA865x_CTRL_REG1, 0x39, 0x00 }, // select 1.5625Hz ODR,
87 #elif (ACCEL_ODR_HZ <= 6)
88 { MMA865x_CTRL_REG1, 0x31, 0x00 }, // select 6.25Hz ODR
89 #elif (ACCEL_ODR_HZ <= 12)
90 { MMA865x_CTRL_REG1, 0x29, 0x00 }, // select 12.5Hz ODR
91 #elif (ACCEL_ODR_HZ <= 50)
92 { MMA865x_CTRL_REG1, 0x21, 0x00 }, // select 50Hz ODR
93 #elif (ACCEL_ODR_HZ <= 100)
94 { MMA865x_CTRL_REG1, 0x19, 0x00 }, // select 100Hz ODR
95 #elif (ACCEL_ODR_HZ <= 200)
96 { MMA865x_CTRL_REG1, 0x11, 0x00 }, // select 200Hz ODR
97 #elif (ACCEL_ODR_HZ <= 400)
98 { MMA865x_CTRL_REG1, 0x09, 0x00 }, // select 400Hz ODR
99 #else
100 { MMA865x_CTRL_REG1, 0x01, 0x00 }, // select 800Hz ODR
101 #endif
102 __END_WRITE_DATA__
103 };
104
105 // All sensor drivers and initialization functions have the same prototype.
106 // sfg is a pointer to the master "global" sensor fusion structure.
107 // sensor = pointer to linked list element used by the sensor fusion subsystem to specify required sensors
108
109 // sfg = pointer to top level (generally global) data structure for sensor fusion
MMA8652_Init(struct PhysicalSensor * sensor,SensorFusionGlobals * sfg)110 int8_t MMA8652_Init(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
111 {
112 int32_t status;
113 uint8_t reg;
114 status = Register_I2C_Read(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, MMA865x_WHO_AM_I, 1, ®);
115 if (status==SENSOR_ERROR_NONE) {
116 sfg->Accel.iWhoAmI = reg;
117 if (reg != MMA8652_WHOAMI_VALUE) return(SENSOR_ERROR_INIT);
118 } else {
119 return(status);
120 }
121
122 status = Sensor_I2C_Write(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, MMA8652_Initialization );
123 // Stash some needed constants in the SF data structure for this sensor
124 sfg->Accel.iCountsPerg = MMA8652_COUNTSPERG;
125 sfg->Accel.fgPerCount = 1.0F / MMA8652_COUNTSPERG;
126 sfg->Accel.fgPerCount = 1.0F / MMA8652_COUNTSPERG;
127
128 sensor->isInitialized = F_USING_ACCEL;
129 sfg->Accel.isEnabled = true;
130
131 return (status);
132 }
133
MMA8652_Read(struct PhysicalSensor * sensor,SensorFusionGlobals * sfg)134 int8_t MMA8652_Read(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
135 {
136 uint8_t I2C_Buffer[6 * MMA8652_ACCEL_FIFO_SIZE]; // I2C read buffer
137 int8_t status; // I2C transaction status
138 int8_t j; // scratch
139 uint8_t sensor_fifo_count = 1;
140 int16_t sample[3];
141
142 if(sensor->isInitialized != F_USING_ACCEL)
143 {
144 return SENSOR_ERROR_INIT;
145 }
146
147 // read the F_STATUS register (mapped to STATUS) and extract number of measurements available (lower 6 bits)
148 status = Sensor_I2C_Read(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, MMA8652_F_STATUS_READ, I2C_Buffer );
149 if (status==SENSOR_ERROR_NONE) {
150 sensor_fifo_count = I2C_Buffer[0] & 0x3F;
151 // return if there are no measurements in the sensor FIFO.
152 // this will only occur when the FAST_LOOP_HZ equals or exceeds ACCEL_ODR_HZ
153 if (sensor_fifo_count == 0) return SENSOR_ERROR_READ;
154 } else {
155 return(status);
156 }
157
158 MMA8652_DATA_READ[0].readFrom = MMA865x_OUT_X_MSB;
159 MMA8652_DATA_READ[0].numBytes = 6 * sensor_fifo_count;
160 status = Sensor_I2C_Read(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, MMA8652_DATA_READ, I2C_Buffer );
161
162 if (status==SENSOR_ERROR_NONE) {
163 for (j = 0; j < sensor_fifo_count; j++)
164 {
165 sample[CHX] = (I2C_Buffer[6 * j ] << 8) | I2C_Buffer[6 * j + 1];
166 sample[CHY] = (I2C_Buffer[6 * j + 2] << 8) | I2C_Buffer[6 * j + 3];
167 sample[CHZ] = (I2C_Buffer[6 * j + 4] << 8) | I2C_Buffer[6 * j + 5];
168 conditionSample(sample); // truncate negative values to -32767
169 addToFifo((union FifoSensor*) &(sfg->Accel), ACCEL_FIFO_SIZE, sample);
170 }
171 }
172
173 return (status);
174 }
175
176
177 // Each entry in a RegisterWriteList is composed of: register address, value to write, bit-mask to apply to write (0 enables)
178 const registerwritelist_t MMA8652_IDLE[] =
179 {
180 // Set ACTIVE = other bits unchanged
181 { MMA865x_CTRL_REG1, 0x00, 0x01 },
182 __END_WRITE_DATA__
183 };
184
185 // MMA8652_Idle places the sensor into SLEEP mode (wakeup time = 2/ODR+1ms)
MMA8652_Idle(struct PhysicalSensor * sensor,SensorFusionGlobals * sfg)186 int8_t MMA8652_Idle(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
187 {
188 int32_t status;
189 if(sensor->isInitialized == F_USING_ACCEL) {
190 status = Sensor_I2C_Write(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, MMA8652_IDLE );
191 sensor->isInitialized = 0;
192 sfg->Accel.isEnabled = false;
193 } else {
194 return SENSOR_ERROR_INIT;
195 }
196 return status;
197 }
198
199 #endif // F_USING_ACCEL
200