00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <uart.h>
00025 #include <uart_defs.h>
00026 #include <uart_private.h>
00027
00028 #if UART_IS_USART
00029
00030 static inline uint8_t get_ucsrc(uint8_t num)
00031 {
00032 #ifdef URSEL
00033 uint8_t tmp;
00034
00035 switch(num) {
00036 #ifdef UART0_COMPILE
00037 case 0:
00038 tmp = UBRR0H;
00039 tmp = UCSR0C;
00040 break;
00041 #endif
00042 #ifdef UART1_COMPILE
00043 case 1:
00044 tmp = UBRR1H;
00045 tmp = UCSR1C;
00046 break;
00047 #endif
00048 #ifdef UART2_COMPILE
00049 case 2:
00050 tmp = UBRR2H;
00051 tmp = UCSR2C;
00052 break;
00053 #endif
00054 #ifdef UART3_COMPILE
00055 case 3:
00056 tmp = UBRR3H;
00057 tmp = UCSR3C;
00058 break;
00059 #endif
00060 default:
00061 tmp = 0;
00062 break;
00063 }
00064 return tmp;
00065 #else
00066 return *uart_regs[num].ucsrc;
00067 #endif
00068 }
00069
00070
00071 uint8_t uart_getconf_nbits(uint8_t num)
00072 {
00073 uint8_t nbits;
00074
00075 nbits = (get_ucsrc(num) >> UCSZ0) & 0x03;
00076 #ifdef CONFIG_MODULE_UART_9BITS
00077 if (*uart_regs[num].ucsrb & (1 << UCSZ2))
00078 nbits += 4;
00079 #endif
00080 nbits += 5;
00081 return nbits;
00082 }
00083
00084 #else
00085
00086
00087 uint8_t uart_getconf_nbits(uint8_t num)
00088 {
00089 #ifdef CONFIG_MODULE_UART_9BITS
00090 if (*uart_regs[num].ucsrb & (uint8_t)(1 << CHR9))
00091 return 8;
00092 else
00093 return 9;
00094 #else
00095 return 8;
00096 #endif
00097 }
00098
00099 #endif
00100
00101
00102 #if UART_IS_USART
00103
00104
00105 static inline uint16_t uart_get_baudreg(uint8_t num)
00106 {
00107 return ((uint16_t)*uart_regs[num].ubrrh << 8) |
00108 (uint16_t)*uart_regs[num].ubrrl;
00109 }
00110
00111 #else
00112
00113
00114 static inline uint16_t uart_get_baudreg(uint8_t num)
00115 {
00116 return (uint16_t)*uart_regs[num].ubrrl;
00117 }
00118
00119 #endif
00120
00121
00122
00123 void uart_getconf(uint8_t num, struct uart_config *u)
00124 {
00125 uint8_t tmp;
00126 uint8_t flags;
00127
00128 IRQ_LOCK(flags);
00129
00130
00131
00132 if (*uart_regs[num].ucsrb & (1 << RXEN))
00133 u->enabled = 1;
00134 else
00135 u->enabled = 0;
00136
00137
00138 if (*uart_regs[num].ucsrb & (1 << RXCIE))
00139 u->intr_enabled = 1;
00140 else
00141 u->intr_enabled = 0;
00142
00143
00144 if (UART_HAS_U2X && (*uart_regs[num].ucsra & (1 << U2X)))
00145 u->use_double_speed = 1;
00146 else
00147 u->use_double_speed = 0;
00148
00149
00150
00151 if (UART_IS_USART) {
00152 tmp = get_ucsrc(num) & ((1 << UPM1) | (1 << UPM0));
00153 if (tmp == ((1 << UPM1) | (1 << UPM0)))
00154 u->parity = UART_PARTITY_ODD;
00155 else if (tmp == (1 << UPM1))
00156 u->parity = UART_PARTITY_EVEN;
00157 else
00158 u->parity = UART_PARTITY_NONE;
00159 }
00160 else {
00161 u->parity = UART_PARTITY_NONE;
00162 }
00163
00164
00165 if (UART_IS_USART && (get_ucsrc(num) & (1 << USBS))) {
00166 u->stop_bits = UART_STOP_BITS_2;
00167 }
00168 else {
00169 u->stop_bits = UART_STOP_BITS_1;
00170 }
00171
00172
00173 u->nbits = uart_getconf_nbits(num);
00174 u->baudrate = (F_CPU / ((uart_get_baudreg(num)+1) * 16)) ;
00175
00176 IRQ_UNLOCK(flags);
00177 }