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/>.
22 #ifndef __AVR_UART_H__
23 #define __AVR_UART_H__
31 #include "fifo_declare.h"
33 DECLARE_FIFO(uint8_t, uart_fifo, 64);
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.
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.
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
54 * volatile int off = 0;
58 * while (!off && bytes_left)
59 * avr_raise_irq(UART_IRQ_INPUT, a_byte);
70 UART_IRQ_OUT_XON, // signaled (continuously) when input fifo is not full
71 UART_IRQ_OUT_XOFF, // signaled when input fifo IS full
75 // add port number to get the real IRQ
76 #define AVR_IOCTL_UART_GETIRQ(_name) AVR_IOCTL_DEF('u','a','r',(_name))
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
86 typedef struct avr_uart_t {
89 avr_regbit_t disabled; // bit in the PRR
92 avr_io_addr_t r_ucsra;
93 avr_io_addr_t r_ucsrb;
94 avr_io_addr_t r_ucsrc;
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
103 avr_io_addr_t r_ubrrl,r_ubrrh;
105 avr_int_vector_t rxc;
106 avr_int_vector_t txc;
107 avr_int_vector_t udrc;
112 avr_cycle_count_t usec_per_byte;
115 int stdio_len; // current size in the stdio output
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))
122 void avr_uart_init(avr_t * avr, avr_uart_t * port);
128 #endif /*__AVR_UART_H__*/