ISSDK  1.7
IoT Sensing Software Development Kit
diff_p_normal.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_normal.c
36  * @brief The diff_p_normal.c file implements the ISSDK DIFF_P sensor driver
37  * example demonstration with polling 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 // Constants
65 //-----------------------------------------------------------------------
66 /*! @brief Register settings for Normal Mode. */
71 
72 /*! @brief Register settings for Clearing Pressure and Temperature Data Ready Bits. */
75 
76 /*! @brief Address of Status Register. */
78 
79 /*! @brief Address and size of Raw Pressure+Temperature Data in Normal Mode. */
82 
83 //-----------------------------------------------------------------------
84 // Functions
85 //-----------------------------------------------------------------------
86 /*!
87  * @brief Main function
88  */
89 int main(void)
90 {
92  int8_t tempInDegrees;
93  float pressureInPascals;
95  uint8_t whoAmI, conversionFactor;
96  uint8_t dataReady, data[DIFF_P_DATA_SIZE];
97 
98  diff_p_i2c_sensorhandle_t diff_pDriver;
99  ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER;
100 
101  BOARD_InitPins();
105 
106  PRINTF("\r\n ISSDK DIFF-P Sensor Example !\r\n");
107 
108  /*! Initialize the I2C driver. */
109  status = I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
110  if (ARM_DRIVER_OK != status)
111  {
112  PRINTF("\r\n I2C Initialization Failed.\r\n");
113  return -1;
114  }
115 
116  /*! Set the I2C Power mode. */
117  status = I2Cdrv->PowerControl(ARM_POWER_FULL);
118  if (ARM_DRIVER_OK != status)
119  {
120  PRINTF("\r\n I2C Power Mode setting Failed.\r\n");
121  return -1;
122  }
123 
124  /*! Set the I2C bus speed. */
125  status = I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
126  if (ARM_DRIVER_OK != status)
127  {
128  PRINTF("\r\n I2C Control Mode setting Failed.\r\n");
129  return -1;
130  }
131 
132  do
133  { /*! Initialize DIFF_P sensor driver. */
136  if (SENSOR_ERROR_NONE == status)
137  {
139  conversionFactor = NPS3000VV_PRESSURE_DIV_FACTOR;
140  break;
141  }
144  if (SENSOR_ERROR_NONE == status)
145  {
147  conversionFactor = NPS3001DV_PRESSURE_DIV_FACTOR;
148  break;
149  }
152  if (SENSOR_ERROR_NONE == status)
153  {
155  conversionFactor = NPS3002VV_PRESSURE_DIV_FACTOR;
156  break;
157  }
160  if (SENSOR_ERROR_NONE == status)
161  {
163  conversionFactor = NPS3005DV_PRESSURE_DIV_FACTOR;
164  break;
165  }
166 
167  PRINTF("\r\n Sensor Initialization Failed.\r\n");
168  return -1;
169  } while (false);
170  PRINTF("\r\n Initiliazed Sensor with WHO_AM_I = [0x%X].\r\n", whoAmI);
171 
172  /*! Set the task to be executed while waiting for I2C transactions to complete. */
174 
175  /*! Configure the DIFF_P sensor. */
176  status = DIFF_P_I2C_Configure(&diff_pDriver, cDiffPConfigNormal);
177  if (SENSOR_ERROR_NONE != status)
178  {
179  PRINTF("\r\nDIFF_P now active and entering data read loop...\r\n");
180  return -1;
181  }
182  PRINTF("\r\n Successfully Applied Sensor Poll Configuration.\r\n");
183 
184  for (;;) /* Forever loop */
185  {
186  /* Read INT_STATUS register */
187  status = DIFF_P_I2C_ReadData(&diff_pDriver, cDiffPStatus, &dataReady);
188  if (ARM_DRIVER_OK != status)
189  {
190  PRINTF("\r\n Read Failed. \r\n");
191  return -1;
192  }
193 
194  /*! Check for both data ready bits from the DIFF_P. */
197  { /* Loop, if new samples are not available. */
198  continue;
199  }
200 
201  /*! Read new raw sensor data from the DIFF_P. */
202  status = DIFF_P_I2C_ReadData(&diff_pDriver, cDiffPOutputNormal, data);
203  if (ARM_DRIVER_OK != status)
204  {
205  PRINTF("\r\n Read Failed. \r\n");
206  return -1;
207  }
208 
209  /*! Explicitly clear the Status Bits. */
210  status = Sensor_I2C_Write(diff_pDriver.pCommDrv, &diff_pDriver.deviceInfo, diff_pDriver.slaveAddress,
211  cDiffPClearStatusBits);
212  if (ARM_DRIVER_OK != status)
213  {
214  PRINTF("\r\n Write Failed. \r\n");
215  return -1;
216  }
217 
218  /*! Process the sample and convert the raw sensor data. */
219  rawData.pressure = ((int16_t)(data[1]) << 8) | data[0];
220  pressureInPascals = (1.0 * rawData.pressure) / conversionFactor;
221 
222  rawData.temperature = (int8_t)(data[2]);
223  tempInDegrees = rawData.temperature;
224 
225  PRINTF("\r\n Pressure = %0.3f Pa", pressureInPascals);
226  PRINTF("\r\n Temperature = %d degC\r\n", tempInDegrees);
227  ASK_USER_TO_RESUME(50); /* Ask for user input after processing 50 samples. */
228  }
229 }
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
const registerwritelist_t cDiffPConfigNormal[]
Register settings for Normal Mode.
Definition: diff_p_normal.c:67
#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
#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
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
int main(void)
Main function.
Definition: diff_p_normal.c:89
#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
#define DIFF_P_DATA_SIZE
Definition: diff_p_normal.c:61
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
const registerreadlist_t cDiffPOutputNormal[]
Address and size of Raw Pressure+Temperature Data in Normal Mode.
Definition: diff_p_normal.c:80
#define ASK_USER_TO_RESUME(x)
Definition: frdm_k64f.h:132
#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_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
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
const registerreadlist_t cDiffPStatus[]
Address of Status Register.
Definition: diff_p_normal.c:77
ARM_DRIVER_I2C * pCommDrv
Definition: diff_p_drv.h:73
ARM Systick Utilities.
#define NPS3000VV_PRESSURE_DIV_FACTOR
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
const registerwritelist_t cDiffPClearStatusBits[]
Register settings for Clearing Pressure and Temperature Data Ready Bits.
Definition: diff_p_normal.c:73
#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
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
#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