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.

Problem with SPI on 28335



Hello all,

   I am trying to interface the TI 28335 with the Parallax Memory Stick Data Logger through SPI to record ~100, 32 bit words/second. In order to initialize the device an "E" must be sent and then will be echoed back by the DataLogger if every thing has been initialized. This process is then repeated with an "e". I have written the code below to achieve this, however it always returns a "positive" test even when no memory device is inserted, so I am convinced that it is an issue with the code. The libraries refrenced in this code are the standard libraries that come with the examples. Would someone help me out?

Thanks so much,

Tim

#include "DSP28x_Project.h"     // Device Headerfile and Examples Include File

void delay_loop(void);
void spi_xmit(char a);
char spi_recieve(void);
void spi_fifo_init(void);
void spi_init(void);
void error(void);
int sync(void);

char sdata;  // send data
char rdata;  // received data
Uint16 ABC;
Uint16 ABS;
int count = 0;

void main(void)
{
   InitSysCtrl();

   InitSpiaGpio();
 
   DINT;

   InitPieCtrl();

   IER = 0x0000;
   IFR = 0x0000;

   InitPieVectTable();
   
   spi_fifo_init();      // Initialize the Spi FIFO
   spi_init();          // init SPI

   sdata = 0x0000;                           

    ABS = sync();

}    
int sync()
{
    int Got_Big_E = 0; //Determines if big "E" has successfully been echoed
    int Got_Little_E = 0; //Determines if little "e" has succfessfully been echoed
    int LoopCount = 0; //Number of tries to read big"E"
    spi_xmit('E');
    spi_xmit(0x0D);
    while((Got_Big_E == 0) && (LoopCount < 256))
    {
        while(SpiaRegs.SPIFFRX.bit.RXFFST !=1) { }
        rdata= spi_recieve();
        if(rdata == 'E')
        {
            Got_Big_E = 1;
            ABC = 127;
        }
        LoopCount++;
    }
    if(Got_Big_E == 1)
    {
        spi_xmit('e');
        spi_xmit(0x0D);
        LoopCount = 0;
        while((Got_Little_E == 0) && (LoopCount < 256))
        {
            while(SpiaRegs.SPIFFRX.bit.RXFFST !=1) { }
            rdata= spi_recieve();
            if(rdata == 'e')
            {
                Got_Little_E = 1;
                ABS = 127;
            }
            LoopCount++;
        }
    }

   return Got_Little_E;
}

void delay_loop()
{
    long      i;
    for (i = 0; i < 1000000; i++) {}
}


void error(void)
{
    asm("     ESTOP0");                        // Test failed!! Stop!
    for (;;);
}

void spi_init()
{   
    SpiaRegs.SPICCR.all =0x0007;                 // Reset on, rising edge, 8-bit char bits 
    SpiaRegs.SPICTL.all =0x0006;                 // Enable master mode, normal phase,
                                                 // enable talk, and SPI int disabled.
    SpiaRegs.SPIBRR =0x007F;                                   
    SpiaRegs.SPICCR.all =0x0087;                 // Relinquish SPI from Reset  
    SpiaRegs.SPIPRI.bit.FREE = 1;                // Set so breakpoints don't disturb xmission

    GpioCtrlRegs.GPAMUX2.bit.GPIO16 = 1; //Switch to SPI
    GpioCtrlRegs.GPAMUX2.bit.GPIO17 = 1; //Switch to SPI
    GpioCtrlRegs.GPAMUX2.bit.GPIO18 = 1; //Switch to SPI
    GpioCtrlRegs.GPAMUX2.bit.GPIO19 = 1; //Switch to SPI

}

void spi_xmit(char a)
{
    Uint16 A = a;
    SpiaRegs.SPITXBUF=(A%256);

}   
char spi_recieve()
{
    char a;
    Uint16 A = SpiaRegs.SPIRXBUF;
    a = (char)(A>>8);
    return a;
}
void spi_fifo_init()                                       
{
// Initialize SPI FIFO registers
    SpiaRegs.SPIFFTX.all=0xE040;
    SpiaRegs.SPIFFRX.all=0x204f;
    SpiaRegs.SPIFFCT.all=0x0;
}

  • From a quick look at your code, should the spi_xmit function have (A * 256), not (A %256).

    (Assuming the intent is to get the relevant char bits into the upper 8 bits of the Uint16.)

    If that's correct, then (A << 8) might be better still.

    (Not sure if this is right, or not!)

    Adrian

     

  • Adrian,

       Thanks for the help. The operation (A << 8) is what I was trying to accomplish. The change makes the code more readable and clear, however the code is still not successfully communicating with the Parallax Data Logger powered by the Vinculum 1 chip.

    Tim

     

  • Tim,

    I've actually made this mistake before myself.  With our SPI peripheral, data to be transmitted must be written into the register left justified, and read out right justified.  Adrian was correct in his suggestion to change your transmit function to a*256, but there is no reason to not put in a  << 8 which I believe is much more readable.

    The main problem you are seeing now is because of your receive function.  Like I said received data is right justified and you as shifting that right justified data down another 8 bits (causing you to loose the recieved data).  If you take out the shift and mask out the upper bits you should be ok.  See below:

    char spi_recieve()
    {
        char a;
        Uint16 A = SpiaRegs.SPIRXBUF;
        a = (char)(A & 0xFF);
        return a;
    }

     

    Trey