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