ISSDK  1.8
IoT Sensing Software Development Kit
mpl3115_fifo_interrupt.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_fifo_interrupt.c
11  * @brief The mpl3115_fifo_interrupt.c file implements the ISSDK MPL3115 sensor driver
12  * example demonstration with interrupt 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 //-----------------------------------------------------------------------
29 // ISSDK Includes
30 //-----------------------------------------------------------------------
31 #include "issdk_hal.h"
32 #include "gpio_driver.h"
33 #include "mpl3115_drv.h"
34 
35 //-----------------------------------------------------------------------
36 // Macros
37 //-----------------------------------------------------------------------
38 /* Application Configurations Selections */
39 #define FIFO_WMRK_SIZE (8) /* Buffer 8 Samples. */
40 #define MPL3115_DATA_SIZE (5) /* 3 byte Pressure/Altitude and 2 byte Temperature. */
41 /*! In MPL3115 the Auto Acquisition Time Step (ODR) can be set only in powers of 2 (i.e. 2^x, where x is the
42  * SAMPLING_EXPONENT).
43  * This gives a range of 1 second to 2^15 seconds (9 hours). */
44 #define MPL3115_SAMPLING_EXPONENT (0) /* 1 seconds. */
45 
46 //-----------------------------------------------------------------------
47 // Constants
48 //-----------------------------------------------------------------------
49 /*! @brief Register settings for FIFO (buffered) mode @ default 1 sample per second with Interrupts. */
51  /* Set FIFO Mode and set FIFO Watermark Level. */
54  /* Enable Data Ready and Event flags for Pressure, Temperature or either. */
58  /* Set Over Sampling Ratio to 128. */
61  /* Set INT1 Active High. */
63  /* Enable Interrupts for FIFO Events. */
65  /* Route Interrupt to INT1. */
68 
69 /*! @brief Address of Status Register. */
71 
72 /*! @brief Address and size of Pressure+Temperature Data in FIFO Mode. */
75 
76 //-----------------------------------------------------------------------
77 // Global Variables
78 //-----------------------------------------------------------------------
79 volatile uint8_t gMpl3115DataReady;
80 
81 //-----------------------------------------------------------------------
82 // Functions
83 //-----------------------------------------------------------------------
84 /*! -----------------------------------------------------------------------
85  * @brief This is the Sensor Data Ready ISR implementation.
86  * @details This function sets the flag which indicates if a new sample(s) is available for reading.
87  * @param[in] pUserData This is a void pointer to the instance of the user specific data structure for the ISR.
88  * @return void There is no return value.
89  * @constraints None
90  * @reeentrant Yes
91  * -----------------------------------------------------------------------*/
92 void mpl3115_int_data_ready_callback(void *pUserData)
93 { /*! @brief Set flag to indicate Sensor has signalled data ready. */
94  gMpl3115DataReady = true;
95 }
96 
97 /*! -----------------------------------------------------------------------
98  * @brief This is the The main function implementation.
99  * @details This function invokes board initializes routines, then then brings up the sensor and
100  * finally enters an endless loop to continuously read available samples.
101  * @param[in] void This is no input parameter.
102  * @return void There is no return value.
103  * @constraints None
104  * @reeentrant No
105  * -----------------------------------------------------------------------*/
106 int main(void)
107 {
108  int16_t tempInDegrees;
109  uint32_t pressureInPascals;
110  int32_t status;
113 
114  ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER; // Now using the shield.h value!!!
115  mpl3115_i2c_sensorhandle_t mpl3115Driver;
117 
118  /*! Initialize the MCU hardware. */
119  BOARD_InitPins();
122 
123  PRINTF("\r\n ISSDK MPL3115 sensor driver example demonstration with interrupt mode.\r\n");
124 
125  /*! Initialize MAG3110 pin used by FRDM board */
127 
128  /*! Initialize RGB LED pin used by FRDM board */
129  pGpioDriver->pin_init(&GREEN_LED, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
130 
131  /*! Initialize the I2C driver. */
132  status = I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
133  if (ARM_DRIVER_OK != status)
134  {
135  PRINTF("\r\n I2C Initialization Failed\r\n");
136  return -1;
137  }
138 
139  /*! Set the I2C Power mode. */
140  status = I2Cdrv->PowerControl(ARM_POWER_FULL);
141  if (ARM_DRIVER_OK != status)
142  {
143  PRINTF("\r\n I2C Power Mode setting Failed\r\n");
144  return -1;
145  }
146 
147  /*! Set the I2C bus speed. */
148  status = I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
149  if (ARM_DRIVER_OK != status)
150  {
151  PRINTF("\r\n I2C Control Mode setting Failed\r\n");
152  return -1;
153  }
154 
155  /*! Initialize MPL3115 sensor driver. */
158  if (SENSOR_ERROR_NONE != status)
159  {
160  PRINTF("\r\n Sensor Initialization Failed\r\n");
161  return -1;
162  }
163  PRINTF("\r\n Successfully Initiliazed Sensor\r\n");
164 
165  /*! Set the task to be executed while waiting for I2C transactions to complete. */
167 
168  gMpl3115DataReady = false;
169 
170  /*! Configure the MAG3110 sensor driver. */
171  status = MPL3115_I2C_Configure(&mpl3115Driver, cMpl3115ConfigFIFO);
172  if (SENSOR_ERROR_NONE != status)
173  {
174  PRINTF("\r\nMPL3115 configuration failed...\r\n");
175  return -1;
176  }
177  PRINTF("\r\n Successfully Applied MAG3110 Sensor Configuration\r\n");
178 
179  for (;;) /* Forever loop (with logic optimized for FIFO Mode). */
180  { /* In ISR Mode we do not need to check Data Ready Register.
181  * The receipt of interrupt will indicate data is ready. */
182  if (false == gMpl3115DataReady)
183  { /* Loop, if new sample is not available. */
185  continue;
186  }
187  else
188  { /*! Clear the data ready flag, it will be set again by the ISR. */
189  gMpl3115DataReady = false;
190  pGpioDriver->toggle_pin(&GREEN_LED);
191  /* Read FIFO status, to clear sensor's INT.
192  * Note: This is a special step in FIFO Mode particular to MPL3115, where we have to read F_STATUS to clear
193  * the sensor's internal INT. */
194  MPL3115_I2C_ReadData(&mpl3115Driver, cMpl3115Status, (uint8_t *)&status);
195  }
196 
197  /*! Read new raw sensor data from the MPL3115. */
198  status = MPL3115_I2C_ReadData(&mpl3115Driver, cMpl3115OutputFIFO, data);
199  if (ARM_DRIVER_OK != status)
200  {
201  PRINTF("\r\n Read Failed. \r\n");
202  return -1;
203  }
204 
205  /* Reset Counters */
206  pressureInPascals = 0;
207  tempInDegrees = 0;
208 
209  for (uint8_t i = 0; i < FIFO_WMRK_SIZE; i++)
210  { /*! Process all samples and convert the raw sensor data. */
211  rawData.pressure = (uint32_t)((data[i * MPL3115_DATA_SIZE + 0]) << 16) |
212  ((data[i * MPL3115_DATA_SIZE + 1]) << 8) | ((data[i * MPL3115_DATA_SIZE + 2]));
213  pressureInPascals += rawData.pressure / MPL3115_PRESSURE_CONV_FACTOR;
214 
215  rawData.temperature = (int16_t)((data[i * MPL3115_DATA_SIZE + 3]) << 8) | (data[i * MPL3115_DATA_SIZE + 4]);
216  tempInDegrees += rawData.temperature / MPL3115_TEMPERATURE_CONV_FACTOR;
217  }
218  /* NOTE: PRINTF is relatively expensive in terms of CPU time, specially when used with-in execution loop. */
219  PRINTF("\r\nAverage Pressure = %d Pa\r\n", pressureInPascals / FIFO_WMRK_SIZE);
220  PRINTF("\r\nAverage Temperature = %d degC\r\n", tempInDegrees / FIFO_WMRK_SIZE);
221  ASK_USER_TO_RESUME(16 / FIFO_WMRK_SIZE); /* Ask for user input after processing 16 samples. */
222  }
223 }
This structure defines the Write command List.
Definition: sensor_drv.h:68
volatile uint8_t gMpl3115DataReady
#define ASK_USER_TO_RESUME(x)
Definition: frdm_k64f.h:106
int32_t status
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:53
#define MPL3115_PRESSURE_CONV_FACTOR
Definition: mpl3115_drv.h:28
status_t SMC_SetPowerModeVlpr(void *arg)
Configures the system to VLPR power mode. API name used from Kinetis family to maintain compatibility...
Definition: lpc54114.c:169
int main(void)
This is the The main function implementation.
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
#define MPL3115_F_SETUP_F_MODE_MASK
Definition: mpl3115.h:410
The mpl3115_drv.h file describes the MPL3115 driver interface and structures.
#define MPL3115_CTRL_REG2_ST_MASK
Definition: mpl3115.h:916
Access structure of the GPIO Driver.
Definition: Driver_GPIO.h:38
#define SMC
Definition: lpc54114.h:118
#define MPL3115_SAMPLING_EXPONENT
#define __END_WRITE_DATA__
Definition: sensor_drv.h:45
#define MPL3115_F_SETUP_F_MODE_STOP_MODE
Definition: mpl3115.h:420
#define MPL3115_PT_DATA_CFG_TDEFE_MASK
Definition: mpl3115.h:550
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
#define MPL3115_F_SETUP_F_WMRK_MASK
Definition: mpl3115.h:407
#define FIFO_WMRK_SIZE
#define I2C_S_SIGNAL_EVENT
Definition: issdk_hal.h:34
#define I2C_S_DRIVER
Definition: issdk_hal.h:33
#define BOARD_BootClockRUN
Definition: clock_config.h:19
#define MPL3115_TEMPERATURE_CONV_FACTOR
Definition: mpl3115_drv.h:30
const registerreadlist_t cMpl3115Status[]
Address of Status Register.
void(* registeridlefunction_t)(void *userParam)
This is the register idle function type.
Definition: sensor_drv.h:97
#define MPL3115_CTRL_REG3_IPOL1_HIGH
Definition: mpl3115.h:990
const registerreadlist_t cMpl3115OutputFIFO[]
Address and size of Pressure+Temperature Data in FIFO Mode.
GENERIC_DRIVER_GPIO Driver_GPIO_KSDK
Definition: gpio_driver.c:177
GENERIC_DRIVER_GPIO * pGpioDriver
This structure defines the mpl3115 data buffer in Pressure Mode.
Definition: mpl3115_drv.h:45
#define MPL3115_CTRL_REG3_IPOL1_MASK
Definition: mpl3115.h:976
#define MPL3115_PT_DATA_CFG_DREM_ENABLED
Definition: mpl3115.h:570
uint8_t data[FXLS8962_DATA_SIZE]
#define MPL3115_PT_DATA_CFG_PDEFE_MASK
Definition: mpl3115.h:553
#define __END_READ_DATA__
Definition: sensor_drv.h:51
This defines the sensor specific information.
Definition: mpl3115_drv.h:36
#define MPL3115_CTRL_REG1_OS_OSR_128
Definition: mpl3115.h:884
#define MPL3115_CTRL_REG4_INT_EN_FIFO_INTENABLED
Definition: mpl3115.h:1068
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_CTRL_REG5_INT_CFG_FIFO_MASK
Definition: mpl3115.h:1125
void(* toggle_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:48
#define MPL3115_CTRL_REG4_INT_EN_FIFO_MASK
Definition: mpl3115.h:1045
#define MPL3115_PT_DATA_CFG_DREM_MASK
Definition: mpl3115.h:556
void mpl3115_int_data_ready_callback(void *pUserData)
This is the Sensor Data Ready ISR implementation.
#define MPL3115_PT_DATA_CFG_PDEFE_ENABLED
Definition: mpl3115.h:567
const registerwritelist_t cMpl3115ConfigFIFO[]
Register settings for FIFO (buffered) mode @ default 1 sample per second with Interrupts.
#define MPL3115_INT1
#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
void(* pin_init)(pinID_t aPinId, gpio_direction_t dir, void *apPinConfig, gpio_isr_handler_t aIsrHandler, void *apUserData)
Definition: Driver_GPIO.h:41
This structure defines the Read command List.
Definition: sensor_drv.h:78
gpioHandleKSDK_t GREEN_LED
Definition: frdm_k64f.c:188
#define MPL3115_DATA_SIZE
#define MPL3115_PT_DATA_CFG_TDEFE_ENABLED
Definition: mpl3115.h:564
#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
#define MPL3115_CTRL_REG5_INT_CFG_FIFO_INT1
Definition: mpl3115.h:1148
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:61
#define MPL3115_CTRL_REG1_OS_MASK
Definition: mpl3115.h:855