ESP-IDF Firmware
Firmware architecture and call graph
Loading...
Searching...
No Matches
tusb_tasks.c
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#include "sdkconfig.h"
8#include "freertos/FreeRTOS.h"
9#include "freertos/task.h"
10#include "esp_log.h"
11#include "esp_check.h"
12#include "tinyusb.h"
13#include "tusb_tasks.h"
14
15const static char *TAG = "tusb_tsk";
16static TaskHandle_t s_tusb_tskh;
17
21static void tusb_device_task(void *arg)
22{
23 ESP_LOGD(TAG, "tinyusb task started");
24 while (1) { // RTOS forever loop
25 tud_task();
26 }
27}
28
30{
31 // This function is not garanteed to be thread safe, if invoked multiple times without calling `tusb_stop_task`, will cause memory leak
32 // doing a sanity check anyway
33 ESP_RETURN_ON_FALSE(!s_tusb_tskh, ESP_ERR_INVALID_STATE, TAG, "TinyUSB main task already started");
34 // Create a task for tinyusb device stack:
35 xTaskCreate(tusb_device_task, "TinyUSB", CONFIG_TINYUSB_TASK_STACK_SIZE, NULL, CONFIG_TINYUSB_TASK_PRIORITY, &s_tusb_tskh);
36 ESP_RETURN_ON_FALSE(s_tusb_tskh, ESP_FAIL, TAG, "create TinyUSB main task failed");
37 return ESP_OK;
38}
39
41{
42 ESP_RETURN_ON_FALSE(s_tusb_tskh, ESP_ERR_INVALID_STATE, TAG, "TinyUSB main task not started yet");
43 vTaskDelete(s_tusb_tskh);
44 s_tusb_tskh = NULL;
45 return ESP_OK;
46}
int esp_err_t
Definition esp_err.h:21
#define ESP_OK
Definition esp_err.h:23
#define ESP_LOGD
Definition esp_log.h:22
static const char * TAG
Definition main/main.c:31
esp_err_t tusb_run_task(void)
This helper function creates and starts a task which wraps tud_task().
Definition tusb_tasks.c:29
static void tusb_device_task(void *arg)
This top level thread processes all usb events and invokes callbacks.
Definition tusb_tasks.c:21
esp_err_t tusb_stop_task(void)
This helper function stops and destroys the task created by tusb_run_task().
Definition tusb_tasks.c:40
static TaskHandle_t s_tusb_tskh
Definition tusb_tasks.c:16