Files
2023-09-14 23:13:38 +08:00

111 lines
3.5 KiB
C++

/*
* Copyright (c) 2023-2023, lihongquan
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2023-9-8 lihongquan add license declaration
*/
#include <stdint.h>
#include "esp_log.h"
#include <errno.h>
#include <dirent.h>
#include <esp_wifi.h>
#include <esp_event.h>
#include <nvs_flash.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "tinyusb.h"
#include "tusb_cdc_acm.h"
#include "sdkconfig.h"
#include "cdc_uart.h"
#include "tusb_config.h"
#include "DAP_config.h"
#include "DAP.h"
#include "usb_desc.h"
#include "msc_disk.h"
#include "esp_netif.h"
#include "web_handler.h"
#include "usb_cdc_handler.h"
#include "esp_http_server.h"
#include "web_server.h"
#include "programmer.h"
#include "protocol_examples_common.h"
static const char *TAG = "main";
static httpd_handle_t http_server = nullptr;
extern "C" uint16_t tud_hid_get_report_cb(uint8_t instance, uint8_t report_id, hid_report_type_t report_type, uint8_t *buffer, uint16_t reqlen)
{
return 0;
}
extern "C" void tud_hid_set_report_cb(uint8_t instance, uint8_t report_id, hid_report_type_t report_type, uint8_t const *buffer, uint16_t bufsize)
{
static uint8_t s_tx_buf[CFG_TUD_HID_EP_BUFSIZE];
DAP_ProcessCommand(buffer, s_tx_buf);
tud_hid_report(0, s_tx_buf, sizeof(s_tx_buf));
}
static void disconnect_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
{
web_server_stop((httpd_handle_t *)arg);
}
static void connect_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
{
web_server_init((httpd_handle_t *)arg);
}
extern "C" void app_main(void)
{
bool ret = false;
ESP_ERROR_CHECK(nvs_flash_init());
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, connect_handler, &http_server));
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, disconnect_handler, &http_server));
ESP_ERROR_CHECK(example_connect());
tinyusb_config_t tusb_cfg = {
.device_descriptor = nullptr,
.string_descriptor = nullptr,
.string_descriptor_count = 0,
.external_phy = false,
.configuration_descriptor = nullptr,
.self_powered = false,
.vbus_monitor_io = 0};
tinyusb_config_cdcacm_t acm_cfg = {
.usb_dev = TINYUSB_USBDEV_0,
.cdc_port = TINYUSB_CDC_ACM_0,
.rx_unread_buf_sz = 64,
.callback_rx = usb_cdc_send_to_uart, // the first way to register a callback
.callback_rx_wanted_char = NULL,
.callback_line_state_changed = NULL,
.callback_line_coding_changed = usb_cdc_set_line_codinig};
DAP_Setup();
ESP_LOGI(TAG, "USB initialization");
ret = msc_dick_mount(CONFIG_TINYUSB_MSC_MOUNT_PATH);
tusb_cfg.configuration_descriptor = get_configuration_descriptor(ret);
tusb_cfg.string_descriptor_count = get_string_descriptor_count(ret);
tusb_cfg.string_descriptor = get_string_descriptor(ret);
tusb_cfg.device_descriptor = get_device_descriptor();
ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg));
ESP_ERROR_CHECK(tusb_cdc_acm_init(&acm_cfg));
programmer_init();
cdc_uart_init(UART_NUM_1, GPIO_NUM_13, GPIO_NUM_14, 115200);
cdc_uart_register_rx_handler(CDC_UART_USB_HANDLER, usb_cdc_send_to_host, (void *)TINYUSB_CDC_ACM_0);
cdc_uart_register_rx_handler(CDC_UART_WEB_HANDLER, web_send_to_clients, &http_server);
ESP_LOGI(TAG, "USB initialization DONE");
}