Because of the Thanksgiving holiday in the U.S., TI E2E™ design support forum responses may be delayed from November 25 through December 2. Thank you for your patience.

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.

SPI communication problem

dear all,

In my project i wants to communicate with ade7763 energy meter ic through SPI by using TMS320f28027 .

for example if i send mode register address (8 bit) 0x09 means it will return (16 bit) 0x000C from that register.now i checked the MISO signal by CRO it gives the same output 0x000C .but the value read by my spi code is data1= 255 and data2= 0

i tried with separate GPIO for cs .but the same problem is continuing .please help me to solve this problem

my code 

while(1)
{

SPI_write(mySpi, 0x09<<8);
DELAY_US(6);
SPI_write(mySpi, 0x00); // dummy data 1
DELAY_US(4);
while(SPI_getRxFifoStatus(mySpi) == SPI_FifoStatus_Empty){
}
data1=SPI_read(mySpi);
SPI_write(mySpi, 0x00); // dummy data 2
DELAY_US(4);
while(SPI_getRxFifoStatus(mySpi) == SPI_FifoStatus_Empty){
}
data2=SPI_read(mySpi);

printf("data1:%d data2:%d\r\n",data1,data2);

for(i=0;i<100;i++)
DELAY_US(10000);
}


Regards
Ramesh

  • Ramesh,

    The code you shared looks like it is correct. You are transmitting three 8-bit words: an 8-bit command properly shifted to the upper byte, and two dummy words to clock out the data from the slave device. How are you handling the chip select? Are you relying on the native SPISTE? If you are, the chip select line will toggle between each word.

    Can you share the scope capture?
    -Mark
  • Dear sir,

    This one is my complete code  and spi waveform


    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    #include <file.h>
    #include "DSP28x_Project.h"     // DSP28x Headerfile
    #include "f2802x_common/include/clk.h"
    #include "f2802x_common/include/flash.h"
    #include "f2802x_common/include/gpio.h"
    #include "f2802x_common/include/pie.h"
    #include "f2802x_common/include/pll.h"
    #include "f2802x_common/include/sci.h"
    #include "f2802x_common/include/sci_io.h"
    #include "f2802x_common/include/spi.h"
    #include "f2802x_common/include/wdog.h"
    CLK_Handle   myClk;
    FLASH_Handle myFlash;
    GPIO_Handle  myGpio;
    PIE_Handle   myPie;
    SCI_Handle   mySci;
    SPI_Handle   mySpi;
    /*__________________________definition and declarations__________________________*/
    void SCIA_initialize();
    void delay_loop(void);
    void spi_init(void);
    void spi_fifo_init(void);
    void error();
    /*_____________________________variables used for SCI_____________________________*/
    #define MAX_FRAME_SIZE  64                 //bytes
    #define START_OF_FRAME  '*'
    #define END_OF_FRAME    '#'
    unsigned char uart0_buf[MAX_FRAME_SIZE];
    unsigned char serial_data_count;
    unsigned char string_match_flag1=1;
    unsigned char string_match_flag2=1;
    unsigned char string_match_flag3=1;
    unsigned char string_match_flag4=1;
    char data_avail=0;
    char temp_str[7];
    char *p;
    int i;
    uint16_t data=0,data1=0,data2=0,data3=0;
    /*_______________________________________________interrupt service routine________________________________________________*/
    interrupt void sciaRxFifoIsr(void)
    {
    if(SciaRegs.SCIRXBUF.all==START_OF_FRAME)                                  // starting character of string (eg: '*')
    {
    serial_data_count=0;
    uart0_buf[serial_data_count]=SciaRegs.SCIRXBUF.all;
    serial_data_count++;
    }
    else if(SciaRegs.SCIRXBUF.all==END_OF_FRAME )                              // ending character of string (eg: '#')
    {
    uart0_buf[serial_data_count]=END_OF_FRAME;
    serial_data_count++;
    uart0_buf[serial_data_count]='\0';
    data_avail=1;
    }
    else if(serial_data_count<MAX_FRAME_SIZE)
    {
    uart0_buf[serial_data_count]=SciaRegs.SCIRXBUF.all;
    serial_data_count++;
    }
        SCI_clearRxFifoOvf(mySci);                                                //  Clear Overflow flag
        SCI_clearRxFifoInt(mySci);                                                //  Clear Interrupt flag
        PIE_clearInt(myPie, PIE_GroupNumber_9);                                   //  Issue PIE ack
        return;
    }
    void main()
      {
        volatile int status = 0;
        volatile FILE *fid;
        CPU_Handle  myCpu;
        PLL_Handle  myPll;
        WDOG_Handle myWDog;
        // Initialize all the handles needed for this application
        myClk   = CLK_init((void *)CLK_BASE_ADDR, sizeof(CLK_Obj));
        myCpu   = CPU_init((void *)NULL, sizeof(CPU_Obj));
        myFlash = FLASH_init((void *)FLASH_BASE_ADDR, sizeof(FLASH_Obj));
        myGpio  = GPIO_init((void *)GPIO_BASE_ADDR, sizeof(GPIO_Obj));
        myPie   = PIE_init((void *)PIE_BASE_ADDR, sizeof(PIE_Obj));
        myPll   = PLL_init((void *)PLL_BASE_ADDR, sizeof(PLL_Obj));
        mySci   = SCI_init((void *)SCIA_BASE_ADDR, sizeof(SCI_Obj));
        mySpi   = SPI_init((void *)SPIA_BASE_ADDR, sizeof(SPI_Obj));
        myWDog  = WDOG_init((void *)WDOG_BASE_ADDR, sizeof(WDOG_Obj));
        // Perform basic system initialization
        WDOG_disable(myWDog);
        CLK_enableAdcClock(myClk);
        (*Device_cal)();
        //Select the internal oscillator 1 as the clock source
        CLK_setOscSrc(myClk, CLK_OscSrc_Internal);
        // Setup the PLL for x10 /2 which will yield 50Mhz = 10Mhz * 10 / 2
        PLL_setup(myPll, PLL_Multiplier_12, PLL_DivideSelect_ClkIn_by_2);
        // Disable the PIE and all interrupts
        PIE_disable(myPie);
        PIE_disableAllInts(myPie);
        CPU_disableGlobalInts(myCpu);
        CPU_clearIntFlags(myCpu);
        // If running from flash copy RAM only functions to RAM
    #ifdef _FLASH
    memcpy(&RamfuncsRunStart, &RamfuncsLoadStart, (size_t)&RamfuncsLoadSize);
    #endif
        // Setup a debug vector table and enable the PIE
        PIE_setDebugIntVectorTable(myPie);
        PIE_enable(myPie);
        PIE_registerPieIntHandler(myPie, PIE_GroupNumber_9, PIE_SubGroupNumber_1, (intVec_t)&sciaRxFifoIsr);
        PIE_enableInt(myPie, PIE_GroupNumber_9, PIE_InterruptSource_SCIARX);
        CPU_enableInt(myCpu, CPU_IntNumber_9);
        CPU_enableGlobalInts(myCpu);
    SCIA_initialize();
        spi_init();
        spi_fifo_init();
        // Set the flash OTP wait-states to minimum. This is important
        // for the performance of the temperature conversion function.
        FLASH_setup(myFlash);
        /*_____________________________initialization of gpio___________________________*/
        // Initalize GPIO for sci communication
        GPIO_setMode(myGpio, GPIO_Number_28, GPIO_28_Mode_SCIRXDA);
        GPIO_setMode(myGpio, GPIO_Number_29, GPIO_29_Mode_SCITXDA);
        GPIO_setPullUp(myGpio, GPIO_Number_28, GPIO_PullUp_Enable);
        GPIO_setPullUp(myGpio, GPIO_Number_29, GPIO_PullUp_Disable);
        GPIO_setQualification(myGpio, GPIO_Number_28, GPIO_Qual_ASync);
        // Initalize GPIO
        GPIO_setPullUp(myGpio, GPIO_Number_16, GPIO_PullUp_Enable);          //mosi
        GPIO_setPullUp(myGpio, GPIO_Number_17, GPIO_PullUp_Enable);          //miso
        GPIO_setPullUp(myGpio, GPIO_Number_18, GPIO_PullUp_Enable);          //sclk
        //GPIO_setPullUp(myGpio, GPIO_Number_19, GPIO_PullUp_Enable);          //ssel
        GPIO_setQualification(myGpio, GPIO_Number_16, GPIO_Qual_ASync);
        GPIO_setQualification(myGpio, GPIO_Number_17, GPIO_Qual_ASync);
        GPIO_setQualification(myGpio, GPIO_Number_18, GPIO_Qual_ASync);
        //GPIO_setQualification(myGpio, GPIO_Number_19, GPIO_Qual_ASync);
        GPIO_setMode(myGpio, GPIO_Number_16, GPIO_16_Mode_SPISIMOA);
        GPIO_setMode(myGpio, GPIO_Number_17, GPIO_17_Mode_SPISOMIA);
        GPIO_setMode(myGpio, GPIO_Number_18, GPIO_18_Mode_SPICLKA);
        //GPIO_setMode(myGpio, GPIO_Number_19, GPIO_19_Mode_SPISTEA_NOT);
        GPIO_setMode(myGpio, GPIO_Number_19, GPIO_19_Mode_GeneralPurpose);         // ADE7763 Chip select
        GPIO_setDirection(myGpio, GPIO_Number_19, GPIO_Direction_Output);
        GPIO_setMode(myGpio, GPIO_Number_1, GPIO_1_Mode_GeneralPurpose);           // TXS0108 Chip select
        GPIO_setDirection(myGpio, GPIO_Number_1, GPIO_Direction_Output);
        GPIO_setHigh(myGpio, GPIO_Number_1);        // TXS 0108  chip select
        GPIO_setHigh(myGpio, GPIO_Number_19);       // ADE7763 Chip select
        status = add_device("scia", _SSA, SCI_open, SCI_close, SCI_read, SCI_write, SCI_lseek, SCI_unlink, SCI_rename);
        fid = fopen("scia","w");
        freopen("scia:", "w", stdout);
        setvbuf(stdout, NULL, _IONBF, 0);
    // Enable global Interrupts and higher priority real-time debug events
    CPU_enableGlobalInts(myCpu);
    CPU_enableDebugInt(myCpu);
    CLK_disableTbClockSync(myClk);
    CLK_enableTbClockSync(myClk);
    DELAY_US(500);
    while(1)
    {
        GPIO_setLow(myGpio, GPIO_Number_19);
        DELAY_US(2);
       SPI_write(mySpi, 0x09<<8);
       DELAY_US(6);
       SPI_write(mySpi, 0x00<<8);
       DELAY_US(4);
       while(SPI_getRxFifoStatus(mySpi) == SPI_FifoStatus_Empty){
       }
       data1=SPI_read(mySpi);
       SPI_write(mySpi, 0x00<<8);
       DELAY_US(4);
       while(SPI_getRxFifoStatus(mySpi) == SPI_FifoStatus_Empty){
       }
       data2=SPI_read(mySpi);
        DELAY_US(2);
        GPIO_setHigh(myGpio, GPIO_Number_19);
       printf("data1:%d  data2:%d  data3:%d\r\n",data1,data2,data3);
       for(i=0;i<100;i++)
        DELAY_US(10000);
    }
    }
    /*_________________________________________________SCI initialize_________________________________________________*/
    // SCIA  8-bit word, baud rate 0x000F, default, 1 STOP bit, no parity
    void SCIA_initialize()
    {
    CLK_enableSciaClock(myClk);
    // 1 stop bit,  No loopback
    // No parity,8 char bits,
    // async mode, idle-line protocol
    SCI_disableParity(mySci);
    SCI_setNumStopBits(mySci, SCI_NumStopBits_One);
    SCI_setCharLength(mySci, SCI_CharLength_8_Bits);
    SCI_enableTx(mySci);
    SCI_enableRx(mySci);
    SCI_enableTxInt(mySci);
    SCI_enableRxInt(mySci);
    // SCI BRR = LSPCLK/(SCI BAUDx8) - 1
    // Configured for 115.2kbps
    #if (CPU_FRQ_60MHZ)
    SCI_setBaudRate(mySci, SCI_BaudRate_9_6_kBaud );     //   SCI_BaudRate_115_2_kBaud
    #elif (CPU_FRQ_50MHZ)
    SCI_setBaudRate(mySci, (SCI_BaudRate_e)13);
    #elif (CPU_FRQ_40MHZ)
    SCI_setBaudRate(mySci, (SCI_BaudRate_e)10);
    #endif
    SCI_enable(mySci);
    return;
    }
    /*_________________________________________________SPI initialize_________________________________________________*/
    void delay_loop()
    {
        long      i;
        for (i = 0; i < 1000000; i++) {
        }
        return;
    }
    void error(void)
    {
        asm(" ESTOP0");     // Test failed!! Stop!
        for (;;){
        }
    }
    void spi_init()
    {
        CLK_enableSpiaClock(myClk);
        // Reset on, rising edge, 16-bit char bits
        SPI_setCharLength(mySpi, SPI_CharLength_8_Bits);
        // Enable master mode, normal phase,
        // enable talk, and SPI int disabled.
        SPI_setMode(mySpi, SPI_Mode_Master);
        SPI_enableTx(mySpi);
        SPI_setBaudRate(mySpi, SPI_BaudRate_1_MBaud);
        // Relinquish SPI from Reset
        //SPI_enableLoopBack(mySpi);
        SPI_enable(mySpi);
        // Set so breakpoints don't disturb xmission
        SPI_setPriority(mySpi, SPI_Priority_FreeRun);
        return;
    }
    void spi_fifo_init()
    {
        // Initialize SPI FIFO registers
        SPI_enableChannels(mySpi);
        SPI_enableFifoEnh(mySpi);
        SPI_resetTxFifo(mySpi);
        SPI_clearTxFifoInt(mySpi);
        SPI_resetRxFifo(mySpi);
        SPI_clearRxFifoInt(mySpi);
        SPI_setRxFifoIntLevel(mySpi,SPI_FifoLevel_1_Word);
        return;
    }

  • Ramesh,

    Have you been able to resolve your issue?

    -Mark