ISSDK  1.8
IoT Sensing Software Development Kit
mma845x_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 mma845x_demo.c
11  * @brief The mma845x_demo.c file implements the ISSDK MMA845x 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 "gpio_driver.h"
33 #include "mma845x_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 MMA845x_STREAM_DATA_SIZE 11 /* 6+1 byte Data */
42 
43 /*! @brief Unique Name for this application which should match the target GUI pkg name. */
44 #define APPLICATION_NAME "MMA845x Accelerometer Demo"
45 /*! @brief Version to distinguish between instances the same application based on target Shield and updates. */
46 #define APPLICATION_VERSION "2.5"
47 
48 typedef struct
49 {
50  uint32_t timestamp; /*!< Time stamp value in micro-seconds. */
51  int16_t accel[3]; /*!< The accel data */
52  uint8_t intsrc;
54 
55 //-----------------------------------------------------------------------
56 // Constants
57 //-----------------------------------------------------------------------
58 /*! Prepare the register write list to configure MMA845x in non-FIFO and ISR mode. */
60  /*! Configure the MMA845x to disable FIFO */
61  {MMA845x_F_SETUP, 0x00, 0},
62  /*! Configure the MMA845x XYZ_DATA_CFG to set FS Range 2G. */
64  /*! Configure the MMA845x CTRL_REG1 to set ODR to 100Hz. */
66  /*! Configure the MMA845x CTRL_REG2 to set the Oversampling mode to High Resolution. */
68  /*! Configure the MMA845x CTRL_REG3 to set the Interrupt polarity to ACTIVE high. */
70  /*! Configure the MMA845x CTRL_REG4 to enable the data ready interrupt. */
71  /*! Configure the MMA845x to set interrupt polarity as Active High. */
73  /*! Configure the MMA845x to set interrupt polarity as Active High. */
74  {MMA845x_PL_COUNT, 0xFF, 0},
75  /*! Configure the MMA845x to set interrupt polarity as Active High. */
77  /*! Configure the MMA845x to set interrupt polarity as Active High. */
78  {MMA845x_FF_MT_COUNT, 0x02, 0},
79  /*! Configure the MMA845x to set interrupt polarity as Active High. */
81  /*! Configure the MMA845x to set interrupt polarity as Active High. */
83  /*! Configure the MMA845x to set interrupt polarity as Active High. */
85  /*! Configure the MMA845x to set interrupt polarity as Active High. */
86  {MMA845x_TRANSIENT_COUNT, 0x0C, 0},
87  /*! Configure the MMA845x to set interrupt polarity as Active High. */
89  /*! Configure the MMA845x to set interrupt polarity as Active High. */
90  {MMA845x_PULSE_THSX, 0x37, 0},
91  /*! Configure the MMA845x to set interrupt polarity as Active High. */
92  {MMA845x_PULSE_THSY, 0x37, 0},
93  /*! Configure the MMA845x to set interrupt polarity as Active High. */
94  {MMA845x_PULSE_THSZ, 0x52, 0},
95  /*! Configure the MMA845x to set interrupt polarity as Active High. */
96  {MMA845x_PULSE_TMLT, 0x30, 0},
97  /*! Configure the MMA845x to set interrupt polarity as Active High. */
98  {MMA845x_PULSE_LTCY, 0xF0, 0},
99  /*! Configure the MMA845x to set interrupt polarity as Active High. */
100  {MMA845x_PULSE_WIND, 0x3C, 0},
101  /*! Configure the MMA845x to enable Interrupts for Data Ready. */
103  /*! Configure the MMA845x CTRL_REG5 to route Interrupt to INT1 pin. */
106 
107 /*! Prepare the register read list to read the raw Accel data from MMA845x. */
110 /*! Prepare the register read for Status Register. */
112  {.readFrom = MMA845x_INT_SOURCE, .numBytes = 1}, __END_READ_DATA__};
114  {.readFrom = MMA845x_PL_STATUS, .numBytes = 1}, __END_READ_DATA__};
115 
116 //-----------------------------------------------------------------------
117 // Global Variables
118 //-----------------------------------------------------------------------
121 volatile bool bStreamingEnabled = false, bMma845xDataReady = false, bMma845xReady = false;
122 uint8_t gStreamID; /* The auto assigned Stream ID. */
125 
126 //-----------------------------------------------------------------------
127 // Functions
128 //-----------------------------------------------------------------------
129 /* This is the Sensor Data Ready ISR implementation.*/
130 void mma845x_int_data_ready_callback(void *pUserData)
131 { /*! @brief Set flag to indicate Sensor has signalled data ready. */
132  bMma845xDataReady = true;
133 }
134 
135 /* Handler for Device Info and Streaming Control Commands. */
137  uint8_t tag, uint8_t *hostCommand, uint8_t *hostResponse, size_t *hostMsgSize, size_t respBufferSize)
138 {
139  bool success = false;
140 
141  /* If it is Host requesting Device Info, send Board Name and Shield Name. */
142  if (tag == HOST_PRO_INT_DEV_TAG)
143  { /* Byte 1 : Payload - Length of APPLICATION_NAME (b)
144  * Bytes=b : Payload Application Name
145  * Byte b+1 : Payload - Length of BOARD_NAME (s)
146  * Bytes=s : Payload Board Name
147  * Byte b+s+2 : Payload - Length of SHIELD_NAME (v)
148  * Bytes=v : Payload Shield Name */
149 
150  size_t appNameLen = strlen(embAppName);
151  size_t boardNameLen = strlen(boardString);
152  size_t shieldNameLen = strlen(shieldString);
153 
154  if (respBufferSize >= boardNameLen + shieldNameLen + appNameLen + 3)
155  { /* We have sufficient buffer. */
156  *hostMsgSize = 0;
157  }
158  else
159  {
160  return false;
161  }
162 
163  hostResponse[*hostMsgSize] = appNameLen;
164  *hostMsgSize += 1;
165 
166  memcpy(hostResponse + *hostMsgSize, embAppName, appNameLen);
167  *hostMsgSize += appNameLen;
168 
169  hostResponse[*hostMsgSize] = boardNameLen;
170  *hostMsgSize += 1;
171 
172  memcpy(hostResponse + *hostMsgSize, boardString, boardNameLen);
173  *hostMsgSize += boardNameLen;
174 
175  hostResponse[*hostMsgSize] = shieldNameLen;
176  *hostMsgSize += 1;
177 
178  memcpy(hostResponse + *hostMsgSize, shieldString, shieldNameLen);
179  *hostMsgSize += shieldNameLen;
180 
181  return true;
182  }
183 
184  /* If it is Host sending Streaming Commands, take necessary actions. */
185  if ((tag == (HOST_PRO_INT_CMD_TAG | HOST_PRO_CMD_W_CFG_TAG)) &&
187  { /* Byte 1 : Payload - Operation Code (Start/Stop Operation Code)
188  * Byte 2 : Payload - Stream ID (Target Stream for carrying out operation) */
189  switch (hostCommand[0]) /* Execute desired operation (Start/Stop) on the requested Stream. */
190  {
191  case HOST_CMD_START:
192  if (hostCommand[1] == gStreamID && bMma845xReady && bStreamingEnabled == false)
193  {
195  bStreamingEnabled = true;
196  success = true;
197  }
198  break;
199  case HOST_CMD_STOP:
200  if (hostCommand[1] == gStreamID && bMma845xReady && bStreamingEnabled == true)
201  {
202  pGpioDriver->clr_pin(&GREEN_LED);
203  bStreamingEnabled = false;
204  success = true;
205  }
206  break;
207  default:
208  break;
209  }
210  *hostMsgSize = 0; /* Zero payload in response. */
211  }
212 
213  return success;
214 }
215 
216 /*!
217  * @brief Main function
218  */
219 int main(void)
220 {
221  int32_t status;
223 
224  mma845x_i2c_sensorhandle_t mma845xDriver;
226 
227  ARM_DRIVER_I2C *pI2cDriver = &I2C_S_DRIVER;
228  ARM_DRIVER_USART *pUartDriver = &HOST_S_DRIVER;
229 
230  /*! Initialize the MCU hardware. */
233 
234  /* Create the Short Application Name String for ADS. */
235  sprintf(embAppName, "%s:%s", APPLICATION_NAME, APPLICATION_VERSION);
236 
237  /* Run ADS. */
239 
240  /* Create the Full Application Name String for Device Info Response. */
242 
243  /*! Initialize INT1 MMA845x pin used by FRDM board */
245 
246  /*! Initialize GREEN LED pin used by FRDM board */
247  pGpioDriver->pin_init(&GREEN_LED, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
248 
249  /*! Initialize the I2C driver. */
250  status = pI2cDriver->Initialize(I2C_S_SIGNAL_EVENT);
251  if (ARM_DRIVER_OK != status)
252  {
253  return -1;
254  }
255 
256  /*! Set the I2C Power mode. */
257  status = pI2cDriver->PowerControl(ARM_POWER_FULL);
258  if (ARM_DRIVER_OK != status)
259  {
260  return -1;
261  }
262 
263  /*! Set the I2C bus speed. */
264  status = pI2cDriver->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
265  if (ARM_DRIVER_OK != status)
266  {
267  return -1;
268  }
269 
270  /*! Initialize the UART driver. */
271  status = pUartDriver->Initialize(HOST_S_SIGNAL_EVENT);
272  if (ARM_DRIVER_OK != status)
273  {
274  return -1;
275  }
276 
277  /*! Set the UART Power mode. */
278  status = pUartDriver->PowerControl(ARM_POWER_FULL);
279  if (ARM_DRIVER_OK != status)
280  {
281  return -1;
282  }
283 
284  /*! Set UART Baud Rate. */
285  status = pUartDriver->Control(ARM_USART_MODE_ASYNCHRONOUS, BOARD_DEBUG_UART_BAUDRATE);
286  if (ARM_DRIVER_OK != status)
287  {
288  return -1;
289  }
290 
291  /*! Initialize the MMA845x sensor driver. */
294  if (SENSOR_ERROR_NONE == status)
295  {
296  /*! Set the task to be executed while waiting for I2C transactions to complete. */
298 
299  /*! Configure the MMA845x sensor driver. */
300  status = MMA845x_I2C_Configure(&mma845xDriver, mma845x_Config_Isr);
301  if (SENSOR_ERROR_NONE == status)
302  {
303  bMma845xReady = true;
304  }
305  }
306 
307  /*! Initialize streaming and assign a Stream ID. */
308  gStreamID =
309  Host_IO_Init(pUartDriver, (void *)mma845xDriver.pCommDrv, &mma845xDriver.deviceInfo, NULL, MMA845x_I2C_ADDR);
310  /* Confirm if a valid Stream ID has been allocated for this stream. */
311  if (0 == gStreamID)
312  {
313  bMma845xReady = false;
314  }
315  else
316  {
317  /*! Populate streaming header. */
319  pGpioDriver->clr_pin(&GREEN_LED); /* Set LED to indicate application is ready. */
320  }
321 
322  for (;;) /* Forever loop */
323  { /* Call UART Non-Blocking Receive. */
325 
326  /* Process packets only if streaming has been enabled by Host and ISR is available.
327  * In ISR Mode we do not need to check Data Ready Register.
328  * The receipt of interrupt will indicate data is ready. */
329  if (false == bStreamingEnabled || false == bMma845xDataReady)
330  {
331  SMC_SetPowerModeWait(SMC); /* Power save, wait if nothing to do. */
332  continue;
333  }
334  else
335  { /*! Clear the data ready flag, it will be set again by the ISR. */
336  bMma845xDataReady = false;
337  pGpioDriver->toggle_pin(&GREEN_LED);
338  }
339 
340  /*! Read raw sensor data from the MMA845x. */
341  status = MMA845x_I2C_ReadData(&mma845xDriver, mma845x_Output_Values, data);
342  if (ARM_DRIVER_OK != status)
343  { /* Loop, if sample read failed. */
344  continue;
345  }
346 
347  /* Update timestamp from Systick framework. */
349 
350  /*! Convert the raw sensor data to signed 32-bit and 16-bit containers for display to the debug port. */
351  rawData.accel[0] = ((int16_t)data[0] << 8) | data[1];
352  rawData.accel[0] /= 4;
353  rawData.accel[1] = ((int16_t)data[2] << 8) | data[3];
354  rawData.accel[1] /= 4;
355  rawData.accel[2] = ((int16_t)data[4] << 8) | data[5];
356  rawData.accel[2] /= 4;
357 
358  /*! Read INT_SRC 0x0C from MMA845x. */
359  status = MMA845x_I2C_ReadData(&mma845xDriver, cMma845xqStatus, &intsrcdata);
360  status = MMA845x_I2C_ReadData(&mma845xDriver, cMma845xqPLStatus, &PLstatusdata);
361  /* Send INTSRC */
362  rawData.intsrc = intsrcdata;
363 
364  /* Copy Raw samples to Streaming Buffer. */
365  memcpy(streamingPacket + STREAMING_HEADER_LEN, &rawData, MMA845x_STREAM_DATA_SIZE);
366  /* Send streaming packet to Host. */
367  Host_IO_Send(streamingPacket, sizeof(streamingPacket), HOST_FORMAT_HDLC);
368  }
369 }
ARM_DRIVER_I2C * pCommDrv
Definition: mma845x_drv.h:32
#define MMA8451_WHO_AM_I_WHOAMI_VALUE
Definition: mma845x.h:482
#define MMA845x_CTRL_REG5_INT_CFG_DRDY_INT1
Definition: mma845x.h:1895
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 mma845x_int_data_ready_callback(void *pUserData)
Definition: mma845x_demo.c:130
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
char embAppName[ADS_MAX_STRING_LENGTH]
Definition: mma845x_demo.c:120
int32_t status
char shieldString[ADS_MAX_STRING_LENGTH]
Definition: mma845x_demo.c:119
volatile bool bMma845xReady
Definition: mma845x_demo.c:121
#define MMA845x_FF_MT_CFG_XEFE_ENABLED
Definition: mma845x.h:832
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
const registerreadlist_t mma845x_Output_Values[]
Definition: mma845x_demo.c:108
The host_io_uart.h file contains the Host Protocol interface definitions and configuration.
int32_t gSystick
Definition: mma845x_demo.c:123
#define MMA845x_STREAM_DATA_SIZE
Definition: mma845x_demo.c:41
#define APPLICATION_VERSION
Version to distinguish between instances the same application based on target Shield and updates...
Definition: mma845x_demo.c:46
#define HOST_PRO_CMD_W_CFG_TAG
Definition: host_io_uart.h:63
#define MMA845x_CTRL_REG4_INT_EN_PULSE_ENABLED
Definition: mma845x.h:1824
int32_t MMA845x_I2C_Configure(mma845x_i2c_sensorhandle_t *pSensorHandle, const registerwritelist_t *pRegWriteList)
The interface function to configure he sensor.
Definition: mma845x_drv.c:61
Access structure of the GPIO Driver.
Definition: Driver_GPIO.h:38
#define SMC
Definition: lpc54114.h:118
int32_t MMA845x_I2C_Initialize(mma845x_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: mma845x_drv.c:22
uint8_t gStreamID
Definition: mma845x_demo.c:122
#define __END_WRITE_DATA__
Definition: sensor_drv.h:45
void MMA845x_I2C_SetIdleTask(mma845x_i2c_sensorhandle_t *pSensorHandle, registeridlefunction_t idleTask, void *userParam)
: The interface function to set the I2C Idle Task.
Definition: mma845x_drv.c:53
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 MMA845x_PL_CFG_DBCNTM_CLR
Definition: mma845x.h:674
GENERIC_DRIVER_GPIO * pGpioDriver
Definition: mma845x_demo.c:124
#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
#define BOARD_BootClockRUN
Definition: clock_config.h:19
#define MMA845x_FF_MT_THS_DBCNTM_CLR
Definition: mma845x.h:953
void(* registeridlefunction_t)(void *userParam)
This is the register idle function type.
Definition: sensor_drv.h:97
bool process_host_command(uint8_t tag, uint8_t *hostCommand, uint8_t *hostResponse, size_t *hostMsgSize, size_t respBufferSize)
Definition: mma845x_demo.c:136
uint8_t streamingPacket[STREAMING_HEADER_LEN+FXLS8962_STREAM_DATA_SIZE]
GENERIC_DRIVER_GPIO Driver_GPIO_KSDK
Definition: gpio_driver.c:177
const registerreadlist_t cMma845xqStatus[]
Definition: mma845x_demo.c:111
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
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]
#define MMA845x_CTRL_REG2_MODS_HIGHRES
Definition: mma845x.h:1669
#define MMA845x_FF_MT_CFG_ZEFE_ENABLED
Definition: mma845x.h:838
#define __END_READ_DATA__
Definition: sensor_drv.h:51
#define HOST_PRO_INT_DEV_TAG
Definition: host_io_uart.h:59
#define MMA845x_XYZ_DATA_CFG_FS_FS_RANGE_2G
Definition: mma845x.h:520
void BOARD_RunADS(const char *appName, char *boardString, char *shieldString, size_t bufferLength)
The function to register Application Name and initialte ADS.
#define MMA845x_FF_MT_CFG_OAE_FREEFALL
Definition: mma845x.h:840
volatile bool bMma845xDataReady
Definition: mma845x_demo.c:121
fxos8700_accelmagdata_t rawData
registerDeviceInfo_t deviceInfo
Definition: mma845x_drv.h:31
const registerreadlist_t cMma845xqPLStatus[]
Definition: mma845x_demo.c:113
#define MMA845x_TRANSIENT_CFG_YTEFE_ENABLED
Definition: mma845x.h:1045
uint16_t readFrom
Definition: sensor_drv.h:80
const registerwritelist_t mma845x_Config_Isr[]
Definition: mma845x_demo.c:59
int main(void)
Main function.
Definition: mma845x_demo.c:219
void(* toggle_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:48
#define ADS_MAX_STRING_LENGTH
#define MMA845x_CTRL_REG4_INT_EN_FF_MT_ENABLED
Definition: mma845x.h:1822
#define MMA845x_TRANSIENT_CFG_XTEFE_ENABLED
Definition: mma845x.h:1042
#define MMA845x_ACCEL_DATA_SIZE
The size of the MMA845x accel data.
Definition: mma845x_drv.h:47
#define MMA845x_CTRL_REG3_IPOL_HIGH
Definition: mma845x.h:1744
void BOARD_SystickEnable(void)
Function to enable systicks framework.
Definition: systick_utils.c:35
#define STREAMING_HEADER_LEN
Definition: host_io_uart.h:25
#define MMA845x_CTRL_REG4_INT_EN_LNDPRT_ENABLED
Definition: mma845x.h:1827
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
This structure defines the Read command List.
Definition: sensor_drv.h:78
ARM Systick Utilities.
gpioHandleKSDK_t GREEN_LED
Definition: frdm_k64f.c:188
char boardString[ADS_MAX_STRING_LENGTH]
Definition: mma845x_demo.c:119
#define MMA845x_TRANSIENT_CFG_ZTEFE_ENABLED
Definition: mma845x.h:1048
#define I2C_S_DEVICE_INDEX
Definition: issdk_hal.h:35
#define MMA845x_PL_CFG_PL_EN_ENABLED
Definition: mma845x.h:671
This defines the sensor specific information.
Definition: mma845x_drv.h:29
#define MMA845x_CTRL_REG1_DR_100HZ
Definition: mma845x.h:1608
The mma845x_drv.h file describes the MMA845x driver interface and structures.
#define MMA845x_CTRL_REG1_DR_MASK
Definition: mma845x.h:1589
#define APPLICATION_NAME
Unique Name for this application which should match the target GUI pkg name.
Definition: mma845x_demo.c:44
#define MMA845x_FF_MT_CFG_YEFE_ENABLED
Definition: mma845x.h:835
#define MMA845x_PULSE_CFG_YSPEFE_ENABLED
Definition: mma845x.h:1259
#define HOST_PRO_INT_CMD_TAG
Bit aligned values for Host Protocol Interface IDs (Bits 5-6).
Definition: host_io_uart.h:57
#define MMA845x_INT1
Definition: frdm_kl25z.h:103
#define SHIELD_NAME
#define MMA845x_XYZ_DATA_CFG_FS_MASK
Definition: mma845x.h:510
#define MMA845x_PULSE_CFG_ZSPEFE_ENABLED
Definition: mma845x.h:1263
#define MMA845x_TRANSIENT_THS_DBCNTM_CLR
Definition: mma845x.h:1163
#define MMA845x_CTRL_REG4_INT_EN_TRANS_ENABLED
Definition: mma845x.h:1829
volatile bool bStreamingEnabled
Definition: mma845x_demo.c:121
#define MMA845x_CTRL_REG4_INT_EN_DRDY_ENABLED
Definition: mma845x.h:1820
int32_t MMA845x_I2C_ReadData(mma845x_i2c_sensorhandle_t *pSensorHandle, const registerreadlist_t *pReadList, uint8_t *pBuffer)
The interface function to read the sensor data.
Definition: mma845x_drv.c:104
#define MMA845x_I2C_ADDR
Definition: frdm_kl25z.h:102
#define MMA845x_PULSE_CFG_XSPEFE_ENABLED
Definition: mma845x.h:1255
#define HOST_S_DRIVER
Definition: frdm_k64f.h:93