ISSDK  1.7
IoT Sensing Software Development Kit
fxos8700_poll_spi.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_spi.c
37  * @brief The fxos8700_poll_spi.c file implements the ISSDK FXOS8700 SPI sensor driver
38  * example demonstration for SPI Mode with polling.
39  */
40 
41 //-----------------------------------------------------------------------
42 // SDK Includes
43 //-----------------------------------------------------------------------
44 #include "board.h"
45 #include "pin_mux.h"
46 #include "clock_config.h"
47 #include "fsl_debug_console.h"
48 
49 //-----------------------------------------------------------------------
50 // ISSDK Includes
51 //-----------------------------------------------------------------------
52 #include "issdk_hal.h"
53 #include "gpio_driver.h"
54 #include "fxos8700_drv.h"
55 
56 //-----------------------------------------------------------------------
57 // CMSIS Includes
58 //-----------------------------------------------------------------------
59 #include "Driver_SPI.h"
60 
61 /*******************************************************************************
62  * Macro Definitions
63  ******************************************************************************/
64 #define RAW_ACCELMAG_DATA_SIZE (12)
65 
66 /*******************************************************************************
67  * Constants
68  ******************************************************************************/
69 /*! Prepare the register write list to configure FXOS8700 in Hybrid mode. */
71  /*! System and Control registers. */
72  /*! Configure the FXOS8700 to 12.5Hz sampling rate. */
75  FXOS8700_M_CTRL_REG1_M_ACAL_MASK | FXOS8700_M_CTRL_REG1_M_HMS_MASK}, /*! Enable the Hybrid Mode. */
77  FXOS8700_M_CTRL_REG2_M_AUTOINC_MASK | FXOS8700_M_CTRL_REG2_M_RST_CNT_MASK}, /*! Enable the Data read with Hybrid Mode. */
79 
80 /*! Command definition to read the Data Ready Status */
82 
83 /*! Command definition to read the Accel + Mag Data */
86 
87 /*******************************************************************************
88  * Code
89  ******************************************************************************/
90 /*!
91  * @brief Main function
92  */
93 int main(void)
94 {
96  uint8_t dataReady;
99 
100  ARM_DRIVER_SPI *pSPIdriver = &SPI_S_DRIVER;
101  fxos8700_spi_sensorhandle_t fxos8700Driver;
102 
103  /*! Initialize the MCU hardware. */
104  BOARD_InitPins();
107 
108  PRINTF("\r\n ISSDK FXOS8700 sensor driver example demonstration for SPI with Poll Mode.\r\n");
109 
110  /*! Initialize the SPI driver. */
111  status = pSPIdriver->Initialize(SPI_S_SIGNAL_EVENT);
112  if (ARM_DRIVER_OK != status)
113  {
114  PRINTF("\r\n SPI Initialization Failed\r\n");
115  return -1;
116  }
117 
118  /*! Set the SPI Power mode. */
119  status = pSPIdriver->PowerControl(ARM_POWER_FULL);
120  if (ARM_DRIVER_OK != status)
121  {
122  PRINTF("\r\n SPI Power Mode setting Failed\r\n");
123  return -1;
124  }
125 
126  /*! Set the SPI Slave speed. */
127  status = pSPIdriver->Control(ARM_SPI_MODE_MASTER | ARM_SPI_CPOL0_CPHA0, SPI_S_BAUDRATE);
128  if (ARM_DRIVER_OK != status)
129  {
130  PRINTF("\r\n SPI Control Mode setting Failed\r\n");
131  return -1;
132  }
133 
134  /*! Initialize the FXOS8700 sensor driver. */
137  if (SENSOR_ERROR_NONE != status)
138  {
139  PRINTF("\r\n Sensor Initialization Failed\r\n");
140  return -1;
141  }
142  PRINTF("\r\n Successfully Initiliazed Sensor\r\n");
143 
144  /*! Set the task to be executed while waiting for SPI transactions to complete. */
146 
147  /*! Configure the FXAS21002 sensor driver. */
148  status = FXOS8700_SPI_Configure(&fxos8700Driver, fxos8700_Config_Hybrid);
149  if (SENSOR_ERROR_NONE != status)
150  {
151  PRINTF("\r\n FXOS8700 Sensor Configuration Failed, Err = %d\r\n", status);
152  return -1;
153  }
154  PRINTF("\r\n Successfully Applied FXOS8700 Sensor Configuration\r\n");
155 
156  for (;;) /* Forever loop */
157  {
158  /*! Wait for data ready from the FXAS21002. */
159  status = FXOS8700_SPI_ReadData(&fxos8700Driver, FXOS8700_STATUS_READ, &dataReady);
160  if (0 == (dataReady & FXOS8700_DR_STATUS_ZYXDR_MASK))
161  { /* Loop, if new sample is not available. */
162  continue;
163  }
164 
165  /*! Read the raw sensor data from the fxos8700. */
166  status = FXOS8700_SPI_ReadData(&fxos8700Driver, FXOS8700_ACCELMAG_READ, data);
167  if (ARM_DRIVER_OK != status)
168  {
169  PRINTF("\r\n Read Failed. \r\n");
170  return -1;
171  }
172 
173  /*! Convert the raw sensor data for display to the debug port. */
174  rawData.accel[0] = ((uint16_t)data[0] << 8) | data[1];
175  rawData.accel[0] /= 4;
176  rawData.accel[1] = ((uint16_t)data[2] << 8) | data[3];
177  rawData.accel[1] /= 4;
178  rawData.accel[2] = ((uint16_t)data[4] << 8) | data[5];
179  rawData.accel[2] /= 4;
180  rawData.mag[0] = ((uint16_t)data[6] << 8) | data[7];
181  rawData.mag[1] = ((uint16_t)data[8] << 8) | data[9];
182  rawData.mag[2] = ((uint16_t)data[10] << 8) | data[11];
183 
184  /* NOTE: PRINTF is relatively expensive in terms of CPU time, specially when used with-in execution loop. */
185  PRINTF("\r\n Accel X = %d Y = %d Z = %d \r\n", rawData.accel[0], rawData.accel[1], rawData.accel[2]);
186  PRINTF("\r\n Mag X = %d Y = %d Z = %d \r\n", rawData.mag[0], rawData.mag[1], rawData.mag[2]);
187  }
188 }
#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
#define BOARD_BootClockRUN
Definition: clock_config.h:45
#define FXOS8700_CTRL_REG1_DR_SINGLE_12P5_HZ
Definition: fxos8700.h:1549
#define FXOS8700_CS
const registerreadlist_t FXOS8700_STATUS_READ[]
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
#define FXOS8700_M_CTRL_REG2_M_RST_CNT_DISABLE
Definition: fxos8700.h:2693
#define __END_READ_DATA__
Definition: sensor_drv.h:77
#define FXOS8700_WHO_AM_I_PROD_VALUE
Definition: fxos8700.h:172
const registerwritelist_t fxos8700_Config_Hybrid[]
void FXOS8700_SPI_SetIdleTask(fxos8700_spi_sensorhandle_t *pSensorHandle, registeridlefunction_t idleTask, void *userParam)
: The interface function to set the SPI Idle Task.
Definition: fxos8700_drv.c:142
#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 RAW_ACCELMAG_DATA_SIZE
#define SPI_S_DEVICE_INDEX
Definition: frdm_k64f.h:115
int main(void)
Main function.
#define FXOS8700_M_CTRL_REG2_M_AUTOINC_HYBRID_MODE
Definition: fxos8700.h:2667
int32_t FXOS8700_SPI_ReadData(fxos8700_spi_sensorhandle_t *pSensorHandle, const registerreadlist_t *pReadList, uint8_t *pBuffer)
The interface function to read the sensor data.
Definition: fxos8700_drv.c:195
#define FXOS8700_M_CTRL_REG2_M_RST_CNT_MASK
Definition: fxos8700.h:2648
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
const registerreadlist_t FXOS8700_ACCELMAG_READ[]
#define SPI_S_BAUDRATE
Transfer baudrate - 500k.
Definition: frdm_k64f.h:114
fxls8962_acceldataUser_t rawData
int32_t FXOS8700_SPI_Initialize(fxos8700_spi_sensorhandle_t *pSensorHandle, ARM_DRIVER_SPI *pBus, uint8_t index, void *pSlaveSelect, uint8_t whoAmi)
The interface function to initialize the sensor.
Definition: fxos8700_drv.c:93
#define FXOS8700_DR_STATUS_ZYXDR_MASK
Definition: fxos8700.h:212
This defines the sensor specific information for SPI.
Definition: fxos8700_drv.h:59
#define SPI_S_SIGNAL_EVENT
Definition: frdm_k64f.h:116
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
void BOARD_InitPins(void)
Configures pin routing and optionally pin electrical features.
Definition: pin_mux.c:73
int32_t FXOS8700_SPI_Configure(fxos8700_spi_sensorhandle_t *pSensorHandle, const registerwritelist_t *pRegWriteList)
The interface function to configure he sensor.
Definition: fxos8700_drv.c:150
#define FXOS8700_M_CTRL_REG1_M_HMS_HYBRID_MODE
Definition: fxos8700.h:2621
#define SPI_S_DRIVER
Definition: frdm_k64f.h:113