diff --git a/printer/avr/main.c b/printer/avr/main.c index 769a05a..ea0627f 100644 --- a/printer/avr/main.c +++ b/printer/avr/main.c @@ -6,8 +6,6 @@ #define BAUDRATE 19200 -inline void spi_process_data(); - /* System functions */ /* Timers */ enum TIMERS{TIM_CARRIAGE, @@ -40,6 +38,65 @@ ISR (TIMER1_COMPA_vect) } } +/* Hardware Functions */ +static inline void uart_irq_enable() { + UCSRB |= (1 << RXCIE); +} + +static inline void uart_irq_disable() { + UCSRB &= ~(1 << RXCIE); +} + +void uart_putc(char c) +{ + while (!(UCSRA & (1 << UDRE))); + UDR = c; +} + +#define ENABLE_SERIAL_DEBUG +#ifdef ENABLE_SERIAL_DEBUG +void debug_putc(char c) { + uart_putc(c); +}; + +void debug_write(char *c) +{ + while (*c) { + uart_putc(*c); + c++; + } +} + +unsigned int debug_printf(char *format,...) +{ + va_list args; + unsigned int i; + char printbuffer[64]; + + va_start (args, format); + + i = vsprintf (printbuffer, format, args); + va_end (args); + + debug_write(printbuffer); + + return i; +} +#else +void debug_write(char *c) {}; +void debug_putc(char c) {}; +unsigned int debug_printf(char *format,...) {return 0;}; +#endif + +void hardfault() { + cli(); + debug_write("HARDFAULT!\r\n"); + PORTB = 0; + PORTC = 0; + PORTD = 0; + while(1); +} + /* Look-up-tables */ /* Daisy wheel */ #define PRINTER_CONTROL_CHAR 56 @@ -201,50 +258,6 @@ const uint8_t ascii_translation_table[ASCII_TRANSLATION_TABLE_SIZE] PROGMEM = { 55, // Ä }; - -/* Hardware functions */ - -void uart_putc(char c) -{ - while (!(UCSRA & (1 << UDRE))); - UDR = c; -} - -#define ENABLE_SERIAL_DEBUG -#ifdef ENABLE_SERIAL_DEBUG -void debug_putc(char c) { - uart_putc(c); -}; - -void debug_write(char *c) -{ - while (*c) { - uart_putc(*c); - c++; - } -} - -unsigned int debug_printf(char *format,...) -{ - va_list args; - unsigned int i; - char printbuffer[64]; - - va_start (args, format); - - i = vsprintf (printbuffer, format, args); - va_end (args); - - debug_write(printbuffer); - - return i; -} -#else -void debug_write(char *c) {}; -void debug_putc(char c) {}; -unsigned int debug_printf(char *format,...) {return 0;}; -#endif - /* DC drive */ #define MOTLIM(t) t##C #define PIN_DCMOTOR (1 << 4) @@ -551,7 +564,7 @@ void arm_correction() #define LIMITSWITCH (!(MOTLIM(PIN) & PIN_LIMITSWITCH)) -/* Main program code */ +/* Initialization Functions */ void move_carriage_to_far_left(uint8_t reset) { @@ -673,150 +686,23 @@ void initialize_paperfeed() debug_write("[lfd] Initialization completed.\r\n"); } -//void printer_test() -//{ -// uint8_t buf[80] = {0}; -// uint8_t *ptr = buf; -// uint8_t *rdptr = buf; -// uint8_t translated = 0; -// int state = 0; +/* Printer Functions */ -// debug_write("[sys] Entering printer test mode\r\n"); - -// while(1) { - -// switch (state) { -// case 0: -// _delay_ms(100); // Motor turnoff delay -// DCMOTOR_STOP; - -// stepper_status_CARRIAGE.target_pos = 80; -// stepper_status_WHEEL.target_pos = 0;stat->lpstate = pstate; -// ptr = buf; -// debug_write(">"); -// state++; -// break; -// case 1: -// if (UCSRA & (1 << RXC)) { -// //stepper_status_WHEEL.target_pos += 2; -// *ptr = UDR; -// uart_putc(*ptr); -// if (*ptr == '\r') { -// debug_write("\r\nOK.\r\n"); -// DCMOTOR_EN; -// _delay_ms(100); // Let motor get up to speed - -// state++; - -// stepper_status_CARRIAGE.target_pos = 80; -// rdptr = buf; -// } else { -// ptr++; -// } -// } -// break; -// case 2: -// stepper_status_CARRIAGE.target_pos += 10; -// translated = pgm_read_byte(&ascii_translation_table[*rdptr]); -// if (translated != PRINTER_NO_CHAR) { -// //debug_printf("Prepare: %x (%c) -> %d", *rdptr, *rdptr, translated); -// SET_TARGET(WHEEL, translated * 2); -// state++; -// } else { -// //debug_printf("Skip: %x (%c) -> %d\r\n", *rdptr, *rdptr, translated); -// rdptr++; -// if (rdptr == ptr) { -// state = 0; -// } -// } -// break; -// case 3: -// if (POSITION_REACHED(CARRIAGE) && POSITION_REACHED(WHEEL)) { -// //debug_write("!!!\r\n"); -// //DCMOTOR_EN; -// arm_hammer(); -// _delay_ms(50); -// //DCMOTOR_STOP; -// rdptr++; -// if (rdptr == ptr) { -// state = 0; -// } else { -// state--; -// } -// } -// break; -// default: -// break; -// } - -// stepper_perform_movement(&stepper_status_CARRIAGE, &STEPPER_CFG_CARRIAGE); -// stepper_perform_movement(&stepper_status_WHEEL, &STEPPER_CFG_WHEEL); -// } -//} - -void systick_test() -{ - debug_write("[tmr] System timer test\r\n"); - while (1) { - if (block_for(TIM_SYSTEM, 1000 * TIMESCALE)) { - debug_write("tick.\r\n"); - } - if (UCSRA & (1 << RXC)) { - break; - } - } -} - - -void hardfault() { - debug_write("HARDFAULT!\r\n"); - STEPPER_STOP(WHEEL); - STEPPER_STOP(CARRIAGE); - STEPPER_STOP(LINEFEED); - DDRB = 0; - DDRC = 0; - DDRD = 0; - PORTB = 0; - PORTC = 0; - PORTD = 0; - while(1); -} - -/* SPI */ #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 SPI_COMMAND_NONE 0x00 - -#define SPI_COMMAND_PRINTER_WRITE 0x01 -#define SPI_COMMAND_PRINTER_RESET 0x02 -#define SPI_COMMAND_PRINTER_SETMODE 0x03 -#define SPI_COMMAND_PRINTER_MANUAL 0x04 -#define SPI_COMMAND_PRINTER_PAUSE 0x05 -#define SPI_COMMAND_PRINTER_RESUME 0x06 -#define SPI_COMMAND_PRINTER_CLEARBUF 0x07 - -#define SPI_COMMAND_KEYBOARD_GETKEY 0x20 -#define SPI_COMMAND_KEYBOARD_CLEARBUF 0x21 - -#define SPI_COMMAND_SYSTEM_BACKLIGHT 0x40 - #define UART_BYTE_READY (UCSRA & (1 << RXC)) - -inline void spi_process_data() +ISR(USART_RXC_vect) { - //print_buffer[print_buffer_wr++] = SPDR; // WARNING: No overflow protection - // Will auto wrap at 256; print_buffer_wr = print_buffer_wr % PRINT_BUFFER_SIZE; + 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; + } } -/*ISR(SPI_STC_vect) -{ - -}*/ - #define PRINT_CHARACTER_WIDTH 10 #define PRINT_LINE_HEIGHT 34 @@ -969,7 +855,7 @@ void printer_process() } backward = 0; // Re-evaluate backward. Default is forward. - stepper_status_LINEFEED.target_pos -= PRINT_LINE_HEIGHT; + stepper_status_LINEFEED.target_pos += PRINT_LINE_HEIGHT; uart_putc('\n'); current_is_linebreak--; //_delay_ms(1000); @@ -1057,7 +943,36 @@ void printer_process() stepper_perform_movement(&stepper_status_WHEEL, &STEPPER_CFG_WHEEL); } -int system_test_auto() +/* Test Functions */ +void systick_test() +{ + debug_write("[tmr] System timer test\r\n"); + while (1) { + if (block_for(TIM_SYSTEM, 1000 * TIMESCALE)) { + debug_write("tick.\r\n"); + } + if (UCSRA & (1 << RXC)) { + break; + } + } +} + +static void system_test_printer() +{ + debug_write("[sys] Entering printer test mode\r\n"); + initialize_paperfeed(); + move_carriage_to_far_left(1); + align_daisy_wheel(); + reset_printhead(); + + uart_irq_enable(); + + while (1) { + printer_process(); + } +} + +static void system_test_auto() { char c; int do_it = 0; @@ -1186,6 +1101,13 @@ int system_test_auto() default: break; } + + /* Launch programs */ + switch (c) { + case 'P': + system_test_printer(); + break; + } } } @@ -1235,7 +1157,9 @@ int main() PORTD = 0; /* Set up UART */ + UCSRA = 0; UCSRB = (1 << TXEN) | (1 << RXEN); + UCSRC = 0; UBRRH = ((((F_CPU / BAUDRATE) / 16) - 1) >> 8); UBRRL = (((F_CPU / BAUDRATE) / 16) - 1); @@ -1262,7 +1186,7 @@ int main() sei(); /* Align printer */ - initialize_paperfeed(); + //initialize_paperfeed(); //move_carriage_to_far_left(1); //align_daisy_wheel(); //reset_printhead();