ISSDK  1.7
IoT Sensing Software Development Kit
diff_p_oneshot.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_oneshot.c
36  * @brief The diff_p_oneshot.c file implements the ISSDK DIFF_P sensor driver
37  * example demonstration with One-Shot 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 OST Mode. */
78 
79 /*! @brief Register settings for Triggring One-Shot Sampling. */
81  /* Set the One ShoT Bit. */
84 
85 /*! @brief Register settings for Clearing Pressure and Temperature Data Ready Bits. */
88 
89 /*! @brief Address of Status Register. */
91 
92 /*! @brief Address and size of Raw Pressure+Temperature Data in Normal Mode. */
95 
96 //-----------------------------------------------------------------------
97 // Functions
98 //-----------------------------------------------------------------------
99 /*! -----------------------------------------------------------------------
100  * @brief This is the Sensor Data Ready ISR implementation.
101  * @details This function sets the flag which indicates if a new sample(s) is available for reading.
102  * @param[in] pUserData This is a void pointer to the instance of the user specific data structure for the ISR.
103  * @return void There is no return value.
104  * @constraints None
105  * @reeentrant Yes
106  * -----------------------------------------------------------------------*/
107 void diff_p_isr(void *pUserData)
108 { /*! @brief Set flag to indicate Sensor has signalled data ready. */
109  diff_pInterrupt = true;
110 }
111 
112 /*!
113  * @brief Main function
114  */
115 int main(void)
116 {
117  int32_t status;
118  int8_t tempInDegrees;
119  float pressureInPascals;
121  uint8_t whoAmI, conversionFactor;
122  uint8_t dataReady, data[DIFF_P_DATA_SIZE];
123 
124  diff_p_i2c_sensorhandle_t diff_pDriver;
125  ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER;
126  GENERIC_DRIVER_GPIO *gpioDriver = &Driver_GPIO_KSDK;
127 
128  BOARD_InitPins();
132 
133  PRINTF("\r\n ISSDK DIFF-P Sensor Example !\r\n");
134 
135  /*! Initialize INT1_DIFF_P pin used by FRDM board */
136  gpioDriver->pin_init(&DIFF_P_INT1, GPIO_DIRECTION_IN, NULL, &diff_p_isr, NULL);
137 
138  /*! Initialize RGB LED pin used by FRDM board */
139  gpioDriver->pin_init(&GREEN_LED, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
140 
141  /*! Initialize the I2C driver. */
142  status = I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
143  if (ARM_DRIVER_OK != status)
144  {
145  PRINTF("\r\n I2C Initialization Failed.\r\n");
146  return -1;
147  }
148 
149  /*! Set the I2C Power mode. */
150  status = I2Cdrv->PowerControl(ARM_POWER_FULL);
151  if (ARM_DRIVER_OK != status)
152  {
153  PRINTF("\r\n I2C Power Mode setting Failed.\r\n");
154  return -1;
155  }
156 
157  /*! Set the I2C bus speed. */
158  status = I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
159  if (ARM_DRIVER_OK != status)
160  {
161  PRINTF("\r\n I2C Control Mode setting Failed.\r\n");
162  return -1;
163  }
164 
165  do
166  { /*! Initialize DIFF_P sensor driver. */
169  if (SENSOR_ERROR_NONE == status)
170  {
172  conversionFactor = NPS3000VV_PRESSURE_DIV_FACTOR;
173  break;
174  }
177  if (SENSOR_ERROR_NONE == status)
178  {
180  conversionFactor = NPS3001DV_PRESSURE_DIV_FACTOR;
181  break;
182  }
185  if (SENSOR_ERROR_NONE == status)
186  {
188  conversionFactor = NPS3002VV_PRESSURE_DIV_FACTOR;
189  break;
190  }
193  if (SENSOR_ERROR_NONE == status)
194  {
196  conversionFactor = NPS3005DV_PRESSURE_DIV_FACTOR;
197  break;
198  }
199 
200  PRINTF("\r\n Sensor Initialization Failed.\r\n");
201  return -1;
202  } while (false);
203  PRINTF("\r\n Initiliazed Sensor with WHO_AM_I = [0x%X].\r\n", whoAmI);
204 
205  /*! Set the task to be executed while waiting for I2C transactions to complete. */
207 
208  /*! We do not need to call DIFF_P_I2C_Configure() in this case as we are going to read samples on demand.
209  * Instead we directly write register settings for One-Shot Mode... */
210  status = Sensor_I2C_Write(diff_pDriver.pCommDrv, &diff_pDriver.deviceInfo, diff_pDriver.slaveAddress,
211  cDiffPConfigOneShot);
212  if (SENSOR_ERROR_NONE != status)
213  {
214  PRINTF("\r\nDIFF_P now configured for OST Mode and entering data read loop...\r\n");
215  return -1;
216  }
217  PRINTF("\r\n Successfully Applied Sensor Interrupt Configuration.\r\n");
218 
219  for (;;) /* Forever loop */
220  { /* Trigger acquisition of One Sample. */
221  status = Sensor_I2C_Write(diff_pDriver.pCommDrv, &diff_pDriver.deviceInfo, diff_pDriver.slaveAddress,
222  cDiffPTriggerOneShot);
223  if (ARM_DRIVER_OK != status)
224  {
225  PRINTF("\r\nWrite Failed.\r\n");
226  return SENSOR_ERROR_WRITE;
227  }
228 
229  while (false == diff_pInterrupt)
230  { /* Loop, until new sample is not available. */
232  }
233 
234  /*! Clear the data ready flag, it will be set again by the ISR. */
235  diff_pInterrupt = false;
236  gpioDriver->toggle_pin(&GREEN_LED);
237 
238  do
239  { /* Read INT_STATUS register */
240  status = DIFF_P_I2C_ReadData(&diff_pDriver, cDiffPStatus, &dataReady);
241  if (ARM_DRIVER_OK != status)
242  {
243  PRINTF("\r\n Read Failed. \r\n");
244  return -1;
245  }
246 
247  /*! Check for both data ready bits from the DIFF_P. */
250  { /* Loop, if new samples are not available. */
251  continue;
252  }
253  } while (false);
254 
255  /*! Read new raw sensor data from the DIFF_P. */
256  status = DIFF_P_I2C_ReadData(&diff_pDriver, cDiffPOutputNormal, data);
257  if (ARM_DRIVER_OK != status)
258  {
259  PRINTF("\r\n Read Failed. \r\n");
260  return -1;
261  }
262 
263  /*! Explicitly clear the Status Bits. */
264  status = Sensor_I2C_Write(diff_pDriver.pCommDrv, &diff_pDriver.deviceInfo, diff_pDriver.slaveAddress,
265  cDiffPClearStatusBits);
266  if (ARM_DRIVER_OK != status)
267  {
268  PRINTF("\r\n Write Failed. \r\n");
269  return -1;
270  }
271 
272  /*! Process the sample and convert the raw sensor data. */
273  rawData.pressure = ((int16_t)(data[1]) << 8) | data[0];
274  pressureInPascals = (1.0 * rawData.pressure) / conversionFactor;
275 
276  rawData.temperature = (int8_t)(data[2]);
277  tempInDegrees = rawData.temperature;
278 
279  PRINTF("\r\n Pressure = %0.3f Pa", pressureInPascals);
280  PRINTF("\r\n Temperature = %d degC\r\n", tempInDegrees);
281  ASK_USER_TO_RESUME(50); /* Ask for user input after processing 50 samples. */
282  }
283 }
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
void diff_p_isr(void *pUserData)
This is the Sensor Data Ready ISR implementation.
#define __END_WRITE_DATA__
Definition: sensor_drv.h:71
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]
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
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 registerreadlist_t cDiffPOutputNormal[]
Address and size of Raw Pressure+Temperature Data in Normal Mode.
#define DIFF_P_DATA_SIZE
#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
const registerwritelist_t cDiffPTriggerOneShot[]
Register settings for Triggring One-Shot Sampling.
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 NPS3001DV_PRESSURE_DIV_FACTOR
#define DIFF_P_INT1
gpioHandleKSDK_t GREEN_LED
Definition: frdm_k64f.c:214
#define ASK_USER_TO_RESUME(x)
Definition: frdm_k64f.h:132
#define NPS3005DV_PRESSURE_DIV_FACTOR
const registerwritelist_t cDiffPConfigOneShot[]
Register settings for OST Mode.
const registerreadlist_t cDiffPStatus[]
Address of Status Register.
void BOARD_SystickEnable(void)
Function to enable systicks framework.
Definition: systick_utils.c:70
#define I2C_S_DEVICE_INDEX
Definition: issdk_hal.h:61
int main(void)
Main function.
#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
#define DIFF_P_CTRL_REG1_OSR_OSR2048
Definition: diff_p.h:736
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_CTRL_REG1_OST_MASK
Definition: diff_p.h:703
volatile bool diff_pInterrupt
#define DIFF_P_CTRL_REG1_OST_ONESHOT
Definition: diff_p.h:718
#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 DIFF_P_INT_STATUS_0_TDR_DRDY
Definition: diff_p.h:170
const registerwritelist_t cDiffPClearStatusBits[]
Register settings for Clearing Pressure and Temperature Data Ready Bits.
#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