ISSDK  1.7
IoT Sensing Software Development Kit
mma865x_interrupt.c
Go to the documentation of this file.
1 /*
2  * The Clear BSD License
3  * Copyright (c) 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 mma865x_interrupt.c
37 * @brief The mma865x_interrupt.c file implements the ISSDK MMA865x sensor driver
38 * 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 // CMSIS Includes
51 //-----------------------------------------------------------------------
52 #include "Driver_I2C.h"
53 
54 //-----------------------------------------------------------------------
55 // ISSDK Includes
56 //-----------------------------------------------------------------------
57 #include "issdk_hal.h"
58 #include "gpio_driver.h"
59 #include "mma865x_drv.h"
60 
61 //-----------------------------------------------------------------------
62 // Macros
63 //-----------------------------------------------------------------------
64 #define MMA865x_ACCEL_DATA_SIZE (6) /* 2 byte X,Y,Z Axis Data each. */
65 
66 //-----------------------------------------------------------------------
67 // Constants
68 //-----------------------------------------------------------------------
69 /*! Prepare the register write list to configure MMA865x in non-FIFO and ISR mode. */
71  {/*! Configure the MMA865x to set FS Range as 2g. */
73  /*! Configure the MMA865x to set ODR to 6.25Hz. */
75  /*! Configure the MMA865x to set High Resolution mode. */
77  /*! Configure the MMA865x to set interrupt polarity as Active High. */
79  /*! Configure the MMA865x to enable Interrupts for Data Ready. */
81  /*! Configure the MMA865x to route Data Ready Interrupts to INT1. */
84 
85 /*! Prepare the register read list to read the raw Accel data from MMA865x. */
88 
89 //-----------------------------------------------------------------------
90 // Global Variables
91 //-----------------------------------------------------------------------
92 volatile bool gMma865xDataReady = false;
93 
94 //-----------------------------------------------------------------------
95 // Functions
96 //-----------------------------------------------------------------------
97 /*! -----------------------------------------------------------------------
98  * @brief This is the Sensor Data Ready ISR implementation.
99  * @details This function sets the flag which indicates if a new sample(s) is available for reading.
100  * @param[in] pUserData This is a void pointer to the instance of the user specific data structure for the ISR.
101  * @return void There is no return value.
102  * @constraints None
103  * @reeentrant Yes
104  * -----------------------------------------------------------------------*/
105 void mma865x_int_data_ready_callback(void *pUserData)
106 { /*! @brief Set flag to indicate Sensor has signalled data ready. */
107  gMma865xDataReady = true;
108 }
109 
110 /*! -----------------------------------------------------------------------
111  * @brief This is the The main function implementation.
112  * @details This function invokes board initializes routines, then then brings up the sensor and
113  * finally enters an endless loop to continuously read available samples.
114  * @param[in] void This is no input parameter.
115  * @return void There is no return value.
116  * @constraints None
117  * @reeentrant No
118  * -----------------------------------------------------------------------*/
119 int main(void)
120 {
121  int32_t status;
122  uint8_t data[MMA865x_ACCEL_DATA_SIZE];
124 
125  ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER; // Now using the shield.h value!!!
126  mma865x_i2c_sensorhandle_t mma865xDriver;
127  GENERIC_DRIVER_GPIO *gpioDriver = &Driver_GPIO_KSDK;
128 
129  /*! Initialize the MCU hardware. */
130  BOARD_InitPins();
133 
134  PRINTF("\r\n ISSDK MMA865x sensor driver example for Interrupt Mode.\r\n");
135 
136  /*! Initialize MMA865x pin used by FRDM board */
138 
139  /*! Initialize RGB LED pin used by FRDM board */
140  gpioDriver->pin_init(&GREEN_LED, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
141 
142  /*! Initialize the I2C driver. */
143  status = I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
144  if (ARM_DRIVER_OK != status)
145  {
146  PRINTF("\r\n I2C Initialization Failed\r\n");
147  return -1;
148  }
149 
150  /*! Set the I2C Power mode. */
151  status = I2Cdrv->PowerControl(ARM_POWER_FULL);
152  if (ARM_DRIVER_OK != status)
153  {
154  PRINTF("\r\n I2C Power Mode setting Failed\r\n");
155  return -1;
156  }
157 
158  /*! Set the I2C bus speed. */
159  status = I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
160  if (ARM_DRIVER_OK != status)
161  {
162  PRINTF("\r\n I2C Control Mode setting Failed\r\n");
163  return -1;
164  }
165 
166  /*! Initialize the MMA865x sensor driver. */
169  if (SENSOR_ERROR_NONE != status)
170  {
171  PRINTF("\r\n Sensor Initialization Failed\r\n");
172  return -1;
173  }
174  PRINTF("\r\n Successfully Initiliazed Sensor\r\n");
175 
176  /*! Set the task to be executed while waiting for I2C transactions to complete. */
178 
179  /*! Configure the MMA865x sensor driver with No FIFO mode. */
180  status = MMA865x_I2C_Configure(&mma865xDriver, cMma865xConfigInterrupt);
181  if (SENSOR_ERROR_NONE != status)
182  {
183  PRINTF("\r\n MMA865x Sensor Configuration Failed, Err = %d \r\n", status);
184  return -1;
185  }
186  PRINTF("\r\n Successfully Applied MMA865x Sensor Configuration\r\n");
187 
188  for (;;) /* Forever loop */
189  { /* In ISR Mode we do not need to check Data Ready Register.
190  * The receipt of interrupt will indicate data is ready. */
191  if (false == gMma865xDataReady)
192  { /* Loop, if new sample is not available. */
194  continue;
195  }
196  else
197  { /*! Clear the data ready flag, it will be set again by the ISR. */
198  gMma865xDataReady = false;
199  gpioDriver->toggle_pin(&GREEN_LED);
200  }
201 
202  /*! Read the raw sensor data from the MMA865x. */
203  status = MMA865x_I2C_ReadData(&mma865xDriver, cMma865xOutputValues, data);
204  if (ARM_DRIVER_OK != status)
205  {
206  PRINTF("\r\n Read Failed. \r\n");
207  return -1;
208  }
209 
210  /*! Convert the raw sensor data to signed 16-bit container for display to the debug port. */
211  rawData.accel[0] = ((int16_t)data[0] << 8) | data[1];
212  rawData.accel[0] /= 16;
213  rawData.accel[1] = ((int16_t)data[2] << 8) | data[3];
214  rawData.accel[1] /= 16;
215  rawData.accel[2] = ((int16_t)data[4] << 8) | data[5];
216  rawData.accel[2] /= 16;
217 
218  /* NOTE: PRINTF is relatively expensive in terms of CPU time, specially when used with-in execution loop. */
219  PRINTF("\r\n Accel X = %d Y = %d Z = %d \r\n", rawData.accel[0], rawData.accel[1], rawData.accel[2]);
220  ASK_USER_TO_RESUME(50); /* Ask for user input after processing 100 samples. */
221  }
222 }
#define MMA865x_CTRL_REG3_IPOL_MASK
Definition: mma865x.h:1664
#define MMA865x_XYZ_DATA_CFG_FS_MASK
Definition: mma865x.h:530
const registerwritelist_t cMma865xConfigInterrupt[]
#define __END_WRITE_DATA__
Definition: sensor_drv.h:71
int32_t MMA865x_I2C_Initialize(mma865x_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: mma865x_drv.c:48
This structure defines the mma865x raw data buffer.
Definition: mma865x_drv.h:64
int main(void)
This is the The main function implementation.
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
#define MMA865x_CTRL_REG3_IPOL_ACTIVE_HIGH
Definition: mma865x.h:1701
int16_t accel[3]
Definition: mma865x_drv.h:67
uint8_t data[FXLS8962_DATA_SIZE]
int32_t MMA865x_I2C_ReadData(mma865x_i2c_sensorhandle_t *pSensorHandle, const registerreadlist_t *pReadList, uint8_t *pBuffer)
The interface function to read the sensor data.
Definition: mma865x_drv.c:132
int32_t status
void(* toggle_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:74
const registerreadlist_t cMma865xOutputValues[]
void BOARD_InitDebugConsole(void)
Definition: board.c:41
This defines the sensor specific information.
Definition: mma865x_drv.h:55
void(* registeridlefunction_t)(void *userParam)
This is the register idle function type.
Definition: sensor_drv.h:123
#define MMA865x_ACCEL_DATA_SIZE
#define MMA865x_CTRL_REG1_DR_MASK
Definition: mma865x.h:1534
#define __END_READ_DATA__
Definition: sensor_drv.h:77
#define MMA8652_I2C_ADDR
#define MMA865x_CTRL_REG4_INT_EN_DRDY_MASK
Definition: mma865x.h:1740
#define MMA865x_CTRL_REG1_DR_6_25HZ
Definition: mma865x.h:1554
void MMA865x_I2C_SetIdleTask(mma865x_i2c_sensorhandle_t *pSensorHandle, registeridlefunction_t idleTask, void *userParam)
: The interface function to set the I2C Idle Task.
Definition: mma865x_drv.c:79
#define MMA8652_WHOAMI_VALUE
Definition: mma865x.h:65
gpioHandleKSDK_t GREEN_LED
Definition: frdm_k64f.c:214
#define ASK_USER_TO_RESUME(x)
Definition: frdm_k64f.h:132
#define MMA865x_CTRL_REG4_INT_EN_DRDY_EN
Definition: mma865x.h:1778
int32_t MMA865x_I2C_Configure(mma865x_i2c_sensorhandle_t *pSensorHandle, const registerwritelist_t *pRegWriteList)
The interface function to configure he sensor.
Definition: mma865x_drv.c:87
#define MMA865x_XYZ_DATA_CFG_FS_2G
Definition: mma865x.h:542
#define MMA865x_CTRL_REG2_MODS_MASK
Definition: mma865x.h:1592
#define I2C_S_DEVICE_INDEX
Definition: issdk_hal.h:61
#define MMA8652_INT1
#define MMA865x_CTRL_REG5_INT_CFG_DRDY_INT1
Definition: mma865x.h:1853
#define I2C_S_DRIVER
Definition: issdk_hal.h:59
The mma865x_drv.h file describes the MMA865x driver interface and structures.
GENERIC_DRIVER_GPIO Driver_GPIO_KSDK
Definition: gpio_driver.c:203
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
Access structure of the GPIO Driver.
Definition: Driver_GPIO.h:64
fxls8962_acceldataUser_t rawData
#define MMA865x_CTRL_REG2_MODS_HR
Definition: mma865x.h:1623
This structure defines the Write command List.
Definition: sensor_drv.h:94
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
void BOARD_InitPins(void)
Configures pin routing and optionally pin electrical features.
Definition: pin_mux.c:73
void mma865x_int_data_ready_callback(void *pUserData)
This is the Sensor Data Ready ISR implementation.
volatile bool gMma865xDataReady
#define I2C_S_SIGNAL_EVENT
Definition: issdk_hal.h:60
#define MMA865x_CTRL_REG5_INT_CFG_DRDY_MASK
Definition: mma865x.h:1815