2016-06-01 22:30:12 -05:00
|
|
|
#include <avr/io.h>
|
|
|
|
#include <avr/interrupt.h>
|
2016-06-01 23:40:40 -05:00
|
|
|
#include <avr/sleep.h>
|
2016-06-01 22:30:12 -05:00
|
|
|
#include <util/delay.h>
|
|
|
|
|
|
|
|
#include <tabby/printer.h>
|
|
|
|
#include <tabby/avr/uart.h>
|
|
|
|
|
2016-06-01 23:40:40 -05:00
|
|
|
#define TIMER0_INTERVAL 1953
|
|
|
|
#define PACKET_BODY_SIZE 640
|
2016-06-01 22:30:12 -05:00
|
|
|
|
2016-06-01 23:49:27 -05:00
|
|
|
enum {
|
|
|
|
TABBY_SEND_READING = 0,
|
|
|
|
TABBY_SEND_BUFFERED = (1 << 0),
|
|
|
|
TABBY_SEND_COMPLETE = (1 << 1)
|
|
|
|
};
|
|
|
|
|
2016-06-01 22:30:12 -05:00
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2016-06-01 23:40:40 -05:00
|
|
|
static volatile uint8_t body[PACKET_BODY_SIZE];
|
|
|
|
|
|
|
|
static volatile uint16_t sum = 0x0000;
|
|
|
|
|
2016-06-01 23:49:27 -05:00
|
|
|
static volatile uint8_t device = 0x00,
|
|
|
|
status = 0x00,
|
|
|
|
flags = TABBY_SEND_READING;
|
2016-06-01 23:40:40 -05:00
|
|
|
|
2016-06-01 22:30:12 -05:00
|
|
|
static volatile uint16_t i = 0,
|
|
|
|
b = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Internal clock source interrupt vector
|
|
|
|
*/
|
|
|
|
ISR(TIMER0_COMPB_vect) {
|
2016-06-01 23:49:27 -05:00
|
|
|
if (!(flags & TABBY_SEND_BUFFERED)) {
|
2016-06-01 23:40:40 -05:00
|
|
|
reti();
|
|
|
|
}
|
|
|
|
|
2016-06-01 22:30:12 -05:00
|
|
|
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) {
|
2016-06-01 23:40:40 -05:00
|
|
|
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;
|
|
|
|
|
2016-06-01 23:49:27 -05:00
|
|
|
i = 0;
|
|
|
|
b = 0;
|
|
|
|
flags &= ~TABBY_SEND_BUFFERED;
|
|
|
|
flags |= TABBY_SEND_COMPLETE;
|
2016-06-01 23:40:40 -05:00
|
|
|
}
|
|
|
|
|
2016-06-01 22:30:12 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-06-01 23:40:40 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-06-01 22:30:12 -05:00
|
|
|
int main() {
|
2016-06-01 23:40:40 -05:00
|
|
|
uint16_t sum = 0;
|
|
|
|
|
2016-06-01 22:30:12 -05:00
|
|
|
uart_init();
|
|
|
|
|
|
|
|
clock_setup();
|
|
|
|
|
|
|
|
sei();
|
|
|
|
|
|
|
|
while (1) {
|
2016-06-01 23:40:40 -05:00
|
|
|
uint8_t c;
|
|
|
|
|
2016-06-01 23:49:27 -05:00
|
|
|
while (flags != TABBY_SEND_READING) {
|
2016-06-01 23:40:40 -05:00
|
|
|
sleep_mode();
|
|
|
|
}
|
|
|
|
|
2016-06-01 23:49:27 -05:00
|
|
|
if (flags & TABBY_SEND_COMPLETE) {
|
|
|
|
uart_putchar(device, NULL);
|
|
|
|
uart_putchar(status, NULL);
|
|
|
|
|
|
|
|
flags = TABBY_SEND_READING;
|
|
|
|
}
|
|
|
|
|
2016-06-01 23:40:40 -05:00
|
|
|
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()) {
|
2016-06-01 23:49:27 -05:00
|
|
|
i = 0;
|
|
|
|
b = 0;
|
|
|
|
flags |= TABBY_SEND_BUFFERED;
|
2016-06-01 23:40:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-01 22:30:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|