Loading...
Searching...
No Matches
uart_base.h
/*
* Copyright (C) 2009-2012 Technische Universität Dresden.
* Copyright (C) 2023-2024 Kernkonzept GmbH.
* Author(s): Adam Lackorzynski <adam@os.inf.tu-dresden.de>
*
* License: see LICENSE.spdx (in this directory or the directories above)
*/
#pragma once
#include <stddef.h>
#include <l4/drivers/io_regblock.h>
#include "poll_timeout_counter.h"
namespace L4 {
class Uart
{
protected:
unsigned _mode;
unsigned _rate;
Io_register_block const *_regs;
public:
void *operator new (size_t, void* a)
{ return a; }
public:
typedef unsigned Transfer_mode;
typedef unsigned Baud_rate;
Uart()
: _mode(~0U), _rate(~0U)
{}
virtual bool startup(Io_register_block const *regs) = 0;
virtual ~Uart() {}
virtual void shutdown() = 0;
virtual bool change_mode(Transfer_mode m, Baud_rate r) = 0;
virtual int write(char const *s, unsigned long count,
bool blocking = true) const = 0;
Transfer_mode mode() const { return _mode; }
Baud_rate rate() const { return _rate; }
virtual bool enable_rx_irq(bool = true) { return false; }
virtual void irq_ack() {}
virtual int char_avail() const = 0;
virtual int get_char(bool blocking = true) const = 0;
protected:
template <typename Uart_driver, bool Timeout_guard = true>
int generic_write(char const *s, unsigned long count,
bool blocking = true) const
{
auto *self = static_cast<Uart_driver const*>(this);
unsigned long c;
for (c = 0; c < count; ++c)
{
if (!blocking && !self->tx_avail())
break;
if constexpr (Timeout_guard)
{
Poll_timeout_counter i(3000000);
while (i.test(!self->tx_avail()))
;
}
else
{
while (!self->tx_avail())
;
}
self->out_char(*s++);
}
if (blocking)
self->wait_tx_done();
return c;
}
};
} // namespace L4
L4::Poll_timeout_counter
Evaluate an expression for a maximum number of times.
Definition poll_timeout_counter.h:35
L4::Poll_timeout_counter::test
bool test(bool expression=true)
Evaluate the expression for a maximum number of times.
Definition poll_timeout_counter.h:61
L4::Uart::irq_ack
virtual void irq_ack()
Acknowledge a received interrupt.
Definition uart_base.h:108
L4::Uart::write
virtual int write(char const *s, unsigned long count, bool blocking=true) const =0
Transmit a number of characters.
L4::Uart::generic_write
int generic_write(char const *s, unsigned long count, bool blocking=true) const
Internal function transmitting each character one-after-another and finally waiting that the transmis...
Definition uart_base.h:141
L4::Uart::shutdown
virtual void shutdown()=0
Terminate the UART driver.
L4::Uart::rate
Baud_rate rate() const
Return the baud rate.
Definition uart_base.h:93
L4::Uart::char_avail
virtual int char_avail() const =0
Check if there is at least one character available for reading from the UART.
L4::Uart::change_mode
virtual bool change_mode(Transfer_mode m, Baud_rate r)=0
Set certain parameters of the UART.
L4::Uart::startup
virtual bool startup(Io_register_block const *regs)=0
Start the UART driver.
L4::Uart::enable_rx_irq
virtual bool enable_rx_irq(bool=true)
Enable the receive IRQ.
Definition uart_base.h:103
L4::Uart::get_char
virtual int get_char(bool blocking=true) const =0
Read a character from the UART.
L4::Uart::mode
Transfer_mode mode() const
Return the transfer mode.
Definition uart_base.h:86
L4
L4 low-level kernel interface.
Definition l4sys-l4f-groups.dox:50