printer: avr: Update to new hardware

* Update I/O constraints
* Remove SPI code (some left for future reference)
* Launch test code instead of printer program
This commit is contained in:
Markus Koch 2019-11-10 18:11:09 +01:00
parent df2ec0cb17
commit 35268c53ae
2 changed files with 194 additions and 179 deletions

View File

@ -1,5 +1,6 @@
MCU=atmega8 MCU=atmega8
CFLAGS=-g -Wall -mcall-prologues -mmcu=$(MCU) -O2 -DF_CPU=8000000 PDEV=/dev/ttyUSB0
CFLAGS=-g -Wall -mcall-prologues -mmcu=$(MCU) -O2 -DF_CPU=16000000
LDFLAGS=-Wl,-gc-sections -Wl,-relax LDFLAGS=-Wl,-gc-sections -Wl,-relax
CC=avr-gcc CC=avr-gcc
TARGET=main TARGET=main
@ -18,8 +19,7 @@ clean:
avr-size -C --mcu=atmega8 main.obj avr-size -C --mcu=atmega8 main.obj
program: $(TARGET).obj program: $(TARGET).obj
avrdude -p $(MCU) -c usbasp -U flash:w:$(TARGET).hex avrdude -p $(MCU) -P $(PDEV) -c arduino -b 19200 -U flash:w:$(TARGET).hex
#avrdude -p $(MCU) -P /dev/ttyUSB1 -c arduino -b 57600 -U flash:w:$(TARGET).hex
fuse: fuse:
avrdude -p m8 -c usbasp -U lfuse:w:0xe4:m -U hfuse:w:0xd9:m avrdude -p m8 -c usbasp -U lfuse:w:0xe4:m -U hfuse:w:0xd9:m

View File

@ -5,7 +5,6 @@
#include <avr/pgmspace.h> #include <avr/pgmspace.h>
#define BAUDRATE 9600 #define BAUDRATE 9600
#define UBRR 51 // (F_CPU / BAUDRATE) / 32
inline void spi_process_data(); inline void spi_process_data();
@ -17,7 +16,7 @@ enum TIMERS{TIM_CARRIAGE,
TIMS}; TIMS};
#define TIMESCALE 4 #define TIMESCALE 4
#define sleep_ms(channel, time_ms) sleep(channel, time_ms / TIMESCALE) #define sleep_ms(channel, time_ms) sleep(channel, time_ms * TIMESCALE)
volatile uint16_t timer[TIMS]; volatile uint16_t timer[TIMS];
#define set_timer(channel, time_systicks) {timer[channel] = time_systicks;} #define set_timer(channel, time_systicks) {timer[channel] = time_systicks;}
@ -30,8 +29,6 @@ uint8_t block_for(uint8_t channel, uint16_t time_systicks)
return 0; return 0;
} }
uint8_t system_status; // CAUTION: Must not be updated from interrupts!
ISR (TIMER1_COMPA_vect) ISR (TIMER1_COMPA_vect)
{ {
uint8_t i; uint8_t i;
@ -40,8 +37,6 @@ ISR (TIMER1_COMPA_vect)
timer[i]--; timer[i]--;
} }
} }
if (SPSR & (1<<SPIF)) // We need to check and jump here, otherwise we'll be too slow.
spi_process_data();
} }
/* Look-up-tables */ /* Look-up-tables */
@ -216,6 +211,10 @@ void uart_putc(char c)
#define ENABLE_SERIAL_DEBUG #define ENABLE_SERIAL_DEBUG
#ifdef ENABLE_SERIAL_DEBUG #ifdef ENABLE_SERIAL_DEBUG
void debug_putc(char c) {
uart_putc(c);
};
void debug_write(char *c) void debug_write(char *c)
{ {
while (*c) { while (*c) {
@ -241,6 +240,7 @@ unsigned int debug_printf(char *format,...)
} }
#else #else
void debug_write(char *c) {}; void debug_write(char *c) {};
void debug_putc(char c) {};
unsigned int debug_printf(char *format,...) {return 0;}; unsigned int debug_printf(char *format,...) {return 0;};
#endif #endif
@ -249,9 +249,9 @@ unsigned int debug_printf(char *format,...) {return 0;};
#define PIN_DCMOTOR (1 << 4) #define PIN_DCMOTOR (1 << 4)
#define PIN_LIMITSWITCH (1 << 5) #define PIN_LIMITSWITCH (1 << 5)
#define SOLENOID(t) t##D #define SOLENOID(t) t##B
#define PIN_CORRECTION (1 << 2) #define PIN_CORRECTION (1 << 1)
#define PIN_ARMHAMMER (1 << 3) #define PIN_ARMHAMMER (1 << 0)
/* Stepper drive */ /* Stepper drive */
@ -286,39 +286,39 @@ struct stepper_status {
/* Stepper configs */ /* Stepper configs */
#define REVERSAL_MULTIPLIER 32 #define REVERSAL_MULTIPLIER 32
#define STEPPER_CFG_HW_CARRIAGE(t) t##C #define STEPPER_CFG_HW_CARRIAGE(t) t##B
const struct stepper_config STEPPER_CFG_CARRIAGE = { const struct stepper_config STEPPER_CFG_CARRIAGE = {
.port = 'C', .port = 'B',
.timch = TIM_CARRIAGE, .timch = TIM_CARRIAGE,
.pin_a = (1 << 3), .pin_a = (1 << 5),
.pin_b = (1 << 1), .pin_b = (1 << 4),
.pin_c = (1 << 0), .pin_c = (1 << 3),
.pin_d = (1 << 2), .pin_d = (1 << 2),
.delay = 3.5 * TIMESCALE, .delay = 3.5 * TIMESCALE,
.wraparound = 0 /* No wraparonud */ .wraparound = 0 /* No wraparound */
}; };
#define STEPPER_CFG_HW_WHEEL(t) t##B #define STEPPER_CFG_HW_WHEEL(t) t##D
const struct stepper_config STEPPER_CFG_WHEEL = { const struct stepper_config STEPPER_CFG_WHEEL = {
.port = 'B', .port = 'D',
.timch = TIM_WHEEL, .timch = TIM_WHEEL,
.pin_a = (1 << 7), .pin_a = (1 << 4),
.pin_b = (1 << 0), .pin_b = (1 << 5),
.pin_c = (1 << 1), .pin_c = (1 << 6),
.pin_d = (1 << 6), .pin_d = (1 << 7),
.delay = 3.5 * TIMESCALE, .delay = 3.5 * TIMESCALE,
.wraparound = 96 * 2 .wraparound = 96 * 2
}; };
#define STEPPER_CFG_HW_LINEFEED(t) t##D #define STEPPER_CFG_HW_LINEFEED(t) t##C
const struct stepper_config STEPPER_CFG_LINEFEED= { const struct stepper_config STEPPER_CFG_LINEFEED= {
.port = 'D', .port = 'C',
.timch = TIM_LINEFEED, .timch = TIM_LINEFEED,
.pin_a = (1 << 4), .pin_a = (1 << 2),
.pin_b = (1 << 7), .pin_b = (1 << 3),
.pin_c = (1 << 6), .pin_c = (1 << 0),
.pin_d = (1 << 5), .pin_d = (1 << 1),
.delay = 6 * TIMESCALE, .delay = 3.5 * TIMESCALE,
.wraparound = 0 /* No wraparound */ .wraparound = 0 /* No wraparound */
}; };
@ -537,6 +537,12 @@ void arm_hammer()
_delay_ms(30); _delay_ms(30);
SOLENOID(PORT) &= ~(PIN_ARMHAMMER); SOLENOID(PORT) &= ~(PIN_ARMHAMMER);
} }
void arm_correction()
{
SOLENOID(PORT) |= PIN_CORRECTION;
_delay_ms(30);
SOLENOID(PORT) &= ~(PIN_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
@ -608,7 +614,7 @@ void align_daisy_wheel()
} }
/* Rotate left for one revolution -> align daisy wheel assembly */ /* Rotate left for one revolution -> align daisy wheel assembly */
for (i = 0; i < (96 + 1) * 2; ) { for (i = 0; i < (96 + 1 + 6) * 2; ) {
if (block_for(TIM_WHEEL, STEPPER_CFG_WHEEL.delay)) { if (block_for(TIM_WHEEL, STEPPER_CFG_WHEEL.delay)) {
STEPPER_NEXT(WHEEL, -1); STEPPER_NEXT(WHEEL, -1);
i++; i++;
@ -618,7 +624,7 @@ void align_daisy_wheel()
STEPPER_STOP(WHEEL); STEPPER_STOP(WHEEL);
_delay_ms(STEPPER_CFG_WHEEL.delay * REVERSAL_MULTIPLIER); _delay_ms(STEPPER_CFG_WHEEL.delay * REVERSAL_MULTIPLIER);
for (i = 0; i < 4;) { for (i = 0; i < 3;) {
if (block_for(TIM_WHEEL, STEPPER_CFG_WHEEL.delay)) { if (block_for(TIM_WHEEL, STEPPER_CFG_WHEEL.delay)) {
STEPPER_NEXT(WHEEL, 1); STEPPER_NEXT(WHEEL, 1);
i++; i++;
@ -628,7 +634,7 @@ void align_daisy_wheel()
stepper_status_WHEEL.ldir = 1; stepper_status_WHEEL.ldir = 1;
stepper_status_WHEEL.target_pos = 0; stepper_status_WHEEL.target_pos = 0;
stepper_status_WHEEL.pos = 4; stepper_status_WHEEL.pos = 0;
debug_write("[whl] Alignment completed.\r\n"); debug_write("[whl] Alignment completed.\r\n");
} }
@ -666,90 +672,6 @@ void initialize_paperfeed()
debug_write("[lfd] Initialization completed.\r\n"); debug_write("[lfd] Initialization completed.\r\n");
} }
//int system_test_auto()
//{
// char c;
// int do_it = 0;
// int print_stat = 0;
// debug_write("[sys] Entering system test mode\r\n");
// debug_write(">");
// while(1) {
// if (UCSRA & (1 << RXC)) {
// print_stat = 1;
// c = UDR;
// if (c >= '0' && c <= '9') {
// stepper_status_CARRIAGE.target_pos = 100 * (c - '0');
// } else {
// switch (c) {
// case ' ':
// stepper_status_CARRIAGE.target_pos += 10;
// break;
// case 'z':
// if (stepper_status_CARRIAGE.target_pos >= 10)
// stepper_status_CARRIAGE.target_pos -= 10;
// break;
// case 'r':
// move_carriage_to_far_left(0);
// break;
// case 'h':
// DCMOTOR_EN;
// arm_hammer();
// _delay_ms(100); /* Note, this also locks the carriage movement -> important! */
// DCMOTOR_STOP;
// break;
// default:
// break;
// }
// switch (c) {
// case '\'':
// //if (stepper_status_WHEEL.target_pos >= 2)
// SET_TARGET_DELTA(WHEEL, -2);
// debug_printf("[whl] New wheel: %d\r\n", stepper_status_WHEEL.target_pos);
// break;
// case ',':
// SET_TARGET_DELTA(WHEEL, 2);
// debug_printf("[whl] New wheel: %d\r\n", stepper_status_WHEEL.target_pos);
// break;
// case 'w':
// align_daisy_wheel();
// break;
// case 't':
// stepper_status_WHEEL.target_pos += 2;
// stepper_status_CARRIAGE.target_pos += 10;
// do_it = 1;
// break;
// default:
// break;
// }
// }
// }
// stepper_perform_movement(&stepper_status_CARRIAGE, &STEPPER_CFG_CARRIAGE);
// stepper_perform_movement(&stepper_status_WHEEL, &STEPPER_CFG_WHEEL);
// if (POSITION_REACHED(WHEEL) && POSITION_REACHED(CARRIAGE) && print_stat) {
// print_stat = 0;
// debug_printf("[pos] CAR: %u\r\n[pos] WHL: %u\r\n",
// stepper_status_CARRIAGE.pos,
// stepper_status_WHEEL.pos);
// }
// if (do_it) {
// if (stepper_status_CARRIAGE.pos == stepper_status_CARRIAGE.target_pos) {
// do_it = 0;
// DCMOTOR_EN;
// arm_hammer();
// _delay_ms(100);
// DCMOTOR_STOP;
// }
// }
// }
//}
//void printer_test() //void printer_test()
//{ //{
// uint8_t buf[80] = {0}; // uint8_t buf[80] = {0};
@ -835,8 +757,8 @@ void initialize_paperfeed()
//{ //{
// debug_write("[tmr] System timer test\r\n"); // debug_write("[tmr] System timer test\r\n");
// while (1) { // while (1) {
// if (block_for(0, 1000*4)) { // if (block_for(0, 1000 * TIMESCALE)) {
// debug_write("hello"); // debug_write("hello\n");
// } // }
// } // }
//} //}
@ -871,62 +793,20 @@ volatile uint8_t print_buffer_rd = 0;
#define SPI_COMMAND_SYSTEM_BACKLIGHT 0x40 #define SPI_COMMAND_SYSTEM_BACKLIGHT 0x40
#define UART_CMD_ACK_KEYPRESS 'A'
#define SPI_GET_CS (!(PINB & (1 << 2)))
#define UART_BYTE_READY (UCSRA & (1 << RXC)) #define UART_BYTE_READY (UCSRA & (1 << RXC))
volatile uint8_t spi_command = SPI_COMMAND_NONE;
volatile uint8_t keys_to_be_acked = 0;
/* This function needs to be fast AF! */
inline void spi_process_data() inline void spi_process_data()
{ {
switch (spi_command) { //print_buffer[print_buffer_wr++] = SPDR; // WARNING: No overflow protection
case SPI_COMMAND_KEYBOARD_GETKEY: /* Needs to be first, quick reply expected. */ // Will auto wrap at 256; print_buffer_wr = print_buffer_wr % PRINT_BUFFER_SIZE;
if (UART_BYTE_READY) {
SPDR = UDR;
keys_to_be_acked++;
} else {
SPDR = 0x00;
}
break;
/* !!! Only no-reply commands below !!! */
case SPI_COMMAND_NONE: /* This is the start of the transaction */
spi_command = SPDR;
break;
case SPI_COMMAND_PRINTER_WRITE:
print_buffer[print_buffer_wr++] = SPDR; // WARNING: No overflow protection
// Will auto wrap at 256; print_buffer_wr = print_buffer_wr % PRINT_BUFFER_SIZE;
break;
default:
//SPDR = 0x0;
break;
}
} }
ISR(SPI_STC_vect) /*ISR(SPI_STC_vect)
{ {
spi_process_data();
}
void ioc_process() }*/
{
if (!SPI_GET_CS) {
/* Cancel the current command */
spi_command = SPI_COMMAND_NONE;
SPDR = system_status; // TODO: status 1
}
if (keys_to_be_acked && (UCSRA & (1 << UDRE))) {
UDR = UART_CMD_ACK_KEYPRESS;
keys_to_be_acked--;
}
}
#define PRINT_CHARACTER_WIDTH 10 #define PRINT_CHARACTER_WIDTH 10
#define PRINT_LINE_HEIGHT 34 #define PRINT_LINE_HEIGHT 34
@ -1167,16 +1047,155 @@ void printer_process()
stepper_perform_movement(&stepper_status_WHEEL, &STEPPER_CFG_WHEEL); stepper_perform_movement(&stepper_status_WHEEL, &STEPPER_CFG_WHEEL);
} }
int system_test_auto()
{
char c;
int do_it = 0;
int print_stat = 0;
static int goto_wheel = 0;
debug_write("[sys] Entering system test mode\r\n");
debug_write(">");
while(1) {
if (UCSRA & (1 << RXC)) {
print_stat = 1;
c = UDR;
if (goto_wheel) {
goto_wheel = 0;
SET_TARGET(WHEEL, (GET_PRINT_CODE(c)) * 2);
} else if (c >= '0' && c <= '9') {
stepper_status_CARRIAGE.target_pos = 100 * (c - '0');
} else {
/* Linefeed control */
switch (c) {
case '=':
stepper_status_LINEFEED.target_pos += 10;
break;
case '\\':
if (stepper_status_LINEFEED.target_pos >= 10)
stepper_status_LINEFEED.target_pos -= 10;
break;
default:
break;
}
/* Carriage control */
// Note: Characters 0 - 9 for carriage handled in if above
switch (c) {
case ' ':
stepper_status_CARRIAGE.target_pos += 10;
break;
case 'z':
if (stepper_status_CARRIAGE.target_pos >= 10)
stepper_status_CARRIAGE.target_pos -= 10;
break;
case 'r':
move_carriage_to_far_left(0);
break;
default:
break;
}
/* DC component control (Hammer / correction / motor) */
switch (c) {
case 'h':
DCMOTOR_EN;
arm_hammer();
_delay_ms(100); /* Note, this also locks the carriage movement -> important! */
DCMOTOR_STOP;
break;
case 'H':
arm_hammer();
break;
case 'c':
arm_correction();
break;
case 'm':
DCMOTOR_STOP;
break;
case 'M':
DCMOTOR_EN;
break;
default:
break;
}
/* Wheel control */
switch (c) {
case 'g':
goto_wheel = 1;
break;
case '\'':
//if (stepper_status_WHEEL.target_pos >= 2)
SET_TARGET_DELTA(WHEEL, -2);
debug_printf("[whl] New wheel: %d\r\n", stepper_status_WHEEL.target_pos);
break;
case ',':
SET_TARGET_DELTA(WHEEL, 2);
debug_printf("[whl] New wheel: %d\r\n", stepper_status_WHEEL.target_pos);
break;
case '"':
//if (stepper_status_WHEEL.target_pos >= 2)
SET_TARGET_DELTA(WHEEL, -60);
debug_printf("[whl] New wheel: %d\r\n", stepper_status_WHEEL.target_pos);
break;
case '<':
SET_TARGET_DELTA(WHEEL, 60);
debug_printf("[whl] New wheel: %d\r\n", stepper_status_WHEEL.target_pos);
break;
case 'w':
align_daisy_wheel();
break;
case 't':
stepper_status_WHEEL.target_pos += 2;
stepper_status_CARRIAGE.target_pos += 10;
do_it = 1;
break;
default:
break;
}
}
}
stepper_perform_movement(&stepper_status_CARRIAGE, &STEPPER_CFG_CARRIAGE);
stepper_perform_movement(&stepper_status_WHEEL, &STEPPER_CFG_WHEEL);
stepper_perform_movement(&stepper_status_LINEFEED, &STEPPER_CFG_LINEFEED);
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);
}
if (do_it) {
if (stepper_status_CARRIAGE.pos == stepper_status_CARRIAGE.target_pos) {
do_it = 0;
DCMOTOR_EN;
arm_hammer();
_delay_ms(100);
DCMOTOR_STOP;
}
}
}
}
void mainloop() void mainloop()
{ {
while (1) { while (1) {
printer_process(); printer_process();
ioc_process();
} }
} }
int main() int main()
{ {
/* Disable interrupts */
cli();
/* Pre-init I/O */ /* Pre-init I/O */
DDRB = 0; DDRB = 0;
DDRC = 0; DDRC = 0;
@ -1187,12 +1206,8 @@ int main()
/* Set up UART */ /* Set up UART */
UCSRB = (1 << TXEN) | (1 << RXEN); UCSRB = (1 << TXEN) | (1 << RXEN);
UBRRH = (UBRR >> 8) & 0xFF; UBRRH = ((((F_CPU / BAUDRATE) / 16) - 1) >> 8);
UBRRL = UBRR & 0xFF; UBRRL = (((F_CPU / BAUDRATE) / 16) - 1);
/* Set up SPI */
DDRB |= (1 << PB4);
SPCR = (1 << SPE) | (1 << CPOL) | (1 << CPHA) | (1 << SPIE);
/* Set up DC components */ /* Set up DC components */
MOTLIM(DDR) |= PIN_DCMOTOR; MOTLIM(DDR) |= PIN_DCMOTOR;
@ -1208,25 +1223,25 @@ int main()
/* Set up SysTick Timer */ /* Set up SysTick Timer */
TCCR1B = (1 << WGM12) | (1 << CS11); // f_tim = 8 MHz / 8 TCCR1B = (1 << WGM12) | (1 << CS11); // f_tim = 8 MHz / 8
OCR1A = 1000 / TIMESCALE; OCR1A = 2000 / TIMESCALE;
TIMSK = (1 << OCIE1A); TIMSK = (1 << OCIE1A);
/* Init system */ /* Init system */
system_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 */
mainloop(); system_test_auto();
// mainloop();
// printer_test(); // printer_test();
// system_test_auto(); // system_test_auto();
// systick_test(); // systick_test();