tabby/avr/send.c

107 lines
1.7 KiB
C
Raw Normal View History

2016-06-01 22:30:12 -05:00
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <tabby/printer.h>
#include <tabby/avr/uart.h>
#define TIMER0_INTERVAL 1953
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
};
static volatile uint16_t i = 0,
b = 0;
/*
* Internal clock source interrupt vector
*/
ISR(TIMER0_COMPB_vect) {
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) {
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);
}
int main() {
uart_init();
clock_setup();
sei();
while (1) {
uint8_t c = uart_getchar(NULL);
}
return 0;
}