ISSDK  1.7
IoT Sensing Software Development Kit
diff_p_threshold.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_threshold.c
36  * @brief The diff_p_threshold.c file implements the ISSDK DIFF_P sensor driver
37  * example demonstration with Threshold detection 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 /* Default Threshold is 800 Counts */
62 #define P_TGT0_MSB 0x03
63 #define P_TGT0_LSB 0x20
64 
65 //-----------------------------------------------------------------------
66 // Global Variables
67 //-----------------------------------------------------------------------
68 volatile bool diff_pInterrupt = false;
69 
70 //-----------------------------------------------------------------------
71 // Constants
72 //-----------------------------------------------------------------------
73 /*! @brief Register settings for Normal (non buffered) mode. */
82 
83 /*! @brief Register settings for Clearing Pressure and Temperature Data Ready Bits. */
86 
87 /*! @brief Address of Status Register. */
89 
90 //-----------------------------------------------------------------------
91 // Functions
92 //-----------------------------------------------------------------------
93 /*! -----------------------------------------------------------------------
94  * @brief This is the Sensor Data Ready ISR implementation.
95  * @details This function sets the flag which indicates if a new sample(s) is available for reading.
96  * @param[in] pUserData This is a void pointer to the instance of the user specific data structure for the ISR.
97  * @return void There is no return value.
98  * @constraints None
99  * @reeentrant Yes
100  * -----------------------------------------------------------------------*/
101 void diff_p_isr(void *pUserData)
102 { /*! @brief Set flag to indicate Sensor has signalled data ready. */
103  diff_pInterrupt = true;
104 }
105 
106 /*!
107  * @brief Main function
108  */
109 int main(void)
110 {
111  int32_t status;
112  uint16_t thresholdValue;
113  uint8_t whoAmI, conversionFactor, dataReady;
114 
115  diff_p_i2c_sensorhandle_t diff_pDriver;
116  ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER;
117  GENERIC_DRIVER_GPIO *gpioDriver = &Driver_GPIO_KSDK;
118 
119  BOARD_InitPins();
123 
124  PRINTF("\r\n ISSDK DIFF-P Sensor Example !\r\n");
125 
126  /*! Initialize INT1_DIFF_P pin used by FRDM board */
127  gpioDriver->pin_init(&DIFF_P_INT1, GPIO_DIRECTION_IN, NULL, &diff_p_isr, NULL);
128 
129  /*! Initialize RGB LED pin used by FRDM board */
130  gpioDriver->pin_init(&GREEN_LED, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
131 
132  /*! Initialize the I2C driver. */
133  status = I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
134  if (ARM_DRIVER_OK != status)
135  {
136  PRINTF("\r\n I2C Initialization Failed.\r\n");
137  return -1;
138  }
139 
140  /*! Set the I2C Power mode. */
141  status = I2Cdrv->PowerControl(ARM_POWER_FULL);
142  if (ARM_DRIVER_OK != status)
143  {
144  PRINTF("\r\n I2C Power Mode setting Failed.\r\n");
145  return -1;
146  }
147 
148  /*! Set the I2C bus speed. */
149  status = I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
150  if (ARM_DRIVER_OK != status)
151  {
152  PRINTF("\r\n I2C Control Mode setting Failed.\r\n");
153  return -1;
154  }
155 
156  do
157  { /*! Initialize DIFF_P sensor driver. */
160  if (SENSOR_ERROR_NONE == status)
161  {
163  conversionFactor = NPS3000VV_PRESSURE_DIV_FACTOR;
164  break;
165  }
168  if (SENSOR_ERROR_NONE == status)
169  {
171  conversionFactor = NPS3001DV_PRESSURE_DIV_FACTOR;
172  break;
173  }
176  if (SENSOR_ERROR_NONE == status)
177  {
179  conversionFactor = NPS3002VV_PRESSURE_DIV_FACTOR;
180  break;
181  }
184  if (SENSOR_ERROR_NONE == status)
185  {
187  conversionFactor = NPS3005DV_PRESSURE_DIV_FACTOR;
188  break;
189  }
190 
191  PRINTF("\r\n Sensor Initialization Failed.\r\n");
192  return -1;
193  } while (false);
194  thresholdValue = (((int16_t)(P_TGT0_MSB) << 8) | P_TGT0_LSB) / conversionFactor;
195  PRINTF("\r\n Initiliazed Sensor with WHO_AM_I = [0x%X] Threshold = [%dPa].\r\n", whoAmI, thresholdValue);
196 
197  /*! Set the task to be executed while waiting for I2C transactions to complete. */
199 
200  /*! Configure the DIFF_P sensor. */
201  status = DIFF_P_I2C_Configure(&diff_pDriver, cDiffPConfigNormal);
202  if (SENSOR_ERROR_NONE != status)
203  {
204  PRINTF("\r\nDIFF_P now active and entering data read loop...\r\n");
205  return -1;
206  }
207  PRINTF("\r\n Successfully Applied Sensor Interrupt Configuration.\r\n");
208 
209  for (;;) /* Forever loop */
210  {
211  if (false == diff_pInterrupt)
212  { /* Loop, if new sample is not available. */
214  continue;
215  }
216  else
217  { /*! Clear the data ready flag, it will be set again by the ISR. */
218  diff_pInterrupt = false;
219  gpioDriver->toggle_pin(&GREEN_LED);
220  }
221 
222  /* Read INT_STATUS register */
223  status = DIFF_P_I2C_ReadData(&diff_pDriver, cDiffPStatus, &dataReady);
224  if (ARM_DRIVER_OK != status)
225  {
226  PRINTF("\r\n Read Failed. \r\n");
227  return -1;
228  }
229 
230  /*! Check for event ready bits from the DIFF_P. */
232  { /* Loop, if new samples are not available. */
233  continue;
234  }
235 
236  /*! Explicitly clear the Status Bits. */
237  status = Sensor_I2C_Write(diff_pDriver.pCommDrv, &diff_pDriver.deviceInfo, diff_pDriver.slaveAddress,
238  cDiffPClearStatusBits);
239  if (ARM_DRIVER_OK != status)
240  {
241  PRINTF("\r\n Write Failed. \r\n");
242  return -1;
243  }
244 
245  PRINTF("\r\n Threshold Event... \r\n", conversionFactor);
246  }
247 }
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 __END_WRITE_DATA__
Definition: sensor_drv.h:71
#define DIFF_P_CTRL_REG2_ODR_MASK
Definition: diff_p.h:774
#define DIFF_P_INT_MASK1_P_TGT0_INT_EN
Definition: diff_p.h:462
const registerwritelist_t cDiffPConfigNormal[]
Register settings for Normal (non buffered) mode.
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 NPS3002VV_PRESSURE_DIV_FACTOR
int main(void)
Main function.
#define DIFF_P_CTRL_REG1_OSR_MASK
Definition: diff_p.h:709
#define DIFF_P_I2C_ADDR
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
void BOARD_InitDebugConsole(void)
Definition: board.c:41
#define DIFF_P_INT_STATUS_1_P_TGT0_MASK
Definition: diff_p.h:222
void(* registeridlefunction_t)(void *userParam)
This is the register idle function type.
Definition: sensor_drv.h:123
#define DIFF_P_NPS3001DV_WHOAMI_VALUE
Definition: diff_p.h:97
const registerwritelist_t cDiffPClearStatusBits[]
Register settings for Clearing Pressure and Temperature Data Ready Bits.
#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_CTRL_REG1_OSR_OSR512
Definition: diff_p.h:731
#define NPS3001DV_PRESSURE_DIV_FACTOR
#define DIFF_P_INT1
#define DIFF_P_INT_STATUS_1_P_TGT0_REACHED
Definition: diff_p.h:242
gpioHandleKSDK_t GREEN_LED
Definition: frdm_k64f.c:214
void diff_p_isr(void *pUserData)
This is the Sensor Data Ready ISR implementation.
#define NPS3005DV_PRESSURE_DIV_FACTOR
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_NPS3000VV_WHOAMI_VALUE
Definition: diff_p.h:96
#define I2C_S_DRIVER
Definition: issdk_hal.h:59
#define P_TGT0_LSB
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 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 P_TGT0_MSB
#define DIFF_P_INT_MASK1_P_TGT0_MASK
Definition: diff_p.h:443
#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
#define I2C_S_SIGNAL_EVENT
Definition: issdk_hal.h:60
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
const registerreadlist_t cDiffPStatus[]
Address of Status Register.