ISSDK  1.7
IoT Sensing Software Development Kit
pedometer_stepcount_fxls8962.c
Go to the documentation of this file.
1 /*
2  * The Clear BSD License
3  * Copyright 2016-2017 NXP
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without modification,
7  * are permitted (subject to the limitations in the disclaimer below) provided
8  * that the following conditions are met:
9  *
10  * o Redistributions of source code must retain the above copyright notice, this list
11  * of conditions and the following disclaimer.
12  *
13  * o Redistributions in binary form must reproduce the above copyright notice, this
14  * list of conditions and the following disclaimer in the documentation and/or
15  * other materials provided with the distribution.
16  *
17  * o Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived from this
19  * software without specific prior written permission.
20  *
21  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
26  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
29  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /**
35  * @file pedometer_stepcount_fxls8962.c
36  * @brief The pedometer_stepcount_fxls8962.c file implements the ISSDK FXLS8962 sensor
37  * driver example demonstration for Motion Activated Pedometer.
38  */
39 
40 //-----------------------------------------------------------------------
41 // SDK Includes
42 //-----------------------------------------------------------------------
43 #include "board.h"
44 #include "pin_mux.h"
45 #include "clock_config.h"
46 #include "fsl_debug_console.h"
47 
48 //-----------------------------------------------------------------------
49 // CMSIS Includes
50 //-----------------------------------------------------------------------
51 #include "Driver_I2C.h"
52 
53 //-----------------------------------------------------------------------
54 // ISSDK Includes
55 //-----------------------------------------------------------------------
56 #include "issdk_hal.h"
57 #include "pedometer.h"
58 #include "gpio_driver.h"
59 #include "fxls8962_drv.h"
60 
61 //-----------------------------------------------------------------------
62 // Macros
63 //-----------------------------------------------------------------------
64 #define SDCD_LTHS_LSB 0xA0 /* Lower Threshold LSB value. */
65 #define SDCD_LTHS_MSB 0x0F /* Lower Threshold MSB value. */
66 #define SDCD_UTHS_LSB 0x40 /* Upper Threshold LSB value. */
67 #define SDCD_UTHS_MSB 0x00 /* Upper Threshold MSB value. */
68 #define ASLP_COUNT_LSB 0xFA /* Auto Sleep after 5s @50Hz Wake ODR */
69 #define ASLP_COUNT_MSB 0x00 /* Auto Sleep after 5s @50Hz Wake ODR */
70 #define FXLS8962_DATA_SIZE 6 /* 2 byte X,Y,Z Axis Data each. */
71 
72 //-----------------------------------------------------------------------
73 // Constants
74 //-----------------------------------------------------------------------
75 /*! @brief FXLS8962 Motion based Pedometer Register Write List. */
77  /* Enable SDCD Events */
80  /* Enable SDCD Block */
83  /* Set WAKE and Sleep ODRs */
86  /* Wake Sleep SDCD OT Event Enabled */
89  /* ASLP and Data INT enabled */
91  /* Auto Sleep LSB value */
93  /* Auto Sleep MSB value */
95  /* Set SDCD Lower Threshold LSB value. */
97  /* Set SDCD Lower Threshold MSB value. */
99  /* Set SDCD Upper Threshold LSB value. */
101  /* Set SDCD Upper Threshold MSB value. */
104 
105 /*! @brief FXLS8962 Motion Detect Mode Register Write List. */
107  /* Only SDCD INT Enabled */
110 
111 /*! @brief FXLS8962 DRDY and ASLP Detect Mode Register Write List. */
113  /* Data and ASLP INT Enabled */
116 
117 /*! @brief Address of INT Status Register. */
119 
120 /*! @brief Address of Data Output Registers. */
122 
123 /*! @brief Pedometer Mode Name Strings. */
124 const char *pActivity[5] = {"Unknown ", "Rest ", "Walking ", "Jogging ", "Running "};
125 
126 //-----------------------------------------------------------------------
127 // Global Variables
128 //-----------------------------------------------------------------------
129 volatile bool gFxls8962EventReady = false;
130 
131 /* Pedometer configuration. These configuration are algorithm and user dependent data. */
133 {
135  .bits = {.config = 1},
136  .keynetik =
137  {
138  .steplength = 0,
139  .height = 175,
140  .weight = 80,
141  .filtersteps = 1,
142  .bits =
143  {
144  .filtertime = 2, .male = 1,
145  },
146  .speedperiod = 3,
147  .stepthreshold = 0,
148  },
149  .stepcoalesce = 1,
150  .oneG = PEDO_ONEG_2G,
151  .frequency = 50,
152 };
153 
155 {
156  .pinConfig = {kGPIO_DigitalInput, 0},
157  .portPinConfig = {.pullSelect = kPORT_PullUp, .mux = kPORT_MuxAsGpio},
158  .interruptMode = kPORT_InterruptFallingEdge,
159 };
160 
161 //-----------------------------------------------------------------------
162 // Functions
163 //-----------------------------------------------------------------------
164 /*! -----------------------------------------------------------------------
165  * @brief This is the Sensor Event Ready ISR implementation.
166  * @details This function sets the flag which indicates if a new sample(s) is available for reading.
167  * @param[in] pUserData This is a void pointer to the instance of the user specific data structure for the ISR.
168  * @return void There is no return value.
169  * @constraints None
170  * @reeentrant Yes
171  * -----------------------------------------------------------------------*/
172 void fxls8962_int_callback(void *pUserData)
173 { /*! @brief Set flag to indicate Sensor has signaled event ready. */
174  gFxls8962EventReady = true;
175 }
176 
177 /*! -----------------------------------------------------------------------
178  * @brief This is the The main function implementation.
179  * @details This function invokes board initializes routines, then then brings up the sensor and
180  * finally enters an endless loop to continuously read available samples.
181  * @param[in] void This is no input parameter.
182  * @return void There is no return value.
183  * @constraints None
184  * @reeentrant No
185  * -----------------------------------------------------------------------*/
186 int main(void)
187 {
188  int32_t status;
189  bool motionDetect;
190  uint16_t lastReportedSteps;
191  uint8_t intStatus, data[FXLS8962_DATA_SIZE];
192 
193  pedometer_t pedometer; /* This handle holds the current configuration and status value for the pedometer.*/
196 
197  ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER; // Now using the shield.h value!!!
199 
200  /*! Initialize the MCU hardware. */
201  BOARD_InitPins();
204 
205  PRINTF("\r\n ISSDK FXLS8962 sensor driver example for Motion Activated Pedometer.\r\n");
206 
207  /*! Initialize FXLS8962 pin used by FRDM board */
208  pGpioDriver->pin_init(&FXLS8962_INT1, GPIO_DIRECTION_IN, &gpioConfigINT1, &fxls8962_int_callback, NULL);
209 
210  /*! Initialize RGB LED pin used by FRDM board */
211  pGpioDriver->pin_init(&GREEN_LED, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
212 
213  /*! Initialize the I2C driver. */
214  status = I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
215  if (ARM_DRIVER_OK != status)
216  {
217  PRINTF("\r\n I2C Initialization Failed\r\n");
218  return -1;
219  }
220 
221  /*! Set the I2C Power mode. */
222  status = I2Cdrv->PowerControl(ARM_POWER_FULL);
223  if (ARM_DRIVER_OK != status)
224  {
225  PRINTF("\r\n I2C Power Mode setting Failed\r\n");
226  return -1;
227  }
228 
229  /*! Set the I2C bus speed. */
230  status = I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
231  if (ARM_DRIVER_OK != status)
232  {
233  PRINTF("\r\n I2C Control Mode setting Failed\r\n");
234  return -1;
235  }
236 
237  /*! Initialize FXLS8962 sensor driver. */
240  if (SENSOR_ERROR_NONE != status)
241  {
242  PRINTF("\r\n Sensor Initialization Failed\r\n");
243  return -1;
244  }
245 
246  /*! Set the task to be executed while waiting for I2C transactions to complete. */
248 
249  /* Apply FXLS8962 Configuration for Motion Activated Pedometer. */
250  status = Sensor_I2C_Write(fxls8962Driver.pCommDrv, &fxls8962Driver.deviceInfo,
251  fxls8962Driver.slaveAddress, cFxls8962ConfigInitialize);
252  if (SENSOR_ERROR_NONE != status)
253  {
254  PRINTF("\r\n Write FXLS8962 Initialization Failed.\r\n");
255  return -1;
256  }
257 
258  /*! Initialize the pedometer*/
259  pedometer_init(&pedometer);
260  PRINTF("\r\n Pedometer successfully Initialized and Ready for measurements.");
261 
262  for (;;) /* Forever loop for Motion Detection */
263  {
264  /*! Configure the pedometer*/
265  pedometer_configure(&pedometer, &cPedoConfig);
266 
267  /* Apply FXLS8962 Configuration for Motion Detection. */
268  status = FXLS8962_I2C_Configure(&fxls8962Driver, cFxls8962ConfigMotionDetect);
269  if (SENSOR_ERROR_NONE != status)
270  {
271  PRINTF("\r\n Write FXLS8962 Motion Configuration Failed!\r\n");
272  return -1;
273  }
274 
275  motionDetect = true;
276  pGpioDriver->set_pin(&GREEN_LED);
277  PRINTF("\r\n\r\n Waiting for Motion | MCU switching to Sleep Mode ...\r\n");
278 
279  for (;;) /* Loop for Motion Detection */
280  { /* In ISR Mode we do not need to check Data Ready Register.
281  * The receipt of interrupt will indicate event is ready. */
282  if (false == gFxls8962EventReady)
283  { /* Loop, if new sample is not available. */
285  continue;
286  }
287  else
288  { /*! Clear the data ready flag, it will be set again by the ISR. */
289  gFxls8962EventReady = false;
290  }
291 
292  /*! Read the INT Status from the FXLS8962. */
293  status = FXLS8962_I2C_ReadData(&fxls8962Driver, cFxls8962INTStatus, &intStatus);
294  if (ARM_DRIVER_OK != status)
295  {
296  PRINTF("\r\n Read INT Status Failed!\r\n");
297  return -1;
298  }
299 
300  if (motionDetect)
301  {
302  if ((intStatus & FXLS8962_INT_STATUS_SRC_SDCD_OT_MASK) == FXLS8962_INT_STATUS_SRC_SDCD_OT_MASK)
303  {
304  /*! Display that a Motion event has been detected. */
305  PRINTF(" Motion detected...\r\n");
306 
307  /* Apply FXLS8962 Configuration for ASLP Detection with DATA. */
308  status = FXLS8962_I2C_Configure(&fxls8962Driver, cFxls8962ConfigDataReady);
309  if (SENSOR_ERROR_NONE != status)
310  {
311  PRINTF("\r\n Write FXLS8962 DRDY Configuration Failed.\r\n");
312  return -1;
313  }
314  motionDetect = false;
315  pGpioDriver->clr_pin(&GREEN_LED);
316  }
317  }
318  else
319  {
320  /*! Read the Output from the FXLS8962. */
321  status = FXLS8962_I2C_ReadData(&fxls8962Driver, cFxls8962Output, data);
322  if (ARM_DRIVER_OK != status)
323  {
324  PRINTF("\r\n ERROR : Read Data Failed!\r\n");
325  return -1;
326  }
327 
328  /*! Convert the raw sensor data for feeding to pedometer algorithm. */
329  rawData.accel[0] = ((int16_t)data[1] << 8) | data[0];
330  rawData.accel[0] *= 16; /* Pedometer requires signed 12-bit Left justified data @1024 counts/g. */
331  rawData.accel[1] = ((int16_t)data[3] << 8) | data[2];
332  rawData.accel[1] *= 16; /* Pedometer requires signed 12-bit Left justified data @1024 counts/g. */
333  rawData.accel[2] = ((int16_t)data[5] << 8) | data[4];
334  rawData.accel[2] *= 16; /* Pedometer requires signed 12-bit Left justified data @1024 counts/g. */
335 
336  /*! Execute the pedometer Algorithm */
337  pedometer_run(&pedometer, (ped_accel_t *)&rawData.accel);
338 
339  /* Display Steps when there is a change */
340  if (pedometer.status.stepcount && pedometer.status.stepcount != lastReportedSteps)
341  {
342  pGpioDriver->toggle_pin(&GREEN_LED);
343  lastReportedSteps = pedometer.status.stepcount;
344  if(pedometer.status.stepcount == 1)
345  {
346  PRINTF("\r\n | Steps | Distance | Speed | Calories | Activity |\r\n");
347  }
348  PRINTF("\033[K | %5d | %4dm | %2dkmph | %3d | %s |\r",
349  pedometer.status.stepcount, pedometer.status.distance, pedometer.status.speed/1000, pedometer.status.calories, pActivity[pedometer.status.status.bits.activity]);
350  }
351 
352  if ((intStatus & FXLS8962_INT_STATUS_SRC_ASLP_MASK) == FXLS8962_INT_STATUS_SRC_ASLP_MASK)
353  {
354  break;
355  }
356  }
357  }
358  }
359 }
const registerwritelist_t cFxls8962ConfigInitialize[]
FXLS8962 Motion based Pedometer Register Write List.
void fxls8962_int_callback(void *pUserData)
This is the Sensor Event Ready ISR implementation.
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 ASLP_COUNT_MSB
#define __END_WRITE_DATA__
Definition: sensor_drv.h:71
const registerreadlist_t cFxls8962INTStatus[]
Address of INT Status Register.
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
const char * pActivity[5]
Pedometer Mode Name Strings.
#define BOARD_BootClockRUN
Definition: clock_config.h:45
const registerwritelist_t cFxls8962ConfigDataReady[]
FXLS8962 DRDY and ASLP Detect Mode Register Write List.
#define SDCD_UTHS_LSB
#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
const registerreadlist_t cFxls8962Output[]
Address of Data Output Registers.
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
#define SDCD_UTHS_MSB
volatile bool gFxls8962EventReady
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
#define __END_READ_DATA__
Definition: sensor_drv.h:77
This defines the pedometer instance.
Definition: pedometer.h:99
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
#define FXLS8962_I2C_ADDR
pedometer_config_t cPedoConfig
#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
#define SDCD_LTHS_MSB
union pedometer_t::pedometer_status_tag::@292 status
gpio_pin_config_t pinConfig
Definition: gpio_driver.h:67
const registerwritelist_t cFxls8962ConfigMotionDetect[]
FXLS8962 Motion Detect Mode Register Write List.
#define ASLP_COUNT_LSB
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
#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
#define FXLS8962_INT_STATUS_SRC_ASLP_MASK
Definition: fxls8962.h:152
#define I2C_S_DRIVER
Definition: issdk_hal.h:59
gpioConfigKSDK_t gpioConfigINT1
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.
#define FXLS8962_WHOAMI_VALUE
Definition: fxls8962.h:113
#define FXLS8962_DATA_SIZE
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 SDCD_LTHS_LSB
int main(void)
This is the The main function implementation.
#define FXLS8962_INT_STATUS_SRC_SDCD_OT_MASK
Definition: fxls8962.h:161
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
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