tabby/avr/main.c

188 lines
3.4 KiB
C
Raw Normal View History

2016-05-25 23:28:06 -05:00
#include <stdio.h>
2016-05-27 12:23:00 -05:00
#include <avr/io.h>
#include <avr/interrupt.h>
2016-05-25 23:28:06 -05:00
#include <avr/sleep.h>
#include <tabby/clock.h>
2016-05-27 12:23:00 -05:00
#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];
2016-05-29 15:26:06 -05:00
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;
}
2016-05-25 23:28:06 -05:00
/*
* SPI byte receipt interrupt vector
2016-05-25 23:28:06 -05:00
*/
ISR(SPI_STC_vect) {
uint8_t value = SPDR;
2016-05-25 23:28:06 -05:00
uart_putchar(value, NULL);
switch (i) {
case 0: {
if (value == 0x88) {
header.data[0] = value;
i++;
2016-05-25 23:28:06 -05:00
b = 0;
}
SPDR = 0;
break;
}
2016-05-25 23:28:06 -05:00
case 1: {
if (value == 0x33) {
header.data[1] = value;
i++;
} else {
i = 0;
}
2016-05-25 23:28:06 -05:00
SPDR = 0;
2016-05-25 23:28:06 -05:00
break;
}
2016-05-25 23:28:06 -05:00
case 2: {
header.type = value;
i++;
SPDR = 0;
break;
}
2016-05-25 23:28:06 -05:00
case 3: {
header.compression = value;
i++;
SPDR = 0;
break;
}
case 4: {
header.size = value;
i++;
SPDR = 0;
2016-05-25 23:28:06 -05:00
break;
}
2016-05-25 23:28:06 -05:00
case 5: {
header.size |= value << 8;
i++;
SPDR = 0;
2016-05-25 23:28:06 -05:00
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;
}
}
}
}
static void setup_clock_external() {
/*
* Configure MISO as output
*/
2016-05-29 01:12:51 -05:00
DDRB |= (1 << DDB4);
/*
2016-05-29 01:12:51 -05:00
* Configure SS as input
*/
2016-05-29 01:12:51 -05:00
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();
2016-05-25 23:28:06 -05:00
/*
* 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();
2016-05-25 23:28:06 -05:00
/*
* We do actually want to globally enable interrupts here, so here's that
* one assembly instruction to do so.
*/
sei();
while (1) {
sleep_mode();
2016-05-25 23:28:06 -05:00
}
return 0;
}