summaryrefslogtreecommitdiffstats
path: root/hal/uart.tpp
blob: 21d747933e2d9e05e57c3de8df4da4d1fa1e49e2 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/* 
 * File:   uart.tpp
 * Author: naopross
 *
 * Created on May 2, 2018, 7:05 PM
 * 
 * Note: Templated functions inside a namespace cannot be created outside of a 
 *       namespace because of a g++ bug in version 4.8.0 on which xc++ is based.
 *       See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56480
 */

#ifndef UART_TPP
#define UART_TPP

#include "hwconfig.hpp"
#include "uart.hpp"

extern "C" {
// #include <proc/p32mx470f512h.h>
#include <xc.h>
#include <sys/attribs.h>
}


/* templated functions */
namespace uart
{
    // access buffers
    template<unsigned dev>
    inline std::queue<uint8_t>& rx_buffer()
    {
        static_assert(dev != 0 && dev <= devices_count, "invalid device number");
        return uart::_rx_buffer[dev -1];
    }

    template<unsigned dev>
    inline std::queue<uint8_t>& tx_buffer()
    {
        static_assert(dev != 0 && dev <= devices_count, "invalid device number");
        return uart::_tx_buffer[dev -1];
    }

    template<unsigned dev>
    inline bool echo(bool enabled)
    {
        static_assert(dev != 0 && dev <= devices_count, "invalid device number");
        _echo[dev -1] = enabled;
    }

    template<unsigned dev>
    inline bool echo_enabled()
    {
        static_assert(dev != 0 && dev <= devices_count, "invalid device number");
        return _echo[dev -1];
    }

    // read from serial device
    template<unsigned dev>
    uint8_t read()
    {
        uint8_t byte = rx_buffer<dev>().front();
        rx_buffer<dev>().pop();

        return byte;
    }

    template<unsigned dev>
    uint8_t read_wait()
    {
        while (rx_buffer<dev>().empty());

        uint8_t byte = rx_buffer<dev>().front();
        rx_buffer<dev>().pop();

        return byte;
    }

#if 0
    template<unsigned dev>
    std::string read(const unsigned len)
    {
        std::string str = "";
        unsigned i = len;

        if (rx_buffer<dev>().size() < len) {
            i = rx_buffer<dev>().size();
        }

        while (i--) {
            str += read<dev>();
        }

        return str;
    }

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

        std::string str = "";
        unsigned i = rx_buffer<dev>().size();

        while (i--) {
            str += read<dev>();

            // compare string endings
            // TODO: make it more efficient
            if (eol_c == eol.size()) {
                break;
            }

            if (str.back() == eol[eol_c]) {
                eol_c++;
            }
        }
    }

    template<unsigned dev>
    unsigned read(uint8_t *buffer, const unsigned numbytes)
    {
        uint8_t *bptr = buffer;
        unsigned len = numbytes;

        while (len--) {
            *(bptr++) = read<dev>();
        }
    }
#endif


    // write to serial device
    template<unsigned dev>
    void write(const uint8_t byte)
    {
        // disable send data flag
        IEC1bits.U1TXIE = 0;

        tx_buffer<dev>().push(byte);

        // enable send data flag
        IEC1bits.U1TXIE = 1;
    }

    template<unsigned dev>
    void write(const std::string &str)
    {
        // TODO: if size of str is reasonale, push to queue AND THEN send,
        //       right now on each call the interrupt is enabled and disabled
        // for (const char &c : str) {
            // write<dev>(c);
        // }

        // disable send data flag
        IEC1bits.U1TXIE = 0;

        for (int i = 0; i < str.size(); i++) {
            tx_buffer<1>().push(static_cast<uint8_t>(str[i]));
        }

        // enable send data flag
        IEC1bits.U1TXIE = 1;
    }

    template<unsigned dev>
    unsigned write(const uint8_t *buffer, const unsigned numbytes)
    {
        uint8_t *bptr = buffer;
        unsigned len = numbytes;

        while (len--) {
            write<dev>(*(bptr++));
        }
    }

    template<unsigned dev>
    void print(const std::string &str)
    {
        write<dev>(str);
        write<dev>("\n\r");
    }
}


/* specializations for UART1 */

#ifdef DEBUG
#include "pin.tpp"
io_pin<2> led_blue(&LATBbits, &TRISBbits, &PORTBbits);
io_pin<3> led_green(&LATBbits, &TRISBbits, &PORTBbits);
#endif

void __ISR(_UART_1_VECTOR, IPL1AUTO) usart_1_isr()
{
    if (IFS1bits.U1RXIF) {
        #ifdef DEBUG
        led_blue.toggle();
        #endif

        // wait for data to be available
        while (!U1STAbits.URXDA);
        uart::rx_buffer<1>().push(static_cast<uint8_t>(U1RXREG));

        // echo
        if (uart::echo_enabled<1>()) {
            // uart::write<1>(uart::rx_buffer<1>().front());
            IEC1bits.U1TXIE = 0;
            uart::tx_buffer<1>().push(uart::rx_buffer<1>().front());
            IEC1bits.U1TXIE = 1;
        }

        IFS1bits.U1RXIF = 0;

    } 
    // on each interrupt call, send one character
    else if (IFS1bits.U1TXIF) {
        #ifdef DEBUG
        led_green.toggle();
        #endif

        // disable TX interrupt flag
        IEC1bits.U1TXIE = 0;

        // write data
        U1TXREG = static_cast<decltype(U1TXREG)>(uart::tx_buffer<1>().front());
        uart::tx_buffer<1>().pop();

        // if not empty, enable tx flag
        if (!uart::tx_buffer<1>().empty()) {
            IEC1bits.U1TXIE = 1;
        }

        IFS1bits.U1TXIF = 0;

    } else {
        if (U1STAbits.OERR == 1) {
            U1STAbits.OERR = 0;
        }

        IFS1bits.U1EIF = 0;
    }
}

namespace uart
{
    template<>
    void initialize<1>()
    {
        // STSEL 1S;
        // IREN disabled;
        // PDSEL 8N;
        // RTSMD disabled;
        // RXINV disabled;
        // SIDL disabled;
        // WAKE disabled;
        // ABAUD disabled;
        // LPBACK disabled;
        // BRGH enabled;
        // UEN TX_RX;
        // ON enabled; 
        U1MODE = 0x8008;
        // UTXISEL TX_ONE_CHAR;
        // UTXINV disabled;
        // ADDR 0;
        // URXEN disabled;
        // OERR disabled;
        // ADM_EN disabled;
        // URXISEL RX_ONE_CHAR;
        // UTXBRK disabled;
        // UTXEN disabled;
        // ADDEN disabled; 
        U1STA = 0x0;
        // U1TXREG 0; 
        U1TXREG = 0x0;
        // BaudRate = 9600;
        // Frequency = 1000000 Hz;
        // BRG 25; 
        U1BRG = 0x19;

        IEC1bits.U1RXIE = 1;

        U1STAbits.UTXEN = 1;
        U1STAbits.URXEN = 1;

        // Enabling  UART
        U1MODEbits.ON = 1;

        // UERI: UART1 Error
        // Priority: 1
        // SubPriority: 0
        IPC7bits.U1IP = 1;
        IPC7bits.U1IS = 0;

        // map uart pins to port F
        hw::regunlock(); // unlock PPS
        CFGCONbits.IOLOCK = 0;

        U1RXRbits.U1RXR = 0x0004;   //RF1->UART1:U1RX;
        RPF0Rbits.RPF0R = 0x0003;   //RF0->UART1:U1TX;

        CFGCONbits.IOLOCK = 1; // lock   PPS
        hw::reglock();
    }

#if 0
    template<>
    void set_baudrate<1>(long baud)
    {
        
    }
#endif
}

#endif