/*
+ * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de>
+ *
+ * SPDX-License-Identifier: GPL-2.0+
*/
#include <avr/io.h>
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;
/* 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;
};
}
/*--------------------------------------------------------------------------*/
-void serial_setup(void)
+void serial_setup(unsigned long baud)
{
stdout = &mystdout;
- usart0_setup();
+ usart0_setup(baud);
}
/*--------------------------------------------------------------------------*/
return ring_read_ch(&rx_ring);
}
-void serial_putc(uint8_t data)
+void serial_putc(char data)
{
while (ring_write_ch(&tx_ring, data) < 0)
;
UCSR0B = _BV(RXCIE0) | _BV(RXEN0) | _BV(TXEN0) | _BV(UDRIE0);
}
+uint_fast8_t serial_tstc(void)
+{
+ return !ring_is_empty(&rx_ring);
+}