Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
15 years ago

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

-mitch

7 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored 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's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored 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's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored 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's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

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

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored 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

     
    #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?