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 fxas21002_interrupt.c
11 * @brief The fxas21002_interrupt.c file implements the ISSDK FXAS21002 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 "fxas21002_drv.h"
34
35 //-----------------------------------------------------------------------
36 // Constants
37 //-----------------------------------------------------------------------
38 /*! Prepare the register write list to configure FXAS21002 in non-FIFO mode. */
39 const registerwritelist_t fxas21002_Config_Isr[] = {
40 /*! Configure CTRL_REG1 register to put FXAS21002 to 12.5Hz sampling rate. */
41 {FXAS21002_CTRL_REG1, FXAS21002_CTRL_REG1_DR_12_5HZ, FXAS21002_CTRL_REG1_DR_MASK},
42 /*! Configure CTRL_REG2 register to set interrupt configuration settings. */
43 {FXAS21002_CTRL_REG2, FXAS21002_CTRL_REG2_IPOL_ACTIVE_HIGH | FXAS21002_CTRL_REG2_INT_EN_DRDY_ENABLE |
44 FXAS21002_CTRL_REG2_INT_CFG_DRDY_INT1,
45 FXAS21002_CTRL_REG2_IPOL_MASK | FXAS21002_CTRL_REG2_INT_EN_DRDY_MASK | FXAS21002_CTRL_REG2_INT_CFG_DRDY_MASK},
46 __END_WRITE_DATA__};
47
48 /*! Prepare the register read list to read FXAS21002 DataReady status. */
49 const registerreadlist_t fxas21002_DRDY[] = {{.readFrom = FXAS21002_STATUS, .numBytes = 1}, __END_READ_DATA__};
50
51 /*! Prepare the register read list to read the raw gyro data from the FXAS21002. */
52 const registerreadlist_t fxas21002_Output_Values[] = {
53 {.readFrom = FXAS21002_OUT_X_MSB, .numBytes = FXAS21002_GYRO_DATA_SIZE}, __END_READ_DATA__};
54 //-----------------------------------------------------------------------
55 // Global Variables
56 //-----------------------------------------------------------------------
57 volatile bool fxas21002Interrupt = false;
58
59 //-----------------------------------------------------------------------
60 // Functions
61 //-----------------------------------------------------------------------
62 /*! -----------------------------------------------------------------------
63 * @brief This is the Sensor Data Ready ISR implementation.
64 * @details This function sets the flag which indicates if a new sample(s) is available for reading.
65 * @param[in] pUserData This is a void pointer to the instance of the user specific data structure for the ISR.
66 * @return void There is no return value.
67 * @constraints None
68 * @reeentrant Yes
69 * -----------------------------------------------------------------------*/
fxas21002_isr(void * pUserData)70 void fxas21002_isr(void *pUserData)
71 { /*! @brief Set flag to indicate Sensor has signalled data ready. */
72 fxas21002Interrupt = true;
73 }
74
75 /*! -----------------------------------------------------------------------
76 * @brief This is the The main function implementation.
77 * @details This function invokes board initializes routines, then then brings up the sensor and
78 * finally enters an endless loop to continuously read available samples.
79 * @param[in] void This is no input parameter.
80 * @return void There is no return value.
81 * @constraints None
82 * @reeentrant No
83 * -----------------------------------------------------------------------*/
main(void)84 int main(void)
85 {
86 int32_t status;
87 uint8_t data[FXAS21002_GYRO_DATA_SIZE];
88 fxas21002_gyrodata_t rawData;
89
90 ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER; // Now using the shield.h value!!!
91 fxas21002_i2c_sensorhandle_t FXAS21002drv;
92 GENERIC_DRIVER_GPIO *gpioDriver = &Driver_GPIO_KSDK;
93
94 /*! Initialize the MCU hardware. */
95 BOARD_InitPins();
96 BOARD_BootClockRUN();
97 BOARD_InitDebugConsole();
98
99 PRINTF("\r\n ISSDK FXAS21002 sensor driver example demonstration with interrupt mode.\r\n");
100
101 /*! Initialize INT1_FXAS21002 pin used by FRDM board */
102 gpioDriver->pin_init(&FXAS21002_INT1, GPIO_DIRECTION_IN, NULL, &fxas21002_isr, NULL);
103
104 /*! Initialize RGB LED pin used by FRDM board */
105 gpioDriver->pin_init(&GREEN_LED, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
106
107 /*! Initialize the I2C driver. */
108 status = I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
109 if (ARM_DRIVER_OK != status)
110 {
111 PRINTF("\r\n I2C Initialization Failed\r\n");
112 return -1;
113 }
114
115 /*! Set the I2C Power mode. */
116 status = I2Cdrv->PowerControl(ARM_POWER_FULL);
117 if (ARM_DRIVER_OK != status)
118 {
119 PRINTF("\r\n I2C Power Mode setting Failed\r\n");
120 return -1;
121 }
122
123 /*! Set the I2C bus speed. */
124 status = I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
125 if (ARM_DRIVER_OK != status)
126 {
127 PRINTF("\r\n I2C Control Mode setting Failed\r\n");
128 return -1;
129 }
130
131 /*! Initialize the FXAS21002 sensor driver. */
132 status = FXAS21002_I2C_Initialize(&FXAS21002drv, &I2C_S_DRIVER, I2C_S_DEVICE_INDEX, FXAS21002_I2C_ADDR,
133 FXAS21002_WHO_AM_I_WHOAMI_PROD_VALUE);
134 if (SENSOR_ERROR_NONE != status)
135 {
136 PRINTF("\r\n Sensor Initialization Failed\r\n");
137 return -1;
138 }
139 PRINTF("\r\n Successfully Initiliazed Sensor\r\n");
140
141 /*! Set the task to be executed while waiting for I2C transactions to complete. */
142 FXAS21002_I2C_SetIdleTask(&FXAS21002drv, (registeridlefunction_t)SMC_SetPowerModeVlpr, SMC);
143
144 /*! Configure the FXAS21002 sensor driver. */
145 status = FXAS21002_I2C_Configure(&FXAS21002drv, fxas21002_Config_Isr);
146 if (SENSOR_ERROR_NONE != status)
147 {
148 PRINTF("\r\n FXAS21002 Sensor Configuration Failed, Err = %d\r\n", status);
149 return -1;
150 }
151 PRINTF("\r\n Successfully Applied FXAS21002 Sensor Configuration\r\n");
152
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 == fxas21002Interrupt)
157 { /* Loop, if new sample is not available. */
158 SMC_SetPowerModeWait(SMC);
159 continue;
160 }
161 else
162 { /*! Clear the data ready flag, it will be set again by the ISR. */
163 fxas21002Interrupt = false;
164 gpioDriver->toggle_pin(&GREEN_LED);
165 }
166
167 /*! Read the raw sensor data from the FXAS21002. */
168 status = FXAS21002_I2C_ReadData(&FXAS21002drv, fxas21002_Output_Values, data);
169 if (ARM_DRIVER_OK != status)
170 {
171 PRINTF("\r\n Read Failed. \r\n");
172 return -1;
173 }
174
175 /*! Convert the raw sensor data to signed 16-bit container for display to the debug port. */
176 rawData.gyro[0] = ((int16_t)data[0] << 8) | data[1];
177 rawData.gyro[1] = ((int16_t)data[2] << 8) | data[3];
178 rawData.gyro[2] = ((int16_t)data[4] << 8) | data[5];
179
180 /* NOTE: PRINTF is relatively expensive in terms of CPU time, specially when used with-in execution loop. */
181 PRINTF("\r\n Gyro X = %d Y = %d Z = %d\r\n", rawData.gyro[0], rawData.gyro[1], rawData.gyro[2]);
182 ASK_USER_TO_RESUME(100); /* Ask for user input after processing 100 samples. */
183 }
184 }
185