ISSDK  1.8
IoT Sensing Software Development Kit
fxls8471q_spi_fifo.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_fifo.c
11  * @brief The fxls8471q_fifo.c file implements the ISSDK FXLS8471Q sensor driver
12  * example demonstration for SPI mode with FIFOs.
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 // Macro Definitions
37 //-----------------------------------------------------------------------
38 /*! @def FIFO_SIZE
39  * @brief The watermark value configured for FXLS8471Q FIFO Buffer.
40  */
41 #define FIFO_SIZE 16
42 
43 //-----------------------------------------------------------------------
44 // Constants
45 //-----------------------------------------------------------------------
46 /*! Prepare the register write list to configure FXLS8471Q in FIFO mode. */
48  /*! Configure CTRL_REG1 register to put FXLS8471Q to 12.5Hz sampling rate. */
50  /*! Configure F_SETUP register to set FIFO in Stop mode, Watermark of 16 */
54 
55 /*! Prepare the register read list to read FXLS8471Q FIFO event. */
57 
58 /*! Prepare the register read list to read the raw accel data from the FXLS8471Q. */
61 
62 //-----------------------------------------------------------------------
63 // Functions
64 //-----------------------------------------------------------------------
65 /*! -----------------------------------------------------------------------
66  * @brief This is the The main function implementation.
67  * @details This function invokes board initializes routines, then then brings up the sensor and
68  * finally enters an endless loop to continuously read available samples.
69  * @param[in] void This is no input parameter.
70  * @return void There is no return value.
71  * @constraints None
72  * @reeentrant No
73  * -----------------------------------------------------------------------*/
74 int main(void)
75 {
77  uint8_t fifoStatus;
80 
81  ARM_DRIVER_SPI *pSPIdriver = &SPI_S_DRIVER;
82  fxls8471q_spi_sensorhandle_t fxls8471qDriver;
83 
84  /*! Initialize the MCU hardware. */
88 
89  PRINTF("\r\n ISSDK FXLS8471Q sensor driver example demonstration with fifo mode\r\n");
90 
91  /*! Initialize the SPI driver. */
92  status = pSPIdriver->Initialize(SPI_S_SIGNAL_EVENT);
93  if (ARM_DRIVER_OK != status)
94  {
95  PRINTF("\r\n SPI Initialization Failed\r\n");
96  return -1;
97  }
98 
99  /*! Set the SPI Power mode. */
100  status = pSPIdriver->PowerControl(ARM_POWER_FULL);
101  if (ARM_DRIVER_OK != status)
102  {
103  PRINTF("\r\n SPI Power Mode setting Failed\r\n");
104  return -1;
105  }
106 
107  /*! Set the SPI Slave speed. */
108 #ifndef FRDM_KL27Z
109  status = pSPIdriver->Control(ARM_SPI_MODE_MASTER | ARM_SPI_CPOL1_CPHA0, SPI_S_BAUDRATE);
110 #else
111  status = pSPIdriver->Control(ARM_SPI_MODE_MASTER | ARM_SPI_SS_MASTER_HW_OUTPUT, SPI_S_BAUDRATE);
112 #endif
113  if (ARM_DRIVER_OK != status)
114  {
115  PRINTF("\r\n SPI Control Mode setting Failed\r\n");
116  return -1;
117  }
118 
119  /*! Initialize the FXLS8471Q sensor driver. */
122  if (SENSOR_ERROR_NONE != status)
123  {
124  PRINTF("\r\n FXLS8471Q Sensor Initialization Failed\r\n");
125  return -1;
126  }
127  PRINTF("\r\n Successfully Initiliazed FXLS8471Q Sensor\r\n");
128 
129  /*! Set the task to be executed while waiting for SPI transactions to complete. */
131 
132  /*! Configure the FXLS8471Q sensor driver. */
133  status = FXLS8471Q_SPI_Configure(&fxls8471qDriver, cFxls8471q_Config_with_Fifo);
134  if (SENSOR_ERROR_NONE != status)
135  {
136  PRINTF("\r\n FXLS8471Q Sensor Configuration Failed, Err = %d\r\n", status);
137  return -1;
138  }
139  PRINTF("\r\n Successfully Applied FXLS8471Q Sensor Configuration\r\n");
140 
141  for (;;) /* Forever loop */
142  {
143  /*! Wait for the FIFO watermark event. */
144  status = FXLS8471Q_SPI_ReadData(&fxls8471qDriver, cFxls8471q_Fifo_Event, &fifoStatus);
145  if (0 == (fifoStatus & FXLS8471Q_F_STATUS_F_WMRK_FLAG_MASK))
146  {
147  continue;
148  }
149 
150  /*! Read the raw sensor data from the FXLS8471Q. */
151  status = FXLS8471Q_SPI_ReadData(&fxls8471qDriver, cFxls8471q_Output_Values, data);
152  if (ARM_DRIVER_OK != status)
153  {
154  PRINTF("\r\nRead Failed.\r\n");
155  return -1; /* Read did not work, exit. */
156  }
157 
158  for (uint8_t i = 0; i < FIFO_SIZE; i++)
159  {
160  /*! Convert the raw sensor data to signed 16-bit container for display to the debug port. */
161  rawData.accel[0] =
162  ((int16_t)data[i * FXLS8471Q_ACCEL_DATA_SIZE + 0] << 8) | data[i * FXLS8471Q_ACCEL_DATA_SIZE + 1];
163  rawData.accel[0] /= 4;
164  rawData.accel[1] =
165  ((int16_t)data[i * FXLS8471Q_ACCEL_DATA_SIZE + 2] << 8) | data[i * FXLS8471Q_ACCEL_DATA_SIZE + 3];
166  rawData.accel[1] /= 4;
167  rawData.accel[2] =
168  ((int16_t)data[i * FXLS8471Q_ACCEL_DATA_SIZE + 4] << 8) | data[i * FXLS8471Q_ACCEL_DATA_SIZE + 5];
169  rawData.accel[2] /= 4;
170  }
171  /* NOTE: PRINTF is relatively expensive in terms of CPU time, specially when used with-in execution loop. */
172  PRINTF("\r\n Accel X = %d Y = %d Z = %d \r\n", rawData.accel[0], rawData.accel[1], rawData.accel[2]);
173  ASK_USER_TO_RESUME(96 / FIFO_SIZE); /* Ask for user input after processing 96 samples. */
174  }
175 }
const registerwritelist_t cFxls8471q_Config_with_Fifo[]
This defines the sensor specific information for SPI.
Definition: fxls8471q_drv.h:31
#define FIFO_SIZE
The watermark value configured for FXLS8471Q FIFO Buffer.
#define FXLS8471_SPI_CS
#define FXLS8471Q_F_SETUP_F_WMRK_MASK
Definition: fxls8471q.h:293
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.
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_F_STATUS_F_WMRK_FLAG_MASK
Definition: fxls8471q.h:196
#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.
#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
const registerreadlist_t cFxls8471q_Fifo_Event[]
#define FXLS8471Q_F_SETUP_F_MODE_FIFOSTOP
Definition: fxls8471q.h:306
#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
int main(void)
This is the The main function implementation.
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
#define FXLS8471Q_F_SETUP_F_WMRK_SHIFT
Definition: fxls8471q.h:294
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
const registerreadlist_t cFxls8471q_Output_Values[]
#define SPI_S_SIGNAL_EVENT
Definition: frdm_k64f.h:90
#define FXLS8471Q_F_SETUP_F_MODE_MASK
Definition: fxls8471q.h:296
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.