From e4ab4045f64cf3fe49e7cc806407ffaa067853fd Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Wed, 25 May 2016 23:44:21 -0500 Subject: [PATCH] This code is violently stupid, but SOON I will be able to handle incoming packet data --- avr/main.c | 10 ++++++++-- avr/uart.c | 34 ++++++++++++++++++++++++++++++++++ include/tabby/avr.h | 6 ------ include/tabby/avr/uart.h | 12 ++++++++++++ 4 files changed, 54 insertions(+), 8 deletions(-) create mode 100644 avr/uart.c delete mode 100644 include/tabby/avr.h create mode 100644 include/tabby/avr/uart.h diff --git a/avr/main.c b/avr/main.c index 26d386d..0d30910 100644 --- a/avr/main.c +++ b/avr/main.c @@ -4,6 +4,7 @@ #include #include +#include static volatile uint8_t bits = 0; static volatile uint8_t value_in = 0x00; /* Data coming in from Game Boy */ @@ -38,9 +39,9 @@ ISR(TIM0_COMPB_vect) { value_out <<= 1; if (--bits == 0) { - putchar(value_in); + uart_putchar(value_in, NULL); - value_out = getchar(); + value_out = uart_getchar(NULL); bits = 8; } @@ -100,6 +101,11 @@ static void snooze() { } int main() { + /* + * Best turn on the serial UART! + */ + uart_init(); + /* * By default, the Game Boy link port is configured to monitor for external * clock pulses, so we'll go ahead and do that in this case. diff --git a/avr/uart.c b/avr/uart.c new file mode 100644 index 0000000..2c86659 --- /dev/null +++ b/avr/uart.c @@ -0,0 +1,34 @@ +#include + +#include + +#define F_CPU 16000000UL +#define BAUD TABBY_AVR_UART_BAUD + +#include + +void uart_init() { + UBRR0H = UBRRH_VALUE; + UBRR0L = UBRRL_VALUE; + +#if USE_2X + UCSR0A |= _BV(U2X0); +#else + UCSR0A &= ~(_BV(U2X0)); +#endif + + UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); + UCSR0B = _BV(RXEN0) | _BV(TXEN0); +} + +void uart_putchar(char c, FILE *fh) { + loop_until_bit_is_set(UCSR0A, UDRE0); + + UDR0 = c; +} + +char uart_getchar(FILE *fh) { + loop_until_bit_is_set(UCSR0A, RXC0); + + return UDR0; +} diff --git a/include/tabby/avr.h b/include/tabby/avr.h deleted file mode 100644 index a13b370..0000000 --- a/include/tabby/avr.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef _TABBY_AVR_H -#define _TABBY_AVR_H - -#define TABBY_AVR_SERIAL_BAUD 115200 - -#endif /* _TABBY_AVR_H */ diff --git a/include/tabby/avr/uart.h b/include/tabby/avr/uart.h new file mode 100644 index 0000000..3e47d5f --- /dev/null +++ b/include/tabby/avr/uart.h @@ -0,0 +1,12 @@ +#ifndef _TABBY_AVR_UART_H +#define _TABBY_AVR_UART_H + +#define TABBY_AVR_UART_BAUD 115200 + +void uart_init(); + +void uart_putchar(char c, FILE *fh); + +char uart_getchar(FILE *fh); + +#endif /* _TABBY_AVR_UART_H */