Nios® II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++
Announcements
Intel Support hours are Monday-Fridays, 8am-5pm PST, except Holidays. Thanks to our community members who provide support during our down time or before we get to your questions. We appreciate you!

Need Forum Guidance? Click here
Search our FPGA Knowledge Articles here.

Blinking LED - stays on

Altera_Forum
Honored Contributor I
1,951 Views

I was trying to use NIOS to get an LED to blink over a set period. However, everytime the code is loaded the LED stays on. 

 

After a couple of hours of troubleshooting I added a print statement for the hell of it and the LED started blinking. Commeting out the print statement the behavior reverted back to what it was before. 

 

I don't see anything wrong with the code, so I doubt that is the problem. I am guessing my SOPC is messed up somewhere but I don't know where it could be.  

 

My SOPC has: 

1 - pio (input) 

1 - pio (output) 

JTAG interface 

onboard memory (RAM 8 kB) 

SD Card interface 

 

I am just learning VHDL, Nios, and SOPC, so right now this has been the most complicated system I have created thus far 

 

--------------------------------------------------------------- 

# include "System.h"# include "altera_avalon_pio_regs.h"# include <stdio.h># include "sys/alt_stdio.h" 

 

int main() 

{  

//alt_putstr("Hello from Nios II!\n"); 

int count = 0; 

int delay = 0; 

 

/* Event loop never exits. */ 

while (1) 

IOWR_ALTERA_AVALON_PIO_DATA(LEDS_BASE, count & 0x01) ; 

delay = 0; 

while( delay < 10000) 

delay++; 

count++; 

//alt_putstr("Hello from Nios II!\n"); 

 

return 0; 

}
0 Kudos
5 Replies
Altera_Forum
Honored Contributor I
201 Views

Your LED is probably blinking but you can't see it because the frequency is very high. 

Adding the print call adds a delay greater than the while loop, so the frequency decreases to a "visible" range. 

Make this test: force the LED always on and you'll see a higher light intensity. 

 

While or for loops are a deprecated method to implement delays; besides the dependence on cpu speed, they are strongly affected by compiler optimizations. Some hard optimized compilers would even remove loops which have no functional side effect, like in your case. 

Using a timer would be a better solution.
Altera_Forum
Honored Contributor I
201 Views

If you make a delay by a while loop, the compiler will optimize it away. If you would like to include the delay, you either set the compiler option to not do optimization, or your should use a timer based delay function from the library.

Altera_Forum
Honored Contributor I
201 Views

Moving the definition of 'delay' outside the function, and making it 

volatile int delay;will force the compiler to generate the code loop. 

I'd also add two more zeros to the count. 

At a guess, the delay loop is about 20 clocks (with volatile data), at 100MHz you'd need about 2500000 iterations to get 1Hz flash.
Altera_Forum
Honored Contributor I
201 Views

 

--- Quote Start ---  

Moving the definition of 'delay' outside the function, and making it 

volatile int delay;will force the compiler to generate the code loop. 

I'd also add two more zeros to the count. 

At a guess, the delay loop is about 20 clocks (with volatile data), at 100MHz you'd need about 2500000 iterations to get 1Hz flash. 

--- Quote End ---  

 

 

This worked perfectly, pretty cool seeing the LED blinking. Also, tried doing it in the manner that Sanmao suggested. 

 

A follow up question; I am working on a project this semester with video (long shot) or image processing, if I add more memory to my SOPC build will I have to worry about manually controlling memory like I did in this code? Or is that something that I will know as I actually begin my project? 

 

Thank you for the help.
Altera_Forum
Honored Contributor I
201 Views

 

--- Quote Start ---  

This worked perfectly, pretty cool seeing the LED blinking. Also, tried doing it in the manner that Sanmao suggested. 

 

A follow up question; I am working on a project this semester with video (long shot) or image processing, if I add more memory to my SOPC build will I have to worry about manually controlling memory like I did in this code? Or is that something that I will know as I actually begin my project? 

 

Thank you for the help. 

--- Quote End ---  

 

This is Final code ? it dosen't work to me ... 

#include <stdio.h># include "system.h"# include "altera_avalon_pio_regs.h" volatile int delay; int main() { int count = 0; //int delay=0; //printf("Test!\n"); while(1) { IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE, count & 0x01); delay = 0; while(delay < 2500000) { printf("Hello from Nios II!\n"); delay++; } count++; } return 0; }
Reply