Setup vim as ESP-IDF editor
Greetings. This blog is for those who want to use Vim to program ESP32 instead of Visual Studio Code. Recently, I have some project that require high speed computing on a microcontroller. After some online researches and considerations to my budget, I decided to use ESP32 board. Not only did I have several of these boards lying around in my drawer but their impressive 240MHz dual-core cpu and built-in WiFi feqtures. Therefore, I began to setup an environment for ESP32 programming and realize it is a disaster.
Installing ESP-IDF
Firstly, clone the ESP-IDF github repo into your preferred directory. This process is straightforward and well-documented, thanks to the open-source nature of the project. In my case, I use the following command and took a quick coffee break while the installation completed:
mkdir esp
cd esp
git clone –recursive https://github.com/espressif/esp-idf.git
cd esp-idf
./install.sh
However, every time I open a new terminal, I’ll need to import the environment manually, which can become troublesome. To automate this process, I made some modifications to the .zshrc file
and of course, for those who are using other shell, just use your coresponding .rc file
. For the modification, just add the following line:
alias idf=’. $HOME/esp/esp-idf/export.sh’
After that, just type idf
in the terminal. This enables we to use ESP-IDF easily.
Setting Neovim
After the installation, let’s setup the neovim. Personally, I am using the NvChad which make neovim into a fully-featured IDE. To get started, I generate a project using the following command.
idf.py create-project hello-world
idf.py set-target esp32
Then, open the directory in neovim. As we can see, the build system is CMake and a main.c
file is prepared for us. I tried to write a hello world code.
As expected, the code autocompletion is broken and warnings are popping up. Initially, I tried ignoring them and building the project with idf.py build
. As it turn out, compilation is not success but I get some new files.
Then, I attemplated to restore the Language Server Protocol (LSP) by adding the following option in CMakeLists.txt. This enabled CMake to generate compile_command.json and allows LSP to function properly.
However, the things seemed to get worsed. Warnings had become errors and even the code formatting was broken. I quickly realized that the project was cross-compiled, meaning that the native x86 clangd may not able to adapt the arguments.
To address this issue, I use an alternative lazy mathod which is just add .clangd file to ignore the unsupported argument.
1 |
|
To my surprise, everythings seemed to work fine and the code autocompletion is working.
Encounter of new problem
Of course, if this problem can be easily solved, I wouldn’t have to make a dedicated blog for it. Unfortunately, after a computer reboot, the error is back with some changes. It keep saying machine/endian.h
is not found. Upon investigation, I realized it is due to the sysroot directory is misconfigured. My native clangd use endian.h
, and the ESP32 cross-compiler relied on machine/endian.h
. To fix this, I first download the ESP32 llvm clang from the github and extract it. Then, get into the directory and find the sysroot directory in it. However, the clangd will access the directory with usr
appended at the end. Hence, we need to make a directory named usr
and copy all the file into it.
Then, just put the directory that contain usr
into the clangd. To do it, I add the --sysroot=[directory]
option to do it.
After restarting Neovim, I encountered another strange error. However, I took this as a sign of progress since it indicated that I was getting closer to solving the problem. As it turned out, the issue is related to the wrong clangd being used. Since I already download the ESP32 llvm, I just simply added the path of the new clangd into the configuration of lspconfig.
Finally, restart the neovim again and everything works prefectly.