summaryrefslogtreecommitdiff
path: root/ucoo
diff options
context:
space:
mode:
authorNicolas Schodet2015-10-31 14:40:04 +0100
committerNicolas Schodet2019-10-07 00:44:57 +0200
commit4fd623cd267ecfb1663be4e7c8b56c55201c3c82 (patch)
treef2169aa7b2420495c5dd9f55662a7e6d8d0e2921 /ucoo
parent2cda37290deeed9ace58dc59ea1f169643f6cbd0 (diff)
ucoo/hal/uart: add event handler
Diffstat (limited to 'ucoo')
-rw-r--r--ucoo/hal/uart/uart.stm32.cc8
-rw-r--r--ucoo/hal/uart/uart.stm32.hh8
2 files changed, 16 insertions, 0 deletions
diff --git a/ucoo/hal/uart/uart.stm32.cc b/ucoo/hal/uart/uart.stm32.cc
index 96e726d..9fdeee9 100644
--- a/ucoo/hal/uart/uart.stm32.cc
+++ b/ucoo/hal/uart/uart.stm32.cc
@@ -198,14 +198,22 @@ Uart::isr (int n)
if (sr & USART_SR_RXNE)
{
if (!uart.rx_fifo_.full ())
+ {
+ bool was_empty = uart.rx_fifo_.empty ();
uart.rx_fifo_.push (dr);
+ if (was_empty && uart.handler_)
+ uart.handler_ (true);
+ }
}
if (sr & USART_SR_TXE)
{
+ bool was_full = uart.tx_fifo_.full ();
if (!uart.tx_fifo_.empty ())
USART_DR (base) = static_cast<uint8_t> (uart.tx_fifo_.pop ());
if (uart.tx_fifo_.empty ())
USART_CR1 (base) &= ~USART_CR1_TXEIE;
+ if (was_full && uart.handler_)
+ uart.handler_ (false);
}
}
diff --git a/ucoo/hal/uart/uart.stm32.hh b/ucoo/hal/uart/uart.stm32.hh
index 7d74946..a38d4d6 100644
--- a/ucoo/hal/uart/uart.stm32.hh
+++ b/ucoo/hal/uart/uart.stm32.hh
@@ -25,6 +25,7 @@
// }}}
#include "ucoo/intf/stream.hh"
#include "ucoo/utils/fifo.hh"
+#include "ucoo/utils/function.hh"
#include "config/ucoo/hal/uart.hh"
@@ -50,6 +51,11 @@ class Uart : public Stream
void enable (int speed, Parity parity = Parity::NONE, int stop_bits = 1);
/// Disable.
void disable ();
+ /// Register a function to be called on events (new data available or room
+ /// available in TX buffer). Handler first parameter is true if new data
+ /// is available.
+ template<typename F>
+ void register_event (F handler) { handler_ = handler; }
/// Change the error character.
void set_error_char (char c);
/// See Stream::read.
@@ -71,6 +77,8 @@ class Uart : public Stream
char error_char_;
/// Is it enabled?
bool enabled_;
+ /// Handler called on event.
+ Function<void (bool)> handler_;
};
} // namespace ucoo