OpenMPTL - STM32F4
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 #ifndef ARCH_USART_HPP_INCLUDED
22 #define ARCH_USART_HPP_INCLUDED
23 
24 #include <arch/gpio.hpp>
25 #include "../../../common/usart.hpp"
26 
27 namespace mptl {
28 
29 namespace mpl
30 {
31  template< typename gpio_type, unsigned gpio_alt_func_num >
32  struct usart_gpio_rx_resources {
33  using type = typelist<
34  typename gpio_type::resources,
35  typename gpio_type::resistor::floating,
36  typename gpio_type::mode::alternate_function, // implicitely set by alt_func_num, but does not harm
37  typename gpio_type::template alt_func_num< gpio_alt_func_num >
38  >;
39  };
40  template< unsigned gpio_alt_func_num >
41  struct usart_gpio_rx_resources< void, gpio_alt_func_num > {
42  using type = void;
43  };
44 
45  template< typename gpio_type, freq_t gpio_speed, unsigned gpio_alt_func_num >
46  struct usart_gpio_tx_resources {
47  using type = typelist<
48  typename gpio_type::resources,
49  typename gpio_type::output_type::push_pull,
50  typename gpio_type::resistor::floating,
51  typename gpio_type::template speed< gpio_speed >,
52  typename gpio_type::mode::alternate_function, // implicitely set by alt_func_num, but does not harm
53  typename gpio_type::template alt_func_num< gpio_alt_func_num >
54  >;
55  };
56  template< freq_t gpio_speed, unsigned gpio_alt_func_num >
57  struct usart_gpio_tx_resources< void, gpio_speed, gpio_alt_func_num > {
58  using type = void;
59  };
60 } // namespace mpl
61 
62 
63 template<
64  unsigned usart_no,
65  typename system_clock_type,
66  typename gpio_rx_type,
67  typename gpio_tx_type,
68  freq_t gpio_tx_speed = mhz(50)
69  >
70 class usart : public usart_stm32_common< usart_no, system_clock_type >
71 {
73 
74  static constexpr unsigned gpio_alt_func_num = (usart_no <= 3) ? 7 : 8;
75 
76 public:
77 
78  using resources = typelist<
79  typename base_type::resources,
80  typename mpl::usart_gpio_rx_resources< gpio_rx_type, gpio_alt_func_num >::type,
81  typename mpl::usart_gpio_tx_resources< gpio_tx_type, gpio_tx_speed, gpio_alt_func_num >::type
82  >;
83 };
84 
85 } // namespace mptl
86 
87 
88 #endif // ARCH_USART_HPP_INCLUDED
typelist< typename base_type::resources, typename mpl::usart_gpio_rx_resources< gpio_rx_type, gpio_alt_func_num >::type, typename mpl::usart_gpio_tx_resources< gpio_tx_type, gpio_tx_speed, gpio_alt_func_num >::type > resources
Definition: usart.hpp:82
unsigned int freq_t
Definition: usart.hpp:70
typename mpl::make_typelist< sane_typelist<>, Tp... >::type typelist
static constexpr freq_t mhz(unsigned long long x)
Definition: rcc.hpp:199