Aruna
UART.h
Go to the documentation of this file.
1 //
2 // Created by noeel on 16-11-20.
3 //
4 
5 #ifndef ARUNA_UART_H
6 #define ARUNA_UART_H
7 
8 #include <pthread.h>
9 #include <cstdint>
10 #include <aruna/comm/Link.h>
11 #include "aruna/arunaTypes.h"
12 
13 namespace aruna {
14  namespace driver {
15  class UART: public aruna::comm::Link {
16  public:
17  enum class flowcontrol_t {
18  HARDWARE,
19  SOFTWARE,
21  DISABLED,
22  NONE,
23  };
24  enum class parity_t {
25  EVEN,
26  ODD,
27  DISABLED,
28  NONE,
29  };
30  enum class stop_bit_t {
31  ONE,
32  ONE_HALF,
33  TWO,
34  NONE,
35  };
36  enum class word_length_t {
37  FIVE,
38  SIX,
39  SEVEN,
40  EIGHT,
41  NONE
42  };
43  private:
44  uint32_t baudrate = 0;
49  pthread_mutex_t write_atomic;
50  pthread_mutex_t read_atomic;
51 
52 // TODO _read and _write are no exposed by comm::Link::transmit/receive. No so safe! Can be fixed with an IO abstraction layer
58  virtual uint32_t _set_baudrate(uint32_t new_baudrate) = 0;
59 
65  virtual err_t _set_flowcontrol(flowcontrol_t new_flowcontrol) = 0;
66 
72  virtual err_t _set_parity(parity_t parity_bit) = 0;
73 
79  virtual err_t _set_stop_bit(stop_bit_t stop_bit) = 0;
80 
86  virtual err_t _set_word_length(word_length_t word_length) = 0;
87 
88  protected:
89 
90  const static uint8_t XON = 0x11;
91  const static uint8_t XOFF = 0x13;
92  public:
93 
97  UART();
98 
99  ~UART();
100 
107  size_t write(uint8_t *data, size_t dataSize);
108 
115  size_t try_write(uint8_t *data, size_t dataSize);
116 // TODO add timed_write with mutex timeout.
117 // TODO add (try_)write/read and lock to IO abstraction layer.
124  size_t read(uint8_t *buffer, size_t length);
125 
132  size_t try_read(uint8_t *buffer, size_t length);
133 // TODO add timed_read with mutex timeout.
134 
139  virtual uint32_t get_read_buffer_length() = 0;
140 // TODO get total buffer length.
141 
147  uint32_t set_baudrate(uint32_t new_baudrate);
148 
153  virtual uint32_t get_baudrate();
154 
160  err_t set_flowcontrol(flowcontrol_t new_flowcontrol);
161 
166  virtual flowcontrol_t get_flowcontrol();
167 
173  err_t set_parity(parity_t parity_bit);
174 
179  virtual parity_t get_parity();
180 
186  err_t set_stop_bit(stop_bit_t stop_bit);
187 
192  virtual stop_bit_t get_stop_bit();
193 
199  err_t set_word_length(word_length_t word_length);
200 
205  virtual word_length_t get_word_length();
206 
207  uint32_t get_speed() override;
208 
209 // TODO add support to lock UART, simular to I2C address locking.
210 // TODO add support for interupts. Simulair to https://github.com/espressif/esp-idf/blob/master/examples/peripherals/uart/uart_events/main/uart_events_example_main.c
211 
212 
213 
214 
215 
216  };
217  }
218 }
219 
220 
221 #endif //ARUNA_UART_H
uint32_t set_baudrate(uint32_t new_baudrate)
Set baudrate of the UART.
Definition: UART.cpp:27
Definition: comm.cpp:14
flowcontrol_t flowcontrol
Definition: UART.h:45
size_t read(uint8_t *buffer, size_t length)
read data from the UART.
Definition: UART.cpp:19
UART()
UART object to write and read data.
Definition: UART.cpp:71
word_length_t wordLength
Definition: UART.h:48
err_t set_word_length(word_length_t word_length)
Set UART data bits length.
Definition: UART.cpp:107
uint32_t get_speed() override
Get speed of link (bits per second)
Definition: UART.cpp:118
pthread_mutex_t read_atomic
Definition: UART.h:50
err_t set_parity(parity_t parity_bit)
Set parity bit.
Definition: UART.cpp:49
virtual err_t _set_word_length(word_length_t word_length)=0
Set UART data bits length.
uint32_t baudrate
Definition: UART.h:44
virtual uint32_t _set_baudrate(uint32_t new_baudrate)=0
Set baudrate of the UART.
stop_bit_t stopBit
Definition: UART.h:47
static const uint8_t XON
Definition: UART.h:90
pthread_mutex_t write_atomic
Definition: UART.h:49
size_t try_read(uint8_t *buffer, size_t length)
read data from the UART.
Definition: UART.cpp:98
virtual err_t _set_stop_bit(stop_bit_t stop_bit)=0
set stop bit
Link * driver
stores the driver.
Definition: comm.cpp:45
err_t set_stop_bit(stop_bit_t stop_bit)
set stop bit
Definition: UART.cpp:60
size_t write(uint8_t *data, size_t dataSize)
Write data to the UART.
Definition: UART.cpp:10
parity_t parity
Definition: UART.h:46
virtual uint32_t get_read_buffer_length()=0
Get amount of bytes in the read buffer.
size_t try_write(uint8_t *data, size_t dataSize)
Write data to the UART.
Definition: UART.cpp:89
virtual err_t _set_parity(parity_t parity_bit)=0
Set parity bit.
virtual flowcontrol_t get_flowcontrol()
get flowcontrol of UART
Definition: UART.cpp:45
virtual parity_t get_parity()
Get parity of UART.
Definition: UART.cpp:56
virtual stop_bit_t get_stop_bit()
get stop bit
Definition: UART.cpp:67
virtual word_length_t get_word_length()
Set UART data bits length.
Definition: UART.cpp:114
virtual err_t _set_flowcontrol(flowcontrol_t new_flowcontrol)=0
Set software or hardware flowcontrol.
err_t set_flowcontrol(flowcontrol_t new_flowcontrol)
Set software or hardware flowcontrol.
Definition: UART.cpp:38
static const uint8_t XOFF
Definition: UART.h:91
virtual uint32_t get_baudrate()
Get baudrate of UART.
Definition: UART.cpp:34