21 #ifndef ARM_CORTEX_STM32_COMMON_USART_HPP_INCLUDED 22 #define ARM_CORTEX_STM32_COMMON_USART_HPP_INCLUDED 24 #include <arch/nvic.hpp> 25 #include <arch/rcc.hpp> 26 #include <arch/reg/usart.hpp> 27 #include <type_traits> 31 template<
unsigned _usart_no,
typename system_clock_type >
34 static_assert((_usart_no >= 1) && (_usart_no <= 3),
"invalid USART number");
35 static_assert(_usart_no != 1,
"usart 1 is not yet supported, sorry...");
40 static constexpr
unsigned usart_no = _usart_no;
44 using irq = irq::usart<usart_no>;
46 using resources = rcc_usart_clock_resources<usart_no>;
50 template<
unsigned value>
51 struct baud_rate_impl {
52 static constexpr
unsigned pclk = (usart_no == 1 ? system_clock_type::pclk2_freq : system_clock_type::pclk1_freq );
53 static constexpr
unsigned div = (pclk * 25) / (4 * value);
54 static constexpr
unsigned mant = div / 100;
55 static constexpr
unsigned fraq = ((div - (mant * 100)) * 16 + 50) / 100;
57 using type =
typename USARTx::BRR::template merge<
63 template<
unsigned value>
64 struct word_length_impl {
65 static_assert((value == 8) || (value == 9),
"illegal word_length (supported values: 8, 9)");
69 template<
unsigned a,
unsigned b = 0>
70 struct stop_bits_impl {
71 static_assert(((a == 1) && (b == 0)) ||
72 ((a == 0) && (b == 5)) ||
73 ((a == 2) && (b == 0)) ||
74 ((a == 1) && (b == 5)),
75 "illegal stop_bits (supported values: 1, 0.5, 2, 1.5)");
77 template<
typename usart>
78 using type =
regval<
typename USARTx::CR2::STOP,
79 ((a == 1) && (b == 0)) ? 0 :
80 ((a == 0) && (b == 5)) ? 1 :
81 ((a == 2) && (b == 0)) ? 2 :
82 ((a == 1) && (b == 5)) ? 3 : 0xff
91 template<
unsigned value>
92 using baud_rate =
typename baud_rate_impl<value>::type;
94 template<
unsigned value>
104 regval< typename USARTx::CR1::PCE, 1 >,
113 template<
unsigned a,
unsigned b = 0>
140 unsigned pclk = (_usart_no == 1 ? system_clock_type::pclk2_freq : system_clock_type::pclk1_freq );
141 unsigned div = (pclk * 25) / (4 * baud_rate);
142 unsigned mant = div / 100;
143 unsigned fraq = ((div - (mant * 100)) * 16 + 50) / 100;
145 return (mant << 4) | (fraq & 0x0f);
156 template<
typename... Tp >
158 reglist< Tp... >::template strict_reset_to<
159 typename USARTx::CR1,
160 typename USARTx::CR2,
161 typename USARTx::CR3,
176 static void send(
typename USARTx::DR::value_type data) {
179 USARTx::DR::store(data);
181 static typename USARTx::DR::value_type
receive(
void) {
185 return USARTx::DR::load();
189 USARTx::CR1::UE::set();
192 USARTx::CR1::UE::clear();
201 auto cr1 = USARTx::CR1::load();
202 if(rxne) cr1 |= USARTx::CR1::RXNEIE::value;
203 if(txe) cr1 |= USARTx::CR1::TXEIE::value;
204 if(pe) cr1 |= USARTx::CR1::PEIE::value;
205 if(tc) cr1 |= USARTx::CR1::TCIE::value;
206 if(idle) cr1 |= USARTx::CR1::IDLEIE::value;
207 USARTx::CR1::store(cr1);
216 auto cr1 = USARTx::CR1::load();
217 if(rxne) cr1 &= ~
USARTx::CR1::RXNEIE::value;
218 if(txe) cr1 &= ~
USARTx::CR1::TXEIE::value;
219 if(pe) cr1 &= ~
USARTx::CR1::PEIE::value;
220 if(tc) cr1 &= ~
USARTx::CR1::TCIE::value;
221 if(idle) cr1 &= ~
USARTx::CR1::IDLEIE::value;
222 USARTx::CR1::store(cr1);
231 #endif // ARM_CORTEX_STM32_COMMON_USART_HPP_INCLUDED irq::usart< usart_no > irq
Definition: usart.hpp:44
static void configure(void)
Configure USART register using Tp type traits.
Definition: usart.hpp:157
USART< usart_no > USARTx
Definition: usart.hpp:42
static void disable_interrupt(bool rxne, bool txe=false, bool pe=false, bool tc=false, bool idle=false)
Definition: usart.hpp:210
typename stop_bits_impl< a, b >::type stop_bits
Definition: usart.hpp:114
static void enable_interrupt(bool rxne, bool txe=false, bool pe=false, bool tc=false, bool idle=false)
Definition: usart.hpp:195
static constexpr unsigned usart_no
Definition: usart.hpp:40
rcc_usart_clock_resources< usart_no > resources
Definition: usart.hpp:46
static void enable_tx_interrupt(void)
Definition: usart.hpp:225
typename word_length_impl< value >::type word_length
Definition: usart.hpp:95
static void send(typename USARTx::DR::value_type data)
Definition: usart.hpp:176
static USARTx::DR::value_type receive(void)
Definition: usart.hpp:181
static void set_baudrate(unsigned value)
Set the BRR register to the value corresponding to the baud_rate provided.
Definition: usart.hpp:172
static void disable(void)
Definition: usart.hpp:191
typename baud_rate_impl< value >::type baud_rate
Definition: usart.hpp:92
Definition: usart.hpp:118
static void enable(void)
Definition: usart.hpp:188
Definition: usart.hpp:123
static void disable_tx_interrupt(void)
Definition: usart.hpp:226
Definition: usart.hpp:133
static USARTx::BRR::value_type baud_to_brr(unsigned baud_rate)
Definition: usart.hpp:139