ISSDK  1.8
IoT Sensing Software Development Kit
fxos8700_poll_spi.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_spi.c
11  * @brief The fxos8700_poll_spi.c file implements the ISSDK FXOS8700 SPI sensor driver
12  * example demonstration for SPI Mode with polling.
13  */
14 
15 //-----------------------------------------------------------------------
16 // SDK Includes
17 //-----------------------------------------------------------------------
18 #include "board.h"
19 #include "pin_mux.h"
20 #include "clock_config.h"
21 #include "fsl_debug_console.h"
22 
23 //-----------------------------------------------------------------------
24 // ISSDK Includes
25 //-----------------------------------------------------------------------
26 #include "issdk_hal.h"
27 #include "gpio_driver.h"
28 #include "fxos8700_drv.h"
29 
30 //-----------------------------------------------------------------------
31 // CMSIS Includes
32 //-----------------------------------------------------------------------
33 #include "Driver_SPI.h"
34 
35 /*******************************************************************************
36  * Macro Definitions
37  ******************************************************************************/
38 #define RAW_ACCELMAG_DATA_SIZE (12)
39 
40 /*******************************************************************************
41  * Constants
42  ******************************************************************************/
43 /*! Prepare the register write list to configure FXOS8700 in Hybrid mode. */
45  /*! System and Control registers. */
46  /*! Configure the FXOS8700 to 12.5Hz sampling rate. */
49  FXOS8700_M_CTRL_REG1_M_ACAL_MASK | FXOS8700_M_CTRL_REG1_M_HMS_MASK}, /*! Enable the Hybrid Mode. */
51  FXOS8700_M_CTRL_REG2_M_AUTOINC_MASK | FXOS8700_M_CTRL_REG2_M_RST_CNT_MASK}, /*! Enable the Data read with Hybrid Mode. */
53 
54 /*! Command definition to read the Data Ready Status */
56 
57 /*! Command definition to read the Accel + Mag Data */
60 
61 /*******************************************************************************
62  * Code
63  ******************************************************************************/
64 /*!
65  * @brief Main function
66  */
67 int main(void)
68 {
70  uint8_t dataReady;
73 
74  ARM_DRIVER_SPI *pSPIdriver = &SPI_S_DRIVER;
75  fxos8700_spi_sensorhandle_t fxos8700Driver;
76 
77  /*! Initialize the MCU hardware. */
81 
82  PRINTF("\r\n ISSDK FXOS8700 sensor driver example demonstration for SPI with Poll Mode.\r\n");
83 
84  /*! Initialize the SPI driver. */
85  status = pSPIdriver->Initialize(SPI_S_SIGNAL_EVENT);
86  if (ARM_DRIVER_OK != status)
87  {
88  PRINTF("\r\n SPI Initialization Failed\r\n");
89  return -1;
90  }
91 
92  /*! Set the SPI Power mode. */
93  status = pSPIdriver->PowerControl(ARM_POWER_FULL);
94  if (ARM_DRIVER_OK != status)
95  {
96  PRINTF("\r\n SPI Power Mode setting Failed\r\n");
97  return -1;
98  }
99 
100  /*! Set the SPI Slave speed. */
101  status = pSPIdriver->Control(ARM_SPI_MODE_MASTER | ARM_SPI_CPOL0_CPHA0, SPI_S_BAUDRATE);
102  if (ARM_DRIVER_OK != status)
103  {
104  PRINTF("\r\n SPI Control Mode setting Failed\r\n");
105  return -1;
106  }
107 
108  /*! Initialize the FXOS8700 sensor driver. */
111  if (SENSOR_ERROR_NONE != status)
112  {
113  PRINTF("\r\n Sensor Initialization Failed\r\n");
114  return -1;
115  }
116  PRINTF("\r\n Successfully Initiliazed Sensor\r\n");
117 
118  /*! Set the task to be executed while waiting for SPI transactions to complete. */
120 
121  /*! Configure the FXAS21002 sensor driver. */
122  status = FXOS8700_SPI_Configure(&fxos8700Driver, fxos8700_Config_Hybrid);
123  if (SENSOR_ERROR_NONE != status)
124  {
125  PRINTF("\r\n FXOS8700 Sensor Configuration Failed, Err = %d\r\n", status);
126  return -1;
127  }
128  PRINTF("\r\n Successfully Applied FXOS8700 Sensor Configuration\r\n");
129 
130  for (;;) /* Forever loop */
131  {
132  /*! Wait for data ready from the FXAS21002. */
133  status = FXOS8700_SPI_ReadData(&fxos8700Driver, FXOS8700_STATUS_READ, &dataReady);
134  if (0 == (dataReady & FXOS8700_DR_STATUS_ZYXDR_MASK))
135  { /* Loop, if new sample is not available. */
136  continue;
137  }
138 
139  /*! Read the raw sensor data from the fxos8700. */
140  status = FXOS8700_SPI_ReadData(&fxos8700Driver, FXOS8700_ACCELMAG_READ, data);
141  if (ARM_DRIVER_OK != status)
142  {
143  PRINTF("\r\n Read Failed. \r\n");
144  return -1;
145  }
146 
147  /*! Convert the raw sensor data for display to the debug port. */
148  rawData.accel[0] = ((uint16_t)data[0] << 8) | data[1];
149  rawData.accel[0] /= 4;
150  rawData.accel[1] = ((uint16_t)data[2] << 8) | data[3];
151  rawData.accel[1] /= 4;
152  rawData.accel[2] = ((uint16_t)data[4] << 8) | data[5];
153  rawData.accel[2] /= 4;
154  rawData.mag[0] = ((uint16_t)data[6] << 8) | data[7];
155  rawData.mag[1] = ((uint16_t)data[8] << 8) | data[9];
156  rawData.mag[2] = ((uint16_t)data[10] << 8) | data[11];
157 
158  /* NOTE: PRINTF is relatively expensive in terms of CPU time, specially when used with-in execution loop. */
159  PRINTF("\r\n Accel X = %d Y = %d Z = %d \r\n", rawData.accel[0], rawData.accel[1], rawData.accel[2]);
160  PRINTF("\r\n Mag X = %d Y = %d Z = %d \r\n", rawData.mag[0], rawData.mag[1], rawData.mag[2]);
161  }
162 }
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
int32_t status
status_t SMC_SetPowerModeVlpr(void *arg)
Configures the system to VLPR power mode. API name used from Kinetis family to maintain compatibility...
Definition: lpc54114.c:169
#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
#define FXOS8700_M_CTRL_REG1_M_HMS_HYBRID_MODE
Definition: fxos8700.h:2595
#define SMC
Definition: lpc54114.h:118
#define FXOS8700_CTRL_REG1_DR_SINGLE_12P5_HZ
Definition: fxos8700.h:1523
#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 SPI_S_BAUDRATE
Transfer baudrate - 500k.
Definition: frdm_k64f.h:88
#define SPI_S_DEVICE_INDEX
Definition: frdm_k64f.h:89
#define RAW_ACCELMAG_DATA_SIZE
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:67
#define BOARD_BootClockRUN
Definition: clock_config.h:19
void(* registeridlefunction_t)(void *userParam)
This is the register idle function type.
Definition: sensor_drv.h:97
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:169
#define FXOS8700_M_CTRL_REG1_M_ACAL_EN
Definition: fxos8700.h:2566
#define FXOS8700_M_CTRL_REG1_M_ACAL_MASK
Definition: fxos8700.h:2559
This defines the sensor specific information for SPI.
Definition: fxos8700_drv.h:33
uint8_t data[FXLS8962_DATA_SIZE]
#define __END_READ_DATA__
Definition: sensor_drv.h:51
const registerwritelist_t fxos8700_Config_Hybrid[]
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:124
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:116
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.
This structure defines the Read command List.
Definition: sensor_drv.h:78
#define SPI_S_DRIVER
Definition: frdm_k64f.h:87
#define SPI_S_SIGNAL_EVENT
Definition: frdm_k64f.h:90
int main(void)
Main function.
void BOARD_InitDebugConsole(void)
Definition: board.c:15
#define FXOS8700_CS
const registerreadlist_t FXOS8700_STATUS_READ[]
#define FXOS8700_DR_STATUS_ZYXDR_MASK
Definition: fxos8700.h:186
void BOARD_InitPins(void)
Configures pin routing and optionally pin electrical features.
Definition: pin_mux.c:47
const registerreadlist_t FXOS8700_ACCELMAG_READ[]