FastArduino  v1.8
C++ library to build fast but small Arduino/AVR projects
uart_commons.h
Go to the documentation of this file.
1 // Copyright 2016-2021 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 
32 namespace 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 
95  class UARTErrors
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 };
175 
176 #endif /* UARTCOMMONS_HH */
177 
serial::UARTErrors::queue_overflow
bool queue_overflow() const
Indicate if a queue overflow has occurred.
Definition: uart_commons.h:148
serial::Parity
Parity
Parity used for serial transmission.
Definition: uart_commons.h:38
serial
Defines all API for UART features.
Definition: soft_uart.h:84
serial::UARTErrors
Holder of latest UART errors.
Definition: uart_commons.h:96
serial::UARTErrors::clear_errors
void clear_errors()
Reset UART errors to no error.
Definition: uart_commons.h:107
serial::Parity::NONE
@ NONE
No parity bit.
serial::StopBits::ONE
@ ONE
One stop bit.
serial::StopBits
StopBits
Number of stop bits used for serial transmission.
Definition: uart_commons.h:51
serial::BufferHandling
BufferHandling
How the TX/RX buffer should be handled when ending transmission (see end() methods) on UATX/UARX.
Definition: uart_commons.h:63
errors
This namespace defines common errors that can be returned by some FastArduino API methods,...
Definition: errors.h:38
serial::UARTErrors::data_overrun
bool data_overrun() const
Indicate if a data overrun has occurred.
Definition: uart_commons.h:138
serial::UARTErrors::frame_error
bool frame_error() const
Indicate if a frame error has occurred.
Definition: uart_commons.h:128
serial::UARTErrors::has_errors
uint8_t has_errors() const
Indicate if there are UART errors pending.
Definition: uart_commons.h:118
serial::UARTErrors::parity_error
bool parity_error() const
Indicate if a parity error has occurred.
Definition: uart_commons.h:158
serial::BufferHandling::KEEP
@ KEEP
Stop transmission immediately, keep buffer as-is.