Kconfig

Kconfig 是一種用於配置系統的語言,常見於 Linux kernel 和 ESP-IDF 等嵌入式開發框架中。

Format

📘 NOTE
同一個 menuchoice 區塊內的所有 config 名稱,應該要有一個共同的 prefix,長度至少要 3 個字母。
若有嵌套關係,不要求 prefix 疊加,但疊加是好習慣。

Keywords

📁 menu

menu "Example Configuration"
    ...
endmenu
  • 定義一個 選單
  • 名稱提示:上例為 "Example Configuration"
  • 一個 Kconfig 內可以有多個 menu,且可嵌套。

🔀 choice / endchoice

choice BLINK_LED
    prompt "Blink LED type"
    default BLINK_LED_GPIO
    ...
endchoice
  • 定義一組 互斥選項(只能選一個)。
  • 一個 choice 由多個 config 組成。
  • prompt:名稱提示。
  • default:預設選項。

⚙️ config

config BLINK_LED_GPIO
    bool "GPIO"
  • 定義一個 設定選項
  • 名稱提示:上例為 "GPIO"
  • 型別:可為 boolintstring
  • 常見屬性
    屬性 說明
    bool 布林選項(開/關)
    int 整數輸入
    string 字串輸入
    prompt 選單顯示文字
    default 預設值(可搭配條件)
    range 限制輸入範圍(僅適用於 int
    depends on 設定條件依賴(僅在條件成立時顯示)
    help 說明文字,會顯示在說明欄位中

🧩 orsource

orsource "$IDF_PATH/examples/common_components/env_caps/$IDF_TARGET/Kconfig.env_caps"
  • 導入其他 Kconfig 檔案內容。
  • 常用於根據目標板子載入不同的配置。

Options

🆘 help

config BLINK_GPIO
    int "Blink GPIO number"
    help
        GPIO number (IOxx) to blink on and off the LED.
  • 提供使用者幫助文檔

🧷 depends on

choice BLINK_LED_STRIP_BACKEND
    depends on BLINK_LED_STRIP
    ...
endchoice
  • 只有當某個 config 被選中時,這個 choice 才會出現。

🧪 if

default BLINK_LED_STRIP_BACKEND_RMT if SOC_RMT_SUPPORTED
  • 條件成立時才會使用。

Example

🍔 漢堡店菜單

menu "Burger Menu"

    menu "Main Course"

        choice BURGER_TYPE
            prompt "Choose your burger type"
            default BURGER_TYPE_CLASSIC
            help
                Please choose your preferred type of burger.

            config BURGER_TYPE_CLASSIC
                bool "Classic Beef Burger"

            config BURGER_TYPE_CHICKEN
                bool "Crispy Chicken Burger"

            config BURGER_TYPE_VEGGIE
                bool "Garden Veggie Burger"
        endchoice

        config BURGER_EXTRA_CHEESE
            bool "Add extra cheese"
            default n
            help
                Add a slice of extra cheese to your burger.

        config BURGER_ADD_BACON
            bool "Add bacon"
            default n
            depends on !BURGER_TYPE_VEGGIE
            help
                Add crispy bacon strips to your burger (Not available for veggie burgers).

        config BURGER_SPICY
            bool "Add hot sauce"
            default n
            depends on BURGER_TYPE_CHICKEN || BURGER_TYPE_CLASSIC
            help
                Only available for meat-based burgers.

    endmenu

    menu "Combo Options"

        config BURGER_SET_COMBO
            bool "Upgrade to combo"
            default y
            help
                Upgrade your meal to a combo with drink and fries.

        menu "Drink Selection"
            depends on BURGER_SET_COMBO

            choice BURGER_DRINK_TYPE
                prompt "Choose your drink"
                default BURGER_DRINK_COKE
                help
                    Choose a drink for your combo meal.

                config BURGER_DRINK_COKE
                    bool "Coke"

                config BURGER_DRINK_TEA
                    bool "Unsweetened Green Tea"

                config BURGER_DRINK_JUICE
                    bool "Orange Juice"
            endchoice
        endmenu

        config BURGER_INCLUDE_KETCHUP
            bool "Include ketchup packets"
            default y
            depends on BURGER_SET_COMBO

        config BURGER_INCLUDE_FRIES
            bool "Include fries"
            default y
            depends on BURGER_SET_COMBO

    endmenu

endmenu