Gonna bit bang this without interrupts yo
This commit is contained in:
parent
a069adb1df
commit
35d47901d6
1 changed files with 57 additions and 129 deletions
186
avr/send.c
186
avr/send.c
|
@ -6,34 +6,18 @@
|
|||
#include <tabby/printer.h>
|
||||
#include <tabby/avr/uart.h>
|
||||
|
||||
#define TIMER1_INTERVAL 1953
|
||||
|
||||
enum {
|
||||
TABBY_SEND_READING = 0,
|
||||
TABBY_SEND_BUFFERED = (1 << 0),
|
||||
TABBY_SEND_COMPLETE = (1 << 1)
|
||||
};
|
||||
|
||||
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 = {
|
||||
static tabby_printer_packet header = {
|
||||
.type = 0,
|
||||
.compression = 0,
|
||||
.size = 0
|
||||
};
|
||||
|
||||
static volatile uint8_t body[TABBY_PRINTER_MAX_PACKET_SIZE];
|
||||
static uint8_t body[TABBY_PRINTER_MAX_PACKET_SIZE];
|
||||
|
||||
static volatile uint16_t sum = 0x0000;
|
||||
static uint16_t sum = 0x0000;
|
||||
|
||||
static volatile uint8_t device = 0x00,
|
||||
status = 0x00,
|
||||
flags = TABBY_SEND_READING;
|
||||
|
||||
static volatile uint16_t i = 0,
|
||||
b = 0;
|
||||
static uint8_t device = 0x00,
|
||||
status = 0x00;
|
||||
|
||||
/*
|
||||
* So like, we're abusing pins here. So badly, it's beyond. So here's what's
|
||||
|
@ -45,111 +29,70 @@ static volatile uint16_t i = 0,
|
|||
* The SI and SO pins over a link cable are crossed over, so SI on one end
|
||||
* always connects to SO on the other, and vice-versa.
|
||||
*/
|
||||
static void spi_start() {
|
||||
static void spi_init() {
|
||||
/*
|
||||
* Configure MISO as output...Oddly
|
||||
* Configure MISO as output...Oddly, alongside SCK as an output
|
||||
*/
|
||||
DDRB |= (1 << DDB4);
|
||||
DDRB |= ((1 << DDB5) | (1 << DDB4));
|
||||
|
||||
/*
|
||||
* Configure MOSI as input...Also oddly
|
||||
*/
|
||||
DDRB &= ~(1 << DDB3);
|
||||
|
||||
/*
|
||||
* Configure SCK pin as output
|
||||
*/
|
||||
DDRB |= (1 << DDB5);
|
||||
|
||||
/*
|
||||
* Set output pins clear by default
|
||||
*/
|
||||
PORTB &= ~((1 << PORTB5) | (1 << PORTB4));
|
||||
|
||||
/*
|
||||
* Set timer clock divider to 1/1
|
||||
*/
|
||||
TCCR1B = (1 << CS10);
|
||||
|
||||
/*
|
||||
* Set timer interval
|
||||
*/
|
||||
OCR1A = TIMER1_INTERVAL;
|
||||
|
||||
/*
|
||||
* Reset timer counter to zero
|
||||
*/
|
||||
TCNT1 = 0;
|
||||
|
||||
/*
|
||||
* Enable timer interrupt vector for match on OCR1A
|
||||
*/
|
||||
TIMSK1 = (1 << OCIE1A);
|
||||
PORTB &= ~(1 << PORTB4);
|
||||
}
|
||||
|
||||
static void spi_end() {
|
||||
PORTB &= ~((1 << PORTB5) | (1 << PORTB4));
|
||||
DDRB &= ~((1 << DDB5) | (1 << DDB4) | (1 << DDB3));
|
||||
TIMSK1 &= ~ (1 << OCIE1A);
|
||||
TCNT1 = 0;
|
||||
OCR1A = TIMER1_INTERVAL;
|
||||
TCCR1B = (1 << CS10);
|
||||
}
|
||||
static uint8_t spi_send_byte(uint8_t value) {
|
||||
uint8_t ret = 0,
|
||||
i = 0;
|
||||
|
||||
ISR(TIMER1_COMPA_vect) {
|
||||
value_in >>= 1;
|
||||
|
||||
PORTB |= (1 << PORTB5);
|
||||
|
||||
if (PINB & (1 << PINB3)) {
|
||||
value_in |= 0x80;
|
||||
}
|
||||
|
||||
if (value_out & 0x80) {
|
||||
PORTB |= (1 << PORTB4);
|
||||
} else {
|
||||
PORTB &= ~(1 << PORTB4);
|
||||
}
|
||||
|
||||
value_out <<= 1;
|
||||
|
||||
if (--bits == 0) {
|
||||
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;
|
||||
|
||||
i = 0;
|
||||
b = 0;
|
||||
flags &= ~TABBY_SEND_BUFFERED;
|
||||
flags |= TABBY_SEND_COMPLETE;
|
||||
|
||||
spi_end();
|
||||
for (i=0; i<8; i++) {
|
||||
if (value & 0x80) {
|
||||
PORTB |= (1 << PORTB4);
|
||||
} else {
|
||||
PORTB &= ~(1 << PORTB4);
|
||||
}
|
||||
|
||||
bits = 8;
|
||||
value <<= 1;
|
||||
|
||||
PORTB |= (1 << PORTB5);
|
||||
|
||||
_delay_us(60);
|
||||
|
||||
ret <<= 1;
|
||||
|
||||
PORTB &= ~(1 << PORTB5);
|
||||
|
||||
if (PINB & (1 << PINB3)) {
|
||||
ret |= 1;
|
||||
}
|
||||
|
||||
_delay_us(60);
|
||||
}
|
||||
|
||||
/*
|
||||
* 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);
|
||||
return ret;
|
||||
}
|
||||
|
||||
PORTB &= ~(1 << PORTB5);
|
||||
static void spi_send_packet() {
|
||||
int i;
|
||||
|
||||
for (i=0; i<sizeof(header); i++) {
|
||||
(void)spi_send_byte(header.data[i]);
|
||||
}
|
||||
|
||||
for (i=0; i<header.size; i++) {
|
||||
(void)spi_send_byte(body[i]);
|
||||
}
|
||||
|
||||
(void)spi_send_byte( sum & 0x00ff);
|
||||
(void)spi_send_byte((sum & 0xff00) >> 8);
|
||||
|
||||
status = spi_send_byte(0);
|
||||
status = spi_send_byte(0);
|
||||
}
|
||||
|
||||
static uint16_t checksum() {
|
||||
|
@ -169,32 +112,16 @@ static uint16_t checksum() {
|
|||
|
||||
int main() {
|
||||
uint16_t sum = 0,
|
||||
i = 0,
|
||||
b = 0,
|
||||
c;
|
||||
|
||||
uart_init();
|
||||
spi_init();
|
||||
|
||||
sei();
|
||||
|
||||
while (1) {
|
||||
uart_putchar('O', NULL);
|
||||
|
||||
c = uart_getchar(NULL);
|
||||
|
||||
if (c == 'K') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
while (1) {
|
||||
if (flags & TABBY_SEND_COMPLETE) {
|
||||
uart_putchar(device, NULL);
|
||||
uart_putchar(status, NULL);
|
||||
|
||||
flags = TABBY_SEND_READING;
|
||||
}
|
||||
|
||||
while (flags != TABBY_SEND_READING);
|
||||
|
||||
c = uart_getchar(NULL);
|
||||
|
||||
switch (i) {
|
||||
|
@ -266,7 +193,7 @@ int main() {
|
|||
|
||||
default: {
|
||||
if (b < header.size) {
|
||||
body[b++];
|
||||
body[b++] = c;
|
||||
} else if (b == header.size) {
|
||||
sum = c;
|
||||
b++;
|
||||
|
@ -277,9 +204,10 @@ int main() {
|
|||
b = 0;
|
||||
|
||||
if (sum == checksum()) {
|
||||
flags |= TABBY_SEND_BUFFERED;
|
||||
spi_send_packet();
|
||||
|
||||
spi_start();
|
||||
uart_putchar(device, NULL);
|
||||
uart_putchar(status, NULL);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue