tabby/avr/recv.c

132 lines
2.3 KiB
C
Raw Normal View History

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/printer.h>
#include <tabby/avr/uart.h>
2016-06-05 12:37:05 -05:00
#include <tabby/avr.h>
2016-06-05 16:45:04 -05:00
static volatile tabby_printer_packet header;
static volatile uint16_t i, b;
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) {
2016-06-01 20:33:47 -05:00
uint8_t in = SPDR,
2016-06-05 12:37:05 -05:00
out = 0;
2016-05-25 23:28:06 -05:00
2016-06-01 20:33:47 -05:00
uart_putchar(in, NULL);
switch (i) {
case 0: {
2016-06-05 12:37:05 -05:00
if (in == TABBY_PRINTER_SYNC_1) {
2016-06-06 19:16:05 -05:00
header.preamble[0] = in;
i++;
2016-05-25 23:28:06 -05:00
b = 0;
}
break;
}
2016-05-25 23:28:06 -05:00
case 1: {
2016-06-05 12:37:05 -05:00
if (in == TABBY_PRINTER_SYNC_2) {
2016-06-06 19:16:05 -05:00
header.preamble[1] = in;
i++;
} else {
i = 0;
}
2016-05-25 23:28:06 -05:00
break;
}
2016-05-25 23:28:06 -05:00
case 2: {
2016-06-01 20:33:47 -05:00
header.type = in;
i++;
break;
}
2016-05-25 23:28:06 -05:00
case 3: {
2016-06-01 20:33:47 -05:00
header.compression = in;
i++;
break;
}
case 4: {
2016-06-01 20:33:47 -05:00
header.size = in;
i++;
2016-05-25 23:28:06 -05:00
break;
}
2016-05-25 23:28:06 -05:00
case 5: {
2016-06-01 20:33:47 -05:00
header.size |= in << 8;
i++;
2016-05-25 23:28:06 -05:00
2016-06-06 19:16:05 -05:00
if (header.size > TABBY_PRINTER_PACKET_MAX_SIZE) {
i = 0;
}
break;
}
default: {
2016-06-01 20:33:47 -05:00
if (b <= header.size) {
b++;
} else if (b == header.size + 1) {
b++;
2016-06-05 12:37:05 -05:00
out = TABBY_PRINTER_DEVICE_ID;
} else if (b == header.size + 2) {
b++;
2016-06-06 19:16:05 -05:00
out = TABBY_PRINTER_OK;
} else if (b == header.size + 3) {
i = 0;
}
}
}
2016-06-01 20:33:47 -05:00
SPDR = out;
}
2016-06-05 12:37:05 -05:00
static void spi_init() {
SS_INPUT();
SC_INPUT();
SI_INPUT();
SO_OUTPUT();
/*
* Set SPI slave mode, and shift in/out most significant bit first
*/
SPCR &= ~((1 << MSTR) | (1 << DORD));
/*
2016-06-05 12:37:05 -05:00
* Enable SPI in Mode 3 with interrupts, clock nominally high, output on
* falling edge, input on rising edge
*/
SPCR |= (1 << CPOL) | (1 << CPHA) | (1 << SPIE) | (1 << SPE);
/*
* Initialize the SPI Data Register and serial buffer
*/
SPDR = 0;
}
int main() {
uart_init();
2016-06-05 12:37:05 -05:00
spi_init();
2016-05-25 23:28:06 -05:00
sei();
while (1) {
sleep_mode();
2016-05-25 23:28:06 -05:00
}
return 0;
}