前言
项目创建


自定义module(以say_hello为例)

module.yml文件
yaml
name: say_hello
build:
cmake: .
kconfig: Kconfig
CMakeLists.txt文件
c
if(CONFIG_SAY_HELLO)
zephyr_include_directories(${CMAKE_CURRENT_LIST_DIR}/)
zephyr_library_sources(${CMAKE_CURRENT_LIST_DIR}/say_hello.c)
endif ()
Kconfig文件(用于条件编译配置的宏定义)
c
# create a menu
config SAY_HELLO
bool "this is a test"
default n # 默认值
depends on PRINTK # 依赖项
help
this is a help message
源文件和头文件
- 头文件
c
/**
******************************************************************************
* @file : say_hello.h
* @author : shchl
* @brief : None
* @attention : None
* @date : 2026/2/2
******************************************************************************
*/
#ifndef INC_01_KCONFIG_LEARN_SAY_HELLO_H
#define INC_01_KCONFIG_LEARN_SAY_HELLO_H
void say_hello(void);
#endif //INC_01_KCONFIG_LEARN_SAY_HELLO_H
- 源文件
c
/**
******************************************************************************
* @file : say_hello.c
* @author : shchl
* @brief : None
* @attention : None
* @date : 2026/2/2
******************************************************************************
*/
#include <zephyr/kernel.h>
#include "say_hello.h"
void say_hello(void)
{
printk("hello word\n");
}
项目中引用
prj.conf文件配置
c
# nothing here
CONFIG_LOG=y
CONFIG_PRINTK=y
# 启用模块
CONFIG_SAY_HELLO=y
main函数中调用
c
/*
* Copyright (c) 2012-2014 Wind River Systems, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/kernel.h>
#include "autoconf.h"
#include <stdio.h>
#include "say_hello.h"
int main(void)
{
printk("Hello World! %s\n", CONFIG_BOARD_TARGET);
say_hello(); // 调用模块api
return 0;
}
编译构建测试
