Sadly I keep stepping on the UART's dick

This commit is contained in:
XANTRONIX Development 2016-05-31 18:15:37 -05:00
parent ffa6882870
commit 12b5aeceeb
7 changed files with 114 additions and 191 deletions

View file

@ -12,7 +12,7 @@ OBJCOPY = $(CROSS)objcopy
OBJCOPY_FLAGS = -S
AVRDUDE = avrdude
AVRDUDE_DEVICE = /dev/cu.usbmodem1411
AVRDUDE_DEVICE = /dev/ttyACM0
AVRDUDE_FLAGS = -c arduino -p atmega328p -b 115200 -D -P $(AVRDUDE_DEVICE)
HEADERS_LOCAL =

View file

@ -6,123 +6,140 @@
#include <tabby/clock.h>
#include <tabby/command.h>
#include <tabby/printer.h>
#include <tabby/avr/uart.h>
#include <tabby/avr/buffer.h>
static const uint16_t timer_counter_intervals[4] = {
1953, 977, 61, 31
static volatile tabby_printer_packet_header header = {
.type = 0,
.compression = 0,
.size = 0
};
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 uint8_t body[648];
static volatile tabby_avr_buffer buffer = {
.len = 0,
.cur = 0,
.read = 0
};
static volatile uint16_t sum = 0;
static tabby_clock_source source = TABBY_CLOCK_SOURCE_INTERNAL;
static tabby_clock_speed speed = TABBY_CLOCK_SPEED_8192HZ;
static volatile uint16_t i = 0,
b = 0;
/*
* Internal clock source interrupt vector
*/
ISR(TIMER0_COMPB_vect) {
value_in >>= 1;
static inline uint16_t checksum() {
uint16_t ret = header.data[2]
+ header.data[3]
+ header.data[4]
+ header.data[5];
if (PORTB & (1 << PORTB2)) {
value_in |= 0x80;
size_t i;
for (i=0; i<header.size; i++) {
ret += ((uint8_t *)body)[i];
}
if (value_out & 0x80) {
PORTB |= (1 << PORTB3);
} else {
PORTB &= ~(1 << PORTB3);
}
value_out <<= 1;
if (--bits == 0) {
uart_putchar(value_in, NULL);
if (buffer.cur < buffer.read) {
value_out = buffer.data[buffer.cur++];
}
bits = 8;
}
/*
* Strobe the SCK pin
*/
PORTB |= (1 << PORTB1);
PORTB &= ~(1 << PORTB1);
return ret;
}
/*
* SPI byte receipt interrupt vector
*/
ISR(SPI_STC_vect) {
uart_putchar(SPDR, NULL);
uint8_t value = SPDR;
if (buffer.cur < buffer.read) {
SPDR = buffer.data[buffer.cur++];
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;
}
}
}
static void setup_clock_internal(tabby_clock_speed speed) {
/*
* 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 = timer_counter_intervals[speed];
/*
* Set timer clock divider to 1/1
*/
TCCR1B = (1 << CS10);
uart_putchar(value, NULL);
}
static void setup_clock_external() {
/*
* Disable internal timer interrupts
*/
TCCR1B = 0;
OCR1A = 0;
TIMSK0 = 0;
/*
* Configure MISO as output
*/
DDRB |= (1 << DDB4);
/*
* Configure SS as input
*/
@ -145,10 +162,6 @@ static void setup_clock_external() {
}
int main() {
tabby_command state = TABBY_COMMAND_NONE;
int received = 0;
/*
* Best turn on the serial UART!
*/
@ -167,93 +180,7 @@ int main() {
sei();
while (1) {
uint8_t c = uart_getchar(NULL);
switch (state) {
case TABBY_COMMAND_NONE: {
state = c;
continue;
}
case TABBY_COMMAND_SEND: {
received++;
if (received == 1 || received == 2) {
buffer.len <<= 8;
buffer.len |= c;
} else {
buffer.data[buffer.read++] = c;
if (buffer.read == buffer.len) {
state = TABBY_COMMAND_NONE;
received = 0;
continue;
}
}
break;
}
case TABBY_COMMAND_CLOCK_SOURCE: {
switch (c) {
case TABBY_CLOCK_SOURCE_INTERNAL: {
source = c;
setup_clock_internal(speed);
goto reset;
}
case TABBY_CLOCK_SOURCE_EXTERNAL: {
source = c;
setup_clock_external();
goto reset;
}
default: {
goto reset;
}
}
}
case TABBY_COMMAND_CLOCK_SPEED: {
switch (c) {
case TABBY_CLOCK_SPEED_8192HZ:
case TABBY_CLOCK_SPEED_16384HZ:
case TABBY_CLOCK_SPEED_262144HZ:
case TABBY_CLOCK_SPEED_524288HZ: {
speed = c;
if (source == TABBY_CLOCK_SOURCE_INTERNAL) {
setup_clock_internal(speed);
}
goto reset;
}
default:
goto reset;
}
}
default: {
goto reset;
}
}
continue;
reset:
state = TABBY_COMMAND_NONE;
received = 0;
buffer.len = 0;
buffer.cur = 0;
buffer.read = 0;
sleep_mode();
}
return 0;

View file

@ -1,8 +1,6 @@
#ifndef _TABBY_AVR_BUFFER_H
#define _TABBY_AVR_BUFFER_H
#include <sys/types.h>
#define TABBY_AVR_BUFFER_SIZE 1024
typedef struct _tabby_avr_buffer {

View file

@ -1,7 +1,7 @@
#ifndef _TABBY_AVR_UART_H
#define _TABBY_AVR_UART_H
#define TABBY_AVR_UART_BAUD 57600
#define TABBY_AVR_UART_BAUD 115200
void uart_init();

View file

@ -11,7 +11,7 @@
#include <tabby/clock.h>
#define TABBY_LINK_BAUD B57600
#define TABBY_LINK_BAUD B115200
int tabby_link_open(const char *dev);

View file

@ -1,3 +1,5 @@
#include <stdio.h>
#include <tabby/link.h>
#include <tabby/command.h>
@ -9,10 +11,6 @@ int tabby_link_open(const char *dev) {
goto error_open;
}
if (fcntl(fd, F_SETFL) < 0) {
goto error_io;
}
attr.c_cflag = CS8 | CREAD;
attr.c_ispeed = TABBY_LINK_BAUD;
attr.c_ospeed = TABBY_LINK_BAUD;

View file

@ -30,7 +30,7 @@ int tabby_printer_packet_recv(int fd, tabby_printer_packet_header *header,
f = 0;
uint8_t value,
last;
last = 0;
int started = 0;