ISSDK  1.7
IoT Sensing Software Development Kit
diff_p_interrupt.c
Go to the documentation of this file.
1 /*
2  * The Clear BSD License
3  * Copyright 2017-2018 NXP
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without modification,
7  * are permitted (subject to the limitations in the disclaimer below) provided
8  * that the following conditions are met:
9  *
10  * o Redistributions of source code must retain the above copyright notice, this list
11  * of conditions and the following disclaimer.
12  *
13  * o Redistributions in binary form must reproduce the above copyright notice, this
14  * list of conditions and the following disclaimer in the documentation and/or
15  * other materials provided with the distribution.
16  *
17  * o Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived from this
19  * software without specific prior written permission.
20  *
21  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
26  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
29  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /**
35  * @file diff_p_interrupt.c
36  * @brief The diff_p_interrupt.c file implements the ISSDK DIFF_P sensor driver
37  * example demonstration with Interrupt mode.
38  */
39 
40 //-----------------------------------------------------------------------
41 // SDK Includes
42 //-----------------------------------------------------------------------
43 #include "board.h"
44 #include "pin_mux.h"
45 #include "clock_config.h"
46 #include "fsl_debug_console.h"
47 
48 //-----------------------------------------------------------------------
49 // CMSIS Includes
50 //-----------------------------------------------------------------------
51 #include "Driver_I2C.h"
52 
53 /* ISSDK Includes */
54 #include "issdk_hal.h"
55 #include "diff_p_drv.h"
56 #include "systick_utils.h"
57 
58 //-----------------------------------------------------------------------
59 // Macros
60 //-----------------------------------------------------------------------
61 #define DIFF_P_DATA_SIZE (3) /* 2 byte Pressure and 1 byte Temperature. */
62 
63 //-----------------------------------------------------------------------
64 // Global Variables
65 //-----------------------------------------------------------------------
66 volatile bool diff_pInterrupt = false;
67 
68 //-----------------------------------------------------------------------
69 // Constants
70 //-----------------------------------------------------------------------
71 /*! @brief Register settings for Normal Mode. */
79 
80 /*! @brief Register settings for Clearing Pressure and Temperature Data Ready Bits. */
83 
84 /*! @brief Address of Status Register. */
86 
87 /*! @brief Address and size of Raw Pressure+Temperature Data in Normal Mode. */
90 
91 //-----------------------------------------------------------------------
92 // Functions
93 //-----------------------------------------------------------------------
94 /*! -----------------------------------------------------------------------
95  * @brief This is the Sensor Data Ready ISR implementation.
96  * @details This function sets the flag which indicates if a new sample(s) is available for reading.
97  * @param[in] pUserData This is a void pointer to the instance of the user specific data structure for the ISR.
98  * @return void There is no return value.
99  * @constraints None
100  * @reeentrant Yes
101  * -----------------------------------------------------------------------*/
102 void diff_p_isr(void *pUserData)
103 { /*! @brief Set flag to indicate Sensor has signalled data ready. */
104  diff_pInterrupt = true;
105 }
106 
107 /*!
108  * @brief Main function
109  */
110 int main(void)
111 {
112  int32_t status;
113  int8_t tempInDegrees;
114  float pressureInPascals;
116  uint8_t whoAmI, conversionFactor;
117  uint8_t dataReady, data[DIFF_P_DATA_SIZE];
118 
119  diff_p_i2c_sensorhandle_t diff_pDriver;
120  ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER;
121  GENERIC_DRIVER_GPIO *gpioDriver = &Driver_GPIO_KSDK;
122 
123  BOARD_InitPins();
127 
128  PRINTF("\r\n ISSDK DIFF-P Sensor Example !\r\n");
129 
130  /*! Initialize INT1_DIFF_P pin used by FRDM board */
131  gpioDriver->pin_init(&DIFF_P_INT1, GPIO_DIRECTION_IN, NULL, &diff_p_isr, NULL);
132 
133  /*! Initialize RGB LED pin used by FRDM board */
134  gpioDriver->pin_init(&GREEN_LED, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
135 
136  /*! Initialize the I2C driver. */
137  status = I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
138  if (ARM_DRIVER_OK != status)
139  {
140  PRINTF("\r\n I2C Initialization Failed.\r\n");
141  return -1;
142  }
143 
144  /*! Set the I2C Power mode. */
145  status = I2Cdrv->PowerControl(ARM_POWER_FULL);
146  if (ARM_DRIVER_OK != status)
147  {
148  PRINTF("\r\n I2C Power Mode setting Failed.\r\n");
149  return -1;
150  }
151 
152  /*! Set the I2C bus speed. */
153  status = I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
154  if (ARM_DRIVER_OK != status)
155  {
156  PRINTF("\r\n I2C Control Mode setting Failed.\r\n");
157  return -1;
158  }
159 
160  do
161  { /*! Initialize DIFF_P sensor driver. */
164  if (SENSOR_ERROR_NONE == status)
165  {
167  conversionFactor = NPS3000VV_PRESSURE_DIV_FACTOR;
168  break;
169  }
172  if (SENSOR_ERROR_NONE == status)
173  {
175  conversionFactor = NPS3001DV_PRESSURE_DIV_FACTOR;
176  break;
177  }
180  if (SENSOR_ERROR_NONE == status)
181  {
183  conversionFactor = NPS3002VV_PRESSURE_DIV_FACTOR;
184  break;
185  }
188  if (SENSOR_ERROR_NONE == status)
189  {
191  conversionFactor = NPS3005DV_PRESSURE_DIV_FACTOR;
192  break;
193  }
194 
195  PRINTF("\r\n Sensor Initialization Failed.\r\n");
196  return -1;
197  } while (false);
198  PRINTF("\r\n Initiliazed Sensor with WHO_AM_I = [0x%X].\r\n", whoAmI);
199 
200  /*! Set the task to be executed while waiting for I2C transactions to complete. */
202 
203  /*! Configure the DIFF_P sensor. */
204  status = DIFF_P_I2C_Configure(&diff_pDriver, cDiffPConfigNormal);
205  if (SENSOR_ERROR_NONE != status)
206  {
207  PRINTF("\r\nDIFF_P now active and entering data read loop...\r\n");
208  return -1;
209  }
210  PRINTF("\r\n Successfully Applied Sensor Interrupt Configuration.\r\n");
211 
212  for (;;) /* Forever loop */
213  {
214  if (false == diff_pInterrupt)
215  { /* Loop, if new sample is not available. */
217  continue;
218  }
219  else
220  { /*! Clear the data ready flag, it will be set again by the ISR. */
221  diff_pInterrupt = false;
222  gpioDriver->toggle_pin(&GREEN_LED);
223  }
224 
225  do
226  { /* Read INT_STATUS register */
227  status = DIFF_P_I2C_ReadData(&diff_pDriver, cDiffPStatus, &dataReady);
228  if (ARM_DRIVER_OK != status)
229  {
230  PRINTF("\r\n Read Failed. \r\n");
231  return -1;
232  }
233 
234  /*! Check for both data ready bits from the DIFF_P. */
237  { /* Loop, if new samples are not available. */
238  continue;
239  }
240  } while (false);
241 
242  /*! Read new raw sensor data from the DIFF_P. */
243  status = DIFF_P_I2C_ReadData(&diff_pDriver, cDiffPOutputNormal, data);
244  if (ARM_DRIVER_OK != status)
245  {
246  PRINTF("\r\n Read Failed. \r\n");
247  return -1;
248  }
249 
250  /*! Explicitly clear the Status Bits. */
251  status = Sensor_I2C_Write(diff_pDriver.pCommDrv, &diff_pDriver.deviceInfo, diff_pDriver.slaveAddress,
252  cDiffPClearStatusBits);
253  if (ARM_DRIVER_OK != status)
254  {
255  PRINTF("\r\n Write Failed. \r\n");
256  return -1;
257  }
258 
259  /*! Process the sample and convert the raw sensor data. */
260  rawData.pressure = ((int16_t)(data[1]) << 8) | data[0];
261  pressureInPascals = (1.0 * rawData.pressure) / conversionFactor;
262 
263  rawData.temperature = (int8_t)(data[2]);
264  tempInDegrees = rawData.temperature;
265 
266  PRINTF("\r\n Pressure = %0.3f Pa", pressureInPascals);
267  PRINTF("\r\n Temperature = %d degC\r\n", tempInDegrees);
268  ASK_USER_TO_RESUME(50); /* Ask for user input after processing 50 samples. */
269  }
270 }
void diff_p_isr(void *pUserData)
This is the Sensor Data Ready ISR implementation.
This defines the sensor specific information for I2C.
Definition: diff_p_drv.h:70
void DIFF_P_I2C_SetIdleTask(diff_p_i2c_sensorhandle_t *pSensorHandle, registeridlefunction_t idleTask, void *userParam)
: The interface function to set the I2C Idle Task.
Definition: diff_p_drv.c:366
#define DIFF_P_INT_MASK0_PDR_INT_EN
Definition: diff_p.h:396
#define DIFF_P_INT_MASK0_TDR_MASK
Definition: diff_p.h:376
#define __END_WRITE_DATA__
Definition: sensor_drv.h:71
#define DIFF_P_CTRL_REG2_ODR_MASK
Definition: diff_p.h:774
int32_t DIFF_P_I2C_Configure(diff_p_i2c_sensorhandle_t *pSensorHandle, const registerwritelist_t *pRegWriteList)
The interface function to configure he sensor.
Definition: diff_p_drv.c:372
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
#define DIFF_P_INT_STATUS_0_PDR_DRDY
Definition: diff_p.h:171
#define NPS3002VV_PRESSURE_DIV_FACTOR
#define DIFF_P_CTRL_REG1_OSR_MASK
Definition: diff_p.h:709
#define DIFF_P_I2C_ADDR
uint8_t data[FXLS8962_DATA_SIZE]
volatile bool diff_pInterrupt
int32_t status
void(* toggle_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:74
int32_t DIFF_P_I2C_ReadData(diff_p_i2c_sensorhandle_t *pSensorHandle, const registerreadlist_t *pReadList, uint8_t *pBuffer)
The interface function to read the sensor data.
Definition: diff_p_drv.c:415
#define DIFF_P_DATA_SIZE
void BOARD_InitDebugConsole(void)
Definition: board.c:41
void(* registeridlefunction_t)(void *userParam)
This is the register idle function type.
Definition: sensor_drv.h:123
const registerreadlist_t cDiffPStatus[]
Address of Status Register.
#define DIFF_P_NPS3001DV_WHOAMI_VALUE
Definition: diff_p.h:97
#define DIFF_P_CTRL_REG2_ODR_ODR6P25
Definition: diff_p.h:799
#define __END_READ_DATA__
Definition: sensor_drv.h:77
registerDeviceInfo_t deviceInfo
Definition: diff_p_drv.h:72
int32_t DIFF_P_I2C_Initialize(diff_p_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: diff_p_drv.c:292
The diff_p_drv.h file describes the DIFF_P driver interface and structures.
#define DIFF_P_INT_STATUS_0_PDR_MASK
Definition: diff_p.h:151
#define DIFF_P_CTRL_REG1_OSR_OSR512
Definition: diff_p.h:731
#define NPS3001DV_PRESSURE_DIV_FACTOR
#define DIFF_P_INT1
gpioHandleKSDK_t GREEN_LED
Definition: frdm_k64f.c:214
const registerreadlist_t cDiffPOutputNormal[]
Address and size of Raw Pressure+Temperature Data in Normal Mode.
#define ASK_USER_TO_RESUME(x)
Definition: frdm_k64f.h:132
#define NPS3005DV_PRESSURE_DIV_FACTOR
const registerwritelist_t cDiffPClearStatusBits[]
Register settings for Clearing Pressure and Temperature Data Ready Bits.
int main(void)
Main function.
void BOARD_SystickEnable(void)
Function to enable systicks framework.
Definition: systick_utils.c:70
#define I2C_S_DEVICE_INDEX
Definition: issdk_hal.h:61
#define DIFF_P_INT_STATUS_0_TDR_MASK
Definition: diff_p.h:148
#define DIFF_P_NPS3000VV_WHOAMI_VALUE
Definition: diff_p.h:96
#define I2C_S_DRIVER
Definition: issdk_hal.h:59
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.
This structure defines the diff_p data buffer in Pressure Mode.
Definition: diff_p_drv.h:79
#define DIFF_P_CTRL_REG3_IPOL1_ACTIVE_HIGH
Definition: diff_p.h:862
Access structure of the GPIO Driver.
Definition: Driver_GPIO.h:64
ARM_DRIVER_I2C * pCommDrv
Definition: diff_p_drv.h:73
ARM Systick Utilities.
#define NPS3000VV_PRESSURE_DIV_FACTOR
#define DIFF_P_INT_MASK0_PDR_MASK
Definition: diff_p.h:379
fxls8962_acceldataUser_t rawData
#define DIFF_P_NPS3002VV_WHOAMI_VALUE
Definition: diff_p.h:98
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 DIFF_P_NPS3005DV_WHOAMI_VALUE
Definition: diff_p.h:99
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
#define DIFF_P_CTRL_REG3_IPOL1_MASK
Definition: diff_p.h:849
void BOARD_InitPins(void)
Configures pin routing and optionally pin electrical features.
Definition: pin_mux.c:73
const registerwritelist_t cDiffPConfigNormal[]
Register settings for Normal Mode.
#define DIFF_P_INT_STATUS_0_TDR_DRDY
Definition: diff_p.h:170
#define I2C_S_SIGNAL_EVENT
Definition: issdk_hal.h:60
#define DIFF_P_INT_MASK0_TDR_INT_EN
Definition: diff_p.h:395
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:97