Hello,
I'm using the code below to communicate between my launchpad and an MPU6050.
I've gone a lone way debugging it to the point that the I2C communication works correctly on the hardware level in an infinite "while (1)" loop.
On my oscilloscope - I see the SDA & SCL lines toggling with the correct address (0x68) being sent and the sensor acknowledging it
Unfortunately,
The payload is always zero.
I see the 0x00 bytes returned from the sensor on the oscilloscope as well as in Code Composer's debug menu.
////////////////////////////////////////////////////////////////////////////////////////////////////
#include <stdbool.h>
#include <stdint.h>
#include "driverlib/debug.h"
#include "driverlib/gpio.h"
#include "driverlib/pin_map.h"
#include "driverlib/pwm.h"
#include "driverlib/rom.h"
#include "driverlib/sysctl.h"
#include "inc/hw_gpio.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
////////////////////////////////////////////////////////////////////////////////////////////////////
// My I2C defines & includes
#include "driverlib/i2c.h"
#include "driverlib/interrupt.h"
#include "inc/hw_i2c.h"
#include "inc/hw_ints.h"
#include "sensorlib/i2cm_drv.h"
#include "sensorlib/mpu6050.h"
#include "sensorlib/hw_mpu6050.h"
#define DEBUG
////////////////////////////////////////////////////////////////////////////////////////////////////
#include "definitions_mpu6050.h"
tI2CMInstance g_sI2CInst; // I2C master driver structure. "tI2CMInstance" is defined in the i2cm_drv.h file.
tMPU6050 g_sMPU6050Inst; // MPU6050 sensor driver structure. "tMPU6050" is defined in the mpu6050.h file.
volatile unsigned long g_vui8DataFlag; // Data ready flag
volatile unsigned long g_vui8ErrorFlag; // Error flag
void
ISL29023I2CIntHandler(void)
{
I2CMIntHandler(&g_sI2CInst);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
int main(void)
{
// Configure the clock
SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);
////////////////////////////////////////////////////////////////////////////////////////////////////
// My I2C initialization code
// Enable GPIO peripheral that contains I2C 2
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
// Enable I2C module 2
SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C2);
// Configure the pin muxing for I2C2 functions on port E4 and E5.
GPIOPinConfigure(GPIO_PE4_I2C2SCL);
GPIOPinConfigure(GPIO_PE5_I2C2SDA);
// Select the I2C function for these pins.
GPIOPinTypeI2CSCL(GPIO_PORTE_BASE, GPIO_PIN_4);
GPIOPinTypeI2C(GPIO_PORTE_BASE, GPIO_PIN_5);
IntMasterEnable();
I2CMInit(&g_sI2CInst, I2C2_BASE, INT_I2C2, 0xFF, 0xFF, SysCtlClockGet());
SysCtlDelay(SysCtlClockGet() / 3);
float fAccel[3], fGyro[3];
// USER_MPU6050Callback will modify the g_bMPU6050Done variable to true if MPU6050Init succeeds.
g_bMPU6050Done = false;
// Initialize the MPU6050
MPU6050Init(&g_sMPU6050Inst, &g_sI2CInst, MPU6050_I2C_ADDRESS, USER_MPU6050Callback, &g_sMPU6050Inst);
while(!g_bMPU6050Done) { }
// USER_MPU6050Callback will modify the g_bMPU6050Done variable to true if MPU6050ReadModifyWrite succeeds.
g_bMPU6050Done = false;
// Configure the MPU6050 for +/- 4 g accelerometer range.
MPU6050ReadModifyWrite(&g_sMPU6050Inst, MPU6050_O_ACCEL_CONFIG, ~MPU6050_ACCEL_CONFIG_AFS_SEL_M, MPU6050_ACCEL_CONFIG_AFS_SEL_4G, USER_MPU6050Callback, &g_sMPU6050Inst);
while(!g_bMPU6050Done) { }
while(1)
{
// USER_MPU6050Callback will modify the g_bMPU6050Done variable to true if MPU6050DataRead succeeds.
g_bMPU6050Done = false;
// Request another reading from the MPU6050.
MPU6050DataRead(&g_sMPU6050Inst, USER_MPU6050Callback, &g_sMPU6050Inst);
while(!g_bMPU6050Done) { }
// Get the new accelerometer and gyroscope readings.
MPU6050DataAccelGetFloat(&g_sMPU6050Inst, &fAccel[0], &fAccel[1], &fAccel[2]);
MPU6050DataGyroGetFloat(&g_sMPU6050Inst, &fGyro[0], &fGyro[1], &fGyro[2]);
}
}