Tool/software: TI-RTOS
HI,
I tried to use i2c for communicating with MPU-6050 sensor.
I don't know where i'm going wrong. I'm getting same response from the sensor. I have checked with oscilloscope and getting the same data even after doing change in movements.
Here I'm attaching
1.my file used for i2c read and write.
2.Invensense register map document
3.image of oscilloscope.
2273.MPU-6000-Register-Map1.pdf
/*
* Copyright (c) 2015, Texas Instruments Incorporated
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of Texas Instruments Incorporated nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* ======== i2crf430cl330_load.c ========
*/
/* XDCtools Header files */
#include <xdc/std.h>
#include <xdc/runtime/Error.h>
#include <xdc/runtime/System.h>
/* BIOS Header files */
#include <ti/sysbios/BIOS.h>
#include <ti/sysbios/knl/Task.h>
#include <ti/sysbios/knl/Semaphore.h>
#include <ti/sysbios/utils/Load.h>
/* TI-RTOS Header files */
#include <ti/drivers/GPIO.h>
#include <ti/drivers/I2C.h>
/* Example/Board Header files */
#include "Board.h"
#include <stdio.h>
#include <stdlib.h>
#define PWMR_MGMT 0x6B
#define IMU_SENSOR (0x68)
#define TASKSTACKSIZE 768
Task_Struct task0Struct;
Char task0Stack[TASKSTACKSIZE];
void writeRegister(I2C_Handle handle, uint16_t regAddr, uint16_t value)
{
uint8_t txBuffer[4];
I2C_Transaction i2cTransaction;
i2cTransaction.slaveAddress = IMU_SENSOR;
/* Write to a 16-bit status register */
i2cTransaction.writeBuf = txBuffer;
i2cTransaction.writeCount = 4;
i2cTransaction.readCount = 0;
txBuffer[0] = regAddr >> 8; //HB Addr
txBuffer[1] = regAddr & 0xFF; //LB Addr
txBuffer[2] = value & 0xFF;
txBuffer[3] = value >> 8;
if (!I2C_transfer(handle, &i2cTransaction)) {
GPIO_write(Board_LED1, Board_LED_ON);
System_abort("Bad I2C transfer!");
}
}
void readRegister(I2C_Handle handle,
uint16_t regAddr,
uint8_t *data,
size_t length)
{
uint8_t txBuffer[2];
I2C_Transaction i2cTransaction;
i2cTransaction.slaveAddress = IMU_SENSOR;
/* Write to a 16-bit status register */
i2cTransaction.writeBuf = txBuffer;
i2cTransaction.readBuf = data;
i2cTransaction.writeCount = 2;
i2cTransaction.readCount = length;
txBuffer[0] = regAddr << 8; //HB Addr
txBuffer[1] = regAddr & 0xFF; //LB Addr
if (!I2C_transfer(handle, &i2cTransaction)) {
GPIO_write(Board_LED1, Board_LED_ON);
System_abort("Bad I2C transfer!");
}
}
/*
* ======== taskFxn ========
* Task for this function is created statically. See the project's .cfg file.
*/
Void imuTask(UArg arg0, UArg arg1)
{
I2C_Handle i2c;
I2C_Params i2cParams;
uint8_t data;
/* Create I2C for usage */
I2C_Params_init(&i2cParams);
i2cParams.bitRate = I2C_400kHz;
i2c = I2C_open(Board_I2C_NFC, &i2cParams);
if (i2c == NULL)
{
System_abort("Error Initializing I2C\n");
}
else
{
System_printf("I2C Initialized!\n");
}
/* Reset the device and wait */
writeRegister(i2c, PWMR_MGMT, 0);
Task_sleep(1000);
GPIO_write(Board_LED0, Board_LED_OFF);
while (1)
{
readRegister(i2c, 0x3B, &data, 14);
System_flush();
Task_sleep(1000);
}
/* Deinitialized I2C */
I2C_close(i2c);
System_printf("I2C closed!\n");
System_flush();
}
/*
* ======== main ========
*/
int main(void)
{
Task_Params taskParams;
/* Call board init functions */
Board_initGeneral();
Board_initGPIO();
Board_initI2C();
/* Construct nfcLoad Task thread */
Task_Params_init(&taskParams);
taskParams.stackSize = TASKSTACKSIZE;
taskParams.stack = &task0Stack;
taskParams.instance->name = "imusensor";
taskParams.priority = 1;
Task_construct(&task0Struct, (Task_FuncPtr)imuTask, &taskParams, NULL);
GPIO_write(Board_LED0, Board_LED_ON);
System_printf("Starting the I2C example\nSystem provider is set to SysMin."
" Halt the target to view any SysMin contents in ROV.\n");
/* SysMin will only print to the console when you call flush or exit */
System_flush();
srand(0xBEA5);
/* Start BIOS */
BIOS_start();
return (0);
}