ISSDK  1.7
IoT Sensing Software Development Kit
pedometer_stepcount_mma8451.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 pedometer_stepcount_mma8451.c
37  * @brief The pedometer_stepcount_mma8451.c file implements the ISSDK prdometer
38  * example using MMA8451 sensor as the acceleration source.
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 "pedometer.h"
53 #include "mma845x_drv.h"
54 
55 /*******************************************************************************
56  * Macro Definitions
57  ******************************************************************************/
58 #define RAW_ACCEL_DATA_SIZE (6)
59 
60 /*******************************************************************************
61  * Constants
62  ******************************************************************************/
63 
64 /*! Prepare the register write list to configure MMA845x in poll mode. */
66  /*! Configure the MMA845x CTRL_REG1 to set mode to STANDBY and odr to 50Hz. */
68  /*! Configure the MMA845x to disable FIFO */
70  /*! Configure the MMA845x CTRL_REG2 to set the Oversampling mode to High Resolution. */
73 
74 /*! Prepare the register read list to read the Data Ready Status from MMA845x. */
76 
77 /*! Prepare the register read list to read the raw Accel data from MMA845x. */
80 
81 /* Pedometer configuration. These configuration are algorithm and user dependent data. */
82 static const pedometer_config_t pedo_config = {
84  .bits = {.config = 1},
85  .keynetik =
86  {
87  .height = 175,
88  .weight = 80,
89  .filtersteps = PEDO_FILTER_STEPS_DEFAULT,
90  .bits =
91  {
92  .filtertime = PEDO_FILTER_TIME_DEFAULT,
93  },
94  .speedperiod = PEDO_SPEED_PERIOD_DEFAULT,
95  .stepthreshold = PEDO_STEP_THRESHOLD_DEFAULT,
96  },
97  .stepcoalesce = 1,
98  .oneG = PEDO_ONEG_2G, // It is the One G representation in 2G scale.
99  .frequency = PEDO_FREQHZ_DEFAULT, // It is 50 HZ
100 
101 };
102 
103 /*******************************************************************************
104  * Code
105  ******************************************************************************/
106 
107 /*!
108  * @brief Main function
109  */
110 
111 int main(void)
112 {
113  int32_t status;
114  uint8_t dataReady;
115  uint8_t data[MMA845x_ACCEL_DATA_SIZE];
117 
118  ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER; // Now using the shield.h value!!!
119  mma845x_i2c_sensorhandle_t MMA845xdrv;
120  pedometer_t pedometer; /* This handle holds the current configuration and status value for the pedometer.*/
121 
122  /*! Initialize the MCU hardware */
123  BOARD_InitPins();
126 
127  PRINTF("\r\n ISSDK Pedometer Example using MMA8451 sensor as the acceleration source\r\n");
128 
129  /*! Initialize the I2C driver. */
130  status = I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
131  if (ARM_DRIVER_OK != status)
132  {
133  PRINTF("\r\n I2C Initialization Failed\r\n");
134  return -1;
135  }
136 
137  /*! Set the I2C Power mode. */
138  status = I2Cdrv->PowerControl(ARM_POWER_FULL);
139  if (ARM_DRIVER_OK != status)
140  {
141  PRINTF("\r\n I2C Power Mode setting Failed\r\n");
142  return -1;
143  }
144 
145  /*! Set the I2C bus speed. */
146  status = I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
147  if (ARM_DRIVER_OK != status)
148  {
149  PRINTF("\r\n I2C Control Mode setting Failed\r\n");
150  return -1;
151  }
152 
153  /*! Initialize the MMA845x 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 Initiliazed Sensor\r\n");
162 
163  /*! Set the task to be executed while waiting for I2C transactions to complete. */
165 
166  /*! Configure the MMA845x sensor driver with Non FIFO mode. */
167  status = MMA845x_I2C_Configure(&MMA845xdrv, mma845x_Config_Normal);
168  if (SENSOR_ERROR_NONE != status)
169  {
170  PRINTF("\r\n MMA845x Sensor Configuration Failed, Err = %d\r\n", status);
171  return -1;
172  }
173  else
174  {
175  PRINTF("\r\n Successfully Applied MMA845x Sensor Configuration\r\n");
176  }
177 
178  /*! Initialize the pedometer*/
179  pedometer_init(&pedometer);
180 
181  /*! Configure the pedometer*/
182  pedometer_configure(&pedometer, &pedo_config);
183 
184  for (;;) // Forever loop
185  {
186  /*! Read the data ready status from MMA845x*/
187  status = MMA845x_I2C_ReadData(&MMA845xdrv, mma845x_Data_Ready, &dataReady);
188  if (0 == (dataReady & MMA845x_STATUS_ZYXDR_MASK))
189  { /* Loop, if new sample is not available. */
190  continue;
191  }
192 
193  /*! Read the raw sensor data from the MMA845x. */
194  status = MMA845x_I2C_ReadData(&MMA845xdrv, mma845x_Output_Values, (uint8_t *)&data[0]);
195  if (ARM_DRIVER_OK != status)
196  {
197  continue; // Read did not work, so loop.
198  }
199 
200  /*! Convert the raw sensor data for display to the debug port. */
201  rawData.accel[0] = ((int16_t)data[0] << 8) | data[1];
202  rawData.accel[1] = ((int16_t)data[2] << 8) | data[3];
203  rawData.accel[2] = ((int16_t)data[4] << 8) | data[5];
204 
205  /*! Execute the pedometer Algorithm */
206  pedometer_run(&pedometer, (ped_accel_t *)&rawData.accel);
207 
208  PRINTF("\r\n StepCount = %d \r\n", pedometer.status.stepcount);
209  }
210 }
211 ////////////////////////////////////////////////////////////////////////////////
212 // EOF
213 ////////////////////////////////////////////////////////////////////////////////
void pedometer_init(pedometer_t *pPedometer)
The interface function initialize the pedometer.
Definition: pedometer.c:123
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
#define PEDO_FREQHZ_DEFAULT
Definition: pedometer.h:57
#define PEDO_ONEG_2G
Definition: pedometer.h:56
This defines the sensor specific information.
Definition: mma845x_drv.h:55
#define BOARD_BootClockRUN
Definition: clock_config.h:45
int16_t accel[3]
Definition: mma845x_drv.h:67
uint8_t data[FXLS8962_DATA_SIZE]
int32_t status
This defines the acceleration input data for the pedometer.
Definition: pedometer.h:65
void BOARD_InitDebugConsole(void)
Definition: board.c:41
#define PEDO_STEP_THRESHOLD_DEFAULT
Definition: pedometer.h:58
const registerwritelist_t mma845x_Config_Normal[]
void(* registeridlefunction_t)(void *userParam)
This is the register idle function type.
Definition: sensor_drv.h:123
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:48
#define __END_READ_DATA__
Definition: sensor_drv.h:77
This defines the pedometer instance.
Definition: pedometer.h:99
This structure defines the mma845x raw data buffer.
Definition: mma845x_drv.h:64
#define MMA845x_F_SETUP_F_MODE_FIFODISABLED
Definition: mma845x.h:311
#define PEDO_SPEED_PERIOD_DEFAULT
Definition: pedometer.h:59
debounce_count_t sleepcount_threshold
Definition: pedometer.h:76
void pedometer_configure(pedometer_t *pPedometer, const pedometer_config_t *pConfig)
The interface function to configure the pedometer.
Definition: pedometer.c:137
const registerreadlist_t mma845x_Data_Ready[]
#define MMA845x_CTRL_REG1_DR_50HZ
Definition: mma845x.h:1635
int32_t pedometer_run(pedometer_t *pPedometer, ped_accel_t *pData)
The interface function excutes the pedometer algorithm.
Definition: pedometer.c:151
#define MMA845x_CTRL_REG2_SMODS_HIGHRES
Definition: mma845x.h:1701
#define MMA845x_I2C_ADDR
Definition: frdm_kl25z.h:128
#define I2C_S_DEVICE_INDEX
Definition: issdk_hal.h:61
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:130
#define I2C_S_DRIVER
Definition: issdk_hal.h:59
#define MMA845x_CTRL_REG1_MODE_STANDBY
Definition: mma845x.h:1625
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
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:87
const registerreadlist_t mma845x_Output_Values[]
#define MMA845x_STATUS_ZYXDR_MASK
Definition: mma845x.h:143
struct pedometer_t::pedometer_status_tag status
fxls8962_acceldataUser_t rawData
#define PEDO_FILTER_STEPS_DEFAULT
Definition: pedometer.h:61
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
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
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:79
#define MMA845x_ACCEL_DATA_SIZE
The size of the MMA845x accel data.
Definition: mma845x_drv.h:73
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
#define MMA8451_WHO_AM_I_WHOAMI_VALUE
Definition: mma845x.h:508
The mma845x_drv.h file describes the MMA845x driver interface and structures.
int main(void)
Main function.
#define PEDO_FILTER_TIME_DEFAULT
Definition: pedometer.h:60