Forum Discussion
Altera_Forum
Honored Contributor
19 years agoSPI RX problems
Hi
I am trying to receive data via SPI on the Nios II using a Cyclone II. The Nios is the slave and the SPI master transmits data at 2MHz. I have tested transmitting data from the Nios to the SPI master and that worked fine. I have written the following code to test the receiving of data: //SPI RX while (1) { //clears the status register IOWR_ALTERA_AVALON_SPI_STATUS(SPI_0_BASE, 0); //if new byte arrived via SPI if ((IORD_ALTERA_AVALON_SPI_STATUS(SPI_0_BASE) & ALTERA_AVALON_SPI_STATUS_RRDY_MSK) == 1){ //read the byte and print it to screen printf("%x\n", (IORD_ALTERA_AVALON_SPI_RXDATA(SPI_0_BASE) & 0x0FF)); } } The IF statement never becomes true. I have removed the IF and read the RXDATA. The data that the SPI master transmitted is correctly received in the RXDATA buffer but the the RRDY bit never goes high. Does anyone have any idea why this not working? Am I doing it correctly, because I used a similar approach to TX data and that worked. Thank you Eric13 Replies
- Altera_Forum
Honored Contributor
--- Quote Start --- originally posted by ericbaker@Dec 4 2006, 11:32 AM hii am trying to receive data via spi on the nios ii using a cyclone ii. the nios is the slave and the spi master transmits data at 2mhz.
i have tested transmitting data from the nios to the spi master and that worked fine.
i have written the following code to test the receiving of data:
//spi rx
while (1) {
//clears the status register
iowr_altera_avalon_spi_status(spi_0_base, 0);
//if new byte arrived via spi
if ((iord_altera_avalon_spi_status(spi_0_base) & altera_avalon_spi_status_rrdy_msk) == 1){
//read the byte and print it to screen
printf("%x\n", (iord_altera_avalon_spi_rxdata(spi_0_base) & 0x0ff));
}
}
the if statement never becomes true. i have removed the if and read the rxdata. the data that the spi master transmitted is correctly received in the rxdata buffer but the the rrdy bit never goes high.
does anyone have any idea why this not working? am i doing it correctly, because i used a similar approach to tx data and that worked.
thank you
eric
<div align='right'><{post_snapback}> (index.php?act=findpost&pid=19808)
--- quote end ---
--- Quote End --- Hi, try this: //SPI RX while (1) { //if new byte arrived via SPI if ((IORD_ALTERA_AVALON_SPI_STATUS(SPI_0_BASE) & ALTERA_AVALON_SPI_STATUS_RRDY_MSK) == 1){ //read the byte and print it to screen printf("%x\n", (IORD_ALTERA_AVALON_SPI_RXDATA(SPI_0_BASE) & 0x0FF)); } //clears the status register IOWR_ALTERA_AVALON_SPI_STATUS(SPI_0_BASE, 0); }
- Altera_Forum
Honored Contributor
--- Quote Start --- originally posted by mondoemme+dec 4 2006, 11:53 am--><div class='quotetop'>quote (mondoemme @ dec 4 2006, 11:53 am)</div>--- quote start ---
<!--quotebegin-ericbaker@Dec 4 2006, 11:32 AM hi
i am trying to receive data via spi on the nios ii using a cyclone ii. the nios is the slave and the spi master transmits data at 2mhz.
i have tested transmitting data from the nios to the spi master and that worked fine.
i have written the following code to test the receiving of data:
//spi rx
while (1) {
//clears the status register
iowr_altera_avalon_spi_status(spi_0_base, 0);
//if new byte arrived via spi
if ((iord_altera_avalon_spi_status(spi_0_base) & altera_avalon_spi_status_rrdy_msk) == 1){
//read the byte and print it to screen
printf("%x\n", (iord_altera_avalon_spi_rxdata(spi_0_base) & 0x0ff));
}
}
the if statement never becomes true. i have removed the if and read the rxdata. the data that the spi master transmitted is correctly received in the rxdata buffer but the the rrdy bit never goes high.
does anyone have any idea why this not working? am i doing it correctly, because i used a similar approach to tx data and that worked.
thank you
eric
<div align='right'><{post_snapback}> (index.php?act=findpost&pid=19808)
--- quote end ---
--- Quote End --- Hi, try this: //SPI RX while (1) { //if new byte arrived via SPI if ((IORD_ALTERA_AVALON_SPI_STATUS(SPI_0_BASE) & ALTERA_AVALON_SPI_STATUS_RRDY_MSK) == 1){ //read the byte and print it to screen printf("%x\n", (IORD_ALTERA_AVALON_SPI_RXDATA(SPI_0_BASE) & 0x0FF)); } //clears the status register IOWR_ALTERA_AVALON_SPI_STATUS(SPI_0_BASE, 0); } <div align='right'><{post_snapback}> (index.php?act=findpost&pid=19809)</div> [/b] --- Quote End --- opps! provided that you are interested in rx only .... //SPI RX while (1) { //if new byte arrived via SPI if ((IORD_ALTERA_AVALON_SPI_STATUS(SPI_0_BASE) & ALTERA_AVALON_SPI_STATUS_RRDY_MSK) == 1){ //read the byte and print it to screen printf("%x\n", (IORD_ALTERA_AVALON_SPI_RXDATA(SPI_0_BASE) & 0x0FF)); //clears the status register IOWR_ALTERA_AVALON_SPI_STATUS(SPI_0_BASE, 0); } }
- Altera_Forum
Honored Contributor
opps!
provided that you are interested in rx only .... //SPI RX while (1) { //if new byte arrived via SPI if ((IORD_ALTERA_AVALON_SPI_STATUS(SPI_0_BASE) & ALTERA_AVALON_SPI_STATUS_RRDY_MSK) == 1){ //read the byte and print it to screen printf("%x\n", (IORD_ALTERA_AVALON_SPI_RXDATA(SPI_0_BASE) & 0x0FF)); //clears the status register IOWR_ALTERA_AVALON_SPI_STATUS(SPI_0_BASE, 0); } } Thanx for the advice, but it did not work. Any other ideas? Eric - Altera_Forum
Honored Contributor
--- Quote Start --- originally posted by ericbaker@Dec 4 2006, 04:12 PM opps!provided that you are interested in rx only ....
//spi rx
while (1) {
//if new byte arrived via spi
if ((iord_altera_avalon_spi_status(spi_0_base) & altera_avalon_spi_status_rrdy_msk) == 1){
//read the byte and print it to screen
printf("%x\n", (iord_altera_avalon_spi_rxdata(spi_0_base) & 0x0ff));
//clears the status register
iowr_altera_avalon_spi_status(spi_0_base, 0);
}
}
thanx for the advice, but it did not work. any other ideas?
eric
<div align='right'><{post_snapback}> (index.php?act=findpost&pid=19815)
--- quote end ---
--- Quote End --- Hi, trivial: did you enable spy rx_rdy flag?
- Altera_Forum
Honored Contributor
There were 2 problems:
1.) The IF statement was wrong... (stupid mistake) 2.) Clearing the Status Register every time erased some of the RRDY bits. Here is the working test code: while (1){ if (IORD_ALTERA_AVALON_SPI_STATUS(SPI_0_BASE) & ALTERA_AVALON_SPI_STATUS_RRDY_MSK){ //read the byte and print it to screen printf("%02x\n", (IORD_ALTERA_AVALON_SPI_RXDATA(SPI_0_BASE) & 0x0FF)); //clears the status register IOWR_ALTERA_AVALON_SPI_STATUS(SPI_0_BASE, 0); } } - Altera_Forum
Honored Contributor
Dear EricBaker,
looks we are working on the same project. I have to built a spi slave using nios2 and test the correct working of the spi master which I've build on max2 cpld. can you help me with the nios programming? Can we use spi core available under avalon components? Thanks, Ashish - Altera_Forum
Honored Contributor
--- Quote Start --- originally posted by ashish@Dec 30 2006, 09:05 AM dear ericbaker,looks we are working on the same project. i have to built a spi slave using nios2 and test the correct working of the spi master which i've build on max2 cpld. can you help me with the nios programming? can we use spi core available under avalon components?
thanks,
ashish
<div align='right'><{post_snapback}> (index.php?act=findpost&pid=20293)
--- quote end ---
--- Quote End --- Hi Ashish I've been away on holiday... Yes, you should be able to use the altera spi core. Set it up as a slave and use the sample code provided by altera. I guess this info comes to late...
- Altera_Forum
Honored Contributor
You problem is wrong condition in "if" statement, it sould be (IORD_ALTERA_AVALON_SPI_STATUS(SPI_0_BASE) &ALTERA_AVALON_SPI_STATUS_RRDY_MSK ) == ALTERA_AVALON_SPI_STATUS_RRDY_MSK
Operator & is bitwise, therefore you condition is never equal 1. - Altera_Forum
Honored Contributor
No....
A C if statement tests for non-zero. - Altera_Forum
Honored Contributor
Yes, and so (IORD_ALTERA_AVALON_SPI_STATUS(SPI_0_BASE) & ALTERA_AVALON_SPI_STATUS_RRDY_MSK) == 1 is Zero anytime, check youself please.
Correct statement should be if(iord_altera_avalon_spi_status(spi_0_base) & altera_avalon_spi_status_rrdy_msk) or if((iord_altera_avalon_spi_status(spi_0_base) & altera_avalon_spi_status_rrdy_msk)==altera_avalon_spi_status_rrdy_msk)