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 mma865x_fifo.c
11 * @brief The mma865x_fifo.c file implements the ISSDK MMA865x 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 "mma865x_drv.h"
31
32 //-----------------------------------------------------------------------
33 // Macros
34 //-----------------------------------------------------------------------
35 /* The watermark value configured for MMA865x FIFO Buffer. */
36 #define FIFO_WATERMARK (16) /* Must be between 1 - 32 */
37 #define MMA865x_ACCEL_DATA_SIZE (6) /* 2 byte X,Y,Z Axis Data each. */
38
39 //-----------------------------------------------------------------------
40 // Constants
41 //-----------------------------------------------------------------------
42 /*! Prepare the register write list to configure MMA865x in FIFO mode. */
43 const registerwritelist_t cMma865xConfigFifo[] =
44 {/*! Configure the MMA865x to enable FIFO Mode and set Watermark. */
45 {MMA865x_F_SETUP, MMA865x_F_SETUP_F_MODE_STOP_MODE | FIFO_WATERMARK,
46 MMA865x_F_SETUP_F_MODE_MASK | MMA865x_F_SETUP_F_WMRK_MASK},
47 /*! Configure the MMA865x to set FS Range as 2g. */
48 {MMA865x_XYZ_DATA_CFG, MMA865x_XYZ_DATA_CFG_FS_2G, MMA865x_XYZ_DATA_CFG_FS_MASK},
49 /*! Configure the MMA865x to set ODR to 100Hz. */
50 {MMA865x_CTRL_REG1, MMA865x_CTRL_REG1_DR_100HZ, MMA865x_CTRL_REG1_DR_MASK},
51 /*! Configure the MMA865x to set High Resolution mode. */
52 {MMA865x_CTRL_REG2, MMA865x_CTRL_REG2_MODS_HR, MMA865x_CTRL_REG2_MODS_MASK},
53 __END_WRITE_DATA__};
54
55 /*! Prepare the register read list to read the FIFO Status from MMA865x. */
56 const registerreadlist_t cMma865xFifoStatus[] = {{.readFrom = MMA865x_F_STATUS, .numBytes = 1}, __END_READ_DATA__};
57
58 /*! Prepare the register read list to read the raw Accel data from MMA865x. */
59 const registerreadlist_t cMma865xOutputValues[] = {
60 {.readFrom = MMA865x_OUT_X_MSB, .numBytes = MMA865x_ACCEL_DATA_SIZE * FIFO_WATERMARK}, __END_READ_DATA__};
61
62 //-----------------------------------------------------------------------
63 // Functions
64 //-----------------------------------------------------------------------
65 /*!
66 * @brief Main function
67 */
main(void)68 int main(void)
69 {
70 int32_t status;
71 uint8_t dataReady;
72 uint8_t data[MMA865x_ACCEL_DATA_SIZE * FIFO_WATERMARK];
73 mma865x_acceldata_t rawData;
74
75 ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER; // Now using the shield.h value!!!
76 mma865x_i2c_sensorhandle_t mma865xDriver;
77
78 BOARD_InitPins();
79 BOARD_BootClockRUN();
80 BOARD_InitDebugConsole();
81
82 PRINTF("\r\n ISSDK MMA865x sensor driver example for FIFO Mode. \r\n");
83
84 /*! Initialize the I2C driver. */
85 status = I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
86 if (ARM_DRIVER_OK != status)
87 {
88 PRINTF("\r\n I2C Initialization Failed\r\n");
89 return -1;
90 }
91
92 /*! Set the I2C Power mode. */
93 status = I2Cdrv->PowerControl(ARM_POWER_FULL);
94 if (ARM_DRIVER_OK != status)
95 {
96 PRINTF("\r\n I2C Power Mode setting Failed\r\n");
97 return -1;
98 }
99
100 /*! Set the I2C bus speed. */
101 status = I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
102 if (ARM_DRIVER_OK != status)
103 {
104 PRINTF("\r\n I2C Control Mode setting Failed\r\n");
105 return -1;
106 }
107
108 /*! Initialize the MMA865x sensor driver. */
109 status = MMA865x_I2C_Initialize(&mma865xDriver, &I2C_S_DRIVER, I2C_S_DEVICE_INDEX, MMA8652_I2C_ADDR,
110 MMA8652_WHOAMI_VALUE);
111 if (SENSOR_ERROR_NONE != status)
112 {
113 PRINTF("\r\n Sensor Initialization Failed\r\n");
114 return -1;
115 }
116 PRINTF("\r\n Successfully Initiliazed Sensor\r\n");
117
118 /*! Set the task to be executed while waiting for I2C transactions to complete. */
119 MMA865x_I2C_SetIdleTask(&mma865xDriver, (registeridlefunction_t)SMC_SetPowerModeVlpr, SMC);
120
121 /*! Configure the MMA865x sensor driver with FIFO mode. */
122 status = MMA865x_I2C_Configure(&mma865xDriver, cMma865xConfigFifo);
123 if (SENSOR_ERROR_NONE != status)
124 {
125 PRINTF("\r\n MMA865x Sensor Configuration Failed, Err = %d \r\n", status);
126 return -1;
127 }
128 PRINTF("\r\n MMA865x now active and entering data read loop... \r\n");
129
130 for (;;) /* Forever loop */
131 {
132 /*! Wait for data ready from the MMA865x. */
133 status = MMA865x_I2C_ReadData(&mma865xDriver, cMma865xFifoStatus, &dataReady);
134 if (0 == (dataReady & MMA865x_F_STATUS_F_WMKF_MASK))
135 { /* Loop, if new sample is not available. */
136 continue;
137 }
138
139 /*! Read the raw sensor data from the MMA865x. */
140 status = MMA865x_I2C_ReadData(&mma865xDriver, cMma865xOutputValues, data);
141 if (ARM_DRIVER_OK != status)
142 {
143 PRINTF("\r\n Read Failed. \r\n");
144 return -1;
145 }
146
147 for (uint8_t i = 0; i < FIFO_WATERMARK; i++)
148 {
149 /*! Convert the raw sensor data to signed 16-bit container for display to the debug port. */
150 rawData.accel[0] =
151 ((int16_t)data[i * MMA865x_ACCEL_DATA_SIZE + 0] << 8) | data[i * MMA865x_ACCEL_DATA_SIZE + 1];
152 rawData.accel[0] /= 16;
153 rawData.accel[1] =
154 ((int16_t)data[i * MMA865x_ACCEL_DATA_SIZE + 2] << 8) | data[i * MMA865x_ACCEL_DATA_SIZE + 3];
155 rawData.accel[1] /= 16;
156 rawData.accel[2] =
157 ((int16_t)data[i * MMA865x_ACCEL_DATA_SIZE + 4] << 8) | data[i * MMA865x_ACCEL_DATA_SIZE + 5];
158 rawData.accel[2] /= 16;
159 }
160 /* NOTE: PRINTF is relatively expensive in terms of CPU time, specially when used with-in execution loop.
161 * Hence, we display only the last sample from each FIFO Burst Read. */
162 PRINTF("\r\n Accel X = %d Y = %d Z = %d \r\n", rawData.accel[0], rawData.accel[1], rawData.accel[2]);
163 ASK_USER_TO_RESUME(992 / FIFO_WATERMARK); /* Ask for user input after processing 992 samples. */
164 }
165 }
166