µTasker Forum

µTasker Forum => Luminary Micro TM LM3SXXXX => Topic started by: Tim on March 12, 2009, 03:58:22 AM

Title: SPI
Post by: Tim on March 12, 2009, 03:58:22 AM
Has anyone used the SPI port (other than flash)? Can you please provide an example?

Thanks

Tim
Title: Re: SPI
Post by: robo on March 12, 2009, 01:39:31 PM
Hi Tim!

Maybe this will help you. Below are two very simple functions (first for write one byte data and second to read one byte data):

Code: [Select]
void fnWrite_command(unsigned char ucCommand)
{

    GPIODATA_D &= ~CS_LINE;                                     // CS low state
    SSIDR_0 = ucCommand;                                          // send 8-bit command
                                                                              //if you want to send more bytes add more lines SSIDR_0 = comand;

while (SSISR_0 & (SSI_BSY | SSI_RNE)) {              // wait for transfer to complete and clear rx buffer
volatile unsigned long ulDummy = SSIDR_0;
}
GPIODATA_D |= CS_LINE;
}

unsigned char fnRead_command()
{
        unsigned char ucValue;

        GPIODATA_D &= ~CS_LINE;                                 // CS low state
                                               
SSIDR_0 = 0x00;            //dummy writes to generate clock
//SSIDR_0 = 0x00;         //if you want to read more bytes add more line SSIDR_0 = 0x00;
    while (SSISR_0 & SSI_BSY) {};

ucValue = (unsigned char)SSIDR_0;
       
while (SSISR_0 & (SSI_BSY | SSI_RNE)) {                          // wait for transfer to complete and clear rx buffer
volatile unsigned long ulDummy = SSIDR_0;
}
GPIODATA_D |= CS_LINE;

return ucValue;
}

Best regards,
robo
Title: Re: SPI
Post by: Tim on March 12, 2009, 04:45:50 PM
Thanks Robo. Do I need to send the 0x00 for the read before each read or just as many times as I need before I read?

i.e.

0x00
0x00
0x00
0x00

Read 4 bytes

0x00
read
0x00
read
0x00
read
0x00
read

Thanks
Title: Re: SPI
Post by: robo on March 13, 2009, 09:54:01 AM
Hi Tim!

First i.e. is correct.
For example: if you want to read 4 bytes

SSIDR_0 = 0x00;
SSIDR_0 = 0x00;
SSIDR_0 = 0x00;
SSIDR_0 = 0x00;

while (SSISR_0 & SSI_BSY) {};

ucValue0 = (unsigned char)SSIDR_0;
ucValue1 = (unsigned char)SSIDR_0;
ucValue2 = (unsigned char)SSIDR_0;
ucValue3 = (unsigned char)SSIDR_0;

Best regards,
robo