summaryrefslogtreecommitdiff
path: root/ucoo/hal/uart/test/test_uart_disc.cc
blob: 029b28d39429df88c27489defcc95656aea705f8 (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
// ucoolib - Microcontroller object oriented library. {{{
//
// Copyright (C) 2012 Nicolas Schodet
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
// }}}
#include "ucoo/hal/uart/uart.hh"

#include "ucoo/arch/arch.hh"
#include "ucoo/hal/gpio/gpio.hh"
#include "ucoo/base/test/test.hh"

static void
check_act (ucoo::Stream &ts, ucoo::Stream &u, char n)
{
    char buf[3 + 16 + 1];
    if (!u.poll ())
        return;
    int r = u.read (buf + 3, 16);
    if (r <= 0)
    {
        buf[3] = '#';
        r = 1;
    }
    buf[0] = '<';
    buf[1] = n;
    buf[2] = ':';
    buf[3 + r] = '>';
    ts.write (buf, 3 + r + 1);
}

int
main (int argc, const char **argv)
{
    ucoo::arch_init (argc, argv);
    ucoo::Stream &ts = ucoo::test_stream ();
    ucoo::Uart u1 (ucoo::Uart::Instance::USART1);
    ucoo::Uart u3 (ucoo::Uart::Instance::USART3);
    ucoo::Uart u4 (ucoo::Uart::Instance::UART4);
    u1.enable (38400, ucoo::Uart::Parity::EVEN, 1);
    u3.enable (38400, ucoo::Uart::Parity::EVEN, 1);
    u4.enable (38400, ucoo::Uart::Parity::EVEN, 1);
    // For this test, shorten B6 & B7 to have a loopback on UART1, shorten C10
    // & C11 to connect UART3 to UART4.
    ucoo::GPIOB.enable ();
    ucoo::GPIOC.enable ();
    ucoo::GPIOB[6].af (7);
    ucoo::GPIOB[7].af (7);
    ucoo::GPIOC[10].af (7);
    ucoo::GPIOC[11].af (8);
    // Loop to report any activity on ports and provide a simple UI.
    char buf[64];
    int buf_i = 0;
    ucoo::Uart *u = &u1;
    while (1)
    {
        check_act (ts, u1, '1');
        check_act (ts, u3, '3');
        check_act (ts, u4, '4');
        while (ts.poll ())
        {
            char c = ts.getc ();
            switch (c)
            {
            case '?':
                static const char help[] =
                    "? - help\n"
                    "1, 3, 4 - set output uart\n"
                    ": - reset output buffer index\n"
                    "! - send output buffer\n"
                    "O, E, N - change parity to Odd, Even or None\n"
                    "any - fill output buffer\n";
                ts.write (help, sizeof (help));
                break;
            case '1':
                u = &u1;
                break;
            case '3':
                u = &u3;
                break;
            case '4':
                u = &u4;
                break;
            case ':':
                buf_i = 0;
                break;
            case '!':
                u->write (buf, buf_i);
                break;
            case 'O':
                u->enable (38400, ucoo::Uart::Parity::ODD, 1);
                break;
            case 'E':
                u->enable (38400, ucoo::Uart::Parity::EVEN, 1);
                break;
            case 'N':
                u->enable (38400, ucoo::Uart::Parity::NONE, 1);
                break;
            default:
                if (buf_i < static_cast<int> (sizeof (buf)))
                    buf[buf_i++] = c;
                break;
            }
        }
    }
}