From 21a3ceb2b2951fbe84b473968426c90534b1eb5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gu=C3=B0ni=20M=C3=A1r=20Gilbert?= Date: Sat, 29 Oct 2022 09:29:42 +0000 Subject: [PATCH] PFW-1428 Enable UART1 receiver when the MMU is ready to process incoming messages --- src/hal/usart.h | 13 ++++++++----- src/main.cpp | 20 +++++++++++--------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/hal/usart.h b/src/hal/usart.h index dc23803..d8fe1dd 100644 --- a/src/hal/usart.h +++ b/src/hal/usart.h @@ -66,20 +66,23 @@ public: /// blocks until the TX buffer was successfully transmitted void Flush(); + /// Enable the RX receiver + __attribute__((always_inline)) inline void rx_enable() { husart->UCSRxB |= (1 << RXEN1); }; + /// Initializes USART interface __attribute__((always_inline)) inline void Init(USART_InitTypeDef *const conf) { gpio::Init(conf->rx_pin, gpio::GPIO_InitTypeDef(gpio::Mode::input, gpio::Level::low)); gpio::Init(conf->tx_pin, gpio::GPIO_InitTypeDef(gpio::Mode::output, gpio::Level::low)); husart->UBRRx = UART_BAUD_SELECT(conf->baudrate, F_CPU); - husart->UCSRxA |= (1 << 1); // Set double baudrate setting. Clear all other status bits/flags + husart->UCSRxA |= (1 << U2X1); // Set double baudrate setting. Clear all other status bits/flags // husart->UCSRxC |= (1 << 3); // 2 stop bits. Preserve data size setting husart->UCSRxD = 0; // disable hardware flow control. Few avr MCUs have this feature, but this register is reserved on all AVR devices with USART, so we can disable it without consequences. - husart->UCSRxB = (1 << 3) | (1 << 4) | (1 << 7); // Turn on the transmission and reception circuitry and enable the RX interrupt + husart->UCSRxB = (1 << TXEN1) | (1 << RXCIE1); // Turn on the transmission and reception circuitry and enable the RX interrupt } /// implementation of the receive ISR's body __attribute__((always_inline)) inline void ISR_RX() { - if (husart->UCSRxA & (1 << 4)) { + if (husart->UCSRxA & (1 << FE1)) { (void)husart->UDRx; } else { rx_buf.push((uint8_t)husart->UDRx); @@ -94,10 +97,10 @@ public: // clear the TXC bit -- "can be cleared by writing a one to its bit // location". This makes sure flush() won't return until the bytes // actually got written - husart->UCSRxA |= (1 << 6); + husart->UCSRxA |= (1 << TXC1); if (tx_buf.empty()) - husart->UCSRxB &= ~(1 << 5); // disable UDRE interrupt + husart->UCSRxB &= ~(1 << UDRIE1); // disable UDRE interrupt } USART(USART_TypeDef *husart) diff --git a/src/main.cpp b/src/main.cpp index af90d79..80a0a2c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -123,17 +123,19 @@ static void setup2() { if (logic::hwSanity.Error() != ErrorCode::OK) { // forward the issue to the logic startup handler. logic::noCommand.SetInitError(logic::hwSanity.Error()); - return; + } else { + // Idler and Selector decide whether homing is possible/safe + mi::idler.Init(); + ms::selector.Init(); + + // activate the correct LED if filament is present + if (mg::globals.FilamentLoaded() > mg::FilamentLoadState::AtPulley) { + ml::leds.SetMode(mg::globals.ActiveSlot(), ml::green, ml::on); + } } - // Idler and Selector decide whether homing is possible/safe - mi::idler.Init(); - ms::selector.Init(); - - // activate the correct LED if filament is present - if (mg::globals.FilamentLoaded() > mg::FilamentLoadState::AtPulley) { - ml::leds.SetMode(mg::globals.ActiveSlot(), ml::green, ml::on); - } + // Finally, enable RX receiver to start talking to the printer + hu::usart1.rx_enable(); } void Panic(ErrorCode ec) {