ISSDK  1.8
IoT Sensing Software Development Kit
fxls8962_fifo_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 fxls8962_fifo_spi.c
11  * @brief The fxls8962_fifo_spi.c file implements the ISSDK FXLS8962 SPI sensor driver
12  * example demonstration for FIFO Mode.
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 "fxls8962_drv.h"
29 #include "systick_utils.h"
30 
31 //-----------------------------------------------------------------------
32 // CMSIS Includes
33 //-----------------------------------------------------------------------
34 #include "Driver_SPI.h"
35 
36 //-----------------------------------------------------------------------
37 // Macros
38 //-----------------------------------------------------------------------
39 #define FXLS8962_DATA_SIZE (6)
40 #define FXLS8962_FIFO_WMRK_SIZE (16)
41 
42 /*******************************************************************************
43  * Constants
44  ******************************************************************************/
45 /*! @brief Register settings for FIFO (buffered) mode. */
47  /* Set Full-scale range as 2G. */
49  /* Set Wake Mode ODR Rate as 12.5Hz. */
51  /* Set Buffering Mode as Stop-when-Full. */
53  /* Set FIFO Water Mark. */
55  /* Enable Interrupts for FIFO Watermark Events. */
58 
59 /*! @brief Address of Raw Accel Data in FIFO Mode. */
62 
63 /*******************************************************************************
64  * Global Variables
65  ******************************************************************************/
66 volatile bool gFxls8962DataReady = false;
67 
68 /*******************************************************************************
69  * Code
70  ******************************************************************************/
71 /*!
72  * @brief The Data ready ISR callback function
73  */
74 void fxls8962_int_data_ready_callback(void *pUserData)
75 { /*! @brief Set flag to indicate Sensor has signalled data ready. */
76  gFxls8962DataReady = true;
77 }
78 
79 /*!
80  * @brief Main function
81  */
82 int main(void)
83 {
87 
89  ARM_DRIVER_SPI *pSPIdriver = &SPI_S_DRIVER;
91 
92  /*! Initialize the MCU hardware. */
97 
98  PRINTF("\r\n ISSDK FXLS8962 sensor driver example demonstration for SPI with FIFO Mode.\r\n");
99 
100  /*! Initialize FXLS8962 pin used by FRDM board */
102 
103  /*! Initialize RGB LED pin used by FRDM board */
104  pGpioDriver->pin_init(&GREEN_LED, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
105 
106  /*! Initialize the SPI driver. */
107  status = pSPIdriver->Initialize(SPI_S_SIGNAL_EVENT);
108  if (ARM_DRIVER_OK != status)
109  {
110  PRINTF("\r\n SPI Initialization Failed\r\n");
111  return -1;
112  }
113 
114  /*! Set the SPI Power mode. */
115  status = pSPIdriver->PowerControl(ARM_POWER_FULL);
116  if (ARM_DRIVER_OK != status)
117  {
118  PRINTF("\r\n SPI Power Mode setting Failed\r\n");
119  return -1;
120  }
121 
122  /*! Set the SPI Slave speed. */
123  status = pSPIdriver->Control(ARM_SPI_MODE_MASTER | ARM_SPI_CPOL0_CPHA0, SPI_S_BAUDRATE);
124  if (ARM_DRIVER_OK != status)
125  {
126  PRINTF("\r\n SPI Control Mode setting Failed\r\n");
127  return -1;
128  }
129 
130  /*! Initialize FXLS8962 sensor driver. */
133  if (SENSOR_ERROR_NONE != status)
134  {
135  PRINTF("\r\n FXLS8962 Sensor Initialization Failed\r\n");
136  return -1;
137  }
138  PRINTF("\r\n Successfully Initiliazed FXLS8962 Sensor\r\n");
139 
140  /*! Set the task to be executed while waiting for SPI transactions to complete. */
142 
143  /*! Configure the FXLS8962 sensor. */
144  status = FXLS8962_SPI_Configure(&fxls8962Driver, cFxls8962ConfigFIFO);
145  if (SENSOR_ERROR_NONE != status)
146  {
147  PRINTF("\r\n FXLS8962 Sensor Configuration Failed, Err = %d\r\n", status);
148  return -1;
149  }
150  PRINTF("\r\n Successfully Applied FXLS8962 Sensor Configuration\r\n");
151 
152  gFxls8962DataReady = false;
153  for (;;) /* Forever loop */
154  { /* In ISR Mode we do not need to check Data Ready Register.
155  * The receipt of interrupt will indicate data is ready. */
156  if (false == gFxls8962DataReady)
157  { /* Loop, if new sample is not available. */
159  continue;
160  }
161  else
162  { /*! Clear the data ready flag, it will be set again by the ISR. */
163  gFxls8962DataReady = false;
164  pGpioDriver->toggle_pin(&GREEN_LED);
165  }
166 
167  /*! Read new raw sensor data from the FXLS8962. */
168  status = FXLS8962_SPI_ReadData(&fxls8962Driver, cFxls8962OutputFIF0, data);
169  if (ARM_DRIVER_OK != status)
170  {
171  PRINTF("\r\n Read Failed. \r\n");
172  return -1;
173  }
174 
175  for (i = 0; i < FXLS8962_FIFO_WMRK_SIZE; i++)
176  { /*! Convert the raw sensor data to signed 16-bit container for display to the debug port. */
177  rawData.accel[0] = ((int16_t)data[i * FXLS8962_DATA_SIZE + 1] << 8) | data[i * FXLS8962_DATA_SIZE + 0];
178  rawData.accel[1] = ((int16_t)data[i * FXLS8962_DATA_SIZE + 3] << 8) | data[i * FXLS8962_DATA_SIZE + 2];
179  rawData.accel[2] = ((int16_t)data[i * FXLS8962_DATA_SIZE + 5] << 8) | data[i * FXLS8962_DATA_SIZE + 4];
180  }
181 
182  /*! Display to the debug port the last sample.
183  * NOTE: PRINTF is relatively expensive in terms of CPU time, specially when used with-in execution loop. */
184  PRINTF("\r\nX=%5d Y=%5d Z=%5d\r\n", rawData.accel[0], rawData.accel[1], rawData.accel[2]);
185  ASK_USER_TO_RESUME(8); /* Ask for user input after processing 8 FIFOs. */
186  }
187 }
#define FXLS8962_BUF_CONFIG1_BUF_MODE_STOP_MODE
Definition: fxls8962.h:1055
This structure defines the fxls8962 raw data buffer.
Definition: fxls8962_drv.h:53
#define FXLS8962_BUF_CONFIG1_BUF_MODE_MASK
Definition: fxls8962.h:1041
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
#define FXLS8962_INT_EN_BUF_EN_EN
Definition: fxls8962.h:859
#define FXLS8962_SENS_CONFIG1_FSR_2G
Definition: fxls8962.h:453
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
The fxls8962_drv.h file describes the FXLS8962AF driver interface and structures. ...
#define FXLS8962_INT1
const registerreadlist_t cFxls8962OutputFIF0[]
Address of Raw Accel Data in FIFO Mode.
#define FXLS8962_DATA_SIZE
int32_t FXLS8962_SPI_ReadData(fxls8962_spi_sensorhandle_t *pSensorHandle, const registerreadlist_t *pReadList, uint8_t *pBuffer)
The interface function to read the sensor data.
Definition: fxls8962_drv.c:176
Access structure of the GPIO Driver.
Definition: Driver_GPIO.h:38
#define SMC
Definition: lpc54114.h:118
#define __END_WRITE_DATA__
Definition: sensor_drv.h:45
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 FXLS8962_SENS_CONFIG3_WAKE_ODR_MASK
Definition: fxls8962.h:551
void FXLS8962_SPI_SetIdleTask(fxls8962_spi_sensorhandle_t *pSensorHandle, registeridlefunction_t idleTask, void *userParam)
: The interface function to set the SPI Idle Task.
Definition: fxls8962_drv.c:123
#define SPI_S_DEVICE_INDEX
Definition: frdm_k64f.h:89
#define BOARD_BootClockRUN
Definition: clock_config.h:19
const registerwritelist_t cFxls8962ConfigFIFO[]
Register settings for FIFO (buffered) mode.
fxls8962_i2c_sensorhandle_t fxls8962Driver
void(* registeridlefunction_t)(void *userParam)
This is the register idle function type.
Definition: sensor_drv.h:97
#define FXLS8962_SENS_CONFIG3_WAKE_ODR_12_5HZ
Definition: fxls8962.h:565
GENERIC_DRIVER_GPIO Driver_GPIO_KSDK
Definition: gpio_driver.c:177
GENERIC_DRIVER_GPIO * pGpioDriver
This defines the sensor specific information for SPI.
Definition: fxls8962_drv.h:33
#define FXLS8962_FIFO_WMRK_SIZE
uint8_t data[FXLS8962_DATA_SIZE]
void fxls8962_int_data_ready_callback(void *pUserData)
The Data ready ISR callback function.
#define __END_READ_DATA__
Definition: sensor_drv.h:51
#define FXLS8962_INT_EN_BUF_EN_MASK
Definition: fxls8962.h:847
fxos8700_accelmagdata_t rawData
int32_t FXLS8962_SPI_Configure(fxls8962_spi_sensorhandle_t *pSensorHandle, const registerwritelist_t *pRegWriteList)
The interface function to configure he sensor.
Definition: fxls8962_drv.c:131
uint16_t readFrom
Definition: sensor_drv.h:80
void(* toggle_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:48
int32_t FXLS8962_SPI_Initialize(fxls8962_spi_sensorhandle_t *pSensorHandle, ARM_DRIVER_SPI *pBus, uint8_t index, void *pSlaveSelect, uint8_t *whoami)
The interface function to initialize the sensor.
Definition: fxls8962_drv.c:68
void BOARD_SystickEnable(void)
Function to enable systicks framework.
Definition: systick_utils.c:35
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:155
void(* pin_init)(pinID_t aPinId, gpio_direction_t dir, void *apPinConfig, gpio_isr_handler_t aIsrHandler, void *apUserData)
Definition: Driver_GPIO.h:41
This structure defines the Read command List.
Definition: sensor_drv.h:78
ARM Systick Utilities.
#define SPI_S_DRIVER
Definition: frdm_k64f.h:87
#define FXLS8962_SENS_CONFIG1_FSR_MASK
Definition: fxls8962.h:423
gpioHandleKSDK_t GREEN_LED
Definition: frdm_k64f.c:188
#define SPI_S_SIGNAL_EVENT
Definition: frdm_k64f.h:90
void BOARD_InitDebugConsole(void)
Definition: board.c:15
#define FXLS8962_WHOAMI_VALUE
Definition: fxls8962.h:87
#define FXLS8962_CS
void BOARD_InitPins(void)
Configures pin routing and optionally pin electrical features.
Definition: pin_mux.c:47
int main(void)
Main function.
#define FXLS8962_BUF_CONFIG2_BUF_WMRK_MASK
Definition: fxls8962.h:1091
volatile bool gFxls8962DataReady