Who loves refactoring? Xan loves refactoring
This commit is contained in:
parent
33bf45149e
commit
319b17c8b2
8 changed files with 241 additions and 205 deletions
|
@ -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)
|
||||
|
|
71
avr/link.c
Normal file
71
avr/link.c
Normal file
|
@ -0,0 +1,71 @@
|
|||
#include <avr/io.h>
|
||||
#include <avr/interrupt.h>
|
||||
#include <avr/sleep.h>
|
||||
#include <util/delay.h>
|
||||
|
||||
#include <tabby/printer.h>
|
||||
#include <tabby/avr/uart.h>
|
||||
#include <tabby/avr.h>
|
||||
|
||||
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;
|
||||
}
|
98
avr/printer.c
Normal file
98
avr/printer.c
Normal file
|
@ -0,0 +1,98 @@
|
|||
#include <stdlib.h>
|
||||
|
||||
#include <tabby/avr/link.h>
|
||||
#include <tabby/avr/printer.h>
|
||||
|
||||
static uint16_t checksum(tabby_printer_packet *header, uint8_t *body) {
|
||||
uint16_t sum = 0;
|
||||
|
||||
int i;
|
||||
|
||||
for (i=2; i<sizeof(*header); i++) {
|
||||
sum += header->data[i];
|
||||
}
|
||||
|
||||
for (i=0; i<header->size; 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<sizeof(header); i++) {
|
||||
(void)tabby_avr_link_send_byte(header.data[i]);
|
||||
}
|
||||
|
||||
for (i=0; i<size; i++) {
|
||||
(void)tabby_avr_link_send_byte(body[i]);
|
||||
}
|
||||
|
||||
(void)tabby_avr_link_send_byte( sum & 0x00ff);
|
||||
(void)tabby_avr_link_send_byte((sum & 0xff00) >> 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<size; i+=TABBY_PRINTER_BAND_SIZE) {
|
||||
tabby_avr_printer_send_data(sheet + i,
|
||||
TABBY_PRINTER_BAND_SIZE,
|
||||
response);
|
||||
}
|
||||
|
||||
tabby_avr_printer_send_data(NULL, 0, response);
|
||||
}
|
||||
|
27
avr/recv.c
27
avr/recv.c
|
@ -4,6 +4,7 @@
|
|||
|
||||
#include <tabby/printer.h>
|
||||
#include <tabby/avr/uart.h>
|
||||
#include <tabby/avr/link.h>
|
||||
#include <tabby/avr.h>
|
||||
|
||||
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();
|
||||
|
||||
|
|
135
avr/send.c
135
avr/send.c
|
@ -5,130 +5,10 @@
|
|||
|
||||
#include <tabby/printer.h>
|
||||
#include <tabby/avr/uart.h>
|
||||
#include <tabby/avr/link.h>
|
||||
#include <tabby/avr/printer.h>
|
||||
#include <tabby/avr.h>
|
||||
|
||||
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; i<sizeof(*header); i++) {
|
||||
sum += header->data[i];
|
||||
}
|
||||
|
||||
for (i=0; i<header->size; 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<sizeof(header); i++) {
|
||||
(void)spi_send_byte(header.data[i]);
|
||||
}
|
||||
|
||||
for (i=0; i<size; i++) {
|
||||
(void)spi_send_byte(body[i]);
|
||||
}
|
||||
|
||||
(void)spi_send_byte( sum & 0x00ff);
|
||||
(void)spi_send_byte((sum & 0xff00) >> 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<size; i+=TABBY_PRINTER_BAND_SIZE) {
|
||||
spi_send_data(sheet + i, TABBY_PRINTER_BAND_SIZE, response);
|
||||
}
|
||||
|
||||
spi_send_data(NULL, 0, response);
|
||||
}
|
||||
|
||||
int main() {
|
||||
uint16_t i = 0,
|
||||
b = 0,
|
||||
|
@ -150,7 +30,8 @@ int main() {
|
|||
uint8_t sheet[TABBY_PRINTER_SHEET_SIZE];
|
||||
|
||||
uart_init();
|
||||
spi_init();
|
||||
|
||||
tabby_avr_link_init_master();
|
||||
|
||||
sei();
|
||||
|
||||
|
@ -253,13 +134,15 @@ int main() {
|
|||
b = 0;
|
||||
|
||||
if (header.type == TABBY_PRINTER_PACKET_JOB) {
|
||||
spi_send_sheet(sheet, sheet_offset, &response);
|
||||
tabby_avr_printer_send_sheet(sheet,
|
||||
sheet_offset,
|
||||
&response);
|
||||
|
||||
spi_send_job(&job, &response);
|
||||
tabby_avr_printer_job_start(&job, &response);
|
||||
|
||||
sheet_offset = 0;
|
||||
} else if (header.type == TABBY_PRINTER_PACKET_INQUIRY) {
|
||||
spi_send_inquiry(&response);
|
||||
tabby_avr_printer_send_inquiry(&response);
|
||||
}
|
||||
|
||||
uart_putchar(response.device, NULL);
|
||||
|
|
67
bin/main.c
67
bin/main.c
|
@ -4,7 +4,6 @@
|
|||
#include <stdint.h>
|
||||
#include <stdarg.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <tabby/link.h>
|
||||
|
@ -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;
|
||||
}
|
||||
|
|
12
include/tabby/avr/link.h
Normal file
12
include/tabby/avr/link.h
Normal file
|
@ -0,0 +1,12 @@
|
|||
#ifndef _TABBY_AVR_LINK_H
|
||||
#define _TABBY_AVR_LINK_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
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 */
|
28
include/tabby/avr/printer.h
Normal file
28
include/tabby/avr/printer.h
Normal file
|
@ -0,0 +1,28 @@
|
|||
#ifndef _TABBY_AVR_PRINTER_H
|
||||
#define _TABBY_AVR_PRINTER_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <tabby/printer.h>
|
||||
|
||||
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 */
|
Loading…
Add table
Reference in a new issue