ISSDK  1.7
IoT Sensing Software Development Kit
fxos8700_poll.c
Go to the documentation of this file.
1 /*
2  * The Clear BSD License
3  * Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
4  * Copyright 2016-2017 NXP
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without modification,
8  * are permitted (subject to the limitations in the disclaimer below) provided
9  * that the following conditions are met:
10  *
11  * o Redistributions of source code must retain the above copyright notice, this list
12  * of conditions and the following disclaimer.
13  *
14  * o Redistributions in binary form must reproduce the above copyright notice, this
15  * list of conditions and the following disclaimer in the documentation and/or
16  * other materials provided with the distribution.
17  *
18  * o Neither the name of the copyright holder nor the names of its
19  * contributors may be used to endorse or promote products derived from this
20  * software without specific prior written permission.
21  *
22  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
27  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /**
36  * @file fxos8700_poll.c
37  * @brief The fxos8700_poll.c file implements the ISSDK FXOS8700 sensor driver
38  * example demonstration with polling mode.
39  */
40 
41 /* SDK Includes */
42 #include "board.h"
43 #include "pin_mux.h"
44 #include "clock_config.h"
45 #include "fsl_debug_console.h"
46 
47 /* CMSIS Includes */
48 #include "Driver_I2C.h"
49 
50 /* ISSDK Includes */
51 #include "issdk_hal.h"
52 #include "fxos8700_drv.h"
53 
54 /*******************************************************************************
55  * Macro Definitions
56  ******************************************************************************/
57 #define RAW_ACCELMAG_DATA_SIZE (12)
58 
59 /*******************************************************************************
60  * Constants
61  ******************************************************************************/
62 /*! Prepare the register write list to configure FXOS8700 in Hybrid mode. */
64  /*! System and Control registers. */
65  /*! Configure the FXOS8700 to 25Hz Hybrid sampling rate. */
68  FXOS8700_M_CTRL_REG1_M_ACAL_MASK | FXOS8700_M_CTRL_REG1_M_HMS_MASK}, /*! Enable the Hybrid Mode. */
70  FXOS8700_M_CTRL_REG2_M_AUTOINC_MASK | FXOS8700_M_CTRL_REG2_M_RST_CNT_MASK}, /*! Enable the Data read with Hybrid Mode. */
72 
73 /*! Command definition to read the Data Ready Status */
75 
76 /*! Command definition to read the Accel + Mag Data */
79 
80 /*! Command definition to read the Accel + Mag Data (in 2 TXNs). */
82  {.readFrom = FXOS8700_M_OUT_X_MSB, .numBytes = 6},
84 
85 /*******************************************************************************
86  * Code
87  ******************************************************************************/
88 /*!
89  * @brief Main function
90  */
91 int main(void)
92 {
94  uint8_t dataReady;
97 
98  ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER; // Now using the shield.h value!!!
99  fxos8700_i2c_sensorhandle_t FXOS8700drv;
100 
101  /*! Initialize the MCU hardware. */
102  BOARD_InitPins();
105 
106  PRINTF("\r\n ISSDK FXOS8700 sensor driver example demonstration with poll mode\r\n");
107 
108  /*! Initialize the I2C driver. */
109  status = I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
110  if (ARM_DRIVER_OK != status)
111  {
112  PRINTF("\r\n I2C Initialization Failed\r\n");
113  return -1;
114  }
115 
116  /*! Set the I2C Power mode. */
117  status = I2Cdrv->PowerControl(ARM_POWER_FULL);
118  if (ARM_DRIVER_OK != status)
119  {
120  PRINTF("\r\n I2C Power Mode setting Failed\r\n");
121  return -1;
122  }
123 
124  /*! Set the I2C bus speed. */
125  status = I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
126  if (ARM_DRIVER_OK != status)
127  {
128  PRINTF("\r\n I2C Control Mode setting Failed\r\n");
129  return -1;
130  }
131 
132  /*! Initialize the FXOS8700 sensor driver. */
135  if (SENSOR_ERROR_NONE != status)
136  {
137  PRINTF("\r\n Sensor Initialization Failed\r\n");
138  return -1;
139  }
140  PRINTF("\r\n Successfully Initiliazed Sensor\r\n");
141 
142  /*! Set the task to be executed while waiting for I2C transactions to complete. */
144 
145  /*! Configure the fxos8700 sensor driver. */
146  status = FXOS8700_I2C_Configure(&FXOS8700drv, fxos8700_Config_Hybrid);
147  if (SENSOR_ERROR_NONE != status)
148  {
149  PRINTF("\r\n FXOS8700 Sensor Configuration Failed, Err = %d\r\n", status);
150  return -1;
151  }
152  PRINTF("\r\n Successfully Applied FXOS8700 Sensor Configuration\r\n");
153 
154  for (;;) /* Forever loop */
155  {
156  /*! Wait for data ready from the FXOS8700. */
157  status = FXOS8700_I2C_ReadData(&FXOS8700drv, FXOS8700_STATUS_READ, &dataReady);
158  if (0 == (dataReady & FXOS8700_DR_STATUS_ZYXDR_MASK))
159  { /* Loop, if new sample is not available. */
160  continue;
161  }
162 
163  /*! Read the raw sensor data from the fxos8700. */
164  status = FXOS8700_I2C_ReadData(&FXOS8700drv, FXOS8700_ACCELMAG_READ, data);
165  if (ARM_DRIVER_OK != status)
166  {
167  PRINTF("\r\n Read Failed. \r\n");
168  return -1;
169  }
170 
171  /*! Convert the raw sensor data to signed 16-bit container for display to the debug port. */
172  rawData.accel[0] = ((int16_t)data[0] << 8) | data[1];
173  rawData.accel[0] /= 4;
174  rawData.accel[1] = ((int16_t)data[2] << 8) | data[3];
175  rawData.accel[1] /= 4;
176  rawData.accel[2] = ((int16_t)data[4] << 8) | data[5];
177  rawData.accel[2] /= 4;
178  rawData.mag[0] = ((int16_t)data[6] << 8) | data[7];
179  rawData.mag[1] = ((int16_t)data[8] << 8) | data[9];
180  rawData.mag[2] = ((int16_t)data[10] << 8) | data[11];
181 
182  /* NOTE: PRINTF is relatively expensive in terms of CPU time, specially when used with-in execution loop. */
183  PRINTF("\r\n Accel X = %d Y = %d Z = %d\r\n", rawData.accel[0], rawData.accel[1], rawData.accel[2]);
184  PRINTF("\r\n Mag X = %d Y = %d Z = %d\r\n", rawData.mag[0], rawData.mag[1], rawData.mag[2]);
185  ASK_USER_TO_RESUME(50); /* Ask for user input after processing 100 samples. */
186  }
187 }
#define FXOS8700_M_CTRL_REG1_M_HMS_MASK
Definition: fxos8700.h:2573
#define FXOS8700_M_CTRL_REG1_M_ACAL_EN
Definition: fxos8700.h:2592
#define __END_WRITE_DATA__
Definition: sensor_drv.h:71
The fxos8700_drv.h file describes the fxos8700 driver interface and structures.
This structure defines the fxos8700 raw data buffer.
Definition: fxos8700_drv.h:79
void FXOS8700_I2C_SetIdleTask(fxos8700_i2c_sensorhandle_t *pSensorHandle, registeridlefunction_t idleTask, void *userParam)
: The interface function to set the I2C Idle Task.
Definition: fxos8700_drv.c:278
#define BOARD_BootClockRUN
Definition: clock_config.h:45
const registerreadlist_t FXOS8700_STATUS_READ[]
Definition: fxos8700_poll.c:74
uint8_t data[FXLS8962_DATA_SIZE]
int32_t status
void BOARD_InitDebugConsole(void)
Definition: board.c:41
void(* registeridlefunction_t)(void *userParam)
This is the register idle function type.
Definition: sensor_drv.h:123
This defines the sensor specific information for I2C.
Definition: fxos8700_drv.h:70
const registerwritelist_t fxos8700_Config_Hybrid[]
Definition: fxos8700_poll.c:63
#define FXOS8700_M_CTRL_REG2_M_RST_CNT_DISABLE
Definition: fxos8700.h:2693
#define __END_READ_DATA__
Definition: sensor_drv.h:77
const registerreadlist_t FXOS8700_ACCELMAG_READ[]
Definition: fxos8700_poll.c:77
#define FXOS8700_CTRL_REG1_DR_HYBRID_25_HZ
Definition: fxos8700.h:1556
const registerreadlist_t FXOS8700_ACCEL_MAG_READ[]
Definition: fxos8700_poll.c:81
#define FXOS8700_WHO_AM_I_PROD_VALUE
Definition: fxos8700.h:172
#define FXOS8700_M_CTRL_REG2_M_AUTOINC_MASK
Definition: fxos8700.h:2660
#define FXOS8700_M_CTRL_REG1_M_ACAL_MASK
Definition: fxos8700.h:2585
#define ASK_USER_TO_RESUME(x)
Definition: frdm_k64f.h:132
int main(void)
Main function.
Definition: fxos8700_poll.c:91
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:248
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:331
#define FXOS8700_M_CTRL_REG2_M_AUTOINC_HYBRID_MODE
Definition: fxos8700.h:2667
#define I2C_S_DEVICE_INDEX
Definition: issdk_hal.h:61
#define I2C_S_DRIVER
Definition: issdk_hal.h:59
#define FXOS8700_M_CTRL_REG2_M_RST_CNT_MASK
Definition: fxos8700.h:2648
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:286
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
#define FXOS8700_CTRL_REG1_DR_MASK
Definition: fxos8700.h:1530
fxls8962_acceldataUser_t rawData
#define FXOS8700_DR_STATUS_ZYXDR_MASK
Definition: fxos8700.h:212
This structure defines the Write command List.
Definition: sensor_drv.h:94
This structure defines the Read command List.
Definition: sensor_drv.h:104
#define SMC
Definition: lpc54114.h:144
status_t SMC_SetPowerModeWait(void *arg)
Configures the system to WAIT power mode. API name used from Kinetis family to maintain compatibility...
Definition: lpc54114.c:181
#define RAW_ACCELMAG_DATA_SIZE
Definition: fxos8700_poll.c:57
void BOARD_InitPins(void)
Configures pin routing and optionally pin electrical features.
Definition: pin_mux.c:73
#define FXOS8700_I2C_ADDR
#define FXOS8700_M_CTRL_REG1_M_HMS_HYBRID_MODE
Definition: fxos8700.h:2621
#define I2C_S_SIGNAL_EVENT
Definition: issdk_hal.h:60