Saturday 17 May 2014

Simple Nios II on the DE0-Nano - Part 4 of 4 (Nios Software)

In my last post I created the VHDL need to instantiate the VHDL component of my Niso processor. In this post  am going to do the final step and write some very simple software to flash the LED's

Download to the device


Make sure you VHDL hardware is downloaded into the FPGA before we start.

Create an new project



 Using the template I first create a new project called my_nios. I have selected the Hello World template as my starting ping for the code.


Because we only have 20k Bytes of RAM in our design we need to make out code memory footprint as small as possible. We do this by changing the properties on the BSP (Board support package) to remove C++ support and enable reduced device drives and small C library.

You should now be able to compile you simple Hello World program to ensure there are no errors be for we modify it with some LED flashing code!

Software

The following C-program is what I have written to flash my LEDs. To keep it simple as possible I did not even use any timers for create a delay instead I chose to use a simple FOR loop. No the best delay I know but it works for this program!  

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

void simple_loop_delay()
{
   int l;
   for (l=0; l<100000;l++);

}

int main()
{
  printf("Hello from Nios II!\n");

  while(1)
  {
     IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE, 0xaa);
     simple_loop_delay();

     IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE, 0x55);
     simple_loop_delay();
  }

  return 0;
}

The BSP (Board Support Package) generates a file for each peripheral you have in the system and a system.h file.

It the program above the system.h holds the address of the PIO block LED_PIO_BASE. It is best to use this instead of an absolute address because if the hardware changes then the address could also change.

The altera_avalon_pio_regs.h holds the prototypes to access the PIO peripheral. We are using the  IOWR_ALTERA_AVALON_PIO_DATA command. Writing 0xaa then 0x55 will flash the LEDs backwards and forwards.

That it! In this series of blog posts we have created and empty project, built a Qsys system, build the Qsys component  into VHDL and finally written some software. And yes it was all very straight forward.

---

No comments:

Post a Comment