0_环境搭建ubuntu
安装必要软件
sudo apt-get install git wget flex bison gperf python3 python3-pip python3-setuptools cmake ninja-build ccache libffi-dev libssl-dev dfu-util libusb-1.0-0
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 1
sudo update-alternatives --config python
国内pip3源
pip3 config set global.index-url http://mirrors.aliyun.com/pypi/simple
pip3 config set global.trusted-host mirrors.aliyun.com
如果遇到串口访问权限问题
sudo usermod -a -G dialout $USER
sudo reboot
安装VScode
安装Espressif IDF插件
选择下载服务器
对于 Linux 用户,OpenOCD 需要在 /etc/udev/rules.d/ 中添加 60-openocd.rules 用于 USB 设备中的权限委托。
在具有 sudo 权限的终端中运行此命令:
sudo cp /home/jin/.espressif/tools/openocd-esp32/v0.11.0-esp32-20220411/openocd-esp32/share/openocd/contrib/60-openocd.rules /etc/udev/rules.d
后面过程看我的视频
开发板型号ESP32-S3-DevKitC-1
2_GPIO
2.1_LED闪烁
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "sdkconfig.h"
#define LED GPIO_NUM_5
void app_main() {
gpio_pad_select_gpio(LED);//选择一个GPIO
gpio_set_direction(LED, GPIO_MODE_OUTPUT);//把这个GPIO作为输出
while (1)
{
gpio_set_level(LED, 0);//把这个GPIO输出低电平
vTaskDelay(1000 / portTICK_PERIOD_MS);
gpio_set_level(LED, 1);//把这个GPIO输出高电平
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
2.2_按钮控制LED
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "sdkconfig.h"
#define LED GPIO_NUM_5
#define botton GPIO_NUM_0
void app_main() {
gpio_pad_select_gpio(LED);//选择一个GPIO
gpio_set_direction(LED, GPIO_MODE_OUTPUT);//把这个GPIO作为输出
gpio_pad_select_gpio(botton);//选择一个GPIO
gpio_set_direction(botton, GPIO_MODE_INPUT);//把这个GPIO作为输入
gpio_set_pull_mode(botton,GPIO_PULLUP_ONLY);//把这个GPIO上拉
while (1)
{
if(gpio_get_level(botton)){
gpio_set_level(LED, 1);//把这个GPIO输出高电平
}else{
gpio_set_level(LED, 0);//把这个GPIO输出低电平
}
}
}
2.3_外部中断
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "sdkconfig.h"
#define LED GPIO_NUM_5
#define botton GPIO_NUM_0
volatile bool state = 0;
static void IRAM_ATTR gpio_isr_handler(void* arg)
{
state = !state;
}
void app_main() {
gpio_pad_select_gpio(LED);//选择一个GPIO
gpio_set_direction(LED, GPIO_MODE_OUTPUT);//把这个GPIO作为输出
gpio_pad_select_gpio(botton);//选择一个GPIO
gpio_set_direction(botton, GPIO_MODE_INPUT);//把这个GPIO作为输入
gpio_set_pull_mode(botton,GPIO_PULLUP_ONLY);//把这个GPIO上拉
gpio_set_intr_type(botton,GPIO_INTR_NEGEDGE);//下降沿触发中断
gpio_install_isr_service(0);//中断标志
gpio_isr_handler_add(botton,gpio_isr_handler,(void *)botton);//设置中断处理函数
gpio_intr_enable(botton);//开启中断
while (1)
{
gpio_set_level(LED, state);
}
}