ISSDK  1.7
IoT Sensing Software Development Kit
diff_p_spi.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_spi.c
36  * @brief The diff_p_spi.c file implements the ISSDK DIFF_P sensor driver
37  * example demonstration with SPI 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_SPI.h"
52 
53 /* ISSDK Includes */
54 #include "issdk_hal.h"
55 #include "diff_p_drv.h"
56 #include "gpio_driver.h"
57 #include "systick_utils.h"
58 
59 //-----------------------------------------------------------------------
60 // Macros
61 //-----------------------------------------------------------------------
62 #define DIFF_P_DATA_SIZE (3) /* 2 byte Pressure and 1 byte Temperature. */
63 
64 //-----------------------------------------------------------------------
65 // Constants
66 //-----------------------------------------------------------------------
67 /*! @brief Register settings for Normal (non buffered) mode. */
72 
73 /*! @brief Register settings for Clearing Pressure and Temperature Data Ready Bits. */
76 
77 /*! @brief Address of Status Register. */
79 
80 /*! @brief Address and size of Raw Pressure+Temperature Data in Normal Mode. */
83 
84 //-----------------------------------------------------------------------
85 // Functions
86 //-----------------------------------------------------------------------
87 /*!
88  * @brief Main function
89  */
90 int main(void)
91 {
93  int8_t tempInDegrees;
94  float pressureInPascals;
96  uint8_t whoAmI, conversionFactor;
97  uint8_t dataReady, data[DIFF_P_DATA_SIZE];
98 
99  diff_p_spi_sensorhandle_t diff_pDriver;
100  ARM_DRIVER_SPI *SPIdrv = &SPI_S_DRIVER;
101 
102  BOARD_InitPins();
106 
107  PRINTF("\r\n ISSDK DIFF-P Sensor SPI Example !\r\n");
108 
109  /*! Initialize the I2C driver. */
110  status = SPIdrv->Initialize(SPI_S_SIGNAL_EVENT);
111  if (ARM_DRIVER_OK != status)
112  {
113  PRINTF("\r\n SPI Initialization Failed.\r\n");
114  return -1;
115  }
116 
117  /*! Set the I2C Power mode. */
118  status = SPIdrv->PowerControl(ARM_POWER_FULL);
119  if (ARM_DRIVER_OK != status)
120  {
121  PRINTF("\r\n SPI Power Mode setting Failed.\r\n");
122  return -1;
123  }
124 
125  /*! Set the I2C bus speed. */
126  status = SPIdrv->Control(ARM_SPI_MODE_MASTER | ARM_SPI_CPOL1_CPHA0, SPI_S_BAUDRATE);
127  if (ARM_DRIVER_OK != status)
128  {
129  PRINTF("\r\n SPI Control Mode setting Failed\r\n");
130  return -1;
131  }
132 
133  do
134  { /*! Initialize DIFF_P sensor driver. */
137  if (SENSOR_ERROR_NONE == status)
138  {
140  conversionFactor = NPS3000VV_PRESSURE_DIV_FACTOR;
141  break;
142  }
145  if (SENSOR_ERROR_NONE == status)
146  {
148  conversionFactor = NPS3001DV_PRESSURE_DIV_FACTOR;
149  break;
150  }
153  if (SENSOR_ERROR_NONE == status)
154  {
156  conversionFactor = NPS3002VV_PRESSURE_DIV_FACTOR;
157  break;
158  }
161  if (SENSOR_ERROR_NONE == status)
162  {
164  conversionFactor = NPS3005DV_PRESSURE_DIV_FACTOR;
165  break;
166  }
167 
168  PRINTF("\r\n Sensor Initialization Failed.\r\n");
169  return -1;
170  } while (false);
171  PRINTF("\r\n Initiliazed Sensor with WHO_AM_I = [0x%X].\r\n", whoAmI);
172 
173  /*! Set the task to be executed while waiting for SPI transactions to complete. */
175 
176  /*! Configure the DIFF_P sensor. */
177  status = DIFF_P_SPI_Configure(&diff_pDriver, cDiffPConfigNormal);
178  if (SENSOR_ERROR_NONE != status)
179  {
180  PRINTF("\r\nDIFF_P now active and entering data read loop...\r\n");
181  return -1;
182  }
183  PRINTF("\r\n Successfully Applied Sensor Poll Configuration.\r\n");
184 
185  for (;;) /* Forever loop */
186  {
187  /* Read INT_STATUS register */
188  status = DIFF_P_SPI_ReadData(&diff_pDriver, cDiffPStatus, &dataReady);
189  if (ARM_DRIVER_OK != status)
190  {
191  PRINTF("\r\n Read Failed. \r\n");
192  return -1;
193  }
194 
195  /*! Check for both data ready bits from the DIFF_P. */
198  { /* Loop, if new samples are not available. */
199  continue;
200  }
201 
202  /*! Read new raw sensor data from the DIFF_P. */
203  status = DIFF_P_SPI_ReadData(&diff_pDriver, cDiffPOutputNormal, data);
204  if (ARM_DRIVER_OK != status)
205  {
206  PRINTF("\r\n Read Failed. \r\n");
207  return -1;
208  }
209 
210  /*! Explicitly clear the Status Bits. */
211  status = Sensor_SPI_Write(diff_pDriver.pCommDrv, &diff_pDriver.deviceInfo, &diff_pDriver.slaveParams,
212  cDiffPClearStatusBits);
213  if (ARM_DRIVER_OK != status)
214  {
215  PRINTF("\r\n Write Failed. \r\n");
216  return -1;
217  }
218 
219  /*! Process the sample and convert the raw sensor data. */
220  rawData.pressure = ((int16_t)(data[1]) << 8) | data[0];
221  pressureInPascals = (1.0 * rawData.pressure) / conversionFactor;
222 
223  rawData.temperature = (int8_t)(data[2]);
224  tempInDegrees = rawData.temperature;
225 
226  PRINTF("\r\n Pressure = %0.3f Pa", pressureInPascals);
227  PRINTF("\r\n Temperature = %d degC\r\n", tempInDegrees);
228  }
229 }
#define __END_WRITE_DATA__
Definition: sensor_drv.h:71
#define DIFF_P_CTRL_REG2_ODR_MASK
Definition: diff_p.h:774
#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
spiSlaveSpecificParams_t slaveParams
Definition: diff_p_drv.h:64
#define DIFF_P_CTRL_REG1_OSR_MASK
Definition: diff_p.h:709
uint8_t data[FXLS8962_DATA_SIZE]
int32_t status
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 registerwritelist_t cDiffPClearStatusBits[]
Register settings for Clearing Pressure and Temperature Data Ready Bits.
Definition: diff_p_spi.c:74
#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
The diff_p_drv.h file describes the DIFF_P driver interface and structures.
int32_t DIFF_P_SPI_Configure(diff_p_spi_sensorhandle_t *pSensorHandle, const registerwritelist_t *pRegWriteList)
The interface function to configure he sensor.
Definition: diff_p_drv.c:189
#define DIFF_P_INT_STATUS_0_PDR_MASK
Definition: diff_p.h:151
#define DIFF_P_DATA_SIZE
Definition: diff_p_spi.c:62
#define DIFF_P_CTRL_REG1_OSR_OSR512
Definition: diff_p.h:731
void DIFF_P_SPI_SetIdleTask(diff_p_spi_sensorhandle_t *pSensorHandle, registeridlefunction_t idleTask, void *userParam)
: The interface function to set the SPI Idle Task.
Definition: diff_p_drv.c:183
#define NPS3001DV_PRESSURE_DIV_FACTOR
registerDeviceInfo_t deviceInfo
Definition: diff_p_drv.h:61
#define NPS3005DV_PRESSURE_DIV_FACTOR
#define SPI_S_DEVICE_INDEX
Definition: frdm_k64f.h:115
ARM_DRIVER_SPI * SPIdrv
These values hold the ARM CMSIS Driver interface pointers.
void BOARD_SystickEnable(void)
Function to enable systicks framework.
Definition: systick_utils.c:70
int32_t DIFF_P_SPI_ReadData(diff_p_spi_sensorhandle_t *pSensorHandle, const registerreadlist_t *pReadList, uint8_t *pBuffer)
The interface function to read the sensor data.
Definition: diff_p_drv.c:232
This defines the sensor specific information for SPI.
Definition: diff_p_drv.h:59
#define DIFF_P_INT_STATUS_0_TDR_MASK
Definition: diff_p.h:148
#define DIFF_P_NPS3000VV_WHOAMI_VALUE
Definition: diff_p.h:96
int32_t Sensor_SPI_Write(ARM_DRIVER_SPI *pCommDrv, registerDeviceInfo_t *devInfo, void *pWriteParams, const registerwritelist_t *pRegWriteList)
Write register data to a sensor.
Definition: sensor_io_spi.c:97
int main(void)
Main function.
Definition: diff_p_spi.c:90
#define DIFF_P_CS
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_spi.c:78
ARM Systick Utilities.
#define NPS3000VV_PRESSURE_DIV_FACTOR
#define SPI_S_BAUDRATE
Transfer baudrate - 500k.
Definition: frdm_k64f.h:114
const registerwritelist_t cDiffPConfigNormal[]
Register settings for Normal (non buffered) mode.
Definition: diff_p_spi.c:68
fxls8962_acceldataUser_t rawData
#define DIFF_P_NPS3002VV_WHOAMI_VALUE
Definition: diff_p.h:98
#define SPI_S_SIGNAL_EVENT
Definition: frdm_k64f.h:116
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
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 registerreadlist_t cDiffPOutputNormal[]
Address and size of Raw Pressure+Temperature Data in Normal Mode.
Definition: diff_p_spi.c:81
int32_t DIFF_P_SPI_Initialize(diff_p_spi_sensorhandle_t *pSensorHandle, ARM_DRIVER_SPI *pBus, uint8_t index, void *pSlaveSelect, uint8_t whoAmi)
The interface function to initialize the sensor.
Definition: diff_p_drv.c:92
ARM_DRIVER_SPI * pCommDrv
Definition: diff_p_drv.h:62
#define SPI_S_DRIVER
Definition: frdm_k64f.h:113