Forum Discussion
Altera_Forum
Honored Contributor
16 years agoNios2 SPI
Hi,
I'm having some trouble reading from my IO expander with the SPI core from Altera :confused: Can anybody provide me with an example on how to issue a correct read to the spi interface with the 'alt_avalon_spi_command' ?? Thanks :) -mitch7 Replies
- Altera_Forum
Honored Contributor
I use something like that
IOWR(SPI_CYCLONE3_BASE, ALTERA_AVALON_SPI_TXDATA_REG, tosend); while (!(IORD(SPI_CYCLONE3_BASE, ALTERA_AVALON_SPI_STATUS_REG) & 0x0020)); return IORD(SPI_CYCLONE3_BASE, ALTERA_AVALON_SPI_RXDATA_REG); - Altera_Forum
Honored Contributor
could you show me the your complete code for your spi interface?
- Altera_Forum
Honored Contributor
I send 16bits. 1 read/write bit, 7 address bits and 8 datas bits.
void WriteReg(alt_u8 adress, alt_u8 m_data) { alt_u16 tosend; tosend = 0x8000 | ((adress << 8) & 0x7F00) | (m_data & 0x00FF); IOWR(SPI_CYCLONE3_BASE, ALTERA_AVALON_SPI_TXDATA_REG, tosend); while (!(IORD(SPI_CYCLONE3_BASE, ALTERA_AVALON_SPI_STATUS_REG) & 0x0020)); } alt_u8 ReadReg(alt_u8 adress) { alt_u16 tosend; tosend = (adress << 8) & 0x7F00; IOWR(SPI_CYCLONE3_BASE, ALTERA_AVALON_SPI_TXDATA_REG, tosend); while (!(IORD(SPI_CYCLONE3_BASE, ALTERA_AVALON_SPI_STATUS_REG) & 0x0020)); return IORD(SPI_CYCLONE3_BASE, ALTERA_AVALON_SPI_RXDATA_REG); } - Altera_Forum
Honored Contributor
Hi
I have the similar problem-i can write to the register, but when i try to read from register, i dont became anything, all signals-CS, CLK, MISO are 0... alt_spi_command run also, but only when i write to register... can anybody help me, any suggestion? - Altera_Forum
Honored Contributor
I ended up using the standard NIOS spi command like this:
Write :
Read :wdata = 0x40; // Write command of the chip wdata = 0x09; // register adr wdata = 0x2; // value alt_avalon_spi_command(SPI0_BASE, 0, 3, wdata, 0, rdata, 0);
Hope this helps :)wdata = 0x41; // read command wdata = 0x12; // register adr ID = 0; alt_avalon_spi_command(SPI0_BASE, 0, 2, wdata, 2, // reading 2 bytes rdata, // store bytes here 0); - Altera_Forum
Honored Contributor
and how would that work without the standard command?
I'm an altera newbee and have the same problem. i need to send 7 bit reg-addr.+1bit r/w and 16 bit data I'm trying with
but the thinks i write dont match with the read data... :( Any help?#include <altera_avalon_spi_regs.h> #include <altera_avalon_spi.h> main{ int test; while (!(IORD_ALTERA_AVALON_SPI_STATUS(SPI_BASE) & ALTERA_AVALON_SPI_STATUS_TRDY_MSK)); IOWR_ALTERA_AVALON_SPI_TXDATA(SPI_BASE, 0xFFFF); while (!(IORD_ALTERA_AVALON_SPI_STATUS(SPI_BASE) & ALTERA_AVALON_SPI_STATUS_TRDY_MSK)); test=IORD_ALTERA_AVALON_SPI_RXDATA(SPI_BASE); } - Altera_Forum
Honored Contributor
what pins did you assign your MISO,MOSI,SS,SCK ??