diff --git a/avr/Makefile b/avr/Makefile index 6751637..e368e72 100644 --- a/avr/Makefile +++ b/avr/Makefile @@ -22,18 +22,18 @@ HEADERS_LOCAL = HEADERS_BUILD = $(HEADERS_LOCAL) \ $(addprefix $(INCLUDE_PATH)/$(HEADER_SUBDIR)/,$(HEADERS)) -HEADERS = avr.h avr/uart.h link.h -OBJS = send.o recv.o uart.o +HEADERS = avr.h avr/uart.h avr/link.h avr/printer.h link.h printer.h +OBJS = send.o recv.o printer.o link.o uart.o RECV_NAME = recv RECV_BIN = $(RECV_NAME).bin RECV_ELF = $(RECV_NAME).elf -RECV_OBJS = recv.o uart.o +RECV_OBJS = recv.o link.o uart.o SEND_NAME = send SEND_BIN = $(SEND_NAME).bin SEND_ELF = $(SEND_NAME).elf -SEND_OBJS = send.o uart.o +SEND_OBJS = send.o printer.o link.o uart.o IMAGES_BIN = $(RECV_BIN) $(SEND_BIN) IMAGES_ELF = $(RECV_ELF) $(SEND_ELF) diff --git a/avr/link.c b/avr/link.c new file mode 100644 index 0000000..6d61d9a --- /dev/null +++ b/avr/link.c @@ -0,0 +1,71 @@ +#include +#include +#include +#include + +#include +#include +#include + +void tabby_avr_link_init_master() { + SC_OUTPUT(); + SO_OUTPUT(); + + SI_INPUT(); + SI_PULLUP(); + + SC_HIGH(); + SO_LOW(); +} + +void tabby_avr_link_init_slave() { + 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)); + + /* + * 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; +} + +uint8_t tabby_avr_link_send_byte(uint8_t value) { + uint8_t i, ret = 0; + + for (i=0; i<8; i++) { + SC_LOW(); + + if (value & 0x80) { + SO_HIGH(); + } else { + SO_LOW(); + } + + _delay_us(SPI_PULSE_USEC); + + value <<= 1; + ret <<= 1; + + if (SI_IS_HIGH()) { + ret |= 1; + } + + SC_HIGH(); + + _delay_us(SPI_PULSE_USEC); + } + + return ret; +} diff --git a/avr/printer.c b/avr/printer.c new file mode 100644 index 0000000..3fed9c1 --- /dev/null +++ b/avr/printer.c @@ -0,0 +1,98 @@ +#include + +#include +#include + +static uint16_t checksum(tabby_printer_packet *header, uint8_t *body) { + uint16_t sum = 0; + + int i; + + for (i=2; idata[i]; + } + + for (i=0; isize; i++) { + sum += body[i]; + } + + return sum; +} + +void tabby_avr_printer_send_packet(uint8_t type, + uint8_t *body, + uint16_t size, + tabby_printer_response *response) { + tabby_printer_packet header = { + .preamble = { TABBY_PRINTER_SYNC_1, TABBY_PRINTER_SYNC_2 }, + .type = type, + .compression = TABBY_PRINTER_COMPRESSION_NONE, + .size = size + }; + + uint16_t sum = checksum(&header, body); + + int i; + + for (i=0; i> 8); + + response->device = tabby_avr_link_send_byte(0); + response->status = tabby_avr_link_send_byte(0); +} + +void tabby_avr_printer_init(tabby_printer_response *response) { + tabby_avr_printer_send_packet(TABBY_PRINTER_PACKET_INIT, + NULL, + 0, + response); +} + +void tabby_avr_printer_send_inquiry(tabby_printer_response *response) { + tabby_avr_printer_send_packet(TABBY_PRINTER_PACKET_INQUIRY, + NULL, + 0, + response); +} + +void tabby_avr_printer_job_start(tabby_printer_job *job, + tabby_printer_response *response) { + tabby_avr_printer_send_packet(TABBY_PRINTER_PACKET_JOB, + job->data, + sizeof(*job), + response); +} + +void tabby_avr_printer_send_data(uint8_t *data, + uint16_t size, + tabby_printer_response *response) { + tabby_avr_printer_send_packet(TABBY_PRINTER_PACKET_DATA, + data, + size, + response); +} + +void tabby_avr_printer_send_sheet(uint8_t *sheet, + uint16_t size, + tabby_printer_response *response) { + int i; + + tabby_avr_printer_init(response); + + for (i=0; i #include +#include #include static volatile tabby_printer_packet header; @@ -94,32 +95,10 @@ ISR(SPI_STC_vect) { SPDR = out; } -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)); - - /* - * 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(); - spi_init(); + + tabby_avr_link_init_slave(); sei(); diff --git a/avr/send.c b/avr/send.c index 50793c6..59bcb16 100644 --- a/avr/send.c +++ b/avr/send.c @@ -5,130 +5,10 @@ #include #include +#include +#include #include -static void spi_init() { - SC_OUTPUT(); - SO_OUTPUT(); - - SI_INPUT(); - SI_PULLUP(); - - SC_HIGH(); - SO_LOW(); -} - -static uint8_t spi_send_byte(uint8_t value) { - uint8_t i, ret = 0; - - for (i=0; i<8; i++) { - SC_LOW(); - - if (value & 0x80) { - SO_HIGH(); - } else { - SO_LOW(); - } - - _delay_us(SPI_PULSE_USEC); - - value <<= 1; - ret <<= 1; - - if (SI_IS_HIGH()) { - ret |= 1; - } - - SC_HIGH(); - - _delay_us(SPI_PULSE_USEC); - } - - return ret; -} - -static uint16_t checksum(tabby_printer_packet *header, uint8_t *body) { - uint16_t sum = 0; - - int i; - - for (i=2; idata[i]; - } - - for (i=0; isize; i++) { - sum += body[i]; - } - - return sum; -} - -static void spi_send_packet(uint8_t type, - uint8_t *body, - uint16_t size, - tabby_printer_response *response) { - tabby_printer_packet header = { - .preamble = { TABBY_PRINTER_SYNC_1, TABBY_PRINTER_SYNC_2 }, - .type = type, - .compression = TABBY_PRINTER_COMPRESSION_NONE, - .size = size - }; - - uint16_t sum = checksum(&header, body); - - int i; - - for (i=0; i> 8); - - response->device = spi_send_byte(0); - response->status = spi_send_byte(0); -} - -static void spi_send_init(tabby_printer_response *response) { - spi_send_packet(TABBY_PRINTER_PACKET_INIT, NULL, 0, response); -} - -static void spi_send_inquiry(tabby_printer_response *response) { - spi_send_packet(TABBY_PRINTER_PACKET_INQUIRY, NULL, 0, response); -} - -static void spi_send_job(tabby_printer_job *job, - tabby_printer_response *response) { - spi_send_packet(TABBY_PRINTER_PACKET_JOB, - job->data, - sizeof(*job), - response); -} - -static void spi_send_data(uint8_t *data, - uint16_t size, - tabby_printer_response *response) { - spi_send_packet(TABBY_PRINTER_PACKET_DATA, data, size, response); -} - -static void spi_send_sheet(uint8_t *sheet, - uint16_t size, - tabby_printer_response *response) { - int i; - - spi_send_init(response); - - for (i=0; i #include #include -#include #include #include @@ -20,34 +19,20 @@ static void usage(int argc, char **argv, char *message, ...) { va_end(args); } - fprintf(stderr, "usage: %s device sheet.tile\n", argv[0]); + fprintf(stderr, "usage: %s device\n", argv[0]); exit(1); } int main(int argc, char **argv) { - int fd, tile_fd; + int fd, status; - tabby_printer_response response; - void *tile; + tabby_printer_packet header; - if (argc != 3) { - usage(argc, argv, "Must specify device and tile"); - } + uint8_t body[TABBY_PRINTER_PACKET_MAX_SIZE]; - if ((tile = malloc(TABBY_PRINTER_SHEET_SIZE)) == NULL) { - goto error_malloc_tile; - } - - if ((tile_fd = open(argv[2], O_RDONLY)) < 0) { - fprintf(stderr, "%s: %s: %s: %s\n", - argv[0], "open()", argv[2], strerror(errno)); - - goto error_open_tile; - } - - if (read(tile_fd, tile, TABBY_PRINTER_SHEET_SIZE) < 0) { - goto error_read_tile; + if (argc != 2) { + usage(argc, argv, "No device specified"); } if ((fd = tabby_link_open(argv[1])) < 0) { @@ -57,32 +42,19 @@ int main(int argc, char **argv) { goto error_link_open; } - printf("Waiting for AVR to reboot after CTS\n"); + while (1) { + status = tabby_printer_packet_recv(fd, &header, &body); - sleep(1); + if (status < 0) { + fprintf(stderr, "%s: %s: %s\n", + argv[0], "tabby_printer_packet_recv()", strerror(errno)); - tabby_printer_link_init(fd); + continue; + } - printf("Link initialized\n"); - - do { - tabby_printer_init(fd, &response); - - usleep(100000); - printf("Got response %02x%02x\n", response.device, response.status); - } while (response.device != TABBY_PRINTER_DEVICE_ID); - - tabby_printer_send_sheet(fd, tile, &response); - - printf("Sent sheet, got response %02x%02x\n", - response.device, response.status); - - tabby_printer_job_start(fd, 1, 0x13, 0x00, 0x40, &response); - - while (response.status & TABBY_PRINTER_UNTRAN) { - tabby_printer_send_inquiry(fd, &response); - - usleep(100000); + if (header.type == 0x04) { + write(1, &body, header.size); + } } tabby_link_close(fd); @@ -90,12 +62,5 @@ int main(int argc, char **argv) { return 0; error_link_open: -error_read_tile: - close(tile_fd); - -error_open_tile: - free(tile); - -error_malloc_tile: return 1; } diff --git a/include/tabby/avr/link.h b/include/tabby/avr/link.h new file mode 100644 index 0000000..7817d65 --- /dev/null +++ b/include/tabby/avr/link.h @@ -0,0 +1,12 @@ +#ifndef _TABBY_AVR_LINK_H +#define _TABBY_AVR_LINK_H + +#include + +void tabby_avr_link_init_master(); + +void tabby_avr_link_init_slave(); + +uint8_t tabby_avr_link_send_byte(uint8_t value); + +#endif /* _TABBY_AVR_LINK_H */ diff --git a/include/tabby/avr/printer.h b/include/tabby/avr/printer.h new file mode 100644 index 0000000..d12074e --- /dev/null +++ b/include/tabby/avr/printer.h @@ -0,0 +1,28 @@ +#ifndef _TABBY_AVR_PRINTER_H +#define _TABBY_AVR_PRINTER_H + +#include + +#include + +void tabby_avr_printer_send_packet(uint8_t type, + uint8_t *body, + uint16_t size, + tabby_printer_response *response); + +void tabby_avr_printer_init(tabby_printer_response *response); + +void tabby_avr_printer_send_inquiry(tabby_printer_response *response); + +void tabby_avr_printer_job_start(tabby_printer_job *job, + tabby_printer_response *response); + +void tabby_avr_printer_send_data(uint8_t *data, + uint16_t size, + tabby_printer_response *response); + +void tabby_avr_printer_send_sheet(uint8_t *sheet, + uint16_t size, + tabby_printer_response *response); + +#endif /* _TABBY_AVR_PRINTER_H */