ISSDK  1.7
IoT Sensing Software Development Kit
fxls8471q_spi_interrupt.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_interrupt.c
37  * @brief The fxls8471q_interrupt.c file implements the ISSDK FXLS8471Q sensor
38  * driver example demonstration with interrupt mode.
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 // Constants
63 //-----------------------------------------------------------------------
64 /*! Prepare the register write list to configure FXLS8471Q in non-FIFO mode. */
66  /*! Configure CTRL_REG1 register to put FXLS8471Q to 12.5Hz sampling rate. */
68  /*! Configure settings for interrupt notification. */
69  /*! Active High, Push-Pull */
73  FXLS8471Q_CTRL_REG4_INT_EN_DRDY_MASK}, /*! Data Ready Event. */
76 
77 /*! Prepare the register read list to read the raw accel data from the FXLS8471Q. */
80 
81 //-----------------------------------------------------------------------
82 // Global Variables
83 //-----------------------------------------------------------------------
84 volatile bool gFxls8471qDataReady = false;
85 
86 //-----------------------------------------------------------------------
87 // Functions
88 //-----------------------------------------------------------------------
89 /*! -----------------------------------------------------------------------
90  * @brief This is the Sensor Data Ready ISR implementation.
91  * @details This function sets the flag which indicates if a new sample(s) is available for reading.
92  * @param[in] pUserData This is a void pointer to the instance of the user specific data structure for the ISR.
93  * @return void There is no return value.
94  * @constraints None
95  * @reeentrant Yes
96  * -----------------------------------------------------------------------*/
97 void fxls8471q_isr(void *pUserData)
98 { /*! @brief Set flag to indicate Sensor has signalled data ready. */
99  gFxls8471qDataReady = true;
100 }
101 
102 /*! -----------------------------------------------------------------------
103  * @brief This is the The main function implementation.
104  * @details This function invokes board initializes routines, then then brings up the sensor and
105  * finally enters an endless loop to continuously read available samples.
106  * @param[in] void This is no input parameter.
107  * @return void There is no return value.
108  * @constraints None
109  * @reeentrant No
110  * -----------------------------------------------------------------------*/
111 int main(void)
112 {
113  int32_t status;
116 
117  ARM_DRIVER_SPI *pSPIdriver = &SPI_S_DRIVER;
118  fxls8471q_spi_sensorhandle_t fxls8471qDriver;
119  GENERIC_DRIVER_GPIO *gpioDriver = &Driver_GPIO_KSDK;
120 
121  /*! Initialize the MCU hardware. */
122  BOARD_InitPins();
125 
126  PRINTF("\r\n ISSDK FXLS8471Q sensor driver example demonstration with interrupt mode.\r\n");
127 
128  /*! Initialize INT1_FXLS8471Q pin used by board */
129  gpioDriver->pin_init(&FXLS8471_INT1, GPIO_DIRECTION_IN, NULL, &fxls8471q_isr, NULL);
130  /*! Initialize RGB LED pin used by board */
131  gpioDriver->pin_init(&GREEN_LED, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
132 
133  /*! Initialize the SPI driver. */
134  status = pSPIdriver->Initialize(SPI_S_SIGNAL_EVENT);
135  if (ARM_DRIVER_OK != status)
136  {
137  PRINTF("\r\n SPI Initialization Failed\r\n");
138  return -1;
139  }
140 
141  /*! Set the SPI Power mode. */
142  status = pSPIdriver->PowerControl(ARM_POWER_FULL);
143  if (ARM_DRIVER_OK != status)
144  {
145  PRINTF("\r\n SPI Power Mode setting Failed\r\n");
146  return -1;
147  }
148 
149  /*! Set the SPI Slave speed. */
150  status = pSPIdriver->Control(ARM_SPI_MODE_MASTER | ARM_SPI_CPOL0_CPHA0, SPI_S_BAUDRATE);
151  if (ARM_DRIVER_OK != status)
152  {
153  PRINTF("\r\n SPI Control Mode setting Failed\r\n");
154  return -1;
155  }
156 
157  /*! Initialize the FXLS8471Q sensor driver. */
160  if (SENSOR_ERROR_NONE != status)
161  {
162  PRINTF("\r\n FXLS8471Q Sensor Initialization Failed\r\n");
163  return -1;
164  }
165  PRINTF("\r\n Successfully Initiliazed FXLS8471Q Sensor\r\n");
166 
167  /*! Set the task to be executed while waiting for SPI transactions to complete. */
169 
170  /*! Configure the FXLS8471Q sensor driver. */
171  status = FXLS8471Q_SPI_Configure(&fxls8471qDriver, cFxls8471q_Config_Isr);
172  if (SENSOR_ERROR_NONE != status)
173  {
174  PRINTF("\r\n FXLS8471Q Sensor Configuration Failed, Err = %d\r\n", status);
175  return -1;
176  }
177  PRINTF("\r\n Successfully Applied FXLS8471Q Sensor Configuration\r\n");
178 
179  for (;;) /* Forever loop */
180  { /* In ISR Mode we do not need to check Data Ready Register.
181  * The receipt of interrupt will indicate data is ready. */
182  if (false == gFxls8471qDataReady)
183  { /* Loop, if new sample is not available. */
185  continue;
186  }
187  else
188  { /*! Clear the data ready flag, it will be set again by the ISR. */
189  gFxls8471qDataReady = false;
190  gpioDriver->toggle_pin(&GREEN_LED);
191  }
192 
193  /*! Read the raw sensor data from the FXLS8471Q. */
194  status = FXLS8471Q_SPI_ReadData(&fxls8471qDriver, cFxls8471q_Output_Values, data);
195  if (ARM_DRIVER_OK != status)
196  {
197  PRINTF("\r\n Read Failed. \r\n");
198  return -1;
199  }
200 
201  /*! Convert the raw sensor data to signed 16-bit container for display to the debug port. */
202  rawData.accel[0] = ((int16_t)data[0] << 8) | (int16_t)data[1];
203  rawData.accel[0] /= 4;
204  rawData.accel[1] = ((int16_t)data[2] << 8) | (int16_t)data[3];
205  rawData.accel[1] /= 4;
206  rawData.accel[2] = ((int16_t)data[4] << 8) | (int16_t)data[5];
207  rawData.accel[2] /= 4;
208 
209  /* NOTE: PRINTF is relatively expensive in terms of CPU time, specially when used with-in execution loop. */
210  PRINTF("\r\n Accel X = %d Y = %d Z = %d\r\n", rawData.accel[0], rawData.accel[1], rawData.accel[2]);
211  ASK_USER_TO_RESUME(100); /* Ask for user input after processing 100 samples. */
212  }
213 }
#define __END_WRITE_DATA__
Definition: sensor_drv.h:71
The fxls8471q_drv.h file describes the fxls8471q driver interface and structures. ...
void(* pin_init)(pinID_t aPinId, gpio_direction_t dir, void *apPinConfig, gpio_isr_handler_t aIsrHandler, void *apUserData)
Definition: Driver_GPIO.h:67
#define BOARD_BootClockRUN
Definition: clock_config.h:45
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]
#define FXLS8471Q_CTRL_REG3_PP_OD_MASK
Definition: fxls8471q.h:1802
#define FXLS8471Q_CTRL_REG5_INT_CFG_DRDY_INT1
Definition: fxls8471q.h:2001
int32_t status
void(* toggle_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:74
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
volatile bool gFxls8471qDataReady
void(* registeridlefunction_t)(void *userParam)
This is the register idle function type.
Definition: sensor_drv.h:123
const registerreadlist_t cFxls8471q_Output_Values[]
#define FXLS8471Q_CTRL_REG4_INT_EN_DRDY_MASK
Definition: fxls8471q.h:1890
#define __END_READ_DATA__
Definition: sensor_drv.h:77
#define FXLS8471_INT1
void FXLS8471Q_SPI_SetIdleTask(fxls8471q_spi_sensorhandle_t *pSensorHandle, registeridlefunction_t idleTask, void *userParam)
: The interface function to set the SPI Idle Task.
gpioHandleKSDK_t GREEN_LED
Definition: frdm_k64f.c:214
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 ASK_USER_TO_RESUME(x)
Definition: frdm_k64f.h:132
#define FXLS8471Q_CTRL_REG3_IPOL_MASK
Definition: fxls8471q.h:1805
#define SPI_S_DEVICE_INDEX
Definition: frdm_k64f.h:115
#define FXLS8471Q_CTRL_REG3_PP_OD_PUSHPULL
Definition: fxls8471q.h:1830
This structure defines the fxls8471q raw data buffer.
Definition: fxls8471q_drv.h:66
#define FXLS8471Q_CTRL_REG5_INT_CFG_DRDY_MASK
Definition: fxls8471q.h:1972
GENERIC_DRIVER_GPIO Driver_GPIO_KSDK
Definition: gpio_driver.c:203
#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 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.
Access structure of the GPIO Driver.
Definition: Driver_GPIO.h:64
#define SPI_S_BAUDRATE
Transfer baudrate - 500k.
Definition: frdm_k64f.h:114
fxls8962_acceldataUser_t rawData
const registerwritelist_t cFxls8471q_Config_Isr[]
int main(void)
This is the The main function implementation.
void fxls8471q_isr(void *pUserData)
This is the Sensor Data Ready ISR implementation.
#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
#define FXLS8471Q_CTRL_REG3_IPOL_HIGH
Definition: fxls8471q.h:1833
void BOARD_InitPins(void)
Configures pin routing and optionally pin electrical features.
Definition: pin_mux.c:73
#define FXLS8471Q_CTRL_REG4_INT_EN_DRDY_ENABLED
Definition: fxls8471q.h:1919
#define SPI_S_DRIVER
Definition: frdm_k64f.h:113