The default EPCS Bootloader from altera resides in the first 0x400 (1K) bytes of the epcs controller. This bootloader figures out the size of the sof information loaded in the EPCS device and then assumes that that code follows immidiately afterwards. This works fine for single hardware and software images. However if you have multiple images or you wish to place the nios code at a different location, the default boot loader will not work for you. Included are two boot loaders. One written in assembly is about 340 bytes long and requies no additional memory and one written in "C" which is 780 bytes. Both bootloaders copy the code from EPCS flash and then jump to the _start routine of the code. These bootloaders can located on any onchip ram and are normally located at the reset location. If you want to use the ROM that is located in the EPCS controller you will need to overwrite the .hex file. i.e epcs_flash_controller_0_boot_rom_synth.hex in this example. Just beware that everytime you generate Qsys/sopc_builder will overwrite this file. So many time it is easiest to just use an onchip memory.
Assembly boot loader
This project consists of 4 files.
All these files are derived from the standard EPCS bootloader included with Nios2 processor. In my_boot_loader_standalone.S there are 2 defines which have to be set for each project
define SOFTWARE_OFFSET 0x1c0000
#define EPCS_FLASH_CONTROLLER_0_BASE 0x2000
THe SOFTWARE_OFFSET is the offset in the EPCS flash where the software is loaded. In this case it is at offset 0x1c0000. But it can be almost anywhere provided it does not conflict with a hardware image.
The EPCS_FLASH_CONTROLLER_0_BASE is the base address of the EPCS controller.
To build the project just run make from the Nios2 command shell.
CODE_BASE is always 0x0 unless you are trying to run this code in the debugger. Then it needs to be the addres of the physical memmory where the code will be loaded.
Importing into Eclipse
If you want to import this project into Eclipse for compiling and/or debugging go to the file menu and select import and Select the "Custom Makefil for Nios II Software Build Tools Project". After you have pointed to the directory and given the project a name you will need to add one more setting.
Right click on the project and go to properties. Select "C/C++ Build", and make sure the "Builder Settings" tab is highlighted
Uncheck the "Use Default Build Command"
In the Build commmand change the command from "make" to
Now it will build correctly from the Eclipse GUI. and if the CODE_BASE is correct you can step through the code with the debugger if you like.
The C bootloader is built off the hello_world_small project template. After the project is created delete the hello_world_small.c file and add the three files included in the zip.
The altera_avalon_spi.c is a small verion of alltera's code. If you look at you will see may functions simply stubbed out. This helps keep the footprint small.
In bootloader.h there is a define that needs to be set
define EPCS_OFFSET 0x1c0000
This is the offset to the code to be loaded.
In bootloader.c it is assumed that the EPCS flash controller is named EPCS_FLASH_CONTROLLER. If it is not then this will have to be updated. Also if you want to see the print statments comment out
be aware that this will double the size of the code.
Placing code in EPCS
Because the code we will load is not located in the default location in the EPCS there is an additional step necissary. elf2flashis used to create the flash image
efl2flash --epcs --input=my.elf --output=my.flash
however it is not located at the right offset. The above line will locate it at address/offset 0x0. To remady this we use nios2-elf-objcopy to offset the address to the correct location