volatile char sci_rx_buf[BUFFER_LEN];
volatile bool sci_have_packet;
volatile u8 sci_rx_buf_ptr;
+volatile bool sci_echo;
void sci_init() {
/* assumes clock of 4.91Mhz */
_io_ports[M6811_SCCR1] = 0;
/* Enable reciever and transmitter & rx interrupt. */
- _io_ports[M6811_SCCR2] = 0x2c;
+ _io_ports[M6811_SCCR2] = M6811_RIE | M6811_TE | M6811_RE;
sci_have_packet = 0;
sci_rx_buf_ptr = 0;
+ sci_echo = 0;
}
-void send_packet() {
+void send_buffer(bool crlf) {
char* c;
for (c = sci_tx_buf; *c; c++) {
/* wait for TX ready */
/* send byte */
_io_ports[M6811_SCDR] = *c;
}
+ if (!crlf) return;
+ /* send CRLF */
+ while (!(_io_ports[M6811_SCSR] & M6811_TDRE));
+ _io_ports[M6811_SCDR] = '\r';
+ while (!(_io_ports[M6811_SCSR] & M6811_TDRE));
+ _io_ports[M6811_SCDR] = '\n';
}
-void rx_int() {
- if (sci_have_packet) return;
- if (sci_rx_buf_ptr >= BUFFER_LEN) {
+void send_string(char* c) {
+ for (; *c; c++) {
+ while (!(_io_ports[M6811_SCSR] & M6811_TDRE)); /* wait for TX ready */
+ _io_ports[M6811_SCDR] = *c; /* send byte */
+ }
+}
+
+void sci_rx_int() {
+ char buf = _io_ports[M6811_SCDR];
+ if (sci_echo) {
+ while (!(_io_ports[M6811_SCSR] & M6811_TDRE)); /* wait for TX ready */
+ _io_ports[M6811_SCDR] = buf; /* send byte */
+ }
+
+ /* XXX FIXME we should do something about errors. nack? */
+ if (sci_have_packet) {
/* 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] = buf;
+
+ if (buf == '\n' || buf == '\r') {
sci_rx_buf[sci_rx_buf_ptr] = '\0';
sci_have_packet = 1;
- sci_rx_buf_ptr = 0;
}
- sci_rx_buf_ptr++;
+
+ if (sci_rx_buf_ptr+1 < BUFFER_LEN)
+ sci_rx_buf_ptr++;
+ else {
+ sci_rx_buf[BUFFER_LEN-1] = '\0'; /* this is as much as we could fit */
+ }
+}
+
+void sci_interrupt_serial() {
+ if (_io_ports[M6811_SCSR] & M6811_RDRF) sci_rx_int();
+
+ if (_io_ports[M6811_SCSR] & M6811_OR)
+ _io_ports[M6811_SCDR]; /* declare it a lost cause */
}
void msg_clr() {
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();
+ send_string("!" CRLF);
}
void send_nack() {
- sci_tx_buf[0] = '?';
- sci_tx_buf[1] = '\n';
- sci_tx_buf[2] = '\0';
- send_packet();
+ send_string("?" CRLF);
}