ISSDK  1.8
IoT Sensing Software Development Kit
fxos8700_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 fxos8700_interrupt.c
11  * @brief The fxos8700_interrupt.c file implements the ISSDK FXOS8700 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 // CMSIS Includes
25 //-----------------------------------------------------------------------
26 #include "Driver_I2C.h"
27 
28 //-----------------------------------------------------------------------
29 // ISSDK Includes
30 //-----------------------------------------------------------------------
31 #include "issdk_hal.h"
32 #include "gpio_driver.h"
33 #include "fxos8700_drv.h"
34 
35 /*******************************************************************************
36  * Macro Definitions
37  ******************************************************************************/
38 #define RAW_ACCEL_DATA_SIZE (6)
39 
40 //-----------------------------------------------------------------------
41 // Constants
42 //-----------------------------------------------------------------------
43 /*! Prepare the register write list to configure FXAS21002 in non-FIFO mode. */
45  /*! System and Control registers. */
46  /*! Configure the FXOS8700 to 12.5Hz sampling rate. */
49  FXOS8700_CTRL_REG3_IPOL_MASK | FXOS8700_CTRL_REG3_PP_OD_MASK}, /*! Active High, Push-Pull */
51  FXOS8700_CTRL_REG4_INT_EN_DRDY_MASK}, /*! Data Ready Event. */
53  /*! Configure the fxos8700 to Accel Only */
56 
57 /*! Command definition to read the Data Ready Status */
59 
60 /*! Command definition to read the Accel Data */
63 
64 //-----------------------------------------------------------------------
65 // Global Variables
66 //-----------------------------------------------------------------------
67 volatile bool fxos8700Interrupt = false;
68 
69 //-----------------------------------------------------------------------
70 // Functions
71 //-----------------------------------------------------------------------
72 /*! -----------------------------------------------------------------------
73  * @brief This is the Sensor Data Ready ISR implementation.
74  * @details This function sets the flag which indicates if a new sample(s) is available for reading.
75  * @param[in] pUserData This is a void pointer to the instance of the user specific data structure for the ISR.
76  * @return void There is no return value.
77  * @constraints None
78  * @reeentrant Yes
79  * -----------------------------------------------------------------------*/
80 void fxos8700_isr_callback(void *pUserData)
81 { /*! @brief Set flag to indicate Sensor has signalled data ready. */
82  fxos8700Interrupt = true;
83 }
84 
85 /*! -----------------------------------------------------------------------
86  * @brief This is the The main function implementation.
87  * @details This function invokes board initializes routines, then then brings up the sensor and
88  * finally enters an endless loop to continuously read available samples.
89  * @param[in] void This is no input parameter.
90  * @return void There is no return value.
91  * @constraints None
92  * @reeentrant No
93  * -----------------------------------------------------------------------*/
94 int main(void)
95 {
97  uint8_t data[RAW_ACCEL_DATA_SIZE];
99 
100  ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER; // Now using the shield.h value!!!
101  fxos8700_i2c_sensorhandle_t FXOS8700drv;
102  GENERIC_DRIVER_GPIO *gpioDriver = &Driver_GPIO_KSDK;
103 
104  /*! Initialize the MCU hardware. */
105  BOARD_InitPins();
108 
109  PRINTF("\r\n ISSDK FXOS8700 sensor driver example demonstration with interrupt mode.\r\n");
110 
111  /*! Initialize INT1_FXOS8700 pin used by FRDM board */
112  gpioDriver->pin_init(&FXOS8700_INT2, GPIO_DIRECTION_IN, NULL, &fxos8700_isr_callback, NULL);
113 
114  /*! Initialize RGB LED pin used by FRDM board */
115  gpioDriver->pin_init(&GREEN_LED, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
116 
117  /*! Initialize the I2C driver. */
118  status = I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
119  if (ARM_DRIVER_OK != status)
120  {
121  PRINTF("\r\n I2C Initialization Failed\r\n");
122  return -1;
123  }
124 
125  /*! Set the I2C Power mode. */
126  status = I2Cdrv->PowerControl(ARM_POWER_FULL);
127  if (ARM_DRIVER_OK != status)
128  {
129  PRINTF("\r\n I2C Power Mode setting Failed\r\n");
130  return -1;
131  }
132 
133  /*! Set the I2C bus speed. */
134  status = I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
135  if (ARM_DRIVER_OK != status)
136  {
137  PRINTF("\r\n I2C Control Mode setting Failed\r\n");
138  return -1;
139  }
140 
141  /*! Initialize the FXOS8700 sensor driver. */
144  if (SENSOR_ERROR_NONE != status)
145  {
146  PRINTF("\r\n Sensor Initialization Failed\r\n");
147  return -1;
148  }
149  PRINTF("\r\n Successfully Initiliazed Sensor\r\n");
150 
151  /*! Set the task to be executed while waiting for I2C transactions to complete. */
153 
154  /*! Configure the fxos8700 sensor driver. */
155  status = FXOS8700_I2C_Configure(&FXOS8700drv, fxos8700_Config_Interrupt);
156  if (SENSOR_ERROR_NONE != status)
157  {
158  PRINTF("\r\n FXOS8700 Sensor Configuration Failed, Err = %d\r\n", status);
159  return -1;
160  }
161  PRINTF("\r\n Successfully Applied FXOS8700 Sensor Configuration\r\n");
162 
163  for (;;) /* Forever loop */
164  { /* In ISR Mode we do not need to check Data Ready Register.
165  * The receipt of interrupt will indicate data is ready. */
166  if (false == fxos8700Interrupt)
167  { /* Loop, if new sample is not available. */
169  continue;
170  }
171  else
172  { /*! Clear the data ready flag, it will be set again by the ISR. */
173  fxos8700Interrupt = false;
174  gpioDriver->toggle_pin(&GREEN_LED);
175  }
176 
177  /*! Read the raw sensor data from the fxos8700. */
178  status = FXOS8700_I2C_ReadData(&FXOS8700drv, FXOS8700_ACCEL_READ, data);
179  if (ARM_DRIVER_OK != status)
180  {
181  PRINTF("\r\n Read Failed. \r\n");
182  return -1;
183  }
184 
185  /*! Convert the raw sensor data to signed 16-bit container for display to the debug port. */
186  rawData.accel[0] = ((int16_t)data[0] << 8) | data[1];
187  rawData.accel[0] /= 4;
188  rawData.accel[1] = ((int16_t)data[2] << 8) | data[3];
189  rawData.accel[1] /= 4;
190  rawData.accel[2] = ((int16_t)data[4] << 8) | data[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(50); /* Ask for user input after processing 100 samples. */
196  }
197 }
int32_t FXOS8700_I2C_Initialize(fxos8700_i2c_sensorhandle_t *pSensorHandle, ARM_DRIVER_I2C *pBus, uint8_t index, uint16_t sAddress, uint8_t whoAmi)
The interface function to initialize the sensor.
Definition: fxos8700_drv.c:222
This structure defines the fxos8700 raw data buffer.
Definition: fxos8700_drv.h:53
This structure defines the Write command List.
Definition: sensor_drv.h:68
#define FXOS8700_M_CTRL_REG2_M_AUTOINC_MASK
Definition: fxos8700.h:2634
#define ASK_USER_TO_RESUME(x)
Definition: frdm_k64f.h:106
int32_t status
#define FXOS8700_CTRL_REG5_INT_CFG_DRDY_MASK
Definition: fxos8700.h:1784
#define FXOS8700_CTRL_REG4_INT_EN_DRDY_EN
Definition: fxos8700.h:1755
const registerwritelist_t fxos8700_Config_Interrupt[]
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 FXOS8700_WHO_AM_I_PROD_VALUE
Definition: fxos8700.h:146
#define FXOS8700_CTRL_REG1_DR_MASK
Definition: fxos8700.h:1504
Access structure of the GPIO Driver.
Definition: Driver_GPIO.h:38
#define SMC
Definition: lpc54114.h:118
#define FXOS8700_INT2
#define FXOS8700_CTRL_REG1_DR_SINGLE_12P5_HZ
Definition: fxos8700.h:1523
#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 FXOS8700_CTRL_REG3_PP_OD_MASK
Definition: fxos8700.h:1629
#define I2C_S_SIGNAL_EVENT
Definition: issdk_hal.h:34
This defines the sensor specific information for I2C.
Definition: fxos8700_drv.h:44
#define I2C_S_DRIVER
Definition: issdk_hal.h:33
int32_t FXOS8700_I2C_Configure(fxos8700_i2c_sensorhandle_t *pSensorHandle, const registerwritelist_t *pRegWriteList)
The interface function to configure he sensor.
Definition: fxos8700_drv.c:260
int32_t FXOS8700_I2C_ReadData(fxos8700_i2c_sensorhandle_t *pSensorHandle, const registerreadlist_t *pReadList, uint8_t *pBuffer)
The interface function to read the sensor data.
Definition: fxos8700_drv.c:305
#define FXOS8700_CTRL_REG4_INT_EN_DRDY_MASK
Definition: fxos8700.h:1711
#define BOARD_BootClockRUN
Definition: clock_config.h:19
#define RAW_ACCEL_DATA_SIZE
void(* registeridlefunction_t)(void *userParam)
This is the register idle function type.
Definition: sensor_drv.h:97
GENERIC_DRIVER_GPIO Driver_GPIO_KSDK
Definition: gpio_driver.c:177
uint8_t data[FXLS8962_DATA_SIZE]
#define FXOS8700_CTRL_REG5_INT_CFG_DRDY_INT2
Definition: fxos8700.h:1826
#define __END_READ_DATA__
Definition: sensor_drv.h:51
void fxos8700_isr_callback(void *pUserData)
This is the Sensor Data Ready ISR implementation.
ARM_DRIVER_I2C * I2Cdrv
fxos8700_accelmagdata_t rawData
#define FXOS8700_M_CTRL_REG2_M_AUTOINC_ACCEL_ONLY_MODE
Definition: fxos8700.h:2653
uint16_t readFrom
Definition: sensor_drv.h:80
void(* toggle_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:48
The fxos8700_drv.h file describes the fxos8700 driver interface and structures.
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
const registerreadlist_t FXOS8700_STATUS_READ[]
volatile bool fxos8700Interrupt
gpioHandleKSDK_t GREEN_LED
Definition: frdm_k64f.c:188
#define I2C_S_DEVICE_INDEX
Definition: issdk_hal.h:35
#define FXOS8700_CTRL_REG3_PP_OD_PUSH_PULL
Definition: fxos8700.h:1681
void FXOS8700_I2C_SetIdleTask(fxos8700_i2c_sensorhandle_t *pSensorHandle, registeridlefunction_t idleTask, void *userParam)
: The interface function to set the I2C Idle Task.
Definition: fxos8700_drv.c:252
#define FXOS8700_CTRL_REG3_IPOL_MASK
Definition: fxos8700.h:1632
void BOARD_InitDebugConsole(void)
Definition: board.c:15
#define FXOS8700_I2C_ADDR
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.
#define FXOS8700_CTRL_REG3_IPOL_ACTIVE_HIGH
Definition: fxos8700.h:1680
const registerreadlist_t FXOS8700_ACCEL_READ[]