ISSDK  1.7
IoT Sensing Software Development Kit
fxpq3115_fifo_interrupt.c
Go to the documentation of this file.
1 /*
2  * The Clear BSD License
3  * Copyright (c) 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 fxpq3115_fifo_interrupt.c
37  * @brief The fxpq3115_fifo_interrupt.c file implements the ISSDK FXPQ3115BV sensor driver
38  * example demonstration with fifo in interrupt 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 //-----------------------------------------------------------------------
55 // ISSDK Includes
56 //-----------------------------------------------------------------------
57 #include "issdk_hal.h"
58 #include "gpio_driver.h"
59 #include "fxpq3115_drv.h"
60 
61 //-----------------------------------------------------------------------
62 // Macros
63 //-----------------------------------------------------------------------
64 /* Application Configurations Selections */
65 #define FIFO_WMRK_SIZE (8) /* Buffer 8 Samples. */
66 #define FXPQ3115_DATA_SIZE (5) /* 3 byte Pressure/Altitude and 2 byte Temperature. */
67 /*! In FXPQ3115 the Auto Acquisition Time Step (ODR) can be set only in powers of 2 (i.e. 2^x, where x is the
68  * SAMPLING_EXPONENT).
69  * This gives a range of 1 second to 2^15 seconds (9 hours). */
70 #define FXPQ3115_SAMPLING_EXPONENT (0) /* 1 seconds. */
71 
72 //-----------------------------------------------------------------------
73 // Constants
74 //-----------------------------------------------------------------------
75 /*! @brief Register settings for FIFO (buffered) mode @ default 1 sample per second with Interrupts. */
77  /* Set FIFO Mode and set FIFO Watermark Level. */
80  /* Enable Data Ready and Event flags for Pressure, Temperature or either. */
84  /* Set Over Sampling Ratio to 128. */
87  /* Set INT1 Active High. */
89  /* Enable Interrupts for FIFO Events. */
91  /* Route Interrupt to INT1. */
94 
95 /*! @brief Address of Status Register. */
97 
98 /*! @brief Address and size of Pressure+Temperature Data in FIFO Mode. */
101 
102 //-----------------------------------------------------------------------
103 // Global Variables
104 //-----------------------------------------------------------------------
105 volatile uint8_t gB3115DataReady;
106 
107 //-----------------------------------------------------------------------
108 // Functions
109 //-----------------------------------------------------------------------
110 /*! -----------------------------------------------------------------------
111  * @brief This is the Sensor Data Ready ISR implementation.
112  * @details This function sets the flag which indicates if a new sample(s) is available for reading.
113  * @param[in] pUserData This is a void pointer to the instance of the user specific data structure for the ISR.
114  * @return void There is no return value.
115  * @constraints None
116  * @reeentrant Yes
117  * -----------------------------------------------------------------------*/
118 void fxpq3115_int_data_ready_callback(void *pUserData)
119 { /*! @brief Set flag to indicate Sensor has signalled data ready. */
120  gB3115DataReady = true;
121 }
122 
123 /*! -----------------------------------------------------------------------
124  * @brief This is the The main function implementation.
125  * @details This function invokes board initializes routines, then then brings up the sensor and
126  * finally enters an endless loop to continuously read available samples.
127  * @param[in] void This is no input parameter.
128  * @return void There is no return value.
129  * @constraints None
130  * @reeentrant No
131  * -----------------------------------------------------------------------*/
132 int main(void)
133 {
134  int16_t tempInDegrees;
135  uint32_t pressureInPascals;
136  int32_t status;
139 
140  ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER; // Now using the shield.h value!!!
141  fxpq3115_i2c_sensorhandle_t fxpq3115Driver;
143 
144  /*! Initialize the MCU hardware. */
145  BOARD_InitPins();
148 
149  PRINTF("\r\n ISSDK FXPQ3115 sensor driver example demonstration with interrupt mode.\r\n");
150 
151  /*! Initialize FXPQ3115 pin used by FRDM board */
153 
154  /*! Initialize RGB LED pin used by FRDM board */
155  pGpioDriver->pin_init(&GREEN_LED, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
156 
157  /*! Initialize the I2C driver. */
158  status = I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
159  if (ARM_DRIVER_OK != status)
160  {
161  PRINTF("\r\n I2C Initialization Failed\r\n");
162  return -1;
163  }
164 
165  /*! Set the I2C Power mode. */
166  status = I2Cdrv->PowerControl(ARM_POWER_FULL);
167  if (ARM_DRIVER_OK != status)
168  {
169  PRINTF("\r\n I2C Power Mode setting Failed\r\n");
170  return -1;
171  }
172 
173  /*! Set the I2C bus speed. */
174  status = I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
175  if (ARM_DRIVER_OK != status)
176  {
177  PRINTF("\r\n I2C Control Mode setting Failed\r\n");
178  return -1;
179  }
180 
181  /*! Initialize FXPQ3115 sensor driver. */
184  if (SENSOR_ERROR_NONE != status)
185  {
186  PRINTF("\r\n Sensor Initialization Failed\r\n");
187  return -1;
188  }
189  PRINTF("\r\n Successfully Initiliazed Sensor\r\n");
190 
191  /*! Set the task to be executed while waiting for I2C transactions to complete. */
193 
194  gB3115DataReady = false;
195 
196  /*! Configure the FXPQ3115 sensor driver. */
197  status = FXPQ3115_I2C_Configure(&fxpq3115Driver, B3115ConfigFIFOInt);
198  if (SENSOR_ERROR_NONE != status)
199  {
200  PRINTF("\r\n FXPQ3115 Sensor Configuration Failed, Err = %d\r\n", status);
201  return -1;
202  }
203  PRINTF("\r\n Successfully Applied FXPQ3115 Sensor Configuration\r\n");
204 
205  for (;;) /* Forever loop (with logic optimized for FIFO Mode). */
206  { /* In ISR Mode we do not need to check Data Ready Register.
207  * The receipt of interrupt will indicate data is ready. */
208  if (false == gB3115DataReady)
209  { /* Loop, if new sample is not available. */
211  continue;
212  }
213  else
214  { /*! Clear the data ready flag, it will be set again by the ISR. */
215  gB3115DataReady = false;
216  pGpioDriver->toggle_pin(&GREEN_LED);
217  /* Read FIFO status, to clear sensor's INT.
218  * Note: This is a special step in FIFO Mode particular to FXPQ3115, where we have to read F_STATUS to clear
219  * the sensor's internal INT. */
220  FXPQ3115_I2C_ReadData(&fxpq3115Driver, B3115Status, (uint8_t *)&status);
221  }
222 
223  /*! Read new raw sensor data from the FXPQ3115. */
224  status = FXPQ3115_I2C_ReadData(&fxpq3115Driver, B3115OutputFIFO, data);
225  if (ARM_DRIVER_OK != status)
226  {
227  PRINTF("\r\n Read Failed. \r\n");
228  return -1;
229  }
230 
231  /* Reset Counters */
232  pressureInPascals = 0;
233  tempInDegrees = 0;
234 
235  for (uint8_t i = 0; i < FIFO_WMRK_SIZE; i++)
236  { /*! Process all samples and convert the raw sensor data. */
237  rawData.pressure = (uint32_t)((data[i * FXPQ3115_DATA_SIZE + 0]) << 16) |
238  ((data[i * FXPQ3115_DATA_SIZE + 1]) << 8) | ((data[i * FXPQ3115_DATA_SIZE + 2]));
239  pressureInPascals += rawData.pressure / FXPQ3115_PRESSURE_CONV_FACTOR;
240 
241  rawData.temperature =
242  (int16_t)((data[i * FXPQ3115_DATA_SIZE + 3]) << 8) | (data[i * FXPQ3115_DATA_SIZE + 4]);
243  tempInDegrees += rawData.temperature / FXPQ3115_TEMPERATURE_CONV_FACTOR;
244  }
245  /* NOTE: PRINTF is relatively expensive in terms of CPU time, specially when used with-in execution loop. */
246  PRINTF("\r\n Average Pressure = %d Pa\r\n", pressureInPascals / FIFO_WMRK_SIZE);
247  PRINTF("\r\n Average Temperature = %d degC\r\n", tempInDegrees / FIFO_WMRK_SIZE);
248  ASK_USER_TO_RESUME(16 / FIFO_WMRK_SIZE); /* Ask for user input after processing 16 samples. */
249  }
250 }
#define FXPQ3115_TEMPERATURE_CONV_FACTOR
Definition: fxpq3115_drv.h:56
#define FXPQ3115_CTRL_REG5_INT_CFG_FIFO_INT1
Definition: fxpq3115.h:1151
#define FXPQ3115_CTRL_REG1_OS_MASK
Definition: fxpq3115.h:855
#define __END_WRITE_DATA__
Definition: sensor_drv.h:71
int main(void)
This is the The main function implementation.
void(* pin_init)(pinID_t aPinId, gpio_direction_t dir, void *apPinConfig, gpio_isr_handler_t aIsrHandler, void *apUserData)
Definition: Driver_GPIO.h:67
#define BOARD_BootClockRUN
Definition: clock_config.h:45
uint8_t data[FXLS8962_DATA_SIZE]
#define FXPQ3115_CTRL_REG4_INT_EN_FIFO_MASK
Definition: fxpq3115.h:1048
int32_t status
This structure defines the fxpq3115 data buffer in Pressure Mode.
Definition: fxpq3115_drv.h:71
void(* toggle_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:74
int32_t FXPQ3115_I2C_Initialize(fxpq3115_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: fxpq3115_drv.c:48
#define FXPQ3115_I2C_ADDR
void BOARD_InitDebugConsole(void)
Definition: board.c:41
const registerreadlist_t B3115Status[]
Address of Status Register.
#define FXPQ3115_PT_DATA_CFG_TDEFE_MASK
Definition: fxpq3115.h:550
void(* registeridlefunction_t)(void *userParam)
This is the register idle function type.
Definition: sensor_drv.h:123
#define FXPQ3115_CTRL_REG3_IPOL1_MASK
Definition: fxpq3115.h:979
#define FXPQ3115_PT_DATA_CFG_PDEFE_ENABLED
Definition: fxpq3115.h:567
#define FXPQ3115_INT1
#define FXPQ3115_WHOAMI_VALUE
Definition: fxpq3115.h:65
#define __END_READ_DATA__
Definition: sensor_drv.h:77
#define FXPQ3115_CTRL_REG2_ST_MASK
Definition: fxpq3115.h:919
GENERIC_DRIVER_GPIO * pGpioDriver
const registerwritelist_t B3115ConfigFIFOInt[]
Register settings for FIFO (buffered) mode @ default 1 sample per second with Interrupts.
#define FXPQ3115_PRESSURE_CONV_FACTOR
Definition: fxpq3115_drv.h:54
#define FXPQ3115_SAMPLING_EXPONENT
#define FXPQ3115_CTRL_REG1_OS_OSR_128
Definition: fxpq3115.h:887
#define FXPQ3115_PT_DATA_CFG_DREM_MASK
Definition: fxpq3115.h:556
gpioHandleKSDK_t GREEN_LED
Definition: frdm_k64f.c:214
#define FXPQ3115_CTRL_REG3_IPOL1_HIGH
Definition: fxpq3115.h:993
This defines the sensor specific information.
Definition: fxpq3115_drv.h:62
#define ASK_USER_TO_RESUME(x)
Definition: frdm_k64f.h:132
#define FXPQ3115_DATA_SIZE
void FXPQ3115_I2C_SetIdleTask(fxpq3115_i2c_sensorhandle_t *pSensorHandle, registeridlefunction_t idleTask, void *userParam)
: The interface function to set the I2C Idle Task.
Definition: fxpq3115_drv.c:79
#define FXPQ3115_CTRL_REG5_INT_CFG_FIFO_MASK
Definition: fxpq3115.h:1128
void fxpq3115_int_data_ready_callback(void *pUserData)
This is the Sensor Data Ready ISR implementation.
#define FXPQ3115_PT_DATA_CFG_DREM_ENABLED
Definition: fxpq3115.h:570
#define I2C_S_DEVICE_INDEX
Definition: issdk_hal.h:61
#define I2C_S_DRIVER
Definition: issdk_hal.h:59
#define FXPQ3115_F_SETUP_F_MODE_MASK
Definition: fxpq3115.h:410
GENERIC_DRIVER_GPIO Driver_GPIO_KSDK
Definition: gpio_driver.c:203
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
#define FXPQ3115_CTRL_REG4_INT_EN_FIFO_INTENABLED
Definition: fxpq3115.h:1071
#define FXPQ3115_PT_DATA_CFG_PDEFE_MASK
Definition: fxpq3115.h:553
Access structure of the GPIO Driver.
Definition: Driver_GPIO.h:64
int32_t FXPQ3115_I2C_Configure(fxpq3115_i2c_sensorhandle_t *pSensorHandle, const registerwritelist_t *pRegWriteList)
The interface function to configure he sensor.
Definition: fxpq3115_drv.c:87
fxls8962_acceldataUser_t rawData
This structure defines the Write command List.
Definition: sensor_drv.h:94
int32_t FXPQ3115_I2C_ReadData(fxpq3115_i2c_sensorhandle_t *pSensorHandle, const registerreadlist_t *pReadList, uint8_t *pBuffer)
The interface function to read the sensor data.
Definition: fxpq3115_drv.c:132
This structure defines the Read command List.
Definition: sensor_drv.h:104
#define SMC
Definition: lpc54114.h:144
volatile uint8_t gB3115DataReady
#define FIFO_WMRK_SIZE
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 FXPQ3115_PT_DATA_CFG_TDEFE_ENABLED
Definition: fxpq3115.h:564
#define I2C_S_SIGNAL_EVENT
Definition: issdk_hal.h:60
#define FXPQ3115_F_SETUP_F_MODE_STOP_MODE
Definition: fxpq3115.h:420
The fxpq3115_drv.h file describes the fxpq3115 driver interface and structures.
const registerreadlist_t B3115OutputFIFO[]
Address and size of Pressure+Temperature Data in FIFO Mode.
#define FXPQ3115_F_SETUP_F_WMRK_MASK
Definition: fxpq3115.h:407