Lots of changes! Takes us to rom S
[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 bool sci_echo;
9
10 void sci_init() {
11         /* assumes clock of 4.91Mhz */
12         _io_ports[M6811_BAUD] = 0x03; /* 9600 baud */
13
14         /* Setup character format 1 start, 8-bits, 1 stop.  */
15         _io_ports[M6811_SCCR1] = 0;
16
17         /* Enable reciever and transmitter & rx interrupt.  */
18         _io_ports[M6811_SCCR2] = M6811_RIE | M6811_TE | M6811_RE;
19
20         sci_have_packet = 0;
21         sci_rx_buf_ptr = 0;
22         sci_echo = 0;
23 }
24
25 void send_buffer(bool crlf) {
26         char* c;
27         for (c = sci_tx_buf; *c; c++) {
28                 /* wait for TX ready */
29                 while (!(_io_ports[M6811_SCSR] & M6811_TDRE));
30
31                 /* send byte */
32                 _io_ports[M6811_SCDR] = *c;
33         }
34         if (!crlf) return;
35         /* send CRLF */
36         while (!(_io_ports[M6811_SCSR] & M6811_TDRE));
37         _io_ports[M6811_SCDR] = '\r';
38         while (!(_io_ports[M6811_SCSR] & M6811_TDRE));
39         _io_ports[M6811_SCDR] = '\n';
40 }
41
42 void send_string(char* c) {
43         for (; *c; c++) {
44                 while (!(_io_ports[M6811_SCSR] & M6811_TDRE)); /* wait for TX ready */
45                 _io_ports[M6811_SCDR] = *c; /* send byte */
46         }
47 }
48
49 void sci_rx_int() {
50         char buf = _io_ports[M6811_SCDR];
51         if (sci_echo) {
52                 while (!(_io_ports[M6811_SCSR] & M6811_TDRE)); /* wait for TX ready */
53                 _io_ports[M6811_SCDR] = buf; /* send byte */
54         }
55
56         /* XXX FIXME we should do something about errors. nack? */
57         if (sci_have_packet) {
58                 /* overrun :( */
59                 return;
60         }
61         sci_rx_buf[sci_rx_buf_ptr] = buf;
62
63         if (buf == '\n' || buf == '\r') {
64                 sci_rx_buf[sci_rx_buf_ptr] = '\0';
65                 sci_have_packet = 1;
66         }
67
68         if (sci_rx_buf_ptr+1 < BUFFER_LEN)
69                 sci_rx_buf_ptr++;
70         else {
71                 sci_rx_buf[BUFFER_LEN-1] = '\0'; /* this is as much as we could fit */
72         }
73 }
74
75 void sci_interrupt_serial() {
76         if (_io_ports[M6811_SCSR] & M6811_RDRF) sci_rx_int();
77
78         if (_io_ports[M6811_SCSR] & M6811_OR)
79                 _io_ports[M6811_SCDR]; /* declare it a lost cause */
80 }
81
82 void msg_clr() {
83         sci_have_packet = 0;
84         sci_rx_buf_ptr = 0;
85 }
86
87 void send_ack() {
88         send_string("!" CRLF);
89 }
90
91 void send_nack() {
92         send_string("?" CRLF);
93 }

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