Programmable Devices
CPLDs, FPGAs, SoC FPGAs, Configuration, and Transceivers
20641 Discussions

C code question !! / NIOS II / SPI interface

Altera_Forum
Honored Contributor II
3,394 Views

Hello, 

 

I have a question about implementing the spi interface in a FPGA, I already constructed a working CPU with a implementation of the SPI, and added the correct settings in SOIC, however i have a problem with my code for the niosII proccesor, for some reason it jumps right over my written code, it jumps straigt to the end of the main() function, I am as you would say a nOOb in programming... so i think i just made a stupid mistake in my code, but I cannot locate it...  

 

Below states the code that i have written, for testing purpose's my idea was to send 0xFF a thousand times.... however when i go in debug mode it goes into int alt_avalon_spi_command(alt_u32 base, alt_u32 slave,......) and after that it jumps strait to the end without execucution the code under it. 

 

The development board is a NIOS development board, Cyclone I edition... 

 

I hope one of you could explain what i am doing wrong !! :confused:  

 

Thanks a lot !! :)  

 

greetings, 

 

Remko 

 

 

# include <stdio.h># include <system.h># include <altera_avalon_pio_regs.h> 

//#include <altera_avalon_pwm_routines.h> 

//#include <altera_avalon_uart_regs.h># include <string.h># include <sys/alt_alarm.h># include <alt_types.h># include <sys/time.h># include <fcntl.h> 

 

//SPI headers# include <altera_avalon_spi_regs.h># include <altera_avalon_spi.h> 

int delay_1(); 

int spi(); 

 

int main() 

{  

int alt_avalon_spi_command(alt_u32 base, alt_u32 slave, 

alt_u32 write_length, 

const alt_u8* wdata, 

alt_u32 read_length, 

alt_u8* read_data, 

alt_u32 flags) 

int i; 

// force SS_N active 

IOWR_ALTERA_AVALON_SPI_CONTROL(base, ALTERA_AVALON_SPI_CONTROL_SSO_MSK); 

for (i = 0; i<10000 ; ++i) 

// transmit a byte 

while (!(IORD_ALTERA_AVALON_SPI_STATUS(base) & ALTERA_AVALON_SPI_STATUS_TRDY_MSK)); 

IOWR_ALTERA_AVALON_SPI_TXDATA(base, 0xFF); 

 

// wait for all bytes to be send 

while(!(IORD_ALTERA_AVALON_SPI_STATUS(base) & ALTERA_AVALON_SPI_STATUS_TMT_MSK)); 

//release SS_N 

IOWR_ALTERA_AVALON_SPI_CONTROL(base, ~ALTERA_AVALON_SPI_CONTROL_SSO_MSK); 

 

delay_1(); 

 

 

int delay_1() 

{  

int count = 0; 

int delay; 

while(1) 

 

IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE, count & 0x03); 

IOWR_ALTERA_AVALON_PIO_DATA(SEVEN_SEG_PIO_BASE, count & 0xff); 

delay = 0; 

while(delay < 200000) 

delay++; 

count++; 

}  

}
0 Kudos
14 Replies
Altera_Forum
Honored Contributor II
1,463 Views

Why do you define a alt_avalon_spi_command function inside main()? 

Just put your code directly inside main() and it will run. 

 

I didn't even know this was legal in C...
0 Kudos
Altera_Forum
Honored Contributor II
1,463 Views

Thanks for the quick reply !! Well, thats a example .c file didint knew that would be a problem. But i don't quite understand what you mean by putting it int main(), if I define alt_avalon_spi_command () somewhere else than in main(); the code will not be accepted as then it will not reconise functions as IOWR_ALTERA_AVALON_SPI_CONTROL(base); I dont fully understandt the alt_avalon_spi_command(), although i have read the software reference, could you maybe give me a example CTRL+C, CTRL+V of my code by what you meant by putting the code inside main()... I have tried it but it didn't work for me...

0 Kudos
Altera_Forum
Honored Contributor II
1,463 Views

Hey, kind of funny to reply to my own thread :p, but i have solved the problem! just as you suggested i can't define alt_avalon_spi_command function inside main()... stupid me... it was already defined in the header file... stupid mistake.... 

 

thanks again for your reply !  

 

greets, 

 

remko
0 Kudos
Altera_Forum
Honored Contributor II
1,463 Views

Hi, I read your code and it´s the only help about programming a SPI interface on a DE2... my question is if you can post your final code, because I have huge problems with programming it at NIOS eclipse and it will be a huge help to me... thanks for your time and help... :)

0 Kudos
Altera_Forum
Honored Contributor II
1,463 Views

Hello, that's a long time ago, I need to find it first ;), but that won't be a problem, I will post it as soon as I can :)

0 Kudos
Altera_Forum
Honored Contributor II
1,463 Views

Hi there, after a lot of searching i have found the code you requested :) 

 

The data is sent 1 byte at a time, i have called it data, good luck with it ;) 

 

int SPI (int data) 

/* 

* Discard any stale data present in the RXDATA register, in case 

* previous communication was interrupted and stale data was left 

* behind. 

*/  

IORD_ALTERA_AVALON_SPI_RXDATA(SPI_0_NVRAM_BASE); 

 

// initiate SS_N 

IOWR_ALTERA_AVALON_SPI_CONTROL(SPI_0_NVRAM_BASE, ALTERA_AVALON_SPI_CONTROL_SSO_MSK); 

 

// transmit a byte, set write enable latch 

while (!(IORD_ALTERA_AVALON_SPI_STATUS(SPI_0_NVRAM_BASE) & ALTERA_AVALON_SPI_STATUS_TRDY_MSK)); 

IOWR_ALTERA_AVALON_SPI_TXDATA(SPI_0_NVRAM_BASE, data); 

 

// wait for all bytes to be send 

while(!(IORD_ALTERA_AVALON_SPI_STATUS(SPI_0_NVRAM_BASE) & ALTERA_AVALON_SPI_STATUS_TMT_MSK)); 

 

// release SS_N 

IOWR_ALTERA_AVALON_SPI_CONTROL(SPI_0_NVRAM_BASE, ~ALTERA_AVALON_SPI_CONTROL_SSO_MSK); 

}
0 Kudos
Altera_Forum
Honored Contributor II
1,463 Views

how can i clean the LCD ? is there any function?

0 Kudos
Altera_Forum
Honored Contributor II
1,463 Views

Hi... thanks about the code... but it is only a function? or is a entire c program? well I need to send a byte to a slave, but I don´t know what "includes" put in my main code... if you know about it, please tell me... :)

0 Kudos
Altera_Forum
Honored Contributor II
1,463 Views

Hi, 

 

I would like o implement SPI with NIOS II in Qsys. I want NIOS to read and write data from a memory outside with SPI interface. Does this code work? I think it only sends data and while sending data to the memory is this function is enough and you don't deal with addresses and stuff. 

 

Thanks
0 Kudos
Altera_Forum
Honored Contributor II
1,463 Views

There is also a register you can use to read back the data that was received from the SPI bus. Have a look at the SPI IP documentation. 

The SPI IP just sends and reads back a bunch of bits. It has no idea about the protocol underneath and you need to do it yourself. So to deal with "addresses and stuff", have a look at your SPI memory datasheet and see how you should implement the protocol.
0 Kudos
Altera_Forum
Honored Contributor II
1,463 Views

Hi, 

 

I did not mean that actually, I know it only transfers data but does not know their meaning or if this comes this is to be done information. What I am trying to ask is how do I get it from SPI buffer to my memory or to NIOS (which are the two sides in my system NIOS-SPI-RAM ) and also I have an information in NIOS and I want to send it to RAM how do I do that where do I select where on the memory is data to be written. 

 

Let me explain what I did. In Qsys I connect NIOS instruction and data master to SPI core's control port and connect SPI's conduit end a memory. I use this function in NIOS "alt_avalon_spi_command" to read or write data. What more should I do? 

 

Thanks
0 Kudos
Altera_Forum
Honored Contributor II
1,463 Views

I'm not sure I understand your problem. What kind of RAM are you using? Is it an external SPI RAM? From your other thread here (http://www.alteraforum.com/forum/showthread.php?t=39915&p=164635#post164635) it doesn't seem to be the case, but in that case why are you connecting it to the SPI component?

0 Kudos
Altera_Forum
Honored Contributor II
1,463 Views

Hi; 

 

I am now connecting it to an SPI component but I could not manage to program the NIOS. Where do I put my code? I created the Qsys project and generated it and with SOPC file I tried to use Eclipse to program the code but I could not manage it. Can you tell be how to do it I am a newbie I would appreciate any step even if you think it is natural. 

 

Thank you 

Ugur
0 Kudos
Altera_Forum
Honored Contributor II
1,462 Views

From Eclipse you should generate a BSP for your project and a sample "hello world" application. If you look on the Altera web site I think you'll find a "my first nios 2" tutorial that should describe how to do it.

0 Kudos
Reply