ISSDK  1.7
IoT Sensing Software Development Kit
mma8491q_example.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 mma8491q_example.c
37  * @brief The mma8491q_example.c file implements the ISSDK MMA8491Q sensor driver
38  * example demonstration with Timers.
39  */
40 
41 //-----------------------------------------------------------------------
42 // SDK Includes
43 //-----------------------------------------------------------------------
44 #include "board.h"
45 #include "pin_mux.h"
46 #include "fsl_lptmr.h"
47 #include "clock_config.h"
48 #include "fsl_debug_console.h"
49 
50 //-----------------------------------------------------------------------
51 // CMSIS Includes
52 //-----------------------------------------------------------------------
53 #include "Driver_I2C.h"
54 
55 //-----------------------------------------------------------------------
56 // ISSDK Includes
57 //-----------------------------------------------------------------------
58 #include "issdk_hal.h"
59 #include "gpio_driver.h"
60 #include "mma8491q_drv.h"
61 
62 //-----------------------------------------------------------------------
63 // Macros
64 //-----------------------------------------------------------------------
65 /* Timer timeout Callback. */
66 #define mma8491q_en_callback LPTMR0_IRQHandler
67 /* Desired ODR rate in milli seconds (since the example uses LPTMR, this should be >= 10ms for reliable timing). */
68 #define MMA8491Q_T_ODR_ms 100U
69 /* The LPTMR Timeout Correction in milliseconds. */
70 #define LPTMR_T_CORRECTION_ms 2
71 
72 //-----------------------------------------------------------------------
73 // Constants
74 //-----------------------------------------------------------------------
75 /*! @brief Address of Status Register. */
77 
78 /*! @brief Address and size of Raw Acceleration Data. */
81 
82 //-----------------------------------------------------------------------
83 // Global Variables
84 //-----------------------------------------------------------------------
85 volatile bool bMma849qDataReady = false;
86 gpioConfigKSDK_t gGpioConfigInPins = /* SDK GPIO Config for Tilt Pins. */
87  {
88  .pinConfig = {kGPIO_DigitalInput, 0},
89  .interruptMode = kPORT_InterruptOrDMADisabled,
90  .portPinConfig = {0},
91  .portPinConfig.mux = kPORT_MuxAsGpio,
92 };
94 
95 //-----------------------------------------------------------------------
96 // Functions
97 //-----------------------------------------------------------------------
98 /* @brief Block for adding EN delay. */
100 {
101  uint32_t delay, count, systemCoreClock;
102 
103  systemCoreClock = CLOCK_GetCoreSysClkFreq();
104  delay = (int)(0.16*MSEC_TO_COUNT(MMA8491Q_T_ON_TYPICAL, systemCoreClock));
105  for(count=0;count<delay;count++)
106  {
107  __NOP();
108  }
109 }
110 
111 /* LPTMR based EN control Timer Callback function. */
113 {
114  LPTMR_ClearStatusFlags(LPTMR0, kLPTMR_TimerCompareFlag);
115  bMma849qDataReady = true;
116 }
117 
118 /* Function for MMA8491Q specific initialization tasks. */
119 void mma8491q_timer_fwk_init(uint32_t samplingInterval)
120 {
121  lptmr_config_t lptmrConfig;
122  /* Initialize ODR Timer. */
123  LPTMR_GetDefaultConfig(&lptmrConfig);
124  LPTMR_Init(LPTMR0, &lptmrConfig);
125  LPTMR_EnableInterrupts(LPTMR0, kLPTMR_TimerInterruptEnable);
126  EnableIRQ(LPTMR0_IRQn);
127 
128  /* Put the Sensor into Active Mode to enable I2C communication. */
129  LPTMR_SetTimerPeriod(LPTMR0, MSEC_TO_COUNT((samplingInterval-LPTMR_T_CORRECTION_ms), CLOCK_GetFreq(kCLOCK_LpoClk)));
130 }
131 
132 /*!
133  * @brief Main function
134  */
135 int main(void)
136 {
137  int32_t status;
138  uint8_t dataReady, data[MMA8491Q_DATA_SIZE];
139 
140  mma8491q_i2c_sensorhandle_t mma8491qDriver;
142 
143  ARM_DRIVER_I2C *pI2cDriver = &I2C_S_DRIVER;
144 
145  /*! Initialize the MCU hardware. */
146  BOARD_InitPins();
149 
150  PRINTF("\r\n ISSDK MMA8491Q sensor driver example demonstration with poll mode\r\n");
151 
152  /* Initialize Tilt Pin IDs. */
153  pGpioDriver->pin_init(&MMA8491_EN, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
154  pGpioDriver->pin_init(&MMA8491_XOUT, GPIO_DIRECTION_IN, &gGpioConfigInPins, NULL, NULL);
155  pGpioDriver->pin_init(&MMA8491_YOUT, GPIO_DIRECTION_IN, &gGpioConfigInPins, NULL, NULL);
156  pGpioDriver->pin_init(&MMA8491_ZOUT, GPIO_DIRECTION_IN, &gGpioConfigInPins, NULL, NULL);
157 
158  /*! Initialize GREEN LED pin used by FRDM board */
159  pGpioDriver->pin_init(&GREEN_LED, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
160 
161  /*! Initialize the I2C driver. */
162  status = pI2cDriver->Initialize(I2C_S_SIGNAL_EVENT);
163  if (ARM_DRIVER_OK != status)
164  {
165  PRINTF("\r\n I2C Initialization Failed\r\n");
166  return -1;
167  }
168 
169  /*! Set the I2C Power mode. */
170  status = pI2cDriver->PowerControl(ARM_POWER_FULL);
171  if (ARM_DRIVER_OK != status)
172  {
173  PRINTF("\r\n I2C Power Mode setting Failed\r\n");
174  return -1;
175  }
176 
177  /*! Set the I2C bus speed. */
178  status = pI2cDriver->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
179  if (ARM_DRIVER_OK != status)
180  {
181  PRINTF("\r\n I2C Control Mode setting Failed\r\n");
182  return -1;
183  }
184 
185  /* Set EN = 1 to enable I2C communication. */
186  pGpioDriver->set_pin(&MMA8491_EN);
188 
189  /* Initialize the Sensor Driver. */
191  if (SENSOR_ERROR_NONE != status)
192  {
193  PRINTF("\r\n Sensor Initialization Failed\r\n");
194  return -1;
195  }
196  PRINTF("\r\n Successfully Initiliazed Sensor\r\n");
197 
198  /*! Set the task to be executed while waiting for I2C transactions to complete. */
200 
201  /*! Initialize MMA8491_ODR Timer framework. */
203  for (;;) /* Forever loop */
204  {
205  /*! Process packets on Data ready Timer Expiry */
206  if (false == bMma849qDataReady)
207  {
208  SMC_SetPowerModeWait(SMC); /* Power save, wait if nothing to do. */
209  continue;
210  }
211  else
212  {
213  pGpioDriver->toggle_pin(&GREEN_LED);
214  bMma849qDataReady = false;
215  }
216 
217  /* Set EN = 1 to put the Sensor into Active Mode. */
218  pGpioDriver->set_pin(&MMA8491_EN);
220  do
221  { /*! Process packets only when data ready is indicated by the MMA8491Q. */
222  MMA8491Q_I2C_ReadData(&mma8491qDriver, cMma8491qStatus, &dataReady);
223  } while (0 == (dataReady & MMA8491Q_STATUS_ZYXDR_MASK));
224 
225  /*! Read the raw sensor data from the MMA8491Q. */
226  status = MMA8491Q_I2C_ReadData(&mma8491qDriver, cMma8491qOutput, data);
227  if (ARM_DRIVER_OK != status)
228  { /* Loop, if sample read failed. */
229  continue;
230  }
231  /* Set EN = 0 to put the Sensor into Shutdown Mode. */
232  pGpioDriver->clr_pin(&MMA8491_EN);
233 
234  /* Read Tilt Output */
235  rawData.tilt[0] = pGpioDriver->read_pin(&MMA8491_XOUT);
236  rawData.tilt[1] = pGpioDriver->read_pin(&MMA8491_YOUT);
237  rawData.tilt[2] = pGpioDriver->read_pin(&MMA8491_ZOUT);
238 
239  /*! Process the sample and convert the raw sensor data. */
240  rawData.accel[0] = ((int16_t)data[0] << 8) | (data[1]);
241  rawData.accel[0] /= 4;
242  rawData.accel[1] = ((int16_t)data[2] << 8) | (data[3]);
243  rawData.accel[1] /= 4;
244  rawData.accel[2] = ((int16_t)data[4] << 8) | (data[5]);
245  rawData.accel[2] /= 4;
246 
247  PRINTF("\r\n Tilt : X = %d Y = %d Z = %d ", rawData.tilt[0], rawData.tilt[1], rawData.tilt[2]);
248  PRINTF("|| Accel : X = %5d Y = %5d Z = %5d \r\n", rawData.accel[0], rawData.accel[1], rawData.accel[2]);
249  ASK_USER_TO_RESUME(100); /* Ask for user input after processing 100 samples. */
250  }
251 }
void mma8491q_en_delay()
#define MMA8491Q_STATUS_ZYXDR_MASK
Definition: mma8491q.h:68
gpioConfigKSDK_t gGpioConfigInPins
const registerreadlist_t cMma8491qStatus[]
Address of Status Register.
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 MMA8491Q_DATA_SIZE
Definition: mma8491q_drv.h:89
#define MMA8491_I2C_ADDR
uint8_t data[FXLS8962_DATA_SIZE]
int32_t status
void(* toggle_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:74
The GPIO Configuration KSDK.
Definition: gpio_driver.h:65
int32_t MMA8491Q_I2C_ReadData(mma8491q_i2c_sensorhandle_t *pSensorHandle, const registerreadlist_t *pReadList, uint8_t *pBuffer)
The interface function to read the sensor data.
Definition: mma8491q_drv.c:90
void BOARD_InitDebugConsole(void)
Definition: board.c:41
void(* registeridlefunction_t)(void *userParam)
This is the register idle function type.
Definition: sensor_drv.h:123
#define mma8491q_en_callback
const registerreadlist_t cMma8491qOutput[]
Address and size of Raw Acceleration Data.
#define __END_READ_DATA__
Definition: sensor_drv.h:77
GENERIC_DRIVER_GPIO * pGpioDriver
uint32_t(* read_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:76
gpioHandleKSDK_t GREEN_LED
Definition: frdm_k64f.c:214
void mma8491q_timer_fwk_init(uint32_t samplingInterval)
int32_t MMA8491Q_I2C_Initialize(mma8491q_i2c_sensorhandle_t *pSensorHandle, ARM_DRIVER_I2C *pBus, uint8_t index, uint16_t sAddress)
The interface function to initialize the sensor.
Definition: mma8491q_drv.c:48
#define ASK_USER_TO_RESUME(x)
Definition: frdm_k64f.h:132
gpio_pin_config_t pinConfig
Definition: gpio_driver.h:67
void(* set_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:72
void(* clr_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:73
void MMA8491Q_I2C_SetIdleTask(mma8491q_i2c_sensorhandle_t *pSensorHandle, registeridlefunction_t idleTask, void *userParam)
: The interface function to set the I2C Idle Task.
Definition: mma8491q_drv.c:82
This defines the function pointers and sensor specific information.
Definition: mma8491q_drv.h:55
#define I2C_S_DEVICE_INDEX
Definition: issdk_hal.h:61
#define I2C_S_DRIVER
Definition: issdk_hal.h:59
int main(void)
Main function.
#define LPTMR_T_CORRECTION_ms
The mma8491q_drv.h file describes the MMA8491Q 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.
#define MMA8491_XOUT
#define MMA8491Q_T_ODR_ms
#define MMA8491_YOUT
Access structure of the GPIO Driver.
Definition: Driver_GPIO.h:64
fxls8962_acceldataUser_t rawData
#define MMA8491Q_T_ON_TYPICAL
Definition: mma8491q.h:30
This structure defines the Read command List.
Definition: sensor_drv.h:104
#define SMC
Definition: lpc54114.h:144
This structure defines the mma8491q data buffer.
Definition: mma8491q_drv.h:64
volatile bool bMma849qDataReady
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
#define I2C_S_SIGNAL_EVENT
Definition: issdk_hal.h:60
#define MMA8491_EN
#define MMA8491_ZOUT