From 323fa682227c9b283a80a33668a55283875e36f4 Mon Sep 17 00:00:00 2001 From: Markus Koch Date: Sun, 10 May 2020 20:43:53 +0200 Subject: [PATCH] printer:avr: Fix startup behavior if in correction mode Also introduce startup wait until 0xFF is received. --- ioc/kernel-driver/lw35ioc.c | 1 + printer/avr/main.c | 40 +++++++++++++++++++++++++++++-------- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/ioc/kernel-driver/lw35ioc.c b/ioc/kernel-driver/lw35ioc.c index 1cfb90a..ceed250 100644 --- a/ioc/kernel-driver/lw35ioc.c +++ b/ioc/kernel-driver/lw35ioc.c @@ -294,6 +294,7 @@ static int lw35ioc_chardev_open(struct inode *inodep, struct file *filep) { lw35ioc_reset_ioc_hw(serdev); usleep_range(2100000, 2210000); dev_info(&serdev->dev, "Put ioc into active mode.\n"); + serdev_device_write(serdev, "\xFF", 1, 0); priv->state = ACTIVE; break; case ACTIVE: diff --git a/printer/avr/main.c b/printer/avr/main.c index 0876c20..91ebe63 100644 --- a/printer/avr/main.c +++ b/printer/avr/main.c @@ -112,7 +112,7 @@ void hardfault() { #define PRINTER_CONTROL_CHAR 56 #define PRINTER_NO_CHAR 128 -#define ASCII_TRANSLATION_TABLE_SIZE 128 + 12 +#define ASCII_TRANSLATION_TABLE_SIZE (128 + 12) const uint8_t ascii_translation_table[ASCII_TRANSLATION_TABLE_SIZE] PROGMEM = { PRINTER_CONTROL_CHAR, PRINTER_CONTROL_CHAR, @@ -277,6 +277,9 @@ const uint8_t ascii_translation_table[ASCII_TRANSLATION_TABLE_SIZE] PROGMEM = { #define PIN_CORRECTION (1 << 1) #define PIN_ARMHAMMER (1 << 0) +/* Misc I/O */ +#define MISCIO(t) t##D +#define PIN_BACKLIGHT (1 << 2) /* Stepper drive */ @@ -671,11 +674,22 @@ void move_carriage_to_far_left(uint8_t reset) if (LIMITSWITCH) { debug_write("[car] Clearing switch area...\r\n"); - while (LIMITSWITCH) { + cnt = 32; + while (LIMITSWITCH && (cnt> 0)) { if (block_for(TIM_CARRIAGE, STEPPER_CFG_CARRIAGE.delay)) { STEPPER_NEXT(CARRIAGE, 1); + cnt--; } } + if (LIMITSWITCH && !cnt) { // We're probably in correction mode + DCMOTOR_EN; + arm_correction(); + _delay_ms(1000); + DCMOTOR_STOP; + } + if (LIMITSWITCH) { // If the switch is still not released, there's some problem we can't fix from SW + hardfault(); + } for (cnt = 0; cnt < 16; cnt++) { STEPPER_NEXT(CARRIAGE, 1); } @@ -911,7 +925,7 @@ void printer_process() switch (state) { case IDLE: - if (print_buffer_rd != print_buffer_wr) { // maybe add "|| current_is_linebreak" + if (print_buffer_rd != print_buffer_wr) { // TODO: maybe add "|| current_is_linebreak" if (!DCMOTOR_ISACTIVE) { DCMOTOR_EN; _delay_ms(100); /* Let the motor get up to speed */ @@ -1019,7 +1033,8 @@ void printer_process() // stepper_status_WHEEL.target_pos += 10; // // ----------- - if (stepper_status_CARRIAGE.target_pos > PRINTER_STOP_RIGHT) { // Right margin safety + if (stepper_status_CARRIAGE.target_pos > PRINTER_STOP_RIGHT || + stepper_status_CARRIAGE.target_pos < 0) { // Margin safety hardfault(); } stepper_perform_movement(&stepper_status_LINEFEED, &STEPPER_CFG_LINEFEED); @@ -1211,10 +1226,10 @@ static void system_test_auto() if (POSITION_REACHED(WHEEL) && POSITION_REACHED(CARRIAGE) && POSITION_REACHED(LINEFEED) && print_stat) { print_stat = 0; debug_printf("[pos] %uL %uC%c %uW\r\n", - stepper_status_LINEFEED.pos, - stepper_status_CARRIAGE.pos, - (LIMITSWITCH ? '*' : '+'), - stepper_status_WHEEL.pos); + stepper_status_LINEFEED.pos, + stepper_status_CARRIAGE.pos, + (LIMITSWITCH ? '*' : '+'), + stepper_status_WHEEL.pos); } if (do_it) { @@ -1274,6 +1289,10 @@ int main() STEPPER_SET_IO(WHEEL); STEPPER_SET_IO(LINEFEED); + /* Set up miscellaneous I/O */ + MISCIO(DDR) |= PIN_BACKLIGHT; + MISCIO(PORT) |= PIN_BACKLIGHT; /* Permanently enable backlight */ + /* Set up SysTick Timer */ TCCR1B = (1 << WGM12) | (1 << CS11); // f_tim = 8 MHz / 8 OCR1A = 2000 / TIMESCALE; @@ -1283,6 +1302,11 @@ int main() sync_uart(); update_status(0); debug_write("\n\n\r[sys] STARTING IO CONTROLLER...\r\n"); +#ifndef DEBUG_BUILD + while (!((UCSRA & (1 << RXC)) && (UDR == 0xFF))); /* Wait for init command from driver */ + update_status(0); + _delay_ms(250); +#endif debug_write("[sys] Enabling interrupts.\r\n"); sei();