zephyr 搭建自己的工程

75 阅读1分钟

前置条件

  • ubuntu 24.04 LTS, 要求Ubuntu version 22.04 LTS and later
  • nucleo_f401re

SDK下载,环境配置

官网的环境搭建教程已经很好了,网络条件不好的情况下多试几下

Getting Started Guide — Zephyr Project Documentation

建立自己的工程目录

wwl@ubuntu:~$ tree my_app/
my_app/
├── app.overlay
├── CMakeLists.txt
├── Kconfig
├── prj.conf
└── src
    └── main.c

其中,my_app/prj.conf, my_app/Kconfig, my_app/app.overlay均为空

CMakeLists.txt内容如下

cmake_minimum_required(VERSION 3.20.0) 
set(BOARD nucleo_f401re)
find_package(Zephyr)
project(my_app)
target_sources(app PRIVATE src/main.c)

main.c内容如下

#include <stdio.h>

int main(void)
{
    printf("Hello World from my_app std!\n");
    return 0;
}

激活python虚拟环境,设置zephyr环境

source ~/zephyrproject/.venv/bin/activate    # 激活,退出用deactivate
source ~/zephyrproject/zephyr/zephyr-env.sh

编译

west build

日志如下:

wwl@ubuntu:~$ cd my_app/
wwl@ubuntu:~/my_app$ ls
app.overlay  CMakeLists.txt  Kconfig  prj.conf  src
wwl@ubuntu:~/my_app$ source ~/zephyrproject/.venv/bin/activate 
(.venv) wwl@ubuntu:~/my_app$ west build

...

[2/152] Generating include/generated/zephyr/version.h
-- Zephyr version: 4.3.99 (/home/wwl/zephyrproject/zephyr), build: v4.3.0-3969-ga0fdebcc908a
[152/152] Linking C executable zephyr/zephyr.elf
Memory region         Used Size  Region Size  %age Used
           FLASH:       17144 B       512 KB      3.27%
             RAM:        4544 B        96 KB      4.62%
           SRAM0:          0 GB        96 KB      0.00%
        IDT_LIST:          0 GB        32 KB      0.00%
Generating files from /home/wwl/my_app/build/zephyr/zephyr.elf for board: nucleo_f401re/stm32f401xe

使用cubeprogrammer,将my_app/build/zephyr/zephyr.hex烧录进单片机

image.png