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_MPL3115.c
10     \brief Provides init() and read() functions for the MPL3115 pressure sensor/altimeter.
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 "mpl3115_drv.h"                // Low level IS-SDK prototype driver
17 #include "drivers.h"                    // Device specific drivers supplied by NXP (can be replaced with user drivers)
18 #define MPL3115_MPERCOUNT 0.0000152587890625F		// Convert 32-bit Altitude value to meters
19 #define MPL3115_CPERCOUNT 0.00390625F			// Convert 16-bit Temperature value to Celcius
20 #define MPL3115_ACCEL_FIFO_SIZE 32
21 
22 #if F_USING_PRESSURE  // driver is only pulled in if the appropriate flag is set in build.h
23 
24 // Command definition to read the WHO_AM_I value.
25 const registerreadlist_t    MPL3115_WHO_AM_I_READ[] =
26 {
27     { .readFrom = MPL3115_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    MPL3115_F_STATUS_READ[] =
32 {
33     { .readFrom = MPL3115_STATUS, .numBytes = 1 }, __END_READ_DATA__
34 };
35 
36 // Command definition to read the number of entries in the accel FIFO.
37 registerreadlist_t          MPL3115_DATA_READ[] =
38 {
39     { .readFrom = MPL3115_OUT_P_MSB, .numBytes = 5 }, __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   MPL3115_Initialization[] =
44 {
45     // write 0000 0000 = 0x00 to MPL3115_CTRL_REG1 to place the MPL3115 in Standby
46     // [7]: ALT=0
47     // [6]: RAW=0
48     // [5-3]: OS=000
49     // [2]: RST=0
50     // [1]: OST=0
51     // [0]: SBYB=0 to enter standby
52     { MPL3115_CTRL_REG1, 0x00, 0x00 },
53 
54     // write 1011 1001 = 0xB9 to configure MPL3115 and enter Active mode in Auto Acquisition mode and 1Hz ODR
55     // [7]: ALT=1 for altitude measurements
56     // [6]: RAW=0 to disable raw measurements
57     // [5-3]: OS=111 for OS ratio=128
58     // [2]: RST=0 do not enter reset
59     // [1]: OST=0 do not initiate a reading
60     // [0]: SBYB=1 to enter active mode
61     { MPL3115_CTRL_REG1, 0xB9, 0x00 },
62      __END_WRITE_DATA__
63 };
64 
65 // All sensor drivers and initialization functions have the same prototype.
66 // sfg is a pointer to the master "global" sensor fusion structure.
67 // sensor = pointer to linked list element used by the sensor fusion subsystem to specify required sensors
MPL3115_Init(struct PhysicalSensor * sensor,SensorFusionGlobals * sfg)68 int8_t MPL3115_Init(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
69 {
70     int32_t status;
71     uint8_t reg;
72     status = Register_I2C_Read(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, MPL3115_WHO_AM_I, 1, &reg);
73     if (status==SENSOR_ERROR_NONE) {
74         sfg->Pressure.iWhoAmI = reg;
75         if (reg!=MPL3115_WHOAMI_VALUE) {
76              // return with error, whoAmI will retain default value of zero
77             return(SENSOR_ERROR_INIT);
78         }
79     } else {
80         // whoAmI will retain default value of zero
81         return(status);
82     }
83 
84     // Configure and start the MPL3115 sensor.  This does multiple register writes
85     // (see MPL3115_Initialization definition above)
86     status = Sensor_I2C_Write(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, MPL3115_Initialization );
87 
88     // Stash some needed constants in the SF data structure for this sensor
89     sfg->Pressure.fmPerCount = MPL3115_MPERCOUNT; // Convert 32-bit Altitude value to meters
90     sfg->Pressure.fCPerCount = MPL3115_CPERCOUNT; // Convert 16-bit Temperature value to Celcius
91 
92     sensor->isInitialized = F_USING_PRESSURE | F_USING_TEMPERATURE;
93     sfg->Pressure.isEnabled = true;
94 
95     return (status);
96 }
97 
MPL3115_Read(struct PhysicalSensor * sensor,SensorFusionGlobals * sfg)98 int8_t MPL3115_Read(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
99 {
100     uint8_t                     I2C_Buffer[6 * MPL3115_ACCEL_FIFO_SIZE];    // I2C read buffer
101     int8_t                      status;         // I2C transaction status
102 
103     if(sensor->isInitialized != (F_USING_PRESSURE | F_USING_TEMPERATURE))
104     {
105         return SENSOR_ERROR_INIT;
106     }
107 
108     status =  Sensor_I2C_Read(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, MPL3115_DATA_READ, I2C_Buffer );
109 
110     if (status==SENSOR_ERROR_NONE) {
111       	// place the read buffer into the 32 bit altitude and 16 bit temperature
112 	sfg->Pressure.iH = (I2C_Buffer[0] << 24) | (I2C_Buffer[1] << 16) | (I2C_Buffer[2] << 8);
113 	sfg->Pressure.iT = (I2C_Buffer[3] << 8) | I2C_Buffer[4];
114 
115 	// convert from counts to metres altitude and C
116 	sfg->Pressure.fH = (float) sfg->Pressure.iH * sfg->Pressure.fmPerCount;
117 	sfg->Pressure.fT = (float) sfg->Pressure.iT * sfg->Pressure.fCPerCount;
118     } else {
119 	sfg->Pressure.fH = 0.0;
120 	sfg->Pressure.fT = 0.0;
121     }
122 
123     return (status);
124 }
125 
126 
127 // Each entry in a RegisterWriteList is composed of: register address, value to write, bit-mask to apply to write (0 enables)
128 const registerwritelist_t   MPL3115_IDLE[] =
129 {
130   // Set ACTIVE = other bits unchanged
131   { MPL3115_CTRL_REG1, 0x00, 0x01 },
132     __END_WRITE_DATA__
133 };
134 
135 // MPL3115_Idle places the sensor into Standby mode (wakeup time = 1 second)
MPL3115_Idle(struct PhysicalSensor * sensor,SensorFusionGlobals * sfg)136 int8_t MPL3115_Idle(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
137 {
138     int32_t     status;
139     if(sensor->isInitialized == (F_USING_PRESSURE | F_USING_TEMPERATURE)) {
140         status = Sensor_I2C_Write(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, MPL3115_IDLE );
141         sensor->isInitialized = 0;
142         sfg->Pressure.isEnabled = false;
143     } else {
144       return SENSOR_ERROR_INIT;
145     }
146     return status;
147 }
148 #endif
149