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.

Use TMS320C5509 McBSP(SPI) Mode to Control AT25256

Other Parts Discussed in Thread: SPRC133

Hello,

Recently I have been testing the EEPROM with SPI  control mode of the McBSP0 in 5509. But it seems that the data written in were quite rambling. I took the reference from the 5502 SPI example code posted before, and made some changes to the current 5509.

During my debugging, when I did step-by-step debugging, sometimes the data could be written in; however, when I run the whole program, the EEPROM would not refresh its data.

One more strange thing, when I checked the MCBSP, the DRR10 and DRR20 there never changed. But the EEPROM could sometimes read the data correctly then... I don't know whether it was just by chance

I attached my code and schematic as follows. Could anyone give me some help? Thanks a lot!

#include <csl.h>
#include <stdio.h>
#include <csl_pll.h>
#include <csl_mcbsp.h>
#include <csl_gpio.h>
#include <csl_irq.h>
#include <csl_chip.h>

#define SPI_WRITE 0x02
#define SPI_READ 0x03
#define SPI_WRITE_EN 0x06
#define SPI_WRITE_DI 0x04
#define SPI_WRITE_REG 0x01
#define SPI_READ_REG 0x05
#define datalength 0x39


PLL_Config myConfig = {
0, //IAI: the PLL locks using the same process that was underway
//before the idle mode was entered
1, //IOB: If the PLL indicates a break in the phase lock,
//it switches to its bypass mode and restarts the PLL phase-locking
//sequence
2, //PLL multiply value; multiply 12 times
0 //Divide by 2 PLL divide value; it can be either PLL divide value
//(when PLL is enabled), or Bypass-mode divide value
//(PLL in bypass mode, if PLL multiply value is set to 1)
};


MCBSP_Handle hMcbsp;


Uint32 data_read[datalength]= {0};

Uint32 SPI_ReadSignal(Uint32 address);
void SPI_WriteSignal(Uint32 address,Uint32 data);

void WRITECOM_Delay()
{
Uint32 j =0;
for(j= 0;j<0x100;j++)
{}


}

void main(void)
{
Uint32 EEPROM_data = 0;
Uint32 i = 0;

Uint32 EPROM_address = 0;

CSL_init();
CHIP_RSET(XBSR,0x0a01);
IRQ_globalDisable();
GPIO_RSET(IODIR,0xFF);
PLL_config(&myConfig);

for(i = 0; i<datalength;i++)
{
SPI_WriteSignal(EPROM_address,i);
EEPROM_data = SPI_ReadSignal(EPROM_address);
data_read[i] = EEPROM_data;
// if(i != (data_read[i] & 0xFF))
// {
// for(;;){}
// }
EPROM_address++;
}
for(;;)
{}
}


void SPI_init(MCBSP_Handle hMcbsp)
{
volatile int i;

// Put McBSP registers in their reset state
// Not necessary, but nice for debug when restarting same code without device reset

MCBSP_RSETH(hMcbsp,SPCR1,0);
MCBSP_RSETH(hMcbsp,SPCR2,0);
MCBSP_RSETH(hMcbsp,RCR1,0);
MCBSP_RSETH(hMcbsp,RCR2,0);
MCBSP_RSETH(hMcbsp,XCR1,0);
MCBSP_RSETH(hMcbsp,XCR2,0);
MCBSP_RSETH(hMcbsp,SRGR1,1);
MCBSP_RSETH(hMcbsp,SRGR2,0x2000);
MCBSP_RSETH(hMcbsp,MCR1,0);
MCBSP_RSETH(hMcbsp,MCR2,0);
MCBSP_RSETH(hMcbsp,PCR,0);


// Configure registers
MCBSP_RSETH(hMcbsp,SRGR1,0x00F0);
MCBSP_RSETH(hMcbsp,SRGR2,0x2000);
MCBSP_RSETH(hMcbsp,SPCR1,0x1800);
// MCBSP_RSETH(hMcbsp,SPCR2,0x0300);
MCBSP_RSETH(hMcbsp,PCR,0x0F0C);
MCBSP_RSETH(hMcbsp,RCR1,0x0000);
MCBSP_RSETH(hMcbsp,RCR2,0x0001);
MCBSP_RSETH(hMcbsp,XCR1,0x0000);
MCBSP_RSETH(hMcbsp,XCR2,0x0001);

// Enable sample rate generator
MCBSP_FSETH(hMcbsp, SPCR2, GRST,1);
for(i=0;i<60;i++){}

// Enable transmitter and receiver
MCBSP_FSETH(hMcbsp, SPCR2, XRST,1);
MCBSP_FSETH(hMcbsp, SPCR1, RRST,1);

// Enable frame sync generator
MCBSP_FSETH(hMcbsp, SPCR2,FRST,1);
for(i=0;i<60;i++){}

// Enable chip select
GPIO_RSET(IODATA,0x00);
}



void SPI_WriteEN()
{
hMcbsp = MCBSP_open(MCBSP_PORT0,MCBSP_OPEN_RESET);
SPI_init(hMcbsp);

GPIO_RSET(IODATA,0x00); // chip select

//Send WREN command
while(!MCBSP_xrdy(hMcbsp)){}; //wait for XRDY
MCBSP_write16(hMcbsp,SPI_WRITE_EN);
while(!MCBSP_rrdy(hMcbsp)){}; //wait for RRDY to indicate write completed
MCBSP_read16(hMcbsp); // throw away dummy data

GPIO_RSET(IODATA,0x10); // deselect the chip
MCBSP_close(hMcbsp);
}


void SPI_WriteDI()
{
hMcbsp = MCBSP_open(MCBSP_PORT0,MCBSP_OPEN_RESET);
SPI_init(hMcbsp);

GPIO_RSET(IODATA,0x00); // chip select

//Send WRDI command
while(!MCBSP_xrdy(hMcbsp)){}; //wait for XRDY
MCBSP_write16(hMcbsp,SPI_WRITE_DI);
while(!MCBSP_rrdy(hMcbsp)){}; //wait for RRDY to indicate write completed
MCBSP_read16(hMcbsp); // throw away dummy data

GPIO_RSET(IODATA,0x10); // deselect the chip

MCBSP_close(hMcbsp);
}


void SPI_WriteStatusReg(Uint16 data)
{
hMcbsp = MCBSP_open(MCBSP_PORT0,MCBSP_OPEN_RESET);
SPI_init(hMcbsp);

GPIO_RSET(IODATA,0x00); // chip select

//Send WRSR command
while(!MCBSP_xrdy(hMcbsp)){}; //wait for XRDY
MCBSP_write16(hMcbsp,SPI_WRITE_REG);
while(!MCBSP_rrdy(hMcbsp)){}; //wait for RRDY to indicate write completed
MCBSP_read16(hMcbsp); // throw away dummy data

while(!MCBSP_xrdy(hMcbsp)){}; //wait for XRDY
MCBSP_write16(hMcbsp,data);
while(!MCBSP_rrdy(hMcbsp)){}; //wait for RRDY to indicate write completed
MCBSP_read16(hMcbsp); // throw away dummy data

GPIO_RSET(IODATA,0x10); // deselect the chip
MCBSP_close(hMcbsp);
}


Uint16 SPI_ReadStatusReg()
{
Uint16 value;

hMcbsp = MCBSP_open(MCBSP_PORT0,MCBSP_OPEN_RESET);
SPI_init(hMcbsp);

GPIO_RSET(IODATA,0x00); // chip select

//Send RDSR command
while(!MCBSP_xrdy(hMcbsp)){}; //wait for XRDY
MCBSP_write16(hMcbsp,SPI_READ_REG);
while(!MCBSP_rrdy(hMcbsp)){}; //wait for RRDY to indicate write completed
MCBSP_read16(hMcbsp); // throw away dummy data

//Read Status Reg
while(!MCBSP_xrdy(hMcbsp)){}; //wait for XRDY
MCBSP_write16(hMcbsp,0);
while(!MCBSP_rrdy(hMcbsp)){}; //wait for RRDY to indicate write completed
value= MCBSP_read16(hMcbsp); // throw away dummy data

GPIO_RSET(IODATA,0x10); // deselect the chip

MCBSP_close(hMcbsp);
return value;
}

void SPI_WriteSignal(Uint32 address, Uint32 data)
{
Uint16 EEPROM_status=0;

SPI_WriteEN();
EEPROM_status = SPI_ReadStatusReg();
while(!(EEPROM_status & 0x2)){};

hMcbsp = MCBSP_open(MCBSP_PORT0,MCBSP_OPEN_RESET);
SPI_init(hMcbsp);

GPIO_RSET(IODATA,0x00); // chip select

//Send WRITE command
while(!MCBSP_xrdy(hMcbsp)){}; //wait for XRDY
MCBSP_write16(hMcbsp,SPI_WRITE);
while(!MCBSP_rrdy(hMcbsp)){}; //wait for RRDY to indicate write completed
MCBSP_read16(hMcbsp); // throw away dummy data


while(!MCBSP_xrdy(hMcbsp)){}; //wait for XRDY
MCBSP_write16(hMcbsp,address>>8);
while(!MCBSP_rrdy(hMcbsp)){}; //wait for RRDY to indicate write completed
MCBSP_read16(hMcbsp); // throw away dummy data

while(!MCBSP_xrdy(hMcbsp)){}; //wait for XRDY
MCBSP_write16(hMcbsp,address);
while(!MCBSP_rrdy(hMcbsp)){}; //wait for RRDY to indicate write completed
MCBSP_read16(hMcbsp); // throw away dummy data


while(!MCBSP_xrdy(hMcbsp)){}; //wait for XRDY
MCBSP_write16(hMcbsp,data);
while(!MCBSP_rrdy(hMcbsp)){}; //wait for RRDY to indicate write completed
MCBSP_read16(hMcbsp); // throw away dummy data

GPIO_RSET(IODATA,0x10); // deselect the chip
//EEPROM_status = SPI_ReadStatusReg();
//while(!(EEPROM_status & 0x1)){};
WRITECOM_Delay();
MCBSP_close(hMcbsp);
}




Uint32 SPI_ReadSignal(Uint32 address)
{
Uint32 value = 0;
hMcbsp = MCBSP_open(MCBSP_PORT0,MCBSP_OPEN_RESET);
SPI_init(hMcbsp);

GPIO_RSET(IODATA,0x00); // chip select

//Send WRITE command
while(!MCBSP_xrdy(hMcbsp)){}; //wait for XRDY
MCBSP_write16(hMcbsp,SPI_READ);
while(!MCBSP_rrdy(hMcbsp)){}; //wait for RRDY to indicate write completed
MCBSP_read16(hMcbsp); // throw away dummy data


while(!MCBSP_xrdy(hMcbsp)){}; //wait for XRDY
MCBSP_write16(hMcbsp,address>>8);
while(!MCBSP_rrdy(hMcbsp)){}; //wait for RRDY to indicate write completed
MCBSP_read16(hMcbsp); // throw away dummy data

while(!MCBSP_xrdy(hMcbsp)){}; //wait for XRDY
MCBSP_write16(hMcbsp,address);
while(!MCBSP_rrdy(hMcbsp)){}; //wait for RRDY to indicate write completed
MCBSP_read16(hMcbsp); // throw away dummy data

while(!MCBSP_xrdy(hMcbsp)){}; //wait for XRDY
MCBSP_write16(hMcbsp,0);
while(!MCBSP_rrdy(hMcbsp)){}; //wait for RRDY to indicate write completed
value= MCBSP_read16(hMcbsp); // throw away dummy data

GPIO_RSET(IODATA,0x10); // deselect the chip
MCBSP_close(hMcbsp);

return value;

}
  • This is very good for you to convert codes from C5502 to VC5509A. I would like to point out that they are in 2 different families of product lines.

    Have you looked into the generic5509 McBSP example code in sprc133?

    Regards.

  • Hello Steve,

    Thanks very much for your reply! I have not checked the SPRC133 before. Just now I installed it and will take a look of the example there soon.

    BTW, you mentioned the 5502 and 5509a are two different families of product lines. Does 5501, 5502 belong to the same family and the other C5000 products are categorized in another family?

    Sincerely,

    Chen Wei

  • What i meant is 5501/5502 are one family. 5503/06/07/09/09A are one family. 5504/05/14/15/32/33/34/35 are one family.

    Regards.

  • Thanks for explanation! Will test the MCBSP soon, and I will let you know about the process then.

    Sincerely,

    Chen Wei

  • Excuse me, I am wondering whether there is any example about the MCBSP in the SPI mode (5509A)? 

  • Hello Steve, 

    I just tried the mcbsp1 example several times, sometimes it showed the result "Test Passed" and other times it  failed the test. The result seems to be quite unstable.

    What could be the possible reasons?

    Thanks!

    Sincerely,

    Chen Wei

  • Steve Tsang said:
    What i meant is 5501/5502 are one family. 5503/06/07/09/09A are one family. 5504/05/14/15/32/33/34/35 are one family.

    Make that 5503/06/07/09/09A/10/10A in one family.  i.e. don't forget the C5510 and C5510A!

  • Have got a solution as follows. Now it can run.

    #include <csl.h>
    #include <stdio.h>
    #include <csl_pll.h>
    #include <csl_mcbsp.h>
    #include <csl_gpio.h>
    #include <csl_irq.h>
    #include <csl_chip.h>

    #define SPI_WRITE 0x02
    #define SPI_READ 0x03
    #define SPI_WRITE_EN 0x06
    #define SPI_WRITE_DI 0x04
    #define SPI_WRITE_REG 0x01
    #define SPI_READ_REG 0x05
    #define datalength 0x39

    PLL_Config myConfig = {
    0, //IAI: the PLL locks using the same process that was underway
    //before the idle mode was entered
    1, //IOB: If the PLL indicates a break in the phase lock,
    //it switches to its bypass mode and restarts the PLL phase-locking
    //sequence
    1, //PLL multiply value; multiply 12 times
    3 //Divide by 2 PLL divide value; it can be either PLL divide value
    //(when PLL is enabled), or Bypass-mode divide value
    //(PLL in bypass mode, if PLL multiply value is set to 1)
    };


    MCBSP_Handle hMcbsp;

    Uint32 data_read[datalength]= {0};
    Uint32 SPI_ReadSignal(Uint32 address);
    void SPI_WriteSignal(Uint32 address,Uint32 data);

    void Delay()
    {
    Uint32 j =0;
    for(j= 0;j<0x100;j++)
    {}
    }

    void main(void)
    {
    Uint32 EEPROM_data = 0;
    Uint32 i = 0;

    Uint32 EPROM_address = 0;

    CSL_init();
    CHIP_RSET(XBSR,0x0a01);
    IRQ_globalDisable();
    GPIO_RSET(IODIR,0xFF);

    PLL_config(&myConfig);

    for(i = 0; i<datalength;i++)
    {
    SPI_WriteSignal(EPROM_address,i);
    EEPROM_data = SPI_ReadSignal(EPROM_address);
    data_read[i] = EEPROM_data;
    EPROM_address++;
    }
    for(;;)
    {}
    }


    void SPI_init(MCBSP_Handle hMcbsp)
    {
    volatile int i;

    // Put McBSP registers in their reset state
    // Not necessary, but nice for debug when restarting same code without device reset

    MCBSP_RSETH(hMcbsp,SPCR1,0);
    MCBSP_RSETH(hMcbsp,SPCR2,0);
    MCBSP_RSETH(hMcbsp,RCR1,0);
    MCBSP_RSETH(hMcbsp,RCR2,0);
    MCBSP_RSETH(hMcbsp,XCR1,0);
    MCBSP_RSETH(hMcbsp,XCR2,0);
    MCBSP_RSETH(hMcbsp,SRGR1,1);
    MCBSP_RSETH(hMcbsp,SRGR2,0x2000);
    MCBSP_RSETH(hMcbsp,MCR1,0);
    MCBSP_RSETH(hMcbsp,MCR2,0);
    MCBSP_RSETH(hMcbsp,PCR,0);


    // Configure registers
    MCBSP_RSETH(hMcbsp,SRGR1,0x00F0);
    MCBSP_RSETH(hMcbsp,SRGR2,0x2000);
    MCBSP_RSETH(hMcbsp,SPCR1,0x1800);
    // MCBSP_RSETH(hMcbsp,SPCR2,0x0300);
    MCBSP_RSETH(hMcbsp,PCR,0x0F0D);
    MCBSP_RSETH(hMcbsp,RCR1,0x0000);
    MCBSP_RSETH(hMcbsp,RCR2,0x0001);
    MCBSP_RSETH(hMcbsp,XCR1,0x0000);
    MCBSP_RSETH(hMcbsp,XCR2,0x0001);

    // Enable sample rate generator
    MCBSP_FSETH(hMcbsp, SPCR2, GRST,1);
    for(i=0;i<60;i++){}

    // Enable transmitter and receiver
    MCBSP_FSETH(hMcbsp, SPCR2, XRST,1);
    MCBSP_FSETH(hMcbsp, SPCR1, RRST,1);

    // Enable frame sync generator
    MCBSP_FSETH(hMcbsp, SPCR2,FRST,1);
    for(i=0;i<60;i++){}


    }



    void SPI_WriteEN()
    {
    hMcbsp = MCBSP_open(MCBSP_PORT0,MCBSP_OPEN_RESET);
    SPI_init(hMcbsp);

    GPIO_RSET(IODATA,0x00); // chip select

    //Send WREN command
    while(!MCBSP_xrdy(hMcbsp)){}; //wait for XRDY
    MCBSP_write16(hMcbsp,SPI_WRITE_EN);
    while(!MCBSP_rrdy(hMcbsp)){}; //wait for RRDY to indicate write completed
    MCBSP_read16(hMcbsp); // throw away dummy data

    GPIO_RSET(IODATA,0x10); // deselect the chip

    MCBSP_close(hMcbsp);
    }


    void SPI_WriteStatusReg(Uint16 data)
    {
    hMcbsp = MCBSP_open(MCBSP_PORT0,MCBSP_OPEN_RESET);
    SPI_init(hMcbsp);

    GPIO_RSET(IODATA,0x00); // chip select

    //Send WRSR command
    while(!MCBSP_xrdy(hMcbsp)){}; //wait for XRDY
    MCBSP_write16(hMcbsp,SPI_WRITE_REG);
    while(!MCBSP_rrdy(hMcbsp)){}; //wait for RRDY to indicate write completed
    MCBSP_read16(hMcbsp); // throw away dummy data

    while(!MCBSP_xrdy(hMcbsp)){}; //wait for XRDY
    MCBSP_write16(hMcbsp,data);
    while(!MCBSP_rrdy(hMcbsp)){}; //wait for RRDY to indicate write completed
    MCBSP_read16(hMcbsp); // throw away dummy data

    GPIO_RSET(IODATA,0x10); // deselect the chip

    MCBSP_close(hMcbsp);
    }


    Uint16 SPI_ReadStatusReg()
    {
    Uint16 value;

    hMcbsp = MCBSP_open(MCBSP_PORT0,MCBSP_OPEN_RESET);
    SPI_init(hMcbsp);

    GPIO_RSET(IODATA,0x00); // chip select

    //Send RDSR command
    while(!MCBSP_xrdy(hMcbsp)){}; //wait for XRDY
    MCBSP_write16(hMcbsp,SPI_READ_REG);
    while(!MCBSP_rrdy(hMcbsp)){}; //wait for RRDY to indicate write completed
    MCBSP_read16(hMcbsp); // throw away dummy data

    //Read Status Reg
    while(!MCBSP_xrdy(hMcbsp)){}; //wait for XRDY
    MCBSP_write16(hMcbsp,0);
    while(!MCBSP_rrdy(hMcbsp)){}; //wait for RRDY to indicate write completed
    value= MCBSP_read16(hMcbsp); // throw away dummy data

    GPIO_RSET(IODATA,0x10); // deselect the chip

    MCBSP_close(hMcbsp);
    return value;
    }

    void SPI_WriteSignal(Uint32 address, Uint32 data)
    {
    Uint16 EEPROM_status=0;

    SPI_WriteEN();
     
      	EEPROM_status = SPI_ReadStatusReg();
    while(!(EEPROM_status & 0x2)){};

    hMcbsp = MCBSP_open(MCBSP_PORT0,MCBSP_OPEN_RESET);
    SPI_init(hMcbsp);

    GPIO_RSET(IODATA,0x00); // chip select

    //Send WRITE command
    while(!MCBSP_xrdy(hMcbsp)){}; //wait for XRDY
    MCBSP_write16(hMcbsp,SPI_WRITE);
    while(!MCBSP_rrdy(hMcbsp)){}; //wait for RRDY to indicate write completed
    MCBSP_read16(hMcbsp); // throw away dummy data


    while(!MCBSP_xrdy(hMcbsp)){}; //wait for XRDY
    MCBSP_write16(hMcbsp,address>>8);
    while(!MCBSP_rrdy(hMcbsp)){}; //wait for RRDY to indicate write completed
    MCBSP_read16(hMcbsp); // throw away dummy data

    while(!MCBSP_xrdy(hMcbsp)){}; //wait for XRDY
    MCBSP_write16(hMcbsp,address);
    while(!MCBSP_rrdy(hMcbsp)){}; //wait for RRDY to indicate write completed
    MCBSP_read16(hMcbsp); // throw away dummy data


    while(!MCBSP_xrdy(hMcbsp)){}; //wait for XRDY
    MCBSP_write16(hMcbsp,data);
    while(!MCBSP_rrdy(hMcbsp)){}; //wait for RRDY to indicate write completed
    MCBSP_read16(hMcbsp); // throw away dummy data

    GPIO_RSET(IODATA,0x10); // deselect the chip
    //EEPROM_status = SPI_ReadStatusReg();
    //while(!(EEPROM_status & 0x1)){};
    Delay();
    MCBSP_close(hMcbsp);
    }

    Uint32 SPI_ReadSignal(Uint32 address)
    {
    Uint32 value = 0;
    hMcbsp = MCBSP_open(MCBSP_PORT0,MCBSP_OPEN_RESET);
    SPI_init(hMcbsp);

    GPIO_RSET(IODATA,0x00); // chip select

    //Send WRITE command
    while(!MCBSP_xrdy(hMcbsp)){}; //wait for XRDY
    MCBSP_write16(hMcbsp,SPI_READ);
    while(!MCBSP_rrdy(hMcbsp)){}; //wait for RRDY to indicate write completed
    MCBSP_read16(hMcbsp); // throw away dummy data


    while(!MCBSP_xrdy(hMcbsp)){}; //wait for XRDY
    MCBSP_write16(hMcbsp,address>>8);
    while(!MCBSP_rrdy(hMcbsp)){}; //wait for RRDY to indicate write completed
    MCBSP_read16(hMcbsp); // throw away dummy data

    while(!MCBSP_xrdy(hMcbsp)){}; //wait for XRDY
    MCBSP_write16(hMcbsp,address);
    while(!MCBSP_rrdy(hMcbsp)){}; //wait for RRDY to indicate write completed
    MCBSP_read16(hMcbsp); // throw away dummy data

    while(!MCBSP_xrdy(hMcbsp)){}; //wait for XRDY
    MCBSP_write16(hMcbsp,0);
    while(!MCBSP_rrdy(hMcbsp)){}; //wait for RRDY to indicate write completed
    value= MCBSP_read16(hMcbsp); // throw away dummy data

    GPIO_RSET(IODATA,0x10); // deselect the chip
    MCBSP_close(hMcbsp);

    return value;

    }
  • Good to hear your success!

    And thanks for sharing your solution.

    Together and sharing is a key to our E2E forum.

    Regards.

  • Thank you! My pleasure.

    Sincerely,

    Chen Wei