L4Re Operating System Framework – Interface and Usage Documentation
Loading...
Searching...
No Matches
uart_base.h
1/* SPDX-License-Identifier: GPL-2.0-only OR License-Ref-kk-custom */
2/*
3 * Copyright (C) 2023 Kernkonzept GmbH.
4 */
5/*
6 * (c) 2009-2012 Adam Lackorzynski <adam@os.inf.tu-dresden.de>
7 * economic rights: Technische Universität Dresden (Germany)
8 *
9 * This file is part of TUD:OS and distributed under the terms of the
10 * GNU General Public License 2.
11 * Please see the COPYING-GPL-2 file for details.
12 */
13#pragma once
14
15#include <stddef.h>
16#include <l4/drivers/io_regblock.h>
17
18#include "poll_timeout_counter.h"
19
20namespace L4
21{
25 class Uart
26 {
27 protected:
28 unsigned _mode;
29 unsigned _rate;
30 Io_register_block const *_regs;
31
32 public:
33 void *operator new (size_t, void* a)
34 { return a; }
35
36 public:
37 typedef unsigned Transfer_mode;
38 typedef unsigned Baud_rate;
39
40 Uart()
41 : _mode(~0U), _rate(~0U)
42 {}
43
51 virtual bool startup(Io_register_block const *regs) = 0;
52
53 virtual ~Uart() {}
54
58 virtual void shutdown() = 0;
59
71 virtual bool change_mode(Transfer_mode m, Baud_rate r) = 0;
72
81 virtual int get_char(bool blocking = true) const = 0;
82
89 virtual int char_avail() const = 0;
90
101 virtual int write(char const *s, unsigned long count,
102 bool blocking = true) const = 0;
103
107 virtual void irq_ack() {}
108
116 virtual bool enable_rx_irq(bool = true) { return false; }
117
123 Transfer_mode mode() const { return _mode; }
124
130 Baud_rate rate() const { return _rate; }
131
132 protected:
144 template <typename Uart_driver>
145 int generic_write(char const *s, unsigned long count,
146 bool blocking = true) const
147 {
148 auto *self = static_cast<Uart_driver const*>(this);
149
150 unsigned long c;
151 for (c = 0; c < count; ++c)
152 {
153 if (!blocking && !self->tx_avail())
154 break;
155
156 Poll_timeout_counter i(3000000);
157 while (i.test(!self->tx_avail()))
158 ;
159
160 self->out_char(*s++);
161 }
162
163 if (blocking)
164 self->wait_tx_done();
165
166 return c;
167 }
168 };
169}
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:26
virtual void irq_ack()
Acknowledge a received interrupt.
Definition uart_base.h:107
virtual int write(char const *s, unsigned long count, bool blocking=true) const =0
Transmit a number of characters.
virtual void shutdown()=0
Terminate the UART driver.
Baud_rate rate() const
Return the baud rate.
Definition uart_base.h:130
virtual int char_avail() const =0
Check if there is at least one character available for reading from the UART.
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:145
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:116
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:123
L4 low-level kernel interface.