diff options
author | Vegard Storheil Eriksen <zyp@jvnv.net> | 2022-04-17 01:34:47 +0200 |
---|---|---|
committer | Vegard Storheil Eriksen <zyp@jvnv.net> | 2022-04-17 01:36:54 +0200 |
commit | 365c0ddd1a5fbf461a13cfdb7a6c0a7f5d4390c0 (patch) | |
tree | a7de316ab81bc0def5678b509db120cfac596a12 | |
parent | 2e31f5e437645bb6c21ace8c4fe9c435ff88bdec (diff) |
stm32_uart: Add more helper functions.
-rw-r--r-- | uart/stm32_uart.h | 50 |
1 files changed, 38 insertions, 12 deletions
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 <typename T> class STM32_UART_t : public mmio_ptr<T> { - static constexpr auto is_v1 = std::is_same_v<T, STM32_UART_reg_v1_t>; - static constexpr auto is_v2 = std::is_same_v<T, STM32_UART_reg_v2_t>; - static constexpr auto is_lpv1 = std::is_same_v<T, STM32_UART_reg_lpv1_t>; - public: using mmio_ptr<T>::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<T, STM32_UART_reg_v1_t>) { + return ptr()->SR & (1 << 7); + } else { + return ptr()->ISR & (1 << 7); + } + } + + bool rxne() const { + if constexpr (std::is_same_v<T, STM32_UART_reg_v1_t>) { + return ptr()->SR & (1 << 5); + } else { + return ptr()->ISR & (1 << 5); + } + } + + uint8_t read() const { + if constexpr (std::is_same_v<T, STM32_UART_reg_v1_t>) { + return ptr()->DR; + } else { + return ptr()->RDR; + } + } + + void write(uint8_t data) const { + if constexpr (std::is_same_v<T, STM32_UART_reg_v1_t>) { + 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); } }; |