ISSDK  1.8
IoT Sensing Software Development Kit
fxls8471q_spi_interrupt.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_interrupt.c
11  * @brief The fxls8471q_interrupt.c file implements the ISSDK FXLS8471Q sensor
12  * driver example demonstration with interrupt 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 "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. */
42  /*! Configure settings for interrupt notification. */
43  /*! Active High, Push-Pull */
47  FXLS8471Q_CTRL_REG4_INT_EN_DRDY_MASK}, /*! Data Ready Event. */
50 
51 /*! Prepare the register read list to read the raw accel data from the FXLS8471Q. */
54 
55 //-----------------------------------------------------------------------
56 // Global Variables
57 //-----------------------------------------------------------------------
58 volatile bool gFxls8471qDataReady = false;
59 
60 //-----------------------------------------------------------------------
61 // Functions
62 //-----------------------------------------------------------------------
63 /*! -----------------------------------------------------------------------
64  * @brief This is the Sensor Data Ready ISR implementation.
65  * @details This function sets the flag which indicates if a new sample(s) is available for reading.
66  * @param[in] pUserData This is a void pointer to the instance of the user specific data structure for the ISR.
67  * @return void There is no return value.
68  * @constraints None
69  * @reeentrant Yes
70  * -----------------------------------------------------------------------*/
71 void fxls8471q_isr(void *pUserData)
72 { /*! @brief Set flag to indicate Sensor has signalled data ready. */
73  gFxls8471qDataReady = true;
74 }
75 
76 /*! -----------------------------------------------------------------------
77  * @brief This is the The main function implementation.
78  * @details This function invokes board initializes routines, then then brings up the sensor and
79  * finally enters an endless loop to continuously read available samples.
80  * @param[in] void This is no input parameter.
81  * @return void There is no return value.
82  * @constraints None
83  * @reeentrant No
84  * -----------------------------------------------------------------------*/
85 int main(void)
86 {
90 
91  ARM_DRIVER_SPI *pSPIdriver = &SPI_S_DRIVER;
92  fxls8471q_spi_sensorhandle_t fxls8471qDriver;
94 
95  /*! Initialize the MCU hardware. */
99 
100  PRINTF("\r\n ISSDK FXLS8471Q sensor driver example demonstration with interrupt mode.\r\n");
101 
102  /*! Initialize INT1_FXLS8471Q pin used by board */
103  gpioDriver->pin_init(&FXLS8471_INT1, GPIO_DIRECTION_IN, NULL, &fxls8471q_isr, NULL);
104  /*! Initialize RGB LED pin used by board */
105  gpioDriver->pin_init(&GREEN_LED, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
106 
107  /*! Initialize the SPI driver. */
108  status = pSPIdriver->Initialize(SPI_S_SIGNAL_EVENT);
109  if (ARM_DRIVER_OK != status)
110  {
111  PRINTF("\r\n SPI Initialization Failed\r\n");
112  return -1;
113  }
114 
115  /*! Set the SPI Power mode. */
116  status = pSPIdriver->PowerControl(ARM_POWER_FULL);
117  if (ARM_DRIVER_OK != status)
118  {
119  PRINTF("\r\n SPI Power Mode setting Failed\r\n");
120  return -1;
121  }
122 
123  /*! Set the SPI Slave speed. */
124 #ifndef FRDM_KL27Z
125  status = pSPIdriver->Control(ARM_SPI_MODE_MASTER | ARM_SPI_CPOL1_CPHA0, SPI_S_BAUDRATE);
126 #else
127  status = pSPIdriver->Control(ARM_SPI_MODE_MASTER | ARM_SPI_SS_MASTER_HW_OUTPUT, SPI_S_BAUDRATE);
128 #endif
129  if (ARM_DRIVER_OK != status)
130  {
131  PRINTF("\r\n SPI Control Mode setting Failed\r\n");
132  return -1;
133  }
134 
135  /*! Initialize the FXLS8471Q sensor driver. */
138  if (SENSOR_ERROR_NONE != status)
139  {
140  PRINTF("\r\n FXLS8471Q Sensor Initialization Failed\r\n");
141  return -1;
142  }
143  PRINTF("\r\n Successfully Initiliazed FXLS8471Q Sensor\r\n");
144 
145  /*! Set the task to be executed while waiting for SPI transactions to complete. */
147 
148  /*! Configure the FXLS8471Q sensor driver. */
149  status = FXLS8471Q_SPI_Configure(&fxls8471qDriver, cFxls8471q_Config_Isr);
150  if (SENSOR_ERROR_NONE != status)
151  {
152  PRINTF("\r\n FXLS8471Q Sensor Configuration Failed, Err = %d\r\n", status);
153  return -1;
154  }
155  PRINTF("\r\n Successfully Applied FXLS8471Q Sensor Configuration\r\n");
156 
157  for (;;) /* Forever loop */
158  { /* In ISR Mode we do not need to check Data Ready Register.
159  * The receipt of interrupt will indicate data is ready. */
160  if (false == gFxls8471qDataReady)
161  { /* Loop, if new sample is not available. */
163  continue;
164  }
165  else
166  { /*! Clear the data ready flag, it will be set again by the ISR. */
167  gFxls8471qDataReady = false;
168  gpioDriver->toggle_pin(&GREEN_LED);
169  }
170 
171  /*! Read the raw sensor data from the FXLS8471Q. */
172  status = FXLS8471Q_SPI_ReadData(&fxls8471qDriver, cFxls8471q_Output_Values, data);
173  if (ARM_DRIVER_OK != status)
174  {
175  PRINTF("\r\n Read Failed. \r\n");
176  return -1;
177  }
178 
179  /*! Convert the raw sensor data to signed 16-bit container for display to the debug port. */
180  rawData.accel[0] = ((int16_t)data[0] << 8) | (int16_t)data[1];
181  rawData.accel[0] /= 4;
182  rawData.accel[1] = ((int16_t)data[2] << 8) | (int16_t)data[3];
183  rawData.accel[1] /= 4;
184  rawData.accel[2] = ((int16_t)data[4] << 8) | (int16_t)data[5];
185  rawData.accel[2] /= 4;
186 
187  /* NOTE: PRINTF is relatively expensive in terms of CPU time, specially when used with-in execution loop. */
188  PRINTF("\r\n Accel X = %d Y = %d Z = %d\r\n", rawData.accel[0], rawData.accel[1], rawData.accel[2]);
189  ASK_USER_TO_RESUME(100); /* Ask for user input after processing 100 samples. */
190  }
191 }
#define FXLS8471Q_CTRL_REG5_INT_CFG_DRDY_MASK
Definition: fxls8471q.h:1946
This defines the sensor specific information for SPI.
Definition: fxls8471q_drv.h:31
#define FXLS8471_SPI_CS
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.
#define FXLS8471Q_CTRL_REG4_INT_EN_DRDY_MASK
Definition: fxls8471q.h:1864
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_CTRL_REG3_PP_OD_PUSHPULL
Definition: fxls8471q.h:1804
#define FXLS8471Q_CTRL_REG5_INT_CFG_DRDY_INT1
Definition: fxls8471q.h:1975
#define FXLS8471Q_ACCEL_DATA_SIZE
The size of the FXLS8471Q accel data.
Definition: fxls8471q_drv.h:48
Access structure of the GPIO Driver.
Definition: Driver_GPIO.h:38
#define SMC
Definition: lpc54114.h:118
#define FXLS8471Q_CTRL_REG3_PP_OD_MASK
Definition: fxls8471q.h:1776
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
#define FXLS8471Q_CTRL_REG4_INT_EN_DRDY_ENABLED
Definition: fxls8471q.h:1893
#define FXLS8471_INT1
#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
GENERIC_DRIVER_GPIO Driver_GPIO_KSDK
Definition: gpio_driver.c:177
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_CTRL_REG3_IPOL_MASK
Definition: fxls8471q.h:1779
fxos8700_accelmagdata_t rawData
const registerwritelist_t cFxls8471q_Config_Isr[]
#define FXLS8471Q_CTRL_REG3_IPOL_HIGH
Definition: fxls8471q.h:1807
uint16_t readFrom
Definition: sensor_drv.h:80
void(* toggle_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:48
#define FXLS8471Q_CTRL_REG1_DR_MASK
Definition: fxls8471q.h:1648
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
#define SPI_S_DRIVER
Definition: frdm_k64f.h:87
gpioHandleKSDK_t GREEN_LED
Definition: frdm_k64f.c:188
#define SPI_S_SIGNAL_EVENT
Definition: frdm_k64f.h:90
const registerreadlist_t cFxls8471q_Output_Values[]
void fxls8471q_isr(void *pUserData)
This is the Sensor Data Ready ISR implementation.
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
int main(void)
This is the The main function implementation.
int32_t FXLS8471Q_SPI_Configure(fxls8471q_spi_sensorhandle_t *pSensorHandle, const registerwritelist_t *pRegWriteList)
The interface function to configure he sensor.