C program for NIOS II running in wrong sequence


I'm writing a code in C language for my NIOS II processor. I'm using Ecplipse that making me crazy! It stuck a lot!!

This part of code should read register using SPI, change the data, write it back and then read it again for a validation.

So the sequence should be SPIread->SPIwrite->SPIread. When I run it i get SPIread->SPIread->SPIwrite.

The code example:

alt_u32 SpiRead(alt_u8 spiNbytes, alt_u8 spiReg)
    IOWR_32DIRECT(NRF24_SPI_BASE, 0, 0x1f & spiReg);    //set register 
    IOWR_8DIRECT(NRF24_SPI_BASE, 8, 0x07 & spiNbytes);  //Start SPI read
    return IORD_32DIRECT(NRF24_SPI_BASE, 12);           //return the data

void SpiWrite(alt_u32 data,alt_u8 spiNbytes,alt_u8 spiReg)
    spiReg = 0x1F & spiReg;
    spiReg = 0x20 | spiReg;
    data = data<<8;
    IOWR_32DIRECT(NRF24_SPI_BASE, 0, data | spiReg); //set register
    IOWR_8DIRECT(NRF24_SPI_BASE, 8, 0x07 & spiNbytes); //begin write SPI

int main(void) 
 alt_u32 dat = 0;
 dat = SpiRead(1, 0x06); //read spi

 dat = ((dat>>8) & 0x000000f8) | 0x00000005; //change data
 SpiWrite(dat, 1, 0x06);  //write data back

 dat = SpiRead(1, 0x06); //read data to validate

 while (1)  {   }  //stay here forever

 return 0;

If I remove the while loop I get 4 times SPIread and after that 2 times SPIwrite. Each little change may change everything... My program is 6Kbytes now and I have 18Kbytes dedicated memory (OnChipMemory).

What is wrong, please help! Dmitry.

asked on Stack Overflow Feb 22, 2015 by Dmitry Kezin

0 Answers

Nobody has answered this question yet.

User contributions licensed under CC BY-SA 3.0