ISSDK  1.8
IoT Sensing Software Development Kit
driver_MPL3115.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_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 },
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
68 int8_t MPL3115_Init(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
69 {
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 
93  sfg->Pressure.isEnabled = true;
94 
95  return (status);
96 }
97 
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 
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 },
133 };
134 
135 // MPL3115_Idle places the sensor into Standby mode (wakeup time = 1 second)
136 int8_t MPL3115_Idle(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
137 {
138  int32_t status;
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
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
int8_t MPL3115_Read(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
#define MPL3115_ACCEL_FIFO_SIZE
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...
The mpl3115_drv.h file describes the MPL3115 driver interface and structures.
#define __END_WRITE_DATA__
Definition: sensor_drv.h:45
The top level fusion structure.
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
#define MPL3115_MPERCOUNT
#define F_USING_PRESSURE
nominally 0x0008 if altimeter is to be used, 0x0000 otherwise
#define F_USING_TEMPERATURE
nominally 0x0010 if temp sensor is to be used, 0x0000 otherwise
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.
int8_t MPL3115_Init(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
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
#define MPL3115_CPERCOUNT
uint16_t isInitialized
Bitfields to indicate sensor is active (use SensorBitFields from build.h)
#define MPL3115_WHOAMI_VALUE
Definition: mpl3115.h:65
int8_t MPL3115_Idle(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
This structure defines the Read command List.
Definition: sensor_drv.h:78
uint16_t addr
I2C address if applicable.
The sensor_io_i2c.h file declares low-level interface functions for reading and writing sensor regist...