From 365c0ddd1a5fbf461a13cfdb7a6c0a7f5d4390c0 Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Sun, 17 Apr 2022 01:34:47 +0200 Subject: stm32_uart: Add more helper functions. --- uart/stm32_uart.h | 50 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 12 deletions(-) (limited to 'uart/stm32_uart.h') diff --git a/uart/stm32_uart.h b/uart/stm32_uart.h index 6db8305..5e4fd29 100644 --- a/uart/stm32_uart.h +++ b/uart/stm32_uart.h @@ -44,22 +44,48 @@ struct STM32_UART_reg_lpv1_t { template class STM32_UART_t : public mmio_ptr { - static constexpr auto is_v1 = std::is_same_v; - static constexpr auto is_v2 = std::is_same_v; - static constexpr auto is_lpv1 = std::is_same_v; - public: using mmio_ptr::ptr; - void write_blocking(uint8_t data) const requires is_v1 { - // wait for TXE/TXFNF - while (!(ptr()->SR & (1<<7))); - ptr()->DR = data; + bool txe() const { + if constexpr (std::is_same_v) { + return ptr()->SR & (1 << 7); + } else { + return ptr()->ISR & (1 << 7); + } + } + + bool rxne() const { + if constexpr (std::is_same_v) { + return ptr()->SR & (1 << 5); + } else { + return ptr()->ISR & (1 << 5); + } + } + + uint8_t read() const { + if constexpr (std::is_same_v) { + return ptr()->DR; + } else { + return ptr()->RDR; + } + } + + void write(uint8_t data) const { + if constexpr (std::is_same_v) { + ptr()->DR = data; + } else { + ptr()->TDR = data; + } + } + + uint8_t read_blocking() const { + while(!rxne()); + return read(); } - void write_blocking(uint8_t data) const requires is_v2 || is_lpv1 { - // wait for TXE/TXFNF - while (!(ptr()->ISR & (1<<7))); - ptr()->TDR = data; + void write_blocking(uint8_t data) const { + while(!txe()); + write(data); } }; -- cgit v1.2.3