ISSDK  1.8
IoT Sensing Software Development Kit
fxls8471q_spi_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 fxls8471q_spi_poll.c
11  * @brief The fxls8471q_poll_spi.c file implements the ISSDK FXLS8471Q sensor driver
12  * example demonstration for SPI Mode with polling.
13  */
14 
15 //-----------------------------------------------------------------------
16 // SDK Includes
17 //-----------------------------------------------------------------------
18 #include "pin_mux.h"
19 #include "clock_config.h"
20 #include "board.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 "fxls8471q_drv.h"
29 
30 //-----------------------------------------------------------------------
31 // CMSIS Includes
32 //-----------------------------------------------------------------------
33 #include "Driver_SPI.h"
34 
35 //-----------------------------------------------------------------------
36 // Constants
37 //-----------------------------------------------------------------------
38 /*! Prepare the register write list to configure FXLS8471Q in non-FIFO mode. */
40  /*! Configure CTRL_REG1 register to put FXLS8471Q to 12.5Hz sampling rate. */
43 
44 /*! Prepare the register read list to read FXLS8471Q DataReady status. */
46 
47 /*! Prepare the register read list to read the raw accel data from the FXLS8471Q. */
50 
51 //-----------------------------------------------------------------------
52 // Functions
53 //-----------------------------------------------------------------------
54 /*! -----------------------------------------------------------------------
55  * @brief This is the The main function implementation.
56  * @details This function invokes board initializes routines, then then brings up the sensor and
57  * finally enters an endless loop to continuously read available samples.
58  * @param[in] void This is no input parameter.
59  * @return void There is no return value.
60  * @constraints None
61  * @reeentrant No
62  * -----------------------------------------------------------------------*/
63 int main(void)
64 {
66  uint8_t gFxls8471qDataReady;
69 
70  ARM_DRIVER_SPI *pSPIdriver = &SPI_S_DRIVER;
71  fxls8471q_spi_sensorhandle_t fxls8471qDriver;
72 
73  /*! Initialize the MCU hardware. */
77 
78  PRINTF("\r\n ISSDK FXLS8471Q sensor driver example demonstration for SPI with Poll Mode.\r\n");
79 
80  /*! Initialize the SPI driver. */
81  status = pSPIdriver->Initialize(SPI_S_SIGNAL_EVENT);
82  if (ARM_DRIVER_OK != status)
83  {
84  PRINTF("\r\n SPI Initialization Failed\r\n");
85  return -1;
86  }
87 
88  /*! Set the SPI Power mode. */
89  status = pSPIdriver->PowerControl(ARM_POWER_FULL);
90  if (ARM_DRIVER_OK != status)
91  {
92  PRINTF("\r\n SPI Power Mode setting Failed\r\n");
93  return -1;
94  }
95 
96  /*! Set the SPI Slave speed. */
97 #ifndef FRDM_KL27Z
98  status = pSPIdriver->Control(ARM_SPI_MODE_MASTER | ARM_SPI_CPOL1_CPHA0, SPI_S_BAUDRATE);
99 #else
100  status = pSPIdriver->Control(ARM_SPI_MODE_MASTER | ARM_SPI_SS_MASTER_HW_OUTPUT, SPI_S_BAUDRATE);
101 #endif
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 FXLS8471Q sensor driver. */
111  if (SENSOR_ERROR_NONE != status)
112  {
113  PRINTF("\r\n FXLS8471Q Sensor Initialization Failed\r\n");
114  return -1;
115  }
116  PRINTF("\r\n Successfully Initiliazed FXLS8471Q Sensor\r\n");
117 
118  /*! Set the task to be executed while waiting for SPI transactions to complete. */
120 
121  /*! Configure the FXLS8471Q sensor driver. */
122  status = FXLS8471Q_SPI_Configure(&fxls8471qDriver, cFxls8471q_Config_Normal);
123  if (SENSOR_ERROR_NONE != status)
124  {
125  PRINTF("\r\n FXLS8471Q Sensor Configuration Failed, Err = %d\r\n", status);
126  return -1;
127  }
128  PRINTF("\r\n Successfully Applied FXLS8471Q Sensor Configuration\r\n");
129 
130  for (;;) /* Forever loop */
131  {
132  /*! Wait for data ready from the FXLS8471Q. */
133  status = FXLS8471Q_SPI_ReadData(&fxls8471qDriver, cFxls8471q_DataReady, &gFxls8471qDataReady);
134  if (0 == (gFxls8471qDataReady & FXLS8471Q_STATUS_ZYXDR_MASK))
135  { /* Loop, if new sample is not available. */
136  continue;
137  }
138 
139  /*! Read the raw sensor data from the FXLS8471Q. */
140  status = FXLS8471Q_SPI_ReadData(&fxls8471qDriver, cFxls8471q_Output_Values, data);
141  if (ARM_DRIVER_OK != status)
142  {
143  PRINTF("\r\nRead Failed.\r\n");
144  return -1; /* Read did not work, exit. */
145  }
146 
147  /*! Convert the raw sensor data to signed 16-bit container for display to the debug port. */
148  rawData.accel[0] = ((int16_t)data[0] << 8) | (int16_t)data[1];
149  rawData.accel[0] /= 4;
150  rawData.accel[1] = ((int16_t)data[2] << 8) | (int16_t)data[3];
151  rawData.accel[1] /= 4;
152  rawData.accel[2] = ((int16_t)data[4] << 8) | (int16_t)data[5];
153  rawData.accel[2] /= 4;
154 
155  /* NOTE: PRINTF is relatively expensive in terms of CPU time, specially when used with-in execution loop. */
156  PRINTF("\r\n Accel X = %d Y = %d Z = %d \r\n", rawData.accel[0], rawData.accel[1], rawData.accel[2]);
157  ASK_USER_TO_RESUME(100); /* Ask for user input after processing 100 samples. */
158  }
159 }
This defines the sensor specific information for SPI.
Definition: fxls8471q_drv.h:31
#define FXLS8471_SPI_CS
const registerwritelist_t cFxls8471q_Config_Normal[]
This structure defines the Write command List.
Definition: sensor_drv.h:68
#define ASK_USER_TO_RESUME(x)
Definition: frdm_k64f.h:106
int32_t status
int32_t FXLS8471Q_SPI_Initialize(fxls8471q_spi_sensorhandle_t *pSensorHandle, ARM_DRIVER_SPI *pBus, uint8_t index, void *pSlaveSelect, uint8_t whoAmi)
The interface function to initialize the sensor for I2C.
Definition: fxls8471q_drv.c:67
int32_t FXLS8471Q_SPI_ReadData(fxls8471q_spi_sensorhandle_t *pSensorHandle, const registerreadlist_t *pReadList, uint8_t *pBuffer)
The interface function to read the sensor data.
volatile bool gFxls8471qDataReady
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 FXLS8471Q_ACCEL_DATA_SIZE
The size of the FXLS8471Q accel data.
Definition: fxls8471q_drv.h:48
#define SMC
Definition: lpc54114.h:118
The fxls8471q_drv.h file describes the fxls8471q driver interface and structures. ...
void FXLS8471Q_SPI_SetIdleTask(fxls8471q_spi_sensorhandle_t *pSensorHandle, registeridlefunction_t idleTask, void *userParam)
: The interface function to set the SPI Idle Task.
const registerreadlist_t cFxls8471q_Output_Values[]
#define __END_WRITE_DATA__
Definition: sensor_drv.h:45
#define FXLS8471Q_WHO_AM_I_WHOAMI_VALUE
Definition: fxls8471q.h:512
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
int main(void)
This is the The main function implementation.
const registerreadlist_t cFxls8471q_DataReady[]
#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
This structure defines the fxls8471q raw data buffer.
Definition: fxls8471q_drv.h:40
uint8_t data[FXLS8962_DATA_SIZE]
#define FXLS8471Q_CTRL_REG1_DR_12DOT5HZ
Definition: fxls8471q.h:1669
#define __END_READ_DATA__
Definition: sensor_drv.h:51
fxos8700_accelmagdata_t rawData
uint16_t readFrom
Definition: sensor_drv.h:80
#define FXLS8471Q_CTRL_REG1_DR_MASK
Definition: fxls8471q.h:1648
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
void BOARD_InitDebugConsole(void)
Definition: board.c:15
void BOARD_InitPins(void)
Configures pin routing and optionally pin electrical features.
Definition: pin_mux.c:47
int32_t FXLS8471Q_SPI_Configure(fxls8471q_spi_sensorhandle_t *pSensorHandle, const registerwritelist_t *pRegWriteList)
The interface function to configure he sensor.
#define FXLS8471Q_STATUS_ZYXDR_MASK
Definition: fxls8471q.h:135