DESK-XZ7-L/Development/Hello World example

From DAVE Developer's Wiki
Jump to: navigation, search
History
Issue Date Notes
2024/01/29 DESK-XZ7-L-1.0.1 release



Hello World example[edit | edit source]

Here below is an example of C code displaying the classic Hello World! message on the target serial console.

This example shows how to use the arm cross-compiler using the environment configured for this purpose

Setting the cross-compiler[edit | edit source]

  • start the Linux development VM and login into the system
  • install the toolchain, for example for BORA SOM
dvdk@vagrant:~$ wget https://mirror.dave.eu/desk-xz-l/desk-xz7-l-1.0.1/desk-xz7-l-1.0.1_bora_sdk.sh
--2024-01-26 11:57:04--  https://mirror.dave.eu/desk-xz-l/desk-xz7-l-1.0.1/desk-xz7-l-1.0.1_bora_sdk.sh
Resolving mirror.dave.eu (mirror.dave.eu)... 84.46.251.143
Connecting to mirror.dave.eu (mirror.dave.eu)|84.46.251.143|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 998591008 (952M) [text/x-sh]
Saving to: ‘desk-xz7-l-1.0.1_bora_sdk.sh’

desk-xz7-l-1.0.1_bora_sdk.sh                    100%[====================================================================================================>] 952.33M  10.8MB/s    in 1m 40s  

2024-01-26 11:58:43 (9.57 MB/s) - ‘desk-xz7-l-1.0.1_bora_sdk.sh’ saved [998591008/998591008]

dvdk@vagrant:~$ chmod 755 desk-xz7-l-1.0.1_bora_sdk.sh 
dvdk@vagrant:~$ ./desk-xz7-l-1.0.1_bora_sdk.sh 
PetaLinux SDK installer version 2021.2
======================================
Enter target directory for SDK (default: /opt/petalinux/2021.2): 
You are about to install the SDK to "/opt/petalinux/2021.2". Proceed [Y/n]? y
Extracting SDK.......................................................................................................................................................................................................................done
Setting it up...done
SDK has been successfully set up and is ready to be used.
Each time you wish to use the SDK in a new shell session, you need to source the environment setup script e.g.
 $ . /opt/petalinux/2021.2/environment-setup-cortexa9t2hf-neon-xilinx-linux-gnueabi
  • open a terminal window and cd into your source code directory
dvdk@vagrant:~$ mkdir -p ~/myproject
dvdk@vagrant:~$ cd ~/myproject/
dvdk@vagrant:~/myproject$ vi hello.c
dvdk@vagrant:~/myproject$ cat hello.c 
#include <stdio.h>

int main(){
        printf("Hello, World!\n");
        return 0;
}
  • configure the build environment
dvdk@vagrant:~/myproject$ source /opt/petalinux/2021.2/environment-setup-cortexa9t2hf-neon-xilinx-linux-gnueabi 
  • as you can see here below, the $CC environment variable has been properly configured for using the SDK sysroot parameter:
dvdk@vagrant:~/myproject$ echo $CC                                                                                                                                                                     
arm-xilinx-linux-gnueabi-gcc -mthumb -mfpu=neon -mfloat-abi=hard -mcpu=cortex-a9 -fstack-protector-strong -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security --sysroot=/opt/petalinux/2021.2/sysroots/cortexa9t2hf-neon-xilinx-linux-gnueabi
  • invoke the cross-compiler for compiling your source code example: the object file obtained, is a proper ELF 32-bit for the target microprocessor
dvdk@vagrant:~/myproject$ $CC -O hello.c -o hello
dvdk@vagrant:~/myproject$ file hello
hello: ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, BuildID[sha1]=cd0630d98370eb6b80423fb1b4b1e3411f2f4576, for GNU/Linux 3.2.0, with debug_info, not stripped

Running the example on the target[edit | edit source]

Now it is enough to copy the object file on target and execute it:

...
...
root@bora:~# ./hello
Hello, World!
root@bora:~#