OpenMPTL - STM32 (common)
C++ Microprocessor Template Library
usart_stream.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 ARM_CORTEX_STM32_COMMON_USART_STREAM_HPP_INCLUDED
22 #define ARM_CORTEX_STM32_COMMON_USART_STREAM_HPP_INCLUDED
23 
24 #include <arch/usart.hpp>
25 #include <fifo.hpp>
26 
27 namespace mptl {
28 
29 template<
30  typename usart_type,
31  typename _fifo_type, // = ring_buffer<char, 256>,
32  bool _crlf = true,
33  bool debug_irqs = false
34  >
36 {
37 public:
38  static constexpr bool crlf = _crlf;
39  using fifo_type = _fifo_type;
40  using char_type = char;
41 
44 
45  static volatile unsigned int irq_count;
46  static volatile unsigned int irq_errors;
47 
48 private:
49 
50  using SR = typename usart_type::USARTx::SR;
51 
52  static void isr(void) {
53  auto flags = SR::load();
54 
55  if(debug_irqs) {
56  irq_count++;
57  if(flags & (SR::ORE::value | SR::FE::value | SR::NE::value | SR::PE::value))
58  irq_errors++;
59  }
60 
61  if(flags & SR::RXNE::value) {
62  uint32_t data = usart_type::receive(); /* implicitely clears RXNE flag */
63  rx_fifo.push(data);
64  }
65  if(flags & SR::TXE::value) {
66  char c;
67  if(tx_fifo.pop(c)) {
68  usart_type::send(c); /* implicitely clears TXE flag */
69  }
70  else {
71  usart_type::disable_tx_interrupt();
72  }
73  }
74  }
75 
76 public:
77 
78  using irq_resources = typelist<
80  >;
81 
82  using resources = typelist<
83  typename usart_type::resources,
85  >;
86 
87  static void flush() {
88  usart_type::enable_tx_interrupt();
89  }
90 
91  /**
92  * open the stream
93  *
94  * - enable USARTx
95  * - enable usart irq channel
96  * - enable RXNE and PE interrupts
97  *
98  * NOTE: Make sure the device is correctly setup before calling this
99  * function. e.g. by calling usart_device.configure()
100  */
101  static void open(void) {
102  tx_fifo.reset();
103  rx_fifo.reset();
104 
105  usart_type::enable();
106  usart_type::irq::enable();
107  usart_type::enable_interrupt(true, false, true, false, false);
108  }
109 
110  /**
111  * close the stream
112  *
113  * - disable interrupts enabled by open()
114  * - disable the usartX irq
115  * - disable USARTx
116  */
117  static void close(void) {
118  usart_type::disable_interrupt(true, false, true, false, false);
119  usart_type::irq::disable();
120  usart_type::disable();
121  }
122 };
123 
124 template<typename usart_type, typename fifo_type, bool crlf, bool debug_irqs>
126 
127 template<typename usart_type, typename fifo_type, bool crlf, bool debug_irqs>
129 
130 template<typename usart_type, typename fifo_type, bool crlf, bool debug_irqs>
132 
133 template<typename usart_type, typename fifo_type, bool crlf, bool debug_irqs>
135 
136 
137 } // namespace mptl
138 
139 #endif // ARM_CORTEX_STM32_COMMON_USART_STREAM_HPP_INCLUDED
_fifo_type fifo_type
Definition: usart_stream.hpp:39
static void close(void)
close the stream
Definition: usart_stream.hpp:117
static constexpr bool crlf
Definition: usart_stream.hpp:38
static fifo_type tx_fifo
Definition: usart_stream.hpp:43
typelist< typename usart_type::resources, irq_resources > resources
Definition: usart_stream.hpp:85
static fifo_type rx_fifo
Definition: usart_stream.hpp:42
static volatile unsigned int irq_errors
Definition: usart_stream.hpp:46
Definition: usart_stream.hpp:35
typelist< irq_handler< typename usart_type::irq, isr > > irq_resources
Definition: usart_stream.hpp:80
char char_type
Definition: usart_stream.hpp:40
static volatile unsigned int irq_count
Definition: usart_stream.hpp:45
typename mpl::make_typelist< sane_typelist<>, Tp... >::type typelist
static void open(void)
open the stream
Definition: usart_stream.hpp:101
static void flush()
Definition: usart_stream.hpp:87