ISSDK  1.8
IoT Sensing Software Development Kit
fxos8700_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 fxos8700_fifo.c
10  * @brief The fxos8700.c file implements the ISSDK FXOS8700 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 "fxos8700_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 #define RAW_ACCEL_DATA_SIZE (6)
37 
38 /*******************************************************************************
39  * Constants
40  ******************************************************************************/
41 /*! Prepare the register write list to configure FXOS8700 in FIFO mode. */
43  /*! System and Control registers. */
44  /*! Configure the FXOS8700 to 12.5Hz sampling rate. */
46  /*! Prepare the register write list to configure FXOS8700 in FIFO mode. */
50 
51 /*! Command definition to read the Data Ready Status */
53 
54 /*! Command definition to read the FIFO_SIZE Bytes of Accel Data */
57 
58 /*******************************************************************************
59  * Code
60  ******************************************************************************/
61 /*!
62  * @brief Main function
63  */
64 int main(void)
65 {
67  uint8_t fifoEvent;
70  sc_ipc_t ipc;
71  uint32_t freq;
72 
73  ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER;
74  fxos8700_i2c_sensorhandle_t FXOS8700drv;
75 
76  /*! Initialize the MCU hardware. */
77  ipc = BOARD_InitRpc();
78  BOARD_InitMemory();
81 
83 
84  PRINTF("\r\n -----------------------------------------------------------------\r\n");
85  PRINTF("\r\n ISSDK FXOS8700 sensor driver example demonstration with FIFO mode\r\n");
86  PRINTF("\r\n -----------------------------------------------------------------\r\n");
87 
88  /* Power on LPI2C. */
89  if (sc_pm_set_resource_power_mode(ipc, SC_R_I2C_0, SC_PM_PW_MODE_ON) != SC_ERR_NONE)
90  {
91  PRINTF("Error: Failed to power on LPI2C\r\n");
92  }
93 
94  /* Set LPI2C clock */
95  freq = CLOCK_SetIpFreq(kCLOCK_DMA_Lpi2c0, SC_24MHZ);
96  if (freq == 0)
97  {
98  PRINTF("Error: Failed to set LPI2C frequency\r\n");
99  }
100 
101  /* Power on IRQSTEER. */
102 #if (MIMX8QM_CM4_CORE0)
103  if (sc_pm_set_resource_power_mode(ipc, SC_R_IRQSTR_M4_0, SC_PM_PW_MODE_ON) != SC_ERR_NONE)
104  {
105  PRINTF("Error: Failed to power on IRQSTEER M4_0\r\n");
106  }
107 #elif (MIMX8QM_CM4_CORE1)
108  if (sc_pm_set_resource_power_mode(ipc, SC_R_IRQSTR_M4_1, SC_PM_PW_MODE_ON) != SC_ERR_NONE)
109  {
110  PRINTF("Error: Failed to power on IRQSTEER M4_1\r\n");
111  }
112 #endif
113 
114  /* Enable interrupt in irqsteer */
115  IRQSTEER_Init(IRQSTEER);
116  IRQSTEER_EnableInterrupt(IRQSTEER, DMA_I2C0_INT_IRQn);
117 
118  /*! Initialize the I2C driver. */
119  status = I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
120  if (ARM_DRIVER_OK != status)
121  {
122  PRINTF("\r\n I2C Initialization Failed\r\n");
123  return -1;
124  }
125 
126  /*! Set the I2C Power mode. */
127  status = I2Cdrv->PowerControl(ARM_POWER_FULL);
128  if (ARM_DRIVER_OK != status)
129  {
130  PRINTF("\r\n I2C Power Mode setting Failed\r\n");
131  return -1;
132  }
133 
134  /*! Set the I2C bus speed. */
135  status = I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
136  if (ARM_DRIVER_OK != status)
137  {
138  PRINTF("\r\n I2C Control Mode setting Failed\r\n");
139  return -1;
140  }
141 
142  /*! Initialize the FXOS8700 sensor driver. */
145  if (SENSOR_ERROR_NONE != status)
146  {
147  PRINTF("\r\n Sensor Initialization Failed\r\n");
148  return -1;
149  }
150  PRINTF("\r\n Successfully Initiliazed FXOS8700 Sensor\r\n");
151  PRINTF("\r\n Device Address is 0x%x and WHOAMI Value is 0x%x\r\n", FXOS8700_I2C_ADDR, FXOS8700_WHO_AM_I_PROD_VALUE);
152 
153  /*! Configure the fxos8700 sensor driver. */
154  status = FXOS8700_I2C_Configure(&FXOS8700drv, fxos8700_Config_with_Fifo);
155  if (SENSOR_ERROR_NONE != status)
156  {
157  PRINTF("\r\n FXOS8700 Sensor Configuration Failed, Err = %d\r\n", status);
158  return -1;
159  }
160  PRINTF("\r\n Successfully Applied FXOS8700 Sensor Configuration\r\n");
161 
162  PRINTF("\r\n Reading FXOS8700 Accel Values in FIFO mode, FIFO width=%d\r\n", FIFO_SIZE);
163 
164  for (;;) /* Forever loop */
165  {
166  /*! Wait for the FIFO watermark event. */
167  status = FXOS8700_I2C_ReadData(&FXOS8700drv, FXOS8700_STATUS_READ, &fifoEvent);
168  if (0 == (fifoEvent & FXOS8700_F_STATUS_F_WMRK_FLAG_MASK))
169  {
170  continue;
171  }
172 
173  /*! Read the raw sensor data from the fxos8700. */
174  status = FXOS8700_I2C_ReadData(&FXOS8700drv, FXOS8700_ACCEL_FIFO_READ, data);
175  if (ARM_DRIVER_OK != status)
176  {
177  PRINTF("\r\n Read Failed. \r\n");
178  return -1;
179  }
180 
181  for (uint8_t i = 0; i < FIFO_SIZE; i++)
182  {
183  /*! Convert the raw sensor data to signed 16-bit container for display to the debug port. */
184  rawData.accel[0] = ((uint16_t)data[i * RAW_ACCEL_DATA_SIZE + 0] << 8) | data[i * RAW_ACCEL_DATA_SIZE + 1];
185  rawData.accel[0] /= 4;
186  rawData.accel[1] = ((uint16_t)data[i * RAW_ACCEL_DATA_SIZE + 2] << 8) | data[i * RAW_ACCEL_DATA_SIZE + 3];
187  rawData.accel[1] /= 4;
188  rawData.accel[2] = ((uint16_t)data[i * RAW_ACCEL_DATA_SIZE + 4] << 8) | data[i * RAW_ACCEL_DATA_SIZE + 5];
189  rawData.accel[2] /= 4;
190  }
191  /* NOTE: PRINTF is relatively expensive in terms of CPU time, specially when used with-in execution loop. */
192  PRINTF("\r\n Accel X = %d Y = %d Z = %d\r\n", rawData.accel[0], rawData.accel[1], rawData.accel[2]);
193  ASK_USER_TO_RESUME(100 / FIFO_SIZE); /* Ask for user input after processing 100 samples. */
194  }
195 }
#define RAW_ACCEL_DATA_SIZE
Definition: fxos8700_fifo.c:36
int32_t FXOS8700_I2C_Initialize(fxos8700_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: fxos8700_drv.c:222
This structure defines the fxos8700 raw data buffer.
Definition: fxos8700_drv.h:53
This structure defines the Write command List.
Definition: sensor_drv.h:68
#define ASK_USER_TO_RESUME(x)
Definition: frdm_k64f.h:106
int32_t status
#define FXOS8700_WHO_AM_I_PROD_VALUE
Definition: fxos8700.h:146
#define FXOS8700_CTRL_REG1_DR_MASK
Definition: fxos8700.h:1504
#define FXOS8700_CTRL_REG1_DR_SINGLE_12P5_HZ
Definition: fxos8700.h:1523
#define __END_WRITE_DATA__
Definition: sensor_drv.h:45
int main(void)
Main function.
Definition: fxos8700_fifo.c:64
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
#define I2C_S_SIGNAL_EVENT
Definition: issdk_hal.h:34
#define FXOS8700_F_SETUP_F_WMRK_SHIFT
Definition: fxos8700.h:448
This defines the sensor specific information for I2C.
Definition: fxos8700_drv.h:44
#define FXOS8700_F_STATUS_F_WMRK_FLAG_MASK
Definition: fxos8700.h:250
#define I2C_S_DRIVER
Definition: issdk_hal.h:33
const registerwritelist_t fxos8700_Config_with_Fifo[]
Definition: fxos8700_fifo.c:42
int32_t FXOS8700_I2C_Configure(fxos8700_i2c_sensorhandle_t *pSensorHandle, const registerwritelist_t *pRegWriteList)
The interface function to configure he sensor.
Definition: fxos8700_drv.c:260
int32_t FXOS8700_I2C_ReadData(fxos8700_i2c_sensorhandle_t *pSensorHandle, const registerreadlist_t *pReadList, uint8_t *pBuffer)
The interface function to read the sensor data.
Definition: fxos8700_drv.c:305
#define BOARD_BootClockRUN
Definition: clock_config.h:19
uint8_t data[FXLS8962_DATA_SIZE]
#define __END_READ_DATA__
Definition: sensor_drv.h:51
#define FXOS8700_F_SETUP_F_WMRK_MASK
Definition: fxos8700.h:447
#define FXOS8700_F_SETUP_F_MODE_FIFO_STOP_OVF
Definition: fxos8700.h:461
#define FIFO_SIZE
The watermark value configured for FXAS21002 FIFO Buffer.
Definition: fxos8700_fifo.c:35
ARM_DRIVER_I2C * I2Cdrv
fxos8700_accelmagdata_t rawData
const registerreadlist_t FXOS8700_ACCEL_FIFO_READ[]
Definition: fxos8700_fifo.c:55
uint16_t readFrom
Definition: sensor_drv.h:80
The fxos8700_drv.h file describes the fxos8700 driver interface and structures.
#define FXOS8700_F_SETUP_F_MODE_MASK
Definition: fxos8700.h:450
This structure defines the Read command List.
Definition: sensor_drv.h:78
const registerreadlist_t FXOS8700_STATUS_READ[]
Definition: fxos8700_fifo.c:52
#define I2C_S_DEVICE_INDEX
Definition: issdk_hal.h:35
void BOARD_InitDebugConsole(void)
Definition: board.c:15
#define FXOS8700_I2C_ADDR
void BOARD_InitPins(void)
Configures pin routing and optionally pin electrical features.
Definition: pin_mux.c:47