ISSDK  1.8
IoT Sensing Software Development Kit
mma865x_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 mma865x_demo.c
11  * @brief The mma865x_demo.c file implements the ISSDK MMA865x sensor
12  * demo example demonstration with interrupt 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 "mma865x_drv.h"
33 #include "gpio_driver.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 MMA865x_ACCEL_DATA_SIZE 6 /* 2 byte X,Y,Z Axis Data each. */
42 #define MMA865x_STREAM_DATA_SIZE 11
43 
44 /*! @brief Unique Name for this application which should match the target GUI pkg name. */
45 #define APPLICATION_NAME "MMA8652 Accelerometer Demo"
46 /*! @brief Version to distinguish between instances the same application based on target Shield and updates. */
47 #define APPLICATION_VERSION "2.5"
48 
49 typedef struct
50 {
51  uint32_t timestamp; /*!< Time stamp value in micro-seconds. */
52  int16_t accel[3]; /*!< The accel data */
53  uint8_t intsrc;
55 
56 //-----------------------------------------------------------------------
57 // Constants
58 //-----------------------------------------------------------------------
59 /*! Prepare the register write list to configure MMA865x in non-FIFO and ISR mode. */
61 { /*! Clear F_SETUP. */
62  {MMA865x_F_SETUP, 0x00, 0},
63  /*! Configure the MMA865x to set FS Range as 2g. */
65  /*! Configure the MMA865x to set ODR to 100Hz. */
67  /*! Configure the MMA865x to set High Resolution mode. */
69  /*! Configure the MMA865x to set interrupt polarity as Active High. */
71  /*! Configure the MMA865x to set interrupt polarity as Active High. */
73  /*! Configure the MMA865x to set interrupt polarity as Active High. */
74  {MMA865x_PL_COUNT, 0xFF, 0},
75  /*! Configure the MMA865x to set interrupt polarity as Active High. */
77  /*! Configure the MMA865x to set interrupt polarity as Active High. */
78  {MMA865x_FF_MT_COUNT, 0x01, 0},
79  /*! Configure the MMA865x to set interrupt polarity as Active High. */
81  /*! Configure the MMA865x to set interrupt polarity as Active High. */
84  {MMA865x_TRANSIENT_COUNT, 0x0C, 0},
85  /*! Configure the MMA865x to set interrupt polarity as Active High. */
87  {MMA865x_PULSE_THSX, 0x37, 0},
88  /*! Configure the MMA865x to set interrupt polarity as Active High. */
89  {MMA865x_PULSE_THSY, 0x37, 0},
90  /*! Configure the MMA865x to set interrupt polarity as Active High. */
91  {MMA865x_PULSE_THSZ, 0x52, 0},
92  /*! Configure the MMA865x to set interrupt polarity as Active High. */
93  {MMA865x_PULSE_TMLT, 0x30, 0},
94  /*! Configure the MMA865x to set interrupt polarity as Active High. */
95  {MMA865x_PULSE_LTCY, 0x3C, 0},
96  /*! Configure the MMA865x to enable Interrupts for Data Ready. */
98  /*! Configure the MMA865x to route Data Ready Interrupts to INT1. */
101 
102 /*! Prepare the register read list to read the raw Accel data from MMA865x. */
105 /*! Prepare the register read for INT Status Register. */
107 /*! Prepare the register read for PL Status Register. */
109 
110 //-----------------------------------------------------------------------
111 // Global Variables
112 //-----------------------------------------------------------------------
115 volatile bool bStreamingEnabled = false, bMma865xDataReady = false, bMma865xReady = 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 mma865x_int_data_ready_callback(void *pUserData)
125 { /*! @brief Set flag to indicate Sensor has signalled data ready. */
126  bMma865xDataReady = 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 && bMma865xReady && bStreamingEnabled == false)
187  {
189  bStreamingEnabled = true;
190  success = true;
191  }
192  break;
193  case HOST_CMD_STOP:
194  if (hostCommand[1] == gStreamID && bMma865xReady && 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;
217 
218  mma865x_i2c_sensorhandle_t mma865xDriver;
220 
221  ARM_DRIVER_I2C *pI2Cdriver = &I2C_S_DRIVER;
222  ARM_DRIVER_USART *pUartDriver = &HOST_S_DRIVER;
223 
224  /*! Initialize the MCU hardware. */
227 
228  /* Create the Short Application Name String for ADS. */
229  sprintf(embAppName, "%s:%s", APPLICATION_NAME, APPLICATION_VERSION);
230 
231  /* Run ADS. */
233 
234  /* Create the Full Application Name String for Device Info Response. */
236 
237  /*! Initialize MMA865x pin used by FRDM board */
239 
240  /*! Initialize RGB LED pin used by FRDM board */
241  pGpioDriver->pin_init(&GREEN_LED, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
242 
243  /*! Initialize the I2C driver. */
244  status = pI2Cdriver->Initialize(I2C_S_SIGNAL_EVENT);
245  if (ARM_DRIVER_OK != status)
246  {
247  return -1;
248  }
249 
250  /*! Set the I2C Power mode. */
251  status = pI2Cdriver->PowerControl(ARM_POWER_FULL);
252  if (ARM_DRIVER_OK != status)
253  {
254  return -1;
255  }
256 
257  /*! Set the I2C bus speed. */
258  status = pI2Cdriver->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
259  if (ARM_DRIVER_OK != status)
260  {
261  return -1;
262  }
263 
264  /*! Initialize the UART driver. */
265  status = pUartDriver->Initialize(HOST_S_SIGNAL_EVENT);
266  if (ARM_DRIVER_OK != status)
267  {
268  return -1;
269  }
270 
271  /*! Set the UART Power mode. */
272  status = pUartDriver->PowerControl(ARM_POWER_FULL);
273  if (ARM_DRIVER_OK != status)
274  {
275  return -1;
276  }
277 
278  /*! Set UART Baud Rate. */
279  status = pUartDriver->Control(ARM_USART_MODE_ASYNCHRONOUS, BOARD_DEBUG_UART_BAUDRATE);
280  if (ARM_DRIVER_OK != status)
281  {
282  return -1;
283  }
284 
285  /*! Initialize MMA865x sensor driver. */
288  if (SENSOR_ERROR_NONE == status)
289  {
290  /*! Set the task to be executed while waiting for SPI transactions to complete. */
292 
293  /*! Configure the MMA865x sensor. */
294  status = MMA865x_I2C_Configure(&mma865xDriver, cMma865xConfigInterrupt);
295  if (SENSOR_ERROR_NONE == status)
296  {
297  bMma865xReady = true;
298  }
299  }
300 
301  /*! Initialize streaming and assign a Stream ID. */
302  gStreamID =
303  Host_IO_Init(pUartDriver, (void *)mma865xDriver.pCommDrv, &mma865xDriver.deviceInfo, NULL, MMA8652_I2C_ADDR);
304  /* Confirm if a valid Stream ID has been allocated for this stream. */
305  if (0 == gStreamID)
306  {
307  bMma865xReady = false;
308  }
309  else
310  {
311  /*! Populate streaming header. */
313  pGpioDriver->clr_pin(&GREEN_LED);
314  }
315 
316  for (;;) /* Forever loop */
317  { /* Call UART Non-Blocking Receive. */
319 
320  /* Process packets only if streaming has been enabled by Host and ISR is available.
321  * In ISR Mode we do not need to check Data Ready Register.
322  * The receipt of interrupt will indicate data is ready. */
323  if (false == bStreamingEnabled || false == bMma865xDataReady)
324  {
325  SMC_SetPowerModeWait(SMC); /* Power save, wait if nothing to do. */
326  continue;
327  }
328  else
329  { /*! Clear the data ready flag, it will be set again by the ISR. */
330  bMma865xDataReady = false;
331  pGpioDriver->toggle_pin(&GREEN_LED);
332  }
333 
334  /*! Read new raw sensor data from the MMA865x. */
335  status = MMA865x_I2C_ReadData(&mma865xDriver, cMma865xOutputValues, data);
336  if (ARM_DRIVER_OK != status)
337  { /* Loop, if sample read failed. */
338  continue;
339  }
340 
341  /* Update timestamp from Systick framework. */
343 
344  /*! Convert the raw sensor data to signed 16-bit container for display to the debug port. */
345  rawData.accel[0] = ((int16_t)data[0] << 8) | data[1];
346  rawData.accel[0] /= 16;
347  rawData.accel[1] = ((int16_t)data[2] << 8) | data[3];
348  rawData.accel[1] /= 16;
349  rawData.accel[2] = ((int16_t)data[4] << 8) | data[5];
350  rawData.accel[2] /= 16;
351 
352  /*! Read INT_SRC 0x0C from MMA865x. */
353  status = MMA865x_I2C_ReadData(&mma865xDriver, cMma8652qStatus, &intsrcdata);
354  /*! Read PL_STATUS 0x10 from MMA865x. */
355  status = MMA865x_I2C_ReadData(&mma865xDriver, cMma8652qPLStatus, &PLstatusdata);
356  /* Send INTSRC */
357  rawData.intsrc = intsrcdata;
358 
359  /* Copy Raw samples to Streaming Buffer. */
360  memcpy(streamingPacket + STREAMING_HEADER_LEN, &rawData, MMA865x_STREAM_DATA_SIZE);
361  /* Send streaming packet to Host. */
362  Host_IO_Send(streamingPacket, sizeof(streamingPacket), HOST_FORMAT_HDLC);
363  }
364 }
#define MMA865x_CTRL_REG1_DR_MASK
Definition: mma865x.h:1534
void mma865x_int_data_ready_callback(void *pUserData)
Definition: mma865x_demo.c:124
ARM_DRIVER_I2C * pCommDrv
Definition: mma865x_drv.h:32
uint8_t Host_IO_Init(ARM_DRIVER_USART *pDrv, void *pBus, void *pDevInfo, void *spiSlaveParams, uint16_t slaveAddress)
Definition: host_io_uart.c:100
const registerreadlist_t cMma8652qStatus[]
Definition: mma865x_demo.c:106
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
This defines the sensor specific information.
Definition: mma865x_drv.h:29
int32_t status
char embAppName[ADS_MAX_STRING_LENGTH]
Definition: mma865x_demo.c:114
bool process_host_command(uint8_t tag, uint8_t *hostCommand, uint8_t *hostResponse, size_t *hostMsgSize, size_t respBufferSize)
Definition: mma865x_demo.c:130
#define MMA865x_CTRL_REG4_INT_EN_PULSE_EN
Definition: mma865x.h:1774
#define MMA865x_XYZ_DATA_CFG_FS_MASK
Definition: mma865x.h:530
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
#define MMA865x_STREAM_DATA_SIZE
Definition: mma865x_demo.c:42
The host_io_uart.h file contains the Host Protocol interface definitions and configuration.
#define MMA865x_CTRL_REG5_INT_CFG_DRDY_INT1
Definition: mma865x.h:1853
#define MMA865x_FF_MT_CFG_XEFE_EN
Definition: mma865x.h:881
volatile bool bMma865xDataReady
Definition: mma865x_demo.c:115
#define MMA8652_INT1
#define HOST_PRO_CMD_W_CFG_TAG
Definition: host_io_uart.h:63
Access structure of the GPIO Driver.
Definition: Driver_GPIO.h:38
char boardString[ADS_MAX_STRING_LENGTH]
Definition: mma865x_demo.c:113
#define SMC
Definition: lpc54114.h:118
#define __END_WRITE_DATA__
Definition: sensor_drv.h:45
#define MMA865x_CTRL_REG3_IPOL_MASK
Definition: mma865x.h:1664
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 APPLICATION_VERSION
Version to distinguish between instances the same application based on target Shield and updates...
Definition: mma865x_demo.c:47
#define MMA865x_FF_MT_THS_DBCNTM_INC_CLR
Definition: mma865x.h:994
int32_t MMA865x_I2C_ReadData(mma865x_i2c_sensorhandle_t *pSensorHandle, const registerreadlist_t *pReadList, uint8_t *pBuffer)
The interface function to read the sensor data.
Definition: mma865x_drv.c:106
char shieldString[ADS_MAX_STRING_LENGTH]
Definition: mma865x_demo.c:113
#define MMA865x_CTRL_REG3_IPOL_ACTIVE_HIGH
Definition: mma865x.h:1701
#define I2C_S_SIGNAL_EVENT
Definition: issdk_hal.h:34
#define MMA865x_FF_MT_CFG_ZEFE_EN
Definition: mma865x.h:875
#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 MMA865x_CTRL_REG4_INT_EN_FF_MT_EN
Definition: mma865x.h:1776
#define BOARD_DEBUG_UART_BAUDRATE
Definition: board.h:31
#define MMA865x_CTRL_REG2_MODS_MASK
Definition: mma865x.h:1592
#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 bMma865xReady
Definition: mma865x_demo.c:115
#define MMA865x_PULSE_CFG_XSPEFE_EN
Definition: mma865x.h:1290
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
#define MMA865x_PULSE_CFG_ZSPEFE_EN
Definition: mma865x.h:1282
void BOARD_SystickStart(int32_t *pStart)
Function to Record the Start systick.
Definition: systick_utils.c:44
#define MMA865x_TRANSIENT_CFG_ZTEFE_EN
Definition: mma865x.h:1062
uint32_t BOARD_SystickElapsedTime_us(int32_t *pStart)
Function to compute the Elapsed Time.
Definition: systick_utils.c:64
uint8_t data[FXLS8962_DATA_SIZE]
int32_t MMA865x_I2C_Configure(mma865x_i2c_sensorhandle_t *pSensorHandle, const registerwritelist_t *pRegWriteList)
The interface function to configure he sensor.
Definition: mma865x_drv.c:61
#define MMA865x_TRANSIENT_THS_DBCNTM_INC_CLR
Definition: mma865x.h:1191
#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.
The mma865x_drv.h file describes the MMA865x driver interface and structures.
#define MMA865x_CTRL_REG4_INT_EN_LNDPRT_EN
Definition: mma865x.h:1772
#define MMA865x_CTRL_REG4_INT_EN_DRDY_EN
Definition: mma865x.h:1778
#define MMA865x_TRANSIENT_CFG_XTEFE_EN
Definition: mma865x.h:1070
fxos8700_accelmagdata_t rawData
const registerwritelist_t cMma865xConfigInterrupt[]
Definition: mma865x_demo.c:60
const registerreadlist_t cMma8652qPLStatus[]
Definition: mma865x_demo.c:108
uint16_t readFrom
Definition: sensor_drv.h:80
registerDeviceInfo_t deviceInfo
Definition: mma865x_drv.h:31
void(* toggle_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:48
#define ADS_MAX_STRING_LENGTH
#define MMA865x_PL_CFG_PL_EN_EN
Definition: mma865x.h:700
#define MMA865x_CTRL_REG2_MODS_HR
Definition: mma865x.h:1623
#define MMA865x_CTRL_REG4_INT_EN_TRANS_EN
Definition: mma865x.h:1770
void BOARD_SystickEnable(void)
Function to enable systicks framework.
Definition: systick_utils.c:35
#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
#define MMA865x_TRANSIENT_CFG_YTEFE_EN
Definition: mma865x.h:1066
#define MMA865x_ACCEL_DATA_SIZE
Definition: mma865x_demo.c:41
#define MMA865x_XYZ_DATA_CFG_FS_2G
Definition: mma865x.h:542
void(* pin_init)(pinID_t aPinId, gpio_direction_t dir, void *apPinConfig, gpio_isr_handler_t aIsrHandler, void *apUserData)
Definition: Driver_GPIO.h:41
This structure defines the Read command List.
Definition: sensor_drv.h:78
#define MMA8652_I2C_ADDR
ARM Systick Utilities.
#define MMA865x_PL_CFG_DBCNTM_CLEAR
Definition: mma865x.h:697
gpioHandleKSDK_t GREEN_LED
Definition: frdm_k64f.c:188
#define I2C_S_DEVICE_INDEX
Definition: issdk_hal.h:35
void MMA865x_I2C_SetIdleTask(mma865x_i2c_sensorhandle_t *pSensorHandle, registeridlefunction_t idleTask, void *userParam)
: The interface function to set the I2C Idle Task.
Definition: mma865x_drv.c:53
const registerreadlist_t cMma865xOutputValues[]
Definition: mma865x_demo.c:103
#define APPLICATION_NAME
Unique Name for this application which should match the target GUI pkg name.
Definition: mma865x_demo.c:45
#define HOST_PRO_INT_CMD_TAG
Bit aligned values for Host Protocol Interface IDs (Bits 5-6).
Definition: host_io_uart.h:57
#define MMA8652_WHOAMI_VALUE
Definition: mma865x.h:65
int32_t gSystick
Definition: mma865x_demo.c:117
#define SHIELD_NAME
int32_t MMA865x_I2C_Initialize(mma865x_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: mma865x_drv.c:22
int main(void)
Main function.
Definition: mma865x_demo.c:213
#define MMA865x_FF_MT_CFG_OAE_FREEFALL
Definition: mma865x.h:872
uint8_t gStreamID
Definition: mma865x_demo.c:116
volatile bool bStreamingEnabled
Definition: mma865x_demo.c:115
#define MMA865x_PULSE_CFG_YSPEFE_EN
Definition: mma865x.h:1286
GENERIC_DRIVER_GPIO * pGpioDriver
Definition: mma865x_demo.c:118
#define HOST_S_DRIVER
Definition: frdm_k64f.h:93
#define MMA865x_CTRL_REG1_DR_100HZ
Definition: mma865x.h:1551
#define MMA865x_FF_MT_CFG_YEFE_EN
Definition: mma865x.h:878