Zephyr sur STM32F411
Installation de l’environnement
sudo apt install python3.12 python3.12-venv python3.12-dev
mkdir ~/WORKSPACE_ZEPHYR && cd ~/WORKSPACE_ZEPHYR
python3 -m venv .venv
pip install west
pip install --upgrade cmake ninja
source .venv/bin/activate
west init
west update
pip install -r zephyr/scripts/requirements.txt
west zephyr-export
west sdk install -t arm-zephyr-eabi # ~/zephyr-sdk-1.0.1 Système de Fichiers
tree -L 2
.
├── bootloader
│ └── mcuboot
├── build
│ ├── app
│ ├── build_info.yml
│ ├── build.ninja
│ ├── CMakeCache.txt
│ ├── CMakeFiles
│ ├── cmake_install.cmake
│ ├── compile_commands.json
│ ├── Kconfig
│ ├── modules
│ ├── sysbuild_modules.txt
│ ├── zephyr
│ ├── zephyr_modules.txt
│ └── zephyr_settings.txt
├── doc
│ └── reqmgmt
├── modules
│ ├── bsim_hw_models
│ ├── crypto
│ ├── debug
│ ├── fs
│ ├── hal
│ ├── lib
│ └── tee
├── tools
│ ├── edtt
│ └── net-tools
└── zephyr
├── AGENTS.md
├── arch
├── boards
├── cmake
├── CMakeLists.txt
├── CODE_OF_CONDUCT.md
├── CODEOWNERS
├── CONTRIBUTING.rst
├── doc
├── drivers
├── dts
├── include
├── Kconfig
├── Kconfig.constants
├── Kconfig.zephyr
├── kernel
├── lib
├── MAINTAINERS.yml
├── misc
├── modules
├── README.license
├── README.rst
├── REUSE.toml
├── samples
├── scripts
├── SDK_VERSION
├── share
├── snippets
├── soc
├── submanifests
├── subsys
├── tests
├── VERSION
├── version.h.in
├── west.yml
├── zephyr-env.cmd
└── zephyr-env.shBlink Led
cd ~/WORKSPACE_ZEPHYR
source .venv/bin/activate
west build -p auto -b nucleo_f411re zephyr/samples/basic/blinky
west flash --runner openocd --no-resetpuis appuyer sur le reset de la carte
REMARQUE: Forçage de l’utilisation d’openocd dans zephyr-sdk :
west flash --runner openocd --no-reset --openocd ~/zephyr-sdk-1.0.1/hosttools/sysroots/x86_64-pokysdk-linux/usr/bin/openocdHello World sur UART
cd ~/WORKSPACE_ZEPHYR
west build -p auto -b nucleo_f411re zephyr/samples/hello_world
west flash --runner openocd --no-resetpuis appuyer sur le reset de la carte
minicom -D /dev/ttyACM0
Welcome to minicom 2.8
OPTIONS: I18n
Port /dev/ttyACM0, 13:04:44
Press CTRL-A Z for help on special keys
*** Booting Zephyr OS build v4.5.0-6146-gdf7ac29b3886 ***
Hello World! nucleo_f411re/stm32f411xe Capteur de température I2C LM75B
Adresse I2C : 0x48
cd ~/WORKSPACE_ZEPHYR
mkdir -p applications/app_lm75/src
cd app_lm75Ajouter les 4 fichiers suivants :
CMakeLists.txt
cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(lm75_uart)
target_sources(app PRIVATE src/main.c) prj.conf
CONFIG_I2C=y
CONFIG_SENSOR=y
CONFIG_LM75=y
CONFIG_PRINTK=y
CONFIG_CBPRINTF_FP_SUPPORT=y app.overlay
&i2c1 {
status = "okay";
clock-frequency = <I2C_BITRATE_STANDARD>;
lm75b: lm75@48 {
compatible = "nxp,lm75b", "lm75";
reg = <0x48>;
status = "okay";
};
}; /src/main.c
#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/drivers/sensor.h>
#include <zephyr/sys/printk.h>
int main(void)
{
const struct device *const dev = DEVICE_DT_GET(DT_NODELABEL(lm75b));
struct sensor_value temp;
if (!device_is_ready(dev)) {
printk("Erreur : le capteur LM75B n'est pas pret (verifiez le cablage I2C).\n");
return 0;
}
printk("Capteur LM75B initialise. Lecture en cours...\n");
while (1) {
if (sensor_sample_fetch(dev) < 0) {
printk("Erreur de lecture I2C sur le LM75B\n");
} else {
sensor_channel_get(dev, SENSOR_CHAN_AMBIENT_TEMP, &temp);
printk("Temperature : %.2f C\n", sensor_value_to_double(&temp));
}
k_msleep(1000);
}
return 0;
}tree applications/app_lm75/ -L 2
applications/app_lm75/
├── app.overlay
├── build
│ ├── app
│ ├── build_info.yml
│ ├── build.ninja
│ ├── CMakeCache.txt
│ ├── CMakeFiles
│ ├── cmake_install.cmake
│ ├── compile_commands.json
│ ├── Kconfig
│ ├── modules
│ ├── sysbuild_modules.txt
│ ├── zephyr
│ ├── zephyr_modules.txt
│ └── zephyr_settings.txt
├── CMakeLists.txt
├── prj.conf
└── src
└── main.cwest build -p auto -b nucleo_f411re applications/app_lm75/ -d build/app_lm75
west flash -d build/app_lm75 --runner openocd --no-resetminicom -D /dev/ttyACM0
Welcome to minicom 2.8
OPTIONS: I18n
Port /dev/ttyACM0, 13:04:44
Press CTRL-A Z for help on special keys
*** Booting Zephyr OS build v4.5.0-6146-gdf7ac29b3886 ***
Capteur LM75B initialise. Lecture en cours...
Temperature : 24.00 C
Temperature : 24.00 C
Temperature : 23.88 C
Temperature : 23.75 C
Temperature : 23.75 C
Temperature : 23.88 C
Temperature : 23.88 CVSCode
Installations des extensions VSCode
code --install-extension ms-vscode.cpptools
code --install-extension ms-vscode.cmake-tools
code --install-extension marus25.cortex-debug
code --install-extension Ac6.zephyr-workbench # https://marketplace.visualstudio.com/items?itemName=Ac6.zephyr-workbench&utm_source=chatgpt.comVariables d’Environnement
Ajouter dans ~/.bashrc :
# Zephyr SDK
export ZEPHYR_SDK_INSTALL_DIR=$HOME/zephyr-sdk-1.0.1
# Zephyr toolchain
export PATH=$ZEPHYR_SDK_INSTALL_DIR/gnu/arm-zephyr-eabi/bin:$PATHsource ~/.bashrc Système de Fichiers Zephyr
WORKSPACE_ZEPHYR/
│
├── .venv/ # Environnement Python
├── zephyr/ # Sources Zephyr
├── modules/ # Modules Zephyr
├── bootloader/
├── tools/
│
└── applications/
├── blink/
│ ├── CMakeLists.txt
│ ├── prj.conf
│ └── src/
│ └── main.c
│
└── uart_test/
├── CMakeLists.txt
├── prj.conf
└── src/
└── main.ccd ~/WORKSPACE_ZEPHYR
mkdir -p applications/blink/src
mkdir -p .vscode Fichiers Applications
Dans applications/blink, ajouter les 3 fichiers suivants :
CMakeLists.txt
cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(blink)
target_sources(app PRIVATE src/main.c) prj.conf
CONFIG_GPIO=y
CONFIG_SERIAL=y
CONFIG_CONSOLE=y
CONFIG_UART_CONSOLE=y
CONFIG_LOG=y /src/main.c
#include <zephyr/kernel.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(blink, LOG_LEVEL_INF);
#define SLEEP_TIME_MS 500
#define LED0_NODE DT_ALIAS(led0)
#if !DT_NODE_HAS_STATUS(LED0_NODE, okay)
#error "Unsupported board: led0 devicetree alias is not defined"
#endif
static const struct gpio_dt_spec led =
GPIO_DT_SPEC_GET(LED0_NODE, gpios);
int main(void)
{
int ret;
bool led_state = false;
if (!gpio_is_ready_dt(&led)) {
LOG_ERR("LED device not ready");
return 0;
}
ret = gpio_pin_configure_dt(&led, GPIO_OUTPUT_INACTIVE);
if (ret < 0) {
LOG_ERR("Cannot configure LED");
return 0;
}
LOG_INF("Starting LED blinking");
while (1) {
ret = gpio_pin_toggle_dt(&led);
if (ret == 0) {
led_state = !led_state;
LOG_INF("LED state: %d", led_state);
}
k_msleep(SLEEP_TIME_MS);
}
return 0;
}Fichiers Configuration vscode
Dans .vscode, ajouter les 3 fichiers suivants :
nano .vscode/tasks.json .vscode/task.json
{
"version": "2.0.0",
"tasks": [
{
"label": "Zephyr: build",
"type": "shell",
"command": "bash",
"args": [
"-c",
"source .venv/bin/activate && west build -b nucleo_f411re applications/blink -d build/blink -- -DCMAKE_EXPORT_COMPILE_COMMANDS=ON"
],
"options": {
"cwd": "${workspaceFolder}"
},
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": []
},
{
"label": "Zephyr: pristine build",
"type": "shell",
"command": "bash",
"args": [
"-c",
"source .venv/bin/activate && west build -p always -b nucleo_f411re applications/blink -d build/blink -- -DCMAKE_EXPORT_COMPILE_COMMANDS=ON"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": []
},
{
"label": "Zephyr: flash",
"type": "shell",
"command": "bash",
"args": [
"-c",
"source .venv/bin/activate && west flash -d build/blink --runner openocd --openocd ~/zephyr-sdk-1.0.1/hosttools/sysroots/x86_64-pokysdk-linux/usr/bin/openocd"
],
"options": {
"cwd": "${workspaceFolder}"
},
"dependsOn": "Zephyr: build",
"problemMatcher": []
}
]
}nano .vscode/settings.json .vscode/settings.json
{
"C_Cpp.default.compileCommands": "${workspaceFolder}/build/blink/compile_commands.json",
"C_Cpp.default.intelliSenseMode": "linux-gcc-arm",
"files.associations": {
"*.overlay": "dts",
"*.dtsi": "dts",
"*.conf": "kconfig"
},
"cmake.configureOnOpen": false,
"terminal.integrated.defaultProfile.linux": "bash"
} .vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Zephyr STM32F411RE - Debug",
"type": "cortex-debug",
"request": "launch",
"servertype": "openocd",
"serverpath": "~/zephyr-sdk-1.0.1/hosttools/sysroots/x86_64-pokysdk-linux/usr/bin/openocd",
"gdbPath": "~/zephyr-sdk-1.0.1/gnu/arm-zephyr-eabi/bin/arm-zephyr-eabi-gdb",
"toolchainPrefix": "arm-zephyr-eabi",
"device": "STM32F411RETx",
"executable": "${workspaceFolder}/build/blink/zephyr/zephyr.elf",
"searchDir": [
"~/zephyr-sdk-1.0.1/hosttools/sysroots/x86_64-pokysdk-linux/usr/share/openocd/scripts"
],
"configFiles": [
"interface/stlink.cfg",
"target/stm32f4x.cfg"
],
"serverArgs": [
"-c",
"adapter speed 100",
"-c",
"reset_config srst_only srst_nogate connect_assert_srst"
],
"runToEntryPoint": "main",
"preLaunchTask": "Zephyr: build",
"showDevDebugOutput": "raw"
}
]
}Test du Projet
Lancer VSCode
cd ~/WORKSPACE_ZEPHYR
code .- Ouvrir applications/blink/src/main.c.
- BUILD: Faire Ctrl + Shift + B.
- Sélectionner Zephyr: build.
- Brancher la Nucleo-F411RE en USB.
Flash du programme sans Debug
- Ouvrir la palette de commandes (Ctrl + Shift + P).
- Sélectionner Tasks: Run Task puis Zephyr: flash.
- Appuyer sur le reset de la carte
Debug
- Appuyer sur F5 pour démarrer le debugger.