ISSDK  1.7
IoT Sensing Software Development Kit
mma845x_fifo.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 mma845x_fifo.c
37 * @brief The mma845x_fifo.c file implements the ISSDK MMA845x sensor driver
38 * example demonstration with FIFO 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 /* ISSDK Includes */
55 #include "issdk_hal.h"
56 #include "mma845x_drv.h"
57 
58 //-----------------------------------------------------------------------
59 // Macros
60 //-----------------------------------------------------------------------
61 /*! @def FIFO_SIZE
62  * @brief The watermark value configured for MMA845x FIFO Buffer.
63  */
64 #define FIFO_SIZE 4 // Must be between 1 - 32
65 
66 //-----------------------------------------------------------------------
67 // Constants
68 //-----------------------------------------------------------------------
69 /*! Prepare the register write list to configure MMA845x in FIFO mode. */
71  /*! Configure the MMA845x CTRL_REG1 to set mode to STANDBY and odr to 12.5Hz. */
73  /*! Configure the MMA845x F_SETUP to set FIFO mode to STOP, set the watermark size to FIFO_SIZE. */
75  /*! Configure the MMA845x CTRL_REG2 to set the Oversampling mode to High Resolution. */
78 
79 /*! Prepare the register read list to read the FIFO event from MMA845x. */
81 
82 /*! Prepare the register read list to read the raw Accel data from MMA845x. */
85 
86 //-----------------------------------------------------------------------
87 // Functions
88 //-----------------------------------------------------------------------
89 /*!
90  * @brief Main function
91  */
92 int main(void)
93 {
95  uint8_t fifoEvent;
98 
99  ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER; // Now using the shield.h value!!!
100  mma845x_i2c_sensorhandle_t MMA845xdrv;
101 
102  BOARD_InitPins();
105 
106  PRINTF("\r\n ISSDK MMA845x sensor driver example for fifo mode\r\n");
107 
108  /*! Initialize the I2C driver. */
109  status = I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
110  if (ARM_DRIVER_OK != status)
111  {
112  PRINTF("\r\n I2C Initialization Failed\r\n");
113  return -1;
114  }
115 
116  /*! Set the I2C Power mode. */
117  status = I2Cdrv->PowerControl(ARM_POWER_FULL);
118  if (ARM_DRIVER_OK != status)
119  {
120  PRINTF("\r\n I2C Power Mode setting Failed\r\n");
121  return -1;
122  }
123 
124  /*! Set the I2C bus speed. */
125  status = I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
126  if (ARM_DRIVER_OK != status)
127  {
128  PRINTF("\r\n I2C Control Mode setting Failed\r\n");
129  return -1;
130  }
131 
132  /*! Initialize the MMA845x sensor driver. */
135  if (SENSOR_ERROR_NONE != status)
136  {
137  PRINTF("\r\n Sensor Initialization Failed\r\n");
138  return -1;
139  }
140  PRINTF("\r\n Successfully Initiliazed Sensor\r\n");
141 
142  /*! Set the task to be executed while waiting for I2C transactions to complete. */
144 
145  /*! Configure the MMA845x sensor driver with FIFO mode. */
146  status = MMA845x_I2C_Configure(&MMA845xdrv, mma845x_Config_Fifo);
147  if (SENSOR_ERROR_NONE != status)
148  {
149  PRINTF("\r\n MMA845x Sensor Configuration Failed, Err = %d\r\n", status);
150  return -1;
151  }
152  PRINTF("\r\n MMA845x now active and entering data read loop... \r\n");
153 
154  for (;;) /* Forever loop */
155  {
156  /*! Wait for data ready from the MMA845x. */
157  status = MMA845x_I2C_ReadData(&MMA845xdrv, mma845x_Fifo_Status, &fifoEvent);
158  if (0 == (fifoEvent & MMA845x_F_STATUS_F_WMRK_FLAG_MASK))
159  { /* Loop, if new sample is not available. */
160  continue;
161  }
162 
163  /*! Read the raw sensor data from the MMA845x. */
164  status = MMA845x_I2C_ReadData(&MMA845xdrv, mma845x_Output_Values, data);
165  if (ARM_DRIVER_OK != status)
166  {
167  PRINTF("\r\n Read Failed. \r\n");
168  return -1;
169  }
170 
171  for (uint8_t i = 0; i < FIFO_SIZE; i++)
172  {
173  /*! Convert the raw sensor data to signed 16-bit container for display to the debug port. */
174  rawData.accel[0] =
175  ((int16_t)data[i * MMA845x_ACCEL_DATA_SIZE + 0] << 8) | data[i * MMA845x_ACCEL_DATA_SIZE + 1];
176  rawData.accel[0] /= 4;
177  rawData.accel[1] =
178  ((int16_t)data[i * MMA845x_ACCEL_DATA_SIZE + 2] << 8) | data[i * MMA845x_ACCEL_DATA_SIZE + 3];
179  rawData.accel[1] /= 4;
180  rawData.accel[2] =
181  ((int16_t)data[i * MMA845x_ACCEL_DATA_SIZE + 4] << 8) | data[i * MMA845x_ACCEL_DATA_SIZE + 5];
182  rawData.accel[2] /= 4;
183  }
184  /* NOTE: PRINTF is relatively expensive in terms of CPU time, specially when used with-in execution loop.
185  * Hence, we display only the last sample from each FIFO Burst Read. */
186  PRINTF("\r\n Accel X = %d Y = %d Z = %d\r\n", rawData.accel[0], rawData.accel[1], rawData.accel[2]);
187  ASK_USER_TO_RESUME(100 / FIFO_SIZE); /* Ask for user input after processing 100 samples. */
188  }
189 }
int main(void)
Main function.
Definition: mma845x_fifo.c:92
#define __END_WRITE_DATA__
Definition: sensor_drv.h:71
This defines the sensor specific information.
Definition: mma845x_drv.h:55
#define BOARD_BootClockRUN
Definition: clock_config.h:45
int16_t accel[3]
Definition: mma845x_drv.h:67
uint8_t data[FXLS8962_DATA_SIZE]
int32_t status
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
int32_t MMA845x_I2C_Initialize(mma845x_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: mma845x_drv.c:48
#define __END_READ_DATA__
Definition: sensor_drv.h:77
#define MMA845x_CTRL_REG2_MODS_HIGHRES
Definition: mma845x.h:1695
This structure defines the mma845x raw data buffer.
Definition: mma845x_drv.h:64
#define MMA845x_F_SETUP_F_MODE_FIFOSTOP
Definition: mma845x.h:314
#define FIFO_SIZE
The watermark value configured for MMA845x FIFO Buffer.
Definition: mma845x_fifo.c:64
#define ASK_USER_TO_RESUME(x)
Definition: frdm_k64f.h:132
const registerreadlist_t mma845x_Fifo_Status[]
Definition: mma845x_fifo.c:80
#define MMA845x_I2C_ADDR
Definition: frdm_kl25z.h:128
#define I2C_S_DEVICE_INDEX
Definition: issdk_hal.h:61
int32_t MMA845x_I2C_ReadData(mma845x_i2c_sensorhandle_t *pSensorHandle, const registerreadlist_t *pReadList, uint8_t *pBuffer)
The interface function to read the sensor data.
Definition: mma845x_drv.c:130
#define I2C_S_DRIVER
Definition: issdk_hal.h:59
#define MMA845x_CTRL_REG1_MODE_STANDBY
Definition: mma845x.h:1625
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
const registerwritelist_t mma845x_Config_Fifo[]
Definition: mma845x_fifo.c:70
int32_t MMA845x_I2C_Configure(mma845x_i2c_sensorhandle_t *pSensorHandle, const registerwritelist_t *pRegWriteList)
The interface function to configure he sensor.
Definition: mma845x_drv.c:87
fxls8962_acceldataUser_t rawData
This structure defines the Write command List.
Definition: sensor_drv.h:94
const registerreadlist_t mma845x_Output_Values[]
Definition: mma845x_fifo.c:83
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 MMA845x_I2C_SetIdleTask(mma845x_i2c_sensorhandle_t *pSensorHandle, registeridlefunction_t idleTask, void *userParam)
: The interface function to set the I2C Idle Task.
Definition: mma845x_drv.c:79
#define MMA845x_ACCEL_DATA_SIZE
The size of the MMA845x accel data.
Definition: mma845x_drv.h:73
void BOARD_InitPins(void)
Configures pin routing and optionally pin electrical features.
Definition: pin_mux.c:73
#define MMA845x_F_STATUS_F_WMRK_FLAG_MASK
Definition: mma845x.h:204
#define I2C_S_SIGNAL_EVENT
Definition: issdk_hal.h:60
#define MMA8451_WHO_AM_I_WHOAMI_VALUE
Definition: mma845x.h:508
The mma845x_drv.h file describes the MMA845x driver interface and structures.
#define MMA845x_CTRL_REG1_DR_12DOT5HZ
Definition: mma845x.h:1636