Yahoo Groups archive

Lpc2000

Index last updated: 2026-04-28 23:31 UTC

Message

Re: SPI usage

2005-01-06 by acetoel

Ok... here is a piece of code that I make.. please somebody give a
hand, I don't want to bit bang a IO pin to make a SPI communication.
Eventhough I will communicate with a Atmel Dataflash, and the speed is
not important (I have to get at least 52k/sec, with SPI at 7.5MH I get
more than 900k/sec, and with bit banging at 1Mhz I get 125k/sec which
is enough).
Here is my code to send a byte, and to receive... really I need
something bidirectional (send and receive at the same time).


#define SPI_SPCR       (*((volatile unsigned char *) 0xE0020000))
#define SPI_SPSR       (*((volatile unsigned char *) 0xE0020004))
#define SPI_SPDR       (*((volatile unsigned char *) 0xE0020008))
#define SPI_SPCCR      (*((volatile unsigned char *) 0xE002000C))
#define SPI_SPTCR      (*((volatile unsigned char *) 0xE0020010))
#define SPI_SPTSR      (*((volatile unsigned char *) 0xE0020014))
#define SPI_SPTOR      (*((volatile unsigned char *) 0xE0020018))
#define SPI_SPINT      (*((volatile unsigned char *) 0xE002001C)) 


void spi_init(unsigned long PCLKBus, SPIRate, CPHA, CPOL, MSTR, LSBF,
SPIE) {
 unsigned long SPIDivider;
 SPI_SPCR=0x00000000; // Disable SPI
 
// Calculate SPI_SPCCR - MIN SPIC_SPCCR = 8 -> @60MHZ, SPIRate = 7.5Mhz 
 SPIDivider = PCLKBus / SPIRate;
 if (SPIDivider <= 0x7) {
   SPIDivider = 0x00000008; // MAX Speed for any PCLK Speed
}
 SPI_SPCCR = SPIDivider;
 
// Set The SPI Mode

 if (CPHA == 1 ) {
    SPI_SPCR = (SPI_SPCR + 0x00000008);
 }
 
 if (CPOL == 1) {
    SPI_SPCR = SPI_SPCR + 0x00000010;
 } 
 
 if (LSBF == 1) {
    SPI_SPCR = SPI_SPCR + 0x00000040; 
 }
 
 if (MSTR == 1) {
 	SPI_SPCR = SPI_SPCR + 0x00000020;
 } 
}

void spi_sendbyte (unsigned char spidata) {
	SPI_SPDR = spidata;
	while ((SPI_SPSR & 0x80) == 1) {} //Waits until TX is complete
	SPI_SPSR = SPI_SPSR & (1 << 7)
}


-------------------------------------------------------------
 

Thanks
Ezequiel


--- In lpc2000@yahoogroups.com, Bruce Paterson <bruce@t...> wrote:
> acetoel wrote:
> 
> > 
> > Hello...
> > 
> > Is there any sample code that shows how to read and write a byte on
> > the SPI bus? or any SPI sample code?
> 
> Ooops sorry ! My message about ARMlib seems to be premature on furthur 
> investigation. It's only byte (not interrupt message based) and has
only 
> C stubs at present.
> 
> -- 
> Cheers,
> Bruce
> -------------------------------------------------------------------
> This email and any files transmitted with it are confidential and
> intended solely for the use of the individual or entity to whom
> they are addressed. If you have received this email in error please
> notify the system manager.
> 
>      /\\\/\\\/\\\    /   /      Bruce Paterson
>     /  \\\ \\\ \\\  /   /    Senior Design Engineer
>    /   /\\\/\\\/\\\/   /   8 Anzed Court, Mulgrave, Vic, 3170
>   /   /  \\\ \\\ \\\  /  PO Box 4112, Mulgrave, Vic, 3170, Australia
> /   /    \\\/\\\ \\\/   Ph: +61 3 8561 4232   Fax: +61 3 9560 9055
>        Tele-IP Ltd.      Email: bruce@t...    Icq: #32015991
>                          WWW:   http://www.tele-ip.com       VK3TJN
> -------------------------------------------------------------------

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.