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{
20 class Uart
21 {
22 protected:
23 unsigned _mode;
24 unsigned _rate;
25 Io_register_block const *_regs;
26
27 public:
28 void *operator new (size_t, void* a)
29 { return a; }
30
31 public:
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
76 virtual int get_char(bool blocking = true) const = 0;
77
84 virtual int char_avail() const = 0;
85
96 virtual int write(char const *s, unsigned long count,
97 bool blocking = true) const = 0;
98
102 virtual void irq_ack() {}
103
111 virtual bool enable_rx_irq(bool = true) { return false; }
112
118 Transfer_mode mode() const { return _mode; }
119
125 Baud_rate rate() const { return _rate; }
126
127 protected:
139 template <typename Uart_driver, bool Timeout_guard = true>
140 int generic_write(char const *s, unsigned long count,
141 bool blocking = true) const
142 {
143 auto *self = static_cast<Uart_driver const*>(this);
144
145 unsigned long c;
146 for (c = 0; c < count; ++c)
147 {
148 if (!blocking && !self->tx_avail())
149 break;
150
151 if constexpr (Timeout_guard)
152 {
153 Poll_timeout_counter i(3000000);
154 while (i.test(!self->tx_avail()))
155 ;
156 }
157 else
158 {
159 while (!self->tx_avail())
160 ;
161 }
162
163 self->out_char(*s++);
164 }
165
166 if (blocking)
167 self->wait_tx_done();
168
169 return c;
170 }
171 };
172}
Evaluate an expression for a maximum number of times.
bool test(bool expression=true)
Evaluate the expression for a maximum number of times.
Uart driver abstraction.
Definition uart_base.h:21
virtual void irq_ack()
Acknowledge a received interrupt.
Definition uart_base.h:102
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:140
virtual void shutdown()=0
Terminate the UART driver.
Baud_rate rate() const
Return the baud rate.
Definition uart_base.h:125
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:111
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:118
L4 low-level kernel interface.