准备工作
首先需要在ubuntu中安装好docker
搭建环境
构建docker容器
esp32搭建环境时需要使用到python3, 所以我使用的镜像是ubuntu20.04。
docker run -itd -v /mnt/hgfs/:/mnt/hgfs/ -p 30024:22 --device /dev/ttyUSB0 --name esp32 moqi/ubuntu/2004:later /usr/sbin/sshd -D
处理好容器以后就根据我之前写的更新下载源来更新我们系统的下载源。后面的指令都是在docker中执行的。
安装需要的软件
sudo apt-get -y update && sudo apt-get -y upgrade
sudo apt-get install -y vim git wget flex bison gperf python3 python3-pip python3-setuptools cmake ninja-build ccache libffi-dev libssl-dev dfu-util libusb-1.0-0
设置Python3为Ubuntu默认Python版本
sudo rm -f /usr/bin/python
sudo ln -s /usr/bin/python3.8 /usr/bin/python
alias pip=pip3
PIP下载过慢
可以更换pip源来解决,先使用以下指令创建文件。
mkdir -p ~/.pip && touch ~/.pip/pip.conf
然后将以下内容加入到pip.conf文件中。
[global]
timeout = 6000
index-url = https://mirrors.aliyun.com/pypi/simple/
trusted-host = mirrors.aliyun.com
下载源码
如果没有翻墙的话,直接使用github的源码搭建环境会非常非常久,所以乐鑫官方出了个esp-gitee-tools工具,用这个工具搭建环境会快很多。
获取源码
mkdir -p ~/sources && cd ~/sources
git clone https://gitee.com/EspressifSystems/esp-gitee-tools.git
git clone https://gitee.com/EspressifSystems/esp-idf.git
下载ESP-IDF下的子模块
首先切换到ESP-IDF下,然后利用esp-gitee-tools的脚本去更新子模块
V4.4
cd ~/sources/esp-idf/
git checkout release/v4.4
~/sources/esp-gitee-tools/submodule-update.sh
V5.1
sudo apt-get install -y python3.8-venv
cd ~/sources/esp-idf/
git checkout release/v5.1
~/sources/esp-gitee-tools/submodule-update.sh
安装交叉编译链
cd ~/sources/esp-idf/
~/sources/esp-gitee-tools/install.sh
更新环境变量
source /home/moqi/sources/esp-idf/export.sh
每次登录机器前都需要更新一次环境变量,如果希望下次自动更新可以输入以下指令。输入完该指令,下次打开命令行时会自动更新环境变量
echo "source /home/moqi/sources/esp-idf/export.sh" >> ~/.bashrc
测试
首先创建一个文件夹存放例程源码
mkdir /home/moqi/project/
将源码复制到该文件夹下
cp -rf /home/moqi/sources/esp-idf/examples/get-started/hello_world/ /home/moqi/project/
切换到例程文件夹内,编译例程,如果可以正常编译,则证明搭建成功。
cd /home/moqi/project/hello_world/
idf.py set-target esp32c3
idf.py all
没有访问串口的权限
下载时可能会提示没有权限,可以执行以下指令获得权限,执行完以后需要重启容器。
sudo usermod -a -G dialout $USER