ISSDK  1.8
IoT Sensing Software Development Kit
diff_p_demo.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, Freescale Semiconductor, Inc.
3  * Copyright 2017 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 /**
10  * @file diff_p_demo.c
11  * @brief The diff_p_demo.c file implements the ISSDK DIFF_P sensor driver
12  * example demonstration with Interrupt mode.
13  */
14 
15 //-----------------------------------------------------------------------
16 // SDK Includes
17 //-----------------------------------------------------------------------
18 #include "board.h"
19 #include "pin_mux.h"
20 #include "clock_config.h"
21 
22 //-----------------------------------------------------------------------
23 // CMSIS Includes
24 //-----------------------------------------------------------------------
25 #include "Driver_I2C.h"
26 #include "Driver_USART.h"
27 
28 //-----------------------------------------------------------------------
29 // ISSDK Includes
30 //-----------------------------------------------------------------------
31 #include "issdk_hal.h"
32 #include "gpio_driver.h"
33 #include "diff_p_drv.h"
34 #include "host_io_uart.h"
35 #include "systick_utils.h"
36 #include "auto_detection_service.h"
37 
38 //-----------------------------------------------------------------------
39 // Macros
40 //-----------------------------------------------------------------------
41 #define DIFF_P_DATA_SIZE (3) /* 2 byte Pressure and 1 byte Temperature. */
42 #define DIFF_P_STREAM_DATA_SIZE (7) /* 7 byte Data */
43 
44 /*! @brief Unique Name for this application which should match the target GUI pkg name. */
45 #define APPLICATION_NAME "NPS300x Differential Pressure Demo"
46 /*! @brief Version to distinguish between instances the same application based on target Shield and updates. */
47 #define APPLICATION_VERSION "2.5"
48 
49 //-----------------------------------------------------------------------
50 // Constants
51 //-----------------------------------------------------------------------
52 /*! @brief Register settings for Normal (non buffered) mode. */
54  {DIFF_P_CTRL_REG2, DIFF_P_CTRL_REG2_ODR_ODR6P25, DIFF_P_CTRL_REG2_ODR_MASK},
55  {DIFF_P_CTRL_REG1, DIFF_P_CTRL_REG1_OSR_OSR512, DIFF_P_CTRL_REG1_OSR_MASK},
56  {DIFF_P_INT_MASK0, DIFF_P_INT_MASK0_TDR_INT_EN | DIFF_P_INT_MASK0_PDR_INT_EN,
57  DIFF_P_INT_MASK0_TDR_MASK | DIFF_P_INT_MASK0_PDR_MASK},
58  {DIFF_P_CTRL_REG3, DIFF_P_CTRL_REG3_IPOL1_ACTIVE_HIGH, DIFF_P_CTRL_REG3_IPOL1_MASK},
60 
61 /*! @brief Register settings for Clearing Pressure and Temperature Data Ready Bits. */
63  {DIFF_P_INT_STATUS_0, 0x00, DIFF_P_INT_STATUS_0_TDR_MASK | DIFF_P_INT_STATUS_0_PDR_MASK}, __END_WRITE_DATA__};
64 
65 /*! @brief Address of Status Register. */
66 const registerreadlist_t cDiffPStatus[] = {{.readFrom = DIFF_P_INT_STATUS_0, .numBytes = 1}, __END_READ_DATA__};
67 
68 /*! @brief Address and size of Raw Pressure+Temperature Data in Normal Mode. */
69 const registerreadlist_t cDiffPOutputNormal[] = {{.readFrom = DIFF_P_OUT_P_LSB, .numBytes = DIFF_P_DATA_SIZE},
71 
72 //-----------------------------------------------------------------------
73 // Global Variables
74 //-----------------------------------------------------------------------
77 volatile bool bStreamingEnabled = false, bDiffPDataReady = false, bDiffPReady = false;
78 uint8_t gStreamID; /* The auto assigned Stream ID. */
81 
82 //-----------------------------------------------------------------------
83 // Functions
84 //-----------------------------------------------------------------------
85 /* This is the Sensor Data Ready ISR implementation.*/
86 void diffp_int_data_ready_callback(void *pUserData)
87 { /*! @brief Set flag to indicate Sensor has signalled data ready. */
88  bDiffPDataReady = true;
89 }
90 
91 /* Handler for Device Info and Streaming Control Commands. */
93  uint8_t tag, uint8_t *hostCommand, uint8_t *hostResponse, size_t *hostMsgSize, size_t respBufferSize)
94 {
95  bool success = false;
96 
97  /* If it is Host requesting Device Info, send Board Name and Shield Name. */
98  if (tag == HOST_PRO_INT_DEV_TAG)
99  { /* Byte 1 : Payload - Length of APPLICATION_NAME (b)
100  * Bytes=b : Payload Application Name
101  * Byte b+1 : Payload - Length of BOARD_NAME (s)
102  * Bytes=s : Payload Board Name
103  * Byte b+s+2 : Payload - Length of SHIELD_NAME (v)
104  * Bytes=v : Payload Shield Name */
105 
106  size_t appNameLen = strlen(embAppName);
107  size_t boardNameLen = strlen(boardString);
108  size_t shieldNameLen = strlen(shieldString);
109 
110  if (respBufferSize >= boardNameLen + shieldNameLen + appNameLen + 3)
111  { /* We have sufficient buffer. */
112  *hostMsgSize = 0;
113  }
114  else
115  {
116  return false;
117  }
118 
119  hostResponse[*hostMsgSize] = appNameLen;
120  *hostMsgSize += 1;
121 
122  memcpy(hostResponse + *hostMsgSize, embAppName, appNameLen);
123  *hostMsgSize += appNameLen;
124 
125  hostResponse[*hostMsgSize] = boardNameLen;
126  *hostMsgSize += 1;
127 
128  memcpy(hostResponse + *hostMsgSize, boardString, boardNameLen);
129  *hostMsgSize += boardNameLen;
130 
131  hostResponse[*hostMsgSize] = shieldNameLen;
132  *hostMsgSize += 1;
133 
134  memcpy(hostResponse + *hostMsgSize, shieldString, shieldNameLen);
135  *hostMsgSize += shieldNameLen;
136 
137  return true;
138  }
139 
140  /* If it is Host sending Streaming Commands, take necessary actions. */
141  if ((tag == (HOST_PRO_INT_CMD_TAG | HOST_PRO_CMD_W_CFG_TAG)) &&
143  { /* Byte 1 : Payload - Operation Code (Start/Stop Operation Code)
144  * Byte 2 : Payload - Stream ID (Target Stream for carrying out operation) */
145  switch (hostCommand[0]) /* Execute desired operation (Start/Stop) on the requested Stream. */
146  {
147  case HOST_CMD_START:
148  if (hostCommand[1] == gStreamID && bDiffPReady && bStreamingEnabled == false)
149  {
151  bStreamingEnabled = true;
152  success = true;
153  }
154  break;
155  case HOST_CMD_STOP:
156  if (hostCommand[1] == gStreamID && bDiffPReady && bStreamingEnabled == true)
157  {
158  pGpioDriver->clr_pin(&GREEN_LED);
159  bStreamingEnabled = false;
160  success = true;
161  }
162  break;
163  default:
164  break;
165  }
166  *hostMsgSize = 0; /* Zero payload in response. */
167  }
168 
169  return success;
170 }
171 
172 /*!
173  * @brief Main function
174  */
175 int main(void)
176 {
177  int32_t status;
179 
180  diff_p_i2c_sensorhandle_t diffpDriver;
181  diff_p_pressuredata_t rawData = {.timestamp = 0};
182 
183  ARM_DRIVER_I2C *pI2cDriver = &I2C_S_DRIVER;
184  ARM_DRIVER_USART *pUartDriver = &HOST_S_DRIVER;
185 
186  /*! Initialize the MCU hardware. */
189 
190  /* Create the Short Application Name String for ADS. */
191  sprintf(embAppName, "%s:%s", APPLICATION_NAME, APPLICATION_VERSION);
192 
193  /* Run ADS. */
195 
196  /* Create the Full Application Name String for Device Info Response. */
198 
199  /*! Initialize INT1 DIFF_P pin used by FRDM board */
201 
202  /*! Initialize GREEN LED pin used by FRDM board */
203  pGpioDriver->pin_init(&GREEN_LED, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
204 
205  /*! Initialize the I2C driver. */
206  status = pI2cDriver->Initialize(I2C_S_SIGNAL_EVENT);
207  if (ARM_DRIVER_OK != status)
208  {
209  return -1;
210  }
211 
212  /*! Set the I2C Power mode. */
213  status = pI2cDriver->PowerControl(ARM_POWER_FULL);
214  if (ARM_DRIVER_OK != status)
215  {
216  return -1;
217  }
218 
219  /*! Set the I2C bus speed. */
220  status = pI2cDriver->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
221  if (ARM_DRIVER_OK != status)
222  {
223  return -1;
224  }
225 
226  /*! Initialize the UART driver. */
227  status = pUartDriver->Initialize(HOST_S_SIGNAL_EVENT);
228  if (ARM_DRIVER_OK != status)
229  {
230  return -1;
231  }
232 
233  /*! Set the UART Power mode. */
234  status = pUartDriver->PowerControl(ARM_POWER_FULL);
235  if (ARM_DRIVER_OK != status)
236  {
237  return -1;
238  }
239 
240  /*! Set UART Baud Rate. */
241  status = pUartDriver->Control(ARM_USART_MODE_ASYNCHRONOUS, BOARD_DEBUG_UART_BAUDRATE);
242  if (ARM_DRIVER_OK != status)
243  {
244  return -1;
245  }
246 
247  do
248  { /*! Initialize DIFF_P sensor driver. */
249  status = DIFF_P_I2C_Initialize(&diffpDriver, &I2C_S_DRIVER, I2C_S_DEVICE_INDEX, DIFF_P_I2C_ADDR,
250  DIFF_P_NPS3000VV_WHOAMI_VALUE);
251  if (SENSOR_ERROR_NONE == status)
252  {
253  bDiffPReady = true;
254  break;
255  }
256  status = DIFF_P_I2C_Initialize(&diffpDriver, &I2C_S_DRIVER, I2C_S_DEVICE_INDEX, DIFF_P_I2C_ADDR,
257  DIFF_P_NPS3001DV_WHOAMI_VALUE);
258  if (SENSOR_ERROR_NONE == status)
259  {
260  bDiffPReady = true;
261  break;
262  }
263  status = DIFF_P_I2C_Initialize(&diffpDriver, &I2C_S_DRIVER, I2C_S_DEVICE_INDEX, DIFF_P_I2C_ADDR,
264  DIFF_P_NPS3002VV_WHOAMI_VALUE);
265  if (SENSOR_ERROR_NONE == status)
266  {
267  bDiffPReady = true;
268  break;
269  }
270  status = DIFF_P_I2C_Initialize(&diffpDriver, &I2C_S_DRIVER, I2C_S_DEVICE_INDEX, DIFF_P_I2C_ADDR,
271  DIFF_P_NPS3005DV_WHOAMI_VALUE);
272  if (SENSOR_ERROR_NONE == status)
273  {
274  bDiffPReady = true;
275  break;
276  }
277  } while (false);
278 
279  if (bDiffPReady)
280  { /*! Configure the DIFF_P sensor. */
281  status = DIFF_P_I2C_Configure(&diffpDriver, cDiffPConfigNormal);
282  if (SENSOR_ERROR_NONE != status)
283  {
284  bDiffPReady = false;
285  }
286  }
287 
288  /*! Initialize streaming and assign a Stream ID. */
289  gStreamID = Host_IO_Init(pUartDriver, (void *)diffpDriver.pCommDrv, &diffpDriver.deviceInfo, NULL, DIFF_P_I2C_ADDR);
290  /* Confirm if a valid Stream ID has been allocated for this stream. */
291  if (0 == gStreamID)
292  {
293  bDiffPReady = false;
294  }
295  else
296  {
297  /*! Populate streaming header. */
299  pGpioDriver->clr_pin(&GREEN_LED); /* Set LED to indicate application is ready. */
300  }
301 
302  for (;;) /* Forever loop */
303  { /* Call UART Non-Blocking Receive. */
305 
306  /* Process packets only if streaming has been enabled by Host and ISR is available.
307  * In ISR Mode we do not need to check Data Ready Register.
308  * The receipt of interrupt will indicate data is ready. */
309  if (false == bStreamingEnabled || false == bDiffPDataReady)
310  {
311  SMC_SetPowerModeWait(SMC); /* Power save, wait if nothing to do. */
312  continue;
313  }
314  else
315  { /*! Clear the data ready flag, it will be set again by the ISR. */
316  bDiffPDataReady = false;
317  pGpioDriver->toggle_pin(&GREEN_LED);
318  }
319 
320  do
321  { /* Read INT_STATUS register */
322  status = DIFF_P_I2C_ReadData(&diffpDriver, cDiffPStatus, &dataReady);
323  if (ARM_DRIVER_OK != status)
324  {
325  break;
326  }
327 
328  /*! Check for both data ready bits from the DIFF_P. */
329  if ((DIFF_P_INT_STATUS_0_PDR_DRDY | DIFF_P_INT_STATUS_0_TDR_DRDY) !=
330  (dataReady & (DIFF_P_INT_STATUS_0_PDR_MASK | DIFF_P_INT_STATUS_0_TDR_MASK)))
331  { /* Loop, if new samples are not available. */
332  continue;
333  }
334 
335  /*! If samples available, explicitly clear the Status Bits. */
336  status = Sensor_I2C_Write(diffpDriver.pCommDrv, &diffpDriver.deviceInfo, diffpDriver.slaveAddress,
337  cDiffPClearStatusBits);
338  if (ARM_DRIVER_OK != status)
339  {
340  break;
341  }
342  } while (false);
343 
344  /*! Read new raw sensor data from the DIFF_P. */
345  status = DIFF_P_I2C_ReadData(&diffpDriver, cDiffPOutputNormal, data);
346  if (ARM_DRIVER_OK != status)
347  { /* Loop, if sample read failed. */
348  return -1;
349  }
350 
351  /* Update timestamp from Systick framework. */
352  rawData.timestamp += BOARD_SystickElapsedTime_us(&gSystick);
353 
354  /*! Process the sample and convert the raw sensor data. */
355  rawData.pressure = ((int16_t)(data[1]) << 8) | data[0];
356  rawData.temperature = (int8_t)(data[2]);
357 
358  /* Copy Raw samples to Streaming Buffer. */
359  memcpy(streamingPacket + STREAMING_HEADER_LEN, &rawData, DIFF_P_STREAM_DATA_SIZE);
360  /* Send streaming packet to Host. */
361  Host_IO_Send(streamingPacket, sizeof(streamingPacket), HOST_FORMAT_HDLC);
362  }
363 }
uint8_t Host_IO_Init(ARM_DRIVER_USART *pDrv, void *pBus, void *pDevInfo, void *spiSlaveParams, uint16_t slaveAddress)
Definition: host_io_uart.c:100
void Host_IO_Receive(host_cmd_proc_fn_t process_host_command, uint8_t encoding)
Definition: host_io_uart.c:207
char embAppName[ADS_MAX_STRING_LENGTH]
Definition: diff_p_demo.c:76
This structure defines the Write command List.
Definition: sensor_drv.h:68
int32_t status
The host_io_uart.h file contains the Host Protocol interface definitions and configuration.
#define APPLICATION_VERSION
Version to distinguish between instances the same application based on target Shield and updates...
Definition: diff_p_demo.c:47
int32_t gSystick
Definition: diff_p_demo.c:79
char boardString[ADS_MAX_STRING_LENGTH]
Definition: diff_p_demo.c:75
#define HOST_PRO_CMD_W_CFG_TAG
Definition: host_io_uart.h:63
Access structure of the GPIO Driver.
Definition: Driver_GPIO.h:38
#define SMC
Definition: lpc54114.h:118
#define APPLICATION_NAME
Unique Name for this application which should match the target GUI pkg name.
Definition: diff_p_demo.c:45
#define __END_WRITE_DATA__
Definition: sensor_drv.h:45
volatile bool bDiffPReady
Definition: diff_p_demo.c:77
void(* clr_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:47
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
#define HOST_S_SIGNAL_EVENT
Definition: frdm_k64f.h:94
const registerwritelist_t cDiffPConfigNormal[]
Register settings for Normal (non buffered) mode.
Definition: diff_p_demo.c:53
#define DIFF_P_STREAM_DATA_SIZE
Definition: diff_p_demo.c:42
#define I2C_S_SIGNAL_EVENT
Definition: issdk_hal.h:34
volatile bool bStreamingEnabled
Definition: diff_p_demo.c:77
#define I2C_S_DRIVER
Definition: issdk_hal.h:33
void diffp_int_data_ready_callback(void *pUserData)
Definition: diff_p_demo.c:86
void Host_IO_Add_ISO_Header(uint8_t streamID, uint8_t *pStreamingPacket, size_t sizePayload)
Definition: host_io_uart.c:86
#define BOARD_DEBUG_UART_BAUDRATE
Definition: board.h:31
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:71
#define BOARD_BootClockRUN
Definition: clock_config.h:19
#define DIFF_P_DATA_SIZE
Definition: diff_p_demo.c:41
uint8_t streamingPacket[STREAMING_HEADER_LEN+FXLS8962_STREAM_DATA_SIZE]
GENERIC_DRIVER_GPIO Driver_GPIO_KSDK
Definition: gpio_driver.c:177
void Host_IO_Send(uint8_t *pMsg, size_t size, uint8_t encoding)
Definition: host_io_uart.c:136
void BOARD_SystickStart(int32_t *pStart)
Function to Record the Start systick.
Definition: systick_utils.c:44
const registerwritelist_t cDiffPClearStatusBits[]
Register settings for Clearing Pressure and Temperature Data Ready Bits.
Definition: diff_p_demo.c:62
uint32_t BOARD_SystickElapsedTime_us(int32_t *pStart)
Function to compute the Elapsed Time.
Definition: systick_utils.c:64
uint8_t data[FXLS8962_DATA_SIZE]
#define __END_READ_DATA__
Definition: sensor_drv.h:51
#define HOST_PRO_INT_DEV_TAG
Definition: host_io_uart.h:59
void BOARD_RunADS(const char *appName, char *boardString, char *shieldString, size_t bufferLength)
The function to register Application Name and initialte ADS.
fxos8700_accelmagdata_t rawData
#define DIFF_P_I2C_ADDR
uint8_t gStreamID
Definition: diff_p_demo.c:78
uint16_t readFrom
Definition: sensor_drv.h:80
int main(void)
Main function.
Definition: diff_p_demo.c:175
void(* toggle_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:48
#define ADS_MAX_STRING_LENGTH
volatile bool bDiffPDataReady
Definition: diff_p_demo.c:77
char shieldString[ADS_MAX_STRING_LENGTH]
Definition: diff_p_demo.c:75
GENERIC_DRIVER_GPIO * pGpioDriver
Definition: diff_p_demo.c:80
const registerreadlist_t cDiffPOutputNormal[]
Address and size of Raw Pressure+Temperature Data in Normal Mode.
Definition: diff_p_demo.c:69
void BOARD_SystickEnable(void)
Function to enable systicks framework.
Definition: systick_utils.c:35
#define STREAMING_HEADER_LEN
Definition: host_io_uart.h:25
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:155
void(* pin_init)(pinID_t aPinId, gpio_direction_t dir, void *apPinConfig, gpio_isr_handler_t aIsrHandler, void *apUserData)
Definition: Driver_GPIO.h:41
This structure defines the Read command List.
Definition: sensor_drv.h:78
ARM Systick Utilities.
bool process_host_command(uint8_t tag, uint8_t *hostCommand, uint8_t *hostResponse, size_t *hostMsgSize, size_t respBufferSize)
Definition: diff_p_demo.c:92
gpioHandleKSDK_t GREEN_LED
Definition: frdm_k64f.c:188
#define I2C_S_DEVICE_INDEX
Definition: issdk_hal.h:35
#define HOST_PRO_INT_CMD_TAG
Bit aligned values for Host Protocol Interface IDs (Bits 5-6).
Definition: host_io_uart.h:57
const registerreadlist_t cDiffPStatus[]
Address of Status Register.
Definition: diff_p_demo.c:66
#define SHIELD_NAME
#define DIFF_P_INT1
#define HOST_S_DRIVER
Definition: frdm_k64f.h:93