Welcome to mirror list, hosted at ThFree Co, Russian Federation.

usart.hpp « cm3cpp - github.com/thirdpin/libopencm3_cpp_extensions.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6ee5ff047aa387d23c2a46aeae42b0a2628182b2 (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
/*
 * This file is part of the libopencm3_cpp_extensions project.
 * hosted at http://github.com/thirdpin/libopencm3_cpp_extensions
 *
 * Copyright (C) 2016  Third Pin LLC
 * Written by Anastasiia Lazareva <a.lazareva@thirdpin.ru>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
USART C++ Wrapper of libopencm3 library for STM32F2, STM32F4
*/

#ifndef CM3CPP_USART_H_
#define CM3CPP_USART_H_

// GENERAL INCLUDES
#include <cstdint>

// LIBOPENCM3 INCLUDES
#include <libopencm3/stm32/usart.h>
#ifdef STM32F2
#include <libopencm3/stm32/f2/nvic.h>
#endif
#ifdef STM32F4
#include <libopencm3/stm32/f4/nvic.h>
#endif

// CM3CPP INCLUDES
#include "gpio.hpp"
#include "irq/irq.hpp"
#include "private/assert.h"

namespace cm3cpp {

namespace usart {

enum DataBits : uint8_t
{
    _8 = 8,
    _9 = 9
};

enum Mode : uint16_t
{
    RX = USART_MODE_RX,
    TX = USART_MODE_TX,
    RX_TX = USART_MODE_TX_RX
};

enum StopBits : uint16_t
{
    _0_5 = USART_STOPBITS_0_5,
    _1 = USART_STOPBITS_1,
    _1_5 = USART_STOPBITS_1_5,
    _2 = USART_STOPBITS_2
};

enum Parity : uint16_t
{
    PAR_NONE = USART_PARITY_NONE,
    PAR_EVEN = USART_PARITY_EVEN,
    PAR_ODD = USART_PARITY_ODD
};

enum FlowControl : uint16_t
{
    NONE = USART_FLOWCONTROL_NONE,
    RTS = USART_FLOWCONTROL_RTS,
    CTS = USART_FLOWCONTROL_CTS,
    RTS_CTS = USART_FLOWCONTROL_RTS_CTS
};

class Usart
{
 public:
    using Gpio = gpio::Gpio;

    enum class Flag : uint16_t
    {
        TRANSMIT_COMPLETE = USART_SR_TC,
        TX_BUFFER_EMPTY = USART_SR_TXE,
        RX_NOT_EMPTY = USART_SR_RXNE,
    };

    struct Settings
    {
        uint32_t baud_rate;
        DataBits word_length;
        StopBits stop_bits;
        Parity parity;
        Mode mode;
        FlowControl flow_control;
    };

    struct LowLevelConfig
    {
        uint8_t usart_number;
        gpio::Gpio::Pinout tx;
        gpio::Gpio::Pinout rx;
        uint8_t nvic_priority;
    };

    Usart() = default;

    Usart(LowLevelConfig config, Settings settings);

    void init(LowLevelConfig config, Settings settings);

    void deinit();

    void set_settings(Settings settings);

    bool get_flag_status(Flag flag)
    {
        return usart_get_flag(_usart, (uint16_t)flag);
    }

    bool interrupt_source_rx()
    {
        return (((USART_CR1(_usart) & USART_CR1_RXNEIE) != 0) &&
                usart_get_flag(_usart, USART_SR_RXNE));
    }

    bool interrupt_source_tx()
    {
        return (((USART_CR1(_usart) & USART_CR1_TXEIE) != 0) &&
                usart_get_flag(_usart, USART_SR_TXE));
    }

    bool interrupt_source_TC()
    {
        return (((USART_CR1(_usart) & USART_CR1_TCIE) != 0) &&
                usart_get_flag(_usart, USART_SR_TC));
    }

    void clear_tc_flag() {
        USART_SR(_usart) = ~static_cast<uint32_t>(USART_SR_TC);
    }

    void enable_irq() { nvic_enable_irq(_usart_nvic); }

    void disable_irq() { nvic_disable_irq(_usart_nvic); }

    void enable_rx_interrupt() { usart_enable_rx_interrupt(_usart); }

    void enable_tx_interrupt() { usart_enable_tx_interrupt(_usart); }

    void enable_tc_interrupt() { USART_CR1(_usart) |= USART_CR1_TCIE; }

    void disable_rx_interrupt() { usart_disable_rx_interrupt(_usart); }

    void disable_tx_interrupt() { usart_disable_tx_interrupt(_usart); }

    void disable_tc_interrupt() {
        USART_CR1(_usart) &= ~static_cast<uint32_t>(USART_CR1_TCIE);
    }

    bool is_framing_error() { return (USART_SR(_usart) & USART_SR_FE) != 0; }

    bool is_overrun_error() { return (USART_SR(_usart) & USART_SR_IDLE) != 0; }

    bool is_any_error_occurred()
    {
        return (USART_SR(_usart) &
                (USART_SR_ORE | USART_SR_FE | USART_SR_PE | USART_SR_NE)) != 0;
    }

    uint32_t get_sr_reg() { return USART_SR(_usart); }

    bool is_data_received() { return (USART_SR(_usart) & USART_SR_RXNE) != 0; }

    bool is_data_sended() { return (USART_SR(_usart) & USART_SR_TXE) != 0; }

    void write_blocking(uint16_t data) { usart_send_blocking(_usart, data); }

    void write(uint16_t data) { usart_send(_usart, data); }

    uint16_t read() { return usart_recv(_usart); }

    uint16_t read_blocking() { return usart_recv_blocking(_usart); }

    auto get_irq() { return static_cast<Interrupt>(_usart_nvic); }

    auto get_uart_periph() { return _usart; }

 protected:
    Gpio _rx;
    Gpio _tx;
    uint32_t _usart;
    uint8_t _usart_nvic;

    Mode _mode;
};

}  // namespace usart

}  // namespace cm3cpp

#endif /* CM3CPP_USART_H_ */