fe59f75b1b6c478a9cfaa5d5e7ebbbec1ce77e4a
[uccvend-snackrom.git] / ROM2 / sci.c
1 #include "vend.h"
2 #include "sci.h"
3
4 char sci_tx_buf[BUFFER_LEN];
5 volatile char sci_rx_buf[BUFFER_LEN];
6 volatile bool sci_have_packet;
7 volatile u8 sci_rx_buf_ptr;
8 volatile u8 sci_rx_buf_ptr_start;
9 volatile bool sci_echo;
10 bool sci_doing_xmodem;
11
12 void sci_init() {
13         /* assumes clock of 4.91Mhz */
14         _io_ports[M6811_BAUD] = 0x03; /* 9600 baud */
15
16         /* Setup character format 1 start, 8-bits, 1 stop.  */
17         _io_ports[M6811_SCCR1] = 0;
18
19         /* Enable reciever and transmitter & rx interrupt.  */
20         _io_ports[M6811_SCCR2] = M6811_RIE | M6811_TE | M6811_RE;
21
22         sci_have_packet = 0;
23         sci_rx_buf_ptr = 0;
24         sci_rx_buf_ptr_start = 0;
25         sci_echo = 0;
26         sci_doing_xmodem = 0;
27 }
28
29 void send_buffer(bool crlf) {
30         char* c;
31         for (c = sci_tx_buf; *c; c++) {
32                 /* wait for TX ready */
33                 while (!(_io_ports[M6811_SCSR] & M6811_TDRE));
34
35                 /* send byte */
36                 _io_ports[M6811_SCDR] = *c;
37         }
38         if (!crlf) return;
39         /* send CRLF */
40         while (!(_io_ports[M6811_SCSR] & M6811_TDRE));
41         _io_ports[M6811_SCDR] = '\r';
42         while (!(_io_ports[M6811_SCSR] & M6811_TDRE));
43         _io_ports[M6811_SCDR] = '\n';
44 }
45
46 void send_string(char* c) {
47         for (; *c; c++) {
48                 while (!(_io_ports[M6811_SCSR] & M6811_TDRE)); /* wait for TX ready */
49                 _io_ports[M6811_SCDR] = *c; /* send byte */
50         }
51 }
52
53 void sci_rx_int() {
54         char buf = _io_ports[M6811_SCDR];
55         if (sci_doing_xmodem) {
56                 if ((sci_rx_buf_ptr+1)%BUFFER_LEN == sci_rx_buf_ptr_start) {
57                         /* we drop following bytes :( */
58                         return;
59                 }
60                 sci_rx_buf[sci_rx_buf_ptr] = buf;
61                 sci_rx_buf_ptr++;
62                 sci_rx_buf_ptr %= BUFFER_LEN;
63                 return;
64         }
65         if (sci_echo) {
66                 while (!(_io_ports[M6811_SCSR] & M6811_TDRE)); /* wait for TX ready */
67                 _io_ports[M6811_SCDR] = buf; /* send byte */
68         }
69
70         /* XXX FIXME we should do something about errors. nack? */
71         if (sci_have_packet) {
72                 /* overrun :( */
73                 return;
74         }
75         sci_rx_buf[sci_rx_buf_ptr] = buf;
76
77         if (buf == '\n' || buf == '\r') {
78                 sci_rx_buf[sci_rx_buf_ptr] = '\0';
79                 sci_have_packet = 1;
80         }
81
82         if (sci_rx_buf_ptr+1 < BUFFER_LEN)
83                 sci_rx_buf_ptr++;
84         else {
85                 sci_rx_buf[BUFFER_LEN-1] = '\0'; /* this is as much as we could fit */
86         }
87 }
88
89 void sci_interrupt_serial() {
90         if (_io_ports[M6811_SCSR] & M6811_RDRF) sci_rx_int();
91
92         if (_io_ports[M6811_SCSR] & M6811_OR)
93                 _io_ports[M6811_SCDR]; /* declare it a lost cause */
94 }
95
96 void msg_clr() {
97         sci_have_packet = 0;
98         sci_rx_buf_ptr = 0;
99 }
100
101 void send_ack() {
102         send_string("!" CRLF);
103 }
104
105 void send_nack() {
106         send_string("?" CRLF);
107 }
108
109 u16 sci_timer;
110 void serial_rti() { /* called every 6.6 ms */
111         if (sci_timer) sci_timer--;
112 }
113
114 /* for gdb compatibility */
115 int serial_readchar(u8 timeout) {
116         sci_timer = timeout * 152;
117         while (sci_timer && sci_rx_buf_ptr_start == sci_rx_buf_ptr); /* spin */
118         if (sci_timer == 0) return SERIAL_TIMEOUT;
119         return sci_rx_buf[sci_rx_buf_ptr_start++];
120 }
121
122 void serial_write(const char *str, int len) {
123         char *c, *end;
124         end = (char*)(str + len);
125         for (c = (char*)str; c < end; c++) {
126                 while (!(_io_ports[M6811_SCSR] & M6811_TDRE)); /* wait for TX ready */
127                 _io_ports[M6811_SCDR] = *c; /* send byte */
128         }
129 }

UCC git Repository :: git.ucc.asn.au