This chapter shows how to write a "hello world" program on the Pico-G1 board, compile it into the rootfs, flash the board, and run it — the complete path from source code to execution on the board.
To keep the project structure clear and maintainable, put the application under SDK/source/app:
mkdir -p source/app/helloworld # create the helloworld application
cd source/app/helloworld/ # change into the directory
touch helloworld.c # create the .c file
touch Makefile # create the Makefile
vim helloworld.c
helloworld.c:
#include <stdio.h>
int main(void)
{
printf("hello world from firmware!\n");
return 0;
}
vim Makefile
The Makefile:
# SDK root directory.
# This Makefile lives in source/app/helloworld/,
# so going up three levels returns to the SDK root.
SDK_DIR := $(shell cd $(shell pwd)/../../.. && /bin/pwd)
# Pull in the SDK common build configuration.
# This defines the cross compiler, compile flags, link flags, rootfs output directory, and more.
include $(SDK_DIR)/build/base.mk
# Name of the final executable.
TARGET := helloworld
# Source files.
SRCS := helloworld.c
# Derive object files from the source files.
# For example helloworld.c -> helloworld.o
OBJS := $(SRCS:.c=.o)
# Declare phony targets so they never conflict with files of the same name.
.PHONY: all clean install
# Default target.
# Running make builds the final program.
all: $(TARGET)
# Link rule.
# Links the .o objects into the final executable.
$(TARGET): $(OBJS)
$(CC) $(SDK_LD_CFLAGS) -o $@ $^
# Compile rule.
# Compiles a .c source file into an .o object file.
%.o: %.c
$(CC) $(SDK_USR_CFLAGS) -c -o $@ $<
# Install into the rootfs.
# The final path on the board will be /usr/bin/helloworld
install: all
@mkdir -p $(XMEDIA_ROOTFS_DIR)/usr/bin
@cp -f $(TARGET) $(XMEDIA_ROOTFS_DIR)/usr/bin/$(TARGET)
@chmod 755 $(XMEDIA_ROOTFS_DIR)/usr/bin/$(TARGET)
@echo "Installed $(TARGET) to $(XMEDIA_ROOTFS_DIR)/usr/bin/$(TARGET)"
# Remove build artifacts.
clean:
rm -f $(TARGET) $(OBJS)
# Run from the SDK root:
make -C source/app/helloworld clean # remove previous build output
make -C source/app/helloworld # cross-compile helloworld for the board
# Run from the SDK root:
make rootfs_clean
make rootfs
# Run from the SDK root:
make -C source/app/helloworld install
Expected output:
ljh@lcwt-KNPP-D32-R-Series:~/GK7206$ make -C source/app/helloworld install
make: Entering directory '/home/diskh/ljh/GK7206/source/app/helloworld'
Installed helloworld to /home/diskh/ljh/GK7206/out/xm7206v12a/rootfs/usr/bin/helloworld
make: Leaving directory '/home/diskh/ljh/GK7206/source/app/helloworld'
#### make "-C source/app/helloworld install" completed successfully! ####
At this point
helloworldappears underout/xm7206v12a/rootfs/usr/bin:ls -l out/xm7206v12a/rootfs/usr/bin/helloworld
make fs_image
See Image Flashing and flash the SPI image.
Enter the board shell over SSH or serial:
cd /usr/bin
./helloworld
Expected output:
hello world from firmware!