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.

CCS/CC3235S: Not getting data over spi, i am connecting ADE9153A shield with my launchpad cc3235s using spimaster program but it's not working for me, i am getting nothing.

Part Number: CC3235S

Tool/software: Code Composer Studio

Hi TI,

I am using cc3235s launchpad and I am using spimaster spi driver program from SDK at the location "C:\ti\simplelink_cc32xx_sdk_3_30_01_02\examples\rtos\CC3235S_LAUNCHXL\drivers\spimaster".I want to connect EV-ADE9153ASHIELDZ , it is Arduino compatible shield board. but I want to hook up it with my launchpad by using spimaster driver program. According to the datasheet 0x0242 is the register, which returns version of the product. but it's giving me some garbage value.

here is my code.

/*
 * Copyright (c) 2018-2019, 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.
 */

/*
 *  ======== spimaster.c ========
 */
#include <stddef.h>
#include <stdint.h>
#include <string.h>

//#include  <ADE9153A.h>
//#include <ADE9153AAPI.h>

/* POSIX Header files */
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>

/* Driver Header files */
#include <ti/drivers/GPIO.h>
#include <ti/drivers/SPI.h>
#include <ti/display/Display.h>

/* Driver configuration */
#include "ti_drivers_config.h"
#define SPI_SPEED  1000000     //SPI Speed

#define THREADSTACKSIZE (1024)

#define SPI_MSG_LENGTH  (30)
#define MASTER_MSG      ("Hello from master, msg#: ")
#define REG_RUN           0x0480    /* Write this register to 1 to start the measurements. */
#define ADE9153A_RUN_ON   0x0001  /*DSP On*/
#define REG_VERSION_PRODUCT 0x0242    /* This register indicates the version of the product being used. */

#define MAX_LOOP        (10)

static Display_Handle display;

uint32_t masterRxBuffer[SPI_MSG_LENGTH];
uint16_t masterTxBuffer[SPI_MSG_LENGTH];

//uint32_t masterRxBuffer;
//uint32_t masterTxBuffer;


//struct EnergyRegs energyVals;  //Energy register values are read and stored in EnergyRegs structure
//struct PowerRegs powerVals;    //Metrology data can be accessed from these structures
//struct RMSRegs rmsVals;
//struct PQRegs pqVals;
//struct AcalRegs acalVals;
//struct Temperature tempVal;

void readandwrite(void);
void resetADE9153A(void);
void Energy_Measurement(void);
void SPI_Write_16(uint16_t, uint16_t);
void spi_Write_16(uint16_t address);
void readyToSend(uint16_t);
uint32_t  SPI_Read_32(uint16_t);

bool            transferOK;
SPI_Handle      masterSpi;
SPI_Params      spiParams;
SPI_Transaction trans;
uint32_t        i;
int32_t         status;

/* Semaphore to block master until slave is ready for transfer */
sem_t masterSem;

/*
 *  ======== slaveReadyFxn ========
 *  Callback function for the GPIO interrupt on CONFIG_SPI_SLAVE_READY.
 */
void slaveReadyFxn(uint_least8_t index)
{
    sem_post(&masterSem);
}


/*
 *  ======== transferCompleteFxn ========
 *  Callback function for SPI_transfer().
 */
void transferCompleteFxn(SPI_Handle handle, SPI_Transaction *transaction)
{
    sem_post(&masterSem);
}

/*
 *  ======== masterThread ========
 *  Master SPI sends a message to slave while simultaneously receiving a
 *  message from the slave.
 */
void *masterThread(void *arg0)
{

    /*
     * CONFIG_SPI_MASTER_READY & CONFIG_SPI_SLAVE_READY are GPIO pins connected
     * between the master & slave.  These pins are used to synchronize
     * the master & slave applications via a small 'handshake'.  The pins
     * are later used to synchronize transfers & ensure the master will not
     * start a transfer until the slave is ready.  These pins behave
     * differently between spimaster & spislave examples:
     *
     * spimaster example:
     *     * CONFIG_SPI_MASTER_READY is configured as an output pin.  During the
     *       'handshake' this pin is changed from low to high output.  This
     *       notifies the slave the master is ready to run the application.
     *       Afterwards, the pin is used by the master to notify the slave it
     *       has opened CONFIG_SPI_MASTER.  When CONFIG_SPI_MASTER is opened, this
     *       pin will be pulled low.
     *
     *     * CONFIG_SPI_SLAVE_READY is configured as an input pin. During the
     *       'handshake' this pin is read & a high value will indicate the slave
     *       ready to run the application.  Afterwards, a falling edge interrupt
     *       will be configured on this pin.  When the slave is ready to perform
     *       a transfer, it will pull this pin low.
     *
     * Below we set CONFIG_SPI_MASTER_READY & CONFIG_SPI_SLAVE_READY initial
     * conditions for the 'handshake'.
     */
        GPIO_setConfig(CONFIG_SPI_MASTER_READY, GPIO_CFG_OUTPUT | GPIO_CFG_OUT_LOW);
        GPIO_setConfig(CONFIG_SPI_SLAVE_READY, GPIO_CFG_INPUT);

    /*
     * Handshake - Set CONFIG_SPI_MASTER_READY high to indicate master is ready
     * to run.  Wait CONFIG_SPI_SLAVE_READY to be high.
     */
        GPIO_write(CONFIG_SPI_MASTER_READY, 1);
        while (GPIO_read(CONFIG_SPI_SLAVE_READY) == 0){}

        /* Handshake complete; now configure interrupt on CONFIG_SPI_SLAVE_READY */
        GPIO_setConfig(CONFIG_SPI_SLAVE_READY, GPIO_CFG_IN_PU | GPIO_CFG_IN_INT_FALLING);
        GPIO_setCallback(CONFIG_SPI_SLAVE_READY, slaveReadyFxn);
        GPIO_enableInt(CONFIG_SPI_SLAVE_READY);

    /*
     * Create synchronization semaphore; the master will wait on this semaphore
     * until the slave is ready.
     */
    status = sem_init(&masterSem, 0, 0);
    if (status != 0) {
        Display_printf(display, 0, 0, "Error creating masterSem\n");

        while(1);
    }

        /*
         * Master has opened CONFIG_SPI_MASTER; set CONFIG_SPI_MASTER_READY high to
         * inform the slave.
         */
        GPIO_write(CONFIG_SPI_MASTER_READY, 0);

//    /* Copy message to transmit buffer */
//    strncpy((char *) masterTxBuffer, MASTER_MSG, SPI_MSG_LENGTH);

        Energy_Measurement();

//    for (i = 0; i < MAX_LOOP; i++) {
//        /*
//         * Wait until slave is ready for transfer; slave will pull
//         * CONFIG_SPI_SLAVE_READY low.
//         */
//        sem_wait(&masterSem);
//
//        /* Initialize master SPI transaction structure */
//        masterTxBuffer[sizeof(MASTER_MSG) - 1] = (i % 10) + '0';
//        memset((void *) masterRxBuffer, 0, SPI_MSG_LENGTH);
//        transaction.count = SPI_MSG_LENGTH;
//        transaction.txBuf = (void *) masterTxBuffer;
//        transaction.rxBuf = (void *) masterRxBuffer;
//
//        /* Toggle user LED, indicating a SPI transfer is in progress */
//        GPIO_toggle(CONFIG_GPIO_LED_1);
//
//        /* Perform SPI transfer */
//        transferOK = SPI_transfer(masterSpi, &transaction);
//        if (transferOK) {
//            Display_printf(display, 0, 0, "Master received: %s", masterRxBuffer);
//        }
//        else {
//            Display_printf(display, 0, 0, "Unsuccessful master SPI transfer");
//        }

        /* Sleep for a bit before starting the next SPI transfer  */
        sleep(3);



    /* Example complete - set pins to a known state */
    GPIO_disableInt(CONFIG_SPI_SLAVE_READY);
    GPIO_setConfig(CONFIG_SPI_SLAVE_READY, GPIO_CFG_OUTPUT | GPIO_CFG_OUT_LOW);
    GPIO_write(CONFIG_SPI_MASTER_READY, 0);

    Display_printf(display, 0, 0, "\nDone");

    return (NULL);
}

/*
 *  ======== mainThread ========
 */
void *mainThread(void *arg0)
{
    pthread_t           thread0;
    pthread_attr_t      attrs;
    struct sched_param  priParam;
    int                 retc;
    int                 detachState;

    /* Call driver init functions. */
    Display_init();
    GPIO_init();
    SPI_init();

    /* Configure the LED pins */
    GPIO_setConfig(CONFIG_GPIO_LED_0, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);
    GPIO_setConfig(CONFIG_GPIO_LED_1, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);

    /* Open the display for output */
    display = Display_open(Display_Type_UART, NULL);
    if (display == NULL) {
        /* Failed to open display driver */
        while (1);
    }

    /* Turn on user LED */
    GPIO_write(CONFIG_GPIO_LED_0, CONFIG_GPIO_LED_ON);

    Display_printf(display, 0, 0, "Starting the SPI master example");
    Display_printf(display, 0, 0, "This example requires external wires to be "
        "connected to the header pins. Please see the Board.html for details.\n");

    /* Create application threads */
    pthread_attr_init(&attrs);

    detachState = PTHREAD_CREATE_DETACHED;
    /* Set priority and stack size attributes */
    retc = pthread_attr_setdetachstate(&attrs, detachState);
    if (retc != 0) {
        /* pthread_attr_setdetachstate() failed */
        while (1);
    }

    retc |= pthread_attr_setstacksize(&attrs, THREADSTACKSIZE);
    if (retc != 0) {
        /* pthread_attr_setstacksize() failed */
        while (1);
    }

    /* Create master thread */
    priParam.sched_priority = 1;
    pthread_attr_setschedparam(&attrs, &priParam);

    retc = pthread_create(&thread0, &attrs, masterThread, NULL);
    if (retc != 0) {
        /* pthread_create() failed */
        while (1);
    }

    return (NULL);
}

void Energy_Measurement()
{
    resetADE9153A();
    sleep(1);

     GPIO_write(chipSelect_Pin, 1);     //Set Chip select pin high

    /* Open SPI as master (default) */
        SPI_Params_init(&spiParams);
        spiParams.dataSize = 16;
        spiParams.frameFormat =  SPI_POL0_PHA1;
        spiParams.bitRate = SPI_SPEED;
        spiParams.transferCallbackFxn = transferCompleteFxn;
        spiParams.transferMode = SPI_MODE_CALLBACK;
        spiParams.mode=SPI_MASTER;
        masterSpi = SPI_open(CONFIG_SPI_MASTER, &spiParams);
        if (masterSpi == NULL) {
            Display_printf(display, 0, 0, "Error initializing master SPI\n");
            while (1);
        }
        else {
            Display_printf(display, 0, 0, "Master SPI initialized\n");
        }

        /*
         * Master has opened CONFIG_SPI_MASTER; set CONFIG_SPI_MASTER_READY high to
         * inform the slave.
         */
        GPIO_write(CONFIG_SPI_MASTER_READY, 0);

        SPI_Write_16(REG_RUN,ADE9153A_RUN_ON);
        usleep(100000);

        Display_printf(display, 0, 0, "IC Version is : %d ",(SPI_Read_32(REG_VERSION_PRODUCT)));

        SPI_close(masterSpi);
}

void resetADE9153A()
{
 GPIO_write(ADE9153A_RESET_PIN, 0);
 usleep(100000);
 GPIO_write(ADE9153A_RESET_PIN, 1);
 sleep(1);
// Serial.println("Reset Done");
 Display_printf(display, 0, 0, "\nReset Done");
}

void SPI_Write_16(uint16_t Address , uint16_t Data)
{

    uint16_t temp_address;

    /* Bitbanging attempt for Chip Select */
    GPIO_write(chipSelect_Pin, 0);
    temp_address = ((Address << 4) & 0xFFF0);   //shift address
    masterTxBuffer[0] = temp_address;//((1<<14) | (address & 0x1F) << 8 ) ;
    masterTxBuffer[1] = Data;

    memset((void *) masterRxBuffer, 0, SPI_MSG_LENGTH);
    trans.count = SPI_MSG_LENGTH;
    trans.txBuf = (void *) masterTxBuffer;
    trans.rxBuf = (void *) masterRxBuffer;

       /* Perform SPI transfer */
           transferOK = SPI_transfer(masterSpi, &trans);
           if (!transferOK){
               Display_printf(display, 0, 0, "Unsuccessful SPI transfer\n");
               while(1);
           }
           else{
                   /* Wait until transfer has completed */
                   sem_wait(&masterSem);
           }

       /* Bitbanging attempt for Chip Select */
           GPIO_write(chipSelect_Pin, 1);
          // GPIO_write(Board_GPIO_LED1, Board_GPIO_LED_OFF);
           sleep(0.5);
}

void spi_Write_16(uint16_t address)
{
    //uint16_t temp_address;

    /* Bitbanging attempt for Chip Select */
    GPIO_write(chipSelect_Pin, 0);
    //temp_address = ((address << 4) & 0xFFF0);   //shift address

    masterTxBuffer[0] = address;
    memset((void *) masterRxBuffer, 0, SPI_MSG_LENGTH);
    trans.count = SPI_MSG_LENGTH;
    trans.txBuf = (void *) masterTxBuffer;
    trans.rxBuf = (void *) masterRxBuffer;

    /* Bitbanging attempt for Chip Select */
       GPIO_write(chipSelect_Pin, 0);

       /* Perform SPI transfer */
           transferOK = SPI_transfer(masterSpi, &trans);
           if (!transferOK){
               Display_printf(display, 0, 0, "Unsuccessful SPI transfer\n");
               while(1);
           }
           else
           {
                   /* Wait until transfer has completed */
                   sem_wait(&masterSem);
           }

       /* Bitbanging attempt for Chip Select */
           GPIO_write(chipSelect_Pin, 1);
          // GPIO_write(Board_GPIO_LED1, Board_GPIO_LED_OFF);
           sleep(0.5);
}

uint32_t  SPI_Read_32(uint16_t Address)
{
        uint16_t temp_address;
        uint32_t *temp_highpacket;
        //uint16_t *temp_lowpacket;
        uint32_t *temp_ptr;
        uint32_t returnData;

        GPIO_write(chipSelect_Pin, 0);
        temp_address = (((Address << 4) & 0xFFF0) + 8);
        spi_Write_16(temp_address);
        spi_Write_16(0);
        Display_printf(display, 0, 0, "temp_highpacket is :%d", masterRxBuffer);
        temp_highpacket=masterRxBuffer;
        spi_Write_16(0);
        Display_printf(display, 0, 0, "temp_lowpacket is :%d", masterRxBuffer);
        temp_ptr = masterRxBuffer;
        *temp_ptr=*temp_ptr>>16;
        returnData=*temp_highpacket<<16;
        returnData = returnData + *temp_ptr;
        return returnData;
}



here i am trying to get product version by writing register value (0x0242) by using
SPI_Read_32(REG_VERSION_PRODUCT) function which should return me product version which is 0x0009153A

but i am not getting, i am doing exactly written in arduino code which came with ADE9153A shield board, and it is working correcly with arduino, but it's not working with my CC3235S.

Please help me in that , all suggestions are welcome.


waiting for your reply...
sarju bhatngar
  • Hi Sarju,

    Have you double-checked that the SPI clock phase, polarity, and rate are correct? Have you also checked that all of the SPI connections are connected correctly? Also, have you checked to see if the CS signal uses the correct polarity? Those are the most common issues to check when debugging SPI connections.

    Something you may find useful to debug is to perform logic analyzer captures to observe what is different between the arduino and the CC3235 performing the same basic SPI transaction. Are you able to perform those logic analyzer captures and see if there are any differences in the signals?


    Regards,

    Michael

  • Thanks you so much Michael, 

    My problem has been resolved, now i am getting data.

    once again thanks a lot.

    sarju bhatnagar