locking in DC11

This commit is contained in:
folkert van heusden 2024-05-09 21:14:13 +02:00
parent a635651474
commit 13c71164e8
Signed by untrusted user who does not match committer: folkert
GPG key ID: 6B6455EDFEED3BD1
4 changed files with 20 additions and 7 deletions

View file

@ -211,7 +211,6 @@ void start_network(console *const c)
Serial.println(F("* Adding DC11"));
dc11 *dc11_ = new dc11(1100, b);
b->add_DC11(dc11_);
#if !defined(BUILD_FOR_RP2040) && defined(TTY_SERIAL_RX)
constexpr uint32_t hwSerialConfig = SERIAL_8N1;
@ -222,9 +221,11 @@ void start_network(console *const c)
Serial_RS232.begin(bitrate, hwSerialConfig, TTY_SERIAL_RX, TTY_SERIAL_TX);
Serial_RS232.setHwFlowCtrlMode(0);
Serial_RS232.println(F("Go!"));
dc11_->set_serial(&Serial_RS232);
#endif
b->add_DC11(dc11_);
Serial.println(F("* Starting (NTP-) clock"));
ntp_ = new ntp("188.212.113.203");

View file

@ -77,6 +77,6 @@ board_build.filesystem = littlefs
lib_deps = greiman/SdFat@^2.1.2
adafruit/Adafruit NeoPixel
bblanchon/ArduinoJson@^6.19.4
build_flags = -std=gnu++17 -DESP32=1 -ggdb3 -D_GLIBCXX_USE_C99 -DBOARD_HAS_PSRAM -mfix-esp32-psram-cache-issue -DCONFIG_SPIRAM_USE_MALLOC -DTTY_SERIAL_TX=4 -DTTY_SERIAL_RX=5 -DNEOPIXELS_PIN=40
build_flags = -std=gnu++17 -DESP32=1 -ggdb3 -D_GLIBCXX_USE_C99 -DBOARD_HAS_PSRAM -mfix-esp32-psram-cache-issue -DCONFIG_SPIRAM_USE_MALLOC -DTTY_SERIAL_TX=17 -DTTY_SERIAL_RX=18 -DNEOPIXELS_PIN=40
build_unflags = -std=gnu++11
extra_scripts = pre:prepare.py

View file

@ -248,6 +248,7 @@ void dc11::set_serial(Stream *const s)
return;
}
std::unique_lock<std::mutex> lck(s_lock);
this->s = s;
s->write("Press enter to connect");
@ -259,9 +260,16 @@ void dc11::serial_handler()
while(!stop_flag) {
yield();
char c = 0;
{
std::unique_lock<std::mutex> lck(s_lock);
if (s->available() == 0)
continue;
c = s->read();
}
// 3 is reserved for a serial port
constexpr const int serial_line = 3;
@ -274,7 +282,7 @@ void dc11::serial_handler()
registers[serial_line * 4 + 0] |= 0160000; // "ERROR", RING INDICATOR, CARRIER TRANSITION
}
recv_buffers[serial_line].push_back(s->read());
recv_buffers[serial_line].push_back(c);
registers[serial_line * 4 + 0] |= 128; // DONE: bit 7
@ -408,8 +416,11 @@ void dc11::write_word(const uint16_t addr, const uint16_t v)
#if defined(ESP32)
if (line_nr == 3) {
if (s != nullptr)
if (s != nullptr) {
std::unique_lock<std::mutex> lck(s_lock);
s->write(c);
}
if (is_tx_interrupt_enabled(line_nr))
trigger_interrupt(line_nr, true);

1
dc11.h
View file

@ -49,6 +49,7 @@ private:
std::vector<char> recv_buffers[dc11_n_lines];
std::mutex input_lock[dc11_n_lines];
#if defined(ESP32)
std::mutex s_lock;
Stream *s { nullptr };
std::thread *serial_th { nullptr };
bool serial_enabled { false };