From 9809b34bcaa6df68bdbf0172af1db606382b3f7d Mon Sep 17 00:00:00 2001 From: BlackMark Date: Sun, 28 Jul 2019 17:33:42 +0200 Subject: [PATCH] Implemented optimal example to compare implementations --- uart/flash | 2 +- uart/main.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++----- uart/uart | 2 +- uart/uart.cppproj | 4 ++-- 4 files changed, 55 insertions(+), 10 deletions(-) diff --git a/uart/flash b/uart/flash index b0f602b..2215491 160000 --- a/uart/flash +++ b/uart/flash @@ -1 +1 @@ -Subproject commit b0f602b3be5608d71a401f5c75609f09e92df4a9 +Subproject commit 2215491c0b0606d734dcf44e6c4213e36176d25f diff --git a/uart/main.cpp b/uart/main.cpp index ef0210b..af3ad06 100644 --- a/uart/main.cpp +++ b/uart/main.cpp @@ -214,8 +214,10 @@ void newUartUsage() uart::uart serial; uart::uart softSerial; - serial << "Hello World using finalized interface!" << F("\r\nAlso works from progmem\r\n"); + serial << "Hello World from RAM. " << F("Hello World from flash\r\n"); // softSerial << "Hello World using finalized software interface!" << F("\r\nAlso greetz from progmem\r\n"); + + _delay_ms(1000); } void oldUsartUsage() @@ -223,8 +225,8 @@ void oldUsartUsage() USART0 &serial = USART0::inst(); serial.init(9600); - serial << "Hello World from hardware serial 0!" - << " And greets from ram:(!\r\n"; + serial << "Hello World from RAM. " + << "Hello World from flash\r\n"; _delay_ms(1000); } @@ -304,13 +306,56 @@ void spiTest() spi::spi uartSpi; } +static inline void initUart(const uint32_t baudRate) +{ + UBRR0 = static_cast((F_CPU / (16 * baudRate)) - 1); + UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); + UCSR0B = (1 << RXEN0) | (1 << TXEN0); +} + +static inline void txUart(uint8_t byte) +{ + while (!(UCSR0A & (1 << UDRE0))) + ; + + UDR0 = byte; +} + +static inline void txString(const char *str) +{ + while (char ch = *str++) + txUart(ch); +} + +static inline void txString(const detail::FlashString *str) +{ + const char *strIt = reinterpret_cast(str); + + while (char ch = pgm_read_byte(strIt++)) + txUart(ch); +} + +void optimalUartTest() +{ + auto ramString = "Hello World from RAM. "; + auto flashString = F("Hello World from flash\r\n"); + + initUart(9600); + + txString(ramString); + txString(flashString); + + _delay_ms(1000); +} + int main() { - usartTestUsage(); + // usartTestUsage(); newUartUsage(); - oldUsartUsage(); + // oldUsartUsage(); + // optimalUartTest(); - spiTest(); + // spiTest(); return 0; } diff --git a/uart/uart b/uart/uart index fd7e8e7..8e0ba5a 160000 --- a/uart/uart +++ b/uart/uart @@ -1 +1 @@ -Subproject commit fd7e8e72389dbef3738822974393345a7c981730 +Subproject commit 8e0ba5a46347beec2ba26e03e2956574577096bf diff --git a/uart/uart.cppproj b/uart/uart.cppproj index 31aafa1..9abeafa 100644 --- a/uart/uart.cppproj +++ b/uart/uart.cppproj @@ -202,9 +202,9 @@ compile - + compile - + compile