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