ISSDK  1.7
IoT Sensing Software Development Kit
mpl3115_oneShot_demo.c
Go to the documentation of this file.
1 /*
2  * The Clear BSD License
3  * Copyright (c) 2017, 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 mpl3115_oneShot_demo.c
37  * @brief The mpl3115_oneShot_demo.c file implements the ISSDK MPL3115 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 "mpl3115_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 MPL3115_DATA_SIZE 5 /* 3 byte Pressure and 2 byte Temperature. */
68 #define MPL3115_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 "MPL3115A2"
73 /*! @brief Version to distinguish between instances the same application based on target Shield and updates. */
74 #define APPLICATION_VERSION "1.1"
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. */
114 
115 //-----------------------------------------------------------------------
116 // Functions
117 //-----------------------------------------------------------------------
118 /* This is the Sensor Data Ready ISR implementation.*/
119 void mpl3115_int_data_ready_callback(void *pUserData)
120 { /*! @brief Set flag to indicate Sensor has signalled data ready. */
121  bMpl3115DataReady = true;
122 }
123 
124 /* Handler for Device Info and Streaming Control Commands. */
126  uint8_t tag, uint8_t *hostCommand, uint8_t *hostResponse, size_t *hostMsgSize, size_t respBufferSize)
127 {
128  bool success = false;
129 
130  /* If it is Host requesting Device Info, send Board Name and Shield Name. */
131  if (tag == HOST_PRO_INT_DEV_TAG)
132  { /* Byte 1 : Payload - Length of APPLICATION_NAME (b)
133  * Bytes=b : Payload Application Name
134  * Byte b+1 : Payload - Length of BOARD_NAME (s)
135  * Bytes=s : Payload Board Name
136  * Byte b+s+2 : Payload - Length of SHIELD_NAME (v)
137  * Bytes=v : Payload Shield Name */
138 
139  size_t appNameLen = strlen(embAppName);
140  size_t boardNameLen = strlen(boardString);
141  size_t shieldNameLen = strlen(shieldString);
142 
143  if (respBufferSize >= boardNameLen + shieldNameLen + appNameLen + 3)
144  { /* We have sufficient buffer. */
145  *hostMsgSize = 0;
146  }
147  else
148  {
149  return false;
150  }
151 
152  hostResponse[*hostMsgSize] = appNameLen;
153  *hostMsgSize += 1;
154 
155  memcpy(hostResponse + *hostMsgSize, embAppName, appNameLen);
156  *hostMsgSize += appNameLen;
157 
158  hostResponse[*hostMsgSize] = boardNameLen;
159  *hostMsgSize += 1;
160 
161  memcpy(hostResponse + *hostMsgSize, boardString, boardNameLen);
162  *hostMsgSize += boardNameLen;
163 
164  hostResponse[*hostMsgSize] = shieldNameLen;
165  *hostMsgSize += 1;
166 
167  memcpy(hostResponse + *hostMsgSize, shieldString, shieldNameLen);
168  *hostMsgSize += shieldNameLen;
169 
170  return true;
171  }
172 
173  /* If it is Host sending Streaming Commands, take necessary actions. */
174  if ((tag == (HOST_PRO_INT_CMD_TAG | HOST_PRO_CMD_W_CFG_TAG)) &&
176  { /* Byte 1 : Payload - Operation Code (Start/Stop Operation Code)
177  * Byte 2 : Payload - Stream ID (Target Stream for carrying out operation) */
178  switch (hostCommand[0]) /* Execute desired operation (Start/Stop) on the requested Stream. */
179  {
180  case HOST_CMD_START:
181  if (hostCommand[1] == gStreamID && bMpl3115Ready && bStreamingEnabled == false)
182  {
183  bStreamingEnabled = true;
184  success = true;
185  }
186  break;
187  case HOST_CMD_STOP:
188  if (hostCommand[1] == gStreamID && bMpl3115Ready && bStreamingEnabled == true)
189  {
190  pGpioDriver->clr_pin(&GREEN_LED);
191  bStreamingEnabled = false;
192  success = true;
193  }
194  break;
195  default:
196  break;
197  }
198  *hostMsgSize = 0; /* Zero payload in response. */
199  }
200 
201  return success;
202 }
203 
204 /*!
205  * @brief Main function
206  */
207 int main(void)
208 {
209  int32_t status, systick;
210  uint8_t toggle_led = 0;
212 
213  mpl3115_i2c_sensorhandle_t mpl3115Driver;
215 
216  ARM_DRIVER_I2C *pI2cDriver = &I2C_S_DRIVER;
217  ARM_DRIVER_USART *pUartDriver = &HOST_S_DRIVER;
218 
219  /*! Initialize the MCU hardware. */
222 
223  /* Create the Short Application Name String for ADS. */
224  sprintf(embAppName, "%s:%s", APPLICATION_NAME, APPLICATION_VERSION);
225 
226  /* Run ADS. */
228 
229  /* Create the Full Application Name String for Device Info Response. */
231 
232  /*! Initialize INT1 MPL3115 pin used by FRDM board */
234 
235  /*! Initialize GREEN LED pin used by FRDM board */
236  pGpioDriver->pin_init(&GREEN_LED, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
237 
238  /*! Initialize the I2C driver. */
239  status = pI2cDriver->Initialize(I2C_S_SIGNAL_EVENT);
240  if (ARM_DRIVER_OK != status)
241  {
242  return -1;
243  }
244 
245  /*! Set the I2C Power mode. */
246  status = pI2cDriver->PowerControl(ARM_POWER_FULL);
247  if (ARM_DRIVER_OK != status)
248  {
249  return -1;
250  }
251 
252  /*! Set the I2C bus speed. */
253  status = pI2cDriver->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
254  if (ARM_DRIVER_OK != status)
255  {
256  return -1;
257  }
258 
259  /*! Initialize the UART driver. */
260  status = pUartDriver->Initialize(HOST_S_SIGNAL_EVENT);
261  if (ARM_DRIVER_OK != status)
262  {
263  return -1;
264  }
265 
266  /*! Set the UART Power mode. */
267  status = pUartDriver->PowerControl(ARM_POWER_FULL);
268  if (ARM_DRIVER_OK != status)
269  {
270  return -1;
271  }
272 
273  /*! Set UART Baud Rate. */
274  status = pUartDriver->Control(ARM_USART_MODE_ASYNCHRONOUS, BOARD_DEBUG_UART_BAUDRATE);
275  if (ARM_DRIVER_OK != status)
276  {
277  return -1;
278  }
279 
280  /*! Initialize the MPL3115 sensor driver. */
283  if (SENSOR_ERROR_NONE == status)
284  {
285  /*! Set the task to be executed while waiting for I2C transactions to complete. */
286  MPL3115_I2C_SetIdleTask(&mpl3115Driver, (registeridlefunction_t)SMC_SetPowerModeVlpr, SMC);
287 
288  /*! We do not call MPL3115_I2C_Configure() in this case as we are going to read samples on demand.
289  * We explicitly only enable ISR settings. */
290  status = Sensor_I2C_Write(mpl3115Driver.pCommDrv, &mpl3115Driver.deviceInfo, mpl3115Driver.slaveAddress,
291  cMpl3115ConfigINT);
292  if (ARM_DRIVER_OK == status)
293  { /* Ready only if INT Configure write success. */
294  bMpl3115Ready = true;
295  }
296  }
297 
298  /*! Initialize streaming and assign a Stream ID. */
299  gStreamID =
300  Host_IO_Init(pUartDriver, (void *)mpl3115Driver.pCommDrv, &mpl3115Driver.deviceInfo, NULL, MPL3115_I2C_ADDR);
301  /* Confirm if a valid Stream ID has been allocated for this stream. */
302  if (0 == gStreamID)
303  {
304  bMpl3115Ready = false;
305  }
306  else
307  {
308  /*! Populate streaming header. */
310  BOARD_SystickStart(&systick);
311  pGpioDriver->clr_pin(&GREEN_LED); /* Set LED to indicate application is ready. */
312  }
313 
314  for (;;) /* Forever loop */
315  { /* Call UART Non-Blocking Receive. */
317 
318  /* Process packets only if streaming has been enabled by Host and ODR has expired.
319  * The receipt of ODR interrupt will indicate data should be ready. */
320  if (false == bStreamingEnabled || false == bMpl3115DataReady)
321  {
322  SMC_SetPowerModeWait(SMC); /* Power save, wait if nothing to do. */
323  continue;
324  }
325  else
326  { /*! Clear the data ready flag, it will be set again by the ODR CB. */
327  bMpl3115DataReady = false;
328  }
329 
330  /* Trigger acquisition of the Next Sample. */
331  status = Sensor_I2C_Write(mpl3115Driver.pCommDrv, &mpl3115Driver.deviceInfo, mpl3115Driver.slaveAddress,
332  cMpl3115SetOST);
333  if (ARM_DRIVER_OK != status)
334  { /* Exit if OST write failed. */
335  return -1;
336  }
337 
338  /*! Read raw sensor data from the MPL3115. */
339  status = MPL3115_I2C_ReadData(&mpl3115Driver, cMpl3115OutputNormal, data);
340  if (ARM_DRIVER_OK != status)
341  { /* Exit if sample read failed. */
342  return -1;
343  }
344 
345  /* Update timestamp from Systick framework. */
346  rawData.timestamp += BOARD_SystickElapsedTime_us(&systick);
347 
348  /*! Convert the raw sensor data to signed 32-bit and 16-bit containers for display to the debug port. */
349  rawData.pressure = (uint32_t)((data[0]) << 16) | ((data[1]) << 8) | ((data[2]));
350  rawData.temperature = (int16_t)((data[3]) << 8) | (data[4]);
351  rawData.pressure /= 16;
352  rawData.temperature /= 16;
353 
354  /* Copy Raw samples to Streaming Buffer. */
355  memcpy(streamingPacket + STREAMING_HEADER_LEN, &rawData, MPL3115_STREAM_DATA_SIZE);
356  /* Send streaming packet to Host. */
357  Host_IO_Send(streamingPacket, sizeof(streamingPacket), HOST_FORMAT_HDLC);
358  if (toggle_led++ == LED_TOGGLE_RATE)
359  { /* Toggle LED at a refresh rate that is perceivable to indicate application is active. */
360  toggle_led = 0;
361  pGpioDriver->toggle_pin(&GREEN_LED);
362  }
363  }
364 }
registerDeviceInfo_t deviceInfo
Definition: mpl3115_drv.h:64
uint32_t BOARD_SystickElapsedTime_us(int32_t *pStart)
Function to compute the Elapsed Time.
Definition: systick_utils.c:99
ARM_DRIVER_I2C * pCommDrv
Definition: mpl3115_drv.h:65
#define __END_WRITE_DATA__
Definition: sensor_drv.h:71
#define HOST_PRO_CMD_W_CFG_TAG
Definition: host_io_uart.h:89
int32_t MPL3115_I2C_Initialize(mpl3115_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: mpl3115_drv.c:48
#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
#define MPL3115_CTRL_REG3_IPOL1_MASK
Definition: mpl3115.h:976
uint8_t data[FXLS8962_DATA_SIZE]
int32_t MPL3115_I2C_ReadData(mpl3115_i2c_sensorhandle_t *pSensorHandle, const registerreadlist_t *pReadList, uint8_t *pBuffer)
The interface function to read the sensor data.
Definition: mpl3115_drv.c:130
#define MPL3115_CTRL_REG1_OST_SET
Definition: mpl3115.h:871
int32_t status
void(* toggle_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:74
#define LED_TOGGLE_RATE
#define SHIELD_NAME
#define MPL3115_PT_DATA_CFG_PDEFE_ENABLED
Definition: mpl3115.h:567
#define ADS_MAX_STRING_LENGTH
#define MPL3115_CTRL_REG1_OST_MASK
Definition: mpl3115.h:849
GENERIC_DRIVER_GPIO * pGpioDriver
#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 APPLICATION_VERSION
Version to distinguish between instances the same application based on target Shield and updates...
#define HOST_PRO_INT_CMD_TAG
Bit aligned values for Host Protocol Interface IDs (Bits 5-6).
Definition: host_io_uart.h:83
void mpl3115_int_data_ready_callback(void *pUserData)
#define __END_READ_DATA__
Definition: sensor_drv.h:77
void MPL3115_I2C_SetIdleTask(mpl3115_i2c_sensorhandle_t *pSensorHandle, registeridlefunction_t idleTask, void *userParam)
: The interface function to set the I2C Idle Task.
Definition: mpl3115_drv.c:79
#define MPL3115_PT_DATA_CFG_DREM_ENABLED
Definition: mpl3115.h:570
#define MPL3115_CTRL_REG1_OS_OSR_2
Definition: mpl3115.h:875
#define MPL3115_CTRL_REG1_OS_MASK
Definition: mpl3115.h:855
#define MPL3115_PT_DATA_CFG_PDEFE_MASK
Definition: mpl3115.h:553
#define MPL3115_PT_DATA_CFG_TDEFE_ENABLED
Definition: mpl3115.h:564
#define MPL3115_WHOAMI_VALUE
Definition: mpl3115.h:65
const registerwritelist_t cMpl3115SetOST[]
Register settings for Triggring One-Shot Sampling.
char embAppName[ADS_MAX_STRING_LENGTH]
uint8_t gStreamID
gpioHandleKSDK_t GREEN_LED
Definition: frdm_k64f.c:214
volatile bool bMpl3115Ready
This defines the sensor specific information.
Definition: mpl3115_drv.h:62
const registerreadlist_t cMpl3115OutputNormal[]
Address and size of Raw Pressure+Temperature Data in Normal Mode.
bool process_host_command(uint8_t tag, uint8_t *hostCommand, uint8_t *hostResponse, size_t *hostMsgSize, size_t respBufferSize)
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
void BOARD_SystickEnable(void)
Function to enable systicks framework.
Definition: systick_utils.c:70
#define I2C_S_DEVICE_INDEX
Definition: issdk_hal.h:61
#define MPL3115_CTRL_REG4_INT_EN_DRDY_INTENABLED
Definition: mpl3115.h:1070
#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
The mpl3115_drv.h file describes the MPL3115 driver interface and structures.
Access structure of the GPIO Driver.
Definition: Driver_GPIO.h:64
#define MPL3115_I2C_ADDR
void BOARD_SystickStart(int32_t *pStart)
Function to Record the Start systick.
Definition: systick_utils.c:79
int main(void)
Main function.
char shieldString[ADS_MAX_STRING_LENGTH]
ARM Systick Utilities.
const registerwritelist_t cMpl3115ConfigINT[]
Register settings for Interrupt (non buffered) Enablement with ONe-Shot Mode.
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 APPLICATION_NAME
Unique Name for this application which should match the target GUI pkg name.
uint8_t Host_IO_Init(ARM_DRIVER_USART *pDrv, void *pBus, void *pDevInfo, void *spiSlaveParams, uint16_t slaveAddress)
Definition: host_io_uart.c:126
volatile bool bStreamingEnabled
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 MPL3115_CTRL_REG4_INT_EN_DRDY_MASK
Definition: mpl3115.h:1048
#define STREAMING_HEADER_LEN
Definition: host_io_uart.h:51
#define MPL3115_PT_DATA_CFG_DREM_MASK
Definition: mpl3115.h:556
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 MPL3115_STREAM_DATA_SIZE
#define MPL3115_CTRL_REG5_INT_CFG_DRDY_MASK
Definition: mpl3115.h:1128
#define MPL3115_PT_DATA_CFG_TDEFE_MASK
Definition: mpl3115.h:550
This structure defines the mpl3115 data buffer in Pressure Mode.
Definition: mpl3115_drv.h:71
volatile bool bMpl3115DataReady
#define I2C_S_SIGNAL_EVENT
Definition: issdk_hal.h:60
#define HOST_S_DRIVER
Definition: frdm_k64f.h:119
#define MPL3115_CTRL_REG5_INT_CFG_DRDY_INT1
Definition: mpl3115.h:1150
#define MPL3115_INT1
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
char boardString[ADS_MAX_STRING_LENGTH]
#define MPL3115_DATA_SIZE
#define MPL3115_CTRL_REG3_IPOL1_HIGH
Definition: mpl3115.h:990