Zephyr RTOS initial setup and building Hello World - joectchan/blog GitHub Wiki
Background
I learn STM32 development using ST Disco L475 IOT01 (B-L475E-IOT01A). It has many on-board peripherals. That saves me a lot of time. It is well worth the money to start learning with this board.
Zephyr is an RTOS with good support for many development boards, including mine. This page describes my board, and Zephyr's support of the board. It is a good reference page. I come back from time to time for info of the microcontroller and peripherals onboard.
I use a NUC pc, running Ubuntu, as my development host.
Setup
I follow steps 1 to 6 from this page to install Zephyr build system, Zephyr source code and development toolchain. It is possible to use other toolchain, but let's take the easy path and focus our energy on the learning itself.
Step 7 starts to build Zephyr RTOS + application. The instruction on that page is generic for all boards, but I already chose a specific board. I do not follow step 7. I go back to the page about my board, follow the steps under the Flashing section. Please read the instruction in that section to understand what should happen, but return here for the exact instructions that I used.
Build
I changed a few parameters when I actually build my Zephyr + Hello world binary. west help build
shows the help screen of the build command.
joseph@nuc:~/workspace/zephyrproject2$ west help build
usage: west build [-h] [-b BOARD] [-d BUILD_DIR]
[-t TARGET] [-p {auto, always, never}] [-c] [--cmake-only]
[-n] [-o BUILD_OPT] [-f]
[source_dir] -- [cmake_opt [cmake_opt ...]]
Convenience wrapper for building Zephyr applications.
positional arguments:
source_dir Use this path as the source directory
cmake_opt Extra options to pass to CMake; implies -c
optional arguments:
-h, --help show this help message and exit
-b BOARD, --board BOARD
Board to build for
-d BUILD_DIR, --build-dir BUILD_DIR
Build directory. If not given, the default build
directory is build/ unless the build.dir-fmt
configuration variable is set. The current directory
is checked after that. If either is a Zephyr build
directory, it is used. Otherwise the default build
directory is created and used.
-t TARGET, --target TARGET
Build system target ("usage" for more info; and "help"
for a list)
-p [{auto,always,never}], --pristine [{auto,always,never}]
Control whether the build folder is made pristine
before running CMake. --pristine is the same as
--pristine=always. If 'auto', it will be made pristine
only if needed.
-c, --cmake Force CMake to run
--cmake-only Just run CMake; don't build. Implies -c.
-n, --just-print, --dry-run, --recon
Just print the build commands; don't run them
-o BUILD_OPT, --build-opt BUILD_OPT
Options to pass to the build tool. May be given more
than once to append multiple values.
-f, --force Ignore any errors and try to proceed
I use these parameters:
-p auto
The first time I build any binary would not need this parameter. Subsequent builds may need it. I will change to-p always
once if I get really stuck. I have not (yet) needed-f
to force build so far.-s zephyr/samples/hello_world
Specify my source directory.-d build-hello-world
Specify my build/destination directory.-b disco_l475_iot1
Specify my build/destination directory. (Note that my zephyr directory is at ~/workspace/zephyrproject2. Yes, it took me a few times before I get all things straighten out.) This is the screenshot of what I typed and my result.
joseph@nuc:~/workspace/zephyrproject2$ west build -p auto -s zephyr/samples/hello_world -d build-hello-world -b disco_l475_iot1
-- west build: generating a build system
Including boilerplate (Zephyr base): /home/joseph/workspace/zephyrproject2/zephyr/cmake/app/boilerplate.cmake
-- Application: /home/joseph/workspace/zephyrproject2/zephyr/samples/hello_world
-- Zephyr version: 2.2.99 (/home/joseph/workspace/zephyrproject2/zephyr)
-- Found Python3: /usr/bin/python3.7 (found suitable exact version "3.7.5") found components: Interpreter
-- Board: disco_l475_iot1
-- Found west: /home/joseph/.local/bin/west (found suitable version "0.7.2", minimum required is "0.7.1")
-- Found toolchain: zephyr (/home/joseph/zephyr-sdk-0.11.2)
-- Found BOARD.dts: /home/joseph/workspace/zephyrproject2/zephyr/boards/arm/disco_l475_iot1/disco_l475_iot1.dts
-- Generated zephyr.dts: /home/joseph/workspace/zephyrproject2/build-hello-world/zephyr/zephyr.dts
-- Generated devicetree_unfixed.h: /home/joseph/workspace/zephyrproject2/build-hello-world/zephyr/include/generated/devicetree_unfixed.h
Parsing /home/joseph/workspace/zephyrproject2/zephyr/Kconfig
Loaded configuration '/home/joseph/workspace/zephyrproject2/zephyr/boards/arm/disco_l475_iot1/disco_l475_iot1_defconfig'
Merged configuration '/home/joseph/workspace/zephyrproject2/zephyr/samples/hello_world/prj.conf'
Configuration saved to '/home/joseph/workspace/zephyrproject2/build-hello-world/zephyr/.config'
Kconfig header saved to '/home/joseph/workspace/zephyrproject2/build-hello-world/zephyr/include/generated/autoconf.h'
-- The C compiler identification is GNU 9.2.0
-- The CXX compiler identification is GNU 9.2.0
-- The ASM compiler identification is GNU
-- Found assembler: /home/joseph/zephyr-sdk-0.11.2/arm-zephyr-eabi/bin/arm-zephyr-eabi-gcc
-- Cache files will be written to: /home/joseph/.cache/zephyr
-- Configuring done
-- Generating done
-- Build files have been written to: /home/joseph/workspace/zephyrproject2/build-hello-world
-- west build: building application
[1/129] Preparing syscall dependency handling
[124/129] Linking C executable zephyr/zephyr_prebuilt.elf
Memory region Used Size Region Size %age Used
FLASH: 12748 B 1 MB 1.22%
SRAM: 4312 B 96 KB 4.39%
IDT_LIST: 168 B 2 KB 8.20%
[129/129] Linking C executable zephyr/zephyr.elf
joseph@nuc:~/workspace/zephyrproject2$
Flashing Zephyr + Hello world
Attach USB cable from my NUC pc to the ST LINK micro USB port near the corner of the target board. (There are two ports. The corner port has label STLINK. The center port has USBOTG. You want the ST LINK port near the corner.)
My Ubuntu detects the USB device and asks for my password before it allows anything to attach. Just type my login password to grant permission.
If Ubuntu does not detect anything, you should double check Setup Step 6 to configure and activate a udev rule.
My NUC pc sees a serial port. I attach a PuTTY serial console (or minicom if you want). It uses the common serial port settings 115200,8,n,1
joseph@nuc:~/workspace/zephyrproject2$ ls /dev/ttyA*
/dev/ttyACM0
I built Zephyr + Hello World elf file in build-hello-world/ directory, so I added -d build-hello-world
to west flash
command. After I issue this command, it will download and execute the RTOS and the application. You will see a Hello World message on PuTTY/minicom.
This is the screenshot of what I typed and my result.
joseph@nuc:~/workspace/zephyrproject2$ west flash -d build-hello-world
-- west flash: rebuilding
ninja: no work to do.
-- west flash: using runner openocd
-- runners.openocd: Flashing file: /home/joseph/workspace/zephyrproject2/build-hello-world/zephyr/zephyr.hex
Open On-Chip Debugger 0.10.0+dev-01340-ga0e8edc4e-dirty (2020-02-14-05:38)
Licensed under GNU GPL v2
For bug reports, read
http://openocd.org/doc/doxygen/bugs.html
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
srst_only separate srst_nogate srst_open_drain connect_deassert_srst
Info : clock speed 500 kHz
Info : STLINK V2J36M26 (API v2) VID:PID 0483:374B
Info : Target voltage: 3.224163
Info : stm32l4x.cpu: hardware has 6 breakpoints, 4 watchpoints
Info : Listening on port 3333 for gdb connections
TargetName Type Endian TapName State
-- ------------------ ---------- ------ ------------------ ------------
0* stm32l4x.cpu hla_target little stm32l4x.cpu running
Info : Unable to match requested speed 500 kHz, using 480 kHz
Info : Unable to match requested speed 500 kHz, using 480 kHz
target halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0x08002f54 msp: 0x200057e8
Info : device idcode = 0x10076415 (STM32L47/L48xx - Rev: 4)
Info : flash size = 1024kbytes
Info : flash mode : dual-bank
Info : Padding image section 0 at 0x080031cc with 4 bytes (bank write end alignment)
Warn : Adding extra erase range, 0x080031d0 .. 0x080037ff
auto erase enabled
wrote 12752 bytes from file /home/joseph/workspace/zephyrproject2/build-hello-world/zephyr/zephyr.hex in 0.822728s (15.136 KiB/s)
Info : Unable to match requested speed 500 kHz, using 480 kHz
Info : Unable to match requested speed 500 kHz, using 480 kHz
target halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0x08000d4c msp: 0x20000648
verified 12748 bytes in 0.448384s (27.765 KiB/s)
Info : Unable to match requested speed 500 kHz, using 480 kHz
Info : Unable to match requested speed 500 kHz, using 480 kHz
shutdown command invoked
joseph@nuc:~/workspace/zephyrproject2$
Hello World message appears on my PuTTY.
*** Booting Zephyr OS build zephyr-v2.2.0-1791-g8d984b336ed4 ***
Hello World! disco_l475_iot1