1 min
0

Thiết lập dự án ESP32 với PlatformIO và Nix

Ghi chú về cách thiết lập dự án ESP32 với PlatformIO và Nix

  • #nix
  • #esp32
  • #platformio
  • #embedded

Khởi tạo dự án với Nix

terminal
mkdir -p ~/test/esp32cd ~/test/esp32nix flake init

Thiết lập môi trường cho Nix

  • Chỉnh sửa file `flake.nix` trong dự án. Đây là ví dụ tôi dùng cho dự án của mình.
nixNIX
1{2  description = "ESP32 + PlatformIO devshell";3  inputs = {4    nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";5  };67  outputs = {8    self,9    nixpkgs,10  }: let11    system = "x86_64-linux";12    pkgs = import nixpkgs {inherit system;};13  in {14    devShells.${system}.default = pkgs.mkShell {15      packages = with pkgs; [16        platformio17        python318        gcc19        gnumake20        pkg-config21        picocom22        clang-tools # this for my setup lsp and formatter23      ];2425      shellHook = ''26        if [ -z "$ZSH_VERSION" ]; then27          export SHELL=${pkgs.zsh}/bin/zsh28          exec ${pkgs.zsh}/bin/zsh -l29        fi30      '';31    };32  };33}
  • Chạy lệnh `nix develop` trong thư mục dự án.

Khởi tạo dự án với PlatformIO

  • Với framework `arduino`:
terminal
pio project init --board esp32dev --project-option "framework=arduino"
  • Với framework `espidf`:
terminal
pio project init --board esp32dev --project-option "framework=espidf"

Biên dịch dự án

terminal
pio run -t compiledb

Thiết lập clangd để giảm lỗi từ LSP

  • Tạo file `.clangd` ở thư mục gốc dự án.
TXT
1CompileFlags:2  Add:3    - "-ferror-limit=0"4    - "-Wno-unknown-attributes"5    - "-Wno-ignored-attributes"6  Remove:7    # Xtensa-specific flags unsupported by clang8    - "-mlongcalls"9    - "-mtext-section-literals"10    - "-fstrict-volatile-bitfields"11    - "-fno-tree-switch-conversion"12    - "-fno-jump-tables"13    # ESP-IDF specific14    - "-fno-rtti"15    - "-fno-exceptions"16    - "-mfix-esp32-psram-cache-issue"17    - "-mfix-esp32-psram-cache-strategy=*"18    # GCC-specific warnings clang doesn't know19    - "-Wno-frame-address"20    - "-Wno-error=unused-but-set-variable"21    - "-freorder-blocks"22  Compiler: clang++23Diagnostics:24  Suppress:25    # Missing Arduino/ESP headers (framework quirks)26    - pp_file_not_found27    # Arduino Print class overload resolution28    - ovl_no_viable_member_function_in_call29    - ovl_no_viable_function_in_call30    # Template issues with Arduino libraries31    - typecheck_invalid_operands32    - typecheck_nonviable_condition33    # ESP-IDF macro expansions34    - builtin_requires_header35    - implicit_function_decl36  UnusedIncludes: None37  MissingIncludes: None

Một số lệnh khi dùng PlatformIO

  • Chạy / tải lên / giám sát dự án.
terminal
pio run # run projectpio run -t upload # upload projectpio device monitor # monitor device
  • Cài thư viện cho dự án. Bạn có thể cài bằng lệnh hoặc khai báo trong `platformio.ini` sau khi dùng `pio lib install`.
terminal
pio lib install <LIB_NAME>
INI
1[env:esp32dev]2platform = espressif323board = esp32dev4framework = arduino5monitor_speed = 11520067lib_deps =8  adafruit/Adafruit SSD13069  adafruit/Adafruit GFX Library

Tài liệu tham khảo

>Newsletter

Stay Updated

Get notified when I publish new articles, tutorials, and project updates. Subscribe for insights and actionable content.