Compare commits

..

No commits in common. "b2502e7f0a20793971972cdec319bf185ce4f299" and "7a7399a602f6a7b7b1ecda128031eaf1fb31326c" have entirely different histories.

4 changed files with 93 additions and 417 deletions

View File

@ -11,7 +11,6 @@
#include <linux/uaccess.h> #include <linux/uaccess.h>
#include <linux/sched.h> #include <linux/sched.h>
#include <linux/poll.h> #include <linux/poll.h>
#include <linux/circ_buf.h>
// #define ENABLE_DEBUG ENABLE_DEBUG // #define ENABLE_DEBUG ENABLE_DEBUG
@ -28,11 +27,6 @@ MODULE_PARM_DESC(debug_level, "Debug level");
static struct serdev_device *static_serdev = NULL; static struct serdev_device *static_serdev = NULL;
static DECLARE_WAIT_QUEUE_HEAD(wq); static DECLARE_WAIT_QUEUE_HEAD(wq);
void pbuf_work_handler(struct work_struct *work);
DECLARE_WORK(pbuf_work, pbuf_work_handler);
static DEFINE_SPINLOCK(printer_status_lock);
static struct of_device_id lw35ioc_dt_ids[] = { static struct of_device_id lw35ioc_dt_ids[] = {
{ .compatible = "brother,lw35", }, { .compatible = "brother,lw35", },
{ /* sentinel */ } { /* sentinel */ }
@ -43,7 +37,7 @@ MODULE_DEVICE_TABLE(of, lw35ioc_dt_ids);
#define DEVICE_NAME "lw35" #define DEVICE_NAME "lw35"
#define CLASS_NAME "lw35ioc" #define CLASS_NAME "lw35ioc"
/* Only used for initialization, not for actual keycode mapping */ /* Only used for initialization, not for actualy keycode mapping */
static int keycode_map[] = { static int keycode_map[] = {
KEY_A, KEY_B, KEY_C, KEY_D, KEY_E, KEY_F, KEY_G, KEY_H, KEY_I, KEY_J, KEY_A, KEY_B, KEY_C, KEY_D, KEY_E, KEY_F, KEY_G, KEY_H, KEY_I, KEY_J,
KEY_K, KEY_L, KEY_M, KEY_N, KEY_O, KEY_P, KEY_Q, KEY_R, KEY_S, KEY_T, KEY_K, KEY_L, KEY_M, KEY_N, KEY_O, KEY_P, KEY_Q, KEY_R, KEY_S, KEY_T,
@ -67,7 +61,6 @@ enum lw35ioc_packet_state {SYNC, TYPE, KEYBOARD, PRINTER};
struct lw35ioc_packet { struct lw35ioc_packet {
enum lw35ioc_packet_state state; enum lw35ioc_packet_state state;
int payload_offset;
}; };
struct lw35ioc_chardev { struct lw35ioc_chardev {
@ -77,15 +70,7 @@ struct lw35ioc_chardev {
int open_count; int open_count;
}; };
struct lw35ioc_printer_status{
int buffer_free;
int status;
int reserved0;
int reserved1;
};
#define UART_BUF_SIZE 256 #define UART_BUF_SIZE 256
#define PBUF_SIZE 256
struct lw35ioc_priv { struct lw35ioc_priv {
struct serdev_device *serdev; struct serdev_device *serdev;
@ -93,17 +78,10 @@ struct lw35ioc_priv {
struct gpio_desc *gpio_rst; /* GPIO to reset both AVRs */ struct gpio_desc *gpio_rst; /* GPIO to reset both AVRs */
struct lw35ioc_chardev chardev; struct lw35ioc_chardev chardev;
struct lw35ioc_printer_status printer_status_temp;
struct lw35ioc_printer_status printer_status;
char uart_buf_rx[UART_BUF_SIZE]; char uart_buf_rx[UART_BUF_SIZE];
char *uart_buf_rx_rd; char *uart_buf_rx_rd;
char *uart_buf_rx_wr; char *uart_buf_rx_wr;
char pbuf[PBUF_SIZE];
unsigned long pbuf_tail;
unsigned long pbuf_head;
enum lw35ioc_state state; enum lw35ioc_state state;
struct lw35ioc_packet packet; struct lw35ioc_packet packet;
}; };
@ -117,7 +95,6 @@ static int lw35ioc_process_buf(struct serdev_device *serdev,
unsigned char c; unsigned char c;
int ev; int ev;
int i; int i;
unsigned long lock_flags;
#ifdef ENABLE_DEBUG #ifdef ENABLE_DEBUG
dev_info(&serdev->dev, "IRQ.\n"); dev_info(&serdev->dev, "IRQ.\n");
@ -132,7 +109,6 @@ static int lw35ioc_process_buf(struct serdev_device *serdev,
if (c == 0xFF) if (c == 0xFF)
priv->packet.state = TYPE; priv->packet.state = TYPE;
break; break;
case TYPE: case TYPE:
if (c == 0x55) if (c == 0x55)
priv->packet.state = KEYBOARD; priv->packet.state = KEYBOARD;
@ -140,9 +116,7 @@ static int lw35ioc_process_buf(struct serdev_device *serdev,
priv->packet.state = PRINTER; priv->packet.state = PRINTER;
else else
priv->packet.state = SYNC; priv->packet.state = SYNC;
priv->packet.payload_offset = 0;
break; break;
case KEYBOARD: case KEYBOARD:
if (c > 127) { if (c > 127) {
c -= 128; c -= 128;
@ -154,46 +128,9 @@ static int lw35ioc_process_buf(struct serdev_device *serdev,
input_sync(priv->input_dev); input_sync(priv->input_dev);
priv->packet.state = SYNC; priv->packet.state = SYNC;
break; break;
case PRINTER: case PRINTER:
switch (priv->packet.payload_offset) { dev_info(&serdev->dev, "Printer not yet implemented.\n");
case 0: /* Status */ priv->packet.state = SYNC;
priv->printer_status_temp.status = (int)c;
break;
case 1: /* Buffer status */
priv->printer_status_temp.buffer_free = (int)c;
break;
case 2: /* Reserved */
priv->printer_status_temp.reserved0 = (int)c;
break;
case 3: /* Reserved */
priv->printer_status_temp.reserved1 = (int)c;
dev_info(&serdev->dev,
"New printer status. status=0x%02X buffer_free=0x%02X [0x%02X,0x%02X].\n",
priv->printer_status_temp.status,
priv->printer_status_temp.buffer_free,
priv->printer_status_temp.reserved0,
priv->printer_status_temp.reserved1);
if (priv->printer_status_temp.status == 0xFF) {
dev_err(&serdev->dev,
"HARDFAULT in printer firmware! Reset required.\n");
}
spin_lock_irqsave(&printer_status_lock, lock_flags);
memcpy(&priv->printer_status,
&priv->printer_status_temp,
sizeof(priv->printer_status));
spin_unlock_irqrestore(&printer_status_lock, lock_flags);
if (priv->printer_status_temp.buffer_free > 0)
schedule_work(&pbuf_work);
priv->packet.state = SYNC;
break;
default:
break;
}
priv->packet.payload_offset++;
break; break;
} }
} }
@ -208,32 +145,26 @@ static int lw35ioc_receive_buf(struct serdev_device *serdev,
struct lw35ioc_priv *priv = serdev_device_get_drvdata(serdev); struct lw35ioc_priv *priv = serdev_device_get_drvdata(serdev);
int i; int i;
switch (priv->state) { if (priv->state == ACTIVE)
case ACTIVE:
return lw35ioc_process_buf(serdev, data, count); return lw35ioc_process_buf(serdev, data, count);
break;
case PASSIVE: for (i = 0; i < count; ++i) {
break;
default:
/* Pass data directly to chardev */
for (i = 0; i < count; ++i) {
#ifdef ENABLE_DEBUG #ifdef ENABLE_DEBUG
dev_info(&serdev->dev, " RX [PSV]: %02x\n", data[i]); dev_info(&serdev->dev, " RX [PSV]: %02x\n", data[i]);
#endif #endif
*(priv->uart_buf_rx_wr) = data[i]; *(priv->uart_buf_rx_wr) = data[i];
if (priv->uart_buf_rx_wr == priv->uart_buf_rx + UART_BUF_SIZE - 1) if (priv->uart_buf_rx_wr == priv->uart_buf_rx + UART_BUF_SIZE - 1)
priv->uart_buf_rx_wr = priv->uart_buf_rx; priv->uart_buf_rx_wr = priv->uart_buf_rx;
else else
priv->uart_buf_rx_wr++; priv->uart_buf_rx_wr++;
if (priv->uart_buf_rx_rd == priv->uart_buf_rx_wr) { if (priv->uart_buf_rx_rd == priv->uart_buf_rx_wr) {
dev_err(&serdev->dev, "UART RX buffer overflow.\n"); dev_err(&serdev->dev, "UART RX buffer overflow.\n");
}
} }
wake_up_interruptible(&wq);
} }
wake_up_interruptible(&wq);
return count; /* Pretend we read all data */ return count; /* Pretend we read all data */
} }
@ -274,38 +205,32 @@ static int lw35ioc_chardev_open(struct inode *inodep, struct file *filep) {
if (!priv->chardev.open_count) { if (!priv->chardev.open_count) {
dev_info(&serdev->dev, "Opened chardev.\n"); dev_info(&serdev->dev, "Opened chardev.\n");
switch (priv->state) { if (priv->state != ACTIVE &&
case PROG_KBD: priv->state != PASSIVE) {
lw35ioc_reset_ioc_hw(serdev); lw35ioc_reset_ioc_hw(serdev);
serdev_device_write(serdev, "X", 1, 0);
dev_info(&serdev->dev, "Put ioc into programming mode: keyboard.\n");
break;
case PROG_PRI:
lw35ioc_reset_ioc_hw(serdev);
serdev_device_write(serdev, "Y", 1, 0);
dev_info(&serdev->dev, "Put ioc into programming mode: printer.\n");
break;
case BYPASS:
lw35ioc_reset_ioc_hw(serdev);
serdev_device_write(serdev, "XX", 2, 0);
dev_info(&serdev->dev, "Put ioc into bypass mode.\n");
break;
case PASSIVE:
lw35ioc_reset_ioc_hw(serdev);
usleep_range(1000000, 1010000);
dev_info(&serdev->dev, "Put ioc into active mode.\n");
priv->state = ACTIVE;
break;
case ACTIVE:
/* Don't do anything when active */
break;
default:
break;
}
switch (priv->state) {
case PROG_KBD:
serdev_device_write(serdev, "X", 1, 0);
dev_info(&serdev->dev, "Put ioc into programming mode: keyboard.\n");
break;
case PROG_PRI:
serdev_device_write(serdev, "Y", 1, 0);
dev_info(&serdev->dev, "Put ioc into programming mode: printer.\n");
break;
case BYPASS:
serdev_device_write(serdev, "XX", 2, 0);
dev_info(&serdev->dev, "Put ioc into bypass mode.\n");
break;
default:
/* For other modes, we just wait for the reset to be over */
usleep_range(1000000, 1010000);
break;
}
}
} else { } else {
dev_err(&serdev->dev, "Rejected. Device may only be opened once.\n"); dev_err(&serdev->dev, "Rejected. Device may only be opened once.\n");
//return -EBUSY; // TODO //return -EBUSY;
} }
priv->chardev.open_count++; priv->chardev.open_count++;
@ -326,7 +251,7 @@ static ssize_t lw35ioc_chardev_read(struct file *filep, char *buffer, size_t len
wait_event_interruptible(wq, priv->uart_buf_rx_rd != priv->uart_buf_rx_wr); wait_event_interruptible(wq, priv->uart_buf_rx_rd != priv->uart_buf_rx_wr);
available = priv->uart_buf_rx_wr - priv->uart_buf_rx_rd; available = priv->uart_buf_rx_wr - priv->uart_buf_rx_rd;
if (available < 0) if (available < 0)
available = 1; /* TODO: We should calculate the remaining block until wraparound here... */ available = 1;
if (!available) if (!available)
return -EAGAIN; return -EAGAIN;
} }
@ -354,61 +279,12 @@ char msg[256]; // TODO
static ssize_t lw35ioc_chardev_write(struct file *filep, const char *buffer, size_t len, loff_t *offset) { static ssize_t lw35ioc_chardev_write(struct file *filep, const char *buffer, size_t len, loff_t *offset) {
struct serdev_device *serdev = filep->private_data; struct serdev_device *serdev = filep->private_data;
struct lw35ioc_priv *priv = serdev_device_get_drvdata(serdev); struct lw35ioc_priv *priv = serdev_device_get_drvdata(serdev);
unsigned long tail;
int i;
int ret;
switch (priv->state) { if (len > 256) {
case ACTIVE: len = 256;
/* Put data into pbuf */
// TODO: spinlock
if (CIRC_SPACE(priv->pbuf_head, READ_ONCE(priv->pbuf_tail), PBUF_SIZE) < 1) {
dev_err(&serdev->dev, "chdev write: Waiting for space...\n");
if (ret = (wait_event_interruptible(wq, CIRC_SPACE(priv->pbuf_head, priv->pbuf_tail, PBUF_SIZE) >= 1))) { // TODO: Accessing pbuf_tail without any protection... this might be bad?
dev_err(&serdev->dev,
"chdev write: device closed while waiting. ret: %d\n",
ret);
// TODO: This is not proper handling of this I guess...
return len;
}
dev_err(&serdev->dev, "chdev write: done waiting...\n");
}
tail = READ_ONCE(priv->pbuf_tail);
for (i = 0; i < len; ++i) { // TODO: Block copy would be faster...
if (CIRC_SPACE(priv->pbuf_head, tail, PBUF_SIZE) >= 1) {
ret = copy_from_user(priv->pbuf + priv->pbuf_head,
buffer + i,
1);
if (ret) {
dev_err(&serdev->dev, "chdev write: copy_from_user failed (%d).\n",
ret);
i = len - 1;
}
smp_store_release(&(priv->pbuf_head),
(priv->pbuf_head + 1) & (PBUF_SIZE - 1));
} else {
dev_warn(&serdev->dev, "chdev write: ring buffer full.\n");
break;
}
}
len = i;
schedule_work(&pbuf_work);
dev_info(&serdev->dev,
"Copied %d bytes to FIFO (FIFO free: %d)",
len, CIRC_SPACE(priv->pbuf_head, tail, PBUF_SIZE));
break;
case PASSIVE:
len = -EBUSY; // TODO: Can I do this?
break;
default: /* Pass-through */
if (len > 256) {
len = 256;
}
copy_from_user(msg, buffer, len); // TODO: Return value
serdev_device_write_buf(serdev, msg, len);
} }
copy_from_user(msg, buffer, len);
serdev_device_write_buf(serdev, msg, len);
return len; return len;
} }
@ -461,75 +337,6 @@ __poll_t poll(struct file *filep, struct poll_table_struct *ptab)
return 0; return 0;
} }
#define PRINTER_READY_MASK (0b00011111)
int printer_ready_for_data(struct lw35ioc_printer_status printer_status)
{
return ((printer_status.status & PRINTER_READY_MASK) == PRINTER_READY_MASK);
}
void pbuf_work_handler(struct work_struct *work)
{
// TODO: get private data using container_of(): https://linux-kernel-labs.github.io/master/labs/deferred_work.html
struct serdev_device *serdev = static_serdev; // Not cool, but whatever...
struct lw35ioc_priv *priv = serdev_device_get_drvdata(serdev);
unsigned long head;
unsigned long tail;
unsigned long lock_flags;
int bsize = 0;
int i;
int locked = 0;
dev_info(&serdev->dev,
"pbuf_work_handler\n");
while (1) {
// TODO: We are the only consumer, so we should be fine without a spinlock, right?
head = smp_load_acquire(&priv->pbuf_head);
tail = priv->pbuf_tail;
bsize = CIRC_CNT(head, tail, PBUF_SIZE);
dev_info(&serdev->dev, "CIRC CNT=%d\n", bsize);
if (bsize == 0)
break;
spin_lock_irqsave(&printer_status_lock, lock_flags);
locked = 1;
if (!printer_ready_for_data(priv->printer_status)) {
dev_warn(&serdev->dev, "Cannot transmit data to device. Device is not ready.\n");
break;
}
if (bsize > priv->printer_status.buffer_free)
bsize = priv->printer_status.buffer_free;
if (bsize > CIRC_CNT_TO_END(head, tail, PBUF_SIZE))
bsize = CIRC_CNT_TO_END(head, tail, PBUF_SIZE);
if (bsize > priv->printer_status.buffer_free - 20)
bsize = priv->printer_status.buffer_free - 20; // TODO: Bug workaround
dev_info(&serdev->dev, "TX %d of %d bytes [%d bytes free on device].\n",
bsize,
CIRC_CNT(head, tail, PBUF_SIZE),
priv->printer_status.buffer_free);
priv->printer_status.buffer_free-=bsize;
spin_unlock_irqrestore(&printer_status_lock, lock_flags);
locked = 0;
if (bsize == 0) {
dev_warn(&serdev->dev, "Cannot transmit data to device. Device buffer is full.\n");
break;
}
serdev_device_write_buf(serdev,
priv->pbuf + tail,
bsize);
for (i=0; i<bsize; ++i) {
dev_info(&serdev->dev,
"UART TX: %c\n",
priv->pbuf[tail + i]);
}
smp_store_release(&priv->pbuf_tail,
(tail + bsize) & (PBUF_SIZE - 1));
}
if (locked)
spin_unlock_irqrestore(&printer_status_lock, lock_flags);
wake_up_interruptible(&wq); // There's some space available in the FIFO now, so wake up chardev write
}
static struct file_operations fops = static struct file_operations fops =
@ -575,7 +382,7 @@ static int lw35ioc_cdev_init(struct serdev_device *serdev)
return PTR_ERR(priv->chardev.device); return PTR_ERR(priv->chardev.device);
} }
dev_info(&serdev->dev, "Device class created correctly\n"); dev_err(&serdev->dev, "Device class created correctly\n");
return 0; return 0;
} }
@ -607,16 +414,11 @@ static int lw35ioc_probe(struct serdev_device *serdev)
priv->uart_buf_rx_rd = priv->uart_buf_rx; priv->uart_buf_rx_rd = priv->uart_buf_rx;
priv->uart_buf_rx_wr = priv->uart_buf_rx; priv->uart_buf_rx_wr = priv->uart_buf_rx;
priv->pbuf_tail = 0;
priv->pbuf_head = 0;
priv->printer_status.buffer_free = 0;
priv->printer_status.status = 0;
priv->serdev = serdev; priv->serdev = serdev;
static_serdev = serdev; static_serdev = serdev;
priv->packet.state = SYNC; priv->packet.state = SYNC;
serdev_device_set_drvdata(serdev, priv); serdev_device_set_drvdata(serdev, priv);
serdev_device_set_client_ops(serdev, &lw35ioc_serdev_ops);
/* Register input device */ /* Register input device */
input_dev = devm_input_allocate_device(&serdev->dev); input_dev = devm_input_allocate_device(&serdev->dev);
@ -638,13 +440,12 @@ static int lw35ioc_probe(struct serdev_device *serdev)
goto fail_input; goto fail_input;
} }
serdev_device_set_client_ops(serdev, &lw35ioc_serdev_ops);
ret = serdev_device_open(serdev); ret = serdev_device_open(serdev);
if (ret) { if (ret) {
dev_err(&serdev->dev, "Unable to open device.\n"); dev_err(&serdev->dev, "Unable to open device.\n");
goto fail_serdev; goto fail_serdev;
} }
speed = serdev_device_set_baudrate(serdev, speed); speed = serdev_device_set_baudrate(serdev, speed);
dev_info(&serdev->dev, "Using baudrate: %u\n", speed); dev_info(&serdev->dev, "Using baudrate: %u\n", speed);
@ -685,26 +486,17 @@ static void lw35ioc_remove(struct serdev_device *serdev)
{ {
struct lw35ioc_priv *priv = serdev_device_get_drvdata(serdev); struct lw35ioc_priv *priv = serdev_device_get_drvdata(serdev);
dev_info(&serdev->dev, "remove\n");
dev_info(&serdev->dev, " cdev class: %p\n", priv->chardev.class);
device_destroy(priv->chardev.class, MKDEV(priv->chardev.major_number, 0)); device_destroy(priv->chardev.class, MKDEV(priv->chardev.major_number, 0));
dev_info(&serdev->dev, "unreg\n");
class_unregister(priv->chardev.class); class_unregister(priv->chardev.class);
dev_info(&serdev->dev, "destroy: SKIP\n"); class_destroy(priv->chardev.class);
//class_destroy(priv->chardev.class);
dev_info(&serdev->dev, "unreg-dev\n");
unregister_chrdev(priv->chardev.major_number, DEVICE_NAME); unregister_chrdev(priv->chardev.major_number, DEVICE_NAME);
dev_info(&serdev->dev, " gpio rst: %p\n", priv->gpio_rst);
dev_info(&serdev->dev, " serdev: %p\n", priv->serdev);
dev_info(&serdev->dev, " input dev: %p\n", priv->input_dev);
lw35ioc_set_hw_reset(serdev, 1); lw35ioc_set_hw_reset(serdev, 1);
gpiod_put(priv->gpio_rst); gpiod_put(priv->gpio_rst);
serdev_device_close(priv->serdev); serdev_device_close(priv->serdev);
input_unregister_device(priv->input_dev); input_unregister_device(priv->input_dev);
dev_info(&serdev->dev, "done.\n"); dev_info(&serdev->dev, "remove\n");
return; return;
} }

View File

@ -1,5 +0,0 @@
cmake_minimum_required(VERSION 3.5)
project(lw35-tool LANGUAGES C)
add_executable(lw35-tool main.c)

View File

@ -14,7 +14,7 @@
#define BAUDRATE 19200 #define BAUDRATE 19200
#define PRINTER_PACKET_SIZE 6 #define PRINTER_PACKET_SIZE 4
/* Keycode from include/linux/input.h */ /* Keycode from include/linux/input.h */
#define KEY_RESERVED 0 #define KEY_RESERVED 0
@ -214,8 +214,7 @@ void uart_putc(char c)
ISR(USART_RXC_vect) ISR(USART_RXC_vect)
{ {
if (UCSRA & (1 << RXC)) { if (UCSRA & (1 << RXC)) {
ring_buffer[ring_buffer_wr] = UDR; ring_buffer[ring_buffer_wr++] = UDR;// WARNING: No overflow protection
ring_buffer_wr++; // WARNING: No overflow protection
// Will auto wrap at 256; print_buffer_wr = print_buffer_wr % PRINT_BUFFER_SIZE; // Will auto wrap at 256; print_buffer_wr = print_buffer_wr % PRINT_BUFFER_SIZE;
} }
} }
@ -230,20 +229,11 @@ void uart_forward_packets()
else else
size = ring_buffer_rd - ring_buffer_wr; size = ring_buffer_rd - ring_buffer_wr;
if (size >= PRINTER_PACKET_SIZE) { if (size > PRINTER_PACKET_SIZE) {
/* check sync */ uart_putc(0xFF);
if (ring_buffer[ring_buffer_rd] != ((char)0xFF)) { uart_putc(0xAA);
ring_buffer_rd++; /* Drop wrong sync header */
return;
}
if (ring_buffer[ring_buffer_rd + 1] != ((char)0xAA)) {
ring_buffer_rd += 1; /* Drop wrong app header */
return;
}
for (i = 0; i < PRINTER_PACKET_SIZE; ++i) { for (i = 0; i < PRINTER_PACKET_SIZE; ++i) {
uart_putc(ring_buffer[ring_buffer_rd]); // Will auto wrap at 256 uart_putc(ring_buffer[ring_buffer_rd++]); // Will auto wrap at 256
ring_buffer_rd++;
} }
} }
} }

View File

@ -5,7 +5,6 @@
#include <avr/pgmspace.h> #include <avr/pgmspace.h>
#define BAUDRATE 19200 #define BAUDRATE 19200
//#define DEBUG_BUILD DEBUG_BUILD
/* System functions */ /* System functions */
/* Timers */ /* Timers */
@ -54,7 +53,8 @@ void uart_putc(char c)
UDR = c; UDR = c;
} }
#ifdef DEBUG_BUILD #define ENABLE_SERIAL_DEBUG
#ifdef ENABLE_SERIAL_DEBUG
void debug_putc(char c) { void debug_putc(char c) {
uart_putc(c); uart_putc(c);
}; };
@ -90,20 +90,10 @@ unsigned int debug_printf(char *format,...) {return 0;};
void hardfault() { void hardfault() {
cli(); cli();
debug_write("HARDFAULT!\r\n");
PORTB = 0; PORTB = 0;
PORTC = 0; PORTC = 0;
PORTD = 0; PORTD = 0;
uart_putc(0xFF);
uart_putc(0xFF);
uart_putc(0xFF);
uart_putc(0xFF);
uart_putc(0xFF);
uart_putc(0xFF);
uart_putc(0xFF);
uart_putc(0x00);
uart_putc(0x00);
uart_putc(0x00);
debug_write("HARDFAULT!\r\n");
while(1); while(1);
} }
@ -570,99 +560,16 @@ void arm_correction()
#define DCMOTOR_EN MOTLIM(PORT) |= PIN_DCMOTOR #define DCMOTOR_EN MOTLIM(PORT) |= PIN_DCMOTOR
#define DCMOTOR_STOP MOTLIM(PORT) &= ~PIN_DCMOTOR #define DCMOTOR_STOP MOTLIM(PORT) &= ~PIN_DCMOTOR
#define DCMOTOR_ISACTIVE (!!(MOTLIM(PORT) & PIN_DCMOTOR)) #define DCMOTOR_ISACTIVE (MOTLIM(PORT) & PIN_DCMOTOR)
#define LIMITSWITCH (!(MOTLIM(PIN) & PIN_LIMITSWITCH)) #define LIMITSWITCH (!(MOTLIM(PIN) & PIN_LIMITSWITCH))
/* Communication Functions */
#define RINGBUF_CNT(wr,rd) ((wr) - (rd))
#define RINGBUF_FREE(wr,rd,sz) ((RINGBUF_CNT(rd,wr+1))) // TODO: 255 is actually not correct...
#define PRINT_BUFFER_SIZE 256 // Important: must be 256 atm, when changed, you need to change code handling print_buffer_*.
volatile char print_buffer[PRINT_BUFFER_SIZE];
volatile uint8_t print_buffer_wr = 0;
volatile uint8_t print_buffer_rd = 0;
#define UART_BYTE_READY (UCSRA & (1 << RXC))
ISR(USART_RXC_vect)
{
if (UCSRA & (1 << RXC)) {
print_buffer[print_buffer_wr++] = UDR;// WARNING: No overflow protection
// Will auto wrap at 256; print_buffer_wr = print_buffer_wr % PRINT_BUFFER_SIZE;
}
}
#define TX_BUFFER_SIZE 256 // Important: must be 256 atm, when changed, you need to change code handling print_buffer_*.
char tx_buffer[TX_BUFFER_SIZE];
uint8_t tx_buffer_wr = 0;
uint8_t tx_buffer_rd = 0;
uint8_t tx_buffer_async = 0;
uint8_t global_status;
#define STATUS_PAPERFEED (1 << 0)
#define STATUS_CARRIAGE (1 << 1)
#define STATUS_DAISY (1 << 2)
#define STATUS_PRINTHEAD (1 << 3)
#define STATUS_READY (1 << 4)
#define STATUSBIT_ACTIVE 5
void tx_buffer_putc(char c)
{
if (tx_buffer_async) {
if (tx_buffer_rd == tx_buffer_wr + 1) // implicit % 256
hardfault();
tx_buffer[tx_buffer_wr++] = c; // implicit % 256
} else {
uart_putc(c);
}
}
void tx_status()
{
unsigned char local_status = 0;
local_status |= (DCMOTOR_ISACTIVE << STATUSBIT_ACTIVE);
tx_buffer_putc(0xFF); /* Header[0] */
tx_buffer_putc(0xAA); /* Header[1] */
tx_buffer_putc((char) (global_status | local_status));
tx_buffer_putc(RINGBUF_FREE(print_buffer_wr, print_buffer_rd, PRINT_BUFFER_SIZE));
tx_buffer_putc(0); /* RESERVED */
tx_buffer_putc(0); /* RESERVED */
}
void update_status(uint8_t status)
{
global_status = status;
tx_status();
}
void set_status(uint8_t bit)
{
update_status(global_status | bit);
}
void clear_status(uint8_t bit)
{
update_status(global_status & ~bit);
}
void tx_buffer_process()
{
if (tx_buffer_rd != tx_buffer_wr) {
if (UCSRA & (1 << UDRE)) {
UDR = tx_buffer[tx_buffer_rd];
tx_buffer_rd++; // implicit % 256
}
}
}
/* Initialization Functions */ /* Initialization Functions */
void move_carriage_to_far_left(uint8_t reset) void move_carriage_to_far_left(uint8_t reset)
{ {
uint16_t cnt = 0; uint16_t cnt = 0;
clear_status(STATUS_CARRIAGE);
/* Init stepper controller */ /* Init stepper controller */
if (reset) { if (reset) {
stepper_status_CARRIAGE.step = 0; stepper_status_CARRIAGE.step = 0;
@ -698,7 +605,6 @@ void move_carriage_to_far_left(uint8_t reset)
stepper_status_CARRIAGE.target_pos = 0; stepper_status_CARRIAGE.target_pos = 0;
debug_printf("[car] Carriage left after %u steps.\r\n", cnt); debug_printf("[car] Carriage left after %u steps.\r\n", cnt);
set_status(STATUS_CARRIAGE);
} }
void align_daisy_wheel() void align_daisy_wheel()
@ -706,8 +612,6 @@ void align_daisy_wheel()
int i; int i;
debug_write("[whl] Aligning wheel...\r\n"); debug_write("[whl] Aligning wheel...\r\n");
clear_status(STATUS_DAISY);
stepper_status_WHEEL.dir = 0; stepper_status_WHEEL.dir = 0;
stepper_status_WHEEL.lpstate = 0; stepper_status_WHEEL.lpstate = 0;
stepper_status_WHEEL.pos = 0; stepper_status_WHEEL.pos = 0;
@ -747,18 +651,15 @@ void align_daisy_wheel()
stepper_status_WHEEL.pos = 0; stepper_status_WHEEL.pos = 0;
debug_write("[whl] Alignment completed.\r\n"); debug_write("[whl] Alignment completed.\r\n");
set_status(STATUS_DAISY);
} }
void reset_printhead() void reset_printhead()
{ {
debug_write("[hmr] Resetting printhead...\r\n"); debug_write("[hmr] Resetting printhead...\r\n");
clear_status(STATUS_PRINTHEAD);
DCMOTOR_EN; DCMOTOR_EN;
_delay_ms(200); _delay_ms(200);
DCMOTOR_STOP; DCMOTOR_STOP;
debug_write("[hmr] Printhead reset completed.\r\n"); debug_write("[hmr] Printhead reset completed.\r\n");
set_status(STATUS_PRINTHEAD);
} }
void initialize_paperfeed() void initialize_paperfeed()
@ -766,7 +667,6 @@ void initialize_paperfeed()
int i; int i;
debug_write("[lfd] Initializing paperfeed...\r\n"); debug_write("[lfd] Initializing paperfeed...\r\n");
clear_status(STATUS_PAPERFEED);
for (i = 0; i < 10;) { for (i = 0; i < 10;) {
if (block_for(TIM_LINEFEED, STEPPER_CFG_LINEFEED.delay)) { if (block_for(TIM_LINEFEED, STEPPER_CFG_LINEFEED.delay)) {
@ -784,10 +684,26 @@ void initialize_paperfeed()
stepper_status_LINEFEED.ldir = 0; stepper_status_LINEFEED.ldir = 0;
debug_write("[lfd] Initialization completed.\r\n"); debug_write("[lfd] Initialization completed.\r\n");
set_status(STATUS_PAPERFEED);
} }
/* Printer Functions */ /* Printer Functions */
#define PRINT_BUFFER_SIZE 256 // Important: must be 256 atm, when changed, you need to change code handling print_buffer_*.
volatile char print_buffer[PRINT_BUFFER_SIZE];
volatile uint8_t print_buffer_wr = 0;
volatile uint8_t print_buffer_rd = 0;
#define UART_BYTE_READY (UCSRA & (1 << RXC))
ISR(USART_RXC_vect)
{
if (UCSRA & (1 << RXC)) {
print_buffer[print_buffer_wr++] = UDR;// WARNING: No overflow protection
// Will auto wrap at 256; print_buffer_wr = print_buffer_wr % PRINT_BUFFER_SIZE;
}
}
#define PRINT_CHARACTER_WIDTH 10 #define PRINT_CHARACTER_WIDTH 10
#define PRINT_LINE_HEIGHT 34 #define PRINT_LINE_HEIGHT 34
#define PRINTER_STOP_RIGHT 1100 #define PRINTER_STOP_RIGHT 1100
@ -925,7 +841,6 @@ void printer_process()
} else { } else {
current_char = print_buffer[print_buffer_rd]; current_char = print_buffer[print_buffer_rd];
print_buffer_rd++; print_buffer_rd++;
tx_status(); // Tell SoC when bytes are consumed from the buffer. TODO: Too often?
} }
} else { } else {
current_char = '\0'; current_char = '\0';
@ -935,30 +850,31 @@ void printer_process()
current_is_linebreak += (current_char == '\n'); current_is_linebreak += (current_char == '\n');
if (current_is_linebreak) { if (current_is_linebreak) {
if (backward) { if (backward) {
debug_putc('='); uart_putc('=');
print_buffer_rd = print_buffer_recovery; print_buffer_rd = print_buffer_recovery;
tx_status();
} }
backward = 0; // Re-evaluate backward. Default is forward. backward = 0; // Re-evaluate backward. Default is forward.
stepper_status_LINEFEED.target_pos += PRINT_LINE_HEIGHT; stepper_status_LINEFEED.target_pos += PRINT_LINE_HEIGHT;
debug_putc('\n'); uart_putc('\n');
current_is_linebreak--; current_is_linebreak--;
//_delay_ms(1000);
/* Decide whether we move the carriage back to the left, or /* Decide whether we move the carriage back to the left, or
* whether to print the new line in reverse. */ * whether print the new line in reverse. */
end_of_next_line = print_buffer_rd; end_of_next_line = print_buffer_rd;
temp = simulate_print_run(&end_of_next_line, &print_buffer_recovery); temp = simulate_print_run(&end_of_next_line, &print_buffer_recovery);
if (temp == -1) { if (temp == -1) {
debug_putc('['); //uart_putc('\r');
uart_putc('[');
backward = 1; backward = 1;
print_buffer_backward = end_of_next_line; print_buffer_backward = end_of_next_line;
} else if (temp == 0) { } else if (temp == 0) {
// do nothing // do nothing
debug_putc('#'); uart_putc('#');
} else { } else {
move_carriage_to_left_margin(); move_carriage_to_left_margin();
@ -975,7 +891,7 @@ void printer_process()
case PRINTER_NO_CHAR: case PRINTER_NO_CHAR:
current_is_linebreak += move_one_character(backward); current_is_linebreak += move_one_character(backward);
if (!current_is_linebreak) if (!current_is_linebreak)
debug_putc(' '); uart_putc(' ');
break; break;
default: /* It's a printable character */ default: /* It's a printable character */
@ -996,7 +912,7 @@ void printer_process()
if (POSITION_REACHED(WHEEL) && POSITION_REACHED(CARRIAGE) && POSITION_REACHED(LINEFEED)) { if (POSITION_REACHED(WHEEL) && POSITION_REACHED(CARRIAGE) && POSITION_REACHED(LINEFEED)) {
arm_hammer(); arm_hammer();
_delay_ms(50); // TODO: replace with timer _delay_ms(50); // TODO: replace with timer
debug_putc(debug_character); uart_putc(debug_character);
current_is_linebreak += move_one_character(backward); current_is_linebreak += move_one_character(backward);
state = IDLE; state = IDLE;
} }
@ -1027,16 +943,7 @@ void printer_process()
stepper_perform_movement(&stepper_status_WHEEL, &STEPPER_CFG_WHEEL); stepper_perform_movement(&stepper_status_WHEEL, &STEPPER_CFG_WHEEL);
} }
void sync_uart()
{
int i;
for (i = 0; i < 10; ++i) {
tx_buffer_putc(0xFF);
}
}
/* Test Functions */ /* Test Functions */
#ifdef DEBUG_BUILD
void systick_test() void systick_test()
{ {
debug_write("[tmr] System timer test\r\n"); debug_write("[tmr] System timer test\r\n");
@ -1228,17 +1135,11 @@ static void system_test_auto()
} }
} }
} }
#endif
void mainloop() void mainloop()
{ {
tx_buffer_async = 1;
uart_irq_enable(); /* rx buffer process */
set_status(STATUS_READY);
while (1) { while (1) {
printer_process(); printer_process();
tx_buffer_process();
} }
} }
@ -1280,26 +1181,24 @@ int main()
TIMSK = (1 << OCIE1A); TIMSK = (1 << OCIE1A);
/* Init system */ /* Init system */
sync_uart();
update_status(0);
debug_write("\n\n\r[sys] STARTING IO CONTROLLER...\r\n"); debug_write("\n\n\r[sys] STARTING IO CONTROLLER...\r\n");
debug_write("[sys] Enabling interrupts.\r\n"); debug_write("[sys] Enabling interrupts.\r\n");
sei(); sei();
/* Align printer */ /* Align printer */
initialize_paperfeed(); //initialize_paperfeed();
move_carriage_to_far_left(1); //move_carriage_to_far_left(1);
align_daisy_wheel(); //align_daisy_wheel();
reset_printhead(); //reset_printhead();
debug_write("[sys] Startup completed.\r\n"); debug_write("[sys] Startup completed.\r\n");
/* Run system */ /* Run system */
#ifdef DEBUG_BUILD
system_test_auto(); system_test_auto();
#else // mainloop();
mainloop(); // printer_test();
#endif // system_test_auto();
// systick_test();
debug_write("[sys] REACHED END OF MAIN. HALTING.\r\n"); debug_write("[sys] REACHED END OF MAIN. HALTING.\r\n");
while (1); while (1);