ISSDK  1.7
IoT Sensing Software Development Kit
mma9553_pedometer_interrupt.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_pedometer_interrupt.c
37 * @brief The mma9553_pedometer_interrupt.c file implements the ISSDK MMA9553L sensor driver
38 * example demonstration as a Pedometer in I2C Mode with Interrupts.
39 */
40 
41 /* SDK Includes */
42 #include "board.h"
43 #include "pin_mux.h"
44 #include "clock_config.h"
45 #include "fsl_debug_console.h"
46 
47 /* CMSIS Includes */
48 #include "Driver_I2C.h"
49 
50 /* ISSDK Includes */
51 #include "issdk_hal.h"
52 #include "mma9553_drv.h"
53 #include "gpio_driver.h"
54 #include "systick_utils.h"
55 
56 /*******************************************************************************
57  * Macros
58  ******************************************************************************/
59 #define MMA9553_ACCEL_DATA_SIZE (6) /* 2 byte X,Y,Z Axis Data each. */
60 #define MMA9553_INT_O A0 /* The INT_O pin of MMA9553 on the FRDM-STBC-SA955x Board. */
61 
62 /*******************************************************************************
63  * Constants
64  ******************************************************************************/
65 /*! Prepare the register write list to configure MMA9553L in 30Hz Mode. */
67  {SetFSRange_2g, 0, sizeof(SetFSRange_2g)}, /* Set FS Range 2G */
68  {SetSampleRate_3Hz, 0, sizeof(SetSampleRate_3Hz)}, /* Set Sensor Sampling Rate 3Hz */
69  {SetAFEPriority_for3Hz, 0, sizeof(SetAFEPriority_for3Hz)}, /* Set AFE Priority for 3Hz Sampling Rate */
70  {SetMBoxPriority_for3Hz, 0, sizeof(SetMBoxPriority_for3Hz)}, /* Set MBox Priority for 3Hz Sampling Rate */
72 
73 /*! Prepare the register read list to read the raw Accel data from MMA9553. */
76 
77 /*! Prepare the command list to read the Pedometer data from MMA9553. */
80 
81 /*! Prepare the command list to enable interrupts for AFE sampling completion with Legacy Mode for MMA9553. */
84 
85 /*! Prepare the read list to read the Pedometer data from MMA9553. */
88 
89 /*******************************************************************************
90  * Globals
91  ******************************************************************************/
92 volatile bool gMma9553DataReady;
93 
94 /*******************************************************************************
95  * Functions
96  ******************************************************************************/
97 /* Data Ready INT Callback function. */
98 void mma9553_int_data_ready_callback(void *pUserData)
99 {
100  gMma9553DataReady = true;
101 }
102 
103 /*!
104  * @brief Main function
105  */
106 int main(void)
107 {
108  int32_t status;
110  mma9553_pedometerdata_t pedometerData;
111  mma9553_i2c_sensorhandle_t mma9553Driver;
112 
114  ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER; // Now using the shield.h value!!!
115 
116  /*! Initialize the MCU hardware */
117  BOARD_InitPins();
121 
122  PRINTF("\r\n ISSDK MMA9553 sensor driver example for Interrupt Mode. \r\n");
123 
124  /* Here MMA9553_INT_O and RGB_LED are the GPIO PIN IDs.
125  * These are generated using the same Port ID(PortB for the pins) and Pin ID(2 and 22 for the pins) used for
126  * creating the GPIO handles above. */
127  pGpioDriver->pin_init(&GREEN_LED, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
129 
130  /*! Initialize the I2C driver. */
131  status = I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
132  if (ARM_DRIVER_OK != status)
133  {
134  PRINTF("\r\n I2C Initialization Failed\r\n");
135  return -1;
136  }
137 
138  /*! Set the I2C Power mode. */
139  status = I2Cdrv->PowerControl(ARM_POWER_FULL);
140  if (ARM_DRIVER_OK != status)
141  {
142  PRINTF("\r\n I2C Power Mode setting Failed\r\n");
143  return -1;
144  }
145 
146  /*! Set the I2C bus speed. */
147  status = I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
148  if (ARM_DRIVER_OK != status)
149  {
150  PRINTF("\r\n I2C Control Mode setting Failed\r\n");
151  return -1;
152  }
153 
154  /*! Initialize the MMA9553 sensor driver. */
156  if (SENSOR_ERROR_NONE != status)
157  {
158  PRINTF("\r\n Sensor Initialization Failed\r\n");
159  return -1;
160  }
161  PRINTF("\r\n Successfully Initialized Sensor. \r\n");
162 
163  /*! Set the task to be executed while waiting for I2C transactions to complete. */
165 
166  gMma9553DataReady = false; /* Do not read data, data will be read after Interrupt is received. */
167 
168  /*! Configure the MMA9553 sensor driver with 30Hz Mode settings. */
169  status = MMA9553_I2C_Configure(&mma9553Driver, cMma9553Config30Hz);
170  if (SENSOR_ERROR_NONE != status)
171  {
172  PRINTF("\r\n MMA9553 Sensor Configuration Failed, Err = %d \r\n", status);
173  return -1;
174  }
175 
176  /*! MMA9553_I2C_Configure sets the part to Legacy Mode.
177  * To enable MMA9553_INT_O interrupt for MMA9553 we need to override this by setting Legacy+AFE Sampling Interrupt.
178  */
179  status = MMA9553_I2C_CommandResponse(&mma9553Driver, cMma9553EnableInterruptCommand, NULL, NULL);
180  if (ARM_DRIVER_OK != status)
181  {
182  PRINTF("\r\n Enable Interrupt Failed. \r\n");
183  return -1; /* Read failed, so exit. */
184  }
185 
186  PRINTF("\r\n Successfully Applied MMA9553 Sensor Configuration\r\n");
187 
188  for (;;) /* Forever loop */
189  { /* In ISR Mode receipt of interrupt will indicate data is ready. */
190  if (false == gMma9553DataReady)
191  { /* Loop, if new sample is not available. */
193  continue;
194  }
195  else
196  { /*! Clear the data ready flag, it will be set again by the ISR. */
197  gMma9553DataReady = false;
198  pGpioDriver->toggle_pin(&GREEN_LED);
199  }
200 
201  /*! Read the Pedometer data from the MMA9553. */
202  status = MMA9553_I2C_CommandResponse(&mma9553Driver, cMma9553ReadPedometerCommand, cMma9553ReadPedometerOutput,
203  (uint8_t *)&pedometerData);
204  if (ARM_DRIVER_OK != status)
205  {
206  PRINTF("\r\n Read Failed. \r\n");
207  return -1;
208  }
209 
210  /* Swap bytes for 2 byte fields for Little Endian to Big Endian conversion. */
211  pedometerData.statusRegister = (pedometerData.statusRegister >> 8) | (pedometerData.statusRegister << 8);
212  pedometerData.stepCount = (pedometerData.stepCount >> 8) | (pedometerData.stepCount << 8);
213  pedometerData.distance = (pedometerData.distance >> 8) | (pedometerData.distance << 8);
214  pedometerData.speed = (pedometerData.speed >> 8) | (pedometerData.speed << 8);
215  pedometerData.calories = (pedometerData.calories >> 8) | (pedometerData.calories << 8);
216  pedometerData.sleepCount = (pedometerData.sleepCount >> 8) | (pedometerData.sleepCount << 8);
217 
218  PRINTF("\r\n Steps = %d Distance = %dm Speed = %dm/h Calories = %d \r\n", pedometerData.stepCount,
219  pedometerData.distance, pedometerData.speed, pedometerData.calories);
220 
221  /*! Read the raw sensor data from the MMA9553. */
222  status = MMA9553_I2C_CommandResponse(&mma9553Driver, NULL, cMma9553ReadRawOutput, (uint8_t *)&rawData.accel);
223  if (ARM_DRIVER_OK != status)
224  {
225  PRINTF("\r\n Read Failed. \r\n");
226  return -1;
227  }
228 
229  PRINTF("\r\n Accel X = %d Y = %d Z = %d \r\n", rawData.accel[0], rawData.accel[1], rawData.accel[2]);
230  ASK_USER_TO_RESUME(30); /* Ask for user input after processing 100 samples. */
231  }
232 }
const uint8_t SetMBoxPriority_for3Hz[5]
Definition: mma9553_drv.c:121
const registercommandlist_t cMma9553Config30Hz[]
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
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
This structure defines the Size of response for pedometer data read command.
Definition: mma9553_drv.h:86
const uint8_t ReadPedometerData[4]
Command to Read Pedometer Data.
Definition: mma9553_drv.c:124
#define MMA9553_XYZ_DATA_OFFSET
XYZ Data Register Offset.
Definition: mma9553.h:22
int32_t status
int main(void)
Main function.
void(* toggle_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:74
This defines the sensor specific information for I2C.
Definition: mma9553_drv.h:70
void BOARD_InitDebugConsole(void)
Definition: board.c:41
This structure defines the mma9553 pedometer data buffer.
Definition: mma9553_drv.h:79
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 __END_READ_DATA__
Definition: sensor_drv.h:77
GENERIC_DRIVER_GPIO * pGpioDriver
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
gpioHandleKSDK_t GREEN_LED
Definition: frdm_k64f.c:214
#define ASK_USER_TO_RESUME(x)
Definition: frdm_k64f.h:132
#define MMA9553_INT_O
#define __END_WRITE_CMD__
Definition: sensor_drv.h:83
#define MMA9553_ACCEL_DATA_SIZE
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
const uint8_t SetLegacyIntMode[5]
Set Sensor to Legacy Mode with INT_O interrupt on completion of the AFE sampling. ...
Definition: mma9553_drv.c:80
const uint8_t SetAFEPriority_for3Hz[5]
Definition: mma9553_drv.c:111
#define I2C_S_DEVICE_INDEX
Definition: issdk_hal.h:61
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.
The mma9553_drv.h file describes the MMA9553L driver interface and structures.
void mma9553_int_data_ready_callback(void *pUserData)
Access structure of the GPIO Driver.
Definition: Driver_GPIO.h:64
ARM Systick Utilities.
fxls8962_acceldataUser_t rawData
const registercommandlist_t cMma9553EnableInterruptCommand[]
This structure defines the Read command List.
Definition: sensor_drv.h:104
#define SMC
Definition: lpc54114.h:144
const registerreadlist_t cMma9553ReadRawOutput[]
const registerreadlist_t cMma9553ReadPedometerOutput[]
const uint8_t SetSampleRate_3Hz[5]
Definition: mma9553_drv.c:101
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
const registercommandlist_t cMma9553ReadPedometerCommand[]
void BOARD_InitPins(void)
Configures pin routing and optionally pin electrical features.
Definition: pin_mux.c:73
volatile bool gMma9553DataReady
#define I2C_S_SIGNAL_EVENT
Definition: issdk_hal.h:60