diff --git a/puara.cpp b/puara.cpp index 5ff56d3..1a0c991 100644 --- a/puara.cpp +++ b/puara.cpp @@ -81,11 +81,10 @@ char Puara::serial_data[PUARA_SERIAL_BUFSIZE]; int Puara::serial_data_length; std::string Puara::serial_data_str; std::string Puara::serial_data_str_buffer; - +int Puara::module_monitor = UART_MONITOR; const std::string Puara::data_start = "<<<"; const std::string Puara::data_end = ">>>"; - unsigned int Puara::get_version() { return version; }; @@ -94,7 +93,7 @@ void Puara::set_version(unsigned int user_version) { version = user_version; }; -void Puara::start() { +void Puara::start(Monitors monitor) { std::cout << "\n" << "**********************************************************\n" @@ -113,6 +112,8 @@ void Puara::start() { start_webserver(); start_mdns_service(dmiName, dmiName); wifi_scan(); + + module_monitor = monitor; // some delay added as start listening blocks the hw monitor std::cout << "Starting serial monitor..." << std::endl; @@ -1269,7 +1270,7 @@ void Puara::interpret_serial(void *pvParameters) { } } - void Puara::serial_monitor(void *pvParameters) { + void Puara::uart_monitor(void *pvParameters) { const int uart_num0 = 0; //UART port 0 uart_config_t uart_config0 = { .baud_rate = 115200, @@ -1301,10 +1302,74 @@ void Puara::interpret_serial(void *pvParameters) { } } + void Puara::jtag_monitor(void *pvParameters) { + // Setup jtag module for USB Serial reads + usb_serial_jtag_driver_config_t jtag_config { + .tx_buffer_size = 256, + .rx_buffer_size = 256, + }; + + // Install jtag module + usb_serial_jtag_driver_install(&jtag_config); + + while(1) { + // serial_data_length = USBSerial.read(); + // Only read if connected to PC + serial_data_length = usb_serial_jtag_read_bytes(serial_data, PUARA_SERIAL_BUFSIZE, 500 / portTICK_RATE_MS); + if (serial_data_length > 0) { + serial_data_str = convertToString(serial_data); + // remove new line character at end + if (serial_data_str[serial_data_str.size() - 1] == '\n') + serial_data_str.erase(serial_data_str.size() - 1); + if (serial_data_str[serial_data_str.size() - 1] == '\r') + serial_data_str.erase(serial_data_str.size() - 1); + memset(serial_data, 0, sizeof serial_data); + } + } + } + + void Puara::usb_monitor(void *pvParameters) { + // // Setup usb module for USB reads + // const char *product_name = dmiName.c_str(); + // const char *manufacturer_name = author.c_str(); + + // tinyusb_device_config_t usb_config = { + // .vid = USB_ESPRESSIF_VID, + // .pid = 0x0002, + // .product_name = product_name, + // .manufacturer_name = manufacturer_name, + // .serial_number = product_name, + // .fw_version = version, + // .usb_version = 0x0200, + // .usb_class = TUSB_CLASS_MISC, + // .usb_subclass = MISC_SUBCLASS_COMMON, + // .usb_protocol = MISC_PROTOCOL_IAD, + // .usb_attributes = TUSB_DESC_CONFIG_ATT_SELF_POWERED, + // .usb_power_ma = 500, + // .webusb_enabled = false, + // .webusb_url = "espressif.github.io/arduino-esp32/webusb.html" + // }; + + // // Setup USB interface + // tinyusb_init(&usb_config); + // TODO: Read from USB interface + std::cout << "USB OTG monitor not supported, use the USB Serial JTAG or UART interface" << std::endl; + } + bool Puara::start_serial_listening() { //std::cout << "starting serial monitor \n"; - xTaskCreate(serial_monitor, "serial_monitor", 2048, NULL, 10, NULL); - xTaskCreate(interpret_serial, "interpret_serial", 4096, NULL, 10, NULL); + if (module_monitor = UART_MONITOR) { + xTaskCreate(uart_monitor, "serial_monitor", 2048, NULL, 10, NULL); + xTaskCreate(interpret_serial, "interpret_serial", 4096, NULL, 10, NULL); + } else if (module_monitor = JTAG_MONITOR) { + xTaskCreate(jtag_monitor, "serial_monitor", 2048, NULL, 10, NULL); + xTaskCreate(interpret_serial, "interpret_serial", 4096, NULL, 10, NULL); + } else if (module_monitor = USB_MONITOR) { + xTaskCreate(usb_monitor, "serial_monitor", 2048, NULL, 10, NULL); + xTaskCreate(interpret_serial, "interpret_serial", 4096, NULL, 10, NULL); + } else { + std::cout << "Invalid Monitor Type" << std::endl; + } return 1; } diff --git a/puara.h b/puara.h index 0e49296..1544ea3 100644 --- a/puara.h +++ b/puara.h @@ -33,6 +33,7 @@ #include #include #include +#include // jtag module #include // The following libraries need to be included if using the espidf framework: @@ -44,6 +45,8 @@ #include #include #include +#include "esp_console.h" +#include "esp32-hal-tinyusb.h" class Puara { @@ -141,13 +144,22 @@ class Puara { static std::string serial_config_str; static std::string convertToString(char* a); static void interpret_serial(void *pvParameters); - static void serial_monitor(void *pvParameters); + static void uart_monitor(void *pvParameters); + static void jtag_monitor(void *pvParameters); + static void usb_monitor(void *pvParameters); static const int reboot_delay = 3000; static void reboot_with_delay(void *pvParameter); static std::string urlDecode(std::string text); public: - static void start(); + // Monitor types + enum Monitors { + UART_MONITOR = 0, + JTAG_MONITOR = 1, + USB_MONITOR = 2 + }; + + static void start(Monitors monitor = UART_MONITOR); static void config_spiffs(); static httpd_handle_t start_webserver(void); static void stop_webserver(void); @@ -181,6 +193,9 @@ class Puara { static std::string getVarText(std::string varName); static bool IP1_ready(); static bool IP2_ready(); + + // Set default monitor as UART + static int module_monitor; }; #endif \ No newline at end of file