From e82789253b7c915270cfdc126e8e000924d90444 Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Fri, 4 May 2018 20:47:39 +0200 Subject: Implement TX read(n) and readline() (not working yet) --- hal/hwconfig.hpp | 8 ++++---- hal/uart.hpp | 16 ++++++++++++---- hal/uart.tpp | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- main.cpp | 12 ++++++++---- 4 files changed, 75 insertions(+), 13 deletions(-) diff --git a/hal/hwconfig.hpp b/hal/hwconfig.hpp index 0cd38ac..2195b8e 100644 --- a/hal/hwconfig.hpp +++ b/hal/hwconfig.hpp @@ -5,18 +5,18 @@ namespace hw { - void reglock(); - void regunlock(); + void reglock(); + void regunlock(); } namespace osc { - void initialize(); + void initialize(); } namespace interrupts { - void initialize(); + void initialize(); } #endif \ No newline at end of file diff --git a/hal/uart.hpp b/hal/uart.hpp index 0be5835..60dd5bb 100644 --- a/hal/uart.hpp +++ b/hal/uart.hpp @@ -13,10 +13,10 @@ #include extern "C" { -void usart_1_isr(); -void usart_2_isr(); -void usart_3_isr(); -void usart_4_isr(); + void usart_1_isr(); + void usart_2_isr(); + void usart_3_isr(); + void usart_4_isr(); } namespace uart @@ -52,8 +52,16 @@ namespace uart template uint8_t read(); +#if 0 + template + std::string read(const unsigned len); + + template + std::string readline(const std::string &eol = "\n\r"); + template unsigned read(uint8_t *buffer, const unsigned numbytes); +#endif template void write(const uint8_t byte); diff --git a/hal/uart.tpp b/hal/uart.tpp index e4b722e..148ff7d 100644 --- a/hal/uart.tpp +++ b/hal/uart.tpp @@ -25,18 +25,23 @@ extern "C" { /* templated functions */ namespace uart { + // access buffers template inline std::queue& rx_buffer() { + static_assert(dev <= devices_count, "invalid device number"); return uart::_rx_buffer[dev -1]; } template inline std::queue& tx_buffer() { + static_assert(dev <= devices_count, "invalid device number"); return uart::_tx_buffer[dev -1]; } + + // read from serial device template uint8_t read() { @@ -46,6 +51,47 @@ namespace uart return byte; } +#if 0 + template + std::string read(const unsigned len) + { + std::string str = ""; + unsigned i = len; + + if (rx_buffer().size() < len) { + i = rx_buffer().size(); + } + + while (i--) { + str += read(); + } + + return str; + } + + template + std::string readline(const std::string& eol = "\n\r") + { + int eol_c = 0; + + std::string str = ""; + unsigned i = rx_buffer().size(); + + while (i--) { + str += read(); + + // compare string endings + // TODO: make it more efficient + if (eol_c == eol.size()) { + break; + } + + if (str.back() == eol[eol_c]) { + eol_c++; + } + } + } + template unsigned read(uint8_t *buffer, const unsigned numbytes) { @@ -56,7 +102,10 @@ namespace uart *(bptr++) = read(); } } +#endif + + // write to serial device template void write(const uint8_t byte) { @@ -94,10 +143,11 @@ namespace uart /* specializations for UART1 */ -// debug +#ifdef DEBUG #include "pin.tpp" pin<2> led_blue(&LATBbits, &TRISBbits, &PORTBbits); pin<3> led_green(&LATBbits, &TRISBbits, &PORTBbits); +#endif void __ISR(_UART_1_VECTOR, IPL1AUTO) usart_1_isr() { diff --git a/main.cpp b/main.cpp index d95bc74..860460c 100644 --- a/main.cpp +++ b/main.cpp @@ -5,7 +5,7 @@ * Created on May 1, 2018, 6:18 PM */ -#include +#define DEBUG // basic devices #include "hal/confbits.hpp" @@ -15,6 +15,10 @@ #include "hal/uart.tpp" #include "hal/pin.tpp" +// standard library +#include + +// microchip libraries extern "C" { #include // #include @@ -40,9 +44,9 @@ int main(int argc, char** argv) while (true) { while (uart::rx_buffer<1>().empty()); - - while (!uart::rx_buffer<1>().empty()) { - uint8_t c = uart::read<1>(); + while (uart::rx_buffer<1>().size() > 0) { + char c = uart::read<1>(); + uart::write<1>(c); uart::write<1>("\n\r"); } -- cgit v1.2.1