From 35068a34ee322d8c4b921954e9610d3ee7389e0c Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Wed, 8 May 2024 09:33:05 +0200 Subject: [PATCH] DC11: I/O to "Stream"-device (serial port on Arduino) --- dc11.cpp | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- dc11.h | 22 ++++++++++----------- 2 files changed, 66 insertions(+), 15 deletions(-) diff --git a/dc11.cpp b/dc11.cpp index 97ad1ff..97b0e79 100644 --- a/dc11.cpp +++ b/dc11.cpp @@ -1,6 +1,9 @@ // (C) 2024 by Folkert van Heusden // Released under MIT license +#if defined(ESP32) +#include +#endif #if defined(ESP32) #include #include @@ -49,6 +52,13 @@ dc11::~dc11() } delete [] pfds; + +#if defined(ESP32) + if (serial_th) { + serial_th->join(); + delete serial_th; + } +#endif } void dc11::trigger_interrupt(const int line_nr, const bool is_tx) @@ -63,13 +73,15 @@ void dc11::operator()() DOLOG(info, true, "DC11 thread started"); for(int i=0; is = s; + + serial_th = new std::thread(&dc11::serial_handler, this); +} + +void dc11::serial_handler() +{ + while(!stop_flag) { + yield(); + + if (s->available() == 0) + continue; + + // 3 is reserved for a serial port + recv_buffers[3].push_back(s->read()); + + registers[3 * 4 + 0] |= 128; // DONE: bit 7 + + if (is_rx_interrupt_enabled(3)) + trigger_interrupt(3, false); + } +} +#endif + void dc11::reset() { } @@ -310,6 +354,13 @@ void dc11::write_word(const uint16_t addr, const uint16_t v) else TRACE("DC11: transmit %c on line %d", c, line_nr); +#if defined(ESP32) + if (line_nr == 3) { + if (s != nullptr) + s->write(c); + return; + } +#endif SOCKET fd = pfds[dc11_n_lines + line_nr].fd; if (fd != INVALID_SOCKET && write(fd, &c, 1) != 1) { diff --git a/dc11.h b/dc11.h index 005e39d..b264351 100644 --- a/dc11.h +++ b/dc11.h @@ -2,9 +2,6 @@ // Released under MIT license #pragma once -#if defined(ESP32) -#include -#endif #include #include #include @@ -27,6 +24,7 @@ #define DC11_BASE DC11_RCSR #define DC11_END (DC11_BASE + (4 * 4 + 1) * 2) // 4 interfaces, + 2 to point behind it +class Stream; class bus; struct pollfd; @@ -36,22 +34,23 @@ constexpr const int dc11_n_lines = 4; class dc11 { private: - int base_port { 1100 }; - bus *const b { nullptr }; - uint16_t registers[4 * dc11_n_lines] { 0 }; - std::atomic_bool stop_flag { false }; - std::thread *th { nullptr }; + int base_port { 1100 }; + bus *const b { nullptr }; + uint16_t registers[4 * dc11_n_lines] { 0 }; + std::atomic_bool stop_flag { false }; + std::thread *th { nullptr }; // not statically allocated because of compiling problems on arduino #if defined(_WIN32) - WSAPOLLFD *pfds { nullptr }; + WSAPOLLFD *pfds { nullptr }; #else - pollfd *pfds { nullptr }; + pollfd *pfds { nullptr }; #endif std::vector recv_buffers[dc11_n_lines]; std::mutex input_lock[dc11_n_lines]; #if defined(ESP32) - Stream *s { nullptr }; + Stream *s { nullptr }; + std::thread *serial_th { nullptr }; #endif void trigger_interrupt(const int line_nr, const bool is_tx); @@ -70,6 +69,7 @@ public: void reset(); #if defined(ESP32) void set_serial(Stream *const s); + void serial_handler(); #endif uint8_t read_byte(const uint16_t addr);