ISSDK  1.7
IoT Sensing Software Development Kit
diff_p_demo.c
Go to the documentation of this file.
1 /*
2  * The Clear BSD License
3  * Copyright (c) 2017, Freescale Semiconductor, Inc.
4  * Copyright 2017 NXP
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without modification,
8  * are permitted (subject to the limitations in the disclaimer below) provided
9  * that the following conditions are met:
10  *
11  * o Redistributions of source code must retain the above copyright notice, this list
12  * of conditions and the following disclaimer.
13  *
14  * o Redistributions in binary form must reproduce the above copyright notice, this
15  * list of conditions and the following disclaimer in the documentation and/or
16  * other materials provided with the distribution.
17  *
18  * o Neither the name of the copyright holder nor the names of its
19  * contributors may be used to endorse or promote products derived from this
20  * software without specific prior written permission.
21  *
22  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
27  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /**
36  * @file diff_p_demo.c
37  * @brief The diff_p_demo.c file implements the ISSDK DIFF_P sensor driver
38  * example demonstration with Interrupt mode.
39  */
40 
41 //-----------------------------------------------------------------------
42 // SDK Includes
43 //-----------------------------------------------------------------------
44 #include "board.h"
45 #include "pin_mux.h"
46 #include "clock_config.h"
47 
48 //-----------------------------------------------------------------------
49 // CMSIS Includes
50 //-----------------------------------------------------------------------
51 #include "Driver_I2C.h"
52 #include "Driver_USART.h"
53 
54 //-----------------------------------------------------------------------
55 // ISSDK Includes
56 //-----------------------------------------------------------------------
57 #include "issdk_hal.h"
58 #include "gpio_driver.h"
59 #include "diff_p_drv.h"
60 #include "host_io_uart.h"
61 #include "systick_utils.h"
62 #include "auto_detection_service.h"
63 
64 //-----------------------------------------------------------------------
65 // Macros
66 //-----------------------------------------------------------------------
67 #define DIFF_P_DATA_SIZE (3) /* 2 byte Pressure and 1 byte Temperature. */
68 #define DIFF_P_STREAM_DATA_SIZE (7) /* 7 byte Data */
69 
70 /*! @brief Unique Name for this application which should match the target GUI pkg name. */
71 #define APPLICATION_NAME "NPS300x Differential Pressure Demo"
72 /*! @brief Version to distinguish between instances the same application based on target Shield and updates. */
73 #define APPLICATION_VERSION "2.5"
74 
75 //-----------------------------------------------------------------------
76 // Constants
77 //-----------------------------------------------------------------------
78 /*! @brief Register settings for Normal (non buffered) mode. */
85 
86 /*! @brief Register settings for Clearing Pressure and Temperature Data Ready Bits. */
89 
90 /*! @brief Address of Status Register. */
92 
93 /*! @brief Address and size of Raw Pressure+Temperature Data in Normal Mode. */
96 
97 //-----------------------------------------------------------------------
98 // Global Variables
99 //-----------------------------------------------------------------------
102 volatile bool bStreamingEnabled = false, bDiffPDataReady = false, bDiffPReady = false;
103 uint8_t gStreamID; /* The auto assigned Stream ID. */
106 
107 //-----------------------------------------------------------------------
108 // Functions
109 //-----------------------------------------------------------------------
110 /* This is the Sensor Data Ready ISR implementation.*/
111 void diffp_int_data_ready_callback(void *pUserData)
112 { /*! @brief Set flag to indicate Sensor has signalled data ready. */
113  bDiffPDataReady = true;
114 }
115 
116 /* Handler for Device Info and Streaming Control Commands. */
118  uint8_t tag, uint8_t *hostCommand, uint8_t *hostResponse, size_t *hostMsgSize, size_t respBufferSize)
119 {
120  bool success = false;
121 
122  /* If it is Host requesting Device Info, send Board Name and Shield Name. */
123  if (tag == HOST_PRO_INT_DEV_TAG)
124  { /* Byte 1 : Payload - Length of APPLICATION_NAME (b)
125  * Bytes=b : Payload Application Name
126  * Byte b+1 : Payload - Length of BOARD_NAME (s)
127  * Bytes=s : Payload Board Name
128  * Byte b+s+2 : Payload - Length of SHIELD_NAME (v)
129  * Bytes=v : Payload Shield Name */
130 
131  size_t appNameLen = strlen(embAppName);
132  size_t boardNameLen = strlen(boardString);
133  size_t shieldNameLen = strlen(shieldString);
134 
135  if (respBufferSize >= boardNameLen + shieldNameLen + appNameLen + 3)
136  { /* We have sufficient buffer. */
137  *hostMsgSize = 0;
138  }
139  else
140  {
141  return false;
142  }
143 
144  hostResponse[*hostMsgSize] = appNameLen;
145  *hostMsgSize += 1;
146 
147  memcpy(hostResponse + *hostMsgSize, embAppName, appNameLen);
148  *hostMsgSize += appNameLen;
149 
150  hostResponse[*hostMsgSize] = boardNameLen;
151  *hostMsgSize += 1;
152 
153  memcpy(hostResponse + *hostMsgSize, boardString, boardNameLen);
154  *hostMsgSize += boardNameLen;
155 
156  hostResponse[*hostMsgSize] = shieldNameLen;
157  *hostMsgSize += 1;
158 
159  memcpy(hostResponse + *hostMsgSize, shieldString, shieldNameLen);
160  *hostMsgSize += shieldNameLen;
161 
162  return true;
163  }
164 
165  /* If it is Host sending Streaming Commands, take necessary actions. */
166  if ((tag == (HOST_PRO_INT_CMD_TAG | HOST_PRO_CMD_W_CFG_TAG)) &&
168  { /* Byte 1 : Payload - Operation Code (Start/Stop Operation Code)
169  * Byte 2 : Payload - Stream ID (Target Stream for carrying out operation) */
170  switch (hostCommand[0]) /* Execute desired operation (Start/Stop) on the requested Stream. */
171  {
172  case HOST_CMD_START:
173  if (hostCommand[1] == gStreamID && bDiffPReady && bStreamingEnabled == false)
174  {
176  bStreamingEnabled = true;
177  success = true;
178  }
179  break;
180  case HOST_CMD_STOP:
181  if (hostCommand[1] == gStreamID && bDiffPReady && bStreamingEnabled == true)
182  {
183  pGpioDriver->clr_pin(&GREEN_LED);
184  bStreamingEnabled = false;
185  success = true;
186  }
187  break;
188  default:
189  break;
190  }
191  *hostMsgSize = 0; /* Zero payload in response. */
192  }
193 
194  return success;
195 }
196 
197 /*!
198  * @brief Main function
199  */
200 int main(void)
201 {
202  int32_t status;
204 
205  diff_p_i2c_sensorhandle_t diffpDriver;
207 
208  ARM_DRIVER_I2C *pI2cDriver = &I2C_S_DRIVER;
209  ARM_DRIVER_USART *pUartDriver = &HOST_S_DRIVER;
210 
211  /*! Initialize the MCU hardware. */
214 
215  /* Create the Short Application Name String for ADS. */
216  sprintf(embAppName, "%s:%s", APPLICATION_NAME, APPLICATION_VERSION);
217 
218  /* Run ADS. */
220 
221  /* Create the Full Application Name String for Device Info Response. */
223 
224  /*! Initialize INT1 DIFF_P pin used by FRDM board */
226 
227  /*! Initialize GREEN LED pin used by FRDM board */
228  pGpioDriver->pin_init(&GREEN_LED, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
229 
230  /*! Initialize the I2C driver. */
231  status = pI2cDriver->Initialize(I2C_S_SIGNAL_EVENT);
232  if (ARM_DRIVER_OK != status)
233  {
234  return -1;
235  }
236 
237  /*! Set the I2C Power mode. */
238  status = pI2cDriver->PowerControl(ARM_POWER_FULL);
239  if (ARM_DRIVER_OK != status)
240  {
241  return -1;
242  }
243 
244  /*! Set the I2C bus speed. */
245  status = pI2cDriver->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
246  if (ARM_DRIVER_OK != status)
247  {
248  return -1;
249  }
250 
251  /*! Initialize the UART driver. */
252  status = pUartDriver->Initialize(HOST_S_SIGNAL_EVENT);
253  if (ARM_DRIVER_OK != status)
254  {
255  return -1;
256  }
257 
258  /*! Set the UART Power mode. */
259  status = pUartDriver->PowerControl(ARM_POWER_FULL);
260  if (ARM_DRIVER_OK != status)
261  {
262  return -1;
263  }
264 
265  /*! Set UART Baud Rate. */
266  status = pUartDriver->Control(ARM_USART_MODE_ASYNCHRONOUS, BOARD_DEBUG_UART_BAUDRATE);
267  if (ARM_DRIVER_OK != status)
268  {
269  return -1;
270  }
271 
272  do
273  { /*! Initialize DIFF_P sensor driver. */
276  if (SENSOR_ERROR_NONE == status)
277  {
278  bDiffPReady = true;
279  break;
280  }
283  if (SENSOR_ERROR_NONE == status)
284  {
285  bDiffPReady = true;
286  break;
287  }
290  if (SENSOR_ERROR_NONE == status)
291  {
292  bDiffPReady = true;
293  break;
294  }
297  if (SENSOR_ERROR_NONE == status)
298  {
299  bDiffPReady = true;
300  break;
301  }
302  } while (false);
303 
304  if (bDiffPReady)
305  { /*! Configure the DIFF_P sensor. */
306  status = DIFF_P_I2C_Configure(&diffpDriver, cDiffPConfigNormal);
307  if (SENSOR_ERROR_NONE != status)
308  {
309  bDiffPReady = false;
310  }
311  }
312 
313  /*! Initialize streaming and assign a Stream ID. */
314  gStreamID = Host_IO_Init(pUartDriver, (void *)diffpDriver.pCommDrv, &diffpDriver.deviceInfo, NULL, DIFF_P_I2C_ADDR);
315  /* Confirm if a valid Stream ID has been allocated for this stream. */
316  if (0 == gStreamID)
317  {
318  bDiffPReady = false;
319  }
320  else
321  {
322  /*! Populate streaming header. */
324  pGpioDriver->clr_pin(&GREEN_LED); /* Set LED to indicate application is ready. */
325  }
326 
327  for (;;) /* Forever loop */
328  { /* Call UART Non-Blocking Receive. */
330 
331  /* Process packets only if streaming has been enabled by Host and ISR is available.
332  * In ISR Mode we do not need to check Data Ready Register.
333  * The receipt of interrupt will indicate data is ready. */
334  if (false == bStreamingEnabled || false == bDiffPDataReady)
335  {
336  SMC_SetPowerModeWait(SMC); /* Power save, wait if nothing to do. */
337  continue;
338  }
339  else
340  { /*! Clear the data ready flag, it will be set again by the ISR. */
341  bDiffPDataReady = false;
342  pGpioDriver->toggle_pin(&GREEN_LED);
343  }
344 
345  do
346  { /* Read INT_STATUS register */
347  status = DIFF_P_I2C_ReadData(&diffpDriver, cDiffPStatus, &dataReady);
348  if (ARM_DRIVER_OK != status)
349  {
350  break;
351  }
352 
353  /*! Check for both data ready bits from the DIFF_P. */
356  { /* Loop, if new samples are not available. */
357  continue;
358  }
359 
360  /*! If samples available, explicitly clear the Status Bits. */
361  status = Sensor_I2C_Write(diffpDriver.pCommDrv, &diffpDriver.deviceInfo, diffpDriver.slaveAddress,
362  cDiffPClearStatusBits);
363  if (ARM_DRIVER_OK != status)
364  {
365  break;
366  }
367  } while (false);
368 
369  /*! Read new raw sensor data from the DIFF_P. */
370  status = DIFF_P_I2C_ReadData(&diffpDriver, cDiffPOutputNormal, data);
371  if (ARM_DRIVER_OK != status)
372  { /* Loop, if sample read failed. */
373  return -1;
374  }
375 
376  /* Update timestamp from Systick framework. */
378 
379  /*! Process the sample and convert the raw sensor data. */
380  rawData.pressure = ((int16_t)(data[1]) << 8) | data[0];
381  rawData.temperature = (int8_t)(data[2]);
382 
383  /* Copy Raw samples to Streaming Buffer. */
384  memcpy(streamingPacket + STREAMING_HEADER_LEN, &rawData, DIFF_P_STREAM_DATA_SIZE);
385  /* Send streaming packet to Host. */
386  Host_IO_Send(streamingPacket, sizeof(streamingPacket), HOST_FORMAT_HDLC);
387  }
388 }
This defines the sensor specific information for I2C.
Definition: diff_p_drv.h:70
uint32_t BOARD_SystickElapsedTime_us(int32_t *pStart)
Function to compute the Elapsed Time.
Definition: systick_utils.c:99
uint8_t gStreamID
Definition: diff_p_demo.c:103
#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 HOST_PRO_CMD_W_CFG_TAG
Definition: host_io_uart.h:89
#define DIFF_P_CTRL_REG2_ODR_MASK
Definition: diff_p.h:774
#define HOST_S_SIGNAL_EVENT
Definition: frdm_k64f.h:120
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 Host_IO_Receive(host_cmd_proc_fn_t process_host_command, uint8_t encoding)
Definition: host_io_uart.c:233
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 DIFF_P_CTRL_REG2_ODR_ODR0P781
Definition: diff_p.h:802
#define BOARD_BootClockRUN
Definition: clock_config.h:45
#define DIFF_P_INT_STATUS_0_PDR_DRDY
Definition: diff_p.h:171
#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
#define SHIELD_NAME
volatile bool bStreamingEnabled
Definition: diff_p_demo.c:102
#define ADS_MAX_STRING_LENGTH
char boardString[ADS_MAX_STRING_LENGTH]
Definition: diff_p_demo.c:100
#define HOST_PRO_INT_DEV_TAG
Definition: host_io_uart.h:85
uint8_t streamingPacket[STREAMING_HEADER_LEN+FXLS8962_STREAM_DATA_SIZE]
#define APPLICATION_VERSION
Version to distinguish between instances the same application based on target Shield and updates...
Definition: diff_p_demo.c:73
#define HOST_PRO_INT_CMD_TAG
Bit aligned values for Host Protocol Interface IDs (Bits 5-6).
Definition: host_io_uart.h:83
int main(void)
Main function.
Definition: diff_p_demo.c:200
#define DIFF_P_NPS3001DV_WHOAMI_VALUE
Definition: diff_p.h:97
#define APPLICATION_NAME
Unique Name for this application which should match the target GUI pkg name.
Definition: diff_p_demo.c:71
#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.
const registerreadlist_t cDiffPOutputNormal[]
Address and size of Raw Pressure+Temperature Data in Normal Mode.
Definition: diff_p_demo.c:94
#define DIFF_P_INT_STATUS_0_PDR_MASK
Definition: diff_p.h:151
GENERIC_DRIVER_GPIO * pGpioDriver
Definition: diff_p_demo.c:105
#define DIFF_P_DATA_SIZE
Definition: diff_p_demo.c:67
#define DIFF_P_INT1
gpioHandleKSDK_t GREEN_LED
Definition: frdm_k64f.c:214
#define DIFF_P_STREAM_DATA_SIZE
Definition: diff_p_demo.c:68
void Host_IO_Add_ISO_Header(uint8_t streamID, uint8_t *pStreamingPacket, size_t sizePayload)
Definition: host_io_uart.c:112
void Host_IO_Send(uint8_t *pMsg, size_t size, uint8_t encoding)
Definition: host_io_uart.c:162
void(* clr_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:73
char embAppName[ADS_MAX_STRING_LENGTH]
Definition: diff_p_demo.c:101
void BOARD_SystickEnable(void)
Function to enable systicks framework.
Definition: systick_utils.c:70
volatile bool bDiffPReady
Definition: diff_p_demo.c:102
#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
const registerwritelist_t cDiffPConfigNormal[]
Register settings for Normal (non buffered) mode.
Definition: diff_p_demo.c:79
#define BOARD_DEBUG_UART_BAUDRATE
Definition: board.h:57
#define DIFF_P_CTRL_REG3_IPOL1_ACTIVE_HIGH
Definition: diff_p.h:862
Access structure of the GPIO Driver.
Definition: Driver_GPIO.h:64
void BOARD_SystickStart(int32_t *pStart)
Function to Record the Start systick.
Definition: systick_utils.c:79
ARM_DRIVER_I2C * pCommDrv
Definition: diff_p_drv.h:73
volatile bool bDiffPDataReady
Definition: diff_p_demo.c:102
void diffp_int_data_ready_callback(void *pUserData)
Definition: diff_p_demo.c:111
ARM Systick Utilities.
void BOARD_RunADS(const char *appName, char *boardString, char *shieldString, size_t bufferLength)
The function to register Application Name and initialte ADS.
const registerwritelist_t cDiffPClearStatusBits[]
Register settings for Clearing Pressure and Temperature Data Ready Bits.
Definition: diff_p_demo.c:87
#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
uint8_t Host_IO_Init(ARM_DRIVER_USART *pDrv, void *pBus, void *pDevInfo, void *spiSlaveParams, uint16_t slaveAddress)
Definition: host_io_uart.c:126
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
The host_io_uart.h file contains the Host Protocol interface definitions and configuration.
#define STREAMING_HEADER_LEN
Definition: host_io_uart.h:51
#define DIFF_P_NPS3005DV_WHOAMI_VALUE
Definition: diff_p.h:99
bool process_host_command(uint8_t tag, uint8_t *hostCommand, uint8_t *hostResponse, size_t *hostMsgSize, size_t respBufferSize)
Definition: diff_p_demo.c:117
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
char shieldString[ADS_MAX_STRING_LENGTH]
Definition: diff_p_demo.c:100
#define DIFF_P_INT_STATUS_0_TDR_DRDY
Definition: diff_p.h:170
int32_t gSystick
Definition: diff_p_demo.c:104
#define I2C_S_SIGNAL_EVENT
Definition: issdk_hal.h:60
#define HOST_S_DRIVER
Definition: frdm_k64f.h:119
#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
const registerreadlist_t cDiffPStatus[]
Address of Status Register.
Definition: diff_p_demo.c:91