ESP-IDF: Difference between revisions

imported>Bendlas
m mention vscode in comment for tutorial link
imported>Mirrexagon
Updated toolchain derivation to latest as of 2021-11-03, overhauled shell.nix to create a Python venv
Line 17: Line 17:


stdenv.mkDerivation rec {
stdenv.mkDerivation rec {
   name = "esp32-toolchain";
   pname = "esp32-toolchain";
   version = "1.22.0";
   version = "2021r2";


   src = fetchurl {
   src = fetchurl {
     url = "https://dl.espressif.com/dl/xtensa-esp32-elf-linux64-1.22.0-80-g6c4433a-5.2.0.tar.gz";
     url = "https://github.com/espressif/crosstool-NG/releases/download/esp-${version}/xtensa-esp32-elf-gcc8_4_0-esp-${version}-linux-amd64.tar.gz";
     sha256 = "0mji8jq1dg198z8bl50i0hs3drdqa446kvf6xpjx9ha63lanrs9z";
     hash = "sha256-PrPWiyf6a6Wvb4jaIcuPrOm+AJTaqolgeTz+Vwq3hf8=";
   };
   };


Line 52: Line 52:


== Setting up ESP-IDF and the development shell ==
== Setting up ESP-IDF and the development shell ==
Clone the [https://github.com/espressif/esp-idf Espressif/esp-idf] repository:
Clone the [https://github.com/espressif/esp-idf espressif/esp-idf] repository:
<syntaxhighlight lang="sh">
<syntaxhighlight lang="sh">
cd ~/esp
cd ~/esp
Line 58: Line 58:
</syntaxhighlight>
</syntaxhighlight>


Now that we have ESP-IDF in place, it's time to set up the nix-shell environment with all the dependencies we need:
Now that we have ESP-IDF in place, it's time to set up the <code>nix-shell</code> environment with all the dependencies we need.
 
Note that this environment uses a Python virtual environment and pip to get all the necessary Python dependencies, which is easier to keep up to date than using Python packages from Nix (at the cost of some reproducibility). The virtual environment is created if it doesn't already exist. When updating ESP-IDF, delete the <code>.python_env</code> directory and re-run <code>nix-shell</code>.


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
{ nixpkgs ? import <nixpkgs> {} }:
{ pkgs ? import <nixpkgs> {} }:
 
pkgs.mkShell {
  name = "esp-idf-env";
 
  buildInputs = with pkgs; [
    (pkgs.callPackage ./esp32-toolchain.nix {})
 
    git
    wget
    gnumake
 
    flex
    bison
    gperf
    pkgconfig


let
    cmake
  inherit (nixpkgs) pkgs;


  # esp-idf as of Nov 2019 requires pyparsing < 2.4
     ncurses5
  python2 = let
     packageOverrides = self: super: {
            pyparsing = super.pyparsing.overridePythonAttrs( old: rec {
                version = "2.3.1";
                src = super.fetchPypi {
                    pname="pyparsing";
                    inherit version;
                    sha256="66c9268862641abcac4a96ba74506e594c884e3f57690a696d21ad8210ed667a";
                };
        });
    };
    in pkgs.python2.override{inherit packageOverrides; self= python2;};
in


pkgs.stdenv.mkDerivation {
    ninja
  name = "esp-idf-env";
 
  src = ./esp-idf;
     (python3.withPackages (p: with p; [
  dontBuild = true;
      pip
  dontConfigure = true;
      virtualenv
  buildInputs = with pkgs; [
    ]))
    gawk gperf gettext automake bison flex texinfo help2man libtool autoconf ncurses5 cmake glibcLocales
     (python2.withPackages (ppkgs: with ppkgs; [ pyserial future cryptography setuptools pyelftools pyparsing click ]))
    (pkgs.callPackage ~/esp/esp32-toolchain.nix {})
   ];
   ];
   shellHook = ''
   shellHook = ''
     export NIX_CFLAGS_LINK=-lncurses
     export IDF_PATH=$(pwd)/esp-idf
     export IDF_PATH=$HOME/esp/esp-idf
     export PATH=$IDF_PATH/tools:$PATH
     export IDF_TOOLS_PATH=$HOME/esp/esp-idf/tools
     export IDF_PYTHON_ENV_PATH=$(pwd)/.python_env
     export PATH="$IDF_TOOLS_PATH:$PATH"
 
    if [ ! -e $IDF_PYTHON_ENV_PATH ]; then
      python -m venv $IDF_PYTHON_ENV_PATH
      . $IDF_PYTHON_ENV_PATH/bin/activate
      pip install -r $IDF_PATH/requirements.txt
     else
      . $IDF_PYTHON_ENV_PATH/bin/activate
    fi
   '';
   '';
}
}
</syntaxhighlight>
</syntaxhighlight>


Save this as <code>~/esp/default.nix</code>.
Save this as <code>~/esp/shell.nix</code>.


You can now enter the development shell with the ESP32 toolchain and dependencies of ESP-IDF:
You can now enter the development shell with the ESP32 toolchain and dependencies of ESP-IDF:
<syntaxhighlight lang="sh">
<syntaxhighlight lang="sh">
cd ~/esp
cd ~/esp
nix-shell .
nix-shell
</syntaxhighlight>
</syntaxhighlight>


Line 113: Line 121:
== See also ==
== See also ==


* [https://github.com/bgamari/esp32.nix esp32.nix] provides nix expression for building the exp32 sdk as well as micropython.
* [https://github.com/bgamari/esp32.nix esp32.nix] provides nix expression for building the esp32 sdk as well as micropython.
* [https://github.com/taktoa/esp32-baremetal esp32-baremetal] has an example how to build esp32 firmware without relying on an sdk.
* [https://github.com/taktoa/esp32-baremetal esp32-baremetal] has an example how to build esp32 firmware without relying on an sdk.
* [https://specific.solutions.limited/projects/hanging-plotter/electronics tutorial] for setting up the prebuilt toolchain with vscode
* [https://specific.solutions.limited/projects/hanging-plotter/electronics tutorial] for setting up the prebuilt toolchain with vscode


[[Category:Guide]]
[[Category:Guide]]