ISSDK  1.8
IoT Sensing Software Development Kit
mma9553_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 mma9553_demo.c
11  * @brief The mma9553_demo.c file implements the ISSDK MMA9553L sensor driver
12  * example demonstration as a Accelerometer in I2C Mode.
13  */
14 
15 /* SDK Includes */
16 #include "board.h"
17 #include "pin_mux.h"
18 #include "fsl_lptmr.h"
19 #include "clock_config.h"
20 
21 /* CMSIS Includes */
22 #include "Driver_I2C.h"
23 
24 /* ISSDK Includes */
25 #include "issdk_hal.h"
26 #include "gpio_driver.h"
27 #include "mma9553_drv.h"
28 #include "host_io_uart.h"
29 #include "systick_utils.h"
30 #include "auto_detection_service.h"
31 
32 /*******************************************************************************
33  * Macros
34  ******************************************************************************/
35 #define SAMPLING_RATE_ms (100) /* Timeout for the ODR Timer. */
36 #define MMA9553_ACCEL_DATA_SIZE (6) /* 2 byte X,Y,Z Axis Data each. */
37 #define mma9553_en_callback LPTMR0_IRQHandler /* Timer timeout Callback. */
38 
39 #define MMA9553_STREAM_DATA_SIZE (10)
40 
41 /*! @brief Unique Name for this application which should match the target GUI pkg name. */
42 #define APPLICATION_NAME "MMA9553 Pedometer Demo"
43 /*! @brief Version to distinguish between instances the same application based on target Shield and updates. */
44 #define APPLICATION_VERSION "2.5"
45 
46 /*******************************************************************************
47  * Constants
48  ******************************************************************************/
49 /*! Prepare the register write list to configure MMA9553L in 30Hz Mode. */
51  {SetFSRange_2g, 0, sizeof(SetFSRange_2g)}, /* Set FS Range 2G */
52  {SetSampleRate_30Hz, 0, sizeof(SetSampleRate_30Hz)}, /* Set Sensor Sampling Rate 30Hz */
53  {SetAFEPriority_for30Hz, 0, sizeof(SetAFEPriority_for30Hz)}, /* Set AFE Priority for 30Hz Sampling Rate */
54  {SetMBoxPriority_for30Hz, 0, sizeof(SetMBoxPriority_for30Hz)}, /* Set MBox Priority for 30Hz Sampling Rate */
56 
57 /*! Prepare the register read list to read the raw Accel data from MMA9553. */
60 
61 /*******************************************************************************
62  * Globals
63  ******************************************************************************/
66 volatile bool bStreamingEnabled = false, bMma9553DataReady = false, bMma9553Ready = false;
67 uint8_t gStreamID; /* The auto assigned Stream ID. */
70 
71 /*******************************************************************************
72  * Functions
73  ******************************************************************************/
74 /* LPTMR based ODR Callback function. */
76 {
77  LPTMR_ClearStatusFlags(LPTMR0, kLPTMR_TimerCompareFlag);
78  bMma9553DataReady = true;
79 }
80 
81 /* Handler for Device Info and Streaming Control Commands. */
83  uint8_t tag, uint8_t *hostCommand, uint8_t *hostResponse, size_t *hostMsgSize, size_t respBufferSize)
84 {
85  bool success = false;
86 
87  /* If it is Host requesting Device Info, send Board Name and Shield Name. */
88  if (tag == HOST_PRO_INT_DEV_TAG)
89  { /* Byte 1 : Payload - Length of APPLICATION_NAME (b)
90  * Bytes=b : Payload Application Name
91  * Byte b+1 : Payload - Length of BOARD_NAME (s)
92  * Bytes=s : Payload Board Name
93  * Byte b+s+2 : Payload - Length of SHIELD_NAME (v)
94  * Bytes=v : Payload Shield Name */
95 
96  size_t appNameLen = strlen(embAppName);
97  size_t boardNameLen = strlen(boardString);
98  size_t shieldNameLen = strlen(shieldString);
99 
100  if (respBufferSize >= boardNameLen + shieldNameLen + appNameLen + 3)
101  { /* We have sufficient buffer. */
102  *hostMsgSize = 0;
103  }
104  else
105  {
106  return false;
107  }
108 
109  hostResponse[*hostMsgSize] = appNameLen;
110  *hostMsgSize += 1;
111 
112  memcpy(hostResponse + *hostMsgSize, embAppName, appNameLen);
113  *hostMsgSize += appNameLen;
114 
115  hostResponse[*hostMsgSize] = boardNameLen;
116  *hostMsgSize += 1;
117 
118  memcpy(hostResponse + *hostMsgSize, boardString, boardNameLen);
119  *hostMsgSize += boardNameLen;
120 
121  hostResponse[*hostMsgSize] = shieldNameLen;
122  *hostMsgSize += 1;
123 
124  memcpy(hostResponse + *hostMsgSize, shieldString, shieldNameLen);
125  *hostMsgSize += shieldNameLen;
126 
127  return true;
128  }
129 
130  /* If it is Host sending Streaming Commands, take necessary actions. */
131  if ((tag == (HOST_PRO_INT_CMD_TAG | HOST_PRO_CMD_W_CFG_TAG)) &&
133  { /* Byte 1 : Payload - Operation Code (Start/Stop Operation Code)
134  * Byte 2 : Payload - Stream ID (Target Stream for carrying out operation) */
135  switch (hostCommand[0]) /* Execute desired operation (Start/Stop) on the requested Stream. */
136  {
137  case HOST_CMD_START:
138  if (hostCommand[1] == gStreamID && bMma9553Ready && bStreamingEnabled == false)
139  {
141  bStreamingEnabled = true;
142  success = true;
143  }
144  break;
145  case HOST_CMD_STOP:
146  if (hostCommand[1] == gStreamID && bMma9553Ready && bStreamingEnabled == true)
147  {
148  pGpioDriver->clr_pin(&GREEN_LED);
149  bStreamingEnabled = false;
150  success = true;
151  }
152  break;
153  default:
154  break;
155  }
156  *hostMsgSize = 0; /* Zero payload in response. */
157  }
158 
159  return success;
160 }
161 
162 /*!
163  * @brief Main function
164  */
165 int main(void)
166 {
167  int32_t status;
169 
170  lptmr_config_t lptmrConfig;
171  mma9553_i2c_sensorhandle_t mma9553Driver;
173 
174  ARM_DRIVER_I2C *pI2Cdriver = &I2C_S_DRIVER;
175  ARM_DRIVER_USART *pUartDriver = &HOST_S_DRIVER;
176 
177  /*! Initialize the MCU hardware */
180 
181  /* Create the Short Application Name String for ADS. */
182  sprintf(embAppName, "%s:%s", APPLICATION_NAME, APPLICATION_VERSION);
183 
184  /* Run ADS. */
186 
187  /* Create the Full Application Name String for Device Info Response. */
189 
190  /*! Initialize RGB LED pin used by FRDM board */
191  pGpioDriver->pin_init(&GREEN_LED, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
192 
193  /* Initialize ODR Timer. */
194  LPTMR_GetDefaultConfig(&lptmrConfig);
195  LPTMR_Init(LPTMR0, &lptmrConfig);
196  LPTMR_EnableInterrupts(LPTMR0, kLPTMR_TimerInterruptEnable);
197  LPTMR_SetTimerPeriod(LPTMR0, MSEC_TO_COUNT(SAMPLING_RATE_ms, CLOCK_GetFreq(kCLOCK_LpoClk)));
198  EnableIRQ(LPTMR0_IRQn);
199 
200  /*! Initialize the I2C driver. */
201  status = pI2Cdriver->Initialize(I2C_S_SIGNAL_EVENT);
202  if (ARM_DRIVER_OK != status)
203  {
204  return -1;
205  }
206 
207  /*! Set the I2C Power mode. */
208  status = pI2Cdriver->PowerControl(ARM_POWER_FULL);
209  if (ARM_DRIVER_OK != status)
210  {
211  return -1;
212  }
213 
214  /*! Set the I2C bus speed. */
215  status = pI2Cdriver->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
216  if (ARM_DRIVER_OK != status)
217  {
218  return -1;
219  }
220 
221  /*! Initialize the UART driver. */
222  status = pUartDriver->Initialize(HOST_S_SIGNAL_EVENT);
223  if (ARM_DRIVER_OK != status)
224  {
225  return -1;
226  }
227 
228  /*! Set the UART Power mode. */
229  status = pUartDriver->PowerControl(ARM_POWER_FULL);
230  if (ARM_DRIVER_OK != status)
231  {
232  return -1;
233  }
234 
235  /*! Set UART Baud Rate. */
236  status = pUartDriver->Control(ARM_USART_MODE_ASYNCHRONOUS, BOARD_DEBUG_UART_BAUDRATE);
237  if (ARM_DRIVER_OK != status)
238  {
239  return -1;
240  }
241 
242  /*! Initialize the MMA9553 sensor driver. */
244  if (SENSOR_ERROR_NONE == status)
245  {
246  /*! Set the task to be executed while waiting for I2C transactions to complete. */
248 
249  /*! Configure the MMA9553 sensor driver with 30Hz Mode settings. */
250  status = MMA9553_I2C_Configure(&mma9553Driver, cMma9553Config30Hz);
251  if (SENSOR_ERROR_NONE == status)
252  {
253  bMma9553Ready = true;
254  }
255  }
256 
257  /*! Initialize streaming and assign a Stream ID. */
258  gStreamID =
259  Host_IO_Init(pUartDriver, (void *)mma9553Driver.pCommDrv, &mma9553Driver.deviceInfo, NULL, MMA9553_I2C_ADDR);
260  /* Confirm if a valid Stream ID has been allocated for this stream. */
261  if (0 == gStreamID)
262  {
263  bMma9553Ready = false;
264  }
265  else
266  {
267  /*! Populate streaming header. */
269  pGpioDriver->clr_pin(&GREEN_LED);
270  }
271 
272  LPTMR_StartTimer(LPTMR0);
273  for (;;) /* Forever loop */
274  { /* Call UART Non-Blocking Receive. */
276 
277  /* Process packets only if streaming has been enabled by Host and ODR Timer ISR has expired. */
278  if (false == bStreamingEnabled || false == bMma9553DataReady)
279  {
280  SMC_SetPowerModeWait(SMC); /* Power save, wait if nothing to do. */
281  continue;
282  }
283  else
284  { /*! Clear the data ready flag, it will be set again by the ISR. */
285  bMma9553DataReady = false;
286  pGpioDriver->toggle_pin(&GREEN_LED);
287  }
288 
289  /*! Read the raw sensor data from the MMA9553. */
290  status = MMA9553_I2C_CommandResponse(&mma9553Driver, NULL, cMma9553ReadRawOutput, (uint8_t *)&rawData.accel);
291  if (ARM_DRIVER_OK != status)
292  {
293  continue;
294  }
295 
296  /* Update timestamp from Systick framework. */
298 
299  /* Copy Raw samples to Streaming Buffer. */
300  memcpy(streamingPacket + STREAMING_HEADER_LEN, &rawData, MMA9553_STREAM_DATA_SIZE);
301  /* Send streaming packet to Host. */
302  Host_IO_Send(streamingPacket, sizeof(streamingPacket), HOST_FORMAT_HDLC);
303  }
304 }
const registerreadlist_t cMma9553ReadRawOutput[]
Definition: mma9553_demo.c:58
This defines the sensor specific information for I2C.
Definition: mma9553_drv.h:44
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
int32_t status
void MMA9553_I2C_SetIdleTask(mma9553_i2c_sensorhandle_t *pSensorHandle, registeridlefunction_t idleTask, void *userParam)
: The interface function to set the I2C Idle Task.
Definition: mma9553_drv.c:414
const uint8_t SetSampleRate_30Hz[5]
Definition: mma9553_drv.c:72
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.
This structure defines the Block command List.
Definition: sensor_drv.h:87
#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
int16_t accel[3]
Definition: mma9553_drv.h:56
#define APPLICATION_NAME
Unique Name for this application which should match the target GUI pkg name.
Definition: mma9553_demo.c:42
void(* clr_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:47
int32_t MMA9553_I2C_Configure(mma9553_i2c_sensorhandle_t *pSensorHandle, const registercommandlist_t *pCommandList)
The interface function to configure he sensor.
Definition: mma9553_drv.c:422
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
The mma9553_drv.h file describes the MMA9553L driver interface and structures.
int32_t gSystick
Definition: mma9553_demo.c:68
This structure defines the mma9553 pedometer data buffer.
Definition: mma9553_drv.h:53
int main(void)
Main function.
Definition: mma9553_demo.c:165
#define I2C_S_SIGNAL_EVENT
Definition: issdk_hal.h:34
#define I2C_S_DRIVER
Definition: issdk_hal.h:33
uint8_t gStreamID
Definition: mma9553_demo.c:67
const uint8_t SetFSRange_2g[5]
Full-Scale Range Selections.
Definition: mma9553_drv.c:63
volatile bool bStreamingEnabled
Definition: mma9553_demo.c:66
void Host_IO_Add_ISO_Header(uint8_t streamID, uint8_t *pStreamingPacket, size_t sizePayload)
Definition: host_io_uart.c:86
#define MMA9553_XYZ_DATA_OFFSET
XYZ Data Register Offset.
Definition: mma9553.h:23
GENERIC_DRIVER_GPIO * pGpioDriver
Definition: mma9553_demo.c:69
#define BOARD_DEBUG_UART_BAUDRATE
Definition: board.h:31
#define MMA9553_I2C_ADDR
#define BOARD_BootClockRUN
Definition: clock_config.h:19
const uint8_t SetAFEPriority_for30Hz[5]
Definition: mma9553_drv.c:82
void(* registeridlefunction_t)(void *userParam)
This is the register idle function type.
Definition: sensor_drv.h:97
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
volatile bool bMma9553DataReady
Definition: mma9553_demo.c:66
int32_t MMA9553_I2C_CommandResponse(mma9553_i2c_sensorhandle_t *pSensorHandle, const registercommandlist_t *pCommandList, const registerreadlist_t *pResponseList, uint8_t *pBuffer)
The interface function to read the sensor data.
Definition: mma9553_drv.c:487
uint32_t BOARD_SystickElapsedTime_us(int32_t *pStart)
Function to compute the Elapsed Time.
Definition: systick_utils.c:64
char embAppName[ADS_MAX_STRING_LENGTH]
Definition: mma9553_demo.c:65
bool process_host_command(uint8_t tag, uint8_t *hostCommand, uint8_t *hostResponse, size_t *hostMsgSize, size_t respBufferSize)
Definition: mma9553_demo.c:82
volatile bool bMma9553Ready
Definition: mma9553_demo.c:66
#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 __END_WRITE_CMD__
Definition: sensor_drv.h:57
#define MMA9553_ACCEL_DATA_SIZE
Definition: mma9553_demo.c:36
fxos8700_accelmagdata_t rawData
int32_t MMA9553_I2C_Initialize(mma9553_i2c_sensorhandle_t *pSensorHandle, ARM_DRIVER_I2C *pBus, uint8_t index, uint16_t sAddress)
The interface function to initialize the sensor.
Definition: mma9553_drv.c:369
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 MMA9553_STREAM_DATA_SIZE
Definition: mma9553_demo.c:39
const uint8_t SetMBoxPriority_for30Hz[5]
Definition: mma9553_drv.c:92
#define SAMPLING_RATE_ms
Definition: mma9553_demo.c:35
#define APPLICATION_VERSION
Version to distinguish between instances the same application based on target Shield and updates...
Definition: mma9553_demo.c:44
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
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.
const registercommandlist_t cMma9553Config30Hz[]
Definition: mma9553_demo.c:50
char boardString[ADS_MAX_STRING_LENGTH]
Definition: mma9553_demo.c:64
gpioHandleKSDK_t GREEN_LED
Definition: frdm_k64f.c:188
#define I2C_S_DEVICE_INDEX
Definition: issdk_hal.h:35
ARM_DRIVER_I2C * pCommDrv
Definition: mma9553_drv.h:47
registerDeviceInfo_t deviceInfo
Definition: mma9553_drv.h:46
#define mma9553_en_callback
Definition: mma9553_demo.c:37
#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]
Definition: mma9553_demo.c:64
#define HOST_S_DRIVER
Definition: frdm_k64f.h:93