Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[Moved] LPC2129 ArmProcessor - SPI C code

Status
Not open for further replies.

ramasamy

Member level 1
Joined
Mar 15, 2005
Messages
34
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,524
Hi
I need C Code for LPC2129 SPI-0


Best regards
Ramasamy
 

Code:
#include "LPC21xx.h"

void SPI_Init(void)
{
	PINSEL0|=(1<<8)|(1<<10)|(1<<12);													//enable alternative functions for SPI
	IO0DIR|=CS_PIN;																		//CS PIN for OUTPUT
	S0SPCR=(1<<4)|(1<<5)|(1<<11);														//configure SPI
	S0SPCCR=0x10;																		//clock devider
	IO0SET=CS_PIN;																		//CS_PIN HIGH
}
unsigned char SPI_Read (unsigned char Data)
{
	S0SPDR=Data;
	while((S0SPSR&(1<<7))==0);
	S0SPSR&=(~(1<<7));
	return S0SPDR;
}
Special for you! Absolutely free! :-D:-D:-D
 

Hi
Thanks for the reply and i have tested your code, but S0SPSR 7th bit is not setting after writing data to S0SPDR,

i have only master , and trying to send some data via SPI port , no clock and MOSI output is not pulse , i checked with oscilloscope..

for checking is it require slave also?, because i want check clock and data pin pulsing in master , then i can decide spi is ok
 

Check SPI pins for your controller. I wrote this code for LPC2101. Your ARM may use different pins.
PINSEL0|=(1<<8)|(1<<10)|(1<<12);
 
Please check below is my code, is there any mistake?
I made register config as per LPC2129

main()
{


init_SPI0() ;
putchar_spi0(0xaa) ;
putchar_spi0(0x11) ;
putchar_spi0(0x55) ;




}


void init_SPI0(void)
{
PINSEL1 |= 0x00005500;
IODIR0 |=0x000000D0 ;
S0SPCCR = 16; //Set SPI clock to 15Mhz/16 = 937.5kHz
// S0SPCR = 0xA8; //SPI in Master Mode, MSB first,
// CPOL=0, CPHA=1, interrupt enable
S0SPCR = 0x30; // SPI in Master Mode, MSB first,


}

/*
* Read one byte from Slave
*/


unsigned char getchar_spi0(void)
{
unsigned char info = 0;
S0SPDR = 0xff; //Dummy write
while(!(S0SPSR&0x80)); //Wait while busy
info = S0SPDR; //Read received byte

return(info);

}

/*
* Send 'info' to Slave
*/
void putchar_spi0(unsigned char info)
{
while(S0SPSR&0x80);//make sure SPIF bit is 0
S0SPDR =info; //copy data to reg. This starts transmission

}

---------- Post added at 10:15 ---------- Previous post was at 10:13 ----------

I Selected PINSEL as SPI mode ,so we need not to do any direction selection and chip enable,
is it?
 

Well, I can't check all your configurations. But it looks ok.
For LPC family, in master mode you should drive CS pin manually by software.
And once more. In spi mode there is no difference between send byte and receive it. Because when you sending a byte throw SDO pin, configured as output, your SDI pin receives data from slave or noises if no slave available. So, you can use only getchar_spi0 function. If you no need a result of this routine (received data), just use it like a void.
So, I remember you - SPI pins SDI, SDO and SCK you should configured as alternative functions of port. And pin CS should be configured as output and driven like a normal pin.
And don't forget about remap functions. Possible, your SPI0 was remapped somewhere to another pins.
 
Hi
Still i am facing prpblem.

below is my code for slave and master



//SLave Code


void init_SPI0_SLAVE(void)
{

// PCONP |= 0x00000400 ;
PINSEL1 |= 0x00001500;
IODIR0 &= ~(0x00000080) ; //CS configured as input
//MOSI
S0SPCCR = 16; //Set SPI clock to 15Mhz/16 = 937.5kHz

//CPOL=0, CPHA=1, interrupt enable
S0SPCR = 0x10; //SPI in Slave Mode, LSB first,
}

unsigned char slave_getchar_spi0(unsigned char cData)
{
unsigned char info = 0;
while(!(S0SPSR&0x80)); //Wait while busy
info=S0SPDR ;
S0SPSR&=(~(0x80));
S0SPDR = cData; //Read received byte
return(info);
}







Master code


void init_SPI0_master(void)
{
PINSEL1 |=0x00001500;
IODIR0 |=0x00000080 ; //CS is configurea as output, all other SPI PIN are set as spi mode pin
S0SPCCR = 16; //Set SPI clock to 15Mhz/16 = 937.5kHz
// S0SPCR = 0xA8; //SPI in Master Mode, MSB first,
//CPOL=0, CPHA=1, interrupt enable
S0SPCR = 0x30; //SPI in Master Mode, MSB first,

}



unsigned char master_getchar_spi0(unsigned char cData)
{
unsigned char info = 0;
IOCLR0 =0x00000080; //Select chip
S0SPDR =cData ; //Dummy write
while(!(S0SPSR&0x80)); //Wait while busy
S0SPSR&=(~(0x80));
info = S0SPDR; //Read received byte
IOSET0 = 0x00000080; //Deselect chip
return(info);
}


the above code i download in to Master and slave board.


code is not working at all , i am checking clock and data line , but no pulse

and also i want to know the connection between master , please confirm which one is correct

Connection 1:
Master Slave
SCK SCK
MOSI MISO
MISO MOSI
SSEL SSEL


or

Connection 2:
Master Slave
SCK SCK
MOSI MOSI
MISO MISO
SSEL SSEL



which one is correct?

Best regards
Ramasamy

---------- Post added at 13:09 ---------- Previous post was at 12:41 ----------

Hi
Still i am facing prpblem.

below is my code for slave and master



//SLave Code


void init_SPI0_SLAVE(void)
{

// PCONP |= 0x00000400 ;
PINSEL1 |= 0x00001500;
IODIR0 &= ~(0x00000080) ; //CS configured as input
//MOSI
S0SPCCR = 16; //Set SPI clock to 15Mhz/16 = 937.5kHz

//CPOL=0, CPHA=1, interrupt enable
S0SPCR = 0x10; //SPI in Slave Mode, LSB first,
}

unsigned char slave_getchar_spi0(unsigned char cData)
{
unsigned char info = 0;
while(!(S0SPSR&0x80)); //Wait while busy
info=S0SPDR ;
S0SPSR&=(~(0x80));
S0SPDR = cData; //Read received byte
return(info);
}







Master code


void init_SPI0_master(void)
{
PINSEL1 |=0x00001500;
IODIR0 |=0x00000080 ; //CS is configurea as output, all other SPI PIN are set as spi mode pin
S0SPCCR = 16; //Set SPI clock to 15Mhz/16 = 937.5kHz
// S0SPCR = 0xA8; //SPI in Master Mode, MSB first,
//CPOL=0, CPHA=1, interrupt enable
S0SPCR = 0x30; //SPI in Master Mode, MSB first,

}



unsigned char master_getchar_spi0(unsigned char cData)
{
unsigned char info = 0;
IOCLR0 =0x00000080; //Select chip
S0SPDR =cData ; //Dummy write
while(!(S0SPSR&0x80)); //Wait while busy
S0SPSR&=(~(0x80));
info = S0SPDR; //Read received byte
IOSET0 = 0x00000080; //Deselect chip
return(info);
}


the above code i download in to Master and slave board.


code is not working at all , i am checking clock and data line , but no pulse

and also i want to know the connection between master , please confirm which one is correct

Connection 1:
Master Slave
SCK SCK
MOSI MISO
MISO MOSI
SSEL SSEL


or

Connection 2:
Master Slave
SCK SCK
MOSI MOSI
MISO MISO
SSEL SSEL



which one is correct?

Best regards
Ramasamy
 

HI
anybody can check my code , and let me know the problem in SPI of LPC2129
 

It is impossible. We can't just take your code, look on it and say what is wrong. You need to be sure that master transmit data correctly. Only than try to work with slave. Anyway, you need to use debugger tool and logic analyzer.
I can only simulate few philips micro controllers. But 2129 I can't.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top