Forum Discussion
Altera_Forum
Honored Contributor
14 years agoSPI Problems
Hi, I'm trying to get handle with the SPI on a Nios II Core but I don't get it. I have a SPI master and a SPI Slave that I can try to transmit some data from the master to the slave. I connecte...
Altera_Forum
Honored Contributor
14 years agoThe correct procedure is something like this:
// select slave (not required in single slave system) IOWR_ALTERA_AVALON_SPI_SLAVE_SEL(master_base, n_slave); // assert CS IOWR_ALTERA_AVALON_SPI_CONTROL(master_base, ALTERA_AVALON_SPI_CONTROL_SSO_MSK); // wait tx channel ready do { status = IORD_ALTERA_AVALON_SPI_STATUS(master_base); } while (((status & ALTERA_AVALON_SPI_STATUS_TRDY_MSK) == 0) && (status & ALTERA_AVALON_SPI_STATUS_RRDY_MSK) == 0); // (optional) data possibly to be transmitted back from slave to master upon request IOWR_ALTERA_AVALON_SPI_TXDATA(slave_base, data_answer); // tx data IOWR_ALTERA_AVALON_SPI_TXDATA(master_base, data_tx); // this will initiate the transfer; in the same time the slave sends out on miso pin // the data ready in its tx register // Wait until the interface has finished transmitting do { status = IORD_ALTERA_AVALON_SPI_STATUS(master_base); } while ((status & ALTERA_AVALON_SPI_STATUS_TMT_MSK) == 0); // release CS IOWR_ALTERA_AVALON_SPI_CONTROL(master_base, 0); // read received data from slave data_rx = IORD_ALTERA_AVALON_SPI_RXDATA(slave_base); // data_rx must match data_tx // (optional) read answer from master recvd_answer = IORD_ALTERA_AVALON_SPI_RXDATA(master_base); // recvd_answer must match data_answer Please note that I wrote this code by heart. I'm not sure this it will compile without errors.