ensure transmission is completed

This commit is contained in:
King Kévin 2015-03-03 10:30:35 +01:00
parent 8de6b2c199
commit 74819c86a2
2 changed files with 15 additions and 13 deletions

View File

@ -30,31 +30,33 @@ FILE uart_io = FDEV_SETUP_STREAM((int (*)(char, struct __file *)) uart_putchar,
/* configure serial port */
void uart_init(void)
{
UBRR0H = UBRRH_VALUE;
UBRR0L = UBRRL_VALUE;
UBRR0H = UBRRH_VALUE; // set baurate (calculated from BAUD)
UBRR0L = UBRRL_VALUE; // set baurate (calculated from BAUD)
#if USE_2X
UCSR0A |= (1<<U2X0);
UCSR0A |= (1<<U2X0); // use double speed (set depending on BAUD)
#else
UCSR0A &= ~(1<<U2X0);
UCSR0A &= ~(1<<U2X0); // do not use double speed (set depending on BAUD)
#endif
UCSR0C = (1<<UCSZ01)|(1<<UCSZ00); /* 8N1 bit data */
UCSR0B = (1<<RXEN0)|(1<<TXEN0); /* enable RX and TX */
UCSR0B |= (1<<RXCIE0); /* enable RX complete interrupt */
UCSR0C = (1<<UCSZ01)|(1<<UCSZ00); // 8N1 bit data
UCSR0B = (1<<RXEN0)|(1<<TXEN0); // enable RX and TX
UCSR0B |= (1<<RXCIE0); // enable RX complete interrupt
}
/* put character on UART stream */
void uart_putchar(char c, FILE *stream)
{
if (c == '\n') {
uart_putchar('\r', stream); /* add carrier return before line feed. this is recommended for most UART terminals */
if (c == '\n') { // add carrier return before line feed. this is recommended for most UART terminals
uart_putchar('\r', stream);
}
loop_until_bit_is_set(UCSR0A, UDRE0);
UDR0 = c;
while (!(UCSR0A&(1<<UDRE0))); // wait for transmit register to empty
UDR0 = c; // put data in transmit register
while (!(UCSR0A&(1<<TXC0))); // wait for transmission to complete (else sleep mode can break it)
UCSR0A |= (1<<TXC0); // clear transmit flag
}
/* get character from UART stream (blocking) */
char uart_getchar(FILE *stream)
{
loop_until_bit_is_set(UCSR0A, RXC0); /* wait until data exists */
while (!(UCSR0A&(1<<RXC0))); // wait until data exists
return UDR0;
}

View File

@ -26,7 +26,7 @@ FILE uart_io;
/* configure serial port */
void uart_init(void);
/* put character on UART stream */
/* put character on UART stream (blocking) */
void uart_putchar(char c, FILE *stream);
/* get character from UART stream (blocking) */
char uart_getchar(FILE *stream);