ISSDK  1.7
IoT Sensing Software Development Kit
mpl3115_altitude.c
Go to the documentation of this file.
1 /*
2  * The Clear BSD License
3  * Copyright (c) 2015-2016, Freescale Semiconductor, Inc.
4  * Copyright 2016-2017 NXP
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without modification,
8  * are permitted (subject to the limitations in the disclaimer below) provided
9  * that the following conditions are met:
10  *
11  * o Redistributions of source code must retain the above copyright notice, this list
12  * of conditions and the following disclaimer.
13  *
14  * o Redistributions in binary form must reproduce the above copyright notice, this
15  * list of conditions and the following disclaimer in the documentation and/or
16  * other materials provided with the distribution.
17  *
18  * o Neither the name of the copyright holder nor the names of its
19  * contributors may be used to endorse or promote products derived from this
20  * software without specific prior written permission.
21  *
22  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
27  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /**
36  * @file mpl3115_altitude.c
37  * @brief The mpl3115_altitude.c file implements the ISSDK MPL3115 sensor driver
38  * example demonstration for Altitude mode.
39  */
40 
41 //-----------------------------------------------------------------------
42 // SDK Includes
43 //-----------------------------------------------------------------------
44 #include "board.h"
45 #include "pin_mux.h"
46 #include "clock_config.h"
47 #include "fsl_debug_console.h"
48 
49 //-----------------------------------------------------------------------
50 // CMSIS Includes
51 //-----------------------------------------------------------------------
52 #include "Driver_I2C.h"
53 
54 /* ISSDK Includes */
55 #include "issdk_hal.h"
56 #include "mpl3115_drv.h"
57 
58 //-----------------------------------------------------------------------
59 // Macros
60 //-----------------------------------------------------------------------
61 #define MPL3115_DATA_SIZE (5) /* 3 byte Pressure/Altitude and 2 byte Temperature. */
62 /*! In MPL3115 the Auto Acquisition Time Step (ODR) can be set only in powers of 2 (i.e. 2^x, where x is the
63  * SAMPLING_EXPONENT).
64  * This gives a range of 1 second to 2^15 seconds (9 hours). */
65 #define MPL3115_SAMPLING_EXPONENT (1) /* 2 seconds */
66 
67 //-----------------------------------------------------------------------
68 // Constants
69 //-----------------------------------------------------------------------
70 /*! @brief Register settings for Altitude readings in Normal (non buffered) mode. */
72  /* Enable Data Ready and Event flags for Altitude, Temperature or either. */
76  /* Enable Altitude output and set Over Sampling Ratio to 128. */
79  /* Set Auto acquisition time step. */
82 
83 /*! @brief Address of Status Register. */
85 
86 /*! @brief Address and size of Raw Altitude+Temperature Data in Normal Mode. */
89 
90 //-----------------------------------------------------------------------
91 // Functions
92 //-----------------------------------------------------------------------
93 /*!
94  * @brief Main function
95  */
96 int main(void)
97 {
98  int16_t tempInDegrees;
99  int32_t altitudeInMeters;
100  int32_t status;
101  uint8_t dataReady;
102  uint8_t data[MPL3115_DATA_SIZE];
104 
105  ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER; // Now using the shield.h value!!!
106  mpl3115_i2c_sensorhandle_t mpl3115Driver;
107 
108  BOARD_InitPins();
111 
112  PRINTF("\r\n ISSDK MPL3115 sensor driver example demonstration with poll mode\r\n");
113 
114  /*! Initialize the I2C driver. */
115  status = I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
116  if (ARM_DRIVER_OK != status)
117  {
118  PRINTF("\r\n I2C Initialization Failed\r\n");
119  return -1;
120  }
121 
122  /*! Set the I2C Power mode. */
123  status = I2Cdrv->PowerControl(ARM_POWER_FULL);
124  if (ARM_DRIVER_OK != status)
125  {
126  PRINTF("\r\n I2C Power Mode setting Failed\r\n");
127  return -1;
128  }
129 
130  /*! Set the I2C bus speed. */
131  status = I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
132  if (ARM_DRIVER_OK != status)
133  {
134  PRINTF("\r\n I2C Control Mode setting Failed\r\n");
135  return -1;
136  }
137 
138  /*! Initialize MPL3115 sensor driver. */
141  if (SENSOR_ERROR_NONE != status)
142  {
143  PRINTF("\r\n Sensor Initialization Failed\r\n");
144  return -1;
145  }
146  PRINTF("\r\n Successfully Initiliazed Sensor\r\n");
147 
148  /*! Set the task to be executed while waiting for I2C transactions to complete. */
150 
151  /*! Configure the MPL3115 sensor. */
152  status = MPL3115_I2C_Configure(&mpl3115Driver, cMpl3115ConfigAltitude);
153  if (SENSOR_ERROR_NONE != status)
154  {
155  PRINTF("\r\nMPL3115 configuration failed...\r\n");
156  return -1;
157  }
158  PRINTF("\r\n Successfully Applied MPL3115 Sensor Configuration\r\n");
159 
160  for (;;) /* Forever loop */
161  {
162  /*! Wait for data ready from the MPL3115. */
163  status = MPL3115_I2C_ReadData(&mpl3115Driver, cMpl3115Status, &dataReady);
164  if (0 == (dataReady & MPL3115_DR_STATUS_PTDR_MASK))
165  { /* Loop, if new sample is not available. */
166  continue;
167  }
168 
169  /*! Read new raw sensor data from the MPL3115. */
170  status = MPL3115_I2C_ReadData(&mpl3115Driver, cMpl3115OutputNormal, data);
171  if (ARM_DRIVER_OK != status)
172  {
173  PRINTF("\r\n Read Failed. \r\n");
174  return -1;
175  }
176 
177  /*! Process the sample and convert the raw sensor data. */
178  rawData.altitude = (int32_t)((data[0]) << 24) | ((data[1]) << 16) | ((data[2]) << 8);
179  altitudeInMeters = rawData.altitude / MPL3115_ALTITUDE_CONV_FACTOR;
180 
181  rawData.temperature = (int16_t)((data[3]) << 8) | (data[4]);
182  tempInDegrees = rawData.temperature / MPL3115_TEMPERATURE_CONV_FACTOR;
183 
184  PRINTF("\r\nAltitude = %d Meters\r\n", altitudeInMeters);
185  PRINTF("\r\nTemperature = %d degC\r\n", tempInDegrees);
186  ASK_USER_TO_RESUME(8); /* Ask for user input after processing 8 samples. */
187  }
188 }
#define MPL3115_TEMPERATURE_CONV_FACTOR
Definition: mpl3115_drv.h:56
#define MPL3115_DR_STATUS_PTDR_MASK
Definition: mpl3115.h:195
#define __END_WRITE_DATA__
Definition: sensor_drv.h:71
int32_t MPL3115_I2C_Initialize(mpl3115_i2c_sensorhandle_t *pSensorHandle, ARM_DRIVER_I2C *pBus, uint8_t index, uint16_t sAddress, uint8_t whoAmi)
The interface function to initialize the sensor.
Definition: mpl3115_drv.c:48
#define MPL3115_ALTITUDE_CONV_FACTOR
Definition: mpl3115_drv.h:55
#define BOARD_BootClockRUN
Definition: clock_config.h:45
#define MPL3115_DATA_SIZE
uint8_t data[FXLS8962_DATA_SIZE]
int32_t MPL3115_I2C_ReadData(mpl3115_i2c_sensorhandle_t *pSensorHandle, const registerreadlist_t *pReadList, uint8_t *pBuffer)
The interface function to read the sensor data.
Definition: mpl3115_drv.c:130
#define MPL3115_CTRL_REG1_ALT_MASK
Definition: mpl3115.h:861
int32_t status
const registerwritelist_t cMpl3115ConfigAltitude[]
Register settings for Altitude readings in Normal (non buffered) mode.
const registerreadlist_t cMpl3115OutputNormal[]
Address and size of Raw Altitude+Temperature Data in Normal Mode.
void BOARD_InitDebugConsole(void)
Definition: board.c:41
#define MPL3115_SAMPLING_EXPONENT
#define MPL3115_PT_DATA_CFG_PDEFE_ENABLED
Definition: mpl3115.h:567
void(* registeridlefunction_t)(void *userParam)
This is the register idle function type.
Definition: sensor_drv.h:123
int main(void)
Main function.
#define MPL3115_CTRL_REG1_OS_OSR_128
Definition: mpl3115.h:884
#define __END_READ_DATA__
Definition: sensor_drv.h:77
void MPL3115_I2C_SetIdleTask(mpl3115_i2c_sensorhandle_t *pSensorHandle, registeridlefunction_t idleTask, void *userParam)
: The interface function to set the I2C Idle Task.
Definition: mpl3115_drv.c:79
#define MPL3115_PT_DATA_CFG_DREM_ENABLED
Definition: mpl3115.h:570
#define MPL3115_CTRL_REG1_OS_MASK
Definition: mpl3115.h:855
#define MPL3115_PT_DATA_CFG_PDEFE_MASK
Definition: mpl3115.h:553
const registerreadlist_t cMpl3115Status[]
Address of Status Register.
int32_t MPL3115_I2C_Configure(mpl3115_i2c_sensorhandle_t *pSensorHandle, const registerwritelist_t *pRegWriteList)
The interface function to configure he sensor.
Definition: mpl3115_drv.c:87
#define MPL3115_CTRL_REG1_ALT_ALT
Definition: mpl3115.h:888
#define MPL3115_PT_DATA_CFG_TDEFE_ENABLED
Definition: mpl3115.h:564
#define MPL3115_WHOAMI_VALUE
Definition: mpl3115.h:65
This defines the sensor specific information.
Definition: mpl3115_drv.h:62
#define ASK_USER_TO_RESUME(x)
Definition: frdm_k64f.h:132
This structure defines the mpl3115 data buffer in Altitude Mode.
Definition: mpl3115_drv.h:80
#define I2C_S_DEVICE_INDEX
Definition: issdk_hal.h:61
#define I2C_S_DRIVER
Definition: issdk_hal.h:59
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
The mpl3115_drv.h file describes the MPL3115 driver interface and structures.
#define MPL3115_I2C_ADDR
#define MPL3115_CTRL_REG2_ST_MASK
Definition: mpl3115.h:916
fxls8962_acceldataUser_t rawData
This structure defines the Write command List.
Definition: sensor_drv.h:94
This structure defines the Read command List.
Definition: sensor_drv.h:104
#define SMC
Definition: lpc54114.h:144
#define MPL3115_PT_DATA_CFG_DREM_MASK
Definition: mpl3115.h:556
status_t SMC_SetPowerModeWait(void *arg)
Configures the system to WAIT power mode. API name used from Kinetis family to maintain compatibility...
Definition: lpc54114.c:181
void BOARD_InitPins(void)
Configures pin routing and optionally pin electrical features.
Definition: pin_mux.c:73
#define MPL3115_PT_DATA_CFG_TDEFE_MASK
Definition: mpl3115.h:550
#define I2C_S_SIGNAL_EVENT
Definition: issdk_hal.h:60