Aruna
aruna::driver::UART Class Referenceabstract

#include <UART.h>

Inheritance diagram for aruna::driver::UART:
Collaboration diagram for aruna::driver::UART:

Public Types

enum  flowcontrol_t {
  flowcontrol_t::HARDWARE, flowcontrol_t::SOFTWARE, flowcontrol_t::HARDWARE_SOFTWARE, flowcontrol_t::DISABLED,
  flowcontrol_t::NONE
}
 
enum  parity_t { parity_t::EVEN, parity_t::ODD, parity_t::DISABLED, parity_t::NONE }
 
enum  stop_bit_t { stop_bit_t::ONE, stop_bit_t::ONE_HALF, stop_bit_t::TWO, stop_bit_t::NONE }
 
enum  word_length_t {
  word_length_t::FIVE, word_length_t::SIX, word_length_t::SEVEN, word_length_t::EIGHT,
  word_length_t::NONE
}
 

Public Member Functions

 UART ()
 UART object to write and read data. More...
 
 ~UART ()
 
size_t write (uint8_t *data, size_t dataSize)
 Write data to the UART. More...
 
size_t try_write (uint8_t *data, size_t dataSize)
 Write data to the UART. More...
 
size_t read (uint8_t *buffer, size_t length)
 read data from the UART. More...
 
size_t try_read (uint8_t *buffer, size_t length)
 read data from the UART. More...
 
virtual uint32_t get_read_buffer_length ()=0
 Get amount of bytes in the read buffer. More...
 
uint32_t set_baudrate (uint32_t new_baudrate)
 Set baudrate of the UART. More...
 
virtual uint32_t get_baudrate ()
 Get baudrate of UART. More...
 
err_t set_flowcontrol (flowcontrol_t new_flowcontrol)
 Set software or hardware flowcontrol. More...
 
virtual flowcontrol_t get_flowcontrol ()
 get flowcontrol of UART More...
 
err_t set_parity (parity_t parity_bit)
 Set parity bit. More...
 
virtual parity_t get_parity ()
 Get parity of UART. More...
 
err_t set_stop_bit (stop_bit_t stop_bit)
 set stop bit More...
 
virtual stop_bit_t get_stop_bit ()
 get stop bit More...
 
err_t set_word_length (word_length_t word_length)
 Set UART data bits length. More...
 
virtual word_length_t get_word_length ()
 Set UART data bits length. More...
 
uint32_t get_speed () override
 Get speed of link (bits per second) More...
 

Static Protected Attributes

static const uint8_t XON = 0x11
 
static const uint8_t XOFF = 0x13
 

Private Member Functions

virtual uint32_t _set_baudrate (uint32_t new_baudrate)=0
 Set baudrate of the UART. More...
 
virtual err_t _set_flowcontrol (flowcontrol_t new_flowcontrol)=0
 Set software or hardware flowcontrol. More...
 
virtual err_t _set_parity (parity_t parity_bit)=0
 Set parity bit. More...
 
virtual err_t _set_stop_bit (stop_bit_t stop_bit)=0
 set stop bit More...
 
virtual err_t _set_word_length (word_length_t word_length)=0
 Set UART data bits length. More...
 

Private Attributes

uint32_t baudrate = 0
 
flowcontrol_t flowcontrol = flowcontrol_t::NONE
 
parity_t parity = parity_t::NONE
 
stop_bit_t stopBit = stop_bit_t::NONE
 
word_length_t wordLength = word_length_t::NONE
 
pthread_mutex_t write_atomic
 
pthread_mutex_t read_atomic
 

Additional Inherited Members

Detailed Description

Definition at line 15 of file UART.h.

Member Enumeration Documentation

◆ flowcontrol_t

Enumerator
HARDWARE 
SOFTWARE 
HARDWARE_SOFTWARE 
DISABLED 
NONE 

Definition at line 17 of file UART.h.

17  {
18  HARDWARE,
19  SOFTWARE,
20  HARDWARE_SOFTWARE,
21  DISABLED,
22  NONE,
23  };

◆ parity_t

Enumerator
EVEN 
ODD 
DISABLED 
NONE 

Definition at line 24 of file UART.h.

24  {
25  EVEN,
26  ODD,
27  DISABLED,
28  NONE,
29  };

◆ stop_bit_t

Enumerator
ONE 
ONE_HALF 
TWO 
NONE 

Definition at line 30 of file UART.h.

30  {
31  ONE,
32  ONE_HALF,
33  TWO,
34  NONE,
35  };

◆ word_length_t

Enumerator
FIVE 
SIX 
SEVEN 
EIGHT 
NONE 

Definition at line 36 of file UART.h.

36  {
37  FIVE,
38  SIX,
39  SEVEN,
40  EIGHT,
41  NONE
42  };

Constructor & Destructor Documentation

◆ UART()

UART::UART ( )

UART object to write and read data.

Definition at line 71 of file UART.cpp.

71  {
72 // write
73  pthread_mutexattr_t write_attr;
74  pthread_mutexattr_init(&write_attr);
75  pthread_mutex_init(&write_atomic, &write_attr);
76  pthread_mutexattr_destroy(&write_attr);
77 // read
78  pthread_mutexattr_t read_attr;
79  pthread_mutexattr_init(&read_attr);
80  pthread_mutex_init(&read_atomic, &read_attr);
81  pthread_mutexattr_destroy(&read_attr);
82 }
pthread_mutex_t read_atomic
Definition: UART.h:50
pthread_mutex_t write_atomic
Definition: UART.h:49

◆ ~UART()

UART::~UART ( )

Definition at line 84 of file UART.cpp.

84  {
85  pthread_mutex_destroy(&write_atomic);
86  pthread_mutex_destroy(&read_atomic);
87 }
pthread_mutex_t read_atomic
Definition: UART.h:50
pthread_mutex_t write_atomic
Definition: UART.h:49

Member Function Documentation

◆ _set_baudrate()

virtual uint32_t aruna::driver::UART::_set_baudrate ( uint32_t  new_baudrate)
privatepure virtual

Set baudrate of the UART.

Parameters
new_baudratebaudrate to change to.
Returns
new baudrate

Implemented in aruna::driver::POSIX_UART, and aruna::driver::ESP32_UART.

Here is the caller graph for this function:

◆ _set_flowcontrol()

virtual err_t aruna::driver::UART::_set_flowcontrol ( flowcontrol_t  new_flowcontrol)
privatepure virtual

Set software or hardware flowcontrol.

Parameters
new_flowcontrol
Returns
err_t::OK if set correctly

Implemented in aruna::driver::POSIX_UART, and aruna::driver::ESP32_UART.

Here is the caller graph for this function:

◆ _set_parity()

virtual err_t aruna::driver::UART::_set_parity ( parity_t  parity_bit)
privatepure virtual

Set parity bit.

Parameters
parity_bitEVEN, UNEVEN or DISABLED
Returns
err_t::OK if no errors.

Implemented in aruna::driver::POSIX_UART, and aruna::driver::ESP32_UART.

Here is the caller graph for this function:

◆ _set_stop_bit()

virtual err_t aruna::driver::UART::_set_stop_bit ( stop_bit_t  stop_bit)
privatepure virtual

set stop bit

Parameters
stop_bit1 1.5 or 2 bits
Returns
err_t::OK if no errors

Implemented in aruna::driver::POSIX_UART, and aruna::driver::ESP32_UART.

Here is the caller graph for this function:

◆ _set_word_length()

virtual err_t aruna::driver::UART::_set_word_length ( word_length_t  word_length)
privatepure virtual

Set UART data bits length.

Parameters
word_lengthfive to eight bits
Returns
err_t::OK if success.

Implemented in aruna::driver::POSIX_UART, and aruna::driver::ESP32_UART.

Here is the caller graph for this function:

◆ get_baudrate()

uint32_t UART::get_baudrate ( )
virtual

Get baudrate of UART.

Returns
baudrate

Reimplemented in aruna::driver::ESP32_UART.

Definition at line 34 of file UART.cpp.

34  {
35  return baudrate;
36 }
uint32_t baudrate
Definition: UART.h:44
Here is the caller graph for this function:

◆ get_flowcontrol()

UART::flowcontrol_t UART::get_flowcontrol ( )
virtual

get flowcontrol of UART

Returns
flowcontrol

Reimplemented in aruna::driver::ESP32_UART.

Definition at line 45 of file UART.cpp.

45  {
46  return flowcontrol;
47 }
flowcontrol_t flowcontrol
Definition: UART.h:45

◆ get_parity()

UART::parity_t UART::get_parity ( )
virtual

Get parity of UART.

Returns
parity bit setting.

Reimplemented in aruna::driver::ESP32_UART.

Definition at line 56 of file UART.cpp.

56  {
57  return parity;
58 }
parity_t parity
Definition: UART.h:46

◆ get_read_buffer_length()

virtual uint32_t aruna::driver::UART::get_read_buffer_length ( )
pure virtual

Get amount of bytes in the read buffer.

Returns
number of bytes in read buffer.

Implemented in aruna::driver::ESP32_UART.

◆ get_speed()

uint32_t UART::get_speed ( )
overridevirtual

Get speed of link (bits per second)

Returns
unsigned int speed in Bytes per second.

Implements aruna::comm::Link.

Definition at line 118 of file UART.cpp.

118  {
119  return get_baudrate();
120 }
virtual uint32_t get_baudrate()
Get baudrate of UART.
Definition: UART.cpp:34
Here is the call graph for this function:

◆ get_stop_bit()

UART::stop_bit_t UART::get_stop_bit ( )
virtual

get stop bit

Returns
ONE ONE_HALF or TWO

Reimplemented in aruna::driver::ESP32_UART.

Definition at line 67 of file UART.cpp.

67  {
68  return stopBit;
69 }
stop_bit_t stopBit
Definition: UART.h:47

◆ get_word_length()

UART::word_length_t UART::get_word_length ( )
virtual

Set UART data bits length.

Returns
word length.

Reimplemented in aruna::driver::ESP32_UART.

Definition at line 114 of file UART.cpp.

114  {
115  return wordLength;
116 }
word_length_t wordLength
Definition: UART.h:48

◆ read()

size_t UART::read ( uint8_t *  buffer,
size_t  length 
)

read data from the UART.

Parameters
bufferpointer to buffer where UART read data will be written.
lengthamount of bytes to read.
Returns
amount of bytes read.

Definition at line 19 of file UART.cpp.

19  {
20  size_t ret = 0;
21  pthread_mutex_lock(&read_atomic);
22  ret = receive(buffer, length);
23  pthread_mutex_unlock(&read_atomic);
24  return ret;
25 }
pthread_mutex_t read_atomic
Definition: UART.h:50
Here is the call graph for this function:

◆ set_baudrate()

uint32_t UART::set_baudrate ( uint32_t  new_baudrate)

Set baudrate of the UART.

Parameters
new_baudratebaudrate to change to.
Returns
new baudrate

Definition at line 27 of file UART.cpp.

27  {
28  uint32_t ret = 0;
29  ret = _set_baudrate(new_baudrate);
30  this->baudrate = ret == new_baudrate ? new_baudrate : baudrate;
31  return ret;
32 }
uint32_t baudrate
Definition: UART.h:44
virtual uint32_t _set_baudrate(uint32_t new_baudrate)=0
Set baudrate of the UART.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_flowcontrol()

err_t UART::set_flowcontrol ( UART::flowcontrol_t  new_flowcontrol)

Set software or hardware flowcontrol.

Parameters
new_flowcontrol
Returns
err_t::OK if set correctly

Definition at line 38 of file UART.cpp.

38  {
39  err_t ret = err_t::OK;
40  ret = _set_flowcontrol(new_flowcontrol);
41  this->flowcontrol = ret != err_t::OK ? this->flowcontrol : new_flowcontrol;
42  return ret;
43 }
flowcontrol_t flowcontrol
Definition: UART.h:45
virtual err_t _set_flowcontrol(flowcontrol_t new_flowcontrol)=0
Set software or hardware flowcontrol.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_parity()

err_t UART::set_parity ( UART::parity_t  parity_bit)

Set parity bit.

Parameters
parity_bitEVEN, UNEVEN or DISABLED
Returns
err_t::OK if no errors.

Definition at line 49 of file UART.cpp.

49  {
50  err_t ret = err_t::OK;
51  ret = _set_parity(parity_bit);
52  this->parity = ret != err_t::OK ? this->parity : parity_bit;
53  return ret;
54 }
parity_t parity
Definition: UART.h:46
virtual err_t _set_parity(parity_t parity_bit)=0
Set parity bit.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_stop_bit()

err_t UART::set_stop_bit ( UART::stop_bit_t  stop_bit)

set stop bit

Parameters
stop_bit1 1.5 or 2 bits
Returns
err_t::OK if no errors

Definition at line 60 of file UART.cpp.

60  {
61  err_t ret = err_t::OK;
62  ret = _set_stop_bit(stop_bit);
63  this->stopBit = ret != err_t::OK ? this->stopBit : stop_bit;
64  return ret;
65 }
stop_bit_t stopBit
Definition: UART.h:47
virtual err_t _set_stop_bit(stop_bit_t stop_bit)=0
set stop bit
Here is the call graph for this function:

◆ set_word_length()

err_t UART::set_word_length ( UART::word_length_t  word_length)

Set UART data bits length.

Parameters
word_lengthfive to eight bits
Returns
err_t::OK if success.

Definition at line 107 of file UART.cpp.

107  {
108  err_t ret = err_t::OK;
109  ret = _set_word_length(word_length);
110  this->wordLength = ret != err_t::OK ? this->wordLength : wordLength;
111  return ret;
112 }
word_length_t wordLength
Definition: UART.h:48
virtual err_t _set_word_length(word_length_t word_length)=0
Set UART data bits length.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ try_read()

size_t UART::try_read ( uint8_t *  buffer,
size_t  length 
)

read data from the UART.

Return of data os already being read.

Parameters
bufferpointer to buffer where UART read data will be written.
lengthamount of bytes to read.
Returns
amount of bytes read.

Definition at line 98 of file UART.cpp.

98  {
99  size_t ret = 0;
100  if (!pthread_mutex_trylock(&read_atomic)) {
101  ret = transmit(buffer, length);
102  pthread_mutex_unlock(&read_atomic);
103  }
104  return ret;
105 }
pthread_mutex_t read_atomic
Definition: UART.h:50
Here is the call graph for this function:

◆ try_write()

size_t UART::try_write ( uint8_t *  data,
size_t  dataSize 
)

Write data to the UART.

Return if data is already being written.

Parameters
datapointer to array of bytes to be written.
dataSizelength of data
Returns
amount of bytes written.

Definition at line 89 of file UART.cpp.

89  {
90  size_t ret = 0;
91  if (!pthread_mutex_trylock(&write_atomic)) {
92  ret = transmit(data, dataSize);
93  pthread_mutex_unlock(&write_atomic);
94  }
95  return ret;
96 }
pthread_mutex_t write_atomic
Definition: UART.h:49
Here is the call graph for this function:

◆ write()

size_t UART::write ( uint8_t *  data,
size_t  dataSize 
)

Write data to the UART.

Parameters
datapointer to array of bytes to be written.
dataSizelength of data
Returns
amount of bytes written.

Definition at line 10 of file UART.cpp.

10  {
11  size_t ret = 0;
12  pthread_mutex_lock(&write_atomic);
13 // TODO this might end badly is comm::Link decite to do comm specific things in transmit.
14  ret = transmit(data, dataSize);
15  pthread_mutex_unlock(&write_atomic);
16  return ret;
17 }
pthread_mutex_t write_atomic
Definition: UART.h:49
Here is the call graph for this function:

Member Data Documentation

◆ baudrate

uint32_t aruna::driver::UART::baudrate = 0
private

Definition at line 44 of file UART.h.

◆ flowcontrol

flowcontrol_t aruna::driver::UART::flowcontrol = flowcontrol_t::NONE
private

Definition at line 45 of file UART.h.

◆ parity

parity_t aruna::driver::UART::parity = parity_t::NONE
private

Definition at line 46 of file UART.h.

◆ read_atomic

pthread_mutex_t aruna::driver::UART::read_atomic
private

Definition at line 50 of file UART.h.

◆ stopBit

stop_bit_t aruna::driver::UART::stopBit = stop_bit_t::NONE
private

Definition at line 47 of file UART.h.

◆ wordLength

word_length_t aruna::driver::UART::wordLength = word_length_t::NONE
private

Definition at line 48 of file UART.h.

◆ write_atomic

pthread_mutex_t aruna::driver::UART::write_atomic
private

Definition at line 49 of file UART.h.

◆ XOFF

const uint8_t aruna::driver::UART::XOFF = 0x13
staticprotected

Definition at line 91 of file UART.h.

◆ XON

const uint8_t aruna::driver::UART::XON = 0x11
staticprotected

Definition at line 90 of file UART.h.


The documentation for this class was generated from the following files: