logging & tracing for DC11

This commit is contained in:
folkert van heusden 2024-05-10 01:29:29 +02:00
parent 4d6b682469
commit 8489b5b781
Signed by untrusted user who does not match committer: folkert
GPG key ID: 6B6455EDFEED3BD1
2 changed files with 38 additions and 7 deletions

View file

@ -19,6 +19,7 @@
#include <sys/socket.h> #include <sys/socket.h>
#endif #endif
#if IS_POSIX #if IS_POSIX
#include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <termios.h> #include <termios.h>
#include <thread> #include <thread>
@ -88,6 +89,8 @@ dc11::dc11(const int base_port, bus *const b):
dc11::~dc11() dc11::~dc11()
{ {
DOLOG(debug, false, "DC11 closing");
stop_flag = true; stop_flag = true;
if (th) { if (th) {
@ -111,6 +114,8 @@ dc11::~dc11()
void dc11::trigger_interrupt(const int line_nr, const bool is_tx) void dc11::trigger_interrupt(const int line_nr, const bool is_tx)
{ {
TRACE("DC11: interrupt for line %d, %s", line_nr, is_tx ? "TX" : "RX");
b->getCpu()->queue_interrupt(5, 0300 + line_nr * 010 + 4 * is_tx); b->getCpu()->queue_interrupt(5, 0300 + line_nr * 010 + 4 * is_tx);
} }
@ -302,12 +307,15 @@ void dc11::set_serial(const int bitrate, const int rx, const int tx)
void dc11::set_serial(const int bitrate, const std::string & device) void dc11::set_serial(const int bitrate, const std::string & device)
{ {
serial_fd = open(device.c_str(), O_RDWR); serial_fd = open(device.c_str(), O_RDWR);
if (serial_fd == -1) if (serial_fd == -1) {
DOLOG(warning, false, "DC11 failed to access %s: %s", device.c_str(), strerror(errno));
return; // TODO error handling return; // TODO error handling
}
// from https://stackoverflow.com/questions/6947413/how-to-open-read-and-write-from-serial-port-in-c // from https://stackoverflow.com/questions/6947413/how-to-open-read-and-write-from-serial-port-in-c
termios tty { }; termios tty { };
if (tcgetattr(serial_fd, &tty) == -1) { if (tcgetattr(serial_fd, &tty) == -1) {
DOLOG(warning, false, "DC11 tcgetattr failed: %s", strerror(errno));
close(serial_fd); close(serial_fd);
return; return;
} }
@ -334,6 +342,7 @@ void dc11::set_serial(const int bitrate, const std::string & device)
tty.c_cflag &= ~CRTSCTS; tty.c_cflag &= ~CRTSCTS;
if (tcsetattr(serial_fd, TCSANOW, &tty) == -1) { if (tcsetattr(serial_fd, TCSANOW, &tty) == -1) {
DOLOG(warning, false, "DC11 tcsetattr failed: %s", strerror(errno));
close(serial_fd); close(serial_fd);
return; return;
} }
@ -344,8 +353,12 @@ void dc11::set_serial(const int bitrate, const std::string & device)
void dc11::serial_handler() void dc11::serial_handler()
{ {
set_thread_name("kek:dc11-serial");
TRACE("DC11: serial handler thread started");
#if IS_POSIX #if IS_POSIX
pollfd fds[] { { serial_fd, POLLIN, 0 } }; pollfd fds[] = { { serial_fd, POLLIN, 0 } };
#endif #endif
while(!stop_flag) { while(!stop_flag) {
@ -364,14 +377,18 @@ void dc11::serial_handler()
continue; continue;
#elif IS_POSIX #elif IS_POSIX
int rc_poll = poll(fds, 1, 100); int rc_poll = poll(fds, 1, 100);
if (rc_poll == -1) if (rc_poll == -1) {
DOLOG(warning, false, "DC11 poll failed: %s", strerror(errno));
break; break;
}
if (rc_poll == 0) if (rc_poll == 0)
continue; continue;
int rc_read = read(serial_fd, &c, 1); int rc_read = read(serial_fd, &c, 1);
if (rc_read <= 0) if (rc_read <= 0) {
DOLOG(warning, false, "DC11 read on %d failed: %s", serial_fd, strerror(errno));
break; break;
}
#endif #endif
// 3 is reserved for a serial port // 3 is reserved for a serial port
@ -380,19 +397,26 @@ void dc11::serial_handler()
std::unique_lock<std::mutex> lck(input_lock[serial_line]); std::unique_lock<std::mutex> lck(input_lock[serial_line]);
if (serial_enabled == false) { if (serial_enabled == false) {
TRACE("DC-11: enabling serial connection");
serial_enabled = true; serial_enabled = true;
// first key press enables the port // first key press enables the port
registers[serial_line * 4 + 0] |= 0160000; // "ERROR", RING INDICATOR, CARRIER TRANSITION registers[serial_line * 4 + 0] |= 0160000; // "ERROR", RING INDICATOR, CARRIER TRANSITION
} }
else {
recv_buffers[serial_line].push_back(c); TRACE("DC-11: key %d pressed", c);
registers[serial_line * 4 + 0] |= 128; // DONE: bit 7 registers[serial_line * 4 + 0] |= 128; // DONE: bit 7
}
recv_buffers[serial_line].push_back(c);
if (is_rx_interrupt_enabled(serial_line)) if (is_rx_interrupt_enabled(serial_line))
trigger_interrupt(serial_line, false); trigger_interrupt(serial_line, false);
} }
TRACE("DC11: serial handler thread terminating");
} }
void dc11::reset() void dc11::reset()
@ -523,10 +547,14 @@ void dc11::write_word(const uint16_t addr, const uint16_t v)
uart_write_bytes(ESP32_UART, &c, 1); uart_write_bytes(ESP32_UART, &c, 1);
#elif IS_POSIX #elif IS_POSIX
if (write(serial_fd, &c, 1) != 1) { if (write(serial_fd, &c, 1) != 1) {
DOLOG(warning, false, "DC11 failed to send %d to (fd %d) serial port: %s", c, serial_fd, strerror(errno));
// TODO error handling // TODO error handling
} }
#endif #endif
} }
else {
TRACE("DC11 serial line 4 not connected yet output %d", c);
}
if (is_tx_interrupt_enabled(line_nr)) if (is_tx_interrupt_enabled(line_nr))
trigger_interrupt(line_nr, true); trigger_interrupt(line_nr, true);
@ -539,6 +567,7 @@ void dc11::write_word(const uint16_t addr, const uint16_t v)
registers[line_nr * 4 + 0] |= 0140000; // "ERROR", CARRIER TRANSITION registers[line_nr * 4 + 0] |= 0140000; // "ERROR", CARRIER TRANSITION
assert(fd != serial_fd);
close(fd); close(fd);
pfds[dc11_n_lines + line_nr].fd = INVALID_SOCKET; pfds[dc11_n_lines + line_nr].fd = INVALID_SOCKET;
} }

View file

@ -600,8 +600,10 @@ int main(int argc, char *argv[])
// TODO // TODO
dc11 *dc11_ = new dc11(1100, b); dc11 *dc11_ = new dc11(1100, b);
if (dc11_device.has_value()) if (dc11_device.has_value()) {
DOLOG(info, false, "Configuring DC11 device for serial port on %s", dc11_device.value().c_str());
dc11_->set_serial(115200, dc11_device.value()); dc11_->set_serial(115200, dc11_device.value());
}
b->add_DC11(dc11_); b->add_DC11(dc11_);
tm_11 *tm_11_ = new tm_11(b); tm_11 *tm_11_ = new tm_11(b);