ISSDK  1.8
IoT Sensing Software Development Kit
fxas21002_poll.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_poll.c
10  * @brief The fxas21002_poll.c file implements the ISSDK FXAS21002 sensor driver
11  * example demonstration with polling 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  * Constants
30  ******************************************************************************/
31 /*! Prepare the register write list to configure FXAS21002 in non-FIFO mode. */
33  /*! Configure CTRL_REG1 register to put FXAS21002 to 12.5Hz sampling rate. */
36 
37 /*! Prepare the register read list to read FXAS21002 DataReady status. */
39 
40 /*! Prepare the register read list to read the raw gyro data from the FXAS21002. */
43 
44 /*******************************************************************************
45  * Code
46  ******************************************************************************/
47 /*!
48  * @brief Main function
49  */
50 int main(void)
51 {
53  uint8_t dataReady;
56  sc_ipc_t ipc;
57  uint32_t freq;
58 
59  ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER;
60  fxas21002_i2c_sensorhandle_t FXAS21002drv;
61 
62  /*! Initialize the MCU hardware. */
63  ipc = BOARD_InitRpc();
64  BOARD_InitMemory();
67 
69 
70  PRINTF("\r\n ------------------------------------------------------------------\r\n");
71  PRINTF("\r\n ISSDK FXAS21002 sensor driver example demonstration with POLL mode\r\n");
72  PRINTF("\r\n ------------------------------------------------------------------\r\n");
73 
74  /* Power on LPI2C. */
75  if (sc_pm_set_resource_power_mode(ipc, SC_R_I2C_0, SC_PM_PW_MODE_ON) != SC_ERR_NONE)
76  {
77  PRINTF("Error: Failed to power on LPI2C\r\n");
78  }
79 
80  /* Set LPI2C clock */
81  freq = CLOCK_SetIpFreq(kCLOCK_DMA_Lpi2c0, SC_24MHZ);
82  if (freq == 0)
83  {
84  PRINTF("Error: Failed to set LPI2C frequency\r\n");
85  }
86 
87  /* Power on IRQSTEER. */
88 #if (MIMX8QM_CM4_CORE0)
89  if (sc_pm_set_resource_power_mode(ipc, SC_R_IRQSTR_M4_0, SC_PM_PW_MODE_ON) != SC_ERR_NONE)
90  {
91  PRINTF("Error: Failed to power on IRQSTEER M4_0\r\n");
92  }
93 #elif (MIMX8QM_CM4_CORE1)
94  if (sc_pm_set_resource_power_mode(ipc, SC_R_IRQSTR_M4_1, SC_PM_PW_MODE_ON) != SC_ERR_NONE)
95  {
96  PRINTF("Error: Failed to power on IRQSTEER M4_1\r\n");
97  }
98 #endif
99 
100  /* Enable interrupt in irqsteer */
101  IRQSTEER_Init(IRQSTEER);
102  IRQSTEER_EnableInterrupt(IRQSTEER, DMA_I2C0_INT_IRQn);
103 
104  /*! Initialize the I2C driver. */
105  status = I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
106  if (ARM_DRIVER_OK != status)
107  {
108  PRINTF("\r\n I2C Initialization Failed\r\n");
109  return -1;
110  }
111 
112  /*! Set the I2C Power mode. */
113  status = I2Cdrv->PowerControl(ARM_POWER_FULL);
114  if (ARM_DRIVER_OK != status)
115  {
116  PRINTF("\r\n I2C Power Mode setting Failed\r\n");
117  return -1;
118  }
119 
120  /*! Set the I2C bus speed. */
121  status = I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
122  if (ARM_DRIVER_OK != status)
123  {
124  PRINTF("\r\n I2C Control Mode setting Failed\r\n");
125  return -1;
126  }
127 
128  /*! Initialize the FXAS21002 sensor driver. */
131  if (SENSOR_ERROR_NONE != status)
132  {
133  PRINTF("\r\n Sensor Initialization Failed\r\n");
134  return -1;
135  }
136  PRINTF("\r\n Successfully Initiliazed FXAS21002 Sensor\r\n");
137  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);
138 
139  /*! Configure the FXAS21002 sensor driver. */
140  status = FXAS21002_I2C_Configure(&FXAS21002drv, fxas21002_Config_Normal);
141  if (SENSOR_ERROR_NONE != status)
142  {
143  PRINTF("\r\n FXAS21002 Sensor Configuration Failed, Err = %d\r\n", status);
144  return -1;
145  }
146  PRINTF("\r\n Successfully Applied FXAS21002 Sensor Configuration\r\n");
147 
148  PRINTF("\r\n Reading FXAS21002 Gyro Values in POLL mode\r\n");
149 
150  for (;;) /* Forever loop */
151  {
152  /*! Wait for data ready from the FXAS21002. */
153  status = FXAS21002_I2C_ReadData(&FXAS21002drv, fxas21002_DRDY, &dataReady);
154  if (0 == (dataReady & FXAS21002_DR_STATUS_ZYXDR_MASK))
155  {
156  continue;
157  }
158 
159  /*! Read the raw sensor data from the FXAS21002. */
160  status = FXAS21002_I2C_ReadData(&FXAS21002drv, fxas21002_Output_Values, data);
161  if (ARM_DRIVER_OK != status)
162  {
163  PRINTF("\r\n Read Failed. \r\n");
164  return -1;
165  }
166 
167  /*! Convert the raw sensor data to signed 16-bit container for display to the debug port. */
168  rawData.gyro[0] = ((int16_t)data[0] << 8) | data[1];
169  rawData.gyro[1] = ((int16_t)data[2] << 8) | data[3];
170  rawData.gyro[2] = ((int16_t)data[4] << 8) | data[5];
171 
172  /* NOTE: PRINTF is relatively expensive in terms of CPU time, specially when used with-in execution loop. */
173  PRINTF("\r\n Gyro X = %d Y = %d Z = %d\r\n", rawData.gyro[0], rawData.gyro[1], rawData.gyro[2]);
174  ASK_USER_TO_RESUME(100); /* Ask for user input after processing 100 samples. */
175  }
176 }
#define FXAS21002_WHO_AM_I_WHOAMI_PROD_VALUE
Definition: fxas21002.h:402
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
const registerreadlist_t fxas21002_Output_Values[]
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.
const registerreadlist_t fxas21002_DRDY[]
This structure defines the fxas21002 raw data buffer.
Definition: fxas21002_drv.h:53
#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.
#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_I2C_ADDR
uint8_t data[FXLS8962_DATA_SIZE]
#define FXAS21002_DR_STATUS_ZYXDR_MASK
Definition: fxas21002.h:160
#define __END_READ_DATA__
Definition: sensor_drv.h:51
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
uint16_t readFrom
Definition: sensor_drv.h:80
#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
int main(void)
Main function.
This structure defines the Read command List.
Definition: sensor_drv.h:78
const registerwritelist_t fxas21002_Config_Normal[]
#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
void BOARD_InitPins(void)
Configures pin routing and optionally pin electrical features.
Definition: pin_mux.c:47