Building a demo executable - GaloisInc/betaflight GitHub Wiki

Once the toolchain is set the next step is to test if everything is working.

Prerequisites

For the purpose we will need couple of things.

  1. The standalone SDK provided from Kendryte : https://github.com/kendryte/kendryte-standalone-sdk
  2. The demo source code provided from Kendryte : https://github.com/kendryte/kendryte-standalone-demo
  3. Kflash, the flashing utility provided from SiPEED to port the binary to the board : https://github.com/sipeed/kflash.py
  4. Check if you have CMake with typing in the terminal which cmake . You should get something like /usr/bin/cmake

I created one directory that I named kendryte . At the end I will have inside three subdirectories. One from cloning the SDK,one from cloning the demo and a third subdirectory that I call test_dir. In the test directory I add the SDK files and I add different source files from the demo folder. This is a personal preference. You can directly modify the SDK folder if you like.

Here is an example of the directory structure:

nik@nik~/development/kendryte $ ls
kendryte-standalone-demo  kendryte-standalone-sdk  test_dir

Here is the directory structure of the standalone-sdk

nik@nik~/development/kendryte $ tree -L 1 kendryte-standalone-sdk/
kendryte-standalone-sdk/
├── CHANGELOG.md
├── cmake
├── CMakeLists.txt
├── kendryte-package.json
├── lds
├── lib
├── LICENSE
├── package.json
├── README.md
├── scripts
├── src
└── third_party

6 directories, 6 files

And if we look in the src folder

nik@nik~/development/kendryte/kendryte-standalone-sdk/src $ tree -L 1 hello_world/
hello_world/
└── main.c

0 directories, 1 file

Building our first demo

  • Let's copy all the files in the test directory

cd kendryte-standalone-sdk/ && cp -r * ../test_dir

  • cd test_dir

If you are familiar with cmake then the following steps will not be anything new. For clarity I will separate the steps, however some of them can be combined. The standalone SDK comes with a project directory called hello_world.

  • mkdir build # create a folder for the binary files
  • cd build
  • cmake ../ -DPROJ=hello_world -DTOOLCHAIN=/opt/kendryte-toolchain/bin # call cmake to create the Makefile
  • make # Compile the source code

Example:

cmake ../ -DPROJ=hello_world -DTOOLCHAIN=/opt/kendryte-toolchain/bin
PROJ = hello_world
-- Check for RISCV toolchain ...
-- Using /opt/kendryte-toolchain/bin RISCV toolchain
-- The C compiler identification is GNU 8.2.0
-- The CXX compiler identification is GNU 8.2.0
-- The ASM compiler identification is GNU
-- Found assembler: /opt/kendryte-toolchain/bin/riscv64-unknown-elf-gcc
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
SOURCE_FILES=../kendryte/test_dir/src/hello_world/main.c

Project: hello_world
  LIST_FILE=../kendryte/test_dir/cmake/executable.cmake
  TOOLCHAIN=/opt/kendryte-toolchain/bin
  KENDRYTE_IDE=
  BUILDING_SDK=yes

  CMAKE_BUILD_TYPE=Debug
  CMAKE_C_COMPILER=/opt/kendryte-toolchain/bin/riscv64-unknown-elf-gcc
  CMAKE_CXX_COMPILER=/opt/kendryte-toolchain/bin/riscv64-unknown-elf-g++
  CMAKE_LINKER=/opt/kendryte-toolchain/bin/riscv64-unknown-elf-ld
  CMAKE_OBJCOPY=/opt/kendryte-toolchain/bin/riscv64-unknown-elf-objcopy
  CMAKE_OBJDUMP=/opt/kendryte-toolchain/bin/riscv64-unknown-elf-objdump
  CMAKE_RANLIB=/opt/kendryte-toolchain/bin/riscv64-unknown-elf-ranlib
  CMAKE_MAKE_PROGRAM=/usr/bin/gmake

  CMAKE_C_FLAGS= -mcmodel=medany -mabi=lp64f -march=rv64imafc -fno-common -ffunction-sections -fdata-sections -fstrict-volatile-bitfields -fno-zero-initialized-in-bss -ffast-math -fno-math-errno -fsingle-precision-constant -Os -ggdb -std=gnu11 -Wno-pointer-to-int-cast -Wall -Werror=all -Wno-error=unused-function -Wno-error=unused-but-set-variable -Wno-error=unused-variable -Wno-error=deprecated-declarations -Wno-multichar -Wextra -Werror=frame-larger-than=32768 -Wno-unused-parameter -Wno-sign-compare -Wno-error=missing-braces -Wno-error=return-type -Wno-error=pointer-sign -Wno-missing-braces -Wno-strict-aliasing -Wno-implicit-fallthrough -Wno-missing-field-initializers -Wno-int-to-pointer-cast -Wno-error=comment -Wno-error=logical-not-parentheses -Wno-error=duplicate-decl-specifier -Wno-error=parentheses -Wno-old-style-declaration
  CMAKE_CXX_FLAGS= -mcmodel=medany -mabi=lp64f -march=rv64imafc -fno-common -ffunction-sections -fdata-sections -fstrict-volatile-bitfields -fno-zero-initialized-in-bss -ffast-math -fno-math-errno -fsingle-precision-constant -Os -ggdb -std=gnu++17 -Wall -Werror=all -Wno-error=unused-function -Wno-error=unused-but-set-variable -Wno-error=unused-variable -Wno-error=deprecated-declarations -Wno-multichar -Wextra -Werror=frame-larger-than=32768 -Wno-unused-parameter -Wno-sign-compare -Wno-error=missing-braces -Wno-error=return-type -Wno-error=pointer-sign -Wno-missing-braces -Wno-strict-aliasing -Wno-implicit-fallthrough -Wno-missing-field-initializers -Wno-int-to-pointer-cast -Wno-error=comment -Wno-error=logical-not-parentheses -Wno-error=duplicate-decl-specifier -Wno-error=parentheses
  LDFLAGS= -nostartfiles -static -Wl,--gc-sections -Wl,-static -Wl,--start-group -Wl,--whole-archive -Wl,--no-whole-archive -Wl,--end-group -Wl,-EL -Wl,--no-relax -T /home/nik/development/kendryte/test_dir/lds/kendryte.ld
  CMAKE_BINARY_DIR=/home/nik/development/kendryte/test_dir/build
Makefile created.


-- Configuring done
-- Generating done
-- Build files have been written to:../kendryte/test_dir/build

Once cmake is done we will make

Example output of make :

......
[ 74%] Building C object lib/CMakeFiles/kendryte.dir/drivers/rtc.c.obj
[ 76%] Building C object lib/CMakeFiles/kendryte.dir/drivers/sha256.c.obj
[ 79%] Building C object lib/CMakeFiles/kendryte.dir/drivers/spi.c.obj
[ 81%] Building C object lib/CMakeFiles/kendryte.dir/drivers/sysctl.c.obj
[ 83%] Building C object lib/CMakeFiles/kendryte.dir/drivers/timer.c.obj
[ 86%] Building C object lib/CMakeFiles/kendryte.dir/drivers/uart.c.obj
[ 88%] Building C object lib/CMakeFiles/kendryte.dir/drivers/uarths.c.obj
[ 90%] Building C object lib/CMakeFiles/kendryte.dir/drivers/utils.c.obj
[ 93%] Building C object lib/CMakeFiles/kendryte.dir/drivers/wdt.c.obj
[ 95%] Linking C static library libkendryte.a
[ 95%] Built target kendryte
Scanning dependencies of target hello_world
[ 97%] Building C object CMakeFiles/hello_world.dir/src/hello_world/main.c.obj
[100%] Linking C executable hello_world
Generating .bin file ...
[100%] Built target hello_world

Flashing the binary

The best way to install kflash is using the pip3 package manager.

sudo pip3 install kflash

Once kflash is installed we can flash the binary.

To flash we need to know where our device is connected to and specifically which USB.

For more information on devices and the /dev directory you can look here.

To find the USB devices type ls /dev/ttyUSB*. To find out what devices are connected to our USB type lsusb. The example output of lsusb looks like below:

Bus 002 Device 002: ID 05ac:8406 Apple, Inc. Card Reader
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 004: ID 05ac:0259 Apple, Inc. Internal Keyboard/Trackpad
Bus 001 Device 007: ID 05ac:8289 Apple, Inc. BRCM20702 Hub
Bus 001 Device 003: ID 0a5c:4500 Broadcom Corp. BCM2046B1 USB 2.0 Hub (part of BCM2046 Bluetooth)
Bus 001 Device 002: ID 046d:c52b Logitech, Inc. Unifying Receiver
Bus 001 Device 008: ID 0403:6010 Future Technology Devices International, Ltd FT2232C/D/H Dual UART/FIFO IC
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Our device is Bus 001 Device 008: ID 0403:6010 Future Technology Devices International, Ltd FT2232C/D/H Dual UART/FIFO IC

Bus 001 means USB0.

Therefore, the last step is to call kflash and port the binary.

kflash -p /dev/ttyUSB0 hello_world.bin

Example output:

INFO] COM Port Selected Manually:  /dev/ttyUSB0 
[INFO] Default baudrate is 115200 , later it may be changed to the value you set. 
[INFO] Trying to Enter the ISP Mode... 
._
[INFO] Automatically detected goE/kd233 

[INFO] Greeting Message Detected, Start Downloading ISP 
Downloading ISP: |==========================================================================| 100.0% 10kiB/s
[INFO] Booting From 0x80000000 
[INFO] Wait For 0.1 second for ISP to Boot 
[INFO] Boot to Flashmode Successfully 
[INFO] Selected Flash:  On-Board 
[INFO] Initialization flash Successfully 
Programming BIN: |==========================================================================| 100.0% 9kiB/s
[INFO] Rebooting... 

With that we have successfully build and tested the SDK and the board ,and we got familiar with how to port the binary with Kflash.

We can add another source folder in the source directory from the demo repository we cloned,and build again and port to the board. We only have to modify the cmake command. There are many demo examples to try.