1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- #pragma once
- #include "common.h"
- #include <USB.h>
- #include <HardwareSerial.h>
- #include <FreeRTOS.h>
- #include <freertos/stream_buffer.h>
- #include <atomic>
- void tty_init();
- void tty_ping();
- struct tty_packet_hdr {
- uint8_t stx;
- uint8_t len;
- uint16_t resv;
- uint32_t offs;
- uint32_t crc;
- };
- class TTY {
- private:
- Stream *_port;
- public:
- TTY(Stream &port);
- ~TTY();
- void reset();
- int rxdata(void *buf, size_t len);
- static int rxdata(token_t me, void *buf, size_t len);
- inline Stream & port() { return *_port; }
- inline operator Stream & () { return port(); }
-
- static void init();
- static void ping();
- private:
- void _onrx();
- void _onerr();
- void _onbreak();
- void _onconnect();
- void _ondisconnect();
-
- static void usb_onevent(void *arg, esp_event_base_t event_base,
- int32_t event_id, void *event_data);
- static void uart_onrx();
- static void uart_onerr(hardwareSerial_error_t);
- enum rx_state {
- normal,
- hdr,
- data
- };
- void _upload_begin();
- void _update_window(bool rst = false);
-
- struct {
- enum rx_state state;
- union {
- struct tty_packet_hdr hdr;
- uint8_t hdr_raw[sizeof(struct tty_packet_hdr)];
- };
- size_t rlen;
- uint32_t last_ack;
- } rx;
- StreamBufferHandle_t rx_sbuf;
- uint32_t tx_credits_ok;
- uint32_t tx_credits_sent;
- uint8_t rx_data[256];
- };
|