ISSDK  1.8
IoT Sensing Software Development Kit
fxos8700_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 fxos8700_poll.c
10  * @brief The fxos8700_poll.c file implements the ISSDK FXOS8700 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 "fxos8700_drv.h"
27 
28 /*******************************************************************************
29  * Macro Definitions
30  ******************************************************************************/
31 #define RAW_ACCELMAG_DATA_SIZE (12)
32 
33 /*******************************************************************************
34  * Constants
35  ******************************************************************************/
36 /*! Prepare the register write list to configure FXOS8700 in Hybrid mode. */
38  /*! System and Control registers. */
39  /*! Configure the FXOS8700 to 25Hz Hybrid sampling rate. */
42  FXOS8700_M_CTRL_REG1_M_ACAL_MASK | FXOS8700_M_CTRL_REG1_M_HMS_MASK}, /*! Enable the Hybrid Mode. */
44  FXOS8700_M_CTRL_REG2_M_AUTOINC_MASK | FXOS8700_M_CTRL_REG2_M_RST_CNT_MASK}, /*! Enable the Data read with Hybrid Mode. */
46 
47 /*! Command definition to read the Data Ready Status */
49 
50 /*! Command definition to read the Accel + Mag Data */
53 
54 /*! Command definition to read the Accel + Mag Data (in 2 TXNs). */
56  {.readFrom = FXOS8700_M_OUT_X_MSB, .numBytes = 6},
58 
59 /*******************************************************************************
60  * Code
61  ******************************************************************************/
62 /*!
63  * @brief Main function
64  */
65 int main(void)
66 {
68  uint8_t dataReady;
71  sc_ipc_t ipc;
72  uint32_t freq;
73 
74  ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER;
75  fxos8700_i2c_sensorhandle_t FXOS8700drv;
76 
77  /*! Initialize the MCU hardware. */
78  ipc = BOARD_InitRpc();
79  BOARD_InitMemory();
82 
84 
85  PRINTF("\r\n -----------------------------------------------------------------\r\n");
86  PRINTF("\r\n ISSDK FXOS8700 sensor driver example demonstration with POLL mode\r\n");
87  PRINTF("\r\n -----------------------------------------------------------------\r\n");
88 
89  /* Power on LPI2C. */
90  if (sc_pm_set_resource_power_mode(ipc, SC_R_I2C_0, SC_PM_PW_MODE_ON) != SC_ERR_NONE)
91  {
92  PRINTF("Error: Failed to power on LPI2C\r\n");
93  }
94 
95  /* Set LPI2C clock */
96  freq = CLOCK_SetIpFreq(kCLOCK_DMA_Lpi2c0, SC_24MHZ);
97  if (freq == 0)
98  {
99  PRINTF("Error: Failed to set LPI2C frequency\r\n");
100  }
101 
102  /* Power on IRQSTEER. */
103 #if (MIMX8QM_CM4_CORE0)
104  if (sc_pm_set_resource_power_mode(ipc, SC_R_IRQSTR_M4_0, SC_PM_PW_MODE_ON) != SC_ERR_NONE)
105  {
106  PRINTF("Error: Failed to power on IRQSTEER M4_0\r\n");
107  }
108 #elif (MIMX8QM_CM4_CORE1)
109  if (sc_pm_set_resource_power_mode(ipc, SC_R_IRQSTR_M4_1, SC_PM_PW_MODE_ON) != SC_ERR_NONE)
110  {
111  PRINTF("Error: Failed to power on IRQSTEER M4_1\r\n");
112  }
113 #endif
114 
115  /* Enable interrupt in irqsteer */
116  IRQSTEER_Init(IRQSTEER);
117  IRQSTEER_EnableInterrupt(IRQSTEER, DMA_I2C0_INT_IRQn);
118 
119  /*! Initialize the I2C driver. */
120  status = I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
121  if (ARM_DRIVER_OK != status)
122  {
123  PRINTF("\r\n I2C Initialization Failed\r\n");
124  return -1;
125  }
126 
127  /*! Set the I2C Power mode. */
128  status = I2Cdrv->PowerControl(ARM_POWER_FULL);
129  if (ARM_DRIVER_OK != status)
130  {
131  PRINTF("\r\n I2C Power Mode setting Failed\r\n");
132  return -1;
133  }
134 
135  /*! Set the I2C bus speed. */
136  status = I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
137  if (ARM_DRIVER_OK != status)
138  {
139  PRINTF("\r\n I2C Control Mode setting Failed\r\n");
140  return -1;
141  }
142 
143  /*! Initialize the FXOS8700 sensor driver. */
146  if (SENSOR_ERROR_NONE != status)
147  {
148  PRINTF("\r\n Sensor Initialization Failed\r\n");
149  return -1;
150  }
151  PRINTF("\r\n Successfully Initiliazed FXOS8700 Sensor\r\n");
152  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);
153 
154  /*! Configure the fxos8700 sensor driver. */
155  status = FXOS8700_I2C_Configure(&FXOS8700drv, fxos8700_Config_Hybrid);
156  if (SENSOR_ERROR_NONE != status)
157  {
158  PRINTF("\r\n FXOS8700 Sensor Configuration Failed, Err = %d\r\n", status);
159  return -1;
160  }
161  PRINTF("\r\n Successfully Applied FXOS8700 Sensor Configuration\r\n");
162 
163  PRINTF("\r\n Reading FXOS8700 Accel & Mag Values in Hybrid POLL mode\r\n");
164 
165  for (;;) /* Forever loop */
166  {
167  /*! Wait for data ready from the FXOS8700. */
168  status = FXOS8700_I2C_ReadData(&FXOS8700drv, FXOS8700_STATUS_READ, &dataReady);
169  if (0 == (dataReady & FXOS8700_DR_STATUS_ZYXDR_MASK))
170  { /* Loop, if new sample is not available. */
171  continue;
172  }
173 
174  /*! Read the raw sensor data from the fxos8700. */
175  status = FXOS8700_I2C_ReadData(&FXOS8700drv, FXOS8700_ACCELMAG_READ, data);
176  if (ARM_DRIVER_OK != status)
177  {
178  PRINTF("\r\n Read Failed. \r\n");
179  return -1;
180  }
181 
182  /*! Convert the raw sensor data to signed 16-bit container for display to the debug port. */
183  rawData.accel[0] = ((int16_t)data[0] << 8) | data[1];
184  rawData.accel[0] /= 4;
185  rawData.accel[1] = ((int16_t)data[2] << 8) | data[3];
186  rawData.accel[1] /= 4;
187  rawData.accel[2] = ((int16_t)data[4] << 8) | data[5];
188  rawData.accel[2] /= 4;
189  rawData.mag[0] = ((int16_t)data[6] << 8) | data[7];
190  rawData.mag[1] = ((int16_t)data[8] << 8) | data[9];
191  rawData.mag[2] = ((int16_t)data[10] << 8) | data[11];
192 
193  /* NOTE: PRINTF is relatively expensive in terms of CPU time, specially when used with-in execution loop. */
194  PRINTF("\r\n Accel X = %d Y = %d Z = %d\r\n", rawData.accel[0], rawData.accel[1], rawData.accel[2]);
195  PRINTF("\r\n Mag X = %d Y = %d Z = %d\r\n\r\n", rawData.mag[0], rawData.mag[1], rawData.mag[2]);
196  ASK_USER_TO_RESUME(50); /* Ask for user input after processing 100 samples. */
197  }
198 }
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 FXOS8700_M_CTRL_REG2_M_AUTOINC_MASK
Definition: fxos8700.h:2634
#define ASK_USER_TO_RESUME(x)
Definition: frdm_k64f.h:106
int32_t status
#define FXOS8700_M_CTRL_REG1_M_HMS_MASK
Definition: fxos8700.h:2547
#define FXOS8700_WHO_AM_I_PROD_VALUE
Definition: fxos8700.h:146
#define FXOS8700_CTRL_REG1_DR_MASK
Definition: fxos8700.h:1504
const registerreadlist_t FXOS8700_STATUS_READ[]
Definition: fxos8700_poll.c:48
#define FXOS8700_M_CTRL_REG1_M_HMS_HYBRID_MODE
Definition: fxos8700.h:2595
#define __END_WRITE_DATA__
Definition: sensor_drv.h:45
#define FXOS8700_M_CTRL_REG2_M_AUTOINC_HYBRID_MODE
Definition: fxos8700.h:2641
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
This defines the sensor specific information for I2C.
Definition: fxos8700_drv.h:44
#define I2C_S_DRIVER
Definition: issdk_hal.h:33
const registerreadlist_t FXOS8700_ACCEL_MAG_READ[]
Definition: fxos8700_poll.c:55
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
const registerwritelist_t fxos8700_Config_Hybrid[]
Definition: fxos8700_poll.c:37
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
#define FXOS8700_M_CTRL_REG1_M_ACAL_EN
Definition: fxos8700.h:2566
#define FXOS8700_M_CTRL_REG1_M_ACAL_MASK
Definition: fxos8700.h:2559
uint8_t data[FXLS8962_DATA_SIZE]
const registerreadlist_t FXOS8700_ACCELMAG_READ[]
Definition: fxos8700_poll.c:51
#define __END_READ_DATA__
Definition: sensor_drv.h:51
ARM_DRIVER_I2C * I2Cdrv
fxos8700_accelmagdata_t rawData
#define FXOS8700_M_CTRL_REG2_M_RST_CNT_DISABLE
Definition: fxos8700.h:2667
#define FXOS8700_M_CTRL_REG2_M_RST_CNT_MASK
Definition: fxos8700.h:2622
uint16_t readFrom
Definition: sensor_drv.h:80
The fxos8700_drv.h file describes the fxos8700 driver interface and structures.
#define FXOS8700_CTRL_REG1_DR_HYBRID_25_HZ
Definition: fxos8700.h:1530
This structure defines the Read command List.
Definition: sensor_drv.h:78
#define I2C_S_DEVICE_INDEX
Definition: issdk_hal.h:35
int main(void)
Main function.
Definition: fxos8700_poll.c:65
void BOARD_InitDebugConsole(void)
Definition: board.c:15
#define FXOS8700_DR_STATUS_ZYXDR_MASK
Definition: fxos8700.h:186
#define FXOS8700_I2C_ADDR
void BOARD_InitPins(void)
Configures pin routing and optionally pin electrical features.
Definition: pin_mux.c:47
#define RAW_ACCELMAG_DATA_SIZE
Definition: fxos8700_poll.c:31