ISSDK  1.8
IoT Sensing Software Development Kit
pedometer_stepcount_mma8451.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_mma8451.c
11  * @brief The pedometer_stepcount_mma8451.c file implements the ISSDK prdometer
12  * example using MMA8451 sensor as the acceleration source.
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 "pedometer.h"
27 #include "mma845x_drv.h"
28 
29 /*******************************************************************************
30  * Macro Definitions
31  ******************************************************************************/
32 #define RAW_ACCEL_DATA_SIZE (6)
33 
34 /*******************************************************************************
35  * Constants
36  ******************************************************************************/
37 
38 /*! Prepare the register write list to configure MMA845x in poll mode. */
40  /*! Configure the MMA845x CTRL_REG1 to set mode to STANDBY and odr to 50Hz. */
42  /*! Configure the MMA845x to disable FIFO */
44  /*! Configure the MMA845x CTRL_REG2 to set the Oversampling mode to High Resolution. */
47 
48 /*! Prepare the register read list to read the Data Ready Status from MMA845x. */
50 
51 /*! Prepare the register read list to read the raw Accel data from MMA845x. */
54 
55 /* Pedometer configuration. These configuration are algorithm and user dependent data. */
56 static const pedometer_config_t pedo_config = {
58  .bits = {.config = 1},
59  .keynetik =
60  {
61  .height = 175,
62  .weight = 80,
63  .filtersteps = PEDO_FILTER_STEPS_DEFAULT,
64  .bits =
65  {
66  .filtertime = PEDO_FILTER_TIME_DEFAULT,
67  },
68  .speedperiod = PEDO_SPEED_PERIOD_DEFAULT,
69  .stepthreshold = PEDO_STEP_THRESHOLD_DEFAULT,
70  },
71  .stepcoalesce = 1,
72  .oneG = PEDO_ONEG_2G, // It is the One G representation in 2G scale.
73  .frequency = PEDO_FREQHZ_DEFAULT, // It is 50 HZ
74 
75 };
76 
77 /*******************************************************************************
78  * Code
79  ******************************************************************************/
80 
81 /*!
82  * @brief Main function
83  */
84 
85 int main(void)
86 {
88  uint8_t dataReady;
91 
92  ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER; // Now using the shield.h value!!!
93  mma845x_i2c_sensorhandle_t MMA845xdrv;
94  pedometer_t pedometer; /* This handle holds the current configuration and status value for the pedometer.*/
95 
96  /*! Initialize the MCU hardware */
100 
101  PRINTF("\r\n ISSDK Pedometer Example using MMA8451 sensor as the acceleration source\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 MMA845x 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 Initiliazed Sensor\r\n");
136 
137  /*! Set the task to be executed while waiting for I2C transactions to complete. */
139 
140  /*! Configure the MMA845x sensor driver with Non FIFO mode. */
141  status = MMA845x_I2C_Configure(&MMA845xdrv, mma845x_Config_Normal);
142  if (SENSOR_ERROR_NONE != status)
143  {
144  PRINTF("\r\n MMA845x Sensor Configuration Failed, Err = %d\r\n", status);
145  return -1;
146  }
147  else
148  {
149  PRINTF("\r\n Successfully Applied MMA845x Sensor Configuration\r\n");
150  }
151 
152  /*! Initialize the pedometer*/
153  pedometer_init(&pedometer);
154 
155  /*! Configure the pedometer*/
156  pedometer_configure(&pedometer, &pedo_config);
157 
158  for (;;) // Forever loop
159  {
160  /*! Read the data ready status from MMA845x*/
161  status = MMA845x_I2C_ReadData(&MMA845xdrv, mma845x_Data_Ready, &dataReady);
162  if (0 == (dataReady & MMA845x_STATUS_ZYXDR_MASK))
163  { /* Loop, if new sample is not available. */
164  continue;
165  }
166 
167  /*! Read the raw sensor data from the MMA845x. */
168  status = MMA845x_I2C_ReadData(&MMA845xdrv, mma845x_Output_Values, (uint8_t *)&data[0]);
169  if (ARM_DRIVER_OK != status)
170  {
171  continue; // Read did not work, so loop.
172  }
173 
174  /*! Convert the raw sensor data for display to the debug port. */
175  rawData.accel[0] = ((int16_t)data[0] << 8) | data[1];
176  rawData.accel[1] = ((int16_t)data[2] << 8) | data[3];
177  rawData.accel[2] = ((int16_t)data[4] << 8) | data[5];
178 
179  /*! Execute the pedometer Algorithm */
180  pedometer_run(&pedometer, (ped_accel_t *)&rawData.accel);
181 
182  PRINTF("\r\n StepCount = %d \r\n", pedometer.status.stepcount);
183  }
184 }
185 ////////////////////////////////////////////////////////////////////////////////
186 // EOF
187 ////////////////////////////////////////////////////////////////////////////////
struct pedometer_t::pedometer_status_tag status
void pedometer_configure(pedometer_t *pPedometer, const pedometer_config_t *pConfig)
The interface function to configure the pedometer.
Definition: pedometer.c:111
#define MMA8451_WHO_AM_I_WHOAMI_VALUE
Definition: mma845x.h:482
int16_t accel[3]
Definition: mma845x_drv.h:41
This structure defines the Write command List.
Definition: sensor_drv.h:68
const registerreadlist_t mma845x_Data_Ready[]
int32_t status
The pedometer.h file contains the interface and structure definitions for pedometer application...
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
#define PEDO_FILTER_STEPS_DEFAULT
Definition: pedometer.h:35
int32_t MMA845x_I2C_Configure(mma845x_i2c_sensorhandle_t *pSensorHandle, const registerwritelist_t *pRegWriteList)
The interface function to configure he sensor.
Definition: mma845x_drv.c:61
#define SMC
Definition: lpc54114.h:118
int32_t MMA845x_I2C_Initialize(mma845x_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: mma845x_drv.c:22
This defines the configuration structure of the pedometer.
Definition: pedometer.h:46
#define __END_WRITE_DATA__
Definition: sensor_drv.h:45
void MMA845x_I2C_SetIdleTask(mma845x_i2c_sensorhandle_t *pSensorHandle, registeridlefunction_t idleTask, void *userParam)
: The interface function to set the I2C Idle Task.
Definition: mma845x_drv.c:53
debounce_count_t sleepcount_threshold
Definition: pedometer.h:50
This structure defines the mma845x raw data buffer.
Definition: mma845x_drv.h:38
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
#define PEDO_FREQHZ_DEFAULT
Definition: pedometer.h:31
#define I2C_S_SIGNAL_EVENT
Definition: issdk_hal.h:34
#define I2C_S_DRIVER
Definition: issdk_hal.h:33
#define PEDO_ONEG_2G
Definition: pedometer.h:30
This defines the pedometer instance.
Definition: pedometer.h:73
#define MMA845x_CTRL_REG1_DR_50HZ
Definition: mma845x.h:1609
This defines the acceleration input data for the pedometer.
Definition: pedometer.h:39
#define BOARD_BootClockRUN
Definition: clock_config.h:19
#define PEDO_FILTER_TIME_DEFAULT
Definition: pedometer.h:34
void(* registeridlefunction_t)(void *userParam)
This is the register idle function type.
Definition: sensor_drv.h:97
uint8_t data[FXLS8962_DATA_SIZE]
#define __END_READ_DATA__
Definition: sensor_drv.h:51
const registerwritelist_t mma845x_Config_Normal[]
ARM_DRIVER_I2C * I2Cdrv
fxos8700_accelmagdata_t rawData
uint16_t readFrom
Definition: sensor_drv.h:80
#define MMA845x_F_SETUP_F_MODE_FIFODISABLED
Definition: mma845x.h:285
#define MMA845x_ACCEL_DATA_SIZE
The size of the MMA845x accel data.
Definition: mma845x_drv.h:47
#define PEDO_SPEED_PERIOD_DEFAULT
Definition: pedometer.h:33
#define MMA845x_STATUS_ZYXDR_MASK
Definition: mma845x.h:117
int main(void)
Main function.
This structure defines the Read command List.
Definition: sensor_drv.h:78
int32_t pedometer_run(pedometer_t *pPedometer, ped_accel_t *pData)
The interface function excutes the pedometer algorithm.
Definition: pedometer.c:125
#define I2C_S_DEVICE_INDEX
Definition: issdk_hal.h:35
#define PEDO_STEP_THRESHOLD_DEFAULT
Definition: pedometer.h:32
#define MMA845x_CTRL_REG1_MODE_STANDBY
Definition: mma845x.h:1599
This defines the sensor specific information.
Definition: mma845x_drv.h:29
The mma845x_drv.h file describes the MMA845x driver interface and structures.
void BOARD_InitDebugConsole(void)
Definition: board.c:15
void pedometer_init(pedometer_t *pPedometer)
The interface function initialize the pedometer.
Definition: pedometer.c:97
void BOARD_InitPins(void)
Configures pin routing and optionally pin electrical features.
Definition: pin_mux.c:47
#define MMA845x_CTRL_REG2_SMODS_HIGHRES
Definition: mma845x.h:1675
const registerreadlist_t mma845x_Output_Values[]
int32_t MMA845x_I2C_ReadData(mma845x_i2c_sensorhandle_t *pSensorHandle, const registerreadlist_t *pReadList, uint8_t *pBuffer)
The interface function to read the sensor data.
Definition: mma845x_drv.c:104
#define MMA845x_I2C_ADDR
Definition: frdm_kl25z.h:102