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;
13 /* assumes clock of 4.91Mhz */
14 _io_ports[M6811_BAUD] = 0x03; /* 9600 baud */
16 /* Setup character format 1 start, 8-bits, 1 stop. */
17 _io_ports[M6811_SCCR1] = 0;
19 /* Enable reciever and transmitter & rx interrupt. */
20 _io_ports[M6811_SCCR2] = M6811_RIE | M6811_TE | M6811_RE;
24 sci_rx_buf_ptr_start = 0;
29 void send_buffer(bool crlf) {
31 for (c = sci_tx_buf; *c; c++) {
32 /* wait for TX ready */
33 while (!(_io_ports[M6811_SCSR] & M6811_TDRE));
36 _io_ports[M6811_SCDR] = *c;
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';
46 void send_string(char* c) {
48 while (!(_io_ports[M6811_SCSR] & M6811_TDRE)); /* wait for TX ready */
49 _io_ports[M6811_SCDR] = *c; /* send byte */
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 :( */
60 sci_rx_buf[sci_rx_buf_ptr] = buf;
62 sci_rx_buf_ptr %= BUFFER_LEN;
66 while (!(_io_ports[M6811_SCSR] & M6811_TDRE)); /* wait for TX ready */
67 _io_ports[M6811_SCDR] = buf; /* send byte */
70 /* XXX FIXME we should do something about errors. nack? */
71 if (sci_have_packet) {
75 sci_rx_buf[sci_rx_buf_ptr] = buf;
77 if (buf == '\n' || buf == '\r') {
78 sci_rx_buf[sci_rx_buf_ptr] = '\0';
82 if (sci_rx_buf_ptr+1 < BUFFER_LEN)
85 sci_rx_buf[BUFFER_LEN-1] = '\0'; /* this is as much as we could fit */
89 void sci_interrupt_serial() {
90 if (_io_ports[M6811_SCSR] & M6811_RDRF) sci_rx_int();
92 if (_io_ports[M6811_SCSR] & M6811_OR)
93 _io_ports[M6811_SCDR]; /* declare it a lost cause */
102 send_string("!" CRLF);
106 send_string("?" CRLF);
110 void serial_rti() { /* called every 6.6 ms */
111 if (sci_timer) sci_timer--;
114 /* for gdb compatibility */
115 int serial_readchar(u8 timeout) {
117 sci_timer = timeout * 152;
118 while (sci_timer && sci_rx_buf_ptr_start == sci_rx_buf_ptr); /* spin */
119 if (sci_timer == 0) return SERIAL_TIMEOUT;
120 ret = sci_rx_buf[sci_rx_buf_ptr_start];
121 sci_rx_buf_ptr_start++;
122 sci_rx_buf_ptr_start %= BUFFER_LEN;
126 void serial_write(const char *str, int len) {
128 end = (char*)(str + len);
129 for (c = (char*)str; c < end; c++) {
130 while (!(_io_ports[M6811_SCSR] & M6811_TDRE)); /* wait for TX ready */
131 _io_ports[M6811_SCDR] = *c; /* send byte */