Select Git revision
st3m_console.c
st3m_console.c 8.99 KiB
#include "st3m_console.h"
#include <sys/fcntl.h>
#include <sys/errno.h>
#include "esp_vfs.h"
#include "esp_vfs_dev.h"
#include "esp_log.h"
#include "esp_timer.h"
#include "driver/uart.h"
#include "freertos/FreeRTOS.h"
#include "freertos/ringbuf.h"
#include "freertos/semphr.h"
static const char *TAG = "st3m-console";
static const uart_port_t uart_num = UART_NUM_0;
static esp_vfs_t _vfs;
typedef struct {
int flags;
} st3m_console_file_t;
typedef struct {
SemaphoreHandle_t mu;
RingbufHandle_t txbuf;
RingbufHandle_t rxbuf;
st3m_console_file_t fstdin;
st3m_console_file_t fstdout;
st3m_console_file_t fstderr;
int64_t cdc_last_read;
int64_t cdc_first_read_at;
} st3m_console_state_t;
static st3m_console_state_t _state = {
.mu = NULL,
.txbuf = NULL,
.rxbuf = NULL,
.fstdin = { 0 },
.fstdout = { 0 },
.fstderr = { 0 },
.cdc_last_read = 0,
.cdc_first_read_at = 0,
};
static void _uart0_write(const char *buffer, size_t bufsize) {
// Skip entire writes that would block, as we don't want to limit stdout
// speed to 115200 baud.
size_t free_bytes = 0;
uart_get_tx_buffer_free_size(uart_num, &free_bytes);
if (bufsize > free_bytes) {
return;
}
uart_write_bytes(uart_num, buffer, bufsize);
}
int st3m_uart0_debug(const char *fmt, ...) {
va_list args;
va_start(args, fmt);
char buf[256];
memset(buf, 0, 256);
vsnprintf(buf, 256, fmt, args);
_uart0_write(buf, strlen(buf));