ISSDK  1.8
IoT Sensing Software Development Kit
fxpq3115_demo.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, Freescale Semiconductor, Inc.
3  * Copyright 2016-2017 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 /**
10  * @file fxpq3115_demo.c
11  * @brief The fxpq3115_demo.c file implements the ISSDK FXPQ3115 sensor
12  * demo example demonstration with One-Shot 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 "fxpq3115_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 FXPQ3115_DATA_SIZE 5 /* 3 byte Pressure and 2 byte Temperature. */
42 #define FXPQ3115_STREAM_DATA_SIZE 10 /* 6 byte Data */
43 #define LED_TOGGLE_RATE 100 /* Toggle LED after every 100 samples. */
44 
45 /*! @brief Unique Name for this application which should match the target GUI pkg name. */
46 #define APPLICATION_NAME "FXPQ3115BV BIO Pressure Demo"
47 /*! @brief Version to distinguish between instances the same application based on target Shield and updates. */
48 #define APPLICATION_VERSION "2.5"
49 
50 //-----------------------------------------------------------------------
51 // Constants
52 //-----------------------------------------------------------------------
53 /*! @brief Register settings for Interrupt (non buffered) Enablement with ONe-Shot Mode. */
55  /* Enable Data Ready and Event flags for Pressure, Temperature or either. */
59  /* Set the One ShoT Bit and 100Hz OSR. */
62  /* Set INT1 Active High. */
64  /* Enable Interrupts for Data Ready Events. */
66  /* Route Interrupt to INT1. */
69 
70 /*! @brief Register settings for Triggring One-Shot Sampling. */
72  /* Set the One ShoT Bit. */
75 
76 /*! @brief Address and size of Raw Pressure+Temperature Data in Normal Mode. */
79 
80 //-----------------------------------------------------------------------
81 // Global Variables
82 //-----------------------------------------------------------------------
85 volatile bool bStreamingEnabled = false, bMpl3115DataReady = false, bMpl3115Ready = false;
86 uint8_t gStreamID; /* The auto assigned Stream ID. */
89 
90 //-----------------------------------------------------------------------
91 // Functions
92 //-----------------------------------------------------------------------
93 /* This is the Sensor Data Ready ISR implementation.*/
94 void fxpq3115_int_data_ready_callback(void *pUserData)
95 { /*! @brief Set flag to indicate Sensor has signalled data ready. */
96  bMpl3115DataReady = true;
97 }
98 
99 /* Handler for Device Info and Streaming Control Commands. */
101  uint8_t tag, uint8_t *hostCommand, uint8_t *hostResponse, size_t *hostMsgSize, size_t respBufferSize)
102 {
103  bool success = false;
104 
105  /* If it is Host requesting Device Info, send Board Name and Shield Name. */
106  if (tag == HOST_PRO_INT_DEV_TAG)
107  { /* Byte 1 : Payload - Length of APPLICATION_NAME (b)
108  * Bytes=b : Payload Application Name
109  * Byte b+1 : Payload - Length of BOARD_NAME (s)
110  * Bytes=s : Payload Board Name
111  * Byte b+s+2 : Payload - Length of SHIELD_NAME (v)
112  * Bytes=v : Payload Shield Name */
113 
114  size_t appNameLen = strlen(embAppName);
115  size_t boardNameLen = strlen(boardString);
116  size_t shieldNameLen = strlen(shieldString);
117 
118  if (respBufferSize >= boardNameLen + shieldNameLen + appNameLen + 3)
119  { /* We have sufficient buffer. */
120  *hostMsgSize = 0;
121  }
122  else
123  {
124  return false;
125  }
126 
127  hostResponse[*hostMsgSize] = appNameLen;
128  *hostMsgSize += 1;
129 
130  memcpy(hostResponse + *hostMsgSize, embAppName, appNameLen);
131  *hostMsgSize += appNameLen;
132 
133  hostResponse[*hostMsgSize] = boardNameLen;
134  *hostMsgSize += 1;
135 
136  memcpy(hostResponse + *hostMsgSize, boardString, boardNameLen);
137  *hostMsgSize += boardNameLen;
138 
139  hostResponse[*hostMsgSize] = shieldNameLen;
140  *hostMsgSize += 1;
141 
142  memcpy(hostResponse + *hostMsgSize, shieldString, shieldNameLen);
143  *hostMsgSize += shieldNameLen;
144 
145  return true;
146  }
147 
148  /* If it is Host sending Streaming Commands, take necessary actions. */
149  if ((tag == (HOST_PRO_INT_CMD_TAG | HOST_PRO_CMD_W_CFG_TAG)) &&
151  { /* Byte 1 : Payload - Operation Code (Start/Stop Operation Code)
152  * Byte 2 : Payload - Stream ID (Target Stream for carrying out operation) */
153  switch (hostCommand[0]) /* Execute desired operation (Start/Stop) on the requested Stream. */
154  {
155  case HOST_CMD_START:
156  if (hostCommand[1] == gStreamID && bMpl3115Ready && bStreamingEnabled == false)
157  {
159  bStreamingEnabled = true;
160  success = true;
161  }
162  break;
163  case HOST_CMD_STOP:
164  if (hostCommand[1] == gStreamID && bMpl3115Ready && bStreamingEnabled == true)
165  {
166  pGpioDriver->clr_pin(&GREEN_LED);
167  bStreamingEnabled = false;
168  success = true;
169  }
170  break;
171  default:
172  break;
173  }
174  *hostMsgSize = 0; /* Zero payload in response. */
175  }
176 
177  return success;
178 }
179 
180 /*!
181  * @brief Main function
182  */
183 int main(void)
184 {
185  int32_t status;
186  uint8_t toggle_led = 0;
188 
189  fxpq3115_i2c_sensorhandle_t fxpq3115Driver;
191 
192  ARM_DRIVER_I2C *pI2cDriver = &I2C_S_DRIVER;
193  ARM_DRIVER_USART *pUartDriver = &HOST_S_DRIVER;
194 
195  /*! Initialize the MCU hardware. */
198 
199  /* Create the Short Application Name String for ADS. */
200  sprintf(embAppName, "%s:%s", APPLICATION_NAME, APPLICATION_VERSION);
201 
202  /* Run ADS. */
204 
205  /* Create the Full Application Name String for Device Info Response. */
207 
208  /*! Initialize INT1 FXPQ3115 pin used by FRDM board */
210 
211  /*! Initialize GREEN LED pin used by FRDM board */
212  pGpioDriver->pin_init(&GREEN_LED, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
213 
214  /*! Initialize the I2C driver. */
215  status = pI2cDriver->Initialize(I2C_S_SIGNAL_EVENT);
216  if (ARM_DRIVER_OK != status)
217  {
218  return -1;
219  }
220 
221  /*! Set the I2C Power mode. */
222  status = pI2cDriver->PowerControl(ARM_POWER_FULL);
223  if (ARM_DRIVER_OK != status)
224  {
225  return -1;
226  }
227 
228  /*! Set the I2C bus speed. */
229  status = pI2cDriver->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
230  if (ARM_DRIVER_OK != status)
231  {
232  return -1;
233  }
234 
235  /*! Initialize the UART driver. */
236  status = pUartDriver->Initialize(HOST_S_SIGNAL_EVENT);
237  if (ARM_DRIVER_OK != status)
238  {
239  return -1;
240  }
241 
242  /*! Set the UART Power mode. */
243  status = pUartDriver->PowerControl(ARM_POWER_FULL);
244  if (ARM_DRIVER_OK != status)
245  {
246  return -1;
247  }
248 
249  /*! Set UART Baud Rate. */
250  status = pUartDriver->Control(ARM_USART_MODE_ASYNCHRONOUS, BOARD_DEBUG_UART_BAUDRATE);
251  if (ARM_DRIVER_OK != status)
252  {
253  return -1;
254  }
255 
256  /*! Initialize the FXPQ3115 sensor driver. */
259  if (SENSOR_ERROR_NONE == status)
260  {
261  /*! Set the task to be executed while waiting for I2C transactions to complete. */
263 
264  /*! We do not call FXPQ3115_I2C_Configure() in this case as we are going to read samples on demand.
265  * We explicitly only enable ISR settings. */
266  status = Sensor_I2C_Write(fxpq3115Driver.pCommDrv, &fxpq3115Driver.deviceInfo, fxpq3115Driver.slaveAddress,
267  cMpl3115ConfigINT);
268  if (ARM_DRIVER_OK == status)
269  { /* Ready only if INT Configure write success. */
270  bMpl3115Ready = true;
271  }
272  }
273 
274  /*! Initialize streaming and assign a Stream ID. */
275  gStreamID =
276  Host_IO_Init(pUartDriver, (void *)fxpq3115Driver.pCommDrv, &fxpq3115Driver.deviceInfo, NULL, FXPQ3115_I2C_ADDR);
277  /* Confirm if a valid Stream ID has been allocated for this stream. */
278  if (0 == gStreamID)
279  {
280  bMpl3115Ready = false;
281  }
282  else
283  {
284  /*! Populate streaming header. */
286  pGpioDriver->clr_pin(&GREEN_LED); /* Set LED to indicate application is ready. */
287  }
288 
289  for (;;) /* Forever loop */
290  { /* Call UART Non-Blocking Receive. */
292 
293  /* Process packets only if streaming has been enabled by Host and ODR has expired.
294  * The receipt of ODR interrupt will indicate data should be ready. */
295  if (false == bStreamingEnabled || false == bMpl3115DataReady)
296  {
297  SMC_SetPowerModeWait(SMC); /* Power save, wait if nothing to do. */
298  continue;
299  }
300  else
301  { /*! Clear the data ready flag, it will be set again by the ODR CB. */
302  bMpl3115DataReady = false;
303  }
304 
305  /* Trigger acquisition of the Next Sample. */
306  status = Sensor_I2C_Write(fxpq3115Driver.pCommDrv, &fxpq3115Driver.deviceInfo, fxpq3115Driver.slaveAddress,
307  cMpl3115SetOST);
308  if (ARM_DRIVER_OK != status)
309  { /* Exit if OST write failed. */
310  return -1;
311  }
312 
313  /*! Read raw sensor data from the FXPQ3115. */
314  status = FXPQ3115_I2C_ReadData(&fxpq3115Driver, cMpl3115OutputNormal, data);
315  if (ARM_DRIVER_OK != status)
316  { /* Exit if sample read failed. */
317  return -1;
318  }
319 
320  /* Update timestamp from Systick framework. */
322 
323  /*! Convert the raw sensor data to signed 32-bit and 16-bit containers for display to the debug port. */
324  rawData.pressure = (uint32_t)((data[0]) << 16) | ((data[1]) << 8) | ((data[2]));
325  rawData.temperature = (int16_t)((data[3]) << 8) | (data[4]);
326  rawData.pressure /= 16;
327  rawData.temperature /= 16;
328 
329  /* Copy Raw samples to Streaming Buffer. */
330  memcpy(streamingPacket + STREAMING_HEADER_LEN, &rawData, FXPQ3115_STREAM_DATA_SIZE);
331  /* Send streaming packet to Host. */
332  Host_IO_Send(streamingPacket, sizeof(streamingPacket), HOST_FORMAT_HDLC);
333  if (toggle_led++ == LED_TOGGLE_RATE)
334  { /* Toggle LED at a refresh rate that is perceivable to indicate application is active. */
335  toggle_led = 0;
336  pGpioDriver->toggle_pin(&GREEN_LED);
337  }
338  }
339 }
#define APPLICATION_VERSION
Version to distinguish between instances the same application based on target Shield and updates...
Definition: fxpq3115_demo.c:48
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
This structure defines the Write command List.
Definition: sensor_drv.h:68
#define FXPQ3115_CTRL_REG5_INT_CFG_DRDY_MASK
Definition: fxpq3115.h:1131
#define FXPQ3115_CTRL_REG4_INT_EN_DRDY_INTENABLED
Definition: fxpq3115.h:1073
int32_t status
status_t SMC_SetPowerModeVlpr(void *arg)
Configures the system to VLPR power mode. API name used from Kinetis family to maintain compatibility...
Definition: lpc54114.c:169
The host_io_uart.h file contains the Host Protocol interface definitions and configuration.
#define HOST_PRO_CMD_W_CFG_TAG
Definition: host_io_uart.h:63
#define LED_TOGGLE_RATE
Definition: fxpq3115_demo.c:43
Access structure of the GPIO Driver.
Definition: Driver_GPIO.h:38
#define SMC
Definition: lpc54114.h:118
#define FXPQ3115_PT_DATA_CFG_TDEFE_MASK
Definition: fxpq3115.h:550
#define FXPQ3115_PT_DATA_CFG_TDEFE_ENABLED
Definition: fxpq3115.h:564
#define FXPQ3115_CTRL_REG3_IPOL1_HIGH
Definition: fxpq3115.h:993
#define FXPQ3115_CTRL_REG1_OS_MASK
Definition: fxpq3115.h:855
#define __END_WRITE_DATA__
Definition: sensor_drv.h:45
This structure defines the fxpq3115 data buffer in Pressure Mode.
Definition: fxpq3115_drv.h:45
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
#define FXPQ3115_PT_DATA_CFG_PDEFE_MASK
Definition: fxpq3115.h:553
#define APPLICATION_NAME
Unique Name for this application which should match the target GUI pkg name.
Definition: fxpq3115_demo.c:46
#define I2C_S_SIGNAL_EVENT
Definition: issdk_hal.h:34
#define I2C_S_DRIVER
Definition: issdk_hal.h:33
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
const registerwritelist_t cMpl3115ConfigINT[]
Register settings for Interrupt (non buffered) Enablement with ONe-Shot Mode.
Definition: fxpq3115_demo.c:54
#define BOARD_BootClockRUN
Definition: clock_config.h:19
volatile bool bStreamingEnabled
Definition: fxpq3115_demo.c:85
void(* registeridlefunction_t)(void *userParam)
This is the register idle function type.
Definition: sensor_drv.h:97
void FXPQ3115_I2C_SetIdleTask(fxpq3115_i2c_sensorhandle_t *pSensorHandle, registeridlefunction_t idleTask, void *userParam)
: The interface function to set the I2C Idle Task.
Definition: fxpq3115_drv.c:53
int32_t FXPQ3115_I2C_Initialize(fxpq3115_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: fxpq3115_drv.c:22
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
registerDeviceInfo_t deviceInfo
Definition: fxpq3115_drv.h:38
void BOARD_SystickStart(int32_t *pStart)
Function to Record the Start systick.
Definition: systick_utils.c:44
int32_t gSystick
Definition: fxpq3115_demo.c:87
const registerreadlist_t cMpl3115OutputNormal[]
Address and size of Raw Pressure+Temperature Data in Normal Mode.
Definition: fxpq3115_demo.c:77
uint32_t BOARD_SystickElapsedTime_us(int32_t *pStart)
Function to compute the Elapsed Time.
Definition: systick_utils.c:64
#define FXPQ3115_PT_DATA_CFG_DREM_MASK
Definition: fxpq3115.h:556
The fxpq3115_drv.h file describes the fxpq3115 driver interface and structures.
uint8_t data[FXLS8962_DATA_SIZE]
#define FXPQ3115_CTRL_REG4_INT_EN_DRDY_MASK
Definition: fxpq3115.h:1051
#define __END_READ_DATA__
Definition: sensor_drv.h:51
void fxpq3115_int_data_ready_callback(void *pUserData)
Definition: fxpq3115_demo.c:94
#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.
#define FXPQ3115_INT1
#define FXPQ3115_CTRL_REG5_INT_CFG_DRDY_INT1
Definition: fxpq3115.h:1153
bool process_host_command(uint8_t tag, uint8_t *hostCommand, uint8_t *hostResponse, size_t *hostMsgSize, size_t respBufferSize)
#define FXPQ3115_STREAM_DATA_SIZE
Definition: fxpq3115_demo.c:42
uint8_t gStreamID
Definition: fxpq3115_demo.c:86
#define FXPQ3115_CTRL_REG1_OS_OSR_2
Definition: fxpq3115.h:875
fxos8700_accelmagdata_t rawData
#define FXPQ3115_CTRL_REG1_OST_SET
Definition: fxpq3115.h:871
#define FXPQ3115_CTRL_REG3_IPOL1_MASK
Definition: fxpq3115.h:979
int main(void)
Main function.
#define FXPQ3115_PT_DATA_CFG_PDEFE_ENABLED
Definition: fxpq3115.h:567
uint16_t readFrom
Definition: sensor_drv.h:80
char shieldString[ADS_MAX_STRING_LENGTH]
Definition: fxpq3115_demo.c:83
GENERIC_DRIVER_GPIO * pGpioDriver
Definition: fxpq3115_demo.c:88
void(* toggle_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:48
#define ADS_MAX_STRING_LENGTH
#define FXPQ3115_WHOAMI_VALUE
Definition: fxpq3115.h:65
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
ARM_DRIVER_I2C * pCommDrv
Definition: fxpq3115_drv.h:39
This structure defines the Read command List.
Definition: sensor_drv.h:78
ARM Systick Utilities.
#define FXPQ3115_I2C_ADDR
const registerwritelist_t cMpl3115SetOST[]
Register settings for Triggring One-Shot Sampling.
Definition: fxpq3115_demo.c:71
#define FXPQ3115_CTRL_REG1_OST_MASK
Definition: fxpq3115.h:849
volatile bool bMpl3115DataReady
Definition: fxpq3115_demo.c:85
char embAppName[ADS_MAX_STRING_LENGTH]
Definition: fxpq3115_demo.c:84
gpioHandleKSDK_t GREEN_LED
Definition: frdm_k64f.c:188
#define I2C_S_DEVICE_INDEX
Definition: issdk_hal.h:35
This defines the sensor specific information.
Definition: fxpq3115_drv.h:36
volatile bool bMpl3115Ready
Definition: fxpq3115_demo.c:85
#define HOST_PRO_INT_CMD_TAG
Bit aligned values for Host Protocol Interface IDs (Bits 5-6).
Definition: host_io_uart.h:57
#define SHIELD_NAME
#define FXPQ3115_DATA_SIZE
Definition: fxpq3115_demo.c:41
int32_t FXPQ3115_I2C_ReadData(fxpq3115_i2c_sensorhandle_t *pSensorHandle, const registerreadlist_t *pReadList, uint8_t *pBuffer)
The interface function to read the sensor data.
Definition: fxpq3115_drv.c:106
#define FXPQ3115_PT_DATA_CFG_DREM_ENABLED
Definition: fxpq3115.h:570
char boardString[ADS_MAX_STRING_LENGTH]
Definition: fxpq3115_demo.c:83
#define HOST_S_DRIVER
Definition: frdm_k64f.h:93