ISSDK  1.7
IoT Sensing Software Development Kit
fxls8471q_spi_fifo.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 fxls8471q_spi_fifo.c
37  * @brief The fxls8471q_fifo.c file implements the ISSDK FXLS8471Q sensor driver
38  * example demonstration for SPI mode with FIFOs.
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 "fxls8471q_drv.h"
55 
56 //-----------------------------------------------------------------------
57 // CMSIS Includes
58 //-----------------------------------------------------------------------
59 #include "Driver_SPI.h"
60 
61 //-----------------------------------------------------------------------
62 // Macro Definitions
63 //-----------------------------------------------------------------------
64 /*! @def FIFO_SIZE
65  * @brief The watermark value configured for FXLS8471Q FIFO Buffer.
66  */
67 #define FIFO_SIZE 16
68 
69 //-----------------------------------------------------------------------
70 // Constants
71 //-----------------------------------------------------------------------
72 /*! Prepare the register write list to configure FXLS8471Q in FIFO mode. */
74  /*! Configure CTRL_REG1 register to put FXLS8471Q to 12.5Hz sampling rate. */
76  /*! Configure F_SETUP register to set FIFO in Stop mode, Watermark of 16 */
80 
81 /*! Prepare the register read list to read FXLS8471Q FIFO event. */
83 
84 /*! Prepare the register read list to read the raw accel data from the FXLS8471Q. */
87 
88 //-----------------------------------------------------------------------
89 // Functions
90 //-----------------------------------------------------------------------
91 /*! -----------------------------------------------------------------------
92  * @brief This is the The main function implementation.
93  * @details This function invokes board initializes routines, then then brings up the sensor and
94  * finally enters an endless loop to continuously read available samples.
95  * @param[in] void This is no input parameter.
96  * @return void There is no return value.
97  * @constraints None
98  * @reeentrant No
99  * -----------------------------------------------------------------------*/
100 int main(void)
101 {
102  int32_t status;
103  uint8_t fifoStatus;
106 
107  ARM_DRIVER_SPI *pSPIdriver = &SPI_S_DRIVER;
108  fxls8471q_spi_sensorhandle_t fxls8471qDriver;
109 
110  /*! Initialize the MCU hardware. */
111  BOARD_InitPins();
114 
115  PRINTF("\r\n ISSDK FXLS8471Q sensor driver example demonstration with fifo mode\r\n");
116 
117  /*! Initialize the SPI driver. */
118  status = pSPIdriver->Initialize(SPI_S_SIGNAL_EVENT);
119  if (ARM_DRIVER_OK != status)
120  {
121  PRINTF("\r\n SPI Initialization Failed\r\n");
122  return -1;
123  }
124 
125  /*! Set the SPI Power mode. */
126  status = pSPIdriver->PowerControl(ARM_POWER_FULL);
127  if (ARM_DRIVER_OK != status)
128  {
129  PRINTF("\r\n SPI Power Mode setting Failed\r\n");
130  return -1;
131  }
132 
133  /*! Set the SPI Slave speed. */
134  status = pSPIdriver->Control(ARM_SPI_MODE_MASTER | ARM_SPI_CPOL0_CPHA0, SPI_S_BAUDRATE);
135  if (ARM_DRIVER_OK != status)
136  {
137  PRINTF("\r\n SPI Control Mode setting Failed\r\n");
138  return -1;
139  }
140 
141  /*! Initialize the FXLS8471Q sensor driver. */
144  if (SENSOR_ERROR_NONE != status)
145  {
146  PRINTF("\r\n FXLS8471Q Sensor Initialization Failed\r\n");
147  return -1;
148  }
149  PRINTF("\r\n Successfully Initiliazed FXLS8471Q Sensor\r\n");
150 
151  /*! Set the task to be executed while waiting for SPI transactions to complete. */
153 
154  /*! Configure the FXLS8471Q sensor driver. */
155  status = FXLS8471Q_SPI_Configure(&fxls8471qDriver, cFxls8471q_Config_with_Fifo);
156  if (SENSOR_ERROR_NONE != status)
157  {
158  PRINTF("\r\n FXLS8471Q Sensor Configuration Failed, Err = %d\r\n", status);
159  return -1;
160  }
161  PRINTF("\r\n Successfully Applied FXLS8471Q Sensor Configuration\r\n");
162 
163  for (;;) /* Forever loop */
164  {
165  /*! Wait for the FIFO watermark event. */
166  status = FXLS8471Q_SPI_ReadData(&fxls8471qDriver, cFxls8471q_Fifo_Event, &fifoStatus);
167  if (0 == (fifoStatus & FXLS8471Q_F_STATUS_F_WMRK_FLAG_MASK))
168  {
169  continue;
170  }
171 
172  /*! Read the raw sensor data from the FXLS8471Q. */
173  status = FXLS8471Q_SPI_ReadData(&fxls8471qDriver, cFxls8471q_Output_Values, data);
174  if (ARM_DRIVER_OK != status)
175  {
176  PRINTF("\r\nRead Failed.\r\n");
177  return -1; /* Read did not work, exit. */
178  }
179 
180  for (uint8_t i = 0; i < FIFO_SIZE; i++)
181  {
182  /*! Convert the raw sensor data to signed 16-bit container for display to the debug port. */
183  rawData.accel[0] =
184  ((int16_t)data[i * FXLS8471Q_ACCEL_DATA_SIZE + 0] << 8) | data[i * FXLS8471Q_ACCEL_DATA_SIZE + 1];
185  rawData.accel[0] /= 4;
186  rawData.accel[1] =
187  ((int16_t)data[i * FXLS8471Q_ACCEL_DATA_SIZE + 2] << 8) | data[i * FXLS8471Q_ACCEL_DATA_SIZE + 3];
188  rawData.accel[1] /= 4;
189  rawData.accel[2] =
190  ((int16_t)data[i * FXLS8471Q_ACCEL_DATA_SIZE + 4] << 8) | data[i * FXLS8471Q_ACCEL_DATA_SIZE + 5];
191  rawData.accel[2] /= 4;
192  }
193  /* NOTE: PRINTF is relatively expensive in terms of CPU time, specially when used with-in execution loop. */
194  PRINTF("\r\n Accel X = %d Y = %d Z = %d \r\n", rawData.accel[0], rawData.accel[1], rawData.accel[2]);
195  ASK_USER_TO_RESUME(96 / FIFO_SIZE); /* Ask for user input after processing 96 samples. */
196  }
197 }
#define __END_WRITE_DATA__
Definition: sensor_drv.h:71
The fxls8471q_drv.h file describes the fxls8471q driver interface and structures. ...
#define BOARD_BootClockRUN
Definition: clock_config.h:45
int main(void)
This is the The main function implementation.
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:93
uint8_t data[FXLS8962_DATA_SIZE]
int32_t status
#define FXLS8471Q_F_STATUS_F_WMRK_FLAG_MASK
Definition: fxls8471q.h:222
const registerreadlist_t cFxls8471q_Output_Values[]
void BOARD_InitDebugConsole(void)
Definition: board.c:41
#define FXLS8471Q_CTRL_REG1_DR_MASK
Definition: fxls8471q.h:1674
#define FXLS8471Q_WHO_AM_I_WHOAMI_VALUE
Definition: fxls8471q.h:538
void(* registeridlefunction_t)(void *userParam)
This is the register idle function type.
Definition: sensor_drv.h:123
#define FXLS8471Q_F_SETUP_F_MODE_FIFOSTOP
Definition: fxls8471q.h:332
#define __END_READ_DATA__
Definition: sensor_drv.h:77
void FXLS8471Q_SPI_SetIdleTask(fxls8471q_spi_sensorhandle_t *pSensorHandle, registeridlefunction_t idleTask, void *userParam)
: The interface function to set the SPI Idle Task.
#define FXLS8471Q_F_SETUP_F_WMRK_SHIFT
Definition: fxls8471q.h:320
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.
const registerwritelist_t cFxls8471q_Config_with_Fifo[]
#define ASK_USER_TO_RESUME(x)
Definition: frdm_k64f.h:132
#define SPI_S_DEVICE_INDEX
Definition: frdm_k64f.h:115
const registerreadlist_t cFxls8471q_Fifo_Event[]
This structure defines the fxls8471q raw data buffer.
Definition: fxls8471q_drv.h:66
#define FIFO_SIZE
The watermark value configured for FXLS8471Q FIFO Buffer.
#define FXLS8471Q_F_SETUP_F_MODE_MASK
Definition: fxls8471q.h:322
#define FXLS8471Q_ACCEL_DATA_SIZE
The size of the FXLS8471Q accel data.
Definition: fxls8471q_drv.h:74
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
#define FXLS8471Q_F_SETUP_F_WMRK_MASK
Definition: fxls8471q.h:319
#define FXLS8471_SPI_CS
This defines the sensor specific information for SPI.
Definition: fxls8471q_drv.h:57
int32_t FXLS8471Q_SPI_Configure(fxls8471q_spi_sensorhandle_t *pSensorHandle, const registerwritelist_t *pRegWriteList)
The interface function to configure he sensor.
#define SPI_S_BAUDRATE
Transfer baudrate - 500k.
Definition: frdm_k64f.h:114
fxls8962_acceldataUser_t rawData
#define SPI_S_SIGNAL_EVENT
Definition: frdm_k64f.h:116
This structure defines the Write command List.
Definition: sensor_drv.h:94
#define FXLS8471Q_CTRL_REG1_DR_12DOT5HZ
Definition: fxls8471q.h:1695
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
#define SPI_S_DRIVER
Definition: frdm_k64f.h:113