From: Bernard Blackham Date: Fri, 15 Aug 2003 08:59:50 +0000 (+0000) Subject: Final workings X-Git-Tag: ROMW~65 X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=293827a53b325c2faffddcba380b6f0c19da802a;p=uccvend-snackrom.git Final workings --- diff --git a/ROM2/comm.c b/ROM2/comm.c index 09ad2a1..ad0a11a 100644 --- a/ROM2/comm.c +++ b/ROM2/comm.c @@ -143,6 +143,28 @@ void uart_interrupt() { * End of interrupt handler */ +void send_ack() { + wait_for_tx_free(); + tx_buffer[0] = '!'; + tx_buffer[1] = '\n'; + tx_buffer[2] = 0; + lock(); + tx_int(); + unlock(); + send_packet(); +} + +void send_nack() { + wait_for_tx_free(); + tx_buffer[0] = '?'; + tx_buffer[1] = '\n'; + tx_buffer[2] = 0; + lock(); + tx_int(); + unlock(); + send_packet(); +} + /* sends the packet in tx_buffer and doesn't return until it's been sent */ void send_packet() { bset((void*)&tx_queue_state, 0x01); diff --git a/ROM2/comm.h b/ROM2/comm.h index 69f8d00..9bff8bd 100644 --- a/ROM2/comm.h +++ b/ROM2/comm.h @@ -23,7 +23,7 @@ * */ -#define TX_BUFFER_LEN 6 /* maximum 13 due to the way tx_int works with the FIFO */ +#define TX_BUFFER_LEN 6 /* maximum 12 due to the way tx_int works with the FIFO */ extern char tx_buffer[TX_BUFFER_LEN+2]; /* \n + null terminated */ #define RX_BUFFER_LEN 11 extern volatile char rx_buffer[RX_BUFFER_LEN+1]; /* null terminated */ @@ -42,8 +42,13 @@ extern volatile u8 rx_queue_state; */ extern volatile u8 tx_queue_state; +extern inline void wait_for_tx_free() { while (tx_queue_state & 0x01); } + void comm_init(); void msg_clr(); +void send_packet(); +void send_ack(); +void send_nack(); /*************************************/ /*** 16550 UART specific #defines ***/ diff --git a/ROM2/main_basic.c b/ROM2/main_basic.c index 21844bc..e459471 100644 --- a/ROM2/main_basic.c +++ b/ROM2/main_basic.c @@ -8,9 +8,107 @@ #include "keypad.h" #include "chime.h" #include "coinmech.h" +#include "motors.h" #include "comm.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; + send_packet(); +} + +void dispense_something() { + /* process a message VXX in msg_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); + return; + } + + slot = (msg_buf[1] - '0') * 10; + slot += msg_buf[2] - '0'; + + motor_reply((char*)&msg_buf[1], dispense_motor(slot)); +} + +void write_to_display() { + /* process a message in the form DXXXXXXXXXXX to send to display */ + u8 i; + char buf[10]; + for (i = 0; i < 10; i++) + if (msg_buf[i+1]) + buf[i] = msg_buf[i+1]; + else + break; + + for (; i < 10; i++) /* pad the rest out with spaces */ + buf[i] = ' '; + + set_msg(buf); + send_ack(); +} + +void send_balance() { + wait_for_tx_free(); + tx_buffer[0] = 'C'; + tx_buffer[1] = (coin_value/10000)%10; + tx_buffer[2] = (coin_value/1000)%10; + tx_buffer[3] = (coin_value/100)%10; + tx_buffer[4] = (coin_value/10)%10; + tx_buffer[5] = coin_value%10; + tx_buffer[6] = '\n'; + tx_buffer[7] = 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')) { + 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'; + + coin_cost(cost); + send_ack(); +} + +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'; + if (key == KEY_RESET) + tx_buffer[1] = 'R'; + else + tx_buffer[1] = key+'0'; + tx_buffer[2] = '\n'; + tx_buffer[3] = 0; + send_packet(); +} + +void do_chime() { + chime_start(); + send_ack(); +} + int main() { u16 last_coin_value = coin_value; @@ -20,15 +118,20 @@ int main() { while(1) { if (rx_queue_state) { switch (msg_buf[0]) { - case 'V': /* dispense something */ + case 'V': + dispense_something(); break; - case 'D': /* write string to display */ + case 'D': + write_to_display(); break; - case 'B': /* beep */ + case 'B': + do_chime(); break; - case 'U': /* query current coin balance */ + case 'U': + send_balance(); break; - case 'G': /* give change */ + case 'G': + give_change(); break; default: /* shrug */ @@ -36,18 +139,17 @@ int main() { } msg_clr(); } + keypad_read(); if (keypad_pressed()) { - /* send packet about it */ + send_keypress(last_key); /* beep? */ } if (coin_value != last_coin_value) { - /* send a packet about it */ + send_balance(); + last_coin_value = coin_value; } } } -void rti() { - chime(); /* turn chime on or off as need be */ -} diff --git a/ROM2/motors.c b/ROM2/motors.c index 3fd99a2..b0e4ee3 100644 --- a/ROM2/motors.c +++ b/ROM2/motors.c @@ -1,6 +1,20 @@ #include "motors.h" #include "vend.h" +const u8 motor_lookup[80] = +{ 1,12,23,34,46,57,68,79, + 11,22,33,44,56,67,78,89, + 21,32,43,54,66,77,88,99, + 31,42,53,64,76,87,98, + 9,41,52,63,74,86,97, + 8,19,51,62,73,84,96, + 7,18,29,61,72,83,94, + 6,17,28,39,71,82,93, + 4,16,27,38,49,81,92, + 3,14,26,37,48,59,91, + 2,13,24,36,47,58,69, +}; + void motor_shift_send(u8 data) { u8 i; /* load it in, MSB first */ diff --git a/ROM2/motors.h b/ROM2/motors.h index a18e4b2..f44e327 100644 --- a/ROM2/motors.h +++ b/ROM2/motors.h @@ -10,19 +10,7 @@ #define MOTOR_CURRENT_FAIL 3 #define MOTOR_VOLTAGE_FAIL 4 -const u8 motor_lookup[80] = -{ 1,12,23,34,46,57,68,79, - 11,22,33,44,56,67,78,89, - 21,32,43,54,66,77,88,99, - 31,42,53,64,76,87,98, - 9,41,52,63,74,86,97, - 8,19,51,62,73,84,96, - 7,18,29,61,72,83,94, - 6,17,28,39,71,82,93, - 4,16,27,38,49,81,92, - 3,14,26,37,48,59,91, - 2,13,24,36,47,58,69, -}; +extern const u8 motor_lookup[80]; bool is_motor(u8 slot); u8 dispense_motor(u8 slot); diff --git a/ROM2/vectors.s b/ROM2/vectors.s index 76e6614..f420128 100644 --- a/ROM2/vectors.s +++ b/ROM2/vectors.s @@ -38,6 +38,13 @@ def: .globl _debug_user_vectors _debug_user_vectors = 0 + +;; RTI interrupt handler + .sect .text +rti: + jsr chime + rti + ;; ;; Interrupt vectors are in a specific section that is ;; mapped at 0xffc0.