ISSDK  1.8
IoT Sensing Software Development Kit
fxos8700_poll.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
3  * Copyright 2016-2017 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 /**
10  * @file fxos8700_poll.c
11  * @brief The fxos8700_poll.c file implements the ISSDK FXOS8700 sensor driver
12  * example demonstration with polling mode.
13  */
14 
15 /* SDK Includes */
16 #include "pin_mux.h"
17 #include "clock_config.h"
18 #include "board.h"
19 #include "fsl_debug_console.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 
72  ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER; // Now using the shield.h value!!!
73  fxos8700_i2c_sensorhandle_t FXOS8700drv;
74 
75  /*! Initialize the MCU hardware. */
79 
80  PRINTF("\r\n ISSDK FXOS8700 sensor driver example demonstration with poll mode\r\n");
81 
82  /*! Initialize the I2C driver. */
83  status = I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
84  if (ARM_DRIVER_OK != status)
85  {
86  PRINTF("\r\n I2C Initialization Failed\r\n");
87  return -1;
88  }
89 
90  /*! Set the I2C Power mode. */
91  status = I2Cdrv->PowerControl(ARM_POWER_FULL);
92  if (ARM_DRIVER_OK != status)
93  {
94  PRINTF("\r\n I2C Power Mode setting Failed\r\n");
95  return -1;
96  }
97 
98  /*! Set the I2C bus speed. */
99  status = I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
100  if (ARM_DRIVER_OK != status)
101  {
102  PRINTF("\r\n I2C Control Mode setting Failed\r\n");
103  return -1;
104  }
105 
106  /*! Initialize the FXOS8700 sensor driver. */
109  if (SENSOR_ERROR_NONE != status)
110  {
111  PRINTF("\r\n Sensor Initialization Failed\r\n");
112  return -1;
113  }
114  PRINTF("\r\n Successfully Initiliazed Sensor\r\n");
115 
116  /*! Set the task to be executed while waiting for I2C transactions to complete. */
117  //FXOS8700_I2C_SetIdleTask(&FXOS8700drv, (registeridlefunction_t)SMC_SetPowerModeVlpr, SMC);
118 
119  /*! Configure the fxos8700 sensor driver. */
120  status = FXOS8700_I2C_Configure(&FXOS8700drv, fxos8700_Config_Hybrid);
121  if (SENSOR_ERROR_NONE != status)
122  {
123  PRINTF("\r\n FXOS8700 Sensor Configuration Failed, Err = %d\r\n", status);
124  return -1;
125  }
126  PRINTF("\r\n Successfully Applied FXOS8700 Sensor Configuration\r\n");
127 
128  for (;;) /* Forever loop */
129  {
130  /*! Wait for data ready from the FXOS8700. */
131  status = FXOS8700_I2C_ReadData(&FXOS8700drv, FXOS8700_STATUS_READ, &dataReady);
132  if (0 == (dataReady & FXOS8700_DR_STATUS_ZYXDR_MASK))
133  { /* Loop, if new sample is not available. */
134  continue;
135  }
136 
137  /*! Read the raw sensor data from the fxos8700. */
138  status = FXOS8700_I2C_ReadData(&FXOS8700drv, FXOS8700_ACCELMAG_READ, data);
139  if (ARM_DRIVER_OK != status)
140  {
141  PRINTF("\r\n Read Failed. \r\n");
142  return -1;
143  }
144 
145  /*! Convert the raw sensor data to signed 16-bit container for display to the debug port. */
146  rawData.accel[0] = ((int16_t)data[0] << 8) | data[1];
147  rawData.accel[0] /= 4;
148  rawData.accel[1] = ((int16_t)data[2] << 8) | data[3];
149  rawData.accel[1] /= 4;
150  rawData.accel[2] = ((int16_t)data[4] << 8) | data[5];
151  rawData.accel[2] /= 4;
152  rawData.mag[0] = ((int16_t)data[6] << 8) | data[7];
153  rawData.mag[1] = ((int16_t)data[8] << 8) | data[9];
154  rawData.mag[2] = ((int16_t)data[10] << 8) | data[11];
155 
156  /* NOTE: PRINTF is relatively expensive in terms of CPU time, specially when used with-in execution loop. */
157  PRINTF("\r\n Accel X = %d Y = %d Z = %d\r\n", rawData.accel[0], rawData.accel[1], rawData.accel[2]);
158  PRINTF("\r\n Mag X = %d Y = %d Z = %d\r\n", rawData.mag[0], rawData.mag[1], rawData.mag[2]);
159  ASK_USER_TO_RESUME(50); /* Ask for user input after processing 100 samples. */
160  }
161 }
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
#define RAW_ACCELMAG_DATA_SIZE
Definition: fxos8700_poll.c:31
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