Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Altera_Forum
Honored Contributor I
1,187 Views

Problems with NIOS II SPI communication

Hi, 

I'm using two DE0 board to communicate with SPI core. One configued as master and another as slave. 

 

I tried to check spi status register to complement the slave code. And whole process is the master send data-- 0xabcd,and the slave receive the data and send back. But I found that the master can't receive the right data sometimes, less than 50% time the received data is right.  

I run the master program with debug mode first and then run the slave one to avoid signal toggle on the slave's input side. I set SCLK from 128kHz to 1MHz,the problem is still there. 

 

 

Here is my code for slave: 

int main(void) 

alt_u32 rddata=0x0; 

alt_u32 status; 

IOWR_ALTERA_AVALON_SPI_STATUS(SPI_BASE,0x0000); 

IOWR_ALTERA_AVALON_SPI_TXDATA(SPI_BASE,0x1001); 

IORD_ALTERA_AVALON_SPI_RXDATA(SPI_BASE); 

while(1) 

do 

status = IORD_ALTERA_AVALON_SPI_STATUS(SPI_BASE); 

while((status & ALTERA_AVALON_SPI_STATUS_RRDY_MSK)==0 && 

(status & ALTERA_AVALON_SPI_STATUS_RRDY_MSK) == 0); 

rddata= IORD_ALTERA_AVALON_SPI_RXDATA(SPI_BASE); 

 

IOWR_ALTERA_AVALON_SPI_TXDATA(SPI_BASE, rddata); 

 

 

Code for master is : 

int main(void) 

void send(alt_u16 write); 

alt_u16 receive(); 

alt_u16 wrdata=0xabcd; 

alt_u16 rddata; 

IOWR_ALTERA_AVALON_SPI_SLAVE_SEL(SPI_BASE, 1); 

IORD_ALTERA_AVALON_SPI_RXDATA(SPI_BASE); 

while(1) 

send(wrdata); 

rddata=receive(); 

return 0; 

void send(alt_u16 write) 

alt_u32 status; 

alt_u16 data; 

 

IOWR_ALTERA_AVALON_SPI_CONTROL(SPI_BASE, 0x0400); 

IORD_ALTERA_AVALON_SPI_RXDATA(SPI_BASE); 

do 

status = IORD_ALTERA_AVALON_SPI_STATUS(SPI_BASE); 

while (((status & ALTERA_AVALON_SPI_STATUS_TRDY_MSK) == 0)&& 

(status & ALTERA_AVALON_SPI_STATUS_RRDY_MSK) == 0); 

IOWR_ALTERA_AVALON_SPI_TXDATA(SPI_BASE , write); 

data=IORD_ALTERA_AVALON_SPI_RXDATA(SPI_BASE); 

do 

status = IORD_ALTERA_AVALON_SPI_STATUS(SPI_BASE); 

while ((status & ALTERA_AVALON_SPI_STATUS_TMT_MSK) == 0); 

IOWR_ALTERA_AVALON_SPI_CONTROL(SPI_BASE, 0); 

 

alt_u16 receive() 

alt_u16 data; 

alt_u32 status; 

IOWR_ALTERA_AVALON_SPI_CONTROL(SPI_BASE, 0x0400); 

IORD_ALTERA_AVALON_SPI_RXDATA(SPI_BASE); 

do 

status = IORD_ALTERA_AVALON_SPI_STATUS(SPI_BASE); 

while (((status & ALTERA_AVALON_SPI_STATUS_TRDY_MSK) == 0 ) && 

(status & ALTERA_AVALON_SPI_STATUS_RRDY_MSK) == 0); 

IOWR_ALTERA_AVALON_SPI_TXDATA(SPI_BASE , 0x00); 

data=IORD_ALTERA_AVALON_SPI_RXDATA(SPI_BASE); 

do 

status = IORD_ALTERA_AVALON_SPI_STATUS(SPI_BASE); 

while ((status & ALTERA_AVALON_SPI_STATUS_TMT_MSK) == 0); 

IOWR_ALTERA_AVALON_SPI_CONTROL(SPI_BASE, 0); 

return data; 

 

 

 

Can anybody help on this problem? I think there maybe some mistake in my code. Has anybody use SPI core as a slave? Most people use the SPI core as a master, I really need some on the slave mode. Thank a lot!
0 Kudos
1 Reply
Altera_Forum
Honored Contributor I
90 Views

hi truefrank,  

I checked your code and it is working fine for  

DE2-115 board.
Reply