CC1310 NoRTOS I2C&Timer interrupt&rftx问题

Other Parts Discussed in Thread: CC1310

TI工程师们以及前辈们您好:

在开发上遇上了问题,详细问题为在TI给我们的范例程式i2ctmp007中读取ADXL345 是没有问题的。可是我将I2C部份的代码移植到rfEasyLinkTx_nortos上并且加入了Timer Interrupt并在中断内读取I2C数据可是这样却没办法执行了。以下附上我的代码以及I2C讯号波形


开发环境为CCS V8,开发版为CC1310,使用I2C元件为ADXL345加速度计。

/*
 *  ======== rfEasyLinkTx_nortos.c ========
 */
/* Application header files */
#include "smartrf_settings/smartrf_settings.h"

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

/* Standard C Libraries */
#include <stdbool.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>

/* TI Drivers */ 
#include <ti/drivers/pin/PINCC26XX.h>
#include <ti/drivers/Power.h>
#include <ti/drivers/rf/RF.h>
#include <ti/devices/DeviceFamily.h>
#include <ti/drivers/timer/GPTimerCC26XX.h>

/* EasyLink API Header files */
#include "easylink/EasyLink.h"

/* Driverlib APIs */
#include DeviceFamily_constructPath(driverlib/sys_ctrl.h)
/* GP Timer Callback */
void rxTimeoutCb(GPTimerCC26XX_Handle handle,
                 GPTimerCC26XX_IntMask interruptMask);

static volatile bool rxDoneFlag;
static volatile bool rxTimeoutFlag;

/* GPTimer handle and timeout value */
GPTimerCC26XX_Handle hTimer;
GPTimerCC26XX_Value rxTimeoutVal;
int x, timer_flag = 0;

//=i2c
#include <ti/drivers/GPIO.h>
#include <ti/drivers/I2C.h>
uint8_t         txBuffer[3];
uint8_t         rxBuffer[6];
I2C_Handle      i2c;
I2C_Params      i2cParams;
I2C_Transaction i2cTransaction;
float accel_xout,accel_yout,accel_zout;

/* Undefine to not use async mode */
#define RFEASYLINKTX_ASYNC
#define RFEASYLINKTX_BURST_SIZE         10
#define RFEASYLINKTXPAYLOAD_LENGTH      30

/* Pin driver handle */
static PIN_Handle pinHandle;
static PIN_State pinState;

PIN_Config pinTable[] = {
	Board_PIN_LED1 | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX,
	Board_PIN_LED2 | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX,
#if defined __CC1352R1_LAUNCHXL_BOARD_H__
    Board_DIO30_RFSW | PIN_GPIO_OUTPUT_EN | PIN_GPIO_HIGH | PIN_PUSHPULL | PIN_DRVSTR_MAX,
#endif	
	PIN_TERMINATE
};

static uint16_t seqNumber;

#ifdef RFEASYLINKTX_ASYNC
static volatile bool txDoneFlag;
static volatile uint8_t txSleepPeriodsElapsed;
#endif //RFEASYLINKTX_ASYNC

#ifdef RFEASYLINKTX_ASYNC
void txDoneCb(EasyLink_Status status)
{
    if (status == EasyLink_Status_Success)
    {
        /* Toggle LED1 to indicate TX */
        PIN_setOutputValue(pinHandle, Board_PIN_LED1,!PIN_getOutputValue(Board_PIN_LED1));
    }
    else if(status == EasyLink_Status_Aborted)
    {
        /* Toggle LED2 to indicate command aborted */
        PIN_setOutputValue(pinHandle, Board_PIN_LED2,!PIN_getOutputValue(Board_PIN_LED2));
    }
    else
    {
        /* Toggle LED1 and LED2 to indicate error */
        PIN_setOutputValue(pinHandle, Board_PIN_LED1,!PIN_getOutputValue(Board_PIN_LED1));
        PIN_setOutputValue(pinHandle, Board_PIN_LED2,!PIN_getOutputValue(Board_PIN_LED2));
    }
    txDoneFlag = true;
    txSleepPeriodsElapsed = 0;
}
#endif //RFEASYLINKTX_ASYNC

void *mainThread(void *arg0)
{
    uint32_t absTime;

    GPIO_init();
    I2C_init();
    I2C_Params_init(&i2cParams);
    i2cParams.bitRate = I2C_100kHz;

    /* Open LED pins */
    pinHandle = PIN_open(&pinState, pinTable);
    if (pinHandle == NULL)
    {
        while(1);
    }

    /* Clear LED pins */
    PIN_setOutputValue(pinHandle, Board_PIN_LED1, 0);
    PIN_setOutputValue(pinHandle, Board_PIN_LED2, 0);

    static uint8_t txBurstSize = 0;


    GPTimerCC26XX_Params params;
    params.width          = GPT_CONFIG_32BIT;
    params.mode           = GPT_MODE_ONESHOT_UP;
    params.debugStallMode = GPTimerCC26XX_DEBUG_STALL_OFF;
    hTimer = GPTimerCC26XX_open(Board_GPTIMER0A, &params);
    if(hTimer == NULL)
    {
        while(1);
    }
    /* Set Timeout value to 300ms */
    rxTimeoutVal = (SysCtrlClockGet()*1UL)/9UL;
    GPTimerCC26XX_setLoadValue(hTimer, rxTimeoutVal);
    /* Register the GPTimer interrupt */
    GPTimerCC26XX_registerInterrupt(hTimer, rxTimeoutCb, GPT_INT_TIMEOUT);
    GPTimerCC26XX_start(hTimer);

#ifdef RFEASYLINKTX_ASYNC
    /* Reset the sleep period counter */
    txSleepPeriodsElapsed = 0;
    /* Set the transmission flag to its default state */
    txDoneFlag = false;
#endif //RFEASYLINKTX_ASYNC

	EasyLink_Params easyLink_params;
    EasyLink_Params_init(&easyLink_params);
	
	easyLink_params.ui32ModType = EasyLink_Phy_Custom; 

    if (EasyLink_init(&easyLink_params) != EasyLink_Status_Success){ 
		while(1); 
	}
    EasyLink_setRfPower(12);


    while(1) {
        EasyLink_TxPacket txPacket =  { {0}, 0, 0, {0} };

        /* Create packet with incrementing sequence number and random payload */
        txPacket.payload[0] = (uint8_t)(seqNumber >> 8);
        txPacket.payload[1] = (uint8_t)(seqNumber++);
        uint8_t i;
        for (i = 2; i < RFEASYLINKTXPAYLOAD_LENGTH; i++)
        {
            txPacket.payload[i] = rand();
        }

        txPacket.len = RFEASYLINKTXPAYLOAD_LENGTH;
        txPacket.dstAddr[0] = 0xaa;

        /* Add a Tx delay for > 500ms, so that the abort kicks in and brakes the burst */
        if(EasyLink_getAbsTime(&absTime) != EasyLink_Status_Success)
        {
            // Problem getting absolute time
        }
        if(txBurstSize++ >= RFEASYLINKTX_BURST_SIZE)
        {
            /* Set Tx absolute time to current time + 1s */
            
            txPacket.absTime = absTime + EasyLink_ms_To_RadioTime(1000);
            txBurstSize = 0;
        }
        /* Else set the next packet in burst to Tx in 100ms */
        else
        {
            /* Set Tx absolute time to current time + 100ms */
            txPacket.absTime = absTime + EasyLink_ms_To_RadioTime(100);
        }

#ifdef RFEASYLINKTX_ASYNC
        /*
         * Set the Transmit done flag to false, callback will set it to true
         * Also set the sleep counter to 0
         */
        txDoneFlag = false;
        txSleepPeriodsElapsed = 0;

        /* Transmit the packet */
        EasyLink_transmitAsync(&txPacket, txDoneCb);

        while(!txDoneFlag){
            /*
             * Set the device to sleep for 108ms. The packet transmission is
             * set 100 ms in the future but takes about 7ms to complete and
             * for the execution to hit the callback. A 1ms buffer is added to
             * the sleep time to ensure the callback always execute prior to
             * the end of usleep().
             */
            usleep(108000);

            /* check to see if the transmit flag was set during sleep */
            if(!txDoneFlag){
                txSleepPeriodsElapsed++;
                if(txSleepPeriodsElapsed == 3){
                    /* 324 ms have passed. We need to abort the transmission */
                    if(EasyLink_abort() == EasyLink_Status_Success)
                    {
                        /*
                         * Abort will cause the txDoneCb to be called and the
                         * txDoneFlag to be set
                         */
                        while(!txDoneFlag){};
                    }
                    break;
                }
            }
        }
#else
        EasyLink_Status result = EasyLink_transmit(&txPacket);

        if (result == EasyLink_Status_Success)
        {
            /* Toggle LED1 to indicate TX */
            PIN_setOutputValue(pinHandle, Board_PIN_LED1,!PIN_getOutputValue(Board_PIN_LED1));
        }
        else
        {
            /* Toggle LED1 and LED2 to indicate error */
            PIN_setOutputValue(pinHandle, Board_PIN_LED1,!PIN_getOutputValue(Board_PIN_LED1));
            PIN_setOutputValue(pinHandle, Board_PIN_LED2,!PIN_getOutputValue(Board_PIN_LED2));
        }
#endif //RFEASYLINKTX_ASYNC
    }
}
void rxTimeoutCb(GPTimerCC26XX_Handle handle,
                 GPTimerCC26XX_IntMask interruptMask)
{
    timer_flag = 1;
    /* Set the Timeout Flag */
    rxTimeoutFlag = true;
    x = x+1;
    /*
     * Timer is automatically stopped in one-shot mode and needs to be reset by
     * loading the interval load value
     */

    i2c = I2C_open(Board_I2C_TMP, &i2cParams);
    txBuffer[0] = 0x32;
    txBuffer[1] = 0xa7;
    i2cTransaction.slaveAddress = 0x53;
    i2cTransaction.writeBuf = txBuffer;
    i2cTransaction.writeCount = 2;
    i2cTransaction.readBuf = rxBuffer;
    i2cTransaction.readCount = 6;
    if (I2C_transfer(i2c, &i2cTransaction))
    {
        accel_xout = (((rxBuffer[0] + (rxBuffer[1]<<8)))*0.00001526);
        accel_yout = (((rxBuffer[2] + (rxBuffer[3]<<8)))*0.00001526);
        accel_zout = (((rxBuffer[4] + (rxBuffer[5]<<8)))*0.00001526);
    }

    I2C_close(i2c);

    GPTimerCC26XX_setLoadValue(hTimer, rxTimeoutVal);
    timer_flag = 0;
    if(timer_flag == 0)  GPTimerCC26XX_start(hTimer);
}