OpenMPTL - STM32 (common)
C++ Microprocessor Template Library
usart.hpp
Go to the documentation of this file.
1 /*
2  * OpenMPTL - C++ Microprocessor Template Library
3  *
4  * Copyright (C) 2012-2017 Axel Burri <axel@tty0.ch>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 /*
22  * This program contains derivative representations of CMSIS System
23  * View Description (SVD) files, and is subject to the "End User
24  * License Agreement for STMicroelectronics" (see "STM_License.html"
25  * in the containing directory).
26  */
27 
28 #ifndef ARM_CORTEX_STM32_COMMON_REG_USART_HPP_INCLUDED
29 #define ARM_CORTEX_STM32_COMMON_REG_USART_HPP_INCLUDED
30 
31 #include <register.hpp>
32 
33 namespace mptl {
34 
35 /**
36  * Universal synchronous asynchronous receiver transmitter (USART),
37  * common to all stm32 processors.
38  *
39  * NOTE: We use std::uint_fast16_t to access the registers. This way
40  * we leave it up to the compiler to use either 16-bit or 32-bit
41  * integers for the access_type. More precicely: "use fastest
42  * unsigned integer type with width of at least 16 bits". In most
43  * situations the compiler toolchain (gcc-4.8) will choose 32bit
44  * integers for ARM / ARM-Thumb(2).
45  *
46  * TODO: move ^^^this^^^ text to the "register access" discussion.
47  */
48 template<reg_addr_t base_addr>
50 {
51  /**
52  * Status register
53  */
54  struct SR
55  : public reg< std::uint_fast16_t, base_addr + 0x00, rw, 0x00c0 >
56  {
57  // TODO: document why it sucks to have to define a typedef "type"
58  // here again. This is required here (by the standard) because
59  // the template parameter "base_addr" is used as
60  // template-parameter in our reg<> base class:
61  //
62  // reg< ..., base_addr + 0x00, ... >
63  // ^^^
64  // C++ Standard says that you should fully qualify
65  // name according to 14.6.2/3:
66  //
67  // In the definition of a class template or a member of a
68  // class template, if a base class of the class template
69  // depends on a template-parameter, the base class scope is
70  // not examined during unqualified name lookup either at the
71  // point of definition of the class template or member or
72  // during an instantiation of the class template or member.
73  //
74  // This all has to do with specialization: the reg<>
75  // template could be specialized later on, possibly making
76  // "type" unavailable.
77  //
78  // see discussion here: http://stackoverflow.com/questions/1643035/propagating-typedef-from-based-to-derived-class-for-template
79  //
81 
82  using CTS = regbits< type, 9, 1 >; /**< CTS flag */
83  using LBD = regbits< type, 8, 1 >; /**< LIN break detection flag */
84  using TXE = regbits< type, 7, 1 >; /**< Transmit data register empty */
85  using TC = regbits< type, 6, 1 >; /**< Transmission complete */
86  using RXNE = regbits< type, 5, 1 >; /**< Read data register not empty */
87  using IDLE = regbits< type, 4, 1 >; /**< IDLE line detected */
88  using ORE = regbits< type, 3, 1 >; /**< Overrun error */
89  using NE = regbits< type, 2, 1 >; /**< Noise error flag */
90  using FE = regbits< type, 1, 1 >; /**< Framing error */
91  using PE = regbits< type, 0, 1 >; /**< Parity error */
92  };
93 
94  /**
95  * Data register
96  */
97  struct DR
98  : public reg< std::uint_fast16_t, base_addr + 0x04, rw, 0x00000000 >
99  {
101 
102  using regbits_type = regbits< type, 0, 9 >; /**< Data value */
103  };
104 
105  /**
106  * Baud rate register
107  */
108  struct BRR
109  : public reg< std::uint_fast16_t, base_addr + 0x08, rw, 0x0000 >
110  {
112 
113  using DIV_Mantissa = regbits< type, 4, 12 >; /**< mantissa of USARTDIV */
114  using DIV_Fraction = regbits< type, 0, 4 >; /**< fraction of USARTDIV */
115  };
116 
117  /**
118  * Control register 1
119  */
120  struct CR1
121  : public reg< std::uint_fast16_t, base_addr + 0x0c, rw, 0x0000 >
122  {
124 
125  using UE = regbits< type, 13, 1 >; /**< USART enable */
126  using M = regbits< type, 12, 1 >; /**< Word length */
127  using WAKE = regbits< type, 11, 1 >; /**< Wakeup method */
128  using PCE = regbits< type, 10, 1 >; /**< Parity control enable */
129  using PS = regbits< type, 9, 1 >; /**< Parity selection */
130  using PEIE = regbits< type, 8, 1 >; /**< PE interrupt enable */
131  using TXEIE = regbits< type, 7, 1 >; /**< TXE interrupt enable */
132  using TCIE = regbits< type, 6, 1 >; /**< Transmission complete interrupt enable */
133  using RXNEIE = regbits< type, 5, 1 >; /**< RXNE interrupt enable */
134  using IDLEIE = regbits< type, 4, 1 >; /**< IDLE interrupt enable */
135  using TE = regbits< type, 3, 1 >; /**< Transmitter enable */
136  using RE = regbits< type, 2, 1 >; /**< Receiver enable */
137  using RWU = regbits< type, 1, 1 >; /**< Receiver wakeup */
138  using SBK = regbits< type, 0, 1 >; /**< Send break */
139  };
140 
141  /**
142  * Control register 2
143  */
144  struct CR2
145  : public reg< std::uint_fast16_t, base_addr + 0x10, rw, 0x0000 >
146  {
148 
149  using LINEN = regbits< type, 14, 1 >; /**< LIN mode enable */
150  using STOP = regbits< type, 12, 2 >; /**< STOP bits */
151  using CLKEN = regbits< type, 11, 1 >; /**< Clock enable */
152  using CPOL = regbits< type, 10, 1 >; /**< Clock polarity */
153  using CPHA = regbits< type, 9, 1 >; /**< Clock phase */
154  using LBCL = regbits< type, 8, 1 >; /**< Last bit clock pulse */
155  using LBDIE = regbits< type, 6, 1 >; /**< LIN break detection interrupt enable */
156  using LBDL = regbits< type, 5, 1 >; /**< LIN break detection length */
157  using ADD = regbits< type, 0, 4 >; /**< Address of the USART node */
158  };
159 
160  /**
161  * Control register 3
162  */
163  struct CR3
164  : public reg< std::uint_fast16_t, base_addr + 0x14, rw, 0x0000 >
165  {
167 
168  using CTSIE = regbits< type, 10, 1 >; /**< CTS interrupt enable */
169  using CTSE = regbits< type, 9, 1 >; /**< CTS enable */
170  using RTSE = regbits< type, 8, 1 >; /**< RTS enable */
171  using DMAT = regbits< type, 7, 1 >; /**< DMA enable transmitter */
172  using DMAR = regbits< type, 6, 1 >; /**< DMA enable receiver */
173  using SCEN = regbits< type, 5, 1 >; /**< Smartcard mode enable */
174  using NACK = regbits< type, 4, 1 >; /**< Smartcard NACK enable */
175  using HDSEL = regbits< type, 3, 1 >; /**< Half-duplex selection */
176  using IRLP = regbits< type, 2, 1 >; /**< IrDA low-power */
177  using IREN = regbits< type, 1, 1 >; /**< IrDA mode enable */
178  using EIE = regbits< type, 0, 1 >; /**< Error interrupt enable */
179  };
180 
181  /**
182  * Guard time and prescaler register
183  */
184  struct GTPR
185  : public reg< std::uint_fast16_t, base_addr + 0x18, rw, 0x0000 >
186  {
188 
189  using GT = regbits< type, 8, 8 >; /**< Guard time value */
190  using PSC = regbits< type, 0, 8 >; /**< Prescaler value */
191  };
192 };
193 
194 
195 /**
196  * Some architectures (e.g. stm32f4xx) provide oversampling and
197  * one-sample-bit mode.
198  */
199 template<reg_addr_t base_addr>
200 class USART_common_ext : public USART_common<base_addr>
201 {
203 
204 public:
205  struct CR1 : public base_type::CR1 {
206  using OVER8 = regbits< typename base_type::CR1::type, 15, 1 >; /**< Oversampling mode */
207  };
208  struct CR3 : public base_type::CR3 {
209  using ONEBIT = regbits< typename base_type::CR3::type, 11, 1 >; /**< One sample bit method enable */
210  };
211 };
212 
213 } // namespace mptl
214 
215 #endif // ARM_CORTEX_STM32_COMMON_REG_USART_HPP_INCLUDED
Definition: usart.hpp:205
Control register 2.
Definition: usart.hpp:144
Status register.
Definition: usart.hpp:54
Data register.
Definition: usart.hpp:97
Baud rate register.
Definition: usart.hpp:108
Control register 3.
Definition: usart.hpp:163
Universal synchronous asynchronous receiver transmitter (USART), common to all stm32 processors...
Definition: usart.hpp:49
Some architectures (e.g.
Definition: usart.hpp:200
Guard time and prescaler register.
Definition: usart.hpp:184
Definition: usart.hpp:208
Control register 1.
Definition: usart.hpp:120