Forum Discussion
Altera_Forum
Honored Contributor
20 years agoSPI for nios
dear every1, i've been stuck with trying to tranfer data out the spi..i just dont know how to use it.i wanna use the alt_avalon_spi_command() but i just cant do it.i'm stuck witthe basic ...
Altera_Forum
Honored Contributor
20 years agoHi edwin,
the main problem, which I encountered with SPI is the SPI enable. Sometimes when trying to send data it goes high or low. Sow it is best to use a dedicated enable PIO for SPI enable. This way it is possible to control it by yourself. It is difficult when you don't have altera_avalon_spi.h, but you've got the control registers I think. Added to this message is my SPI-code, which I rewrote. It is better to use I think http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/smile.gif . Good luck with it.... When you've got any questions feel free to ask... Cheers, Danny # define TMT (1 << 5) //Transmitter shift-register empty# define TRDY (1 << 6) //Transmitter ready# define RRDY (1 << 7) //Receiver ready# define spi_base SPI_0_BASE //Define SPI_base, less typing# define spi_cs SPI_0_CS_BASE //Define SPI_base, less typing void spi_send(alt_u32 base, const alt_u8 regBit, const alt_u8 subRegBit) { IOWR_ALTERA_AVALON_SPI_CONTROL(base, 0x00); //Clear status register with zero's while(~IORD_ALTERA_AVALON_SPI_STATUS(base) & TRDY); //check if tx data register is empty. If not, keep looping until it is IOWR_ALTERA_AVALON_PIO_DATA(spi_cs, 0); //use this SPI-select, the one delivered by Altera doesn't work!!! IOWR_ALTERA_AVALON_SPI_SLAVE_SEL(base, 0x01); //slave select mask enable, slave selection device 0 IOWR_ALTERA_AVALON_SPI_TXDATA(base, dummyBit); //write dummyBit to tx-buffer, so sending is possible usleep(10); //Sleep for 10 useconds. Needed, otherwise SDI2RGB-board will NOT receive any commands IOWR_ALTERA_AVALON_SPI_TXDATA(base, regBit); //write to tx-buffer IOWR_ALTERA_AVALON_SPI_TXDATA(base, subRegBit); //write to tx-buffer while(~IORD_ALTERA_AVALON_SPI_STATUS(base) & RRDY); //check if receiver is ready while(~IORD_ALTERA_AVALON_SPI_STATUS(base) & TMT); //check if transmitter shift register is empty IOWR_ALTERA_AVALON_PIO_DATA(spi_cs, 1); //use this SPI-select, the one delivered by Altera doesn't work!!! } /* * Start of SPI_receive function, 1 parameter is given: * * Base */ alt_u8 spi_receive(alt_u32 base) { alt_u8 data; //Variable for SPI data to be stored in IOWR_ALTERA_AVALON_PIO_DATA(spi_cs, 0); //use this SPI-select, the one delivered by Altera doesn't work!!! while(~IORD_ALTERA_AVALON_SPI_STATUS(base) & TRDY); //check if tx data register is empty. If not, keep looping until it is while(~IORD_ALTERA_AVALON_SPI_STATUS(base) & TMT); //check if transmitter shift register is empty while(~IORD_ALTERA_AVALON_SPI_STATUS(base) & RRDY); //check if receiver is ready data = 10; //variable for test-purpose IOWR_ALTERA_AVALON_SPI_RXDATA(base, data); //read from RX-register IOWR_ALTERA_AVALON_PIO_DATA(spi_cs, 1); //use this SPI-select, the one delivered by Altera doesn't work!!! return data; }