Yikes, did I actually make a thing that might be used to talk to the Game Boy Printer? I think so.

This commit is contained in:
XANTRONIX Development 2016-06-01 23:40:40 -05:00
parent c679426262
commit 75b588a288
2 changed files with 134 additions and 4 deletions

View file

@ -54,8 +54,11 @@ $(RECV_ELF): $(RECV_OBJS)
$(OBJS): %.o: %.c $(HEADERS_BUILD) $(OBJS): %.o: %.c $(HEADERS_BUILD)
$(CC) $(CFLAGS) -c $< $(CC) $(CFLAGS) -c $<
flash: $(IMAGE_BIN) flash-send: $(SEND_BIN)
$(AVRDUDE) $(AVRDUDE_FLAGS) -U flash:w:$(IMAGE_BIN):r $(AVRDUDE) $(AVRDUDE_FLAGS) -U flash:w:$(SEND_BIN):r
flash-recv: $(RECV_BIN)
$(AVRDUDE) $(AVRDUDE_FLAGS) -U flash:w:$(RECV_BIN):r
clean: clean:
$(RM) -f $(IMAGES_BIN) $(IMAGES_ELF) $(OBJS) $(RM) -f $(IMAGES_BIN) $(IMAGES_ELF) $(OBJS)

View file

@ -1,11 +1,13 @@
#include <avr/io.h> #include <avr/io.h>
#include <avr/interrupt.h> #include <avr/interrupt.h>
#include <avr/sleep.h>
#include <util/delay.h> #include <util/delay.h>
#include <tabby/printer.h> #include <tabby/printer.h>
#include <tabby/avr/uart.h> #include <tabby/avr/uart.h>
#define TIMER0_INTERVAL 1953 #define TIMER0_INTERVAL 1953
#define PACKET_BODY_SIZE 640
static volatile uint8_t bits = 0; static volatile uint8_t bits = 0;
static volatile uint8_t value_in = 0x00; /* Data coming in from Game Boy */ static volatile uint8_t value_in = 0x00; /* Data coming in from Game Boy */
@ -17,6 +19,14 @@ static volatile tabby_printer_packet header = {
.size = 0 .size = 0
}; };
static volatile uint8_t body[PACKET_BODY_SIZE];
static volatile uint16_t sum = 0x0000;
static volatile uint8_t device = 0x00,
status = 0x00,
buffered = 0;
static volatile uint16_t i = 0, static volatile uint16_t i = 0,
b = 0; b = 0;
@ -24,6 +34,10 @@ static volatile uint16_t i = 0,
* Internal clock source interrupt vector * Internal clock source interrupt vector
*/ */
ISR(TIMER0_COMPB_vect) { ISR(TIMER0_COMPB_vect) {
if (!buffered) {
reti();
}
value_in >>= 1; value_in >>= 1;
PORTB |= (1 << PORTB5); PORTB |= (1 << PORTB5);
@ -41,6 +55,29 @@ ISR(TIMER0_COMPB_vect) {
value_out <<= 1; value_out <<= 1;
if (--bits == 0) { if (--bits == 0) {
if (i < sizeof(header)) {
value_out = header.data[i++];
} else if (b < header.size) {
value_out = body[b++];
} else if (b == header.size) {
value_out = sum & 0x00ff;
b++;
} else if (b == header.size + 1) {
value_out = (sum & 0xff00) >> 8;
b++;
} else if (b == header.size + 2) {
device = value_in;
value_out = 0x00;
b++;
} else if (b == header.size + 3) {
status = value_in;
value_out = 0x00;
i = 0;
b = 0;
buffered = 0;
}
bits = 8; bits = 8;
} }
@ -90,7 +127,24 @@ static void clock_setup() {
TCCR1B = (1 << CS10); TCCR1B = (1 << CS10);
} }
static uint16_t checksum() {
uint16_t sum = 0;
size_t i;
for (i=2; i<6; i++) {
sum += header.data[i];
}
for (i=0; i<header.size; i++) {
sum += body[i];
}
return sum;
}
int main() { int main() {
uint16_t sum = 0;
uart_init(); uart_init();
clock_setup(); clock_setup();
@ -98,8 +152,81 @@ int main() {
sei(); sei();
while (1) { while (1) {
uint8_t c = uart_getchar(NULL); uint8_t c;
while (buffered) {
sleep_mode();
}
c = uart_getchar(NULL);
switch (i) {
case 0: {
if (c == 0x88) {
header.data[0] = c;
i++;
b = 0;
}
break;
}
case 1: {
if (c == 0x33) {
header.data[1] = c;
i++;
} else {
i = 0;
}
break;
}
case 2: {
header.type = c;
i++;
break;
}
case 3: {
header.compression = c;
i++;
break;
}
case 4: {
header.size = c;
i++;
break;
}
case 5: {
header.size |= c << 8;
i++;
break;
}
default: {
if (b < header.size) {
b++;
} else if (b == header.size) {
sum = c;
} else if (b == header.size + 1) {
sum |= c << 8;
if (sum == checksum()) {
i = 0;
b = 0;
buffered = 1;
}
}
}
}
} }
return 0; return 0;