PFW-1428 Enable UART1 receiver when the MMU is ready to process incoming messages

pull/234/head
Guðni Már Gilbert 2022-10-29 09:29:42 +00:00
parent 0e625dc551
commit 06fe792a94
2 changed files with 19 additions and 14 deletions

View File

@ -66,20 +66,23 @@ public:
/// blocks until the TX buffer was successfully transmitted /// blocks until the TX buffer was successfully transmitted
void Flush(); void Flush();
/// Enable the RX receiver
__attribute__((always_inline)) inline void rx_enable() { husart->UCSRxB |= (1 << RXEN1); };
/// Initializes USART interface /// Initializes USART interface
__attribute__((always_inline)) inline void Init(USART_InitTypeDef *const conf) { __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->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)); gpio::Init(conf->tx_pin, gpio::GPIO_InitTypeDef(gpio::Mode::output, gpio::Level::low));
husart->UBRRx = UART_BAUD_SELECT(conf->baudrate, F_CPU); 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->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->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 /// implementation of the receive ISR's body
__attribute__((always_inline)) inline void ISR_RX() { __attribute__((always_inline)) inline void ISR_RX() {
if (husart->UCSRxA & (1 << 4)) { if (husart->UCSRxA & (1 << FE1)) {
(void)husart->UDRx; (void)husart->UDRx;
} else { } else {
rx_buf.push((uint8_t)husart->UDRx); 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 // 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 // location". This makes sure flush() won't return until the bytes
// actually got written // actually got written
husart->UCSRxA |= (1 << 6); husart->UCSRxA |= (1 << TXC1);
if (tx_buf.empty()) if (tx_buf.empty())
husart->UCSRxB &= ~(1 << 5); // disable UDRE interrupt husart->UCSRxB &= ~(1 << UDRIE1); // disable UDRE interrupt
} }
USART(USART_TypeDef *husart) USART(USART_TypeDef *husart)

View File

@ -123,17 +123,19 @@ static void setup2() {
if (logic::hwSanity.Error() != ErrorCode::OK) { if (logic::hwSanity.Error() != ErrorCode::OK) {
// forward the issue to the logic startup handler. // forward the issue to the logic startup handler.
logic::noCommand.SetInitError(logic::hwSanity.Error()); 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 // Finally, enable RX receiver to start talking to the printer
mi::idler.Init(); hu::usart1.rx_enable();
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);
}
} }
void Panic(ErrorCode ec) { void Panic(ErrorCode ec) {