sim_uart.h Source File

yasimavr: sim_uart.h Source File
yasimavr
Loading...
Searching...
No Matches
sim_uart.h
Go to the documentation of this file.
1/*
2 * sim_uart.h
3 *
4 * Copyright 2022-2026 Clement Savergne <csavergne@yahoo.com>
5
6 This file is part of yasim-avr.
7
8 yasim-avr is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 yasim-avr is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with yasim-avr. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22//=======================================================================================
23
24#ifndef __YASIMAVR_UART_H__
25#define __YASIMAVR_UART_H__
26
27#include "../core/sim_types.h"
28#include "../core/sim_cycle_timer.h"
29#include "../core/sim_signal.h"
30#include "../core/sim_logger.h"
31#include <deque>
32
34
35
36//=======================================================================================
51#define AVR_CTLREQ_USART_BYTES (AVR_CTLREQ_BASE + 1)
52
53
56
57
58//=======================================================================================
59
64namespace UART {
65
86
95
105
114
115
141
142public:
143
144 USART();
145 virtual ~USART() = default;
146
147 void init(CycleManager& cycle_manager, Logger* logger = nullptr);
148
149 void reset();
150
151 Signal& signal();
152
153 void set_clock_mode(ClockMode mode);
154 void set_bit_delay(cycle_count_t delay);
155
156 void set_stopbits(unsigned short count);
157 void set_databits(unsigned short count);
158 void set_parity(Parity parity);
159
160 void set_tx_buffer_limit(size_t limit);
161 void push_tx(uint16_t frame);
162 void cancel_tx_pending();
163 size_t tx_pending() const;
164 void set_tx_dir_enabled(bool enabled);
165 bool tx_in_progress() const;
166
167 void set_rx_buffer_limit(size_t limit);
168 void set_rx_enabled(bool enabled);
169 size_t rx_available() const;
170 void pop_rx();
171 uint16_t read_rx() const;
172 bool has_frame_error() const;
173 bool has_parity_error() const;
174 bool has_rx_overrun() const;
175 bool rx_in_progress() const;
176
177 void push_rx_frame(uint16_t frame);
178
179 void set_paused(bool enabled);
180
181 uint16_t build_frame(uint16_t data) const;
182 uint16_t parse_frame(uint16_t frame) const;
183
184 void line_state_changed(Line line, bool new_state);
185
186protected:
187
188 virtual void set_line_state(Line line, bool state) = 0;
189 virtual bool get_line_state(Line line) const = 0;
190
191private:
192
193 CycleManager* m_cycle_manager;
194 Logger* m_logger;
195
196 Signal m_signal;
197
198 //=================================
199 //Clock management
200 cycle_count_t m_delay; //Bit duration in clock cycles
201 ClockMode m_clk_mode;
203
204 unsigned short m_databits;
205 unsigned short m_stopbits;
206 Parity m_parity;
207
208 uint16_t m_tx_shifter;
209 unsigned short m_tx_shift_counter;
210 std::deque<uint16_t> m_tx_buffer;
211 //Size limit for the TX FIFO, including the shift register
212 size_t m_tx_limit;
213 //Collision flag
214 //bool m_tx_collision;
215 bool m_tx_dir_enabled;
216 //Cycle timer to simulate the delay to emit a frame
218
219 //Enable/disable flag for RX
220 bool m_rx_enabled;
221 uint16_t m_rx_shifter;
222 unsigned short m_rx_shift_counter;
223 //RX FIFO buffers
224 std::deque<uint16_t> m_rx_buffer;
225 std::deque<uint16_t> m_rx_pending;
226 //Size limit for the received part of the RX FIFO
227 //The pending part of the FIFO is not limited
228 size_t m_rx_limit;
229 //Cycle timer to simulate the delay to receive a frame
231
232 //Pause flag for both RX and TX
233 bool m_paused;
234
235 unsigned short framesize() const;
236
237 void fill_tx_shifter();
238 void start_bitwise_tx();
239 void shift_tx();
240
241 void start_bitwise_rx();
242 void shift_rx();
243
244 cycle_count_t clk_timer_next(cycle_count_t when);
245 cycle_count_t tx_timer_next(cycle_count_t when);
246 cycle_count_t rx_timer_next(cycle_count_t when);
247
248 void process_clock_change(bool new_state);
249
250};
251
254{
255 return m_signal;
256}
257
259inline size_t USART::rx_available() const
260{
261 return m_rx_buffer.size();
262}
263
265inline size_t USART::tx_pending() const
266{
267 return m_tx_buffer.size() ? (m_tx_buffer.size() - 1) : 0;
268}
269
270
271}; //namespace UART
272
273
275
276#endif //__YASIMAVR_UART_H__
Definition sim_cycle_timer.h:85
Definition sim_cycle_timer.h:134
Definition sim_logger.h:91
Signalling framework class.
Definition sim_signal.h:97
Generic model defining an universal synchronous/asynchronous serial interface a.k....
Definition sim_uart.h:140
size_t rx_available() const
number of frames stored in the RX buffer.
Definition sim_uart.h:259
virtual bool get_line_state(Line line) const =0
size_t tx_pending() const
Number of frames waiting in the buffer to be emitted.
Definition sim_uart.h:265
Signal & signal()
Internal signal used for operation signalling.
Definition sim_uart.h:253
virtual ~USART()=default
virtual void set_line_state(Line line, bool state)=0
Common enums and signal definitions for UART classes.
Definition sim_uart.h:64
SignalId
Definition sim_uart.h:66
@ Signal_TX_Collision
Raised when the TX buffer overruns.
Definition sim_uart.h:84
@ Signal_TX_Data
Raised on transmission of a frame. sigdata contains the data transmitted.
Definition sim_uart.h:80
@ Signal_TX_Frame
Raised on transmission of a frame. sigdata contains the raw frame transmitted.
Definition sim_uart.h:78
@ Signal_TX_Complete
Definition sim_uart.h:71
@ Signal_RX_Start
Raised at the start of a frame reception.
Definition sim_uart.h:73
@ Signal_TX_Start
Raised at the start of a frame transmission, with sigdata containing the frame.
Definition sim_uart.h:68
@ Signal_RX_Overflow
Raised when the RX buffer overruns.
Definition sim_uart.h:82
@ Signal_RX_Complete
Definition sim_uart.h:76
ClockMode
Definition sim_uart.h:87
@ Clock_Emitter
Synchronous mode where the interface is the clock master.
Definition sim_uart.h:91
@ Clock_Async
Asynchronous mode, the clock line is not used.
Definition sim_uart.h:89
@ Clock_Receiver
Synchronous mode where the interface is not the clock master.
Definition sim_uart.h:93
Line
Definition sim_uart.h:99
@ Line_TXD
Definition sim_uart.h:100
@ Line_RXD
Definition sim_uart.h:101
@ Line_XCK
Definition sim_uart.h:102
@ Line_DIR
Definition sim_uart.h:103
Parity
Definition sim_uart.h:109
@ Parity_Odd
Definition sim_uart.h:111
@ Parity_No
Definition sim_uart.h:110
@ Parity_Even
Definition sim_uart.h:112
#define YASIMAVR_BEGIN_NAMESPACE
Definition sim_globals.h:58
#define AVR_CORE_PUBLIC_API
Definition sim_globals.h:46
#define YASIMAVR_END_NAMESPACE
Definition sim_globals.h:59
YASIMAVR_BEGIN_NAMESPACE typedef long long cycle_count_t
Definition sim_types.h:40