2023-03-17 16:12:49 +07:00
|
|
|
/*
|
2019-03-20 16:11:42 +07:00
|
|
|
* The MIT License (MIT)
|
|
|
|
|
*
|
2019-05-14 11:48:05 +07:00
|
|
|
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
2019-03-20 16:11:42 +07:00
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
* THE SOFTWARE.
|
|
|
|
|
*
|
|
|
|
|
*/
|
2018-08-22 19:13:29 +07:00
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
2023-08-03 15:44:05 +07:00
|
|
|
#include "bsp/board_api.h"
|
2018-08-22 19:13:29 +07:00
|
|
|
#include "tusb.h"
|
|
|
|
|
|
2025-07-02 15:32:12 +07:00
|
|
|
#ifdef ESP_PLATFORM
|
2021-11-29 13:24:10 +07:00
|
|
|
#define USBD_STACK_SIZE 4096
|
2021-09-26 12:23:08 +07:00
|
|
|
#else
|
2021-11-29 13:24:10 +07:00
|
|
|
// Increase stack size when debug log is enabled
|
|
|
|
|
#define USBD_STACK_SIZE (3*configMINIMAL_STACK_SIZE/2) * (CFG_TUSB_DEBUG ? 2 : 1)
|
2021-09-26 12:23:08 +07:00
|
|
|
#endif
|
2021-09-25 16:16:55 +07:00
|
|
|
|
2024-02-22 19:20:28 +07:00
|
|
|
#define CDC_STACK_SIZE configMINIMAL_STACK_SIZE
|
|
|
|
|
#define BLINKY_STACK_SIZE configMINIMAL_STACK_SIZE
|
2022-12-05 12:09:41 +07:00
|
|
|
|
2018-08-22 19:13:29 +07:00
|
|
|
//--------------------------------------------------------------------+
|
2023-09-25 16:53:11 +07:00
|
|
|
// MACRO CONSTANT TYPEDEF PROTOTYPES
|
2018-08-22 19:13:29 +07:00
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
|
|
2019-04-19 00:23:15 +07:00
|
|
|
/* Blink pattern
|
|
|
|
|
* - 250 ms : device not mounted
|
|
|
|
|
* - 1000 ms : device mounted
|
|
|
|
|
* - 2500 ms : device is suspended
|
|
|
|
|
*/
|
2023-09-25 16:53:11 +07:00
|
|
|
enum {
|
2019-04-19 00:23:15 +07:00
|
|
|
BLINK_NOT_MOUNTED = 250,
|
|
|
|
|
BLINK_MOUNTED = 1000,
|
|
|
|
|
BLINK_SUSPENDED = 2500,
|
|
|
|
|
};
|
|
|
|
|
|
2024-02-22 19:20:28 +07:00
|
|
|
// static task
|
2022-12-05 12:09:41 +07:00
|
|
|
#if configSUPPORT_STATIC_ALLOCATION
|
2024-02-22 19:20:28 +07:00
|
|
|
StackType_t blinky_stack[BLINKY_STACK_SIZE];
|
|
|
|
|
StaticTask_t blinky_taskdef;
|
2019-04-19 00:23:15 +07:00
|
|
|
|
2020-04-07 15:34:05 +07:00
|
|
|
StackType_t usb_device_stack[USBD_STACK_SIZE];
|
|
|
|
|
StaticTask_t usb_device_taskdef;
|
2020-03-10 23:16:09 +07:00
|
|
|
|
2024-02-22 19:20:28 +07:00
|
|
|
StackType_t cdc_stack[CDC_STACK_SIZE];
|
2020-04-07 15:34:05 +07:00
|
|
|
StaticTask_t cdc_taskdef;
|
2022-12-05 12:09:41 +07:00
|
|
|
#endif
|
2020-03-10 23:16:09 +07:00
|
|
|
|
2024-02-22 19:20:28 +07:00
|
|
|
static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED;
|
2020-03-10 23:16:09 +07:00
|
|
|
|
2023-09-25 16:53:11 +07:00
|
|
|
static void usb_device_task(void *param);
|
2024-02-22 19:20:28 +07:00
|
|
|
void led_blinking_task(void* param);
|
2023-09-25 16:53:11 +07:00
|
|
|
void cdc_task(void *params);
|
2025-01-29 15:28:19 +01:00
|
|
|
extern void msc_disk_init(void);
|
2020-04-07 16:19:47 +07:00
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
|
// Main
|
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
|
|
2023-09-25 16:53:11 +07:00
|
|
|
int main(void) {
|
2018-08-22 19:13:29 +07:00
|
|
|
board_init();
|
|
|
|
|
|
2024-11-21 17:49:38 +07:00
|
|
|
// Create task for: tinyusb, blinky, cdc
|
2022-12-05 12:09:41 +07:00
|
|
|
#if configSUPPORT_STATIC_ALLOCATION
|
2024-02-22 19:20:28 +07:00
|
|
|
xTaskCreateStatic(led_blinking_task, "blinky", BLINKY_STACK_SIZE, NULL, 1, blinky_stack, &blinky_taskdef);
|
2022-12-05 12:09:41 +07:00
|
|
|
xTaskCreateStatic(usb_device_task, "usbd", USBD_STACK_SIZE, NULL, configMAX_PRIORITIES-1, usb_device_stack, &usb_device_taskdef);
|
2024-02-22 19:20:28 +07:00
|
|
|
xTaskCreateStatic(cdc_task, "cdc", CDC_STACK_SIZE, NULL, configMAX_PRIORITIES - 2, cdc_stack, &cdc_taskdef);
|
2022-12-05 12:09:41 +07:00
|
|
|
#else
|
2024-02-22 19:20:28 +07:00
|
|
|
xTaskCreate(led_blinking_task, "blinky", BLINKY_STACK_SIZE, NULL, 1, NULL);
|
2023-09-25 16:53:11 +07:00
|
|
|
xTaskCreate(usb_device_task, "usbd", USBD_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL);
|
|
|
|
|
xTaskCreate(cdc_task, "cdc", CDC_STACK_SZIE, NULL, configMAX_PRIORITIES - 2, NULL);
|
2022-12-05 12:09:41 +07:00
|
|
|
#endif
|
|
|
|
|
|
2025-07-02 15:32:12 +07:00
|
|
|
#ifndef ESP_PLATFORM
|
|
|
|
|
// only start scheduler for non-espressif mcu
|
2018-10-24 12:39:04 +07:00
|
|
|
vTaskStartScheduler();
|
2021-03-27 20:22:32 +09:00
|
|
|
#endif
|
2021-03-31 20:50:08 +07:00
|
|
|
|
2021-03-27 20:22:32 +09:00
|
|
|
return 0;
|
2020-04-07 15:34:05 +07:00
|
|
|
}
|
2018-08-23 14:11:21 +07:00
|
|
|
|
2025-07-02 15:32:12 +07:00
|
|
|
#ifdef ESP_PLATFORM
|
2023-09-25 16:53:11 +07:00
|
|
|
void app_main(void) {
|
2020-04-07 15:34:05 +07:00
|
|
|
main();
|
2018-08-22 19:13:29 +07:00
|
|
|
}
|
2020-04-07 15:34:05 +07:00
|
|
|
#endif
|
2018-08-22 19:13:29 +07:00
|
|
|
|
2018-12-13 13:49:09 +07:00
|
|
|
// USB Device Driver task
|
|
|
|
|
// This top level thread process all usb events and invoke callbacks
|
2023-09-25 16:53:11 +07:00
|
|
|
static void usb_device_task(void *param) {
|
2018-12-13 13:49:09 +07:00
|
|
|
(void) param;
|
|
|
|
|
|
2022-06-06 17:35:17 +07:00
|
|
|
// init device stack on configured roothub port
|
2020-07-23 09:20:45 +07:00
|
|
|
// This should be called after scheduler/kernel is started.
|
|
|
|
|
// Otherwise it could cause kernel issue since USB IRQ handler does use RTOS queue API.
|
2024-10-11 12:58:18 +07:00
|
|
|
tusb_rhport_init_t dev_init = {
|
|
|
|
|
.role = TUSB_ROLE_DEVICE,
|
|
|
|
|
.speed = TUSB_SPEED_AUTO
|
|
|
|
|
};
|
2024-10-14 18:27:52 +07:00
|
|
|
tusb_init(BOARD_TUD_RHPORT, &dev_init);
|
2020-07-23 09:20:45 +07:00
|
|
|
|
2023-07-31 19:09:40 +07:00
|
|
|
if (board_init_after_tusb) {
|
|
|
|
|
board_init_after_tusb();
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-29 15:28:19 +01:00
|
|
|
msc_disk_init();
|
2018-12-13 13:49:09 +07:00
|
|
|
// RTOS forever loop
|
2023-09-25 16:53:11 +07:00
|
|
|
while (1) {
|
2022-04-20 18:35:38 +07:00
|
|
|
// put this thread to waiting state until there is new events
|
2018-12-13 14:51:37 +07:00
|
|
|
tud_task();
|
2022-04-20 14:37:42 +07:00
|
|
|
|
2022-04-20 18:35:38 +07:00
|
|
|
// following code only run if tud_task() process at least 1 event
|
2022-04-20 18:52:15 +07:00
|
|
|
tud_cdc_write_flush();
|
2018-12-13 13:49:09 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-19 13:16:06 +07:00
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
|
// Device callbacks
|
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
|
|
|
|
|
|
// Invoked when device is mounted
|
2023-09-25 16:53:11 +07:00
|
|
|
void tud_mount_cb(void) {
|
2024-02-22 19:20:28 +07:00
|
|
|
blink_interval_ms = BLINK_MOUNTED;
|
2019-04-19 13:16:06 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Invoked when device is unmounted
|
2023-09-25 16:53:11 +07:00
|
|
|
void tud_umount_cb(void) {
|
2024-02-22 19:20:28 +07:00
|
|
|
blink_interval_ms = BLINK_NOT_MOUNTED;
|
2019-04-19 13:16:06 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Invoked when usb bus is suspended
|
|
|
|
|
// remote_wakeup_en : if host allow us to perform remote wakeup
|
|
|
|
|
// Within 7ms, device must draw an average of current less than 2.5 mA from bus
|
2023-09-25 16:53:11 +07:00
|
|
|
void tud_suspend_cb(bool remote_wakeup_en) {
|
2019-04-19 13:16:06 +07:00
|
|
|
(void) remote_wakeup_en;
|
2024-02-22 19:20:28 +07:00
|
|
|
blink_interval_ms = BLINK_SUSPENDED;
|
2019-04-19 13:16:06 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Invoked when usb bus is resumed
|
2023-09-25 16:53:11 +07:00
|
|
|
void tud_resume_cb(void) {
|
2024-02-22 19:20:28 +07:00
|
|
|
blink_interval_ms = tud_mounted() ? BLINK_MOUNTED : BLINK_NOT_MOUNTED;
|
2019-04-19 13:16:06 +07:00
|
|
|
}
|
|
|
|
|
|
2018-08-22 19:13:29 +07:00
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
|
// USB CDC
|
|
|
|
|
//--------------------------------------------------------------------+
|
2023-09-25 16:53:11 +07:00
|
|
|
void cdc_task(void *params) {
|
2018-08-23 14:45:22 +07:00
|
|
|
(void) params;
|
|
|
|
|
|
2018-12-13 13:49:09 +07:00
|
|
|
// RTOS forever loop
|
2023-09-25 16:53:11 +07:00
|
|
|
while (1) {
|
2020-11-24 00:06:59 +07:00
|
|
|
// connected() check for DTR bit
|
|
|
|
|
// Most but not all terminal client set this when making connection
|
|
|
|
|
// if ( tud_cdc_connected() )
|
2018-08-23 14:45:22 +07:00
|
|
|
{
|
2020-11-24 00:06:59 +07:00
|
|
|
// There are data available
|
2023-09-25 16:53:11 +07:00
|
|
|
while (tud_cdc_available()) {
|
2020-11-18 09:47:39 +01:00
|
|
|
uint8_t buf[64];
|
2018-11-13 15:34:50 +07:00
|
|
|
|
2020-11-18 09:47:39 +01:00
|
|
|
// read and echo back
|
|
|
|
|
uint32_t count = tud_cdc_read(buf, sizeof(buf));
|
2021-03-18 15:23:08 +07:00
|
|
|
(void) count;
|
2018-12-13 13:49:09 +07:00
|
|
|
|
2021-03-18 15:23:08 +07:00
|
|
|
// Echo back
|
|
|
|
|
// Note: Skip echo by commenting out write() and write_flush()
|
|
|
|
|
// for throughput test e.g
|
|
|
|
|
// $ dd if=/dev/zero of=/dev/ttyACM0 count=10000
|
|
|
|
|
tud_cdc_write(buf, count);
|
2020-11-18 09:47:39 +01:00
|
|
|
}
|
2022-04-20 18:52:15 +07:00
|
|
|
|
|
|
|
|
tud_cdc_write_flush();
|
2025-07-03 14:28:19 +07:00
|
|
|
|
|
|
|
|
// Press on-board button to send Uart status notification
|
|
|
|
|
static uint32_t btn_prev = 0;
|
|
|
|
|
static cdc_notify_uart_state_t uart_state = { .value = 0 };
|
|
|
|
|
const uint32_t btn = board_button_read();
|
|
|
|
|
if (!btn_prev && btn) {
|
|
|
|
|
uart_state.dsr ^= 1;
|
|
|
|
|
tud_cdc_notify_uart_state(&uart_state);
|
|
|
|
|
}
|
|
|
|
|
btn_prev = btn;
|
2018-08-23 14:45:22 +07:00
|
|
|
}
|
2022-04-20 18:29:41 +07:00
|
|
|
|
|
|
|
|
// For ESP32-Sx this delay is essential to allow idle how to run and reset watchdog
|
|
|
|
|
vTaskDelay(1);
|
2018-11-13 15:34:50 +07:00
|
|
|
}
|
|
|
|
|
}
|
2018-08-22 19:13:29 +07:00
|
|
|
|
2020-11-18 09:47:39 +01:00
|
|
|
// Invoked when cdc when line state changed e.g connected/disconnected
|
2023-09-25 16:53:11 +07:00
|
|
|
void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts) {
|
2018-11-13 15:34:50 +07:00
|
|
|
(void) itf;
|
2020-11-24 00:18:18 +07:00
|
|
|
(void) rts;
|
2018-11-13 15:34:50 +07:00
|
|
|
|
2021-03-18 15:23:08 +07:00
|
|
|
// TODO set some indicator
|
2023-09-25 16:53:11 +07:00
|
|
|
if (dtr) {
|
2021-03-18 15:23:08 +07:00
|
|
|
// Terminal connected
|
2023-09-25 16:53:11 +07:00
|
|
|
} else {
|
2021-03-18 15:23:08 +07:00
|
|
|
// Terminal disconnected
|
2018-08-22 19:13:29 +07:00
|
|
|
}
|
|
|
|
|
}
|
2019-04-19 00:23:15 +07:00
|
|
|
|
|
|
|
|
// Invoked when CDC interface received data from host
|
2023-09-25 16:53:11 +07:00
|
|
|
void tud_cdc_rx_cb(uint8_t itf) {
|
2019-04-19 00:23:15 +07:00
|
|
|
(void) itf;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-22 19:13:29 +07:00
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
|
// BLINKING TASK
|
|
|
|
|
//--------------------------------------------------------------------+
|
2024-02-22 19:20:28 +07:00
|
|
|
void led_blinking_task(void* param) {
|
|
|
|
|
(void) param;
|
|
|
|
|
static uint32_t start_ms = 0;
|
2018-08-22 19:13:29 +07:00
|
|
|
static bool led_state = false;
|
|
|
|
|
|
2024-02-22 19:20:28 +07:00
|
|
|
while (1) {
|
|
|
|
|
// Blink every interval ms
|
|
|
|
|
vTaskDelay(blink_interval_ms / portTICK_PERIOD_MS);
|
|
|
|
|
start_ms += blink_interval_ms;
|
|
|
|
|
|
|
|
|
|
board_led_write(led_state);
|
|
|
|
|
led_state = 1 - led_state; // toggle
|
|
|
|
|
}
|
2018-08-22 19:13:29 +07:00
|
|
|
}
|