summaryrefslogtreecommitdiffstats
path: root/hal/uart.tpp
diff options
context:
space:
mode:
authorNao Pross <naopross@thearcway.org>2018-05-04 20:47:39 +0200
committerNao Pross <naopross@thearcway.org>2018-05-04 20:47:39 +0200
commite82789253b7c915270cfdc126e8e000924d90444 (patch)
treea58d4194d22e507afe506159b12c0cb2d182be7f /hal/uart.tpp
parentImplement uart TX features (diff)
downloadSAMLiquidSmoke-e82789253b7c915270cfdc126e8e000924d90444.tar.gz
SAMLiquidSmoke-e82789253b7c915270cfdc126e8e000924d90444.zip
Implement TX read(n) and readline() (not working yet)
Diffstat (limited to '')
-rw-r--r--hal/uart.tpp52
1 files changed, 51 insertions, 1 deletions
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<unsigned dev>
inline std::queue<uint8_t>& rx_buffer()
{
+ static_assert(dev <= devices_count, "invalid device number");
return uart::_rx_buffer[dev -1];
}
template<unsigned dev>
inline std::queue<uint8_t>& tx_buffer()
{
+ static_assert(dev <= devices_count, "invalid device number");
return uart::_tx_buffer[dev -1];
}
+
+ // read from serial device
template<unsigned dev>
uint8_t read()
{
@@ -46,6 +51,47 @@ namespace uart
return byte;
}
+#if 0
+ template<unsigned dev>
+ std::string read(const unsigned len)
+ {
+ std::string str = "";
+ unsigned i = len;
+
+ if (rx_buffer<dev>().size() < len) {
+ i = rx_buffer<dev>().size();
+ }
+
+ while (i--) {
+ str += read<dev>();
+ }
+
+ return str;
+ }
+
+ template<unsigned dev>
+ std::string readline(const std::string& eol = "\n\r")
+ {
+ int eol_c = 0;
+
+ std::string str = "";
+ unsigned i = rx_buffer<dev>().size();
+
+ while (i--) {
+ str += read<dev>();
+
+ // compare string endings
+ // TODO: make it more efficient
+ if (eol_c == eol.size()) {
+ break;
+ }
+
+ if (str.back() == eol[eol_c]) {
+ eol_c++;
+ }
+ }
+ }
+
template<unsigned dev>
unsigned read(uint8_t *buffer, const unsigned numbytes)
{
@@ -56,7 +102,10 @@ namespace uart
*(bptr++) = read<dev>();
}
}
+#endif
+
+ // write to serial device
template<unsigned dev>
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()
{