Tutorial: Altera Cyclone5 SoC Baremetal from scratch

Using ARM processors is all the rage recently. Apple, Samsung, and many others including Intel (see CNET article) are making devices with ARM processor designs at their core. Not to be left out, Xilinx and Altera are also jumping on the bandwagon including ARM Cortex-A9 cores in their Zynq and Cyclone5 SoC devices respectively. Some of these companies are farther along than others…

Which brings me to the point of this post, that finding helpful and complete tutorials for Altera’s Cyclone5 SoC is very difficult. What is available is some items from Altera and others from ARM but nothing cohesive or complete. Although the ARM processor architecture is the same between Zynq and Cyclone5 SoC the tool flows are very different. Hopefully by the end of this tutorial you’ll understand exactly what the state of the tools (and licensing) are and have a from scratch DS-5/Eclipse hello world project running on your Cyclone5 SoC.

On my side, I’m using Altera’s SoC Tools version 13.1 which includes the ARM DS-5 (Altera Edition) Version 5.15.0 and that these are already installed. I’ll assume that you’ve got the board setup (powered on, connected to your system, drivers installed — meaning you can see the JTAG chain in Quartus programmer).

First, licensing. With your C5SoC you got what looks like a certificate from Altera with an “ARM License Serial#”. Unfortunately, this only entitles you to use the ARM debugger and other associated tools. Not the ARM compiler, linker, etc. But you can get a 30-day license for these tools! To do so, open the DS-5 Eclipse (located in <install_dir>/DS-5/bin/ called “eclipse”) and then click Help –> ARM License Manager. You’ll need an account at ARM’s website to do this too, but you can create one from the GUI. After you do this, the license file will reside in <path_to_your_home_directory>/<your_username>/.ds5/licenses/. If you install 2 licenses: one from Altera and the 30-day license just remember that you’ll need the 30-day one for compiling.

Now, you need to create an environment variable as such:
ARMLMD_LICENSE_FILE=”/<path_to_your_home_directory>/<your_username>/.ds-5/licenses/<license_filename>.lic”

At this point, you’re ready to create the project. Choose File –> New –> C Project and give it a meaningful name. Choose Hello World Project and notice that the only toolchain available is the “ARM Compiler”. Click Finish. So now you have a project, that you’d expect to be able to just build, download, and run. But if it were that easy, a monkey could do it instead of a talented and trained engineer so its actually not that simple…

Next we’ll need to setup the build settings so right-click on the project and choose Properties. On the left side expand C/C++ Build and click Settings. Under ARM C Compiler –> Code Generation enter “Cortex-A9” for the Target CPU (perhaps this is something that will be already setup for you in the future…hint hint!). Next, since we’re using the evaluation license from ARM we need to point the executable at the specific licensed component we want it to use. Under ARM C Compiler –> Miscellaneous enter “–tool_variant=ds5eval” in the Other flags box. Do the same again for the ARM Assembler as well (for future proofing in case you decide to write some assembly code). For the ARM Linker, you’ll need to enter “Cortex-A9” under General (instead of Code Generation) and and add the “–tool_variant=ds5eval” in Miscellaneous –> Other flags. Lastly, under Image Layout a scatter file must be supplied (this is the equivalent of a linker script to place the code in the appropriate sections  of the processors memory. A sample file from ARM/Altera is shown below (and I claim no credit for its creation):

;**************************************************
; Copyright (c) 2013 ARM Ltd. All rights reserved.
;**************************************************

; Scatter-file for RAM based example on Altera Cyclone V

; This scatter-file places application code, data, stack and heap at suitable addresses in the memory map.

; Altera Cyclone V has internal RAM at 0xFFFF0000, which this scatter-file uses.

SDRAM 0xFFFF0000
{
APP_CODE +0
{
* (+RO, +RW, +ZI)
}

ARM_LIB_STACKHEAP +0 ALIGN 8 EMPTY 0x1000 ; Application heap and stack
{ }
}

So you’ll need to create a file called “scatter.scat” in the project directory and put the contents shown above (or you’re own modified version) into this file. Then add this file in the “Scatter file” option in Image Layout for the ARM Linker in the Project Settings.

Thats it, now you can click the hammer icon and build the project.

The last order of business is setting up a Debug Configuration to be able to download the executable to the board. Choose Run –> Debug Configurations… and double-click on DS-5 Debugger to create a new configuration. Set the name to be something meaningful to you. Then you can select the target to be Altera –> Cyclone V SoC –> Bare Metal Debug –> Debug Cortex-A9_0 via Altera USB-Blaster. Under Connections click browse and select the USB Blaster thats connected to your board. Thats all for the Connection tab, switch to the Files tab and select the *.axf file in the Workspace under the Debug folder in your project. Click Apply and then Debug. Click Yes to switch to debug view and then once it gets all set up click the green triangle (play button) to start your program running. Notice the print statement in the Application Console in the bottom right.

Thats it, now you have a from scratch baremetal application.

Leave a comment