187 lines
3.4 KiB
C
187 lines
3.4 KiB
C
#include <stdio.h>
|
|
|
|
#include <avr/io.h>
|
|
#include <avr/interrupt.h>
|
|
#include <avr/sleep.h>
|
|
|
|
#include <tabby/clock.h>
|
|
#include <tabby/command.h>
|
|
#include <tabby/printer.h>
|
|
|
|
#include <tabby/avr/uart.h>
|
|
|
|
static volatile tabby_printer_packet_header header = {
|
|
.type = 0,
|
|
.compression = 0,
|
|
.size = 0
|
|
};
|
|
|
|
static volatile uint8_t body[648];
|
|
|
|
static volatile uint16_t sum = 0;
|
|
|
|
static volatile uint16_t i = 0,
|
|
b = 0;
|
|
|
|
static inline uint16_t checksum() {
|
|
uint16_t ret = header.data[2]
|
|
+ header.data[3]
|
|
+ header.data[4]
|
|
+ header.data[5];
|
|
|
|
size_t i;
|
|
|
|
for (i=0; i<header.size; i++) {
|
|
ret += ((uint8_t *)body)[i];
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
/*
|
|
* SPI byte receipt interrupt vector
|
|
*/
|
|
ISR(SPI_STC_vect) {
|
|
uint8_t value = SPDR;
|
|
|
|
switch (i) {
|
|
case 0: {
|
|
if (value == 0x88) {
|
|
header.data[0] = value;
|
|
i++;
|
|
|
|
b = 0;
|
|
}
|
|
|
|
SPDR = 0;
|
|
|
|
break;
|
|
}
|
|
|
|
case 1: {
|
|
if (value == 0x33) {
|
|
header.data[1] = value;
|
|
i++;
|
|
} else {
|
|
i = 0;
|
|
}
|
|
|
|
SPDR = 0;
|
|
|
|
break;
|
|
}
|
|
|
|
case 2: {
|
|
header.type = value;
|
|
i++;
|
|
SPDR = 0;
|
|
|
|
break;
|
|
}
|
|
|
|
case 3: {
|
|
header.compression = value;
|
|
i++;
|
|
SPDR = 0;
|
|
|
|
break;
|
|
}
|
|
|
|
case 4: {
|
|
header.size = value;
|
|
i++;
|
|
SPDR = 0;
|
|
|
|
break;
|
|
}
|
|
|
|
case 5: {
|
|
header.size |= value << 8;
|
|
i++;
|
|
SPDR = 0;
|
|
|
|
break;
|
|
}
|
|
|
|
default: {
|
|
if (b < header.size) {
|
|
body[b++] = value;
|
|
} else if (b == header.size) {
|
|
sum >>= 8;
|
|
sum |= value << 8;
|
|
|
|
b++;
|
|
|
|
SPDR = 0;
|
|
} else if (b == header.size + 1) {
|
|
sum >>= 8;
|
|
sum |= value << 8;
|
|
|
|
b++;
|
|
|
|
SPDR = 0x81;
|
|
} else if (b == header.size + 2) {
|
|
b++;
|
|
|
|
SPDR = 0;
|
|
} else if (b == header.size + 3) {
|
|
i = 0;
|
|
|
|
SPDR = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
uart_putchar(value, NULL);
|
|
}
|
|
|
|
static void setup_clock_external() {
|
|
/*
|
|
* Configure MISO as output
|
|
*/
|
|
DDRB |= (1 << DDB4);
|
|
/*
|
|
* Configure SS as input
|
|
*/
|
|
DDRB &= ~(1 << DDB2);
|
|
|
|
/*
|
|
* Set SPI slave mode, and shift in/out most significant bit first
|
|
*/
|
|
SPCR &= ~((1 << MSTR) | (1 << DORD));
|
|
|
|
/*
|
|
* Enable SPI in Mode 3 with interrupts
|
|
*/
|
|
SPCR |= (1 << CPOL) | (1 << CPHA) | (1 << SPIE) | (1 << SPE);
|
|
|
|
/*
|
|
* Initialize the SPI Data Register and serial buffer
|
|
*/
|
|
SPDR = 0;
|
|
}
|
|
|
|
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.
|
|
*/
|
|
setup_clock_external();
|
|
|
|
/*
|
|
* We do actually want to globally enable interrupts here, so here's that
|
|
* one assembly instruction to do so.
|
|
*/
|
|
sei();
|
|
|
|
while (1) {
|
|
sleep_mode();
|
|
}
|
|
|
|
return 0;
|
|
}
|