From c679426262d49f4591f22a1fd8d41458e233921f Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Wed, 1 Jun 2016 22:30:12 -0500 Subject: [PATCH] *shrug* Gotta commit somethin' --- avr/Makefile | 35 +++++++++---- avr/{main.c => recv.c} | 2 - avr/send.c | 106 +++++++++++++++++++++++++++++++++++++++ avr/uart.c | 3 +- include/tabby/avr/uart.h | 2 + 5 files changed, 134 insertions(+), 14 deletions(-) rename avr/{main.c => recv.c} (99%) create mode 100644 avr/send.c diff --git a/avr/Makefile b/avr/Makefile index 66ea28a..7ade12e 100644 --- a/avr/Makefile +++ b/avr/Makefile @@ -1,11 +1,14 @@ include ../mk/build.mk +CLOCK_SPEED = 16000000L + INCLUDE_PATH = ../include HEADER_SUBDIR = tabby CROSS = avr- CC = $(CROSS)gcc -CFLAGS = $(CGFLAGS) -Wall -Os -mmcu=atmega328p -I$(INCLUDE_PATH) +CFLAGS = $(CGFLAGS) -Wall -Os -mmcu=atmega328p -I$(INCLUDE_PATH) \ + -DF_CPU=$(CLOCK_SPEED) LDFLAGS = OBJCOPY = $(CROSS)objcopy @@ -20,21 +23,33 @@ HEADERS_BUILD = $(HEADERS_LOCAL) \ $(addprefix $(INCLUDE_PATH)/$(HEADER_SUBDIR)/,$(HEADERS)) HEADERS = avr/buffer.h avr/uart.h link.h -OBJS = main.o uart.o +OBJS = send.o recv.o uart.o -IMAGE_NAME = tabby -IMAGE_ELF = $(IMAGE_NAME).elf -IMAGE_BIN = $(IMAGE_NAME).bin +RECV_NAME = recv +RECV_BIN = $(RECV_NAME).bin +RECV_ELF = $(RECV_NAME).elf +RECV_OBJS = recv.o uart.o + +SEND_NAME = send +SEND_BIN = $(SEND_NAME).bin +SEND_ELF = $(SEND_NAME).elf +SEND_OBJS = send.o uart.o + +IMAGES_BIN = $(RECV_BIN) $(SEND_BIN) +IMAGES_ELF = $(RECV_ELF) $(SEND_ELF) RM = /bin/rm -all: $(IMAGE_BIN) +all: $(IMAGES_BIN) -$(IMAGE_BIN): $(IMAGE_ELF) +$(IMAGES_BIN): %.bin: %.elf $(OBJCOPY) $(OBJCOPY_FLAGS) -O binary $< $@ -$(IMAGE_ELF): $(OBJS) - $(CC) $(CFLAGS) $(OBJS) -o $@ +$(SEND_ELF): $(SEND_OBJS) + $(CC) $(CFLAGS) $(SEND_OBJS) -o $@ + +$(RECV_ELF): $(RECV_OBJS) + $(CC) $(CFLAGS) $(RECV_OBJS) -o $@ $(OBJS): %.o: %.c $(HEADERS_BUILD) $(CC) $(CFLAGS) -c $< @@ -43,4 +58,4 @@ flash: $(IMAGE_BIN) $(AVRDUDE) $(AVRDUDE_FLAGS) -U flash:w:$(IMAGE_BIN):r clean: - $(RM) -f $(IMAGE_BIN) $(IMAGE_ELF) $(OBJS) + $(RM) -f $(IMAGES_BIN) $(IMAGES_ELF) $(OBJS) diff --git a/avr/main.c b/avr/recv.c similarity index 99% rename from avr/main.c rename to avr/recv.c index 5f81dda..c6f3260 100644 --- a/avr/main.c +++ b/avr/recv.c @@ -1,5 +1,3 @@ -#include - #include #include #include diff --git a/avr/send.c b/avr/send.c new file mode 100644 index 0000000..7411ded --- /dev/null +++ b/avr/send.c @@ -0,0 +1,106 @@ +#include +#include +#include + +#include +#include + +#define TIMER0_INTERVAL 1953 + +static volatile uint8_t bits = 0; +static volatile uint8_t value_in = 0x00; /* Data coming in from Game Boy */ +static volatile uint8_t value_out = 0x00; /* Data going out to Game Boy */ + +static volatile tabby_printer_packet header = { + .type = 0, + .compression = 0, + .size = 0 +}; + +static volatile uint16_t i = 0, + b = 0; + +/* + * Internal clock source interrupt vector + */ +ISR(TIMER0_COMPB_vect) { + value_in >>= 1; + + PORTB |= (1 << PORTB5); + + if (PORTB & (1 << PORTB4)) { + value_in |= 0x80; + } + + if (value_out & 0x80) { + PORTB |= (1 << PORTB3); + } else { + PORTB &= ~(1 << PORTB3); + } + + value_out <<= 1; + + if (--bits == 0) { + bits = 8; + } + + /* + * Chill out for 60µsec, then ride out the rest of the time until the next + * interrupt with a high as fuck SCK + */ + _delay_us(60); + + PORTB &= ~(1 << PORTB5); +} + +static void clock_setup() { + /* + * Configure MISO as output + */ + DDRB |= (1 << DDB4); + + /* + * Configure MOSI as input + */ + DDRB &= ~(1 << DDB3); + + /* + * Configure SCK pin as output + */ + DDRB |= (1 << DDB5); + + /* + * Enable timer interrupt vector + */ + TIMSK0 = (1 << TOIE1); + + /* + * Reset timer counter to zero + */ + TCNT1 = 0; + + /* + * Set timer interval + */ + OCR1A = TIMER0_INTERVAL; + + /* + * Set timer clock divider to 1/1 + */ + TCCR1B = (1 << CS10); +} + +int main() { + uart_init(); + + clock_setup(); + + sei(); + + while (1) { + uint8_t c = uart_getchar(NULL); + + } + + return 0; +} diff --git a/avr/uart.c b/avr/uart.c index 2397d10..22c512f 100644 --- a/avr/uart.c +++ b/avr/uart.c @@ -5,8 +5,7 @@ #include -#define F_CPU 16000000UL -#define BAUD TABBY_AVR_UART_BAUD +#define BAUD TABBY_AVR_UART_BAUD #include diff --git a/include/tabby/avr/uart.h b/include/tabby/avr/uart.h index 55c34a1..0db78ba 100644 --- a/include/tabby/avr/uart.h +++ b/include/tabby/avr/uart.h @@ -1,6 +1,8 @@ #ifndef _TABBY_AVR_UART_H #define _TABBY_AVR_UART_H +#include + #define TABBY_AVR_UART_BAUD 57600 void uart_init();