X-Git-Url: http://cloudbase.mooo.com/gitweb/z180-stamp.git/blobdiff_plain/4bc81323e51ac138e733c14eeb6f283ae706e904..2c60e1dc26e1f424d9673541aebe778601070838:/avr/serial.c diff --git a/avr/serial.c b/avr/serial.c index b2fea51..d3aeb4e 100644 --- a/avr/serial.c +++ b/avr/serial.c @@ -1,4 +1,7 @@ /* + * (C) Copyright 2014 Leo C. + * + * SPDX-License-Identifier: GPL-2.0+ */ #include @@ -12,19 +15,19 @@ static int _write(char c, FILE *stream); -static FILE mystdout = FDEV_SETUP_STREAM(_write, +static FILE mystdout = FDEV_SETUP_STREAM(_write, NULL, _FDEV_SETUP_WRITE); -#define BUFFER_SIZE 64 +#define BUFFER_SIZE 128 #if ((BUFFER_SIZE-1) & BUFFER_SIZE) # error: BUFFER_SIZE not power of 2 #endif #if ((BUFFER_SIZE) > 256) -# error: BUFFER_SIZE +# error: BUFFER_SIZE #endif struct ring rx_ring; @@ -36,21 +39,20 @@ uint8_t tx_ring_buffer[BUFFER_SIZE]; /* Initialize UART */ -void usart0_setup(void) { +void usart0_setup(unsigned long baud) { ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { PRR0 &= ~_BV(PRUSART0); UCSR0B = 0; + UCSR0A = 0; + UBRR0 = (F_CPU + (baud/2) + 8) / baud / 16 - 1; + UCSR0B = _BV(RXCIE0) | _BV(RXEN0) | _BV(TXEN0); + UCSR0C = 3 << UCSZ00; /* Initialize ring buffers. */ ring_init(&rx_ring, rx_ring_buffer, BUFFER_SIZE); ring_init(&tx_ring, tx_ring_buffer, BUFFER_SIZE); - - UCSR0A = 0; - UBRR0 = F_CPU / BAUD / 16 - 1; - UCSR0B = _BV(RXCIE0) | _BV(RXEN0) | _BV(TXEN0); - UCSR0C = 3 << UCSZ00; }; } @@ -82,10 +84,10 @@ ISR(USART0_UDRE_vect) /*--------------------------------------------------------------------------*/ -void serial_setup(void) +void serial_setup(unsigned long baud) { stdout = &mystdout; - usart0_setup(); + usart0_setup(baud); } /*--------------------------------------------------------------------------*/ @@ -119,5 +121,3 @@ uint_fast8_t serial_tstc(void) { return !ring_is_empty(&rx_ring); } - -