esp32笔记[1]-blink闪灯

硬件平台

开发板:ESP32 DEVKIT V1
主控模块:ESP WROOM-32

获取例程

git clone -b v4.4 --recursive https://gitee.com/EspressifSystems/esp-idf.git

例程在esp-idf/examples/get-started/blink
修改例程中的IO口,要不然灯是不会闪的
blink_example_main.c

/* Blink Example

   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "led_strip.h"
#include "sdkconfig.h"

static const char *TAG = "example";

/* Use project configuration menu (idf.py menuconfig) to choose the GPIO to blink,
   or you can edit the following line and set a number here.
*/
//#define BLINK_GPIO CONFIG_BLINK_GPIO
#define BLINK_GPIO 2
static uint8_t s_led_state = 0;

#ifdef CONFIG_BLINK_LED_RMT
static led_strip_t *pStrip_a;

static void blink_led(void)
{
    /* If the addressable LED is enabled */
    if (s_led_state) {
        /* Set the LED pixel using RGB from 0 (0%) to 255 (100%) for each color */
        pStrip_a->set_pixel(pStrip_a, 0, 16, 16, 16);
        /* Refresh the strip to send data */
        pStrip_a->refresh(pStrip_a, 100);
    } else {
        /* Set all LED off to clear all pixels */
        pStrip_a->clear(pStrip_a, 50);
    }
}

static void configure_led(void)
{
    ESP_LOGI(TAG, "Example configured to blink addressable LED!");
    /* LED strip initialization with the GPIO and pixels number*/
    pStrip_a = led_strip_init(CONFIG_BLINK_LED_RMT_CHANNEL, BLINK_GPIO, 1);
    /* Set all LED off to clear all pixels */
    pStrip_a->clear(pStrip_a, 50);
}

#elif CONFIG_BLINK_LED_GPIO

static void blink_led(void)
{
    /* Set the GPIO level according to the state (LOW or HIGH)*/
    gpio_set_level(BLINK_GPIO, s_led_state);
}

static void configure_led(void)
{
    ESP_LOGI(TAG, "Example configured to blink GPIO LED!");
    gpio_reset_pin(BLINK_GPIO);
    /* Set the GPIO as a push/pull output */
    gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
}

#endif

void app_main(void)
{

    /* Configure the peripheral according to the LED type */
    configure_led();

    while (1) {
        ESP_LOGI(TAG, "Turning the LED %s!", s_led_state == true ? "ON" : "OFF");
        blink_led();
        /* Toggle the LED state */
        s_led_state = !s_led_state;
        vTaskDelay(CONFIG_BLINK_PERIOD / portTICK_PERIOD_MS);
    }
}

macOS搭建ESP-IDF环境(docker)

[https://blog.csdn.net/skb666/article/details/122007521]

安装编译环境

#获取docker镜像
docker pull espressif/idf:v4.4
#简化指令
alias esp-idf='docker run --rm --privileged -v $PWD:/project -w /project -it espressif/idf:v4.4 bash -c'
#使用示例
#esp-idf "idf.py set-target esp32"
#编译blink
docker run --name=espidf --privileged -v $PWD:/project -w /project -d espressif/idf:v4.4 /bin/sh -c "while true; do sleep 1; done"
docker exec -it espidf bash
##进入容器
cd ./proj_blink/blink
source /opt/esp/idf/export.sh
idf.py set-target esp32
#idf.py menuconfig
idf.py build
exit



宿主机安装烧录工具

#安装esp烧入工具
python3 -m pip install esptool -i https://pypi.tuna.tsinghua.edu.cn/simple
#擦除原有程序(改成自己的串口号,可能要sudo)
ls /dev/cu*
esptool.py --chip esp32 --port /dev/cu.usbserial-0001 erase_flash
#烧入bin文件(注意匹配你的 bin文件名、串口号、flash大小与速率、传输波特率等)
cd ./proj_blink/blink/build
esptool.py --chip esp32 --port /dev/cu.usbserial-0001 -b 460800 --before=default_reset --after=hard_reset write_flash --flash_mode dio --flash_freq 40m --flash_size 2MB 0x1000 bootloader/bootloader.bin 0x10000 blink.bin 0x8000 partition_table/partition-table.bin


效果


posted @ 2023-01-27 03:10  qsBye  阅读(252)  评论(0编辑  收藏  举报