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