2 * Copyright Droids Corporation, Microb Technology, Eirbot (2005)
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Revision : $Id: uart_getconf.c,v 1.1.2.3 2009-02-20 20:16:09 zer0 Exp $
22 /* Olivier MATZ, Droids-corp 2004 - 2007 */
25 #include <uart_defs.h>
26 #include <uart_private.h>
30 static inline uint8_t get_ucsrc(uint8_t num)
34 /* on some uC, reading UCSRxC is a bit tricky */
66 return *uart_regs[num].ucsrc;
70 /* return number of bits in current conf. Intr must be disabled. */
71 uint8_t uart_getconf_nbits(uint8_t num)
75 nbits = (get_ucsrc(num) >> UCSZ0) & 0x03;
76 #ifdef CONFIG_MODULE_UART_9BITS
77 if (*uart_regs[num].ucsrb & (1 << UCSZ2))
84 #else /* UART_IS_USART */
86 /* return number of bits in current conf */
87 uint8_t uart_getconf_nbits(uint8_t num)
89 #ifdef CONFIG_MODULE_UART_9BITS
90 if (*uart_regs[num].ucsrb & (uint8_t)(1 << CHR9))
99 #endif /* UART_IS_USART */
104 /* return number of bits in current conf */
105 static inline uint16_t uart_get_baudreg(uint8_t num)
107 return ((uint16_t)*uart_regs[num].ubrrh << 8) |
108 (uint16_t)*uart_regs[num].ubrrl;
111 #else /* UART_IS_USART */
113 /* return number of bits in current conf */
114 static inline uint16_t uart_get_baudreg(uint8_t num)
116 return (uint16_t)*uart_regs[num].ubrrl;
119 #endif /* UART_IS_USART */
122 /* get the running uart configurtion */
123 void uart_getconf(uint8_t num, struct uart_config *u)
131 /* enabled if RXEN is set */
132 if (*uart_regs[num].ucsrb & (1 << RXEN))
137 /* intrp enabled if RXCIE is set */
138 if (*uart_regs[num].ucsrb & (1 << RXCIE))
143 /* use double speed */
144 if (UART_HAS_U2X && (*uart_regs[num].ucsra & (1 << U2X)))
145 u->use_double_speed = 1;
147 u->use_double_speed = 0;
152 tmp = get_ucsrc(num) & ((1 << UPM1) | (1 << UPM0));
153 if (tmp == ((1 << UPM1) | (1 << UPM0)))
154 u->parity = UART_PARTITY_ODD;
155 else if (tmp == (1 << UPM1))
156 u->parity = UART_PARTITY_EVEN;
158 u->parity = UART_PARTITY_NONE;
161 u->parity = UART_PARTITY_NONE;
165 if (UART_IS_USART && (get_ucsrc(num) & (1 << USBS))) {
166 u->stop_bits = UART_STOP_BITS_2;
169 u->stop_bits = UART_STOP_BITS_1;
173 u->nbits = uart_getconf_nbits(num);
174 u->baudrate = (F_CPU / ((uart_get_baudreg(num)+1) * 16)) ;