Skip to content
Snippets Groups Projects
Select Git revision
  • 15ed740ec667d2bfc0bd775f2ee7903111dd5fdd
  • main default protected
  • blm_dev_chan
  • release/1.4.0 protected
  • widgets_draw
  • return_of_melodic_demo
  • task_cleanup
  • mixer2
  • dx/fb-save-restore
  • dx/dldldld
  • fpletz/flake
  • dx/jacksense-headset-mic-only
  • release/1.3.0 protected
  • fil3s-limit-filesize
  • allow-reloading-sunmenu
  • wifi-json-error-handling
  • app_text_viewer
  • shoegaze-fps
  • media_has_video_has_audio
  • fil3s-media
  • more-accurate-battery
  • v1.4.0
  • v1.3.0
  • v1.2.0
  • v1.2.0+rc1
  • v1.1.1
  • v1.1.0
  • v1.1.0+rc1
  • v1.0.0
  • v1.0.0+rc6
  • v1.0.0+rc5
  • v1.0.0+rc4
  • v1.0.0+rc3
  • v1.0.0+rc2
  • v1.0.0+rc1
35 results

st3m_console.c

Blame
  • 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));