ISSDK  1.8
IoT Sensing Software Development Kit
mma9553_pedometer_interrupt.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_interrupt.c
11 * @brief The mma9553_pedometer_interrupt.c file implements the ISSDK MMA9553L sensor driver
12 * example demonstration as a Pedometer in I2C Mode with Interrupts.
13 */
14 
15 /* SDK Includes */
16 #include "pin_mux.h"
17 #include "clock_config.h"
18 #include "board.h"
19 #include "fsl_debug_console.h"
20 
21 /* CMSIS Includes */
22 #include "Driver_I2C.h"
23 
24 /* ISSDK Includes */
25 #include "issdk_hal.h"
26 #include "mma9553_drv.h"
27 #include "gpio_driver.h"
28 #include "systick_utils.h"
29 
30 /*******************************************************************************
31  * Macros
32  ******************************************************************************/
33 #define MMA9553_ACCEL_DATA_SIZE (6) /* 2 byte X,Y,Z Axis Data each. */
34 #define MMA9553_INT_O A0 /* The INT_O pin of MMA9553 on the FRDM-STBC-SA955x Board. */
35 
36 /*******************************************************************************
37  * Constants
38  ******************************************************************************/
39 /*! Prepare the register write list to configure MMA9553L in 30Hz Mode. */
41  {SetFSRange_2g, 0, sizeof(SetFSRange_2g)}, /* Set FS Range 2G */
42  {SetSampleRate_3Hz, 0, sizeof(SetSampleRate_3Hz)}, /* Set Sensor Sampling Rate 3Hz */
43  {SetAFEPriority_for3Hz, 0, sizeof(SetAFEPriority_for3Hz)}, /* Set AFE Priority for 3Hz Sampling Rate */
44  {SetMBoxPriority_for3Hz, 0, sizeof(SetMBoxPriority_for3Hz)}, /* Set MBox Priority for 3Hz Sampling Rate */
46 
47 /*! Prepare the register read list to read the raw Accel data from MMA9553. */
50 
51 /*! Prepare the command list to read the Pedometer data from MMA9553. */
54 
55 /*! Prepare the command list to enable interrupts for AFE sampling completion with Legacy Mode for MMA9553. */
58 
59 /*! Prepare the read list to read the Pedometer data from MMA9553. */
62 
63 /*******************************************************************************
64  * Globals
65  ******************************************************************************/
66 volatile bool gMma9553DataReady;
67 
68 /*******************************************************************************
69  * Functions
70  ******************************************************************************/
71 /* Data Ready INT Callback function. */
72 void mma9553_int_data_ready_callback(void *pUserData)
73 {
74  gMma9553DataReady = true;
75 }
76 
77 /*!
78  * @brief Main function
79  */
80 int main(void)
81 {
84  mma9553_pedometerdata_t pedometerData;
85  mma9553_i2c_sensorhandle_t mma9553Driver;
86 
88  ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER; // Now using the shield.h value!!!
89 
90  /*! Initialize the MCU hardware */
95 
96  PRINTF("\r\n ISSDK MMA9553 sensor driver example for Interrupt Mode. \r\n");
97 
98  /* Here MMA9553_INT_O and RGB_LED are the GPIO PIN IDs.
99  * These are generated using the same Port ID(PortB for the pins) and Pin ID(2 and 22 for the pins) used for
100  * creating the GPIO handles above. */
101  pGpioDriver->pin_init(&GREEN_LED, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
103 
104  /*! Initialize the I2C driver. */
105  status = I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
106  if (ARM_DRIVER_OK != status)
107  {
108  PRINTF("\r\n I2C Initialization Failed\r\n");
109  return -1;
110  }
111 
112  /*! Set the I2C Power mode. */
113  status = I2Cdrv->PowerControl(ARM_POWER_FULL);
114  if (ARM_DRIVER_OK != status)
115  {
116  PRINTF("\r\n I2C Power Mode setting Failed\r\n");
117  return -1;
118  }
119 
120  /*! Set the I2C bus speed. */
121  status = I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
122  if (ARM_DRIVER_OK != status)
123  {
124  PRINTF("\r\n I2C Control Mode setting Failed\r\n");
125  return -1;
126  }
127 
128  /*! Initialize the MMA9553 sensor driver. */
130  if (SENSOR_ERROR_NONE != status)
131  {
132  PRINTF("\r\n Sensor Initialization Failed\r\n");
133  return -1;
134  }
135  PRINTF("\r\n Successfully Initialized Sensor. \r\n");
136 
137  /*! Set the task to be executed while waiting for I2C transactions to complete. */
139 
140  gMma9553DataReady = false; /* Do not read data, data will be read after Interrupt is received. */
141 
142  /*! Configure the MMA9553 sensor driver with 30Hz Mode settings. */
143  status = MMA9553_I2C_Configure(&mma9553Driver, cMma9553Config30Hz);
144  if (SENSOR_ERROR_NONE != status)
145  {
146  PRINTF("\r\n MMA9553 Sensor Configuration Failed, Err = %d \r\n", status);
147  return -1;
148  }
149 
150  /*! MMA9553_I2C_Configure sets the part to Legacy Mode.
151  * To enable MMA9553_INT_O interrupt for MMA9553 we need to override this by setting Legacy+AFE Sampling Interrupt.
152  */
153  status = MMA9553_I2C_CommandResponse(&mma9553Driver, cMma9553EnableInterruptCommand, NULL, NULL);
154  if (ARM_DRIVER_OK != status)
155  {
156  PRINTF("\r\n Enable Interrupt Failed. \r\n");
157  return -1; /* Read failed, so exit. */
158  }
159 
160  PRINTF("\r\n Successfully Applied MMA9553 Sensor Configuration\r\n");
161 
162  for (;;) /* Forever loop */
163  { /* In ISR Mode receipt of interrupt will indicate data is ready. */
164  if (false == gMma9553DataReady)
165  { /* Loop, if new sample is not available. */
167  continue;
168  }
169  else
170  { /*! Clear the data ready flag, it will be set again by the ISR. */
171  gMma9553DataReady = false;
172  pGpioDriver->toggle_pin(&GREEN_LED);
173  }
174 
175  /*! Read the Pedometer data from the MMA9553. */
176  status = MMA9553_I2C_CommandResponse(&mma9553Driver, cMma9553ReadPedometerCommand, cMma9553ReadPedometerOutput,
177  (uint8_t *)&pedometerData);
178  if (ARM_DRIVER_OK != status)
179  {
180  PRINTF("\r\n Read Failed. \r\n");
181  return -1;
182  }
183 
184  /* Swap bytes for 2 byte fields for Little Endian to Big Endian conversion. */
185  pedometerData.statusRegister = (pedometerData.statusRegister >> 8) | (pedometerData.statusRegister << 8);
186  pedometerData.stepCount = (pedometerData.stepCount >> 8) | (pedometerData.stepCount << 8);
187  pedometerData.distance = (pedometerData.distance >> 8) | (pedometerData.distance << 8);
188  pedometerData.speed = (pedometerData.speed >> 8) | (pedometerData.speed << 8);
189  pedometerData.calories = (pedometerData.calories >> 8) | (pedometerData.calories << 8);
190  pedometerData.sleepCount = (pedometerData.sleepCount >> 8) | (pedometerData.sleepCount << 8);
191 
192  PRINTF("\r\n Steps = %d Distance = %dm Speed = %dm/h Calories = %d \r\n", pedometerData.stepCount,
193  pedometerData.distance, pedometerData.speed, pedometerData.calories);
194 
195  /*! Read the raw sensor data from the MMA9553. */
196  status = MMA9553_I2C_CommandResponse(&mma9553Driver, NULL, cMma9553ReadRawOutput, (uint8_t *)&rawData.accel);
197  if (ARM_DRIVER_OK != status)
198  {
199  PRINTF("\r\n Read Failed. \r\n");
200  return -1;
201  }
202 
203  PRINTF("\r\n Accel X = %d Y = %d Z = %d \r\n", rawData.accel[0], rawData.accel[1], rawData.accel[2]);
204  ASK_USER_TO_RESUME(30); /* Ask for user input after processing 100 samples. */
205  }
206 }
const uint8_t SetAFEPriority_for3Hz[5]
Definition: mma9553_drv.c:85
#define MMA9553_ACCEL_DATA_SIZE
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
const registercommandlist_t cMma9553ReadPedometerCommand[]
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
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 SetSampleRate_3Hz[5]
Definition: mma9553_drv.c:75
const registerreadlist_t cMma9553ReadPedometerOutput[]
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
const registercommandlist_t cMma9553Config30Hz[]
This structure defines the Block command List.
Definition: sensor_drv.h:87
Access structure of the GPIO Driver.
Definition: Driver_GPIO.h:38
#define SMC
Definition: lpc54114.h:118
#define MMA9553_INT_O
int16_t accel[3]
Definition: mma9553_drv.h:56
void mma9553_int_data_ready_callback(void *pUserData)
const registerreadlist_t cMma9553ReadRawOutput[]
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
void(* registeridlefunction_t)(void *userParam)
This is the register idle function type.
Definition: sensor_drv.h:97
const registercommandlist_t cMma9553EnableInterruptCommand[]
volatile bool gMma9553DataReady
GENERIC_DRIVER_GPIO Driver_GPIO_KSDK
Definition: gpio_driver.c:177
const uint8_t SetMBoxPriority_for3Hz[5]
Definition: mma9553_drv.c:95
GENERIC_DRIVER_GPIO * pGpioDriver
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 __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
void(* toggle_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:48
int main(void)
Main function.
void BOARD_SystickEnable(void)
Function to enable systicks framework.
Definition: systick_utils.c:35
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
#define I2C_S_DEVICE_INDEX
Definition: issdk_hal.h:35
const uint8_t SetLegacyIntMode[5]
Set Sensor to Legacy Mode with INT_O interrupt on completion of the AFE sampling. ...
Definition: mma9553_drv.c:54
void BOARD_InitDebugConsole(void)
Definition: board.c:15
void BOARD_InitPins(void)
Configures pin routing and optionally pin electrical features.
Definition: pin_mux.c:47