This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CC2650 ADXL345 I2C problem question

Other Parts Discussed in Thread: CC2640

Hello,

could anybody help me in determining what is wrong with the code below (it attempts to read dev_id register on ADXL345 via I2C). The problem is that dev_id is always 0 after I2C transfer instead of 0xE5 declared by producer. 

/*
 * 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.
 */

/*
 *  ======== lab1-main.c ========
 */

/* XDCtools Header files */
#include <xdc/std.h>
#include <xdc/cfg/global.h>
#include <xdc/runtime/System.h>

/* BIOS Header files */
#include <ti/sysbios/BIOS.h>
#include <ti/sysbios/knl/Task.h>

/* TI-RTOS Header files */
#include <ti/drivers/PIN.h>
#include <ti/drivers/pin/PINCC26XX.h>

/* Example/Board Header files */
#include "Board.h"

/* Driverlib CPU functions, used here for CPUdelay*/
#include <driverlib/cpu.h>
#include <ti/drivers/i2c/I2CCC26XX.h>

/* Could be anything, like computing primes */
#define FakeBlockingSlowWork()   CPUdelay(12e6)
#define FakeBlockingFastWork()   CPUdelay(3e6)

/* Pin driver handles */
static PIN_Handle pinHandle;

/* Global memory storage for a PIN_Config table */
static PIN_State pinState;

Task_Struct workTask;
static uint8_t workTaskStack[256];

bool transferDone = false;

static const I2CCC26XX_I2CPinCfg pinCfg =
{
   // Pin configuration for I2C interface 1
  .pinSDA = Board_I2C0_SDA0,
  .pinSCL = Board_I2C0_SCL0
};

/*
 * Initial pin configuration table
 *   - LEDs Board_LED0 & Board_LED1 are off after the pin table is initialized.
 *   - Button is set to input with pull-up. On SmartRF06, BUTTON0 is UP, for the
 *     others it's the LEFT button.
 */
PIN_Config pinTable[] = {
    Board_LED0 | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX,
	Board_ADXL_PWR| PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX,
	PIN_TERMINATE
};

void doWork(void)
{
	PIN_setOutputValue(pinHandle, Board_LED0, 1);
        FakeBlockingSlowWork(); /* Pretend to do something useful but time-consuming */
	PIN_setOutputValue(pinHandle, Board_LED0, 0);
}

Void workTaskFunc(UArg arg0, UArg arg1)
{
    while (1) {

    	/* Do work */
    	doWork();

    	/* Wait a while, because doWork should be a periodic thing, not continuous.*/
    	CPUdelay(24e6);
    }
}


void transferCallback(I2C_Handle handle, I2C_Transaction *transac, bool result)
{
	if (result)
	{
		transferDone = true;
	}
	else
	{
		System_abort("Transfer failed\n");
	}
}
/*
 *  ======== main ========
 *
 */
int main(void)
{
	uint8_t dev_id = 0;

	/* Call board init functions */
	PIN_init(BoardGpioInitTable);

    /* Open LED pins */
    pinHandle = PIN_open(&pinState, pinTable);
    if(!pinHandle) {
        System_abort("Error initializing board pins\n");
    }

    PIN_setOutputValue(pinHandle, Board_ADXL_PWR, 1);

   uint8_t txBuffer[2] = {0};
   uint8_t rxBuffer[2] = {0};

   I2C_Handle i2c;
   I2C_Params i2cParams;
   I2C_Transaction i2cTransaction;

   I2C_Params_init(&i2cParams);     // sets custom to NULL

   i2cParams.custom = (void *)&pinCfg;
   i2cParams.bitRate = I2C_400kHz;
   i2cParams.transferMode = I2C_MODE_CALLBACK;
   i2cParams.transferCallbackFxn = transferCallback;

   i2c = I2C_open(Board_I2C, &i2cParams);
   if (i2c == NULL)
   {
      System_abort("Error initializing I2C\n");
   }
   else
   {
     System_printf("I2C Initialized\n");
   }

   i2cTransaction.slaveAddress = 0x53;

   i2cTransaction.writeBuf = txBuffer;
   i2cTransaction.writeCount = 1;

   txBuffer[0] = 0x00;
   txBuffer[1] = 0;

   i2cTransaction.readBuf = rxBuffer;
   i2cTransaction.readCount = 1;

   I2C_transfer(i2c, &i2cTransaction);

   dev_id = rxBuffer[0];
   System_printf("dev_id: %04x", dev_id);

   I2C_close(i2c);
   System_printf("I2C closed\n");

  /* Set up the led task */
  Task_Params workTaskParams;
  Task_Params_init(&workTaskParams);
  workTaskParams.stackSize = 256;
  workTaskParams.priority = 2;
  workTaskParams.stack = &workTaskStack;

  Task_construct(&workTask, workTaskFunc, &workTaskParams, NULL);

  /* Start kernel. */
  BIOS_start();

  return (0);
}

Any help appreciated. Thanks in advance.

Kind regards,

Andrew

  • Hello,

    Can you give us some more details about your setup? Which TI dev board are you using? Are you using the TI-RTOS Lab 1 example project as a base? If so, which version of SimpleLink Academy do you have installed?

    Have you looked at the I2C lines using a logic analyzer?
  • Hello,

    I am using the following custom board:

    Yes, I am using TI-TROS Lab 1 example as a base for my project. I am using SimpleLink Academy 1.07,00.0000. I have not used logic analyzer yet.

    Kind regards,

    Andrew

  • Hi Andrew,

    A logic analyzer capture of the i2c lines may provide some useful information about why the communication is failing.

    Do you have a TI dev board that you can use for debugging? Can you use the example project from the latest version of SimpleLink Academy?
  • Hi User,


    Are you successfully read data through ADXL-345 sensor using cc2640 via i2c interface?


    If so please give me your success code sample example so I can implement it in my code.

    Please let me know.

    Thanks

    Regards,

    Raj