想要实现在nxp的板子上实现Zephyr 开发,还得使用 MCUXpresso for VS Code。最好有加速工具,不然访问GitHub会失败或加载很慢。因为是重新使用 MCUXpresso for VS Code,之前可能下载过了一些资源,导致现在重新下载没有出现异常,奇怪的时候都删除了相关扩展,VS Code也试过卸载,vscode有关的文件也删除,但依旧没能删除干净。如之前没有west的,就会报错误 'west' tool could not be found.
需要 git,python,cmake,ninja,west
安装VS Code就不过多介绍了,那就开始在VS Code上搭建 Zephyr 开发环境。官方也提供了指导步骤:
https://docs.mcuxpresso.nxp.com/mcux-vscode/25.12//html/Zephyr-Lab-Installation-and-Preparation.html
并不需要下载MCUXpresso SDK,就下载Zephyr就可以了。
1,在扩展中搜索MCUXpresso for VS Code,并下载该扩展。
2,在展开的 快速启动面板 中,点击 "Open MCUXpresso Installer",下载组件
Zephyr Developer
LinkServer


3,导入 Zephyr 源代码仓库

之后需要大概两个小时时间才下完,下了3GB左右,解压之后8G,之后报错了,之后说是SSL证书问题,没能解决,但似乎也不影响
WARNING: An error occurred: HTTPSConnectionPool(host='github.com', port=443): Max retries exceeded with url: /NXP/wifi_nb_fw/raw/nxp-v4.3.5/IW610/sduart_iw610.bin.se (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1010)')))
ERROR: Failed to fetch blob: No URL worked for this blob
FATAL ERROR: 25 blobs failed to be fetched
之后可以在 IMPORTED REPOSITORIES 查看导入的仓库

4,开始尝试使用示例"hello_world"
在 快速启动面板 中,点击 "Import Example from Repository"

开始build


点击debug之后,出现一个 “Retrieve SVD file from MCUXpresso SoC SVD repository or load from file system?”,点击下载。
SVD (System View Description) 文件是芯片的“外设地图” 。有了它,在调试时就能在“外设 (Peripherals)”视图里,以结构化的方式查看每个外设(如 UART、GPIO、ADC)寄存器的实时值和每个比特位的含义,而不是面对一堆难以理解的内存地址 。如果选择不下载,依然可以正常调试、打断点、运行程序。只是当需要查看某个外设寄存器的状态时,会非常不便,需要手动翻阅参考手册去核对每个内存地址对应的值。 deepseek是这么说的。

5,那么接下来尝试点亮蓝灯 PTC1


在 Zephyr 中操作硬件(比如控制 LED、读取按键)的正确方式,就是使用设备树,可以打开开发板的设备树文件查看,按下 Ctrl+P 打开命令面板,输入 frdm_mcxw71.dts ,打开文件后,查看设备树文件中定义蓝灯的信息。
/* 定义别名,方便代码引用 */
aliases {
led0 = &blue_led;// 把 "led0" 这个名字指向 blue_led
sw0 = &user_button_0;
blue-pwm-led = &blue_pwm_led;
green-pwm-led = &green_pwm_led;
red-pwm-led = &red_pwm_led;
rtc = &counter_rtc;
};
// ...
user_led {
compatible = "gpio-leds";
blue_led: led {
gpios = <&gpioc 1 GPIO_ACTIVE_LOW>;// 连接在 GPIOC 的 Pin 1,低电平有效
};
};所以要操作要操作 led0 时,实际就是在操作 GPIOC 的 Pin 1,并且是低电平点亮。代码实现
/*
* Copyright (c) 2012-2014 Wind River Systems, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdio.h>
#include <zephyr/kernel.h>
#include <zephyr/drivers/gpio.h>
/* 使用设备树获取 LED0 的硬件信息 */
#define LED0_NODE DT_ALIAS(led0)
/* 通过设备树 API 获取 LED 的 gpio_dt_spec 结构体,
* 这个结构体包含了 LED 的端口、引脚号和标志等所有信息。
*/
static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(LED0_NODE, gpios);
int main(void)
{
int ret;
bool led_state = true; /* 用于记录LED状态,true=点亮,false=熄灭 */
printf("Hello World! %s\n", CONFIG_BOARD_TARGET);
/* 1. 检查 LED 是否在设备树中被正确定义 */
if (!device_is_ready(led.port)) {
printk("错误: LED 设备 %s 未就绪\n", led.port->name);
return 1;
}
/* 2. 配置 LED 引脚为输出模式,初始状态设为非活动电平(熄灭) */
ret = gpio_pin_configure_dt(&led, GPIO_OUTPUT_INACTIVE);
if (ret < 0) {
printk("错误: 无法配置 LED 引脚 (错误码 %d)\n", ret);
return 1;
}
/* 3. 主循环:让 LED 以 1 秒为周期闪烁 */
while (1) {
/* 设置LED状态: led_state为true时调用gpio_pin_set_dt将引脚置为活动电平(点亮) */
ret = gpio_pin_set_dt(&led, (int)led_state);
if (ret < 0)
{
printk("错误: 无法设置 LED 状态 (错误码 %d)\n", ret);
return 1;
}
/* 翻转LED状态,为下一次循环做准备 */
led_state = !led_state;
/* 延时 1 秒,产生闪烁效果 */
k_msleep(1000);
}
}之后的效果为

我要赚赏金
