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_FXLS8962.c
10 \brief Provides init() and read() functions for the FXLS8962 3-axis accelerometer
11 */
12
13 #include "board.h" // generated by Kinetis Expert. Long term - merge sensor_board.h into this file
14 #include "sensor_fusion.h" // Sensor fusion structures and types
15 #include "sensor_io_i2c.h" // Required for registerreadlist_t / registerwritelist_t declarations
16 #include "sensor_drv.h" // Contains sensor state and error definitions
17 #include "fxls8962.h" // describes the FXLS8962 register definition and its bit mask
18 #include "drivers.h" // Device specific drivers supplied by NXP (can be replaced with user drivers)
19 #define FXLS8962_COUNTSPERG 512
20 #define FXLS8962_ACCEL_FIFO_SIZE 32
21
22 #if F_USING_ACCEL
23
24 // Command definition to read the WHO_AM_I value.
25 const registerreadlist_t FXLS8962_WHO_AM_I_READ[] =
26 {
27 { .readFrom = FXLS8962_WHO_AM_I, .numBytes = 1 }, __END_READ_DATA__
28 };
29
30 // Command definition to read the number of entries in the accel FIFO.
31 const registerreadlist_t FXLS8962_INT_STATUS_READ[] =
32 {
33 { .readFrom = FXLS8962_INT_STATUS, .numBytes = 1 }, __END_READ_DATA__
34 };
35
36 // Command definition to read the number of entries in the accel FIFO.
37 registerreadlist_t FXLS8962_DATA_READ[] =
38 {
39 { .readFrom = FXLS8962_OUT_X_LSB, .numBytes = 6 }, __END_READ_DATA__
40 };
41
42 // Each entry in a RegisterWriteList is composed of: register address, value to write, bit-mask to apply to write (0 enables)
43 const registerwritelist_t FXLS8962_Initialization[] =
44 {
45 // write 0000 0000 = 0x00 to SENS_CONFIG1 to place FXLS8962 into standby
46 // [7-1]: Set when taking the part out of standby
47 // [0]: ACTIVE=0 for standby
48 { FXLS8962_SENS_CONFIG1, 0x00, 0x00 },
49
50 // write 0101 0000 = 0x50 to SENS_CONFIG2 register to set High Performance Mode
51 // [7-6]: WAKE_PM=01 for High Performance Mode
52 // [5-4]: SLEEP_PM=01 for High Performance Mode
53 // [3]: LE_BE=0 for Little Endian
54 // [2]: BLOCK=0 for normal latching
55 // [1]: AINC_TEMP=0 to not include temperature in auto increment
56 // [0]: F_READ=0 for full register reads
57 { FXLS8962_SENS_CONFIG2, 0x50, 0x00 },
58
59 // write XXXX 0000 = to SENS_CONFIG3 register to set ODR
60 // [7-4]: WAKE_ODR=1100 for 0.781Hz ODR and 4096x decimation from 3200Hz giving 0xC0
61 // [7-4]: WAKE_ODR=1011 for 1.563Hz ODR and 12048x decimation from 3200Hz giving 0xB0
62 // [7-4]: WAKE_ODR=1010 for 3.125Hz ODR and 1024x decimation from 3200Hz giving 0xA0
63 // [7-4]: WAKE_ODR=1001 for 6.25Hz ODR and 512x decimation from 3200Hz giving 0x90
64 // [7-4]: WAKE_ODR=1000 for 12.5Hz ODR and 256x decimation from 3200Hz giving 0x80
65 // [7-4]: WAKE_ODR=0111 for 25Hz ODR and 128x decimation from 3200Hz giving 0x70
66 // [7-4]: WAKE_ODR=0110 for 50Hz ODR and 64x decimation from 3200Hz giving 0x60
67 // [7-4]: WAKE_ODR=0101 for 100Hz ODR and 32x decimation from 3200Hz giving 0x50
68 // [7-4]: WAKE_ODR=0100 for 200Hz ODR and 16x decimation from 3200Hz giving 0x40
69 // [7-4]: WAKE_ODR=0011 for 400Hz ODR and 8x decimation from 3200Hz giving 0x30
70 // [7-4]: WAKE_ODR=0010 for 800Hz ODR and 4x decimation from 3200Hz giving 0x20
71 // [7-4]: WAKE_ODR=0001 for 1600Hz ODR and 2x decimation from 3200Hz giving 0x10
72 // [7-4]: WAKE_ODR=0000 for 3200Hz ODR and 1x decimation from 3200Hz giving 0x00
73 // [3-0]: SLEEP_ODR=0000
74 #if (ACCEL_ODR_HZ <= 1) // select 0.781Hz ODR
75 { FXLS8962_SENS_CONFIG3, 0xC0, 0x00 },
76 #elif (ACCEL_ODR_HZ <= 2) // select 1.5625Hz ODR
77 { FXLS8962_SENS_CONFIG3, 0x80, 0x00 },
78 #elif (ACCEL_ODR_HZ <= 3) // select 3.125Hz ODR
79 { FXLS8962_SENS_CONFIG3, 0xA0, 0x00 },
80 #elif (ACCEL_ODR_HZ <= 6) // select 6.25Hz ODR
81 { FXLS8962_SENS_CONFIG3, 0x90, 0x00 },
82 #elif (ACCEL_ODR_HZ <= 12) // select 12.5Hz ODR
83 { FXLS8962_SENS_CONFIG3, 0x80, 0x00 },
84 #elif (ACCEL_ODR_HZ <= 25) // select 25Hz ODR
85 { FXLS8962_SENS_CONFIG3, 0x70, 0x00 },
86 #elif (ACCEL_ODR_HZ <= 50) // select 50Hz ODR
87 { FXLS8962_SENS_CONFIG3, 0x60, 0x00 },
88 #elif (ACCEL_ODR_HZ <= 100) // select 100Hz ODR
89 { FXLS8962_SENS_CONFIG3, 0x50, 0x00 },
90 #elif (ACCEL_ODR_HZ <= 200) // select 200Hz ODR
91 { FXLS8962_SENS_CONFIG3, 0x40, 0x00 },
92 #elif (ACCEL_ODR_HZ <= 400) // select 400Hz ODR
93 { FXLS8962_SENS_CONFIG3, 0x30, 0x00 },
94 #elif (ACCEL_ODR_HZ <= 800) // select 800Hz ODR
95 { FXLS8962_SENS_CONFIG3, 0x20, 0x00 },
96 #elif (ACCEL_ODR_HZ <= 1600) // select 1600Hz ODR
97 { FXLS8962_SENS_CONFIG3, 0x10, 0x00 },
98 #else // select 3200Hz ODR
99 { FXLS8962_SENS_CONFIG3, 0x00, 0x00 },
100 #endif
101
102 // write 0000 0011 = 0x03 to SENS_CONFIG1 to configure 4g mode and Active mode
103 // [7]: RST=0 for no software reset
104 // [6-5]: ST_AXIS_SEL=00 to disable self test
105 // [4]: ST_POL=0 (default)
106 // [3]: AWS_EN=0 (default)
107 // [2-1]: FSR=01 for 4g mode
108 // [1]: ACTIVE=1 for standby
109 { FXLS8962_SENS_CONFIG1, 0x03, 0x00 },
110 __END_WRITE_DATA__
111 };
112
113 // All sensor drivers and initialization functions have the same prototype.
114 // sfg is a pointer to the master "global" sensor fusion structure.
115 // sensor = pointer to linked list element used by the sensor fusion subsystem to specify required sensors
116
117 // sfg = pointer to top level (generally global) data structure for sensor fusion
FXLS8962_Init(struct PhysicalSensor * sensor,SensorFusionGlobals * sfg)118 int8_t FXLS8962_Init(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
119 {
120 int32_t status;
121 uint8_t reg;
122 status = Register_I2C_Read(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, FXLS8962_WHO_AM_I, 1, ®);
123 if (status==SENSOR_ERROR_NONE) {
124 if (reg==FXLS8962_WHOAMI_VALUE) {
125 sfg->Accel.iWhoAmI = reg;
126 } else {
127 return(SENSOR_ERROR_INIT);
128 }
129 } else {
130 return(status);
131 }
132
133 // Configure and start the FXLS8962 sensor. This does multiple register writes
134 // (see FXLS8962_Initialization definition above)
135 status = Sensor_I2C_Write(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, FXLS8962_Initialization );
136
137 // Stash some needed constants in the SF data structure for this sensor
138 sfg->Accel.iCountsPerg = FXLS8962_COUNTSPERG;
139 sfg->Accel.fgPerCount = 1.0F / FXLS8962_COUNTSPERG;
140 sfg->Accel.fgPerCount = 1.0F / FXLS8962_COUNTSPERG;
141
142 sensor->isInitialized = F_USING_ACCEL;
143 sfg->Accel.isEnabled = true;
144
145 return (status);
146 }
147
FXLS8962_Read(struct PhysicalSensor * sensor,SensorFusionGlobals * sfg)148 int8_t FXLS8962_Read(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
149 {
150 uint8_t I2C_Buffer[6 * FXLS8962_ACCEL_FIFO_SIZE]; // I2C read buffer
151 int8_t status; // I2C transaction status
152 uint8_t sensor_data_ready = 0;
153 int16_t sample[3];
154
155 if(sensor->isInitialized != F_USING_ACCEL)
156 {
157 return SENSOR_ERROR_INIT;
158 }
159
160 // read the FXLS8962_BUF_STATUS register (mapped to STATUS) and extract number of measurements available (lower 6 bits)
161 status = Sensor_I2C_Read(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, FXLS8962_INT_STATUS_READ, I2C_Buffer );
162 if (status==SENSOR_ERROR_NONE) {
163 sensor_data_ready = I2C_Buffer[0] & 0x80;
164 // return if there are no measurements in the sensor FIFO.
165 // this will only occur when the FAST_LOOP_HZ equals or exceeds ACCEL_ODR_HZ
166 if (sensor_data_ready == 0) return status;
167 } else {
168 return(status);
169 }
170
171 status = Sensor_I2C_Read(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, FXLS8962_DATA_READ, I2C_Buffer );
172
173 if (status==SENSOR_ERROR_NONE) {
174 // place the 6 bytes read into the 16 bit accelerometer structure
175 sample[CHX] = (I2C_Buffer[1] << 8) | I2C_Buffer[0];
176 sample[CHY] = (I2C_Buffer[3] << 8) | I2C_Buffer[2];
177 sample[CHZ] = (I2C_Buffer[5] << 8) | I2C_Buffer[4];
178 conditionSample(sample); // truncate negative values to -32767
179 addToFifo((union FifoSensor*) &(sfg->Accel), ACCEL_FIFO_SIZE, sample);
180 }
181
182 return (status);
183 }
184
185
186 // Each entry in a RegisterWriteList is composed of: register address, value to write, bit-mask to apply to write (0 enables)
187 const registerwritelist_t FXLS8962_IDLE[] =
188 {
189 // Set ACTIVE = other bits unchanged
190 { FXLS8962_SENS_CONFIG1, 0x00, 0x01 },
191 __END_WRITE_DATA__
192 };
193
194 // FXLS8962_Idle places the sensor into Standby mode (see datasheet for wakeup time)
FXLS8962_Idle(struct PhysicalSensor * sensor,SensorFusionGlobals * sfg)195 int8_t FXLS8962_Idle(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
196 {
197 int32_t status;
198 if(sensor->isInitialized == F_USING_ACCEL) {
199 status = Sensor_I2C_Write(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, FXLS8962_IDLE );
200 sensor->isInitialized = 0;
201 sfg->Accel.isEnabled = false;
202 } else {
203 return SENSOR_ERROR_INIT;
204 }
205 return status;
206 }
207 #endif // if F_USING_ACCEL
208