Nios® V/II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® V/II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++

Nios2 SPI

Altera_Forum
Honored Contributor II
2,153 Views

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 :)  

 

-mitch  

 

 

0 Kudos
7 Replies
Altera_Forum
Honored Contributor II
470 Views

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);
0 Kudos
Altera_Forum
Honored Contributor II
470 Views

could you show me the your complete code for your spi interface?

0 Kudos
Altera_Forum
Honored Contributor II
470 Views

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); }
0 Kudos
Altera_Forum
Honored Contributor II
470 Views

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?
0 Kudos
Altera_Forum
Honored Contributor II
470 Views

I ended up using the standard NIOS spi command like this: 

 

Write :  

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);  

 

Read : 

 

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);  

 

Hope this helps :)  

 

0 Kudos
Altera_Forum
Honored Contributor II
470 Views

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  

#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); }  

 

but the thinks i write dont match with the read data... :( 

 

Any help? 

0 Kudos
Altera_Forum
Honored Contributor II
470 Views

what pins did you assign your MISO,MOSI,SS,SCK ??

0 Kudos
Reply