ESP-IDF Firmware
Firmware architecture and call graph
Loading...
Searching...
No Matches
tusb_console.c File Reference
#include <stdio.h>
#include <stdio_ext.h>
#include "esp_log.h"
#include "cdc.h"
#include "tusb_console.h"
#include "tinyusb.h"
#include "vfs_tinyusb.h"
#include "esp_check.h"
Include dependency graph for tusb_console.c:

Go to the source code of this file.

Data Structures

struct  console_handle_t

Macros

#define STRINGIFY(s)
#define STRINGIFY2(s)

Functions

static esp_err_t redirect_std_streams_to (FILE **f_in, FILE **f_out, FILE **f_err, const char *path)
 Reopen standard streams using a new path.
static esp_err_t restore_std_streams (FILE **f_in, FILE **f_out, FILE **f_err)
 Restore output to default.
esp_err_t esp_tusb_init_console (int cdc_intf)
 Redirect output to the USB serial.
esp_err_t esp_tusb_deinit_console (int cdc_intf)
 Switch log to the default output.

Variables

static const char * TAG = "tusb_console"
static console_handle_t con

Macro Definition Documentation

◆ STRINGIFY

#define STRINGIFY ( s)
Value:
#define STRINGIFY2(s)

Definition at line 17 of file tusb_console.c.

Referenced by restore_std_streams().

◆ STRINGIFY2

#define STRINGIFY2 ( s)
Value:
#s

Definition at line 18 of file tusb_console.c.

Function Documentation

◆ esp_tusb_deinit_console()

esp_err_t esp_tusb_deinit_console ( int cdc_intf)

Switch log to the default output.

Parameters
cdc_intf- interface number of TinyUSB's CDC
Returns
esp_err_t

Definition at line 110 of file tusb_console.c.

111{
112 ESP_RETURN_ON_ERROR(restore_std_streams(&con.in, &con.out, &con.err), TAG, "Failed to restore STD streams");
114 return ESP_OK;
115}
#define ESP_RETURN_ON_ERROR(x, log_tag, format,...)
#define ESP_OK
Definition esp_err.h:23
static const char * TAG
Definition main/main.c:31
static console_handle_t con
static esp_err_t restore_std_streams(FILE **f_in, FILE **f_out, FILE **f_err)
Restore output to default.
esp_err_t esp_vfs_tusb_cdc_unregister(char const *path)
Unregister TinyUSB CDC from VFS.

References con, ESP_OK, ESP_RETURN_ON_ERROR, esp_vfs_tusb_cdc_unregister(), restore_std_streams(), and TAG.

Here is the call graph for this function:

◆ esp_tusb_init_console()

esp_err_t esp_tusb_init_console ( int cdc_intf)

Redirect output to the USB serial.

Parameters
cdc_intf- interface number of TinyUSB's CDC
Returns
esp_err_t - ESP_OK, ESP_FAIL or an error code

Definition at line 102 of file tusb_console.c.

103{
104 /* Registering TUSB at VFS */
106 ESP_RETURN_ON_ERROR(redirect_std_streams_to(&con.in, &con.out, &con.err, VFS_TUSB_PATH_DEFAULT), TAG, "Failed to redirect STD streams");
107 return ESP_OK;
108}
static esp_err_t redirect_std_streams_to(FILE **f_in, FILE **f_out, FILE **f_err, const char *path)
Reopen standard streams using a new path.
#define VFS_TUSB_PATH_DEFAULT
Definition vfs_tinyusb.h:16
esp_err_t esp_vfs_tusb_cdc_register(int cdc_intf, char const *path)
Register TinyUSB CDC at VFS with path.

References con, ESP_OK, ESP_RETURN_ON_ERROR, esp_vfs_tusb_cdc_register(), redirect_std_streams_to(), TAG, and VFS_TUSB_PATH_DEFAULT.

Here is the call graph for this function:

◆ redirect_std_streams_to()

esp_err_t redirect_std_streams_to ( FILE ** f_in,
FILE ** f_out,
FILE ** f_err,
const char * path )
static

Reopen standard streams using a new path.

Parameters
f_in- pointer to a pointer holding a file for in or NULL to don't change stdin
f_out- pointer to a pointer holding a file for out or NULL to don't change stdout
f_err- pointer to a pointer holding a file for err or NULL to don't change stderr
path- mount point
Returns
esp_err_t ESP_FAIL or ESP_OK

Definition at line 40 of file tusb_console.c.

41{
42 if (f_in) {
43 *f_in = freopen(path, "r", stdin);
44 if (*f_in == NULL) {
45 ESP_LOGE(TAG, "Failed to reopen in!");
46 return ESP_FAIL;
47 }
48 }
49 if (f_out) {
50 *f_out = freopen(path, "w", stdout);
51 if (*f_out == NULL) {
52 ESP_LOGE(TAG, "Failed to reopen out!");
53 return ESP_FAIL;
54 }
55 }
56 if (f_err) {
57 *f_err = freopen(path, "w", stderr);
58 if (*f_err == NULL) {
59 ESP_LOGE(TAG, "Failed to reopen err!");
60 return ESP_FAIL;
61 }
62 }
63
64 return ESP_OK;
65}

References ESP_OK, and TAG.

Referenced by esp_tusb_init_console().

Here is the caller graph for this function:

◆ restore_std_streams()

esp_err_t restore_std_streams ( FILE ** f_in,
FILE ** f_out,
FILE ** f_err )
static

Restore output to default.

Parameters
f_in- pointer to a pointer of an in file updated with redirect_std_streams_to or NULL to don't change stdin
f_out- pointer to a pointer of an out file updated with redirect_std_streams_to or NULL to don't change stdout
f_err- pointer to a pointer of an err file updated with redirect_std_streams_to or NULL to don't change stderr
Returns
esp_err_t ESP_FAIL or ESP_OK

Definition at line 75 of file tusb_console.c.

76{
77 const char *default_uart_dev = "/dev/uart/" STRINGIFY(CONFIG_ESP_CONSOLE_UART_NUM);
78 if (f_in) {
79 stdin = freopen(default_uart_dev, "r", *f_in);
80 if (stdin == NULL) {
81 ESP_LOGE(TAG, "Failed to reopen stdin!");
82 return ESP_FAIL;
83 }
84 }
85 if (f_out) {
86 stdout = freopen(default_uart_dev, "w", *f_out);
87 if (stdout == NULL) {
88 ESP_LOGE(TAG, "Failed to reopen stdout!");
89 return ESP_FAIL;
90 }
91 }
92 if (f_err) {
93 stderr = freopen(default_uart_dev, "w", *f_err);
94 if (stderr == NULL) {
95 ESP_LOGE(TAG, "Failed to reopen stderr!");
96 return ESP_FAIL;
97 }
98 }
99 return ESP_OK;
100}
#define STRINGIFY(s)

References ESP_OK, STRINGIFY, and TAG.

Referenced by esp_tusb_deinit_console().

Here is the caller graph for this function:

Variable Documentation

◆ con

console_handle_t con
static

Definition at line 28 of file tusb_console.c.

Referenced by esp_tusb_deinit_console(), and esp_tusb_init_console().

◆ TAG

const char* TAG = "tusb_console"
static

Definition at line 20 of file tusb_console.c.