ISSDK  1.8
IoT Sensing Software Development Kit
mpl3115_oneshot.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2016, Freescale Semiconductor, Inc.
3  * Copyright 2016-2017 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 /**
10  * @file mpl3115_oneshot.c
11  * @brief The mpl3115_oneshot.c file implements the ISSDK MPL3115 sensor driver
12  * example demonstration with one shot mode.
13  */
14 
15 //-----------------------------------------------------------------------
16 // SDK Includes
17 //-----------------------------------------------------------------------
18 #include "pin_mux.h"
19 #include "clock_config.h"
20 #include "board.h"
21 #include "fsl_debug_console.h"
22 
23 //-----------------------------------------------------------------------
24 // CMSIS Includes
25 //-----------------------------------------------------------------------
26 #include "Driver_I2C.h"
27 
28 /* ISSDK Includes */
29 #include "issdk_hal.h"
30 #include "mpl3115_drv.h"
31 
32 //-----------------------------------------------------------------------
33 // Macros
34 //-----------------------------------------------------------------------
35 #define MPL3115_DATA_SIZE (5) /* 3 byte Pressure/Altitude and 2 byte Temperature. */
36 
37 //-----------------------------------------------------------------------
38 // Constants
39 //-----------------------------------------------------------------------
40 /*! @brief Register settings for Altitude readings in One-Shot mode. */
42  /* Enable Altitude output. */
45 
46 /*! @brief Register settings for Triggring One-Shot Sampling. */
48  /* Set the One ShoT Bit. */
51 
52 /*! @brief Address of Register containing OST Bit. */
54 
55 /*! @brief Address and size of Raw Altitude+Temperature Data. */
58 
59 //-----------------------------------------------------------------------
60 // Functions
61 //-----------------------------------------------------------------------
62 /*!
63  * @brief Main function
64  */
65 int main(void)
66 {
67  int16_t tempInDegrees;
68  int32_t altitudeInMeters;
70  uint8_t dataReady;
71  uint8_t data[MPL3115_DATA_SIZE];
73 
74  ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER; // Now using the shield.h value!!!
75  mpl3115_i2c_sensorhandle_t mpl3115Driver;
76  registerDeviceInfo_t deviceInfo;
77 
81 
82  PRINTF("\r\n ISSDK MPL3115 sensor driver example demonstration with oneshot mode\r\n");
83 
84  /*! Initialize the I2C driver. */
85  status = I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
86  if (ARM_DRIVER_OK != status)
87  {
88  PRINTF("\r\n I2C Initialization Failed\r\n");
89  return -1;
90  }
91 
92  /*! Set the I2C Power mode. */
93  status = I2Cdrv->PowerControl(ARM_POWER_FULL);
94  if (ARM_DRIVER_OK != status)
95  {
96  PRINTF("\r\n I2C Power Mode setting Failed\r\n");
97  return -1;
98  }
99 
100  /*! Set the I2C bus speed. */
101  status = I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
102  if (ARM_DRIVER_OK != status)
103  {
104  PRINTF("\r\n I2C Control Mode setting Failed\r\n");
105  return -1;
106  }
107 
108  /*! Initialize MPL3115 sensor driver. */
111  if (SENSOR_ERROR_NONE != status)
112  {
113  PRINTF("\r\n Sensor Initialization Failed\r\n");
114  return -1;
115  }
116  PRINTF("\r\n Successfully Initiliazed Sensor\r\n");
117 
118  /*! We do not need to call MPL3115_I2C_Configure() in this case as we are going to read samples on demand.
119  * Instead we directly write register settings for One-Shot Mode... */
120  deviceInfo.deviceInstance = I2C_S_DEVICE_INDEX;
121  deviceInfo.functionParam = SMC;
123  status = Sensor_I2C_Write(mpl3115Driver.pCommDrv, &deviceInfo, mpl3115Driver.slaveAddress, cMpl3115ConfigAltitude);
124  if (ARM_DRIVER_OK != status)
125  {
126  PRINTF("\r\nWrite Failed.\r\n");
127  return -1;
128  }
129  PRINTF("\r\nMPL3115 will now trigger acquisition of one sample and then read the data...\r\n");
130 
131  /*! The code contained withing the braces illustrate steps to read one-sample.
132  * These can be repated at desired intervals to acquire more samples. */
133  { /* Trigger acquisition of One Sample. */
134  status = Sensor_I2C_Write(mpl3115Driver.pCommDrv, &deviceInfo, mpl3115Driver.slaveAddress, cMpl3115SetOST);
135  if (ARM_DRIVER_OK != status)
136  {
137  PRINTF("\r\nWrite Failed.\r\n");
138  return SENSOR_ERROR_WRITE;
139  }
140 
141  do /*! Keep checking the OST FLAG for completion. */
142  {
143  status = MPL3115_I2C_ReadData(&mpl3115Driver, cMpl3115GetOST, &dataReady);
144  if (ARM_DRIVER_OK != status)
145  {
146  PRINTF("\r\nRead Failed.\r\n");
147  return -1;
148  }
149  } /* Loop, untill sample acquisition is not completed. */
150  while (0 != (dataReady & MPL3115_CTRL_REG1_OST_MASK));
151 
152  /*! Read raw sensor data from the MPL3115. */
153  status = MPL3115_I2C_ReadData(&mpl3115Driver, cMpl3115OutputNormal, data);
154  if (ARM_DRIVER_OK != status)
155  {
156  PRINTF("\r\n Read Failed. \r\n");
157  return -1;
158  }
159 
160  /*! Process the sample and convert the raw sensor data. */
161  rawData.altitude = (int32_t)((data[0]) << 24) | ((data[1]) << 16) | ((data[2]) << 8);
162  rawData.temperature = (int16_t)((data[3]) << 8) | (data[4]);
163  altitudeInMeters = rawData.altitude / MPL3115_ALTITUDE_CONV_FACTOR;
164  tempInDegrees = rawData.temperature / MPL3115_TEMPERATURE_CONV_FACTOR;
165 
166  PRINTF("\r\nAltitude = %d Meters\r\n", altitudeInMeters);
167  PRINTF("\r\nTemperature = %d degC\r\n", tempInDegrees);
168  ASK_USER_TO_RESUME(1); /* Ask for user input after processing 8 samples. */
169  }
170 
171  return 0;
172 }
int main(void)
Main function.
This structure defines the device specific info required by register I/O.
Definition: sensor_drv.h:102
This structure defines the Write command List.
Definition: sensor_drv.h:68
#define ASK_USER_TO_RESUME(x)
Definition: frdm_k64f.h:106
int32_t status
This structure defines the mpl3115 data buffer in Altitude Mode.
Definition: mpl3115_drv.h:54
const registerwritelist_t cMpl3115ConfigAltitude[]
Register settings for Altitude readings in One-Shot mode.
const registerreadlist_t cMpl3115GetOST[]
Address of Register containing OST Bit.
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:104
The mpl3115_drv.h file describes the MPL3115 driver interface and structures.
#define SMC
Definition: lpc54114.h:118
#define __END_WRITE_DATA__
Definition: sensor_drv.h:45
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
#define I2C_S_SIGNAL_EVENT
Definition: issdk_hal.h:34
#define I2C_S_DRIVER
Definition: issdk_hal.h:33
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
#define BOARD_BootClockRUN
Definition: clock_config.h:19
#define MPL3115_TEMPERATURE_CONV_FACTOR
Definition: mpl3115_drv.h:30
void(* registeridlefunction_t)(void *userParam)
This is the register idle function type.
Definition: sensor_drv.h:97
const registerwritelist_t cMpl3115SetOST[]
Register settings for Triggring One-Shot Sampling.
const registerreadlist_t cMpl3115OutputNormal[]
Address and size of Raw Altitude+Temperature Data.
uint8_t data[FXLS8962_DATA_SIZE]
#define __END_READ_DATA__
Definition: sensor_drv.h:51
#define MPL3115_CTRL_REG1_OST_MASK
Definition: mpl3115.h:849
#define MPL3115_DATA_SIZE
This defines the sensor specific information.
Definition: mpl3115_drv.h:36
#define MPL3115_CTRL_REG1_ALT_MASK
Definition: mpl3115.h:861
ARM_DRIVER_I2C * I2Cdrv
fxos8700_accelmagdata_t rawData
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:22
uint16_t readFrom
Definition: sensor_drv.h:80
#define MPL3115_WHOAMI_VALUE
Definition: mpl3115.h:65
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:155
ARM_DRIVER_I2C * pCommDrv
Definition: mpl3115_drv.h:39
This structure defines the Read command List.
Definition: sensor_drv.h:78
#define MPL3115_CTRL_REG1_OST_SET
Definition: mpl3115.h:871
#define I2C_S_DEVICE_INDEX
Definition: issdk_hal.h:35
#define MPL3115_I2C_ADDR
void BOARD_InitDebugConsole(void)
Definition: board.c:15
void BOARD_InitPins(void)
Configures pin routing and optionally pin electrical features.
Definition: pin_mux.c:47
registeridlefunction_t idleFunction
Definition: sensor_drv.h:104
#define MPL3115_CTRL_REG1_ALT_ALT
Definition: mpl3115.h:888
#define MPL3115_ALTITUDE_CONV_FACTOR
Definition: mpl3115_drv.h:29