ISSDK  1.8
IoT Sensing Software Development Kit
pedometer_stepcount_motion_mma8652.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 pedometer_stepcount_mma8652.c
11  * @brief The pedometer_stepcount_mma8652.c file implements the ISSDK MMA8652 sensor
12  * driver example demonstration for Motion Activated Pedometer.
13  */
14 
15 //-----------------------------------------------------------------------
16 // SDK Includes
17 //-----------------------------------------------------------------------
18 #include "board.h"
19 #include "pin_mux.h"
20 #include "clock_config.h"
21 #include "fsl_debug_console.h"
22 
23 //-----------------------------------------------------------------------
24 // CMSIS Includes
25 //-----------------------------------------------------------------------
26 #include "Driver_I2C.h"
27 
28 //-----------------------------------------------------------------------
29 // ISSDK Includes
30 //-----------------------------------------------------------------------
31 #include "issdk_hal.h"
32 #include "pedometer.h"
33 #include "gpio_driver.h"
34 #include "mma865x_drv.h"
35 
36 //-----------------------------------------------------------------------
37 // Macros
38 //-----------------------------------------------------------------------
39 #define MT_FF_MT_THS 0x15 /* Threshold Value. */
40 #define ASLP_COUNTER 0x10 /* Auto Sleep after ~5s. */
41 #define MMA865x_DATA_SIZE 6 /* 2-byte Accel x,y,z each */
42 
43 //-----------------------------------------------------------------------
44 // Constants
45 //-----------------------------------------------------------------------
46 /*! @brief MMA865x Motion based Pedometer Register Write List. */
62 
63 /*! @brief MMA865x Motion Detect Mode Register Write List. */
65  /* Only FFMT INT Enabled */
68  /* Route FFMT to INT1 */
71 
72 /*! @brief MMA865x DRDY and ASLP Detect Mode Register Write List. */
74  /* Data and ASLP INT Enabled */
77  /* Route FFMT to INT2 (Ignore FFMT INT) */
80 
81 /*! @brief Address of INT Source Register. */
83 
84 /*! @brief Address of Data Output Registers. */
86 
87 /*! @brief Pedometer Mode Name Strings. */
88 const char *pActivity[5] = {"Unknown ", "Rest ", "Walking ", "Jogging ", "Running "};
89 
90 //-----------------------------------------------------------------------
91 // Global Variables
92 //-----------------------------------------------------------------------
93 volatile bool gMma865xEventReady = false;
94 
95 /* Pedometer configuration. These configuration are algorithm and user dependent data. */
97 {
99  .bits = {.config = 1},
100  .keynetik =
101  {
102  .steplength = 0,
103  .height = 175,
104  .weight = 80,
105  .filtersteps = 1,
106  .bits =
107  {
108  .filtertime = 2, .male = 1,
109  },
110  .speedperiod = 3,
111  .stepthreshold = 0,
112  },
113  .stepcoalesce = 1,
114  .oneG = PEDO_ONEG_2G,
115  .frequency = 50,
116 };
117 
118 //-----------------------------------------------------------------------
119 // Functions
120 //-----------------------------------------------------------------------
121 /*! -----------------------------------------------------------------------
122  * @brief This is the Sensor Event Ready ISR implementation.
123  * @details This function sets the flag which indicates if a new sample(s) is available for reading.
124  * @param[in] pUserData This is a void pointer to the instance of the user specific data structure for the ISR.
125  * @return void There is no return value.
126  * @constraints None
127  * @reeentrant Yes
128  * -----------------------------------------------------------------------*/
129 void mma865x_int_callback(void *pUserData)
130 { /*! @brief Set flag to indicate Sensor has signaled event ready. */
131  gMma865xEventReady = true;
132 }
133 
134 /*! -----------------------------------------------------------------------
135  * @brief This is the The main function implementation.
136  * @details This function invokes board initializes routines, then then brings up the sensor and
137  * finally enters an endless loop to continuously read available samples.
138  * @param[in] void This is no input parameter.
139  * @return void There is no return value.
140  * @constraints None
141  * @reeentrant No
142  * -----------------------------------------------------------------------*/
143 int main(void)
144 {
145  int32_t status;
146  bool motionDetect;
147  uint16_t lastReportedSteps;
148  uint8_t intStatus, data[MMA865x_DATA_SIZE];
149 
150  pedometer_t pedometer; /* This handle holds the current configuration and status value for the pedometer.*/
152  mma865x_i2c_sensorhandle_t mma865xDriver;
153 
154  ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER; // Now using the shield.h value!!!
156 
157  /*! Initialize the MCU hardware. */
158  BOARD_InitPins();
161 
162  PRINTF("\r\n ISSDK MMA865x sensor driver example for Motion Activated Pedometer.\r\n");
163 
164  /*! Initialize MMA865x pin used by FRDM board */
165  pGpioDriver->pin_init(&MMA8652_INT1, GPIO_DIRECTION_IN, NULL, &mma865x_int_callback, NULL);
166 
167  /*! Initialize RGB LED pin used by FRDM board */
168  pGpioDriver->pin_init(&GREEN_LED, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
169 
170  /*! Initialize the I2C driver. */
171  status = I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
172  if (ARM_DRIVER_OK != status)
173  {
174  PRINTF("\r\n I2C Initialization Failed\r\n");
175  return -1;
176  }
177 
178  /*! Set the I2C Power mode. */
179  status = I2Cdrv->PowerControl(ARM_POWER_FULL);
180  if (ARM_DRIVER_OK != status)
181  {
182  PRINTF("\r\n I2C Power Mode setting Failed\r\n");
183  return -1;
184  }
185 
186  /*! Set the I2C bus speed. */
187  status = I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
188  if (ARM_DRIVER_OK != status)
189  {
190  PRINTF("\r\n I2C Control Mode setting Failed\r\n");
191  return -1;
192  }
193 
194  /*! Initialize MMA865x sensor driver. */
197  if (SENSOR_ERROR_NONE != status)
198  {
199  PRINTF("\r\n Sensor Initialization Failed\r\n");
200  return -1;
201  }
202 
203  /*! Set the task to be executed while waiting for I2C transactions to complete. */
205 
206  /* Apply MMA865x Configuration for Motion Activated Pedometer. */
207  status = Sensor_I2C_Write(mma865xDriver.pCommDrv, &mma865xDriver.deviceInfo,
208  mma865xDriver.slaveAddress, cMma865xCommonConfiguration);
209  if (SENSOR_ERROR_NONE != status)
210  {
211  PRINTF("\r\n Write MMA865x Initialization Failed.\r\n");
212  return -1;
213  }
214 
215  /*! Initialize the pedometer*/
216  pedometer_init(&pedometer);
217  PRINTF("\r\n Pedometer successfully Initialized and Ready for measurements.");
218 
219  for (;;) /* Forever loop for Motion Detection */
220  {
221  /*! Configure the pedometer*/
222  pedometer_configure(&pedometer, &cPedoConfig);
223 
224  /* Apply MMA865x Configuration for Motion Detection. */
225  status = MMA865x_I2C_Configure(&mma865xDriver, cMma865xConfigMotionDetect);
226  if (SENSOR_ERROR_NONE != status)
227  {
228  PRINTF("\r\n Write MMA865x Motion Configuration Failed!\r\n");
229  return -1;
230  }
231 
232  motionDetect = true;
233  pGpioDriver->set_pin(&GREEN_LED);
234  PRINTF("\r\n\r\n Waiting for Motion | MCU switching to Sleep Mode ...\r\n");
235 
236  for (;;) /* Loop for Motion Detection */
237  { /* In ISR Mode we do not need to check Data Ready Register.
238  * The receipt of interrupt will indicate event is ready. */
239  if (false == gMma865xEventReady)
240  { /* Loop, if new sample is not available. */
242  continue;
243  }
244  else
245  { /*! Clear the data ready flag, it will be set again by the ISR. */
246  gMma865xEventReady = false;
247  }
248 
249  /*! Read the INT_SRC from the MMA865x. */
250  status = MMA865x_I2C_ReadData(&mma865xDriver, cMma865xINTSrc, &intStatus);
251  if (ARM_DRIVER_OK != status)
252  {
253  PRINTF("\r\n Read INT SRC Failed!\r\n");
254  return -1;
255  }
256 
257  if (motionDetect)
258  {
260  {
261  /*! Display that a Motion event has been detected. */
262  PRINTF(" Motion detected...\r\n");
263 
264  /* Apply MMA865x Configuration for ASLP Detection with DATA. */
265  status = MMA865x_I2C_Configure(&mma865xDriver, cMma865xConfigDataReady);
266  if (SENSOR_ERROR_NONE != status)
267  {
268  PRINTF("\r\n Write MMA865x DRDY Configuration Failed.\r\n");
269  return -1;
270  }
271  motionDetect = false;
272  pGpioDriver->clr_pin(&GREEN_LED);
273  }
274  }
275  else
276  {
277  /*! Read the Output from the MMA865x. */
278  status = MMA865x_I2C_ReadData(&mma865xDriver, cMma865xOutput, data);
279  if (ARM_DRIVER_OK != status)
280  {
281  PRINTF("\r\n ERROR : Read Data Failed!\r\n");
282  return -1;
283  }
284 
285  /*! Convert the raw sensor data for feeding to pedometer algorithm. */
286  rawData.accel[0] = ((int16_t)data[0] << 8) | data[1];
287  rawData.accel[1] = ((int16_t)data[2] << 8) | data[3];
288  rawData.accel[2] = ((int16_t)data[4] << 8) | data[5];
289 
290  /*! Execute the pedometer Algorithm */
291  pedometer_run(&pedometer, (ped_accel_t *)&rawData.accel);
292 
293  /* Display Steps when there is a change */
294  if (pedometer.status.stepcount && pedometer.status.stepcount != lastReportedSteps)
295  {
296  pGpioDriver->toggle_pin(&GREEN_LED);
297  lastReportedSteps = pedometer.status.stepcount;
298  if(pedometer.status.stepcount == 1)
299  {
300  PRINTF("\r\n | Steps | Distance | Speed | Calories | Activity |\r\n");
301  }
302  PRINTF("\033[K | %5d | %4dm | %2dkmph | %3d | %s |\r",
303  pedometer.status.stepcount, pedometer.status.distance, pedometer.status.speed/1000, pedometer.status.calories, pActivity[pedometer.status.status.bits.activity]);
304  }
305 
307  {
308  break;
309  }
310  }
311  }
312  }
313 }
void(* set_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:46
struct pedometer_t::pedometer_status_tag status
#define MMA865x_CTRL_REG1_DR_MASK
Definition: mma865x.h:1534
ARM_DRIVER_I2C * pCommDrv
Definition: mma865x_drv.h:32
#define MMA865x_FF_MT_CFG_YEFE_MASK
Definition: mma865x.h:854
void pedometer_configure(pedometer_t *pPedometer, const pedometer_config_t *pConfig)
The interface function to configure the pedometer.
Definition: pedometer.c:111
#define MMA865x_CTRL_REG5_INT_CFG_DRDY_MASK
Definition: mma865x.h:1815
This structure defines the Write command List.
Definition: sensor_drv.h:68
This defines the sensor specific information.
Definition: mma865x_drv.h:29
#define MMA865x_CTRL_REG5_INT_CFG_FF_MT_INT1
Definition: mma865x.h:1851
int32_t status
The pedometer.h file contains the interface and structure definitions for pedometer application...
#define MMA865x_CTRL_REG4_INT_EN_DRDY_DISABLED
Definition: mma865x.h:1777
#define MMA865x_CTRL_REG3_WAKE_FF_MT_MASK
Definition: mma865x.h:1667
#define MMA865x_CTRL_REG1_ASLP_RATE_1_56HZ
Definition: mma865x.h:1547
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 registerwritelist_t cMma865xCommonConfiguration[]
MMA865x Motion based Pedometer Register Write List.
#define MMA865x_CTRL_REG1_DR_50HZ
Definition: mma865x.h:1552
#define MMA865x_CTRL_REG5_INT_CFG_DRDY_INT1
Definition: mma865x.h:1853
#define MMA865x_FF_MT_CFG_XEFE_EN
Definition: mma865x.h:881
#define MMA865x_INT_SOURCE_SRC_ASLP_MASK
Definition: mma865x.h:464
#define MMA865x_CTRL_REG5_INT_CFG_ASLP_MASK
Definition: mma865x.h:1833
#define MMA8652_INT1
Access structure of the GPIO Driver.
Definition: Driver_GPIO.h:38
#define SMC
Definition: lpc54114.h:118
#define MMA865x_INT_SOURCE_SRC_ASLP_READY
Definition: mma865x.h:471
This defines the configuration structure of the pedometer.
Definition: pedometer.h:46
#define MMA865x_FF_MT_THS_THS_MASK
Definition: mma865x.h:983
#define __END_WRITE_DATA__
Definition: sensor_drv.h:45
debounce_count_t sleepcount_threshold
Definition: pedometer.h:50
#define MMA865x_CTRL_REG3_IPOL_MASK
Definition: mma865x.h:1664
void(* clr_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:47
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
struct pedometer_t::pedometer_status_tag::@351::@352 bits
int32_t MMA865x_I2C_ReadData(mma865x_i2c_sensorhandle_t *pSensorHandle, const registerreadlist_t *pReadList, uint8_t *pBuffer)
The interface function to read the sensor data.
Definition: mma865x_drv.c:106
#define MMA865x_CTRL_REG3_IPOL_ACTIVE_HIGH
Definition: mma865x.h:1701
#define I2C_S_SIGNAL_EVENT
Definition: issdk_hal.h:34
#define MMA865x_FF_MT_CFG_ZEFE_EN
Definition: mma865x.h:875
#define I2C_S_DRIVER
Definition: issdk_hal.h:33
#define PEDO_ONEG_2G
Definition: pedometer.h:30
#define MMA865x_CTRL_REG4_INT_EN_FF_MT_EN
Definition: mma865x.h:1776
#define MMA865x_INT_SOURCE_SRC_FF_MT_READY
Definition: mma865x.h:488
This defines the pedometer instance.
Definition: pedometer.h:73
#define MMA865x_CTRL_REG4_INT_EN_FF_MT_MASK
Definition: mma865x.h:1743
int32_t Sensor_I2C_Write(ARM_DRIVER_I2C *pCommDrv, registerDeviceInfo_t *devInfo, uint16_t slaveAddress, const registerwritelist_t *pRegWriteList)
Write register data to a sensor.
Definition: sensor_io_i2c.c:71
This defines the acceleration input data for the pedometer.
Definition: pedometer.h:39
#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
int16_t accel[3]
Definition: mma865x_drv.h:41
GENERIC_DRIVER_GPIO Driver_GPIO_KSDK
Definition: gpio_driver.c:177
GENERIC_DRIVER_GPIO * pGpioDriver
const registerwritelist_t cMma865xConfigMotionDetect[]
MMA865x Motion Detect Mode Register Write List.
uint8_t data[FXLS8962_DATA_SIZE]
int32_t MMA865x_I2C_Configure(mma865x_i2c_sensorhandle_t *pSensorHandle, const registerwritelist_t *pRegWriteList)
The interface function to configure he sensor.
Definition: mma865x_drv.c:61
#define MMA865x_CTRL_REG4_INT_EN_ASLP_MASK
Definition: mma865x.h:1758
#define __END_READ_DATA__
Definition: sensor_drv.h:51
The mma865x_drv.h file describes the MMA865x driver interface and structures.
const registerreadlist_t cMma865xINTSrc[]
Address of INT Source Register.
#define MMA865x_CTRL_REG4_INT_EN_DRDY_EN
Definition: mma865x.h:1778
ARM_DRIVER_I2C * I2Cdrv
fxos8700_accelmagdata_t rawData
#define MMA865x_CTRL_REG5_INT_CFG_FF_MT_MASK
Definition: mma865x.h:1818
#define MMA865x_FF_MT_CFG_XEFE_MASK
Definition: mma865x.h:851
pedometer_config_t cPedoConfig
#define MMA865x_CTRL_REG5_INT_CFG_FF_MT_INT2
Definition: mma865x.h:1850
uint16_t readFrom
Definition: sensor_drv.h:80
#define MMA865x_CTRL_REG3_PP_OD_MASK
Definition: mma865x.h:1661
void mma865x_int_callback(void *pUserData)
This is the Sensor Event Ready ISR implementation.
registerDeviceInfo_t deviceInfo
Definition: mma865x_drv.h:31
void(* toggle_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:48
#define MMA865x_FF_MT_CFG_OAE_MOTION
Definition: mma865x.h:873
volatile bool gMma865xEventReady
#define MMA865x_CTRL_REG2_SMODS_MASK
Definition: mma865x.h:1598
const registerwritelist_t cMma865xConfigDataReady[]
MMA865x DRDY and ASLP Detect Mode Register Write List.
#define MMA865x_CTRL_REG1_ASLP_RATE_MASK
Definition: mma865x.h:1537
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
#define MMA865x_FF_MT_CFG_ZEFE_MASK
Definition: mma865x.h:857
void(* pin_init)(pinID_t aPinId, gpio_direction_t dir, void *apPinConfig, gpio_isr_handler_t aIsrHandler, void *apUserData)
Definition: Driver_GPIO.h:41
const registerreadlist_t cMma865xOutput[]
Address of Data Output Registers.
union pedometer_t::pedometer_status_tag::@351 status
This structure defines the Read command List.
Definition: sensor_drv.h:78
#define MMA8652_I2C_ADDR
This structure defines the mma865x raw data buffer.
Definition: mma865x_drv.h:38
#define MMA865x_INT_SOURCE_SRC_FF_MT_MASK
Definition: mma865x.h:449
gpioHandleKSDK_t GREEN_LED
Definition: frdm_k64f.c:188
int32_t pedometer_run(pedometer_t *pPedometer, ped_accel_t *pData)
The interface function excutes the pedometer algorithm.
Definition: pedometer.c:125
#define MMA865x_CTRL_REG4_INT_EN_ASLP_DISABLED
Definition: mma865x.h:1765
#define I2C_S_DEVICE_INDEX
Definition: issdk_hal.h:35
void MMA865x_I2C_SetIdleTask(mma865x_i2c_sensorhandle_t *pSensorHandle, registeridlefunction_t idleTask, void *userParam)
: The interface function to set the I2C Idle Task.
Definition: mma865x_drv.c:53
#define MMA865x_CTRL_REG2_SMODS_LP
Definition: mma865x.h:1618
#define MMA865x_CTRL_REG2_SLPE_MASK
Definition: mma865x.h:1595
#define MMA865x_CTRL_REG3_PP_OD_PUSH_PULL
Definition: mma865x.h:1702
void BOARD_InitDebugConsole(void)
Definition: board.c:15
#define MMA865x_CTRL_REG2_SLPE_EN
Definition: mma865x.h:1620
#define MMA8652_WHOAMI_VALUE
Definition: mma865x.h:65
void pedometer_init(pedometer_t *pPedometer)
The interface function initialize the pedometer.
Definition: pedometer.c:97
#define MMA865x_FF_MT_CFG_OAE_MASK
Definition: mma865x.h:860
int main(void)
This is the The main function implementation.
#define MMA865x_CTRL_REG4_INT_EN_DRDY_MASK
Definition: mma865x.h:1740
int32_t MMA865x_I2C_Initialize(mma865x_i2c_sensorhandle_t *pSensorHandle, ARM_DRIVER_I2C *pBus, uint8_t index, uint16_t sAddress, uint8_t whoAmi)
The interface function to initialize the sensor.
Definition: mma865x_drv.c:22
void BOARD_InitPins(void)
Configures pin routing and optionally pin electrical features.
Definition: pin_mux.c:47
#define MMA865x_CTRL_REG3_WAKE_FF_MT_EN
Definition: mma865x.h:1699
#define MMA865x_CTRL_REG5_INT_CFG_ASLP_INT1
Definition: mma865x.h:1841
#define MMA865x_CTRL_REG4_INT_EN_ASLP_EN
Definition: mma865x.h:1766
const char * pActivity[5]
Pedometer Mode Name Strings.
#define MMA865x_FF_MT_CFG_YEFE_EN
Definition: mma865x.h:878