ISSDK  1.7
IoT Sensing Software Development Kit
mpl3115_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 mpl3115_demo.c
37  * @brief The mpl3115_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_T_ON_MAX 1000 /* MAX Turn On Time after RST */
68 #define MPL3115_DATA_SIZE 5 /* 3 byte Pressure and 2 byte Temperature. */
69 #define MPL3115_STREAM_DATA_SIZE 10 /* 6 byte Data */
70 #define LED_TOGGLE_RATE 100 /* Toggle LED after every 100 samples. */
71 
72 /*! @brief Unique Name for this application which should match the target GUI pkg name. */
73 #define APPLICATION_NAME "Digital Pressure Altimeter Demo (MPL3115)"
74 /*! @brief Version to distinguish between instances the same application based on target Shield and updates. */
75 #define APPLICATION_VERSION "2.5"
76 
77 //-----------------------------------------------------------------------
78 // Constants
79 //-----------------------------------------------------------------------
80 /*! @brief Register settings for Interrupt (non buffered) Enablement with ONe-Shot Mode. */
82  /* Set 100Hz OSR and Clear ALT and ACTIBE Bits. */
85  /* Reset INT1 Pin state. */
87  /* Set INT1 Active High. */
88  {MPL3115_CTRL_REG3, MPL3115_CTRL_REG3_IPOL1_HIGH, MPL3115_CTRL_REG3_IPOL1_MASK},
89  /* Enable Interrupts for Data Ready Events. */
91  /* Route Interrupt to INT1. */
93  /* Enable Data Ready and Event flags for Pressure, Temperature or either. */
96  /* Set the One ShoT Bit. */
99 
100 /*! @brief Register settings for Triggring One-Shot Sampling. */
102  /* Set the One ShoT Bit. */
105 
106 /*! @brief Address and size of Raw Pressure+Temperature Data in Normal Mode. */
109 
110 //-----------------------------------------------------------------------
111 // Global Variables
112 //-----------------------------------------------------------------------
115 volatile bool bStreamingEnabled = false, bMpl3115DataReady = false, bMpl3115Ready = false;
116 uint8_t gStreamID; /* The auto assigned Stream ID. */
119 
120 //-----------------------------------------------------------------------
121 // Functions
122 //-----------------------------------------------------------------------
123 /* This is the Sensor Data Ready ISR implementation.*/
124 void mpl3115_int_data_ready_callback(void *pUserData)
125 { /*! @brief Set flag to indicate Sensor has signalled data ready. */
126  bMpl3115DataReady = true;
127 }
128 
129 /* Handler for Device Info and Streaming Control Commands. */
131  uint8_t tag, uint8_t *hostCommand, uint8_t *hostResponse, size_t *hostMsgSize, size_t respBufferSize)
132 {
133  bool success = false;
134 
135  /* If it is Host requesting Device Info, send Board Name and Shield Name. */
136  if (tag == HOST_PRO_INT_DEV_TAG)
137  { /* Byte 1 : Payload - Length of APPLICATION_NAME (b)
138  * Bytes=b : Payload Application Name
139  * Byte b+1 : Payload - Length of BOARD_NAME (s)
140  * Bytes=s : Payload Board Name
141  * Byte b+s+2 : Payload - Length of SHIELD_NAME (v)
142  * Bytes=v : Payload Shield Name */
143 
144  size_t appNameLen = strlen(embAppName);
145  size_t boardNameLen = strlen(boardString);
146  size_t shieldNameLen = strlen(shieldString);
147 
148  if (respBufferSize >= boardNameLen + shieldNameLen + appNameLen + 3)
149  { /* We have sufficient buffer. */
150  *hostMsgSize = 0;
151  }
152  else
153  {
154  return false;
155  }
156 
157  hostResponse[*hostMsgSize] = appNameLen;
158  *hostMsgSize += 1;
159 
160  memcpy(hostResponse + *hostMsgSize, embAppName, appNameLen);
161  *hostMsgSize += appNameLen;
162 
163  hostResponse[*hostMsgSize] = boardNameLen;
164  *hostMsgSize += 1;
165 
166  memcpy(hostResponse + *hostMsgSize, boardString, boardNameLen);
167  *hostMsgSize += boardNameLen;
168 
169  hostResponse[*hostMsgSize] = shieldNameLen;
170  *hostMsgSize += 1;
171 
172  memcpy(hostResponse + *hostMsgSize, shieldString, shieldNameLen);
173  *hostMsgSize += shieldNameLen;
174 
175  return true;
176  }
177 
178  /* If it is Host sending Streaming Commands, take necessary actions. */
179  if ((tag == (HOST_PRO_INT_CMD_TAG | HOST_PRO_CMD_W_CFG_TAG)) &&
181  { /* Byte 1 : Payload - Operation Code (Start/Stop Operation Code)
182  * Byte 2 : Payload - Stream ID (Target Stream for carrying out operation) */
183  switch (hostCommand[0]) /* Execute desired operation (Start/Stop) on the requested Stream. */
184  {
185  case HOST_CMD_START:
186  if (hostCommand[1] == gStreamID && bMpl3115Ready && bStreamingEnabled == false)
187  {
189  bStreamingEnabled = true;
190  success = true;
191  }
192  break;
193  case HOST_CMD_STOP:
194  if (hostCommand[1] == gStreamID && bMpl3115Ready && bStreamingEnabled == true)
195  {
196  pGpioDriver->clr_pin(&GREEN_LED);
197  bStreamingEnabled = false;
198  success = true;
199  }
200  break;
201  default:
202  break;
203  }
204  *hostMsgSize = 0; /* Zero payload in response. */
205  }
206 
207  return success;
208 }
209 
210 /*!
211  * @brief Main function
212  */
213 int main(void)
214 {
215  int32_t status;
216  uint8_t toggle_led = 0;
218 
219  mpl3115_i2c_sensorhandle_t mpl3115Driver;
221 
222  ARM_DRIVER_I2C *pI2cDriver = &I2C_S_DRIVER;
223  ARM_DRIVER_USART *pUartDriver = &HOST_S_DRIVER;
224 
225  /*! Initialize the MCU hardware. */
228 
229  /* Create the Short Application Name String for ADS. */
230  sprintf(embAppName, "%s:%s", APPLICATION_NAME, APPLICATION_VERSION);
231 
232  /* Run ADS. */
234 
235  /* Create the Full Application Name String for Device Info Response. */
237 
238  /*! Initialize INT1 MPL3115 pin used by FRDM board */
240 
241  /*! Initialize GREEN LED pin used by FRDM board */
242  pGpioDriver->pin_init(&GREEN_LED, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
243 
244  /*! Initialize the I2C driver. */
245  status = pI2cDriver->Initialize(I2C_S_SIGNAL_EVENT);
246  if (ARM_DRIVER_OK != status)
247  {
248  return -1;
249  }
250 
251  /*! Set the I2C Power mode. */
252  status = pI2cDriver->PowerControl(ARM_POWER_FULL);
253  if (ARM_DRIVER_OK != status)
254  {
255  return -1;
256  }
257 
258  /*! Set the I2C bus speed. */
259  status = pI2cDriver->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
260  if (ARM_DRIVER_OK != status)
261  {
262  return -1;
263  }
264 
265  /*! Initialize the UART driver. */
266  status = pUartDriver->Initialize(HOST_S_SIGNAL_EVENT);
267  if (ARM_DRIVER_OK != status)
268  {
269  return -1;
270  }
271 
272  /*! Set the UART Power mode. */
273  status = pUartDriver->PowerControl(ARM_POWER_FULL);
274  if (ARM_DRIVER_OK != status)
275  {
276  return -1;
277  }
278 
279  /*! Set UART Baud Rate. */
280  status = pUartDriver->Control(ARM_USART_MODE_ASYNCHRONOUS, BOARD_DEBUG_UART_BAUDRATE);
281  if (ARM_DRIVER_OK != status)
282  {
283  return -1;
284  }
285 
286  /*! Initialize the MPL3115 sensor driver. */
289  if (SENSOR_ERROR_NONE == status)
290  { /*! Set the task to be executed while waiting for I2C transactions to complete. */
292 
293  /*! We do not call MPL3115_I2C_Configure() in this case as we are going to read samples on demand.
294  * We explicitly only enable ISR settings. */
295  status = Sensor_I2C_Write(mpl3115Driver.pCommDrv, &mpl3115Driver.deviceInfo, mpl3115Driver.slaveAddress,
296  cMpl3115ConfigINT);
297  if (ARM_DRIVER_OK == status)
298  { /* Ready only if INT Configure write success. */
299  bMpl3115Ready = true;
300  }
301  }
302 
303  /*! Initialize streaming and assign a Stream ID. */
304  gStreamID =
305  Host_IO_Init(pUartDriver, (void *)mpl3115Driver.pCommDrv, &mpl3115Driver.deviceInfo, NULL, MPL3115_I2C_ADDR);
306  /* Confirm if a valid Stream ID has been allocated for this stream. */
307  if (0 == gStreamID)
308  {
309  bMpl3115Ready = false;
310  }
311  else
312  {
313  /*! Populate streaming header. */
315  pGpioDriver->clr_pin(&GREEN_LED); /* Set LED to indicate application is ready. */
316  }
317 
318  for (;;) /* Forever loop */
319  { /* Call UART Non-Blocking Receive. */
321 
322  /* Process packets only if streaming has been enabled by Host and ODR has expired.
323  * The receipt of ODR interrupt will indicate data should be ready. */
324  if (false == bStreamingEnabled || false == bMpl3115DataReady)
325  {
326  SMC_SetPowerModeWait(SMC); /* Power save, wait if nothing to do. */
327  continue;
328  }
329  else
330  { /*! Clear the data ready flag, it will be set again by the ODR CB. */
331  bMpl3115DataReady = false;
332  }
333 
334  /* Trigger acquisition of the Next Sample. */
335  status = Sensor_I2C_Write(mpl3115Driver.pCommDrv, &mpl3115Driver.deviceInfo, mpl3115Driver.slaveAddress,
336  cMpl3115SetOST);
337  if (ARM_DRIVER_OK != status)
338  { /* Exit if OST write failed. */
339  return -1;
340  }
341 
342  /*! Read raw sensor data from the MPL3115. */
343  status = MPL3115_I2C_ReadData(&mpl3115Driver, cMpl3115OutputNormal, data);
344  if (ARM_DRIVER_OK != status)
345  { /* Exit if sample read failed. */
346  return -1;
347  }
348 
349  /* Update timestamp from Systick framework. */
351 
352  /*! Convert the raw sensor data to signed 32-bit and 16-bit containers for display to the debug port. */
353  rawData.pressure = (uint32_t)((data[0]) << 16) | ((data[1]) << 8) | ((data[2]));
354  rawData.temperature = (int16_t)((data[3]) << 8) | (data[4]);
355  rawData.pressure /= 16;
356  rawData.temperature /= 16;
357 
358  /* Copy Raw samples to Streaming Buffer. */
359  memcpy(streamingPacket + STREAMING_HEADER_LEN, &rawData, MPL3115_STREAM_DATA_SIZE);
360  /* Send streaming packet to Host. */
361  Host_IO_Send(streamingPacket, sizeof(streamingPacket), HOST_FORMAT_HDLC);
362  if (toggle_led++ == LED_TOGGLE_RATE)
363  { /* Toggle LED at a refresh rate that is perceivable to indicate application is active. */
364  toggle_led = 0;
365  pGpioDriver->toggle_pin(&GREEN_LED);
366  }
367  }
368 }
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
#define MPL3115_CTRL_REG1_SBYB_MASK
Definition: mpl3115.h:846
int32_t gSystick
Definition: mpl3115_demo.c:117
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
GENERIC_DRIVER_GPIO * pGpioDriver
Definition: mpl3115_demo.c:118
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_ALT_MASK
Definition: mpl3115.h:861
#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 SHIELD_NAME
const registerwritelist_t cMpl3115SetOST[]
Register settings for Triggring One-Shot Sampling.
Definition: mpl3115_demo.c:101
#define MPL3115_PT_DATA_CFG_PDEFE_ENABLED
Definition: mpl3115.h:567
bool process_host_command(uint8_t tag, uint8_t *hostCommand, uint8_t *hostResponse, size_t *hostMsgSize, size_t respBufferSize)
Definition: mpl3115_demo.c:130
#define ADS_MAX_STRING_LENGTH
#define MPL3115_STREAM_DATA_SIZE
Definition: mpl3115_demo.c:69
#define MPL3115_CTRL_REG1_OST_MASK
Definition: mpl3115.h:849
volatile bool bMpl3115DataReady
Definition: mpl3115_demo.c:115
#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
volatile bool bMpl3115Ready
Definition: mpl3115_demo.c:115
#define HOST_PRO_INT_CMD_TAG
Bit aligned values for Host Protocol Interface IDs (Bits 5-6).
Definition: host_io_uart.h:83
#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 APPLICATION_NAME
Unique Name for this application which should match the target GUI pkg name.
Definition: mpl3115_demo.c:73
#define MPL3115_PT_DATA_CFG_TDEFE_ENABLED
Definition: mpl3115.h:564
#define MPL3115_WHOAMI_VALUE
Definition: mpl3115.h:65
char embAppName[ADS_MAX_STRING_LENGTH]
Definition: mpl3115_demo.c:114
gpioHandleKSDK_t GREEN_LED
Definition: frdm_k64f.c:214
This defines the sensor specific information.
Definition: mpl3115_drv.h:62
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
#define LED_TOGGLE_RATE
Definition: mpl3115_demo.c:70
#define MPL3115_CTRL_REG3_IPOL1_LOW
Definition: mpl3115.h:989
void(* clr_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:73
void mpl3115_int_data_ready_callback(void *pUserData)
Definition: mpl3115_demo.c:124
void BOARD_SystickEnable(void)
Function to enable systicks framework.
Definition: systick_utils.c:70
#define I2C_S_DEVICE_INDEX
Definition: issdk_hal.h:61
const registerreadlist_t cMpl3115OutputNormal[]
Address and size of Raw Pressure+Temperature Data in Normal Mode.
Definition: mpl3115_demo.c:107
#define MPL3115_CTRL_REG4_INT_EN_DRDY_INTENABLED
Definition: mpl3115.h:1070
#define I2C_S_DRIVER
Definition: issdk_hal.h:59
#define MPL3115_CTRL_REG1_ALT_BAR
Definition: mpl3115.h:889
uint8_t gStreamID
Definition: mpl3115_demo.c:116
GENERIC_DRIVER_GPIO Driver_GPIO_KSDK
Definition: gpio_driver.c:203
int main(void)
Main function.
Definition: mpl3115_demo.c:213
#define MPL3115_DATA_SIZE
Definition: mpl3115_demo.c:68
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
char boardString[ADS_MAX_STRING_LENGTH]
Definition: mpl3115_demo.c:113
#define BOARD_DEBUG_UART_BAUDRATE
Definition: board.h:57
The mpl3115_drv.h file describes the MPL3115 driver interface and structures.
char shieldString[ADS_MAX_STRING_LENGTH]
Definition: mpl3115_demo.c:113
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
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.
volatile bool bStreamingEnabled
Definition: mpl3115_demo.c:115
fxls8962_acceldataUser_t rawData
#define MPL3115_CTRL_REG1_SBYB_STANDBY
Definition: mpl3115.h:868
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 MPL3115_CTRL_REG4_INT_EN_DRDY_MASK
Definition: mpl3115.h:1048
#define STREAMING_HEADER_LEN
Definition: host_io_uart.h:51
const registerwritelist_t cMpl3115ConfigINT[]
Register settings for Interrupt (non buffered) Enablement with ONe-Shot Mode.
Definition: mpl3115_demo.c:81
#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 APPLICATION_VERSION
Version to distinguish between instances the same application based on target Shield and updates...
Definition: mpl3115_demo.c:75
#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
#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
#define MPL3115_CTRL_REG3_IPOL1_HIGH
Definition: mpl3115.h:990