summaryrefslogtreecommitdiffstats
path: root/hal/uart.hpp
blob: b1dce299a3b2c9729d7b78b1d9589936643e7ce8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* 
 * File:   uart.hpp
 * Author: naopross
 *
 * Created on May 2, 2018, 7:04 PM
 *
 * Note: Non working or untested features are commented out with #if 0 .. #endif
 */

#ifndef UART_HPP
#define UART_HPP

#include <cstdint>
#include <cstddef>
#include <queue>

extern "C" {
    void usart_1_isr();
    void usart_2_isr();
    void usart_3_isr();
    void usart_4_isr();
}

namespace uart
{
    const unsigned devices_count = 4;

#if 0
    enum class status : unsigned int
    {
        rx_data_available = 1<<0,
        rx_overrun_error = 1<<1,
        framing_error = 1<<2,
        parity_error = 1<<3,
        receiver_ide = 1<<4,
        tx_complete = 1<<8,
        tx_full = 1<<9,
    };

    enum class transfer_status : unsigned int
    {
        rx_full = 1<<0,
        rx_data_present = 1<<1,
        rx_empty = 1<<2,
        tx_full = 1<<3,
        tx_empty = 1<<4,
    };
#endif

    bool _echo[devices_count];
    std::queue<uint8_t> _rx_buffer[devices_count];
    std::queue<uint8_t> _tx_buffer[devices_count];

    // the following functions have been inlined 

    // template<unsigned dev>
    // std::queue<uint8_t>& rx_buffer();

    // template<unsigned dev>
    // std::queue<uint8_t>& tx_buffer();

    template<unsigned dev>
    void initialize();

    // the following functions have been inlined 

    // template<unsigned dev>
    // void echo(bool enabled);

    // template<unsigned dev>
    // bool echo_enabled();

    template<unsigned dev>
    uint8_t read();

    template<unsigned dev>
    uint8_t read_wait();

#if 0
    template<unsigned dev>
    std::string read(const unsigned len);

    template<unsigned dev>
    std::string readline(const std::string &eol = "\n\r");

    template<unsigned dev>
    unsigned read(uint8_t *buffer, const unsigned numbytes);
#endif
    
    template<unsigned dev>
    void write(const uint8_t byte);

    template<unsigned dev>
    void write(const std::string &str);

    template<unsigned dev>
    unsigned write(const uint8_t *buffer, const unsigned numbytes);

    template<unsigned dev>
    void print(const std::string &str);

#if 0
    template<unsigned dev>
    status status();

    template<unsigned dev>
    transfer_status tranfer_status();
#endif
}

#endif  /* UART_HPP */