esp32-s2/main/main.c

103 lines
3.8 KiB
C
Raw Normal View History

2022-07-18 11:58:28 +02:00
/* SPDX-License-Identifier: GPL-3.0-or-later
2023-01-07 01:43:01 +01:00
* Copyright 2022-2023 King Kévin <kingkevin@cuvoodoo.info>
2022-07-18 11:58:28 +02:00
*/
2022-07-18 11:32:41 +02:00
#include <stdio.h>
2022-07-18 14:36:50 +02:00
#include <stdlib.h>
#include <sys/reent.h>
#include "esp_log.h"
2023-01-07 01:43:01 +01:00
#include "esp_mac.h"
2022-07-18 14:36:50 +02:00
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "tinyusb.h"
#include "tusb_cdc_acm.h"
#include "tusb_dfu_rt.h"
2022-07-18 14:36:50 +02:00
#include "tusb_console.h"
#include "sdkconfig.h"
2023-01-07 05:47:32 +01:00
#include "driver/gpio.h"
2023-01-07 06:11:53 +01:00
// GPIO to force DFU mode (on high)
#define DFU_PIN 14
2023-01-07 05:47:32 +01:00
// GPIO for on-board LED (WEMOS S2 mini, source on)
#define LED_BOARD 15
2022-07-18 14:36:50 +02:00
static const char *TAG = "main";
2022-07-18 11:32:41 +02:00
2023-01-07 01:43:01 +01:00
/**
* @brief USB string descriptor
*/
static const char* usb_str_desc[USB_STRING_DESCRIPTOR_ARRAY_SIZE] = {
(char[]){0x09, 0x04}, // 0: language: English
CONFIG_TINYUSB_DESC_MANUFACTURER_STRING, // 1: Manufacturer
CONFIG_TINYUSB_DESC_PRODUCT_STRING, // 2: Product
CONFIG_TINYUSB_DESC_SERIAL_STRING, // 3: Serials, should use chip ID
CONFIG_TINYUSB_DESC_CDC_STRING, // 4: CDC Interface
"", // 5: MSC Interface
"DFU (runtime mode)" // 6: DFU RT
};
2022-07-18 11:32:41 +02:00
void app_main(void)
{
2023-01-07 06:11:53 +01:00
// check DFU force
2023-01-07 05:47:32 +01:00
gpio_config_t io_conf = {}; // to configure GPIO
2023-03-13 19:35:06 +01:00
// GPIO0 can also be pressed while soft reboot
io_conf.pin_bit_mask = (1ULL << 0); // GPIO to configure
io_conf.intr_type = GPIO_INTR_DISABLE; // disable interrupt
io_conf.mode = GPIO_MODE_INPUT; // set as input
io_conf.pull_down_en = false; // disable pull-down mode
io_conf.pull_up_en = true; // enable pull-up mode
ESP_ERROR_CHECK( gpio_config(&io_conf) ); // configure GPIO
// dedicated DFU button
2023-01-07 06:11:53 +01:00
io_conf.pin_bit_mask = (1ULL << DFU_PIN); // GPIO to configure
io_conf.intr_type = GPIO_INTR_DISABLE; // disable interrupt
io_conf.mode = GPIO_MODE_INPUT; // set as input
io_conf.pull_down_en = 1; // enable pull-down mode
io_conf.pull_up_en = 0; // disable pull-up mode
ESP_ERROR_CHECK( gpio_config(&io_conf) ); // configure GPIO
2023-03-13 19:35:06 +01:00
if (!gpio_get_level(0) || gpio_get_level(DFU_PIN)) { // DFU mode asserted
2023-01-07 06:11:53 +01:00
tud_dfu_runtime_reboot_to_dfu_cb(); // reboot to DFU mode
}
// configure LEDs
2023-01-07 05:47:32 +01:00
io_conf.pin_bit_mask = (1ULL << LED_BOARD); // GPIO to configure
io_conf.intr_type = GPIO_INTR_DISABLE; // disable interrupt
io_conf.mode = GPIO_MODE_INPUT_OUTPUT; // set as output (push-pull), and input to read state
io_conf.pull_down_en = 0; // disable pull-down mode
io_conf.pull_up_en = 0; // disable pull-up mode
ESP_ERROR_CHECK( gpio_config(&io_conf) ); // configure GPIO
gpio_set_level(LED_BOARD, 0); // switch running LED off until all is ready
2022-07-18 14:36:50 +02:00
// setup USB CDC ACM for printing
ESP_LOGI(TAG, "USB initialization");
2023-01-07 01:43:01 +01:00
// get MAC
uint8_t mac[6];
esp_read_mac(mac, ESP_MAC_ETH);
static char usb_serial[13] = {0};
snprintf(usb_serial, sizeof(usb_serial), "%02X%02X%02X%02X%02X%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
usb_str_desc[3] = usb_serial;
const tinyusb_config_t tusb_cfg = {
.device_descriptor = NULL, // use default USB descriptor
.string_descriptor = usb_str_desc, // use custom string description to set serial
.external_phy = false, // use integrated phy
.configuration_descriptor = NULL,
};
2022-07-18 14:36:50 +02:00
ESP_ERROR_CHECK( tinyusb_driver_install(&tusb_cfg) ); // configure USB
tinyusb_config_cdcacm_t amc_cfg = { 0 }; // the configuration uses default values
ESP_ERROR_CHECK( tusb_cdc_acm_init(&amc_cfg) ); // configure CDC ACM
ESP_ERROR_CHECK( tusb_dfu_rf_init() ); // configure DFU runtime (ensures we can use it)
2022-07-18 14:36:50 +02:00
esp_tusb_init_console(TINYUSB_CDC_ACM_0); // log to USB
ESP_LOGI(TAG, "USB initialized");
2022-07-18 11:32:41 +02:00
2022-07-18 14:36:50 +02:00
ESP_LOGI(TAG, "application ready");
2023-01-07 05:47:32 +01:00
gpio_set_level(LED_BOARD, 0); // switch running LED on to indicate all is ready
2022-07-18 14:36:50 +02:00
while (1) {
vTaskDelay(1000 / portTICK_PERIOD_MS);
2023-01-07 05:47:32 +01:00
// toggle LED as heart beat
if (gpio_get_level(LED_BOARD)) {
gpio_set_level(LED_BOARD, 0);
} else {
gpio_set_level(LED_BOARD, 1);
}
2022-07-18 14:36:50 +02:00
}
2022-07-18 11:32:41 +02:00
}