ISSDK  1.8
IoT Sensing Software Development Kit
pedometer_stepcount_fxls896xaf.c
Go to the documentation of this file.
1 /*
2  * Copyright 2021 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 /**
9  * @file pedometer_stepcount_fxls896xaf.c
10  * @brief The pedometer_stepcount_fxls896xaf.c file implements the ISSDK FXLS896XAF
11  * sensor driver example demonstration for Motion Activated Pedometer.
12  */
13 
14 //-----------------------------------------------------------------------
15 // SDK Includes
16 //-----------------------------------------------------------------------
17 #include "pin_mux.h"
18 #include "clock_config.h"
19 #include "board.h"
20 #include "fsl_debug_console.h"
21 
22 //-----------------------------------------------------------------------
23 // CMSIS Includes
24 //-----------------------------------------------------------------------
25 #include "Driver_I2C.h"
26 
27 //-----------------------------------------------------------------------
28 // ISSDK Includes
29 //-----------------------------------------------------------------------
30 #include "issdk_hal.h"
31 #include "pedometer.h"
32 #include "gpio_driver.h"
33 #include "fxls896x_drv.h"
34 
35 //-----------------------------------------------------------------------
36 // Macros
37 //-----------------------------------------------------------------------
38 #define FXLS896x_DATA_SIZE 6 /* 2 byte X,Y,Z Axis Data each. */
39 
40 //-----------------------------------------------------------------------
41 // Constants
42 //-----------------------------------------------------------------------
43 /*! @brief FXLS896x Motion based Pedometer Register Write List. */
45  /* Set Full-scale range as 2G. */
47  /* Set ODR */
50 
51 /*! @brief Address of DATA Ready Status Register. */
53 
54 /*! @brief Address of Data Output Registers. */
56 
57 /*! @brief Pedometer Mode Name Strings. */
58 const char *pActivity[5] = {"Unknown ", "Rest ", "Walking ", "Jogging ", "Running "};
59 
60 //-----------------------------------------------------------------------
61 // Global Variables
62 //-----------------------------------------------------------------------
63 /* Pedometer configuration. These configuration are algorithm and user dependent data. */
64 static const pedometer_config_t cPedoConfig = {
66  .bits = {.config = 1},
67  .keynetik =
68  {
69  .height = 175,
70  .weight = 80,
71  .filtersteps = PEDO_FILTER_STEPS_DEFAULT,
72  .bits =
73  {
74  .filtertime = PEDO_FILTER_TIME_DEFAULT,
75  },
76  .speedperiod = PEDO_SPEED_PERIOD_DEFAULT,
77  .stepthreshold = PEDO_STEP_THRESHOLD_DEFAULT,
78  },
79  .stepcoalesce = 1,
80  .oneG = PEDO_ONEG_2G, // It is the One G representation in 2G scale.
81  .frequency = PEDO_FREQHZ_DEFAULT, // It is 50 HZ
82 
83 };
84 
86 {
87  .pinConfig = {kGPIO_DigitalInput, 0},
88  .portPinConfig = {.pullSelect = kPORT_PullUp, .mux = kPORT_MuxAsGpio},
89  .interruptMode = kPORT_InterruptFallingEdge,
90 };
91 
92 //-----------------------------------------------------------------------
93 // Functions
94 //-----------------------------------------------------------------------
95 
96 /*! -----------------------------------------------------------------------
97  * @brief This is the The main function implementation.
98  * @details This function invokes board initializes routines, then then brings up the sensor and
99  * finally enters an endless loop to continuously read available samples.
100  * @param[in] void This is no input parameter.
101  * @return void There is no return value.
102  * -----------------------------------------------------------------------*/
103 int main(void)
104 {
105  int32_t status;
106  uint8_t whoami;
107  uint8_t dataReady;
108  uint16_t lastReportedSteps;
109  uint8_t data[FXLS896x_DATA_SIZE];
110 
111  pedometer_t pedometer; /* This handle holds the current configuration and status value for the pedometer.*/
114 
115  ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER; // Now using the shield.h value!!!
117 
118  /*! Initialize the MCU hardware. */
119  BOARD_InitPins();
122 
123  PRINTF("\r\n ISSDK FXLS896x sensor driver example for Pedometer.\r\n");
124 
125  /*! Initialize RGB LED pin used by FRDM board */
126  pGpioDriver->pin_init(&GREEN_LED, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
127 
128  /*! Initialize the I2C driver. */
129  status = I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
130  if (ARM_DRIVER_OK != status)
131  {
132  PRINTF("\r\n I2C Initialization Failed\r\n");
133  return -1;
134  }
135 
136  /*! Set the I2C Power mode. */
137  status = I2Cdrv->PowerControl(ARM_POWER_FULL);
138  if (ARM_DRIVER_OK != status)
139  {
140  PRINTF("\r\n I2C Power Mode setting Failed\r\n");
141  return -1;
142  }
143 
144  /*! Set the I2C bus speed. */
145  status = I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
146  if (ARM_DRIVER_OK != status)
147  {
148  PRINTF("\r\n I2C Control Mode setting Failed\r\n");
149  return -1;
150  }
151 
152  /*! Initialize FXLS896x sensor driver. */
154  &whoami);
155  if (ARM_DRIVER_OK != status)
156  {
157  PRINTF("\r\n Sensor Initialization Failed\r\n");
158  return -1;
159  }
160  if ((FXLS8964_WHOAMI_VALUE == whoami) || (FXLS8967_WHOAMI_VALUE == whoami))
161  {
162  PRINTF("\r\n Successfully Initialized Gemini with WHO_AM_I = 0x%X\r\n", whoami);
163  }
164  else if ((FXLS8974_WHOAMI_VALUE == whoami) || (FXLS8968_WHOAMI_VALUE == whoami))
165  {
166  PRINTF("\r\n Successfully Initialized Timandra with WHO_AM_I = 0x%X\r\n", whoami);
167  }
168  else if (FXLS8962_WHOAMI_VALUE == whoami)
169  {
170  PRINTF("\r\n Successfully Initialized Newstein with WHO_AM_I = 0x%X\r\n", whoami);
171  }
172  else
173  {
174  PRINTF("\r\n Bad WHO_AM_I = 0x%X\r\n", whoami);
175  return -1;
176  }
177 
178  /*! Set the task to be executed while waiting for I2C transactions to complete. */
180 
181  /* Apply FXLS896x Configuration for Motion Activated Pedometer. */
182  /*! Configure the FXLS896x sensor. */
183  status = FXLS896x_I2C_Configure(&fxls896xDriver, cFxls896xConfigInitialize);
184  if (SENSOR_ERROR_NONE != status)
185  {
186  PRINTF("\r\n FXLS896x Sensor Configuration Failed, Err = %d\r\n", status);
187  return -1;
188  }
189  PRINTF("\r\n Successfully Applied FXLS896x Sensor Configuration\r\n");
190 
191  /*! Initialize the pedometer*/
192  pedometer_init(&pedometer);
193  PRINTF("\r\n Pedometer successfully Initialized.");
194  PRINTF("\r\n\r\n Ready for measurements.");
195 
196  for (;;) /* Forever loop for Motion Detection */
197  {
198  /*! Configure the pedometer*/
199  pedometer_configure(&pedometer, &cPedoConfig);
200 
201  pGpioDriver->set_pin(&GREEN_LED);
202 
203  for (;;) /* Loop for Motion Detection */
204  { /* In ISR Mode we do not need to check Data Ready Register.
205  * The receipt of interrupt will indicate event is ready. */
206  /*! Wait for data ready from the FXLS896x. */
207  status = FXLS896x_I2C_ReadData(&fxls896xDriver, cFxls896xDRDYEvent, &dataReady);
208  if (0 == (dataReady & FXLS896x_INT_STATUS_SRC_DRDY_MASK))
209  {
210  continue;
211  }
212 
213  /*! Read the Output from the FXLS896x. */
214  status = FXLS896x_I2C_ReadData(&fxls896xDriver, cFxls896xOutput, data);
215  if (ARM_DRIVER_OK != status)
216  {
217  PRINTF("\r\n ERROR : Read Data Failed!\r\n");
218  return -1;
219  }
220 
221  /*! Convert the raw sensor data for feeding to pedometer algorithm. */
222  rawData.accel[0] = ((int16_t)data[1] << 8) | data[0];
223  rawData.accel[0] *= 16; /* Pedometer requires signed 12-bit Left justified data @1024 counts/g. */
224  rawData.accel[1] = ((int16_t)data[3] << 8) | data[2];
225  rawData.accel[1] *= 16; /* Pedometer requires signed 12-bit Left justified data @1024 counts/g. */
226  rawData.accel[2] = ((int16_t)data[5] << 8) | data[4];
227  rawData.accel[2] *= 16; /* Pedometer requires signed 12-bit Left justified data @1024 counts/g. */
228 
229  /*! Execute the pedometer Algorithm */
230  pedometer_run(&pedometer, (ped_accel_t *)&rawData.accel);
231 
232  /* Display Steps when there is a change */
233  if (pedometer.status.stepcount && pedometer.status.stepcount != lastReportedSteps)
234  {
235  pGpioDriver->toggle_pin(&GREEN_LED);
236  lastReportedSteps = pedometer.status.stepcount;
237  if(pedometer.status.stepcount == 4)
238  {
239  PRINTF("\r\n\r\n | Steps |\r\n");
240  }
241  PRINTF("\033[K | %5d |\r",pedometer.status.stepcount);
242  }
243  }
244  }
245 }
void(* set_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:46
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
The fxls896x_drv.h file describes the FXLS8964/67AF driver interface and structures.
The GPIO Configuration KSDK.
Definition: gpio_driver.h:39
This structure defines the Write command List.
Definition: sensor_drv.h:68
#define FXLS896x_SENS_CONFIG3_WAKE_ODR_50HZ
Definition: fxls896x.h:563
int32_t status
The pedometer.h file contains the interface and structure definitions for pedometer application...
#define FXLS896x_I2C_ADDR
int main(void)
This is the The main function implementation.
#define PEDO_FILTER_STEPS_DEFAULT
Definition: pedometer.h:35
int32_t FXLS896x_I2C_Initialize(fxls896x_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: fxls896x_drv.c:239
Access structure of the GPIO Driver.
Definition: Driver_GPIO.h:38
#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
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
const registerreadlist_t cFxls896xOutput[]
Address of Data Output Registers.
#define PEDO_FREQHZ_DEFAULT
Definition: pedometer.h:31
#define FXLS8967_WHOAMI_VALUE
Definition: fxls8962.h:89
This structure defines the fxls896x raw data buffer.
Definition: fxls896x_drv.h:52
gpio_pin_config_t pinConfig
Definition: gpio_driver.h:41
#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
const registerwritelist_t cFxls896xConfigInitialize[]
FXLS896x Motion based Pedometer Register Write List.
const char * pActivity[5]
Pedometer Mode Name Strings.
This defines the pedometer instance.
Definition: pedometer.h:73
gpioConfigKSDK_t gpioConfigINT1
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
#define FXLS896x_SENS_CONFIG1_FSR_MASK
Definition: fxls896x.h:423
GENERIC_DRIVER_GPIO Driver_GPIO_KSDK
Definition: gpio_driver.c:177
GENERIC_DRIVER_GPIO * pGpioDriver
This defines the sensor specific information for I2C.
Definition: fxls896x_drv.h:43
uint8_t data[FXLS8962_DATA_SIZE]
int32_t FXLS896x_I2C_Configure(fxls896x_i2c_sensorhandle_t *pSensorHandle, const registerwritelist_t *pRegWriteList)
The interface function to configure he sensor.
Definition: fxls896x_drv.c:285
#define __END_READ_DATA__
Definition: sensor_drv.h:51
int32_t FXLS896x_I2C_ReadData(fxls896x_i2c_sensorhandle_t *pSensorHandle, const registerreadlist_t *pReadList, uint8_t *pBuffer)
The interface function to read the sensor data.
Definition: fxls896x_drv.c:330
ARM_DRIVER_I2C * I2Cdrv
fxos8700_accelmagdata_t rawData
uint16_t readFrom
Definition: sensor_drv.h:80
#define FXLS896x_DATA_SIZE
fxls896x_i2c_sensorhandle_t fxls896xDriver
void FXLS896x_I2C_SetIdleTask(fxls896x_i2c_sensorhandle_t *pSensorHandle, registeridlefunction_t idleTask, void *userParam)
: The interface function to set the I2C Idle Task.
Definition: fxls896x_drv.c:277
void(* toggle_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:48
#define PEDO_SPEED_PERIOD_DEFAULT
Definition: pedometer.h:33
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
void(* pin_init)(pinID_t aPinId, gpio_direction_t dir, void *apPinConfig, gpio_isr_handler_t aIsrHandler, void *apUserData)
Definition: Driver_GPIO.h:41
This structure defines the Read command List.
Definition: sensor_drv.h:78
const registerreadlist_t cFxls896xDRDYEvent[]
Address of DATA Ready Status Register.
gpioHandleKSDK_t GREEN_LED
Definition: frdm_k64f.c:188
#define FXLS8974_WHOAMI_VALUE
Definition: fxls8962.h:90
int32_t pedometer_run(pedometer_t *pPedometer, ped_accel_t *pData)
The interface function excutes the pedometer algorithm.
Definition: pedometer.c:125
#define FXLS8968_WHOAMI_VALUE
Definition: fxls896x.h:89
#define I2C_S_DEVICE_INDEX
Definition: issdk_hal.h:35
#define PEDO_STEP_THRESHOLD_DEFAULT
Definition: pedometer.h:32
void BOARD_InitDebugConsole(void)
Definition: board.c:15
#define FXLS8962_WHOAMI_VALUE
Definition: fxls8962.h:87
#define FXLS896x_INT_STATUS_SRC_DRDY_MASK
Definition: fxls896x.h:147
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 FXLS896x_SENS_CONFIG3_WAKE_ODR_MASK
Definition: fxls896x.h:551
#define FXLS8964_WHOAMI_VALUE
Definition: fxls8962.h:88
#define FXLS896x_SENS_CONFIG1_FSR_2G
Definition: fxls896x.h:453