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
|
/**
******************************************************************************
* @file cm3cpp_usart_dma.hpp
* @author
* @version V1.0
* @date 05-2019
* @brief This is file realise usart.
******************************************************************************
* @attention
*
******************************************************************************
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#pragma once
// LIBOPENCM3 INCLUDES
#include <libopencm3/stm32/usart.h>
// CM3CPP INCLUDES
#include "cm3cpp_dma.hpp"
#include "cm3cpp_gpio.h"
namespace cm3cpp {
namespace usart_dma {
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
};
/// Low level config for USART
struct LowLevelConfigUsart
{
uint8_t usart_number;
gpio::Gpio::Pinout tx;
gpio::Gpio::Pinout rx;
};
/// Settings USART
struct Settings
{
uint32_t baud_rate;
DataBits word_length;
StopBits stop_bits;
Parity parity;
Mode mode;
FlowControl flow_control;
};
/**
* Class Usart & Dma
*/
class UsartDma
{
public:
/// Constructor
UsartDma(const LowLevelConfigUsart& usart_config,
const Settings& settings,
const dma::LowLevelConfig& dma_tx_config,
const dma::LowLevelConfig& dma_rx_config);
/// Usart enable
void enable() const;
/// Usart disable
void disable() const;
/// Sending data to USART(not safe)
void write(uint8_t& data, uint16_t len);
/* These classes must be public because
they can be accessed by interrupt handlers */
class dma::Dma _dma_tx; ///< DMA for transmission
class dma::Dma _dma_rx; ///< DMA for receive
protected:
UsartDma() = delete; /// Constructor default is delete
UsartDma(const UsartDma& a) = delete; /// Constructor copy is delete
UsartDma(const UsartDma&& a) = delete; /// Constructor move is delete
UsartDma& operator=(const UsartDma&) = delete; /// Operator copy is delete
UsartDma& operator=(UsartDma&&) = delete; /// Operator move is delete
private:
/// Set clock port GPIO
void port_config_clock_enable(uint32_t port) const;
/// Config USART
void init_uasrt(const Settings& settings) const;
uint32_t _usart; ///< Number USART
};
} // namespace usart_dma
} // namespace cm3cpp
|