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