tabby/avr/main.c

279 lines
5.6 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/avr/uart.h>
#include <tabby/avr/buffer.h>
2016-05-25 23:28:06 -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 */
2016-05-25 23:28:06 -05:00
static const uint16_t timer_counter_intervals[4] = {
1953, 977, 61, 31
};
static volatile tabby_avr_buffer buffer = {
2016-05-29 01:12:51 -05:00
.len = 0,
.cur = 0,
.read = 0
};
static tabby_clock_source source = TABBY_CLOCK_SOURCE_INTERNAL;
static tabby_clock_speed speed = TABBY_CLOCK_SPEED_8192HZ;
2016-05-25 23:28:06 -05:00
/*
* Internal clock source interrupt vector
*/
2016-05-27 12:23:00 -05:00
ISR(TIMER0_COMPB_vect) {
2016-05-25 23:28:06 -05:00
value_in >>= 1;
if (PORTB & (1 << PORTB2)) {
value_in |= 0x80;
}
if (value_out & 0x80) {
PORTB |= (1 << PORTB3);
} else {
PORTB &= ~(1 << PORTB3);
}
value_out <<= 1;
if (--bits == 0) {
uart_putchar(value_in, NULL);
2016-05-25 23:28:06 -05:00
if (buffer.cur < buffer.len) {
value_out = buffer.data[buffer.cur++];
} else {
2016-05-29 01:12:51 -05:00
buffer.len = 0;
buffer.cur = 0;
buffer.read = 0;
}
2016-05-25 23:28:06 -05:00
bits = 8;
}
/*
* Strobe the SCK pin
*/
PORTB |= (1 << PORTB1);
PORTB &= ~(1 << PORTB1);
2016-05-25 23:28:06 -05:00
}
/*
* SPI byte receipt interrupt vector
*/
ISR(SPI_STC_vect) {
2016-05-27 12:23:00 -05:00
uart_putchar(SPDR, NULL);
if (buffer.cur < buffer.len) {
SPDR = buffer.data[buffer.cur++];
} else {
2016-05-29 01:12:51 -05:00
SPDR = 0xff;
buffer.len = 0;
buffer.cur = 0;
buffer.read = 0;
}
}
static void setup_clock_internal(tabby_clock_speed speed) {
2016-05-25 23:28:06 -05:00
/*
* Configure MISO as output
*/
2016-05-29 01:12:51 -05:00
DDRB |= (1 << DDB4);
2016-05-25 23:28:06 -05:00
/*
* Configure MOSI as input
*/
2016-05-29 01:12:51 -05:00
DDRB &= ~(1 << DDB3);
2016-05-25 23:28:06 -05:00
/*
* Configure SCK pin as output
*/
2016-05-29 01:12:51 -05:00
DDRB |= (1 << DDB5);
2016-05-25 23:28:06 -05:00
/*
* Enable timer interrupt vector
*/
2016-05-27 12:23:00 -05:00
TIMSK0 = (1 << TOIE1);
/*
* Reset timer counter to zero
*/
TCNT1 = 0;
/*
* Set timer interval
*/
OCR1A = timer_counter_intervals[speed];
/*
* Set timer clock divider to 1/1
*/
TCCR1B = (1 << CS10);
}
static void setup_clock_external() {
/*
* Disable internal timer interrupts
*/
TCCR1B = 0;
OCR1A = 0;
2016-05-27 12:23:00 -05:00
TIMSK0 = 0;
/*
* 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;
2016-05-29 01:12:51 -05:00
buffer.len = 0;
buffer.cur = 0;
buffer.read = 0;
}
2016-05-27 12:23:00 -05:00
/*static void snooze() {
2016-05-25 23:28:06 -05:00
set_sleep_mode(mode);
sleep_enable();
sleep_mode();
sleep_disable();
2016-05-27 12:23:00 -05:00
}*/
2016-05-25 23:28:06 -05:00
int main() {
tabby_command state = TABBY_COMMAND_NONE;
int received = 0;
/*
* 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) {
uint8_t c = uart_getchar(NULL);
switch (state) {
2016-05-28 19:22:07 -05:00
case TABBY_COMMAND_NONE: {
state = c;
2016-05-28 19:22:07 -05:00
continue;
}
case TABBY_COMMAND_SEND: {
received++;
2016-05-28 19:22:07 -05:00
if (received == 1 || received == 2) {
buffer.len <<= 8;
buffer.len |= c;
} else {
buffer.data[buffer.read++] = c;
if (buffer.read == buffer.len) {
goto reset;
}
}
break;
2016-05-28 19:22:07 -05:00
}
2016-05-29 01:12:51 -05:00
case TABBY_COMMAND_CLOCK_SOURCE: {
switch (c) {
2016-05-28 19:22:07 -05:00
case TABBY_CLOCK_SOURCE_INTERNAL: {
source = c;
setup_clock_internal(speed);
goto reset;
2016-05-28 19:22:07 -05:00
}
2016-05-28 19:22:07 -05:00
case TABBY_CLOCK_SOURCE_EXTERNAL: {
source = c;
setup_clock_external();
goto reset;
2016-05-28 19:22:07 -05:00
}
2016-05-28 19:22:07 -05:00
default: {
goto reset;
2016-05-28 19:22:07 -05:00
}
}
2016-05-29 01:12:51 -05:00
}
2016-05-28 19:22:07 -05:00
case TABBY_COMMAND_CLOCK_SPEED: {
switch (c) {
case TABBY_CLOCK_SPEED_8192HZ:
case TABBY_CLOCK_SPEED_16384HZ:
case TABBY_CLOCK_SPEED_262144HZ:
2016-05-28 19:22:07 -05:00
case TABBY_CLOCK_SPEED_524288HZ: {
speed = c;
if (source == TABBY_CLOCK_SOURCE_INTERNAL) {
setup_clock_internal(speed);
}
goto reset;
2016-05-28 19:22:07 -05:00
}
default:
goto reset;
}
2016-05-28 19:22:07 -05:00
}
2016-05-28 19:22:07 -05:00
default: {
2016-05-29 01:12:51 -05:00
goto reset;
2016-05-28 19:22:07 -05:00
}
}
continue;
reset:
state = TABBY_COMMAND_NONE;
received = 0;
buffer.len = 0;
buffer.cur = 0;
buffer.read = 0;
2016-05-25 23:28:06 -05:00
}
return 0;
}