FastArduino v1.10
C++ library to build fast but small Arduino/AVR projects
Loading...
Searching...
No Matches
uart_commons.h
Go to the documentation of this file.
1// Copyright 2016-2023 Jean-Francois Poilpret
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
16
21#ifndef UARTCOMMONS_HH
22#define UARTCOMMONS_HH
23
24#include <stdint.h>
25
32namespace serial
33{
37 enum class Parity : uint8_t
38 {
40 NONE = 0,
42 EVEN = 1,
44 ODD = 3
45 };
46
50 enum class StopBits : uint8_t
51 {
53 ONE = 1,
55 TWO = 2
56 };
57
62 enum class BufferHandling : uint8_t
63 {
65 KEEP = 0x00,
67 CLEAR = 0x01,
72 FLUSH = 0x02
73 };
74
76 union Errors
77 {
78 Errors() = default;
79
80 uint8_t has_errors = 0;
81 struct
82 {
83 bool frame_error : 1;
84 bool data_overrun : 1;
85 bool queue_overflow : 1;
86 bool parity_error : 1;
87 };
88 };
90
96 {
97 public:
99 UARTErrors() : errors_{} {}
100 UARTErrors(const UARTErrors& that) = default;
101 UARTErrors& operator=(const UARTErrors& that) = default;
103
108 {
109 errors_.has_errors = 0;
110 }
111
118 uint8_t has_errors() const
119 {
120 return errors_.has_errors;
121 }
122
128 bool frame_error() const
129 {
130 return errors_.frame_error;
131 }
132
138 bool data_overrun() const
139 {
140 return errors_.data_overrun;
141 }
142
148 bool queue_overflow() const
149 {
150 return errors_.queue_overflow;
151 }
152
158 bool parity_error() const
159 {
160 return errors_.parity_error;
161 }
162
163 protected:
165 Errors& errors()
166 {
167 return errors_;
168 }
170
171 private:
172 Errors errors_;
173 };
174
176 // Useful traits for checking a provided type (in template) is an UART and what traits it supports
177 template<typename> struct UART_trait
178 {
179 static constexpr bool IS_UART = false;
180 static constexpr bool IS_HW_UART = false;
181 static constexpr bool IS_SW_UART = false;
182 static constexpr bool HAS_TX = false;
183 static constexpr bool HAS_RX = false;
184 };
186};
187
188#endif /* UARTCOMMONS_HH */
Holder of latest UART errors.
Definition: uart_commons.h:96
bool frame_error() const
Indicate if a frame error has occurred.
Definition: uart_commons.h:128
bool data_overrun() const
Indicate if a data overrun has occurred.
Definition: uart_commons.h:138
bool queue_overflow() const
Indicate if a queue overflow has occurred.
Definition: uart_commons.h:148
void clear_errors()
Reset UART errors to no error.
Definition: uart_commons.h:107
uint8_t has_errors() const
Indicate if there are UART errors pending.
Definition: uart_commons.h:118
bool parity_error() const
Indicate if a parity error has occurred.
Definition: uart_commons.h:158
This namespace defines common errors that can be returned by some FastArduino API methods,...
Definition: errors.h:38
Defines all API for UART features.
Definition: soft_uart.h:84
BufferHandling
How the TX/RX buffer should be handled when ending transmission (see end() methods) on UATX/UARX.
Definition: uart_commons.h:63
@ FLUSH
Flush buffer before stopping transmission (buffer will be empty after calling end()).
@ KEEP
Stop transmission immediately, keep buffer as-is.
@ CLEAR
Stop transmission immediately, clear buffer.
StopBits
Number of stop bits used for serial transmission.
Definition: uart_commons.h:51
@ TWO
Two stop bits.
@ ONE
One stop bit.
Parity
Parity used for serial transmission.
Definition: uart_commons.h:38
@ ODD
Odd parity bit.
@ EVEN
Even parity bit.
@ NONE
No parity bit.