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