X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;ds=sidebyside;f=ROM2%2Fmotors.c;h=efed7cc73a85255be7cdacc015db394cc2b4c104;hb=f5bc521c83d95d35e852747402a44d1411252c2b;hp=a114490c8d9038bacc47bf2cfacd56178dd1f815;hpb=080c85d87da93ba2472b0c90d17527838d668db2;p=uccvend-snackrom.git diff --git a/ROM2/motors.c b/ROM2/motors.c index a114490..efed7cc 100644 --- a/ROM2/motors.c +++ b/ROM2/motors.c @@ -36,7 +36,7 @@ void motor_shift_send(u8 data) { } } -void motor_on(u8 slot) { +void set_motor(u8 slot) { u8 row, col; row = slot%10; col = slot/10; @@ -55,37 +55,44 @@ void motor_on(u8 slot) { break; default: /* < 8 */ motor_shift_send(1 << col); /* cols from 0..7 */ + bclr((void*)&_io_ports[M6811_PORTA], PORTA_MOTOR_COL_DISABLE); } motor_shift_send(1 << (row-1)); /* rows from 1..8 here */ bclr((void*)&_io_ports[M6811_PORTA], PORTA_MOTOR_CLOCK); +} + +void motor_start() { bclr_changer_output(A3000_MOTOR_ROW_DISABLE); - bclr((void*)&_io_ports[M6811_PORTA], PORTA_MOTOR_COL_DISABLE); } -void motors_off() { +void motor_pause() { + bset_changer_output(A3000_MOTOR_ROW_DISABLE); +} + +void motor_stop() { bset_changer_output(A3000_MOTOR_ROW_DISABLE); - delay(10); /* XXX cf motors_off */ bset((void*)&_io_ports[M6811_PORTA], PORTA_MOTOR_COL_DISABLE); bclr_misc_output(A3800_MOTOR_COL8_ENABLE | A3800_MOTOR_COL9_ENABLE); } bool motor_here(u8 slot) { u8 i, c = 0; - motor_on(slot); + set_motor(slot); for (i=0; i < 8; i++) { + motor_start(); delay(5); - if (_io_ports[M6811_PORTE] & PORTE_MOTOR_OVERVOLTAGE) { + if ((_io_ports[M6811_PORTE] & PORTE_MOTOR_OVERVOLTAGE) == 0) { c++; - if (c == 0xff) { - motors_off(); + if (c == 3) { + motor_stop(); return 1; - } else - continue; + } } + motor_pause(); } - motors_off(); + motor_stop(); return 0; } @@ -94,65 +101,62 @@ bool is_motor(u8 slot) { return motor_here(slot); } -bool left_home(u8 slot) { - u8 i, r = slot%10; +bool start_motor(u8 slot) { + u16 i; + u8 r = slot%10; if (r >= 5) r--; - r = 1 << r; - - for (i = 0; i < 5; i++) - if ((home_sensors & r) == 0) return 1; - + r = 1 << (r-1); + set_motor(slot); + motor_start(); + delay(50); + for (i = 0; i < 1000; i++) { + if ((home_sensors & r) != 0) return 1; + delay(1); + } /* it never left */ + motor_stop(); return 0; } bool back_home(u8 slot) { u8 i, r = slot%10; if (r >= 5) r--; - r = 1 << r; + r = 1 << (r-1); for (i = 0; i < 5; i++) { - if (home_sensors & r) return 1; - if ((_io_ports[M6811_PORTE] & PORTE_MOTOR_OVERCURRENT) == 0) return 1; + if ((home_sensors & r) == 0) return 1; + delay(1); } - /* it never left */ + /* it never arrived */ return 0; } +#define is_overcurrent() ((_io_ports[M6811_PORTE] & PORTE_MOTOR_NOT_OVERCURRENT)==0) bool motor_overcurrent() { - u8 t = 0, i = 0; - while(1) { - t++; - if (7 == t) return 1; - if (_io_ports[M6811_PORTE] & PORTE_MOTOR_OVERCURRENT) continue; + u8 good_passes = 0, t; + for (t = 0; t < 8; t++) { + delay(1); + if (is_overcurrent()) continue; t = 0; - i++; - if (5 == i) return 0; + good_passes++; + if (good_passes == 5) return 0; } + return 1; } u8 dispense_motor(u8 slot) { - //if (!is_motor(slot)) return MOTOR_NOSLOT; - - motor_on(slot); - delay(1000); - motors_off(); - return MOTOR_SUCCESS; - - if (!left_home(slot)) { - motors_off(); - return MOTOR_HOME_FAIL; - } + if (!is_motor(slot)) return MOTOR_NOSLOT; + if (!start_motor(slot)) return MOTOR_HOME_FAIL; + delay(100); while (1) { if (motor_overcurrent()) { - motors_off(); + motor_stop(); return MOTOR_CURRENT_FAIL; } - /* something should call motor_here? */ if (back_home(slot)) { - motors_off(); + motor_stop(); return MOTOR_SUCCESS; } }