uart: Added logic to regulate data rate
[simavr] / simavr / sim / avr_uart.h
1 /*
2         avr_uart.h
3
4         Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
5
6         This file is part of simavr.
7
8         simavr 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         simavr 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 simavr.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #ifndef AVR_UART_H_
23 #define AVR_UART_H_
24
25 #include "sim_avr.h"
26
27 #include "fifo_declare.h"
28
29 DECLARE_FIFO(uint8_t, uart_fifo, 64);
30
31 /*
32  * The method of "connecting" the the UART from external code is to use 4 IRQS.
33  * The easy one is UART->YOU, where you will be called with the byte everytime
34  * the AVR firmware sends one. Do whatever you like with it.
35  *
36  * The slightly more tricky one is the INPUT part. Since the AVR is quite a bit
37  * slower than your code most likely, there is a way for the AVR UART to tell
38  * you to "pause" sending it bytes when its own input buffer is full.
39  * So, the UART will send XON to you when its fifo is empty, XON means you can
40  * send as many bytes as you have until XOFF is sent. Note that these are two
41  * IRQs because you /will/ be called with XOFF when sending a byte in INPUT...
42  * So it's a reentrant process.
43  *
44  * When XOFF has been called, do not send any new bytes, they would be dropped.
45  * Instead wait for XON again and continue.
46  * See examples/parts/uart_udp.c for a full implementation
47  *
48  * Pseudo code:
49  *
50  * volatile int off = 0;
51  * void irq_xon()
52  * {
53  *     off = 0;
54  *     while (!off && bytes_left)
55  *     avr_raise_irq(UART_IRQ_INPUT, a_byte);
56  * }
57  * void irq_xoff()
58  * {
59  *     off = 1;
60  * }
61  *
62  */
63 enum {
64         UART_IRQ_INPUT = 0,
65         UART_IRQ_OUTPUT,
66         UART_IRQ_OUT_XON,               // signaled (continuously) when input fifo is not full
67         UART_IRQ_OUT_XOFF,              // signaled when input fifo IS full
68         UART_IRQ_COUNT
69 };
70
71 // add port number to get the real IRQ
72 #define AVR_IOCTL_UART_GETIRQ(_name) AVR_IOCTL_DEF('u','a','r',(_name))
73
74 enum {
75         // the uart code monitors for firmware that pool on
76         // reception registers, and can do an atomic usleep()
77         // if it's detected, this helps regulating CPU
78         AVR_UART_FLAG_POOL_SLEEP = (1 << 0),
79         AVR_UART_FLAG_STDIO = (1 << 1),                 // print lines on the console
80 };
81
82 typedef struct avr_uart_t {
83         avr_io_t        io;
84         char name;
85         avr_regbit_t    disabled;       // bit in the PRR
86         
87         avr_io_addr_t r_udr;
88         avr_io_addr_t r_ucsra;
89         avr_io_addr_t r_ucsrb;
90         avr_io_addr_t r_ucsrc;
91
92         avr_regbit_t    rxen;           // receive enabled
93         avr_regbit_t    txen;           // transmit enable
94         avr_regbit_t    u2x;            // double UART speed
95         avr_regbit_t    usbs;           // stop bits
96         avr_regbit_t    ucsz;           // data bits
97         avr_regbit_t    ucsz2;          // data bits, continued
98
99         avr_io_addr_t r_ubrrl,r_ubrrh;
100
101         avr_int_vector_t rxc;
102         avr_int_vector_t txc;
103         avr_int_vector_t udrc;  
104
105         uart_fifo_t     input;
106
107         uint32_t                flags;
108         avr_cycle_count_t usec_per_byte;
109 } avr_uart_t;
110
111 /* takes a uint32_t* as parameter */
112 #define AVR_IOCTL_UART_SET_FLAGS(_name) AVR_IOCTL_DEF('u','a','s',(_name))
113 #define AVR_IOCTL_UART_GET_FLAGS(_name) AVR_IOCTL_DEF('u','a','g',(_name))
114
115 void avr_uart_init(avr_t * avr, avr_uart_t * port);
116
117 #endif /* AVR_UART_H_ */