ISSDK  1.8
IoT Sensing Software Development Kit
mpl3115_oneShot_demo.c
Go to the documentation of this file.
1 /*
2  * Copyright 2016-2017 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 /**
9  * @file mpl3115_oneShot_demo.c
10  * @brief The mpl3115_oneShot_demo.c file implements the ISSDK MPL3115 sensor
11  * demo example demonstration with One-Shot mode.
12  */
13 
14 //-----------------------------------------------------------------------
15 // SDK Includes
16 //-----------------------------------------------------------------------
17 #include "board.h"
18 #include "pin_mux.h"
19 #include "clock_config.h"
20 
21 //-----------------------------------------------------------------------
22 // CMSIS Includes
23 //-----------------------------------------------------------------------
24 #include "Driver_I2C.h"
25 #include "Driver_USART.h"
26 
27 //-----------------------------------------------------------------------
28 // ISSDK Includes
29 //-----------------------------------------------------------------------
30 #include "issdk_hal.h"
31 #include "gpio_driver.h"
32 #include "mpl3115_drv.h"
33 #include "host_io_uart.h"
34 #include "systick_utils.h"
35 #include "auto_detection_service.h"
36 
37 //-----------------------------------------------------------------------
38 // Macros
39 //-----------------------------------------------------------------------
40 #define MPL3115_DATA_SIZE 5 /* 3 byte Pressure and 2 byte Temperature. */
41 #define MPL3115_STREAM_DATA_SIZE 10 /* 6 byte Data */
42 #define LED_TOGGLE_RATE 100 /* Toggle LED after every 100 samples. */
43 
44 /*! @brief Unique Name for this application which should match the target GUI pkg name. */
45 #define APPLICATION_NAME "MPL3115A2"
46 /*! @brief Version to distinguish between instances the same application based on target Shield and updates. */
47 #define APPLICATION_VERSION "1.1"
48 
49 //-----------------------------------------------------------------------
50 // Constants
51 //-----------------------------------------------------------------------
52 /*! @brief Register settings for Interrupt (non buffered) Enablement with ONe-Shot Mode. */
54  /* Enable Data Ready and Event flags for Pressure, Temperature or either. */
58  /* Set the One ShoT Bit and 100Hz OSR. */
61  /* Set INT1 Active High. */
63  /* Enable Interrupts for Data Ready Events. */
65  /* Route Interrupt to INT1. */
68 
69 /*! @brief Register settings for Triggring One-Shot Sampling. */
71  /* Set the One ShoT Bit. */
74 
75 /*! @brief Address and size of Raw Pressure+Temperature Data in Normal Mode. */
78 
79 //-----------------------------------------------------------------------
80 // Global Variables
81 //-----------------------------------------------------------------------
84 volatile bool bStreamingEnabled = false, bMpl3115DataReady = false, bMpl3115Ready = false;
85 uint8_t gStreamID; /* The auto assigned Stream ID. */
87 
88 //-----------------------------------------------------------------------
89 // Functions
90 //-----------------------------------------------------------------------
91 /* This is the Sensor Data Ready ISR implementation.*/
92 void mpl3115_int_data_ready_callback(void *pUserData)
93 { /*! @brief Set flag to indicate Sensor has signalled data ready. */
94  bMpl3115DataReady = true;
95 }
96 
97 /* Handler for Device Info and Streaming Control Commands. */
99  uint8_t tag, uint8_t *hostCommand, uint8_t *hostResponse, size_t *hostMsgSize, size_t respBufferSize)
100 {
101  bool success = false;
102 
103  /* If it is Host requesting Device Info, send Board Name and Shield Name. */
104  if (tag == HOST_PRO_INT_DEV_TAG)
105  { /* Byte 1 : Payload - Length of APPLICATION_NAME (b)
106  * Bytes=b : Payload Application Name
107  * Byte b+1 : Payload - Length of BOARD_NAME (s)
108  * Bytes=s : Payload Board Name
109  * Byte b+s+2 : Payload - Length of SHIELD_NAME (v)
110  * Bytes=v : Payload Shield Name */
111 
112  size_t appNameLen = strlen(embAppName);
113  size_t boardNameLen = strlen(boardString);
114  size_t shieldNameLen = strlen(shieldString);
115 
116  if (respBufferSize >= boardNameLen + shieldNameLen + appNameLen + 3)
117  { /* We have sufficient buffer. */
118  *hostMsgSize = 0;
119  }
120  else
121  {
122  return false;
123  }
124 
125  hostResponse[*hostMsgSize] = appNameLen;
126  *hostMsgSize += 1;
127 
128  memcpy(hostResponse + *hostMsgSize, embAppName, appNameLen);
129  *hostMsgSize += appNameLen;
130 
131  hostResponse[*hostMsgSize] = boardNameLen;
132  *hostMsgSize += 1;
133 
134  memcpy(hostResponse + *hostMsgSize, boardString, boardNameLen);
135  *hostMsgSize += boardNameLen;
136 
137  hostResponse[*hostMsgSize] = shieldNameLen;
138  *hostMsgSize += 1;
139 
140  memcpy(hostResponse + *hostMsgSize, shieldString, shieldNameLen);
141  *hostMsgSize += shieldNameLen;
142 
143  return true;
144  }
145 
146  /* If it is Host sending Streaming Commands, take necessary actions. */
147  if ((tag == (HOST_PRO_INT_CMD_TAG | HOST_PRO_CMD_W_CFG_TAG)) &&
149  { /* Byte 1 : Payload - Operation Code (Start/Stop Operation Code)
150  * Byte 2 : Payload - Stream ID (Target Stream for carrying out operation) */
151  switch (hostCommand[0]) /* Execute desired operation (Start/Stop) on the requested Stream. */
152  {
153  case HOST_CMD_START:
154  if (hostCommand[1] == gStreamID && bMpl3115Ready && bStreamingEnabled == false)
155  {
156  bStreamingEnabled = true;
157  success = true;
158  }
159  break;
160  case HOST_CMD_STOP:
161  if (hostCommand[1] == gStreamID && bMpl3115Ready && bStreamingEnabled == true)
162  {
163  pGpioDriver->clr_pin(&GREEN_LED);
164  bStreamingEnabled = false;
165  success = true;
166  }
167  break;
168  default:
169  break;
170  }
171  *hostMsgSize = 0; /* Zero payload in response. */
172  }
173 
174  return success;
175 }
176 
177 /*!
178  * @brief Main function
179  */
180 int main(void)
181 {
182  int32_t status, systick;
183  uint8_t toggle_led = 0;
185 
186  mpl3115_i2c_sensorhandle_t mpl3115Driver;
188 
189  ARM_DRIVER_I2C *pI2cDriver = &I2C_S_DRIVER;
190  ARM_DRIVER_USART *pUartDriver = &HOST_S_DRIVER;
191 
192  /*! Initialize the MCU hardware. */
195 
196  /* Create the Short Application Name String for ADS. */
197  sprintf(embAppName, "%s:%s", APPLICATION_NAME, APPLICATION_VERSION);
198 
199  /* Run ADS. */
201 
202  /* Create the Full Application Name String for Device Info Response. */
204 
205  /*! Initialize INT1 MPL3115 pin used by FRDM board */
207 
208  /*! Initialize GREEN LED pin used by FRDM board */
209  pGpioDriver->pin_init(&GREEN_LED, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
210 
211  /*! Initialize the I2C driver. */
212  status = pI2cDriver->Initialize(I2C_S_SIGNAL_EVENT);
213  if (ARM_DRIVER_OK != status)
214  {
215  return -1;
216  }
217 
218  /*! Set the I2C Power mode. */
219  status = pI2cDriver->PowerControl(ARM_POWER_FULL);
220  if (ARM_DRIVER_OK != status)
221  {
222  return -1;
223  }
224 
225  /*! Set the I2C bus speed. */
226  status = pI2cDriver->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
227  if (ARM_DRIVER_OK != status)
228  {
229  return -1;
230  }
231 
232  /*! Initialize the UART driver. */
233  status = pUartDriver->Initialize(HOST_S_SIGNAL_EVENT);
234  if (ARM_DRIVER_OK != status)
235  {
236  return -1;
237  }
238 
239  /*! Set the UART Power mode. */
240  status = pUartDriver->PowerControl(ARM_POWER_FULL);
241  if (ARM_DRIVER_OK != status)
242  {
243  return -1;
244  }
245 
246  /*! Set UART Baud Rate. */
247  status = pUartDriver->Control(ARM_USART_MODE_ASYNCHRONOUS, BOARD_DEBUG_UART_BAUDRATE);
248  if (ARM_DRIVER_OK != status)
249  {
250  return -1;
251  }
252 
253  /*! Initialize the MPL3115 sensor driver. */
256  if (SENSOR_ERROR_NONE == status)
257  {
258  /*! Set the task to be executed while waiting for I2C transactions to complete. */
260 
261  /*! We do not call MPL3115_I2C_Configure() in this case as we are going to read samples on demand.
262  * We explicitly only enable ISR settings. */
263  status = Sensor_I2C_Write(mpl3115Driver.pCommDrv, &mpl3115Driver.deviceInfo, mpl3115Driver.slaveAddress,
264  cMpl3115ConfigINT);
265  if (ARM_DRIVER_OK == status)
266  { /* Ready only if INT Configure write success. */
267  bMpl3115Ready = true;
268  }
269  }
270 
271  /*! Initialize streaming and assign a Stream ID. */
272  gStreamID =
273  Host_IO_Init(pUartDriver, (void *)mpl3115Driver.pCommDrv, &mpl3115Driver.deviceInfo, NULL, MPL3115_I2C_ADDR);
274  /* Confirm if a valid Stream ID has been allocated for this stream. */
275  if (0 == gStreamID)
276  {
277  bMpl3115Ready = false;
278  }
279  else
280  {
281  /*! Populate streaming header. */
283  BOARD_SystickStart(&systick);
284  pGpioDriver->clr_pin(&GREEN_LED); /* Set LED to indicate application is ready. */
285  }
286 
287  for (;;) /* Forever loop */
288  { /* Call UART Non-Blocking Receive. */
290 
291  /* Process packets only if streaming has been enabled by Host and ODR has expired.
292  * The receipt of ODR interrupt will indicate data should be ready. */
293  if (false == bStreamingEnabled || false == bMpl3115DataReady)
294  {
295  SMC_SetPowerModeWait(SMC); /* Power save, wait if nothing to do. */
296  continue;
297  }
298  else
299  { /*! Clear the data ready flag, it will be set again by the ODR CB. */
300  bMpl3115DataReady = false;
301  }
302 
303  /* Trigger acquisition of the Next Sample. */
304  status = Sensor_I2C_Write(mpl3115Driver.pCommDrv, &mpl3115Driver.deviceInfo, mpl3115Driver.slaveAddress,
305  cMpl3115SetOST);
306  if (ARM_DRIVER_OK != status)
307  { /* Exit if OST write failed. */
308  return -1;
309  }
310 
311  /*! Read raw sensor data from the MPL3115. */
312  status = MPL3115_I2C_ReadData(&mpl3115Driver, cMpl3115OutputNormal, data);
313  if (ARM_DRIVER_OK != status)
314  { /* Exit if sample read failed. */
315  return -1;
316  }
317 
318  /* Update timestamp from Systick framework. */
319  rawData.timestamp += BOARD_SystickElapsedTime_us(&systick);
320 
321  /*! Convert the raw sensor data to signed 32-bit and 16-bit containers for display to the debug port. */
322  rawData.pressure = (uint32_t)((data[0]) << 16) | ((data[1]) << 8) | ((data[2]));
323  rawData.temperature = (int16_t)((data[3]) << 8) | (data[4]);
324  rawData.pressure /= 16;
325  rawData.temperature /= 16;
326 
327  /* Copy Raw samples to Streaming Buffer. */
328  memcpy(streamingPacket + STREAMING_HEADER_LEN, &rawData, MPL3115_STREAM_DATA_SIZE);
329  /* Send streaming packet to Host. */
330  Host_IO_Send(streamingPacket, sizeof(streamingPacket), HOST_FORMAT_HDLC);
331  if (toggle_led++ == LED_TOGGLE_RATE)
332  { /* Toggle LED at a refresh rate that is perceivable to indicate application is active. */
333  toggle_led = 0;
334  pGpioDriver->toggle_pin(&GREEN_LED);
335  }
336  }
337 }
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
int32_t status
volatile bool bMpl3115Ready
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
const registerreadlist_t cMpl3115OutputNormal[]
Address and size of Raw Pressure+Temperature Data in Normal Mode.
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 APPLICATION_VERSION
Version to distinguish between instances the same application based on target Shield and updates...
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
void mpl3115_int_data_ready_callback(void *pUserData)
#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
bool process_host_command(uint8_t tag, uint8_t *hostCommand, uint8_t *hostResponse, size_t *hostMsgSize, size_t respBufferSize)
int main(void)
Main function.
#define I2C_S_SIGNAL_EVENT
Definition: issdk_hal.h:34
uint8_t gStreamID
#define MPL3115_CTRL_REG4_INT_EN_DRDY_MASK
Definition: mpl3115.h:1048
#define I2C_S_DRIVER
Definition: issdk_hal.h:33
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
GENERIC_DRIVER_GPIO * pGpioDriver
#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 registerwritelist_t cMpl3115SetOST[]
Register settings for Triggring One-Shot Sampling.
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
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
const registerwritelist_t cMpl3115ConfigINT[]
Register settings for Interrupt (non buffered) Enablement with ONe-Shot Mode.
This defines the sensor specific information.
Definition: mpl3115_drv.h:36
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 APPLICATION_NAME
Unique Name for this application which should match the target GUI pkg name.
uint16_t readFrom
Definition: sensor_drv.h:80
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
volatile bool bStreamingEnabled
#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
volatile bool bMpl3115DataReady
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
This structure defines the Read command List.
Definition: sensor_drv.h:78
ARM Systick Utilities.
#define MPL3115_DATA_SIZE
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_STREAM_DATA_SIZE
#define MPL3115_I2C_ADDR
char boardString[ADS_MAX_STRING_LENGTH]
#define LED_TOGGLE_RATE
#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 shieldString[ADS_MAX_STRING_LENGTH]
registerDeviceInfo_t deviceInfo
Definition: mpl3115_drv.h:38
#define MPL3115_CTRL_REG1_OS_MASK
Definition: mpl3115.h:855
char embAppName[ADS_MAX_STRING_LENGTH]
#define HOST_S_DRIVER
Definition: frdm_k64f.h:93