11#include <l4/drivers/io_regblock.h>
13#include <l4/drivers/device.h>
14#include "poll_timeout_counter.h"
21class Uart :
public Dev_obj
26 Io_register_block
const *_regs;
29 void *
operator new (size_t,
void *p) {
return p; }
32 typedef unsigned Transfer_mode;
33 typedef unsigned Baud_rate;
36 : _mode(~0U), _rate(~0U)
46 virtual bool startup(Io_register_block
const *regs) = 0;
78 virtual int write(
char const *s,
unsigned long count,
79 bool blocking =
true)
const = 0;
86 Transfer_mode
mode()
const {
return _mode; }
93 Baud_rate
rate()
const {
return _rate; }
95#ifndef UART_WITHOUT_INPUT
127 virtual int get_char(
bool blocking =
true)
const = 0;
143 template <
typename Uart_driver,
bool Timeout_guard = true>
145 bool blocking =
true)
const
147 auto *self =
static_cast<Uart_driver const*
>(
this);
150 for (c = 0; c < count; ++c)
152 if (!blocking && !self->tx_avail())
155 if constexpr (Timeout_guard)
158 while (i.
test(!self->tx_avail()))
163 while (!self->tx_avail())
167 self->out_char(*s++);
171 self->wait_tx_done();
184l4re_dev_uart_create_by_dt_compatible(
const char *dt_compatible,
185 void *obj_buf,
unsigned obj_buf_size,
188 L4::Dev_obj *dev = l4re_dev_create_by_dt_compatible(dt_compatible,
189 obj_buf, obj_buf_size,
191 return static_cast<L4::Uart *
>(dev);
199l4re_dev_uart_create_by_dt_compatible_once(
const char *dt_compatible,
unsigned freq)
201 static bool once_done =
false;
206 static char __attribute__((aligned(
sizeof(
long) * 2))) obj_buf[64];
208 = l4re_dev_uart_create_by_dt_compatible(dt_compatible,
209 obj_buf,
sizeof(obj_buf),
217#define l4re_register_device_uart(Device_class, instance_name, \
218 device_dt_ids, pci_ids) \
219 static const struct l4re_device_ids __dev_spec_##instance_name \
220 = { .dt = device_dt_ids, .pcidev = pci_ids }; \
221 l4re_register_device(L4::Uart, Device_class, instance_name, \
222 __dev_spec_##instance_name)
224#define l4re_register_device_uart_dt(Device_class, instance_name, dt_ids) \
225 l4re_register_device_uart(Device_class, instance_name, dt_ids, nullptr)
227#define l4re_register_device_uart_pci(Device_class, instance_name, pci_ids) \
228 l4re_register_device_uart(Device_class, instance_name, nullptr, pci_ids)
Evaluate an expression for a maximum number of times.
bool test(bool expression=true)
Evaluate the expression for a maximum number of times.
virtual void irq_ack()
Acknowledge a received interrupt.
virtual int write(char const *s, unsigned long count, bool blocking=true) const =0
Transmit a number of characters.
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...
virtual void shutdown()=0
Terminate the UART driver.
Baud_rate rate() const
Return the baud rate.
virtual int char_avail() const =0
Check if there is at least one character available for reading from the UART.
virtual bool change_mode(Transfer_mode m, Baud_rate r)=0
Set certain parameters of the UART.
virtual bool startup(Io_register_block const *regs)=0
Start the UART driver.
virtual bool enable_rx_irq(bool=true)
Enable the receive IRQ.
virtual int get_char(bool blocking=true) const =0
Read a character from the UART.
Transfer_mode mode() const
Return the transfer mode.
L4 low-level kernel interface.