ISSDK  1.8
IoT Sensing Software Development Kit
fxas21002_fifo.c
Go to the documentation of this file.
1 /*
2  * Copyright 2018 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 /**
9  * @file fxas21002_fifo.c
10  * @brief The fxas21002_fifo.c file implements the ISSDK FXAS21002 sensor driver
11  * example demonstration with FIFO mode.
12  */
13 
14 /* SDK Includes */
15 #include "pin_mux.h"
16 #include "clock_config.h"
17 #include "board.h"
18 #include "fsl_debug_console.h"
19 #include "fsl_irqsteer.h"
20 
21 /* CMSIS Includes */
22 #include "Driver_I2C.h"
23 
24 /* ISSDK Includes */
25 #include "issdk_hal.h"
26 #include "fxas21002_drv.h"
27 
28 /*******************************************************************************
29  * Macro Definitions
30  ******************************************************************************/
31 /*---------------------------------------------------------------------------*/
32 /*! @def FIFO_SIZE
33  * @brief The watermark value configured for FXAS21002 FIFO Buffer.
34  */
35 #define FIFO_SIZE 4
36 
37 /*******************************************************************************
38  * Constants
39  ******************************************************************************/
40 /*! Prepare the register write list to configure FXAS21002 in FIFO mode. */
42  /*! Configure CTRL_REG1 register to put FXAS21002 to 12.5Hz sampling rate. */
44  /*! Configure F_SETUP register to set FIFO in Stop mode, Watermark of FIFO_SIZE. */
49 
50 /*! Prepare the register read list to read FXAS21002 FIFO event. */
52 
53 /*! Prepare the register read list to read the raw gyro data from the FXAS21002. */
56 
57 /*******************************************************************************
58  * Code
59  ******************************************************************************/
60 /*!
61  * @brief Main function
62  */
63 int main(void)
64 {
66  uint8_t fifoEvent;
69  sc_ipc_t ipc;
70  uint32_t freq;
71 
72  ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER;
73  fxas21002_i2c_sensorhandle_t FXAS21002drv;
74 
75  /*! Initialize the MCU hardware. */
76  ipc = BOARD_InitRpc();
77  BOARD_InitMemory();
80 
82 
83  PRINTF("\r\n ------------------------------------------------------------------\r\n");
84  PRINTF("\r\n ISSDK FXAS21002 sensor driver example demonstration with FIFO mode\r\n");
85  PRINTF("\r\n ------------------------------------------------------------------\r\n");
86 
87  /* Power on LPI2C. */
88  if (sc_pm_set_resource_power_mode(ipc, SC_R_I2C_0, SC_PM_PW_MODE_ON) != SC_ERR_NONE)
89  {
90  PRINTF("Error: Failed to power on LPI2C\r\n");
91  }
92 
93  /* Set LPI2C clock */
94  freq = CLOCK_SetIpFreq(kCLOCK_DMA_Lpi2c0, SC_24MHZ);
95  if (freq == 0)
96  {
97  PRINTF("Error: Failed to set LPI2C frequency\r\n");
98  }
99 
100  /* Power on IRQSTEER. */
101 #if (MIMX8QM_CM4_CORE0)
102  if (sc_pm_set_resource_power_mode(ipc, SC_R_IRQSTR_M4_0, SC_PM_PW_MODE_ON) != SC_ERR_NONE)
103  {
104  PRINTF("Error: Failed to power on IRQSTEER M4_0\r\n");
105  }
106 #elif (MIMX8QM_CM4_CORE1)
107  if (sc_pm_set_resource_power_mode(ipc, SC_R_IRQSTR_M4_1, SC_PM_PW_MODE_ON) != SC_ERR_NONE)
108  {
109  PRINTF("Error: Failed to power on IRQSTEER M4_1\r\n");
110  }
111 #endif
112 
113  /* Enable interrupt in irqsteer */
114  IRQSTEER_Init(IRQSTEER);
115  IRQSTEER_EnableInterrupt(IRQSTEER, DMA_I2C0_INT_IRQn);
116 
117  /*! Initialize the I2C driver. */
118  status = I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
119  if (ARM_DRIVER_OK != status)
120  {
121  PRINTF("\r\n I2C Initialization Failed\r\n");
122  return -1;
123  }
124 
125  /*! Set the I2C Power mode. */
126  status = I2Cdrv->PowerControl(ARM_POWER_FULL);
127  if (ARM_DRIVER_OK != status)
128  {
129  PRINTF("\r\n I2C Power Mode setting Failed\r\n");
130  return -1;
131  }
132 
133  /*! Set the I2C bus speed. */
134  status = I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
135  if (ARM_DRIVER_OK != status)
136  {
137  PRINTF("\r\n I2C Control Mode setting Failed\r\n");
138  return -1;
139  }
140 
141  /*! Initialize the FXAS21002 sensor driver. */
144  if (SENSOR_ERROR_NONE != status)
145  {
146  PRINTF("\r\n Sensor Initialization Failed\r\n");
147  return -1;
148  }
149  PRINTF("\r\n Successfully Initiliazed FXAS21002 Sensor\r\n");
150  PRINTF("\r\n Device Address is 0x%x and WHOAMI Value is 0x%x\r\n", FXAS21002_I2C_ADDR, FXAS21002_WHO_AM_I_WHOAMI_PROD_VALUE);
151 
152  /*! Configure the FXAS21002 sensor driver. */
153  status = FXAS21002_I2C_Configure(&FXAS21002drv, fxas21002_Config_with_Fifo);
154  if (SENSOR_ERROR_NONE != status)
155  {
156  PRINTF("\r\n FXAS21002 Sensor Configuration Failed, Err = %d\r\n", status);
157  return -1;
158  }
159  PRINTF("\r\n Successfully Applied FXAS21002 Sensor Configuration\r\n");
160 
161  PRINTF("\r\n Reading FXAS21002 Gyro Values in FIFO mode, FIFO width=%d\r\n", FIFO_SIZE);
162 
163  for (;;) /* Forever loop */
164  {
165  /*! Wait for the FIFO watermark event. */
166  status = FXAS21002_I2C_ReadData(&FXAS21002drv, fxas21002_Fifo_Event, &fifoEvent);
167  if (0 == (fifoEvent & FXAS21002_F_EVENT_F_EVENT_MASK))
168  {
169  continue;
170  }
171 
172  /*! Read the raw sensor data from the FXAS21002. */
173  status = FXAS21002_I2C_ReadData(&FXAS21002drv, fxas21002_Output_Values, data);
174  if (ARM_DRIVER_OK != status)
175  {
176  PRINTF("\r\n Read Failed. \r\n");
177  return -1;
178  }
179 
180  for (uint8_t i = 0; i < FIFO_SIZE; i++)
181  {
182  /*! Convert the raw sensor data to signed 16-bit container for display to the debug port. */
183  rawData.gyro[0] =
184  ((int16_t)data[i * FXAS21002_GYRO_DATA_SIZE + 0] << 8) | data[i * FXAS21002_GYRO_DATA_SIZE + 1];
185  rawData.gyro[1] =
186  ((int16_t)data[i * FXAS21002_GYRO_DATA_SIZE + 2] << 8) | data[i * FXAS21002_GYRO_DATA_SIZE + 3];
187  rawData.gyro[2] =
188  ((int16_t)data[i * FXAS21002_GYRO_DATA_SIZE + 4] << 8) | data[i * FXAS21002_GYRO_DATA_SIZE + 5];
189  }
190  /* NOTE: PRINTF is relatively expensive in terms of CPU time, specially when used with-in execution loop. */
191  PRINTF("\r\n Gyro X = %d Y = %d Z = %d\r\n", rawData.gyro[0], rawData.gyro[1], rawData.gyro[2]);
192  ASK_USER_TO_RESUME(100 / FIFO_SIZE); /* Ask for user input after processing 100 samples. */
193  }
194 }
#define FXAS21002_WHO_AM_I_WHOAMI_PROD_VALUE
Definition: fxas21002.h:402
const registerwritelist_t fxas21002_Config_with_Fifo[]
This structure defines the Write command List.
Definition: sensor_drv.h:68
#define FXAS21002_F_EVENT_F_EVENT_MASK
Definition: fxas21002.h:311
#define ASK_USER_TO_RESUME(x)
Definition: frdm_k64f.h:106
int32_t status
int32_t FXAS21002_I2C_Initialize(fxas21002_i2c_sensorhandle_t *pSensorHandle, ARM_DRIVER_I2C *pBus, uint8_t index, uint16_t sAddress, uint8_t whoAmi)
The interface function to initialize the sensor.
This structure defines the fxas21002 raw data buffer.
Definition: fxas21002_drv.h:53
#define FXAS21002_F_SETUP_F_MODE_STOP_MODE
Definition: fxas21002.h:282
#define FXAS21002_CTRL_REG3_WRAPTOONE_ROLL_DATA
Definition: fxas21002.h:848
#define __END_WRITE_DATA__
Definition: sensor_drv.h:45
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
int main(void)
Main function.
#define I2C_S_SIGNAL_EVENT
Definition: issdk_hal.h:34
int32_t FXAS21002_I2C_Configure(fxas21002_i2c_sensorhandle_t *pSensorHandle, const registerwritelist_t *pRegWriteList)
The interface function to configure he sensor.
#define I2C_S_DRIVER
Definition: issdk_hal.h:33
This defines the sensor specific information for I2C.
Definition: fxas21002_drv.h:44
#define BOARD_BootClockRUN
Definition: clock_config.h:19
#define FXAS21002_CTRL_REG3_WRAPTOONE_MASK
Definition: fxas21002.h:829
#define FXAS21002_I2C_ADDR
uint8_t data[FXLS8962_DATA_SIZE]
#define __END_READ_DATA__
Definition: sensor_drv.h:51
#define FXAS21002_F_SETUP_F_WMRK_SHIFT
Definition: fxas21002.h:271
The fxas21002_drv.h file describes the fxas21002 driver interface and structures. ...
int32_t FXAS21002_I2C_ReadData(fxas21002_i2c_sensorhandle_t *pSensorHandle, const registerreadlist_t *pReadList, uint8_t *pBuffer)
The interface function to read the sensor data.
ARM_DRIVER_I2C * I2Cdrv
fxos8700_accelmagdata_t rawData
const registerreadlist_t fxas21002_Output_Values[]
uint16_t readFrom
Definition: sensor_drv.h:80
#define FXAS21002_F_SETUP_F_WMRK_MASK
Definition: fxas21002.h:270
#define FXAS21002_GYRO_DATA_SIZE
The size of the FXAS21002 gyro data.
Definition: fxas21002_drv.h:61
#define FXAS21002_CTRL_REG1_DR_MASK
Definition: fxas21002.h:684
#define FIFO_SIZE
The watermark value configured for FXAS21002 FIFO Buffer.
This structure defines the Read command List.
Definition: sensor_drv.h:78
#define I2C_S_DEVICE_INDEX
Definition: issdk_hal.h:35
#define FXAS21002_CTRL_REG1_DR_12_5HZ
Definition: fxas21002.h:710
void BOARD_InitDebugConsole(void)
Definition: board.c:15
const registerreadlist_t fxas21002_Fifo_Event[]
void BOARD_InitPins(void)
Configures pin routing and optionally pin electrical features.
Definition: pin_mux.c:47
#define FXAS21002_F_SETUP_F_MODE_MASK
Definition: fxas21002.h:273