ISSDK  1.7
IoT Sensing Software Development Kit
pedometer_stepcount_motion_fxls8962.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_fxls8962.c
33  * @brief The pedometer_stepcount_fxls8962.c file implements the ISSDK FXLS8962 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 "fxls8962_drv.h"
57 
58 //-----------------------------------------------------------------------
59 // Macros
60 //-----------------------------------------------------------------------
61 #define SDCD_LTHS_LSB 0xA0 /* Lower Threshold LSB value. */
62 #define SDCD_LTHS_MSB 0x0F /* Lower Threshold MSB value. */
63 #define SDCD_UTHS_LSB 0x40 /* Upper Threshold LSB value. */
64 #define SDCD_UTHS_MSB 0x00 /* Upper Threshold MSB value. */
65 #define ASLP_COUNT_LSB 0xFA /* Auto Sleep after 5s @50Hz Wake ODR */
66 #define ASLP_COUNT_MSB 0x00 /* Auto Sleep after 5s @50Hz Wake ODR */
67 #define FXLS8962_DATA_SIZE 6 /* 2 byte X,Y,Z Axis Data each. */
68 
69 //-----------------------------------------------------------------------
70 // Constants
71 //-----------------------------------------------------------------------
72 /*! @brief FXLS8962 Motion based Pedometer Register Write List. */
74  /* Enable SDCD Events */
77  /* Enable SDCD Block */
80  /* Set WAKE and Sleep ODRs */
83  /* Wake Sleep SDCD OT Event Enabled */
86  /* ASLP and Data INT enabled */
88  /* Auto Sleep LSB value */
90  /* Auto Sleep MSB value */
92  /* Set SDCD Lower Threshold LSB value. */
94  /* Set SDCD Lower Threshold MSB value. */
96  /* Set SDCD Upper Threshold LSB value. */
98  /* Set SDCD Upper Threshold MSB value. */
101 
102 /*! @brief FXLS8962 Motion Detect Mode Register Write List. */
104  /* Only SDCD INT Enabled */
107 
108 /*! @brief FXLS8962 DRDY and ASLP Detect Mode Register Write List. */
110  /* Data and ASLP INT Enabled */
113 
114 /*! @brief Address of INT Status Register. */
116 
117 /*! @brief Address of Data Output Registers. */
119 
120 /*! @brief Pedometer Mode Name Strings. */
121 const char *pActivity[5] = {"Unknown ", "Rest ", "Walking ", "Jogging ", "Running "};
122 
123 //-----------------------------------------------------------------------
124 // Global Variables
125 //-----------------------------------------------------------------------
126 volatile bool gFxls8962EventReady = false;
127 
128 /* Pedometer configuration. These configuration are algorithm and user dependent data. */
130 {
132  .bits = {.config = 1},
133  .keynetik =
134  {
135  .steplength = 0,
136  .height = 175,
137  .weight = 80,
138  .filtersteps = 1,
139  .bits =
140  {
141  .filtertime = 2, .male = 1,
142  },
143  .speedperiod = 3,
144  .stepthreshold = 0,
145  },
146  .stepcoalesce = 1,
147  .oneG = PEDO_ONEG_2G,
148  .frequency = 50,
149 };
150 
152 {
153  .pinConfig = {kGPIO_DigitalInput, 0},
154  .portPinConfig = {.pullSelect = kPORT_PullUp, .mux = kPORT_MuxAsGpio},
155  .interruptMode = kPORT_InterruptFallingEdge,
156 };
157 
158 //-----------------------------------------------------------------------
159 // Functions
160 //-----------------------------------------------------------------------
161 /*! -----------------------------------------------------------------------
162  * @brief This is the Sensor Event Ready ISR implementation.
163  * @details This function sets the flag which indicates if a new sample(s) is available for reading.
164  * @param[in] pUserData This is a void pointer to the instance of the user specific data structure for the ISR.
165  * @return void There is no return value.
166  * @constraints None
167  * @reeentrant Yes
168  * -----------------------------------------------------------------------*/
169 void fxls8962_int_callback(void *pUserData)
170 { /*! @brief Set flag to indicate Sensor has signaled event ready. */
171  gFxls8962EventReady = true;
172 }
173 
174 /*! -----------------------------------------------------------------------
175  * @brief This is the The main function implementation.
176  * @details This function invokes board initializes routines, then then brings up the sensor and
177  * finally enters an endless loop to continuously read available samples.
178  * @param[in] void This is no input parameter.
179  * @return void There is no return value.
180  * @constraints None
181  * @reeentrant No
182  * -----------------------------------------------------------------------*/
183 int main(void)
184 {
185  int32_t status;
186  bool motionDetect;
187  uint16_t lastReportedSteps;
188  uint8_t intStatus, data[FXLS8962_DATA_SIZE];
189 
190  pedometer_t pedometer; /* This handle holds the current configuration and status value for the pedometer.*/
193 
194  ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER; // Now using the shield.h value!!!
196 
197  /*! Initialize the MCU hardware. */
198  BOARD_InitPins();
201 
202  PRINTF("\r\n ISSDK FXLS8962 sensor driver example for Motion Activated Pedometer.\r\n");
203 
204  /*! Initialize FXLS8962 pin used by FRDM board */
205  pGpioDriver->pin_init(&FXLS8962_INT1, GPIO_DIRECTION_IN, &gpioConfigINT1, &fxls8962_int_callback, NULL);
206 
207  /*! Initialize RGB LED pin used by FRDM board */
208  pGpioDriver->pin_init(&GREEN_LED, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
209 
210  /*! Initialize the I2C driver. */
211  status = I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
212  if (ARM_DRIVER_OK != status)
213  {
214  PRINTF("\r\n I2C Initialization Failed\r\n");
215  return -1;
216  }
217 
218  /*! Set the I2C Power mode. */
219  status = I2Cdrv->PowerControl(ARM_POWER_FULL);
220  if (ARM_DRIVER_OK != status)
221  {
222  PRINTF("\r\n I2C Power Mode setting Failed\r\n");
223  return -1;
224  }
225 
226  /*! Set the I2C bus speed. */
227  status = I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
228  if (ARM_DRIVER_OK != status)
229  {
230  PRINTF("\r\n I2C Control Mode setting Failed\r\n");
231  return -1;
232  }
233 
234  /*! Initialize FXLS8962 sensor driver. */
237  if (SENSOR_ERROR_NONE != status)
238  {
239  PRINTF("\r\n Sensor Initialization Failed\r\n");
240  return -1;
241  }
242 
243  /*! Set the task to be executed while waiting for I2C transactions to complete. */
245 
246  /* Apply FXLS8962 Configuration for Motion Activated Pedometer. */
247  status = Sensor_I2C_Write(fxls8962Driver.pCommDrv, &fxls8962Driver.deviceInfo,
248  fxls8962Driver.slaveAddress, cFxls8962ConfigInitialize);
249  if (SENSOR_ERROR_NONE != status)
250  {
251  PRINTF("\r\n Write FXLS8962 Initialization Failed.\r\n");
252  return -1;
253  }
254 
255  /*! Initialize the pedometer*/
256  pedometer_init(&pedometer);
257  PRINTF("\r\n Pedometer successfully Initialized and Ready for measurements.");
258 
259  for (;;) /* Forever loop for Motion Detection */
260  {
261  /*! Configure the pedometer*/
262  pedometer_configure(&pedometer, &cPedoConfig);
263 
264  /* Apply FXLS8962 Configuration for Motion Detection. */
265  status = FXLS8962_I2C_Configure(&fxls8962Driver, cFxls8962ConfigMotionDetect);
266  if (SENSOR_ERROR_NONE != status)
267  {
268  PRINTF("\r\n Write FXLS8962 Motion Configuration Failed!\r\n");
269  return -1;
270  }
271 
272  motionDetect = true;
273  pGpioDriver->set_pin(&GREEN_LED);
274  PRINTF("\r\n\r\n Waiting for Motion | MCU switching to Sleep Mode ...\r\n");
275 
276  for (;;) /* Loop for Motion Detection */
277  { /* In ISR Mode we do not need to check Data Ready Register.
278  * The receipt of interrupt will indicate event is ready. */
279  if (false == gFxls8962EventReady)
280  { /* Loop, if new sample is not available. */
282  continue;
283  }
284  else
285  { /*! Clear the data ready flag, it will be set again by the ISR. */
286  gFxls8962EventReady = false;
287  }
288 
289  /*! Read the INT Status from the FXLS8962. */
290  status = FXLS8962_I2C_ReadData(&fxls8962Driver, cFxls8962INTStatus, &intStatus);
291  if (ARM_DRIVER_OK != status)
292  {
293  PRINTF("\r\n Read INT Status Failed!\r\n");
294  return -1;
295  }
296 
297  if (motionDetect)
298  {
299  if ((intStatus & FXLS8962_INT_STATUS_SRC_SDCD_OT_MASK) == FXLS8962_INT_STATUS_SRC_SDCD_OT_MASK)
300  {
301  /*! Display that a Motion event has been detected. */
302  PRINTF(" Motion detected...\r\n");
303 
304  /* Apply FXLS8962 Configuration for ASLP Detection with DATA. */
305  status = FXLS8962_I2C_Configure(&fxls8962Driver, cFxls8962ConfigDataReady);
306  if (SENSOR_ERROR_NONE != status)
307  {
308  PRINTF("\r\n Write FXLS8962 DRDY Configuration Failed.\r\n");
309  return -1;
310  }
311  motionDetect = false;
312  pGpioDriver->clr_pin(&GREEN_LED);
313  }
314  }
315  else
316  {
317  /*! Read the Output from the FXLS8962. */
318  status = FXLS8962_I2C_ReadData(&fxls8962Driver, cFxls8962Output, data);
319  if (ARM_DRIVER_OK != status)
320  {
321  PRINTF("\r\n ERROR : Read Data Failed!\r\n");
322  return -1;
323  }
324 
325  /*! Convert the raw sensor data for feeding to pedometer algorithm. */
326  rawData.accel[0] = ((int16_t)data[1] << 8) | data[0];
327  rawData.accel[0] *= 16; /* Pedometer requires signed 12-bit Left justified data @1024 counts/g. */
328  rawData.accel[1] = ((int16_t)data[3] << 8) | data[2];
329  rawData.accel[1] *= 16; /* Pedometer requires signed 12-bit Left justified data @1024 counts/g. */
330  rawData.accel[2] = ((int16_t)data[5] << 8) | data[4];
331  rawData.accel[2] *= 16; /* Pedometer requires signed 12-bit Left justified data @1024 counts/g. */
332 
333  /*! Execute the pedometer Algorithm */
334  pedometer_run(&pedometer, (ped_accel_t *)&rawData.accel);
335 
336  /* Display Steps when there is a change */
337  if (pedometer.status.stepcount && pedometer.status.stepcount != lastReportedSteps)
338  {
339  pGpioDriver->toggle_pin(&GREEN_LED);
340  lastReportedSteps = pedometer.status.stepcount;
341  if(pedometer.status.stepcount == 1)
342  {
343  PRINTF("\r\n | Steps | Distance | Speed | Calories | Activity |\r\n");
344  }
345  PRINTF("\033[K | %5d | %4dm | %2dkmph | %3d | %s |\r",
346  pedometer.status.stepcount, pedometer.status.distance, pedometer.status.speed/1000, pedometer.status.calories, pActivity[pedometer.status.status.bits.activity]);
347  }
348 
349  if ((intStatus & FXLS8962_INT_STATUS_SRC_ASLP_MASK) == FXLS8962_INT_STATUS_SRC_ASLP_MASK)
350  {
351  break;
352  }
353  }
354  }
355  }
356 }
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
#define FXLS8962_SENS_CONFIG4_WK_SDCD_OT_MASK
Definition: fxls8962.h:656
The pedometer.h file contains the interface and structure definitions for pedometer application...
This structure defines the fxls8962 raw data buffer.
Definition: fxls8962_drv.h:79
#define FXLS8962_SDCD_CONFIG1_Z_OT_EN_EN
Definition: fxls8962.h:1579
#define __END_WRITE_DATA__
Definition: sensor_drv.h:71
int32_t FXLS8962_I2C_Initialize(fxls8962_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: fxls8962_drv.c:260
#define PEDO_ONEG_2G
Definition: pedometer.h:56
#define FXLS8962_SENS_CONFIG3_WAKE_ODR_50HZ
Definition: fxls8962.h:586
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
pedometer_config_t cPedoConfig
#define FXLS8962_SDCD_CONFIG1_Y_OT_EN_EN
Definition: fxls8962.h:1575
This defines the sensor specific information for I2C.
Definition: fxls8962_drv.h:70
#define FXLS8962_INT1
uint8_t data[FXLS8962_DATA_SIZE]
int32_t FXLS8962_I2C_Configure(fxls8962_i2c_sensorhandle_t *pSensorHandle, const registerwritelist_t *pRegWriteList)
The interface function to configure he sensor.
Definition: fxls8962_drv.c:299
#define FXLS8962_SDCD_CONFIG2_SDCD_EN_EN
Definition: fxls8962.h:1651
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
The GPIO Configuration KSDK.
Definition: gpio_driver.h:65
void FXLS8962_I2C_SetIdleTask(fxls8962_i2c_sensorhandle_t *pSensorHandle, registeridlefunction_t idleTask, void *userParam)
: The interface function to set the I2C Idle Task.
Definition: fxls8962_drv.c:291
#define FXLS8962_SENS_CONFIG3_SLEEP_ODR_MASK
Definition: fxls8962.h:571
void BOARD_InitDebugConsole(void)
Definition: board.c:41
#define FXLS8962_SDCD_CONFIG1_X_OT_EN_EN
Definition: fxls8962.h:1571
#define FXLS8962_SENS_CONFIG4_INT_POL_ACT_LOW
Definition: fxls8962.h:701
registerDeviceInfo_t deviceInfo
Definition: fxls8962_drv.h:72
#define FXLS8962_SDCD_CONFIG2_WT_DBCTM_CLEARED
Definition: fxls8962.h:1671
void(* registeridlefunction_t)(void *userParam)
This is the register idle function type.
Definition: sensor_drv.h:123
fxls8962_i2c_sensorhandle_t fxls8962Driver
const registerwritelist_t cFxls8962ConfigDataReady[]
FXLS8962 DRDY and ASLP Detect Mode Register Write List.
#define __END_READ_DATA__
Definition: sensor_drv.h:77
This defines the pedometer instance.
Definition: pedometer.h:99
int main(void)
This is the The main function implementation.
GENERIC_DRIVER_GPIO * pGpioDriver
#define FXLS8962_SDCD_CONFIG2_OT_DBCTM_CLEARED
Definition: fxls8962.h:1665
#define FXLS8962_INT_EN_SDCD_OT_EN_EN
Definition: fxls8962.h:884
const registerwritelist_t cFxls8962ConfigMotionDetect[]
FXLS8962 Motion Detect Mode Register Write List.
volatile bool gFxls8962EventReady
#define FXLS8962_I2C_ADDR
#define FXLS8962_SENS_CONFIG4_INT_POL_MASK
Definition: fxls8962.h:641
gpioHandleKSDK_t GREEN_LED
Definition: frdm_k64f.c:214
#define FXLS8962_SENS_CONFIG3_WAKE_ODR_MASK
Definition: fxls8962.h:574
debounce_count_t sleepcount_threshold
Definition: pedometer.h:76
union pedometer_t::pedometer_status_tag::@292 status
gpio_pin_config_t pinConfig
Definition: gpio_driver.h:67
void(* set_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:72
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
const registerreadlist_t cFxls8962INTStatus[]
Address of INT Status Register.
#define FXLS8962_SDCD_CONFIG2_REF_UPDM_SDCD_REF
Definition: fxls8962.h:1656
The fxls8962_drv.h file describes the FXLS8962AF driver interface and structures. ...
int32_t pedometer_run(pedometer_t *pPedometer, ped_accel_t *pData)
The interface function excutes the pedometer algorithm.
Definition: pedometer.c:151
#define I2C_S_DEVICE_INDEX
Definition: issdk_hal.h:61
const registerwritelist_t cFxls8962ConfigInitialize[]
FXLS8962 Motion based Pedometer Register Write List.
#define FXLS8962_INT_STATUS_SRC_ASLP_MASK
Definition: fxls8962.h:152
#define I2C_S_DRIVER
Definition: issdk_hal.h:59
GENERIC_DRIVER_GPIO Driver_GPIO_KSDK
Definition: gpio_driver.c:203
void fxls8962_int_callback(void *pUserData)
This is the Sensor Event Ready ISR implementation.
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
#define FXLS8962_WHOAMI_VALUE
Definition: fxls8962.h:113
const registerreadlist_t cFxls8962Output[]
Address of Data Output Registers.
ARM_DRIVER_I2C * pCommDrv
Definition: fxls8962_drv.h:73
struct pedometer_t::pedometer_status_tag status
Access structure of the GPIO Driver.
Definition: Driver_GPIO.h:64
#define FXLS8962_INT_STATUS_SRC_SDCD_OT_MASK
Definition: fxls8962.h:161
fxls8962_acceldataUser_t rawData
gpioConfigKSDK_t gpioConfigINT1
const char * pActivity[5]
Pedometer Mode Name Strings.
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
int32_t FXLS8962_I2C_ReadData(fxls8962_i2c_sensorhandle_t *pSensorHandle, const registerreadlist_t *pReadList, uint8_t *pBuffer)
The interface function to read the sensor data.
Definition: fxls8962_drv.c:344
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 BOARD_InitPins(void)
Configures pin routing and optionally pin electrical features.
Definition: pin_mux.c:73
#define FXLS8962_INT_EN_DRDY_EN_EN
Definition: fxls8962.h:880
#define FXLS8962_INT_EN_ASLP_EN_EN
Definition: fxls8962.h:891
#define FXLS8962_SENS_CONFIG3_SLEEP_ODR_0_781HZ
Definition: fxls8962.h:605
#define I2C_S_SIGNAL_EVENT
Definition: issdk_hal.h:60
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 FXLS8962_SENS_CONFIG4_WK_SDCD_OT_EN
Definition: fxls8962.h:678