ISSDK  1.8
IoT Sensing Software Development Kit
mma845x_fifo.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 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 mma845x_fifo.c
11 * @brief The mma845x_fifo.c file implements the ISSDK MMA845x sensor driver
12 * example demonstration with FIFO 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 /* ISSDK Includes */
29 #include "issdk_hal.h"
30 #include "mma845x_drv.h"
31 
32 //-----------------------------------------------------------------------
33 // Macros
34 //-----------------------------------------------------------------------
35 /*! @def FIFO_SIZE
36  * @brief The watermark value configured for MMA845x FIFO Buffer.
37  */
38 #define FIFO_SIZE 4 // Must be between 1 - 32
39 
40 //-----------------------------------------------------------------------
41 // Constants
42 //-----------------------------------------------------------------------
43 /*! Prepare the register write list to configure MMA845x in FIFO mode. */
45  /*! Configure the MMA845x CTRL_REG1 to set mode to STANDBY and odr to 12.5Hz. */
47  /*! Configure the MMA845x F_SETUP to set FIFO mode to STOP, set the watermark size to FIFO_SIZE. */
49  /*! Configure the MMA845x CTRL_REG2 to set the Oversampling mode to High Resolution. */
52 
53 /*! Prepare the register read list to read the FIFO event from MMA845x. */
55 
56 /*! Prepare the register read list to read the raw Accel data from MMA845x. */
59 
60 //-----------------------------------------------------------------------
61 // Functions
62 //-----------------------------------------------------------------------
63 /*!
64  * @brief Main function
65  */
66 int main(void)
67 {
69  uint8_t fifoEvent;
72 
73  ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER; // Now using the shield.h value!!!
74  mma845x_i2c_sensorhandle_t MMA845xdrv;
75 
79 
80  PRINTF("\r\n ISSDK MMA845x sensor driver example for fifo mode\r\n");
81 
82  /*! Initialize the I2C driver. */
83  status = I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
84  if (ARM_DRIVER_OK != status)
85  {
86  PRINTF("\r\n I2C Initialization Failed\r\n");
87  return -1;
88  }
89 
90  /*! Set the I2C Power mode. */
91  status = I2Cdrv->PowerControl(ARM_POWER_FULL);
92  if (ARM_DRIVER_OK != status)
93  {
94  PRINTF("\r\n I2C Power Mode setting Failed\r\n");
95  return -1;
96  }
97 
98  /*! Set the I2C bus speed. */
99  status = I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
100  if (ARM_DRIVER_OK != status)
101  {
102  PRINTF("\r\n I2C Control Mode setting Failed\r\n");
103  return -1;
104  }
105 
106  /*! Initialize the MMA845x sensor driver. */
109  if (SENSOR_ERROR_NONE != status)
110  {
111  PRINTF("\r\n Sensor Initialization Failed\r\n");
112  return -1;
113  }
114  PRINTF("\r\n Successfully Initiliazed Sensor\r\n");
115 
116  /*! Set the task to be executed while waiting for I2C transactions to complete. */
118 
119  /*! Configure the MMA845x sensor driver with FIFO mode. */
120  status = MMA845x_I2C_Configure(&MMA845xdrv, mma845x_Config_Fifo);
121  if (SENSOR_ERROR_NONE != status)
122  {
123  PRINTF("\r\n MMA845x Sensor Configuration Failed, Err = %d\r\n", status);
124  return -1;
125  }
126  PRINTF("\r\n MMA845x now active and entering data read loop... \r\n");
127 
128  for (;;) /* Forever loop */
129  {
130  /*! Wait for data ready from the MMA845x. */
131  status = MMA845x_I2C_ReadData(&MMA845xdrv, mma845x_Fifo_Status, &fifoEvent);
132  if (0 == (fifoEvent & MMA845x_F_STATUS_F_WMRK_FLAG_MASK))
133  { /* Loop, if new sample is not available. */
134  continue;
135  }
136 
137  /*! Read the raw sensor data from the MMA845x. */
138  status = MMA845x_I2C_ReadData(&MMA845xdrv, mma845x_Output_Values, data);
139  if (ARM_DRIVER_OK != status)
140  {
141  PRINTF("\r\n Read Failed. \r\n");
142  return -1;
143  }
144 
145  for (uint8_t i = 0; i < FIFO_SIZE; i++)
146  {
147  /*! Convert the raw sensor data to signed 16-bit container for display to the debug port. */
148  rawData.accel[0] =
149  ((int16_t)data[i * MMA845x_ACCEL_DATA_SIZE + 0] << 8) | data[i * MMA845x_ACCEL_DATA_SIZE + 1];
150  rawData.accel[0] /= 4;
151  rawData.accel[1] =
152  ((int16_t)data[i * MMA845x_ACCEL_DATA_SIZE + 2] << 8) | data[i * MMA845x_ACCEL_DATA_SIZE + 3];
153  rawData.accel[1] /= 4;
154  rawData.accel[2] =
155  ((int16_t)data[i * MMA845x_ACCEL_DATA_SIZE + 4] << 8) | data[i * MMA845x_ACCEL_DATA_SIZE + 5];
156  rawData.accel[2] /= 4;
157  }
158  /* NOTE: PRINTF is relatively expensive in terms of CPU time, specially when used with-in execution loop.
159  * Hence, we display only the last sample from each FIFO Burst Read. */
160  PRINTF("\r\n Accel X = %d Y = %d Z = %d\r\n", rawData.accel[0], rawData.accel[1], rawData.accel[2]);
161  ASK_USER_TO_RESUME(100 / FIFO_SIZE); /* Ask for user input after processing 100 samples. */
162  }
163 }
#define MMA845x_CTRL_REG1_DR_12DOT5HZ
Definition: mma845x.h:1610
#define MMA8451_WHO_AM_I_WHOAMI_VALUE
Definition: mma845x.h:482
#define MMA845x_F_SETUP_F_MODE_FIFOSTOP
Definition: mma845x.h:288
int16_t accel[3]
Definition: mma845x_drv.h:41
This structure defines the Write command List.
Definition: sensor_drv.h:68
#define ASK_USER_TO_RESUME(x)
Definition: frdm_k64f.h:106
int32_t status
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
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:61
#define SMC
Definition: lpc54114.h:118
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:22
#define __END_WRITE_DATA__
Definition: sensor_drv.h:45
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:53
This structure defines the mma845x raw data buffer.
Definition: mma845x_drv.h:38
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
#define I2C_S_SIGNAL_EVENT
Definition: issdk_hal.h:34
#define I2C_S_DRIVER
Definition: issdk_hal.h:33
const registerreadlist_t mma845x_Fifo_Status[]
Definition: mma845x_fifo.c:54
#define BOARD_BootClockRUN
Definition: clock_config.h:19
void(* registeridlefunction_t)(void *userParam)
This is the register idle function type.
Definition: sensor_drv.h:97
#define MMA845x_F_STATUS_F_WMRK_FLAG_MASK
Definition: mma845x.h:178
uint8_t data[FXLS8962_DATA_SIZE]
#define MMA845x_CTRL_REG2_MODS_HIGHRES
Definition: mma845x.h:1669
#define __END_READ_DATA__
Definition: sensor_drv.h:51
ARM_DRIVER_I2C * I2Cdrv
fxos8700_accelmagdata_t rawData
const registerreadlist_t mma845x_Output_Values[]
Definition: mma845x_fifo.c:57
uint16_t readFrom
Definition: sensor_drv.h:80
#define MMA845x_ACCEL_DATA_SIZE
The size of the MMA845x accel data.
Definition: mma845x_drv.h:47
This structure defines the Read command List.
Definition: sensor_drv.h:78
#define I2C_S_DEVICE_INDEX
Definition: issdk_hal.h:35
#define FIFO_SIZE
The watermark value configured for MMA845x FIFO Buffer.
Definition: mma845x_fifo.c:38
#define MMA845x_CTRL_REG1_MODE_STANDBY
Definition: mma845x.h:1599
This defines the sensor specific information.
Definition: mma845x_drv.h:29
The mma845x_drv.h file describes the MMA845x driver interface and structures.
void BOARD_InitDebugConsole(void)
Definition: board.c:15
int main(void)
Main function.
Definition: mma845x_fifo.c:66
void BOARD_InitPins(void)
Configures pin routing and optionally pin electrical features.
Definition: pin_mux.c:47
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:104
#define MMA845x_I2C_ADDR
Definition: frdm_kl25z.h:102
const registerwritelist_t mma845x_Config_Fifo[]
Definition: mma845x_fifo.c:44