ISSDK  1.8
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  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 /**
10  * @file pedometer_stepcount_fxls8962.c
11  * @brief The pedometer_stepcount_fxls8962.c file implements the ISSDK FXLS8962 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 "fxls8962_drv.h"
35 
36 //-----------------------------------------------------------------------
37 // Macros
38 //-----------------------------------------------------------------------
39 #define SDCD_LTHS_LSB 0xA0 /* Lower Threshold LSB value. */
40 #define SDCD_LTHS_MSB 0x0F /* Lower Threshold MSB value. */
41 #define SDCD_UTHS_LSB 0x40 /* Upper Threshold LSB value. */
42 #define SDCD_UTHS_MSB 0x00 /* Upper Threshold MSB value. */
43 #define ASLP_COUNT_LSB 0xFA /* Auto Sleep after 5s @50Hz Wake ODR */
44 #define ASLP_COUNT_MSB 0x00 /* Auto Sleep after 5s @50Hz Wake ODR */
45 #define FXLS8962_DATA_SIZE 6 /* 2 byte X,Y,Z Axis Data each. */
46 
47 //-----------------------------------------------------------------------
48 // Constants
49 //-----------------------------------------------------------------------
50 /*! @brief FXLS8962 Motion based Pedometer Register Write List. */
52  /* Enable SDCD Events */
55  /* Enable SDCD Block */
58  /* Set WAKE and Sleep ODRs */
61  /* Wake Sleep SDCD OT Event Enabled */
64  /* ASLP and Data INT enabled */
66  /* Auto Sleep LSB value */
68  /* Auto Sleep MSB value */
70  /* Set SDCD Lower Threshold LSB value. */
72  /* Set SDCD Lower Threshold MSB value. */
74  /* Set SDCD Upper Threshold LSB value. */
76  /* Set SDCD Upper Threshold MSB value. */
79 
80 /*! @brief FXLS8962 Motion Detect Mode Register Write List. */
82  /* Only SDCD INT Enabled */
85 
86 /*! @brief FXLS8962 DRDY and ASLP Detect Mode Register Write List. */
88  /* Data and ASLP INT Enabled */
91 
92 /*! @brief Address of INT Status Register. */
94 
95 /*! @brief Address of Data Output Registers. */
97 
98 /*! @brief Pedometer Mode Name Strings. */
99 const char *pActivity[5] = {"Unknown ", "Rest ", "Walking ", "Jogging ", "Running "};
100 
101 //-----------------------------------------------------------------------
102 // Global Variables
103 //-----------------------------------------------------------------------
104 volatile bool gFxls8962EventReady = false;
105 
106 /* Pedometer configuration. These configuration are algorithm and user dependent data. */
108 {
110  .bits = {.config = 1},
111  .keynetik =
112  {
113  .steplength = 0,
114  .height = 175,
115  .weight = 80,
116  .filtersteps = 1,
117  .bits =
118  {
119  .filtertime = 2, .male = 1,
120  },
121  .speedperiod = 3,
122  .stepthreshold = 0,
123  },
124  .stepcoalesce = 1,
125  .oneG = PEDO_ONEG_2G,
126  .frequency = 50,
127 };
128 
130 {
131  .pinConfig = {kGPIO_DigitalInput, 0},
132  .portPinConfig = {.pullSelect = kPORT_PullUp, .mux = kPORT_MuxAsGpio},
133  .interruptMode = kPORT_InterruptFallingEdge,
134 };
135 
136 //-----------------------------------------------------------------------
137 // Functions
138 //-----------------------------------------------------------------------
139 /*! -----------------------------------------------------------------------
140  * @brief This is the Sensor Event Ready ISR implementation.
141  * @details This function sets the flag which indicates if a new sample(s) is available for reading.
142  * @param[in] pUserData This is a void pointer to the instance of the user specific data structure for the ISR.
143  * @return void There is no return value.
144  * @constraints None
145  * @reeentrant Yes
146  * -----------------------------------------------------------------------*/
147 void fxls8962_int_callback(void *pUserData)
148 { /*! @brief Set flag to indicate Sensor has signaled event ready. */
149  gFxls8962EventReady = true;
150 }
151 
152 /*! -----------------------------------------------------------------------
153  * @brief This is the The main function implementation.
154  * @details This function invokes board initializes routines, then then brings up the sensor and
155  * finally enters an endless loop to continuously read available samples.
156  * @param[in] void This is no input parameter.
157  * @return void There is no return value.
158  * @constraints None
159  * @reeentrant No
160  * -----------------------------------------------------------------------*/
161 int main(void)
162 {
163  int32_t status;
164  bool motionDetect;
165  uint16_t lastReportedSteps;
166  uint8_t intStatus, data[FXLS8962_DATA_SIZE];
167 
168  pedometer_t pedometer; /* This handle holds the current configuration and status value for the pedometer.*/
171 
172  ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER; // Now using the shield.h value!!!
174 
175  /*! Initialize the MCU hardware. */
176  BOARD_InitPins();
179 
180  PRINTF("\r\n ISSDK FXLS8962 sensor driver example for Motion Activated Pedometer.\r\n");
181 
182  /*! Initialize FXLS8962 pin used by FRDM board */
183  pGpioDriver->pin_init(&FXLS8962_INT1, GPIO_DIRECTION_IN, &gpioConfigINT1, &fxls8962_int_callback, NULL);
184 
185  /*! Initialize RGB LED pin used by FRDM board */
186  pGpioDriver->pin_init(&GREEN_LED, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
187 
188  /*! Initialize the I2C driver. */
189  status = I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
190  if (ARM_DRIVER_OK != status)
191  {
192  PRINTF("\r\n I2C Initialization Failed\r\n");
193  return -1;
194  }
195 
196  /*! Set the I2C Power mode. */
197  status = I2Cdrv->PowerControl(ARM_POWER_FULL);
198  if (ARM_DRIVER_OK != status)
199  {
200  PRINTF("\r\n I2C Power Mode setting Failed\r\n");
201  return -1;
202  }
203 
204  /*! Set the I2C bus speed. */
205  status = I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
206  if (ARM_DRIVER_OK != status)
207  {
208  PRINTF("\r\n I2C Control Mode setting Failed\r\n");
209  return -1;
210  }
211 
212  /*! Initialize FXLS8962 sensor driver. */
215  if (SENSOR_ERROR_NONE != status)
216  {
217  PRINTF("\r\n Sensor Initialization Failed\r\n");
218  return -1;
219  }
220 
221  /*! Set the task to be executed while waiting for I2C transactions to complete. */
223 
224  /* Apply FXLS8962 Configuration for Motion Activated Pedometer. */
225  status = Sensor_I2C_Write(fxls8962Driver.pCommDrv, &fxls8962Driver.deviceInfo,
226  fxls8962Driver.slaveAddress, cFxls8962ConfigInitialize);
227  if (SENSOR_ERROR_NONE != status)
228  {
229  PRINTF("\r\n Write FXLS8962 Initialization Failed.\r\n");
230  return -1;
231  }
232 
233  /*! Initialize the pedometer*/
234  pedometer_init(&pedometer);
235  PRINTF("\r\n Pedometer successfully Initialized and Ready for measurements.");
236 
237  for (;;) /* Forever loop for Motion Detection */
238  {
239  /*! Configure the pedometer*/
240  pedometer_configure(&pedometer, &cPedoConfig);
241 
242  /* Apply FXLS8962 Configuration for Motion Detection. */
243  status = FXLS8962_I2C_Configure(&fxls8962Driver, cFxls8962ConfigMotionDetect);
244  if (SENSOR_ERROR_NONE != status)
245  {
246  PRINTF("\r\n Write FXLS8962 Motion Configuration Failed!\r\n");
247  return -1;
248  }
249 
250  motionDetect = true;
251  pGpioDriver->set_pin(&GREEN_LED);
252  PRINTF("\r\n\r\n Waiting for Motion | MCU switching to Sleep Mode ...\r\n");
253 
254  for (;;) /* Loop for Motion Detection */
255  { /* In ISR Mode we do not need to check Data Ready Register.
256  * The receipt of interrupt will indicate event is ready. */
257  if (false == gFxls8962EventReady)
258  { /* Loop, if new sample is not available. */
260  continue;
261  }
262  else
263  { /*! Clear the data ready flag, it will be set again by the ISR. */
264  gFxls8962EventReady = false;
265  }
266 
267  /*! Read the INT Status from the FXLS8962. */
268  status = FXLS8962_I2C_ReadData(&fxls8962Driver, cFxls8962INTStatus, &intStatus);
269  if (ARM_DRIVER_OK != status)
270  {
271  PRINTF("\r\n Read INT Status Failed!\r\n");
272  return -1;
273  }
274 
275  if (motionDetect)
276  {
277  if ((intStatus & FXLS8962_INT_STATUS_SRC_SDCD_OT_MASK) == FXLS8962_INT_STATUS_SRC_SDCD_OT_MASK)
278  {
279  /*! Display that a Motion event has been detected. */
280  PRINTF(" Motion detected...\r\n");
281 
282  /* Apply FXLS8962 Configuration for ASLP Detection with DATA. */
283  status = FXLS8962_I2C_Configure(&fxls8962Driver, cFxls8962ConfigDataReady);
284  if (SENSOR_ERROR_NONE != status)
285  {
286  PRINTF("\r\n Write FXLS8962 DRDY Configuration Failed.\r\n");
287  return -1;
288  }
289  motionDetect = false;
290  pGpioDriver->clr_pin(&GREEN_LED);
291  }
292  }
293  else
294  {
295  /*! Read the Output from the FXLS8962. */
296  status = FXLS8962_I2C_ReadData(&fxls8962Driver, cFxls8962Output, data);
297  if (ARM_DRIVER_OK != status)
298  {
299  PRINTF("\r\n ERROR : Read Data Failed!\r\n");
300  return -1;
301  }
302 
303  /*! Convert the raw sensor data for feeding to pedometer algorithm. */
304  rawData.accel[0] = ((int16_t)data[1] << 8) | data[0];
305  rawData.accel[0] *= 16; /* Pedometer requires signed 12-bit Left justified data @1024 counts/g. */
306  rawData.accel[1] = ((int16_t)data[3] << 8) | data[2];
307  rawData.accel[1] *= 16; /* Pedometer requires signed 12-bit Left justified data @1024 counts/g. */
308  rawData.accel[2] = ((int16_t)data[5] << 8) | data[4];
309  rawData.accel[2] *= 16; /* Pedometer requires signed 12-bit Left justified data @1024 counts/g. */
310 
311  /*! Execute the pedometer Algorithm */
312  pedometer_run(&pedometer, (ped_accel_t *)&rawData.accel);
313 
314  /* Display Steps when there is a change */
315  if (pedometer.status.stepcount && pedometer.status.stepcount != lastReportedSteps)
316  {
317  pGpioDriver->toggle_pin(&GREEN_LED);
318  lastReportedSteps = pedometer.status.stepcount;
319  if(pedometer.status.stepcount == 1)
320  {
321  PRINTF("\r\n | Steps | Distance | Speed | Calories | Activity |\r\n");
322  }
323  PRINTF("\033[K | %5d | %4dm | %2dkmph | %3d | %s |\r",
324  pedometer.status.stepcount, pedometer.status.distance, pedometer.status.speed/1000, pedometer.status.calories, pActivity[pedometer.status.status.bits.activity]);
325  }
326 
327  if ((intStatus & FXLS8962_INT_STATUS_SRC_ASLP_MASK) == FXLS8962_INT_STATUS_SRC_ASLP_MASK)
328  {
329  break;
330  }
331  }
332  }
333  }
334 }
void(* set_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:46
struct pedometer_t::pedometer_status_tag status
#define FXLS8962_INT_EN_DRDY_EN_EN
Definition: fxls8962.h:857
This structure defines the fxls8962 raw data buffer.
Definition: fxls8962_drv.h:53
void pedometer_configure(pedometer_t *pPedometer, const pedometer_config_t *pConfig)
The interface function to configure the pedometer.
Definition: pedometer.c:111
The GPIO Configuration KSDK.
Definition: gpio_driver.h:39
This structure defines the Write command List.
Definition: sensor_drv.h:68
int32_t status
The pedometer.h file contains the interface and structure definitions for pedometer application...
const registerwritelist_t cFxls8962ConfigInitialize[]
FXLS8962 Motion based Pedometer Register Write List.
#define FXLS8962_SENS_CONFIG4_INT_POL_ACT_LOW
Definition: fxls8962.h:678
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 FXLS8962_INT_EN_SDCD_OT_EN_EN
Definition: fxls8962.h:861
gpioConfigKSDK_t gpioConfigINT1
The fxls8962_drv.h file describes the FXLS8962AF driver interface and structures. ...
#define FXLS8962_INT1
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:240
#define FXLS8962_SDCD_CONFIG2_OT_DBCTM_CLEARED
Definition: fxls8962.h:1642
Access structure of the GPIO Driver.
Definition: Driver_GPIO.h:38
#define FXLS8962_SENS_CONFIG3_SLEEP_ODR_0_781HZ
Definition: fxls8962.h:582
pedometer_config_t cPedoConfig
#define SMC
Definition: lpc54114.h:118
This defines the configuration structure of the pedometer.
Definition: pedometer.h:46
#define __END_WRITE_DATA__
Definition: sensor_drv.h:45
debounce_count_t sleepcount_threshold
Definition: pedometer.h:50
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
#define FXLS8962_SENS_CONFIG3_WAKE_ODR_MASK
Definition: fxls8962.h:551
const registerreadlist_t cFxls8962Output[]
Address of Data Output Registers.
This defines the sensor specific information for I2C.
Definition: fxls8962_drv.h:44
gpio_pin_config_t pinConfig
Definition: gpio_driver.h:41
#define FXLS8962_SDCD_CONFIG2_SDCD_EN_EN
Definition: fxls8962.h:1628
#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
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
fxls8962_i2c_sensorhandle_t fxls8962Driver
void(* registeridlefunction_t)(void *userParam)
This is the register idle function type.
Definition: sensor_drv.h:97
GENERIC_DRIVER_GPIO Driver_GPIO_KSDK
Definition: gpio_driver.c:177
GENERIC_DRIVER_GPIO * pGpioDriver
uint8_t data[FXLS8962_DATA_SIZE]
const registerwritelist_t cFxls8962ConfigDataReady[]
FXLS8962 DRDY and ASLP Detect Mode Register Write List.
#define __END_READ_DATA__
Definition: sensor_drv.h:51
const char * pActivity[5]
Pedometer Mode Name Strings.
#define FXLS8962_SENS_CONFIG4_WK_SDCD_OT_MASK
Definition: fxls8962.h:633
#define FXLS8962_SENS_CONFIG4_WK_SDCD_OT_EN
Definition: fxls8962.h:655
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:331
ARM_DRIVER_I2C * I2Cdrv
#define FXLS8962_SENS_CONFIG3_WAKE_ODR_50HZ
Definition: fxls8962.h:563
fxos8700_accelmagdata_t rawData
#define FXLS8962_INT_STATUS_SRC_ASLP_MASK
Definition: fxls8962.h:129
const registerreadlist_t cFxls8962INTStatus[]
Address of INT Status Register.
#define FXLS8962_SENS_CONFIG4_INT_POL_MASK
Definition: fxls8962.h:618
#define FXLS8962_SENS_CONFIG3_SLEEP_ODR_MASK
Definition: fxls8962.h:548
uint16_t readFrom
Definition: sensor_drv.h:80
int main(void)
This is the The main function implementation.
const registerwritelist_t cFxls8962ConfigMotionDetect[]
FXLS8962 Motion Detect Mode Register Write List.
ARM_DRIVER_I2C * pCommDrv
Definition: fxls8962_drv.h:47
void(* toggle_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:48
#define FXLS8962_SDCD_CONFIG1_X_OT_EN_EN
Definition: fxls8962.h:1548
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:286
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
volatile bool gFxls8962EventReady
void(* pin_init)(pinID_t aPinId, gpio_direction_t dir, void *apPinConfig, gpio_isr_handler_t aIsrHandler, void *apUserData)
Definition: Driver_GPIO.h:41
union pedometer_t::pedometer_status_tag::@351 status
#define FXLS8962_SDCD_CONFIG2_WT_DBCTM_CLEARED
Definition: fxls8962.h:1648
This structure defines the Read command List.
Definition: sensor_drv.h:78
gpioHandleKSDK_t GREEN_LED
Definition: frdm_k64f.c:188
#define FXLS8962_INT_STATUS_SRC_SDCD_OT_MASK
Definition: fxls8962.h:138
#define FXLS8962_SDCD_CONFIG1_Z_OT_EN_EN
Definition: fxls8962.h:1556
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
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:278
#define FXLS8962_I2C_ADDR
#define FXLS8962_SDCD_CONFIG2_REF_UPDM_SDCD_REF
Definition: fxls8962.h:1633
void BOARD_InitDebugConsole(void)
Definition: board.c:15
#define FXLS8962_WHOAMI_VALUE
Definition: fxls8962.h:87
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 FXLS8962_INT_EN_ASLP_EN_EN
Definition: fxls8962.h:868
registerDeviceInfo_t deviceInfo
Definition: fxls8962_drv.h:46
void fxls8962_int_callback(void *pUserData)
This is the Sensor Event Ready ISR implementation.
#define FXLS8962_SDCD_CONFIG1_Y_OT_EN_EN
Definition: fxls8962.h:1552