4 Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
6 This file is part of simavr.
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.
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.
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/>.
27 #include "fifo_declare.h"
29 DECLARE_FIFO(uint8_t, uart_fifo, 64);
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.
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.
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
50 * volatile int off = 0;
54 * while (!off && bytes_left)
55 * avr_raise_irq(UART_IRQ_INPUT, a_byte);
66 UART_IRQ_OUT_XON, // signaled (continuously) when input fifo is not full
67 UART_IRQ_OUT_XOFF, // signaled when input fifo IS full
71 // add port number to get the real IRQ
72 #define AVR_IOCTL_UART_GETIRQ(_name) AVR_IOCTL_DEF('u','a','r',(_name))
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
82 typedef struct avr_uart_t {
85 avr_regbit_t disabled; // bit in the PRR
88 avr_io_addr_t r_ucsra;
89 avr_io_addr_t r_ucsrb;
90 avr_io_addr_t r_ucsrc;
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
99 avr_io_addr_t r_ubrrl,r_ubrrh;
101 avr_int_vector_t rxc;
102 avr_int_vector_t txc;
103 avr_int_vector_t udrc;
108 avr_cycle_count_t usec_per_byte;
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))
115 void avr_uart_init(avr_t * avr, avr_uart_t * port);
117 #endif /* AVR_UART_H_ */