ISSDK  1.8
IoT Sensing Software Development Kit
mma9553_pedometer_i2c.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_pedometer_i2c.c
11  * @brief The mma9553_pedometer_i2c.c file implements the ISSDK MMA9553L sensor driver
12  * example demonstration as a Pedometer in I2C Mode.
13  */
14 
15 /* SDK Includes */
16 #include "pin_mux.h"
17 #include "clock_config.h"
18 #include "board.h"
19 #include "fsl_lptmr.h"
20 #include "fsl_debug_console.h"
21 
22 /* CMSIS Includes */
23 #include "Driver_I2C.h"
24 
25 /* ISSDK Includes */
26 #include "issdk_hal.h"
27 #include "mma9553_drv.h"
28 #include "systick_utils.h"
29 
30 /*******************************************************************************
31  * Macros
32  ******************************************************************************/
33 #define SAMPLING_RATE_ms (100) /* Timeout for the ODR Timer. */
34 #define MMA9553_ACCEL_DATA_SIZE (6) /* 2 byte X,Y,Z Axis Data each. */
35 #define mma9553_en_callback LPTMR0_IRQHandler /* Timer timeout Callback. */
36 
37 /*******************************************************************************
38  * Constants
39  ******************************************************************************/
40 /*! Prepare the register write list to configure MMA9553L in 30Hz Mode. */
42  {SetFSRange_2g, 0, sizeof(SetFSRange_2g)}, /* Set FS Range 2G */
43  {SetSampleRate_30Hz, 0, sizeof(SetSampleRate_30Hz)}, /* Set Sensor Sampling Rate 30Hz */
44  {SetAFEPriority_for30Hz, 0, sizeof(SetAFEPriority_for30Hz)}, /* Set AFE Priority for 30Hz Sampling Rate */
45  {SetMBoxPriority_for30Hz, 0, sizeof(SetMBoxPriority_for30Hz)}, /* Set MBox Priority for 30Hz Sampling Rate */
47 
48 /*! Prepare the register read list to read the raw Accel data from MMA9553. */
51 
52 /*! Prepare the command list to read the Pedometer data from MMA9553. */
55 
56 /*! Prepare the read list to read the Pedometer data from MMA9553. */
59 
60 /*******************************************************************************
61  * Globals
62  ******************************************************************************/
63 volatile bool gMma9553DataReady;
64 
65 /*******************************************************************************
66  * Functions
67  ******************************************************************************/
68 /* LPTMR based ODR Callback function. */
70 {
71  LPTMR_ClearStatusFlags(LPTMR0, kLPTMR_TimerCompareFlag);
72  gMma9553DataReady = true;
73 }
74 
75 /*!
76  * @brief Main function
77  */
78 int main(void)
79 {
81  lptmr_config_t lptmrConfig;
83  mma9553_pedometerdata_t pedometerData;
84  mma9553_i2c_sensorhandle_t mma9553Driver;
85 
86  ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER; // Now using the shield.h value!!!
87 
88  /*! Initialize the MCU hardware */
93 
94  /* Initialize ODR Timer. */
95  LPTMR_GetDefaultConfig(&lptmrConfig);
96  LPTMR_Init(LPTMR0, &lptmrConfig);
97  LPTMR_EnableInterrupts(LPTMR0, kLPTMR_TimerInterruptEnable);
98  LPTMR_SetTimerPeriod(LPTMR0, MSEC_TO_COUNT(SAMPLING_RATE_ms, CLOCK_GetFreq(kCLOCK_LpoClk)));
99  EnableIRQ(LPTMR0_IRQn);
100 
101  PRINTF("\r\n ISSDK MMA9553 sensor driver example for Pedometer Mode. \r\n");
102 
103  /*! Initialize the I2C driver. */
104  status = I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
105  if (ARM_DRIVER_OK != status)
106  {
107  PRINTF("\r\n I2C Initialization Failed\r\n");
108  return -1;
109  }
110 
111  /*! Set the I2C Power mode. */
112  status = I2Cdrv->PowerControl(ARM_POWER_FULL);
113  if (ARM_DRIVER_OK != status)
114  {
115  PRINTF("\r\n I2C Power Mode setting Failed\r\n");
116  return -1;
117  }
118 
119  /*! Set the I2C bus speed. */
120  status = I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
121  if (ARM_DRIVER_OK != status)
122  {
123  PRINTF("\r\n I2C Control Mode setting Failed\r\n");
124  return -1;
125  }
126 
127  /*! Initialize the MMA9553 sensor driver. */
129  if (SENSOR_ERROR_NONE != status)
130  {
131  PRINTF("\r\n Sensor Initialization Failed\r\n");
132  return -1;
133  }
134  PRINTF("\r\n Successfully Initialized Sensor. \r\n");
135 
136  /*! Set the task to be executed while waiting for I2C transactions to complete. */
138 
139  gMma9553DataReady = false; /* Do not read data, data will be read after ODR Timer expires. */
140 
141  /*! Configure the MMA9553 sensor driver with 30Hz Mode settings. */
142  status = MMA9553_I2C_Configure(&mma9553Driver, cMma9553Config30Hz);
143  if (SENSOR_ERROR_NONE != status)
144  {
145  PRINTF("\r\n MMA9553 Sensor Configuration Failed, Err = %d \r\n", status);
146  return -1;
147  }
148 
149  LPTMR_StartTimer(LPTMR0);
150  PRINTF("\r\n Successfully Applied MMA9553 Sensor Configuration\r\n");
151 
152  for (;;) /* Forever loop */
153  {
154  if (gMma9553DataReady == false)
155  {
156  continue;
157  }
158  else
159  {
160  gMma9553DataReady = false;
161  }
162 
163  /*! Read the Pedometer data from the MMA9553. */
164  status = MMA9553_I2C_CommandResponse(&mma9553Driver, cMma9553ReadPedometerCommand, cMma9553ReadPedometerOutput,
165  (uint8_t *)&pedometerData);
166  if (ARM_DRIVER_OK != status)
167  {
168  PRINTF("\r\n Read Failed. \r\n");
169  return -1;
170  }
171 
172  /* Swap bytes for 2 byte fields for Big Endian to Little Endian conversion. */
173  pedometerData.statusRegister = (pedometerData.statusRegister >> 8) | (pedometerData.statusRegister << 8);
174  pedometerData.stepCount = (pedometerData.stepCount >> 8) | (pedometerData.stepCount << 8);
175  pedometerData.distance = (pedometerData.distance >> 8) | (pedometerData.distance << 8);
176  pedometerData.speed = (pedometerData.speed >> 8) | (pedometerData.speed << 8);
177  pedometerData.calories = (pedometerData.calories >> 8) | (pedometerData.calories << 8);
178  pedometerData.sleepCount = (pedometerData.sleepCount >> 8) | (pedometerData.sleepCount << 8);
179 
180  PRINTF("\r\n Steps = %d Distance = %dm Speed = %dm/h Calories = %d \r\n", pedometerData.stepCount,
181  pedometerData.distance, pedometerData.speed, pedometerData.calories);
182 
183  /*! Read the raw sensor data from the MMA9553. */
184  status = MMA9553_I2C_CommandResponse(&mma9553Driver, NULL, cMma9553ReadRawOutput, (uint8_t *)&rawData.accel);
185  if (ARM_DRIVER_OK != status)
186  {
187  PRINTF("\r\n Read Failed. \r\n");
188  return -1;
189  }
190 
191  PRINTF("\r\n Accel X = %d Y = %d Z = %d \r\n", rawData.accel[0], rawData.accel[1], rawData.accel[2]);
192  ASK_USER_TO_RESUME(100); /* Ask for user input after processing 100 samples. */
193  }
194 }
This defines the sensor specific information for I2C.
Definition: mma9553_drv.h:44
#define ASK_USER_TO_RESUME(x)
Definition: frdm_k64f.h:106
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
const uint8_t ReadPedometerData[4]
Command to Read Pedometer Data.
Definition: mma9553_drv.c:98
This structure defines the Size of response for pedometer data read command.
Definition: mma9553_drv.h:60
This structure defines the Block command List.
Definition: sensor_drv.h:87
#define SMC
Definition: lpc54114.h:118
int16_t accel[3]
Definition: mma9553_drv.h:56
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.
The mma9553_drv.h file describes the MMA9553L driver interface and structures.
This structure defines the mma9553 pedometer data buffer.
Definition: mma9553_drv.h:53
#define I2C_S_SIGNAL_EVENT
Definition: issdk_hal.h:34
#define I2C_S_DRIVER
Definition: issdk_hal.h:33
const uint8_t SetFSRange_2g[5]
Full-Scale Range Selections.
Definition: mma9553_drv.c:63
#define MMA9553_XYZ_DATA_OFFSET
XYZ Data Register Offset.
Definition: mma9553.h:23
#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
#define SAMPLING_RATE_ms
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
#define mma9553_en_callback
const registerreadlist_t cMma9553ReadRawOutput[]
#define __END_READ_DATA__
Definition: sensor_drv.h:51
#define __END_WRITE_CMD__
Definition: sensor_drv.h:57
ARM_DRIVER_I2C * I2Cdrv
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
const uint8_t SetMBoxPriority_for30Hz[5]
Definition: mma9553_drv.c:92
void BOARD_SystickEnable(void)
Function to enable systicks framework.
Definition: systick_utils.c:35
This structure defines the Read command List.
Definition: sensor_drv.h:78
ARM Systick Utilities.
#define I2C_S_DEVICE_INDEX
Definition: issdk_hal.h:35
int main(void)
Main function.
void BOARD_InitDebugConsole(void)
Definition: board.c:15
#define MMA9553_ACCEL_DATA_SIZE
void BOARD_InitPins(void)
Configures pin routing and optionally pin electrical features.
Definition: pin_mux.c:47
volatile bool gMma9553DataReady
const registercommandlist_t cMma9553Config30Hz[]
const registerreadlist_t cMma9553ReadPedometerOutput[]
const registercommandlist_t cMma9553ReadPedometerCommand[]