Using an IDE is good for development, but when the work is done, there is no big reason to continue to open the IDE and click “Compile, Program, Run…“. So, the best solution is to run scripts and automate the process!
The FPGA needs to compile, be programmed and then run the C file over Nios 2 processor. The compiling and programming can be done only using Quartus tools, but, in order to Run a C file, it is necessary to use the Nios 2 Command Shell. Therefore, compiling and programming will be done using the Quartus Tools while building and running a C file can be done using the Nios 2 Command Shell.
Before starting, note that everything is done in a Windows computer. In a Linux computer the steps would be similar, yet different.
All the documentation is available online. A good reference is the Quartus II Scripting Reference Manual and the Nios II Software Developer Handbook, for Quartus Tools and Nios 2 Command Shell, respectively.
Quartus Tools
Compile
Using quartus_sh
is possible to run commands using a shell, which has a very small foorprint.
Running this command allows us to compile using the top file of the project.
Program
To program, it is not used the quartus_sh
anymore but the quartus_pgm
instead.
Note: try this command above to check if there are any USB-Blaster connected.
After testing connection, try to run the following command to program the previous compiled files to the FPGA:
If the command does not fail, the FPGA is now successfully programmed through USB-Blaster using JTAG.
Nios 2 Command Shell
This shell uses Windows Subsystem for Linux and, although some things are similar, others are not.
Nios 2 Command Shell uses the Linux terminal instead of Windows’ Power Shell, so it’s possible to use Linux terminal commands like cd
or make
.
After programming the FPGA, the system is changed - this means that the board support package (BSP) also needs to be refreshed or updated with new system parameters or characteristics. (Check older articles about this)
Updating BSP
To update the BSP the command is the following:
Nios II Command Shell.bat
opens the Linux terminal necessary to use the followed commands, like nios2-bsp-generate-files.exe
Note: if the software folder project name is adcnios the BSP directory will be adcnios_bsp.
After updating the BSP is now ok to build and run the C program written.
Build
To build, run:
The make
command builds the C file and checks if there is any error!
Run
If there are no errors, it is safe to proceed by running:
The terminal should output some text informing the processor is being programmed with the new C file and is running.
Note: ‘-g’ enables the processor to run. If the ‘-g’ is not specified, the processor will be paused even though the code is flashed.
Scripting with Python
To run terminal commands using Python there are a few possibilities. The most part of developers recommend to use the subprocess library.
By using this library, running a terminal command in python is as easy as:
Inside [ ] is the first command in the Quartus Tools, the equivalent to:
And ADC_TOP_PATH is a string variable pointing to the path of the top file of the project.
By using subprocess
to all of the previous commands, it is possible to get something like this:
Final Code
I’ve made some variables in order to create an-easy-to-manage-directory-variable-group, kinda. So the final code looks something like this: