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