ISSDK  1.7
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  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * o Redistributions of source code must retain the above copyright notice, this list
9  * of conditions and the following disclaimer.
10  *
11  * o Redistributions in binary form must reproduce the above copyright notice, this
12  * list of conditions and the following disclaimer in the documentation and/or
13  * other materials provided with the distribution.
14  *
15  * o Neither the name of the copyright holder nor the names of its
16  * contributors may be used to endorse or promote products derived from this
17  * software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 /**
32  * @file pedometer_stepcount_mma8652.c
33  * @brief The pedometer_stepcount_mma8652.c file implements the ISSDK MMA8652 sensor
34  * driver example demonstration for Motion Activated Pedometer.
35  */
36 
37 //-----------------------------------------------------------------------
38 // SDK Includes
39 //-----------------------------------------------------------------------
40 #include "board.h"
41 #include "pin_mux.h"
42 #include "clock_config.h"
43 #include "fsl_debug_console.h"
44 
45 //-----------------------------------------------------------------------
46 // CMSIS Includes
47 //-----------------------------------------------------------------------
48 #include "Driver_I2C.h"
49 
50 //-----------------------------------------------------------------------
51 // ISSDK Includes
52 //-----------------------------------------------------------------------
53 #include "issdk_hal.h"
54 #include "pedometer.h"
55 #include "gpio_driver.h"
56 #include "mma865x_drv.h"
57 
58 //-----------------------------------------------------------------------
59 // Macros
60 //-----------------------------------------------------------------------
61 #define MT_FF_MT_THS 0x15 /* Threshold Value. */
62 #define ASLP_COUNTER 0x10 /* Auto Sleep after ~5s. */
63 #define MMA865x_DATA_SIZE 6 /* 2-byte Accel x,y,z each */
64 
65 //-----------------------------------------------------------------------
66 // Constants
67 //-----------------------------------------------------------------------
68 /*! @brief MMA865x Motion based Pedometer Register Write List. */
84 
85 /*! @brief MMA865x Motion Detect Mode Register Write List. */
87  /* Only FFMT INT Enabled */
90  /* Route FFMT to INT1 */
93 
94 /*! @brief MMA865x DRDY and ASLP Detect Mode Register Write List. */
96  /* Data and ASLP INT Enabled */
99  /* Route FFMT to INT2 (Ignore FFMT INT) */
102 
103 /*! @brief Address of INT Source Register. */
105 
106 /*! @brief Address of Data Output Registers. */
108 
109 /*! @brief Pedometer Mode Name Strings. */
110 const char *pActivity[5] = {"Unknown ", "Rest ", "Walking ", "Jogging ", "Running "};
111 
112 //-----------------------------------------------------------------------
113 // Global Variables
114 //-----------------------------------------------------------------------
115 volatile bool gMma865xEventReady = false;
116 
117 /* Pedometer configuration. These configuration are algorithm and user dependent data. */
119 {
121  .bits = {.config = 1},
122  .keynetik =
123  {
124  .steplength = 0,
125  .height = 175,
126  .weight = 80,
127  .filtersteps = 1,
128  .bits =
129  {
130  .filtertime = 2, .male = 1,
131  },
132  .speedperiod = 3,
133  .stepthreshold = 0,
134  },
135  .stepcoalesce = 1,
136  .oneG = PEDO_ONEG_2G,
137  .frequency = 50,
138 };
139 
140 //-----------------------------------------------------------------------
141 // Functions
142 //-----------------------------------------------------------------------
143 /*! -----------------------------------------------------------------------
144  * @brief This is the Sensor Event Ready ISR implementation.
145  * @details This function sets the flag which indicates if a new sample(s) is available for reading.
146  * @param[in] pUserData This is a void pointer to the instance of the user specific data structure for the ISR.
147  * @return void There is no return value.
148  * @constraints None
149  * @reeentrant Yes
150  * -----------------------------------------------------------------------*/
151 void mma865x_int_callback(void *pUserData)
152 { /*! @brief Set flag to indicate Sensor has signaled event ready. */
153  gMma865xEventReady = true;
154 }
155 
156 /*! -----------------------------------------------------------------------
157  * @brief This is the The main function implementation.
158  * @details This function invokes board initializes routines, then then brings up the sensor and
159  * finally enters an endless loop to continuously read available samples.
160  * @param[in] void This is no input parameter.
161  * @return void There is no return value.
162  * @constraints None
163  * @reeentrant No
164  * -----------------------------------------------------------------------*/
165 int main(void)
166 {
167  int32_t status;
168  bool motionDetect;
169  uint16_t lastReportedSteps;
170  uint8_t intStatus, data[MMA865x_DATA_SIZE];
171 
172  pedometer_t pedometer; /* This handle holds the current configuration and status value for the pedometer.*/
174  mma865x_i2c_sensorhandle_t mma865xDriver;
175 
176  ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER; // Now using the shield.h value!!!
178 
179  /*! Initialize the MCU hardware. */
180  BOARD_InitPins();
183 
184  PRINTF("\r\n ISSDK MMA865x sensor driver example for Motion Activated Pedometer.\r\n");
185 
186  /*! Initialize MMA865x pin used by FRDM board */
187  pGpioDriver->pin_init(&MMA8652_INT1, GPIO_DIRECTION_IN, NULL, &mma865x_int_callback, NULL);
188 
189  /*! Initialize RGB LED pin used by FRDM board */
190  pGpioDriver->pin_init(&GREEN_LED, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
191 
192  /*! Initialize the I2C driver. */
193  status = I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
194  if (ARM_DRIVER_OK != status)
195  {
196  PRINTF("\r\n I2C Initialization Failed\r\n");
197  return -1;
198  }
199 
200  /*! Set the I2C Power mode. */
201  status = I2Cdrv->PowerControl(ARM_POWER_FULL);
202  if (ARM_DRIVER_OK != status)
203  {
204  PRINTF("\r\n I2C Power Mode setting Failed\r\n");
205  return -1;
206  }
207 
208  /*! Set the I2C bus speed. */
209  status = I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
210  if (ARM_DRIVER_OK != status)
211  {
212  PRINTF("\r\n I2C Control Mode setting Failed\r\n");
213  return -1;
214  }
215 
216  /*! Initialize MMA865x sensor driver. */
219  if (SENSOR_ERROR_NONE != status)
220  {
221  PRINTF("\r\n Sensor Initialization Failed\r\n");
222  return -1;
223  }
224 
225  /*! Set the task to be executed while waiting for I2C transactions to complete. */
227 
228  /* Apply MMA865x Configuration for Motion Activated Pedometer. */
229  status = Sensor_I2C_Write(mma865xDriver.pCommDrv, &mma865xDriver.deviceInfo,
230  mma865xDriver.slaveAddress, cMma865xCommonConfiguration);
231  if (SENSOR_ERROR_NONE != status)
232  {
233  PRINTF("\r\n Write MMA865x Initialization Failed.\r\n");
234  return -1;
235  }
236 
237  /*! Initialize the pedometer*/
238  pedometer_init(&pedometer);
239  PRINTF("\r\n Pedometer successfully Initialized and Ready for measurements.");
240 
241  for (;;) /* Forever loop for Motion Detection */
242  {
243  /*! Configure the pedometer*/
244  pedometer_configure(&pedometer, &cPedoConfig);
245 
246  /* Apply MMA865x Configuration for Motion Detection. */
247  status = MMA865x_I2C_Configure(&mma865xDriver, cMma865xConfigMotionDetect);
248  if (SENSOR_ERROR_NONE != status)
249  {
250  PRINTF("\r\n Write MMA865x Motion Configuration Failed!\r\n");
251  return -1;
252  }
253 
254  motionDetect = true;
255  pGpioDriver->set_pin(&GREEN_LED);
256  PRINTF("\r\n\r\n Waiting for Motion | MCU switching to Sleep Mode ...\r\n");
257 
258  for (;;) /* Loop for Motion Detection */
259  { /* In ISR Mode we do not need to check Data Ready Register.
260  * The receipt of interrupt will indicate event is ready. */
261  if (false == gMma865xEventReady)
262  { /* Loop, if new sample is not available. */
264  continue;
265  }
266  else
267  { /*! Clear the data ready flag, it will be set again by the ISR. */
268  gMma865xEventReady = false;
269  }
270 
271  /*! Read the INT_SRC from the MMA865x. */
272  status = MMA865x_I2C_ReadData(&mma865xDriver, cMma865xINTSrc, &intStatus);
273  if (ARM_DRIVER_OK != status)
274  {
275  PRINTF("\r\n Read INT SRC Failed!\r\n");
276  return -1;
277  }
278 
279  if (motionDetect)
280  {
282  {
283  /*! Display that a Motion event has been detected. */
284  PRINTF(" Motion detected...\r\n");
285 
286  /* Apply MMA865x Configuration for ASLP Detection with DATA. */
287  status = MMA865x_I2C_Configure(&mma865xDriver, cMma865xConfigDataReady);
288  if (SENSOR_ERROR_NONE != status)
289  {
290  PRINTF("\r\n Write MMA865x DRDY Configuration Failed.\r\n");
291  return -1;
292  }
293  motionDetect = false;
294  pGpioDriver->clr_pin(&GREEN_LED);
295  }
296  }
297  else
298  {
299  /*! Read the Output from the MMA865x. */
300  status = MMA865x_I2C_ReadData(&mma865xDriver, cMma865xOutput, data);
301  if (ARM_DRIVER_OK != status)
302  {
303  PRINTF("\r\n ERROR : Read Data Failed!\r\n");
304  return -1;
305  }
306 
307  /*! Convert the raw sensor data for feeding to pedometer algorithm. */
308  rawData.accel[0] = ((int16_t)data[0] << 8) | data[1];
309  rawData.accel[1] = ((int16_t)data[2] << 8) | data[3];
310  rawData.accel[2] = ((int16_t)data[4] << 8) | data[5];
311 
312  /*! Execute the pedometer Algorithm */
313  pedometer_run(&pedometer, (ped_accel_t *)&rawData.accel);
314 
315  /* Display Steps when there is a change */
316  if (pedometer.status.stepcount && pedometer.status.stepcount != lastReportedSteps)
317  {
318  pGpioDriver->toggle_pin(&GREEN_LED);
319  lastReportedSteps = pedometer.status.stepcount;
320  if(pedometer.status.stepcount == 1)
321  {
322  PRINTF("\r\n | Steps | Distance | Speed | Calories | Activity |\r\n");
323  }
324  PRINTF("\033[K | %5d | %4dm | %2dkmph | %3d | %s |\r",
325  pedometer.status.stepcount, pedometer.status.distance, pedometer.status.speed/1000, pedometer.status.calories, pActivity[pedometer.status.status.bits.activity]);
326  }
327 
329  {
330  break;
331  }
332  }
333  }
334  }
335 }
#define MMA865x_CTRL_REG5_INT_CFG_ASLP_INT1
Definition: mma865x.h:1841
#define MMA865x_CTRL_REG3_IPOL_MASK
Definition: mma865x.h:1664
#define MMA865x_CTRL_REG3_PP_OD_PUSH_PULL
Definition: mma865x.h:1702
void pedometer_init(pedometer_t *pPedometer)
The interface function initialize the pedometer.
Definition: pedometer.c:123
const registerwritelist_t cMma865xConfigDataReady[]
MMA865x DRDY and ASLP Detect Mode Register Write List.
This defines the configuration structure of the pedometer.
Definition: pedometer.h:72
The pedometer.h file contains the interface and structure definitions for pedometer application...
#define __END_WRITE_DATA__
Definition: sensor_drv.h:71
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:48
#define MMA865x_INT_SOURCE_SRC_ASLP_READY
Definition: mma865x.h:471
#define MMA865x_CTRL_REG1_ASLP_RATE_MASK
Definition: mma865x.h:1537
const registerwritelist_t cMma865xConfigMotionDetect[]
MMA865x Motion Detect Mode Register Write List.
This structure defines the mma865x raw data buffer.
Definition: mma865x_drv.h:64
#define PEDO_ONEG_2G
Definition: pedometer.h:56
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
#define MMA865x_CTRL_REG5_INT_CFG_ASLP_MASK
Definition: mma865x.h:1833
#define MMA865x_CTRL_REG5_INT_CFG_FF_MT_MASK
Definition: mma865x.h:1818
void mma865x_int_callback(void *pUserData)
This is the Sensor Event Ready ISR implementation.
#define MMA865x_CTRL_REG3_IPOL_ACTIVE_HIGH
Definition: mma865x.h:1701
int16_t accel[3]
Definition: mma865x_drv.h:67
#define MMA865x_FF_MT_THS_THS_MASK
Definition: mma865x.h:983
uint8_t data[FXLS8962_DATA_SIZE]
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:132
int32_t status
void(* toggle_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:74
This defines the acceleration input data for the pedometer.
Definition: pedometer.h:65
#define MMA865x_CTRL_REG4_INT_EN_DRDY_DISABLED
Definition: mma865x.h:1777
void BOARD_InitDebugConsole(void)
Definition: board.c:41
#define MMA865x_CTRL_REG3_PP_OD_MASK
Definition: mma865x.h:1661
This defines the sensor specific information.
Definition: mma865x_drv.h:55
registerDeviceInfo_t deviceInfo
Definition: mma865x_drv.h:57
void(* registeridlefunction_t)(void *userParam)
This is the register idle function type.
Definition: sensor_drv.h:123
#define MMA865x_INT_SOURCE_SRC_FF_MT_READY
Definition: mma865x.h:488
#define MMA865x_CTRL_REG1_DR_MASK
Definition: mma865x.h:1534
#define MMA865x_CTRL_REG5_INT_CFG_FF_MT_INT2
Definition: mma865x.h:1850
#define __END_READ_DATA__
Definition: sensor_drv.h:77
#define MMA865x_CTRL_REG1_DR_50HZ
Definition: mma865x.h:1552
This defines the pedometer instance.
Definition: pedometer.h:99
#define MMA865x_CTRL_REG2_SLPE_MASK
Definition: mma865x.h:1595
#define MMA865x_FF_MT_CFG_YEFE_MASK
Definition: mma865x.h:854
GENERIC_DRIVER_GPIO * pGpioDriver
const char * pActivity[5]
Pedometer Mode Name Strings.
#define MMA8652_I2C_ADDR
#define MMA865x_CTRL_REG4_INT_EN_DRDY_MASK
Definition: mma865x.h:1740
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:79
#define MMA8652_WHOAMI_VALUE
Definition: mma865x.h:65
#define MMA865x_CTRL_REG4_INT_EN_FF_MT_EN
Definition: mma865x.h:1776
gpioHandleKSDK_t GREEN_LED
Definition: frdm_k64f.c:214
#define MMA865x_CTRL_REG3_WAKE_FF_MT_MASK
Definition: mma865x.h:1667
debounce_count_t sleepcount_threshold
Definition: pedometer.h:76
#define MMA865x_CTRL_REG4_INT_EN_ASLP_DISABLED
Definition: mma865x.h:1765
#define MMA865x_INT_SOURCE_SRC_ASLP_MASK
Definition: mma865x.h:464
#define MMA865x_CTRL_REG4_INT_EN_DRDY_EN
Definition: mma865x.h:1778
union pedometer_t::pedometer_status_tag::@292 status
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:87
int main(void)
This is the The main function implementation.
#define MMA865x_CTRL_REG4_INT_EN_ASLP_MASK
Definition: mma865x.h:1758
#define MMA865x_FF_MT_CFG_XEFE_EN
Definition: mma865x.h:881
#define MMA865x_CTRL_REG4_INT_EN_FF_MT_MASK
Definition: mma865x.h:1743
void(* set_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:72
#define MMA865x_CTRL_REG1_ASLP_RATE_1_56HZ
Definition: mma865x.h:1547
#define MMA865x_INT_SOURCE_SRC_FF_MT_MASK
Definition: mma865x.h:449
void(* clr_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:73
void pedometer_configure(pedometer_t *pPedometer, const pedometer_config_t *pConfig)
The interface function to configure the pedometer.
Definition: pedometer.c:137
#define MMA865x_FF_MT_CFG_ZEFE_EN
Definition: mma865x.h:875
#define MMA865x_CTRL_REG3_WAKE_FF_MT_EN
Definition: mma865x.h:1699
#define MMA865x_CTRL_REG2_SMODS_MASK
Definition: mma865x.h:1598
int32_t pedometer_run(pedometer_t *pPedometer, ped_accel_t *pData)
The interface function excutes the pedometer algorithm.
Definition: pedometer.c:151
#define MMA865x_FF_MT_CFG_ZEFE_MASK
Definition: mma865x.h:857
#define MMA865x_CTRL_REG2_SMODS_LP
Definition: mma865x.h:1618
#define MMA865x_CTRL_REG5_INT_CFG_FF_MT_INT1
Definition: mma865x.h:1851
volatile bool gMma865xEventReady
pedometer_config_t cPedoConfig
#define I2C_S_DEVICE_INDEX
Definition: issdk_hal.h:61
const registerwritelist_t cMma865xCommonConfiguration[]
MMA865x Motion based Pedometer Register Write List.
#define MMA8652_INT1
#define MMA865x_FF_MT_CFG_XEFE_MASK
Definition: mma865x.h:851
#define MMA865x_CTRL_REG5_INT_CFG_DRDY_INT1
Definition: mma865x.h:1853
#define I2C_S_DRIVER
Definition: issdk_hal.h:59
The mma865x_drv.h file describes the MMA865x driver interface and structures.
#define MMA865x_CTRL_REG4_INT_EN_ASLP_EN
Definition: mma865x.h:1766
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.
const registerreadlist_t cMma865xINTSrc[]
Address of INT Source Register.
struct pedometer_t::pedometer_status_tag status
Access structure of the GPIO Driver.
Definition: Driver_GPIO.h:64
fxls8962_acceldataUser_t rawData
This structure defines the Write command List.
Definition: sensor_drv.h:94
This structure defines the Read command List.
Definition: sensor_drv.h:104
#define SMC
Definition: lpc54114.h:144
struct pedometer_t::pedometer_status_tag::@292::@293 bits
#define MMA865x_FF_MT_CFG_YEFE_EN
Definition: mma865x.h:878
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
#define MMA865x_FF_MT_CFG_OAE_MOTION
Definition: mma865x.h:873
void BOARD_InitPins(void)
Configures pin routing and optionally pin electrical features.
Definition: pin_mux.c:73
#define I2C_S_SIGNAL_EVENT
Definition: issdk_hal.h:60
const registerreadlist_t cMma865xOutput[]
Address of Data Output Registers.
#define MMA865x_CTRL_REG5_INT_CFG_DRDY_MASK
Definition: mma865x.h:1815
ARM_DRIVER_I2C * pCommDrv
Definition: mma865x_drv.h:58
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:97
#define MMA865x_FF_MT_CFG_OAE_MASK
Definition: mma865x.h:860
#define MMA865x_CTRL_REG2_SLPE_EN
Definition: mma865x.h:1620