ISSDK  1.8
IoT Sensing Software Development Kit
mpl3115_poll_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_poll_demo.c
11  * @brief The mpl3115_poll_demo.c file implements the ISSDK MPL3115 sensor
12  * demo example demonstration with One-Shot Poll 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  /* Enable Data Ready and Event flags for Pressure, Temperature or either. */
63 
64 /*! @brief Register settings for Triggring One-Shot Sampling. */
66  /* Set the One ShoT Bit. */
69 
70 /*! @brief Address of Register containing OST Bit. */
72 
73 /*! @brief Address and size of Raw Pressure+Temperature Data in Normal Mode. */
76 
77 //-----------------------------------------------------------------------
78 // Global Variables
79 //-----------------------------------------------------------------------
82 volatile bool bStreamingEnabled = false, bMpl3115Ready = false;
83 uint8_t gStreamID; /* The auto assigned Stream ID. */
86 
87 //-----------------------------------------------------------------------
88 // Functions
89 //-----------------------------------------------------------------------
90 /* Handler for Device Info and Streaming Control Commands. */
92  uint8_t tag, uint8_t *hostCommand, uint8_t *hostResponse, size_t *hostMsgSize, size_t respBufferSize)
93 {
94  bool success = false;
95 
96  /* If it is Host requesting Device Info, send Board Name and Shield Name. */
97  if (tag == HOST_PRO_INT_DEV_TAG)
98  { /* Byte 1 : Payload - Length of APPLICATION_NAME (b)
99  * Bytes=b : Payload Application Name
100  * Byte b+1 : Payload - Length of BOARD_NAME (s)
101  * Bytes=s : Payload Board Name
102  * Byte b+s+2 : Payload - Length of SHIELD_NAME (v)
103  * Bytes=v : Payload Shield Name */
104 
105  size_t appNameLen = strlen(embAppName);
106  size_t boardNameLen = strlen(boardString);
107  size_t shieldNameLen = strlen(shieldString);
108 
109  if (respBufferSize >= boardNameLen + shieldNameLen + appNameLen + 3)
110  { /* We have sufficient buffer. */
111  *hostMsgSize = 0;
112  }
113  else
114  {
115  return false;
116  }
117 
118  hostResponse[*hostMsgSize] = appNameLen;
119  *hostMsgSize += 1;
120 
121  memcpy(hostResponse + *hostMsgSize, embAppName, appNameLen);
122  *hostMsgSize += appNameLen;
123 
124  hostResponse[*hostMsgSize] = boardNameLen;
125  *hostMsgSize += 1;
126 
127  memcpy(hostResponse + *hostMsgSize, boardString, boardNameLen);
128  *hostMsgSize += boardNameLen;
129 
130  hostResponse[*hostMsgSize] = shieldNameLen;
131  *hostMsgSize += 1;
132 
133  memcpy(hostResponse + *hostMsgSize, shieldString, shieldNameLen);
134  *hostMsgSize += shieldNameLen;
135 
136  return true;
137  }
138 
139  /* If it is Host sending Streaming Commands, take necessary actions. */
140  if ((tag == (HOST_PRO_INT_CMD_TAG | HOST_PRO_CMD_W_CFG_TAG)) &&
142  { /* Byte 1 : Payload - Operation Code (Start/Stop Operation Code)
143  * Byte 2 : Payload - Stream ID (Target Stream for carrying out operation) */
144  switch (hostCommand[0]) /* Execute desired operation (Start/Stop) on the requested Stream. */
145  {
146  case HOST_CMD_START:
147  if (hostCommand[1] == gStreamID && bMpl3115Ready && bStreamingEnabled == false)
148  {
150  bStreamingEnabled = true;
151  success = true;
152  }
153  break;
154  case HOST_CMD_STOP:
155  if (hostCommand[1] == gStreamID && bMpl3115Ready && bStreamingEnabled == true)
156  {
157  pGpioDriver->clr_pin(&GREEN_LED);
158  bStreamingEnabled = false;
159  success = true;
160  }
161  break;
162  default:
163  break;
164  }
165  *hostMsgSize = 0; /* Zero payload in response. */
166  }
167 
168  return success;
169 }
170 
171 /*!
172  * @brief Main function
173  */
174 int main(void)
175 {
176  int32_t status;
177  uint8_t dataReady, toggle_led = 0;
179 
180  mpl3115_i2c_sensorhandle_t mpl3115Driver;
182 
183  ARM_DRIVER_I2C *pI2cDriver = &I2C_S_DRIVER;
184  ARM_DRIVER_USART *pUartDriver = &HOST_S_DRIVER;
185 
186  /*! Initialize the MCU hardware. */
189 
190  /* Create the Short Application Name String for ADS. */
191  sprintf(embAppName, "%s:%s", APPLICATION_NAME, APPLICATION_VERSION);
192 
193  /* Run ADS. */
195 
196  /* Create the Full Application Name String for Device Info Response. */
198 
199  /*! Initialize GREEN LED pin used by FRDM board */
200  pGpioDriver->pin_init(&GREEN_LED, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
201 
202  /*! Initialize the I2C driver. */
203  status = pI2cDriver->Initialize(I2C_S_SIGNAL_EVENT);
204  if (ARM_DRIVER_OK != status)
205  {
206  return -1;
207  }
208 
209  /*! Set the I2C Power mode. */
210  status = pI2cDriver->PowerControl(ARM_POWER_FULL);
211  if (ARM_DRIVER_OK != status)
212  {
213  return -1;
214  }
215 
216  /*! Set the I2C bus speed. */
217  status = pI2cDriver->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
218  if (ARM_DRIVER_OK != status)
219  {
220  return -1;
221  }
222 
223  /*! Initialize the UART driver. */
224  status = pUartDriver->Initialize(HOST_S_SIGNAL_EVENT);
225  if (ARM_DRIVER_OK != status)
226  {
227  return -1;
228  }
229 
230  /*! Set the UART Power mode. */
231  status = pUartDriver->PowerControl(ARM_POWER_FULL);
232  if (ARM_DRIVER_OK != status)
233  {
234  return -1;
235  }
236 
237  /*! Set UART Baud Rate. */
238  status = pUartDriver->Control(ARM_USART_MODE_ASYNCHRONOUS, BOARD_DEBUG_UART_BAUDRATE);
239  if (ARM_DRIVER_OK != status)
240  {
241  return -1;
242  }
243 
244  /*! Initialize the MPL3115 sensor driver. */
247  if (SENSOR_ERROR_NONE == status)
248  { /*! Set the task to be executed while waiting for I2C transactions to complete. */
250 
251  /*! We do not call MPL3115_I2C_Configure() in this case as we are going to read samples on demand.
252  * We explicitly only enable ISR settings. */
253  status = Sensor_I2C_Write(mpl3115Driver.pCommDrv, &mpl3115Driver.deviceInfo, mpl3115Driver.slaveAddress,
254  cMpl3115ConfigPoll);
255  if (ARM_DRIVER_OK == status)
256  { /* Ready only if INT Configure write success. */
257  bMpl3115Ready = true;
258  }
259  }
260 
261  /*! Initialize streaming and assign a Stream ID. */
262  gStreamID =
263  Host_IO_Init(pUartDriver, (void *)mpl3115Driver.pCommDrv, &mpl3115Driver.deviceInfo, NULL, MPL3115_I2C_ADDR);
264  /* Confirm if a valid Stream ID has been allocated for this stream. */
265  if (0 == gStreamID)
266  {
267  bMpl3115Ready = false;
268  }
269  else
270  {
271  /*! Populate streaming header. */
273  pGpioDriver->clr_pin(&GREEN_LED); /* Set LED to indicate application is ready. */
274  }
275 
276  for (;;) /* Forever loop */
277  { /* Call UART Non-Blocking Receive. */
279 
280  /* Process packets only if streaming has been enabled by Host. */
281  if (false == bStreamingEnabled)
282  {
283  SMC_SetPowerModeWait(SMC); /* Power save, wait if nothing to do. */
284  continue;
285  }
286 
287  /* Trigger acquisition of the Next Sample. */
288  status = Sensor_I2C_Write(mpl3115Driver.pCommDrv, &mpl3115Driver.deviceInfo, mpl3115Driver.slaveAddress,
289  cMpl3115SetOST);
290  if (ARM_DRIVER_OK != status)
291  { /* Exit if OST write failed. */
292  return -1;
293  }
294 
295  do /*! Keep checking the OST FLAG for completion. */
296  {
297  status = MPL3115_I2C_ReadData(&mpl3115Driver, cMpl3115GetOST, &dataReady);
298  if (ARM_DRIVER_OK != status)
299  {
300  return -1;
301  }
302  /* Call UART Non-Blocking Receive while waiting for OST. */
304  } /* Loop, untill sample acquisition is not completed. */
306 
307  /*! Read raw sensor data from the MPL3115. */
308  status = MPL3115_I2C_ReadData(&mpl3115Driver, cMpl3115OutputNormal, data);
309  if (ARM_DRIVER_OK != status)
310  { /* Exit if sample read failed. */
311  return -1;
312  }
313 
314  /* Update timestamp from Systick framework. */
316 
317  /*! Convert the raw sensor data to signed 32-bit and 16-bit containers for display to the debug port. */
318  rawData.pressure = (uint32_t)((data[0]) << 16) | ((data[1]) << 8) | ((data[2]));
319  rawData.temperature = (int16_t)((data[3]) << 8) | (data[4]);
320  rawData.pressure /= 16;
321  rawData.temperature /= 16;
322 
323  /* Copy Raw samples to Streaming Buffer. */
324  memcpy(streamingPacket + STREAMING_HEADER_LEN, &rawData, MPL3115_STREAM_DATA_SIZE);
325  /* Send streaming packet to Host. */
326  Host_IO_Send(streamingPacket, sizeof(streamingPacket), HOST_FORMAT_HDLC);
327  if (toggle_led++ == LED_TOGGLE_RATE)
328  { /* Toggle LED at a refresh rate that is perceivable to indicate application is active. */
329  toggle_led = 0;
330  pGpioDriver->toggle_pin(&GREEN_LED);
331  }
332  }
333 }
const registerwritelist_t cMpl3115ConfigPoll[]
Register settings for Interrupt (non buffered) Enablement with ONe-Shot Mode.
const registerreadlist_t cMpl3115OutputNormal[]
Address and size of Raw Pressure+Temperature Data in Normal Mode.
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
bool process_host_command(uint8_t tag, uint8_t *hostCommand, uint8_t *hostResponse, size_t *hostMsgSize, size_t respBufferSize)
int32_t status
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
uint8_t gStreamID
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
int main(void)
Main function.
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
char shieldString[ADS_MAX_STRING_LENGTH]
GENERIC_DRIVER_GPIO * pGpioDriver
const registerwritelist_t cMpl3115SetOST[]
Register settings for Triggring One-Shot Sampling.
#define __END_WRITE_DATA__
Definition: sensor_drv.h:45
#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 I2C_S_DRIVER
Definition: issdk_hal.h:33
const registerreadlist_t cMpl3115GetOST[]
Address of Register containing OST Bit.
void Host_IO_Add_ISO_Header(uint8_t streamID, uint8_t *pStreamingPacket, size_t sizePayload)
Definition: host_io_uart.c:86
char embAppName[ADS_MAX_STRING_LENGTH]
#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 BOARD_BootClockRUN
Definition: clock_config.h:19
void(* registeridlefunction_t)(void *userParam)
This is the register idle function type.
Definition: sensor_drv.h:97
volatile bool bMpl3115Ready
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
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
uint32_t BOARD_SystickElapsedTime_us(int32_t *pStart)
Function to compute the Elapsed Time.
Definition: systick_utils.c:64
#define MPL3115_PT_DATA_CFG_DREM_ENABLED
Definition: mpl3115.h:570
#define APPLICATION_VERSION
Version to distinguish between instances the same application based on target Shield and updates...
uint8_t data[FXLS8962_DATA_SIZE]
char boardString[ADS_MAX_STRING_LENGTH]
#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_RESET
Definition: mpl3115.h:870
#define MPL3115_CTRL_REG1_OST_MASK
Definition: mpl3115.h:849
#define MPL3115_STREAM_DATA_SIZE
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
#define MPL3115_DATA_SIZE
#define MPL3115_CTRL_REG1_SBYB_MASK
Definition: mpl3115.h:846
uint16_t readFrom
Definition: sensor_drv.h:80
#define LED_TOGGLE_RATE
#define MPL3115_CTRL_REG1_SBYB_STANDBY
Definition: mpl3115.h:868
void(* toggle_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:48
#define ADS_MAX_STRING_LENGTH
#define MPL3115_PT_DATA_CFG_DREM_MASK
Definition: mpl3115.h:556
#define MPL3115_PT_DATA_CFG_PDEFE_ENABLED
Definition: mpl3115.h:567
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
#define APPLICATION_NAME
Unique Name for this application which should match the target GUI pkg name.
This structure defines the Read command List.
Definition: sensor_drv.h:78
ARM Systick Utilities.
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
volatile bool bStreamingEnabled
#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
int32_t gSystick
registerDeviceInfo_t deviceInfo
Definition: mpl3115_drv.h:38
#define MPL3115_CTRL_REG1_OS_MASK
Definition: mpl3115.h:855
#define HOST_S_DRIVER
Definition: frdm_k64f.h:93