L4Re Operating System Framework
Interface and Usage Documentation
Loading...
Searching...
No Matches
uart_base.h
1/*
2 * Copyright (C) 2009-2012 Technische Universität Dresden.
3 * Copyright (C) 2023-2024 Kernkonzept GmbH.
4 * Author(s): Adam Lackorzynski <adam@os.inf.tu-dresden.de>
5 *
6 * License: see LICENSE.spdx (in this directory or the directories above)
7 */
8#pragma once
9
10#include <stddef.h>
11#include <l4/drivers/io_regblock.h>
12
13#include "poll_timeout_counter.h"
14
15namespace L4 {
16
20class Uart
21{
22protected:
23 unsigned _mode;
24 unsigned _rate;
25 Io_register_block const *_regs;
26
27public:
28 void *operator new (size_t, void* a)
29 { return a; }
30
31public:
32 typedef unsigned Transfer_mode;
33 typedef unsigned Baud_rate;
34
35 Uart()
36 : _mode(~0U), _rate(~0U)
37 {}
38
46 virtual bool startup(Io_register_block const *regs) = 0;
47
48 virtual ~Uart() {}
49
53 virtual void shutdown() = 0;
54
66 virtual bool change_mode(Transfer_mode m, Baud_rate r) = 0;
67
78 virtual int write(char const *s, unsigned long count,
79 bool blocking = true) const = 0;
80
86 Transfer_mode mode() const { return _mode; }
87
93 Baud_rate rate() const { return _rate; }
94
95
103 virtual bool enable_rx_irq(bool = true) { return false; }
104
108 virtual void irq_ack() {}
109
116 virtual int char_avail() const = 0;
117
126 virtual int get_char(bool blocking = true) const = 0;
127
128protected:
140 template <typename Uart_driver, bool Timeout_guard = true>
141 int generic_write(char const *s, unsigned long count,
142 bool blocking = true) const
143 {
144 auto *self = static_cast<Uart_driver const*>(this);
145
146 unsigned long c;
147 for (c = 0; c < count; ++c)
148 {
149 if (!blocking && !self->tx_avail())
150 break;
151
152 if constexpr (Timeout_guard)
153 {
154 Poll_timeout_counter i(3000000);
155 while (i.test(!self->tx_avail()))
156 ;
157 }
158 else
159 {
160 while (!self->tx_avail())
161 ;
162 }
163
164 self->out_char(*s++);
165 }
166
167 if (blocking)
168 self->wait_tx_done();
169
170 return c;
171 }
172};
173
174} // namespace L4
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.
Definition uart_base.h:108
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...
Definition uart_base.h:141
virtual void shutdown()=0
Terminate the UART driver.
Baud_rate rate() const
Return the baud rate.
Definition uart_base.h:93
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.
Definition uart_base.h:103
virtual int get_char(bool blocking=true) const =0
Read a character from the UART.
Transfer_mode mode() const
Return the transfer mode.
Definition uart_base.h:86
L4 low-level kernel interface.