From: Bernard Blackham Date: Mon, 21 Jun 2004 14:07:29 +0000 (+0000) Subject: To romO X-Git-Tag: ROMW~34 X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=c34cbe163ce52f1d8fc553993701054f4942152f;p=uccvend-snackrom.git To romO --- diff --git a/ROM2/Makefile b/ROM2/Makefile index 5ebabe2..17694c0 100644 --- a/ROM2/Makefile +++ b/ROM2/Makefile @@ -2,7 +2,7 @@ OBJS = \ motors.o keypad.o display_basic.o coinmech.o chime.o \ - helpers.o main_basic.o comm.o \ + helpers.o main_basic.o sci.o \ vectors.o start.o INCLUDES = vend.h keypad.h chime.h asm.h display_basic.h ports.h types.h diff --git a/ROM2/coinmech.c b/ROM2/coinmech.c index d3b0992..124c835 100644 --- a/ROM2/coinmech.c +++ b/ROM2/coinmech.c @@ -51,7 +51,7 @@ void send_byte(u8 c) { } #define IS_CTRL(x) (x & 0x10) /* true if this packet is a control packet */ -void sci_interrupt() { +void sci_interrupt_coinmech() { u8 in; in = _io_ports[M6811_SCDR]; diff --git a/ROM2/main_basic.c b/ROM2/main_basic.c index ae25a79..b2923bc 100644 --- a/ROM2/main_basic.c +++ b/ROM2/main_basic.c @@ -9,36 +9,36 @@ #include "chime.h" #include "coinmech.h" #include "motors.h" -#include "comm.h" +#include "sci.h" #include "vend.h" void motor_reply(char* slotptr, u8 code) { /* returns a message of the form MXYY - X is return code, YY is motor */ wait_for_tx_free(); - tx_buffer[0] = 'M'; - tx_buffer[1] = code + '0'; - tx_buffer[2] = *slotptr; - tx_buffer[3] = *(slotptr+1); - tx_buffer[4] = '\n'; - tx_buffer[5] = 0; + sci_tx_buf[0] = 'M'; + sci_tx_buf[1] = code + '0'; + sci_tx_buf[2] = *slotptr; + sci_tx_buf[3] = *(slotptr+1); + sci_tx_buf[4] = '\n'; + sci_tx_buf[5] = 0; send_packet(); } void dispense_something() { - /* process a message VXX in msg_buf where XX is motor number */ + /* process a message VXX in sci_rx_buf where XX is motor number */ u8 slot; - if ((msg_buf[1] < '0') || (msg_buf[1] > '9') || - (msg_buf[2] < '0') || (msg_buf[2] > '9')) { - msg_buf[1] = msg_buf[2] = '0'; - motor_reply((char*)&msg_buf[1], MOTOR_NOSLOT); + if ((sci_rx_buf[1] < '0') || (sci_rx_buf[1] > '9') || + (sci_rx_buf[2] < '0') || (sci_rx_buf[2] > '9')) { + sci_rx_buf[1] = sci_rx_buf[2] = '0'; + motor_reply((char*)&sci_rx_buf[1], MOTOR_NOSLOT); return; } - slot = (msg_buf[1] - '0') * 10; - slot += msg_buf[2] - '0'; + slot = (sci_rx_buf[1] - '0') * 10; + slot += sci_rx_buf[2] - '0'; - motor_reply((char*)&msg_buf[1], dispense_motor(slot)); + motor_reply((char*)&sci_rx_buf[1], dispense_motor(slot)); } void write_to_display() { @@ -46,8 +46,8 @@ void write_to_display() { u8 i; char buf[10]; for (i = 0; i < 10; i++) - if (msg_buf[i+1]) - buf[i] = msg_buf[i+1]; + if (sci_rx_buf[i+1]) + buf[i] = sci_rx_buf[i+1]; else break; @@ -60,33 +60,33 @@ void write_to_display() { void send_balance() { wait_for_tx_free(); - tx_buffer[0] = 'C'; - tx_buffer[1] = have_change?'0':'1'; - tx_buffer[2] = (coin_value/10000)%10; - tx_buffer[3] = (coin_value/1000)%10; - tx_buffer[4] = (coin_value/100)%10; - tx_buffer[5] = (coin_value/10)%10; - tx_buffer[6] = coin_value%10; - tx_buffer[7] = '\n'; - tx_buffer[8] = 0; + sci_tx_buf[0] = 'C'; + sci_tx_buf[1] = have_change?'0':'1'; + sci_tx_buf[2] = (coin_value/10000)%10; + sci_tx_buf[3] = (coin_value/1000)%10; + sci_tx_buf[4] = (coin_value/100)%10; + sci_tx_buf[5] = (coin_value/10)%10; + sci_tx_buf[6] = coin_value%10; + sci_tx_buf[7] = '\n'; + sci_tx_buf[8] = 0; send_packet(); } void give_change() { u16 cost; - if ((msg_buf[1] < '0') || (msg_buf[1] > '9') || - (msg_buf[2] < '0') || (msg_buf[2] > '9') || - (msg_buf[3] < '0') || (msg_buf[3] > '9') || - (msg_buf[4] < '0') || (msg_buf[4] > '9') || - (msg_buf[5] < '0') || (msg_buf[5] > '9')) { + if ((sci_rx_buf[1] < '0') || (sci_rx_buf[1] > '9') || + (sci_rx_buf[2] < '0') || (sci_rx_buf[2] > '9') || + (sci_rx_buf[3] < '0') || (sci_rx_buf[3] > '9') || + (sci_rx_buf[4] < '0') || (sci_rx_buf[4] > '9') || + (sci_rx_buf[5] < '0') || (sci_rx_buf[5] > '9')) { send_nack(); } - cost = msg_buf[1] - '0'; - cost *= 10; cost = msg_buf[2] - '0'; - cost *= 10; cost = msg_buf[3] - '0'; - cost *= 10; cost = msg_buf[4] - '0'; - cost *= 10; cost = msg_buf[5] - '0'; + cost = sci_rx_buf[1] - '0'; + cost *= 10; cost = sci_rx_buf[2] - '0'; + cost *= 10; cost = sci_rx_buf[3] - '0'; + cost *= 10; cost = sci_rx_buf[4] - '0'; + cost *= 10; cost = sci_rx_buf[5] - '0'; coin_cost(cost); send_ack(); @@ -95,22 +95,22 @@ void give_change() { void send_keypress(u8 key) { /* send a packet of the form KX with X being the key, or R for reset */ wait_for_tx_free(); - tx_buffer[0] = 'K'; + sci_tx_buf[0] = 'K'; if (key == KEY_RESET) - tx_buffer[1] = 'R'; + sci_tx_buf[1] = 'R'; else - tx_buffer[1] = (key%10)+'0'; - tx_buffer[2] = '\n'; - tx_buffer[3] = 0; + sci_tx_buf[1] = (key%10)+'0'; + sci_tx_buf[2] = '\n'; + sci_tx_buf[3] = 0; send_packet(); } void send_door_msg(bool open) { wait_for_tx_free(); - tx_buffer[0] = 'D'; - tx_buffer[1] = open?'1':'0'; - tx_buffer[2] = '\n'; - tx_buffer[3] = 0; + sci_tx_buf[0] = 'D'; + sci_tx_buf[1] = open?'1':'0'; + sci_tx_buf[2] = '\n'; + sci_tx_buf[3] = 0; send_packet(); } @@ -121,20 +121,20 @@ void do_chime() { void ping_pong() { /* make sure it's really a ping */ - if (msg_buf[1] != 'I' || - msg_buf[2] != 'N' || - msg_buf[3] != 'G') { + if (sci_rx_buf[1] != 'I' || + sci_rx_buf[2] != 'N' || + sci_rx_buf[3] != 'G') { send_nack(); return; } /* respond with ack & pong */ wait_for_tx_free(); - tx_buffer[0] = 'P'; - tx_buffer[1] = 'O'; - tx_buffer[2] = 'N'; - tx_buffer[3] = 'G'; - tx_buffer[4] = '\n'; - tx_buffer[5] = 0; + sci_tx_buf[0] = 'P'; + sci_tx_buf[1] = 'O'; + sci_tx_buf[2] = 'N'; + sci_tx_buf[3] = 'G'; + sci_tx_buf[4] = '\n'; + sci_tx_buf[5] = 0; send_packet(); } @@ -163,8 +163,9 @@ int main() { unlock(); /* enable interrupts */ - comm_init(); - coinmech_init(); + //comm_init(); + //coinmech_init(); + sci_init(); keypad_init(); last_coin_value = 0; last_door_open = 0; @@ -198,9 +199,8 @@ int main() { } } - /* - if (rx_queue_state) { - switch (msg_buf[0]) { + if (sci_have_packet) { + switch (sci_rx_buf[0]) { case 'V': dispense_something(); break; @@ -226,7 +226,6 @@ int main() { } msg_clr(); } - */ keypad_read(); if (keypad_pressed()) { @@ -252,6 +251,9 @@ int main() { case MOTOR_SUCCESS: set_msg("THANK YOU"); break; + case MOTOR_NOSLOT: + set_msg(" NO MOTOR "); + break; default: set_msg("ERRRRRRRR?"); break; diff --git a/ROM2/motors.c b/ROM2/motors.c index 64158ae..13316ff 100644 --- a/ROM2/motors.c +++ b/ROM2/motors.c @@ -73,19 +73,18 @@ void motors_off() { bool motor_here(u8 slot) { u8 i, c = 0; - motor_on(slot); for (i=0; i < 8; i++) { + motor_on(slot); delay(5); - if (_io_ports[M6811_PORTE] & PORTE_MOTOR_OVERVOLTAGE) { + if ((_io_ports[M6811_PORTE] & PORTE_MOTOR_OVERVOLTAGE) == 0) { c++; - if (c == 0xff) { + if (c == 3) { motors_off(); return 1; - } else - continue; + } } + motors_off(); } - motors_off(); return 0; } @@ -95,49 +94,51 @@ bool is_motor(u8 slot) { } bool left_home(u8 slot) { - u8 i, r = slot%10; + u16 i; + u8 r = slot%10; if (r >= 5) r--; r = 1 << (r-1); - - for (i = 0; i < 5; i++) + for (i = 0; i < 1000; i++) { if ((home_sensors & r) == 0) return 1; - + delay(1); + } /* it never left */ return 0; } +#define is_overcurrent() ((_io_ports[M6811_PORTE] & PORTE_MOTOR_NOT_OVERCURRENT)==0) + bool back_home(u8 slot) { u8 i, r = slot%10; if (r >= 5) 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; + if (is_overcurrent()) return MOTOR_CURRENT_FAIL; } - /* it never left */ + /* it never arrived */ return 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; + if (!is_motor(slot)) return MOTOR_NOSLOT; motor_on(slot); - delay(100); - + if (!left_home(slot)) { motors_off(); return MOTOR_HOME_FAIL; diff --git a/ROM2/sci.c b/ROM2/sci.c index 39006bc..3241f23 100644 --- a/ROM2/sci.c +++ b/ROM2/sci.c @@ -1,11 +1,73 @@ #include "vend.h" #include "sci.h" +char sci_tx_buf[BUFFER_LEN]; +volatile char sci_rx_buf[BUFFER_LEN]; +volatile bool sci_have_packet; +volatile u8 sci_rx_buf_ptr; + void sci_init() { - _io_ports[M6811_BAUD] = M6811_DEF_BAUD; + /* assumes clock of 4.91Mhz */ + _io_ports[M6811_BAUD] = 0x03; /* 9600 baud */ + /* Setup character format 1 start, 8-bits, 1 stop. */ _io_ports[M6811_SCCR1] = 0; - /* Enable reciever and transmitter. */ - _io_ports[M6811_SCCR2] = 0xc; + /* Enable reciever and transmitter & rx interrupt. */ + _io_ports[M6811_SCCR2] = 0x2c; + + sci_have_packet = 0; + sci_rx_buf_ptr = 0; +} + +void send_packet() { + char* c; + for (c = sci_tx_buf; *c; c++) { + /* wait for TX ready */ + while (!(_io_ports[M6811_SCSR] & M6811_TDRE)); + + /* send byte */ + _io_ports[M6811_SCDR] = *c; + } +} + +void rx_int() { + if (sci_have_packet) return; + if (sci_rx_buf_ptr >= BUFFER_LEN) { + /* overrun :( */ + sci_rx_buf[BUFFER_LEN] = '\0'; + sci_have_packet = 1; + sci_rx_buf_ptr = 0; + return; + } + sci_rx_buf[sci_rx_buf_ptr] = _io_ports[M6811_SCDR]; + if (sci_rx_buf[sci_rx_buf_ptr] == '\n') { + sci_rx_buf[sci_rx_buf_ptr] = '\0'; + sci_have_packet = 1; + sci_rx_buf_ptr = 0; + } + sci_rx_buf_ptr++; +} + +void msg_clr() { + sci_have_packet = 0; + sci_rx_buf_ptr = 0; +} + +void sci_interrupt_serial() { + if (_io_ports[M6811_SCSR] & M6811_RDRF) rx_int(); +} + +void send_ack() { + sci_tx_buf[0] = '!'; + sci_tx_buf[1] = '\n'; + sci_tx_buf[2] = '\0'; + send_packet(); +} + +void send_nack() { + sci_tx_buf[0] = '?'; + sci_tx_buf[1] = '\n'; + sci_tx_buf[2] = '\0'; + send_packet(); } diff --git a/ROM2/sci.h b/ROM2/sci.h index 80ac024..4b3a6d1 100644 --- a/ROM2/sci.h +++ b/ROM2/sci.h @@ -3,4 +3,17 @@ #include "vend.h" +#define BUFFER_LEN 12 + +void sci_init(); +void msg_clr(); +void send_packet(); +void send_ack(); +void send_nack(); +#define wait_for_tx_free() do { } while(0) + +extern char sci_tx_buf[BUFFER_LEN]; +extern volatile char sci_rx_buf[BUFFER_LEN]; +extern volatile u8 sci_have_packet; + #endif /* _SCI_H_ */ diff --git a/ROM2/vectors.s b/ROM2/vectors.s index 14612df..3ffb6a6 100644 --- a/ROM2/vectors.s +++ b/ROM2/vectors.s @@ -27,8 +27,9 @@ Boston, MA 02111-1307, USA. */ .sect .text .globl _start - .globl sci_interrupt - .globl uart_interrupt + .globl sci_interrupt_serial + .globl sci_interrupt_coinmech + ;.globl uart_interrupt ;; Default interrupt handler. .sect .text @@ -67,7 +68,7 @@ vectors: .word def ; ffd4 ;; SCI - .word sci_interrupt ; ffd6 + .word sci_interrupt_serial ; ffd6 ;; SPI .word def ; ffd8 @@ -90,7 +91,8 @@ vectors: ;; Misc ;.word def ; fff0 (RTII) ; uncomment to disable the RTI & comment below .word rti ; fff0 (RTII) - .word uart_interrupt ; fff2 (IRQ) + ;.word uart_interrupt ; fff2 (IRQ) + .word def ; fff2 (IRQ) .word def ; fff4 (XIRQ) .word def ; fff6 (SWI) .word def ; fff8 (ILL) diff --git a/ROM2/vend.h b/ROM2/vend.h index 03d8c9e..17ba674 100644 --- a/ROM2/vend.h +++ b/ROM2/vend.h @@ -41,7 +41,7 @@ void my_memcpy(char* dst, char* src, u8 size); #define PORTD_KEYPAD_ROW 0x20 /* clear for row 0, set for row 1 */ -#define PORTE_MOTOR_OVERCURRENT 0x01 +#define PORTE_MOTOR_NOT_OVERCURRENT 0x01 #define PORTE_MOTOR_OVERVOLTAGE 0x02 /* Address 1800 bits */ @@ -60,7 +60,8 @@ void my_memcpy(char* dst, char* src, u8 size); /******* from main.c *******/ int __attribute__((noreturn)) main (void); void __attribute__((interrupt)) rti (void); -void __attribute__((interrupt)) sci_interrupt (void); +void __attribute__((interrupt)) sci_interrupt_serial (void); +void __attribute__((interrupt)) sci_interrupt_coinmech (void); void __attribute__((interrupt)) uart_interrupt (void); /* other one liners */