Renamed uart functions and added support for stderr via uart3

This commit is contained in:
Arti Zirk 2016-09-10 00:56:05 +03:00
parent 8c5f723619
commit ae55fbac04
2 changed files with 48 additions and 13 deletions

View File

@ -12,29 +12,61 @@
/* http://www.cs.mun.ca/~rod/Winter2007/4723/notes/serial/serial.html */ /* http://www.cs.mun.ca/~rod/Winter2007/4723/notes/serial/serial.html */
void uart_init(void) { void uart0_init(void)
{
UBRR0H = UBRRH_VALUE; UBRR0H = UBRRH_VALUE;
UBRR0L = UBRRL_VALUE; UBRR0L = UBRRL_VALUE;
#if USE_2X #if USE_2X
UCSR0A |= _BV(U2X0); UCSR0A |= _BV(U2X0);
#else #else
UCSR0A &= ~(_BV(U2X0)); UCSR0A &= ~(_BV(U2X0));
#endif #endif
UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); /* 8-bit data */ UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); /* 8-bit data */
UCSR0B = _BV(RXEN0) | _BV(TXEN0); /* Enable RX and TX */ UCSR0B = _BV(RXEN0) | _BV(TXEN0); /* Enable RX and TX */
} }
void uart_putchar(char c, FILE *stream) { void uart3_init(void)
if (c == '\n') { {
uart_putchar('\r', stream); UBRR3H = UBRRH_VALUE;
} UBRR3L = UBRRL_VALUE;
loop_until_bit_is_set(UCSR0A, UDRE0); #if USE_2X
UDR0 = c; UCSR3A |= _BV(U2X3);
#else
UCSR3A &= ~(_BV(U2X3));
#endif
UCSR3C = _BV(UCSZ31) | _BV(UCSZ30); /* 8-bit data */
UCSR3B = _BV(TXEN3); /* Enable RX and TX */
} }
char uart_getchar(FILE *stream) { int uart0_putchar(char c, FILE *stream)
{
(void) stream;
if (c == '\n') {
uart0_putchar('\r', stream);
}
loop_until_bit_is_set(UCSR0A, UDRE0);
UDR0 = c;
return 0;
}
int uart3_putchar(char c, FILE *stream)
{
(void) stream;
if (c == '\n') {
uart3_putchar('\r', stream);
}
loop_until_bit_is_set(UCSR3A, UDRE3);
UDR3 = c;
return 0;
}
int uart0_getchar(FILE *stream)
{
(void) stream;
loop_until_bit_is_set(UCSR0A, RXC0); loop_until_bit_is_set(UCSR0A, RXC0);
return UDR0; return UDR0;
} }

View File

@ -1,9 +1,12 @@
void uart_putchar(char c, FILE *stream); int uart0_putchar(char c, FILE *stream);
char uart_getchar(FILE *stream); int uart0_getchar(FILE *stream);
void uart_init(void); int uart3_putchar(char c, FILE *stream);
void uart0_init(void);
void uart3_init(void);
/* http://www.ermicro.com/blog/?p=325 */ /* http://www.ermicro.com/blog/?p=325 */
FILE uart_output = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE); FILE uart0_io = FDEV_SETUP_STREAM(uart0_putchar, uart0_getchar, _FDEV_SETUP_RW);
FILE uart_input = FDEV_SETUP_STREAM(NULL, uart_getchar, _FDEV_SETUP_READ); FILE uart3_out = FDEV_SETUP_STREAM(uart3_putchar, NULL, _FDEV_SETUP_WRITE);