5 #include "serial_api.h"
\r
6 #include "serial_irq.h"
\r
9 #define SIRQ_LEN 1024
\r
11 /* Port structure */
\r
15 uint8_t buf[SIRQ_LEN];
\r
25 static sirq_t sirq_ports[SIRQ_NUM_UART];
\r
27 /* Receive handler */
\r
28 void sirq_handler(uint32_t _port, SerialIrq event)
\r
30 sirq_t *port = (sirq_t *)_port;
\r
33 if (event == TxIrq && port->xmt.rix != port->xmt.wix) {
\r
34 int byte = port->xmt.buf[port->xmt.rix];
\r
35 serial_putc(&port->uart, byte);
\r
36 port->xmt.rix = (port->xmt.rix+1) % SIRQ_LEN;
\r
38 serial_irq_set(&port->uart, TxIrq, 0);
\r
42 if (event == RxIrq) {
\r
43 int byte = serial_getc(&port->uart);
\r
44 port->rcv.buf[port->rcv.wix] = byte;
\r
45 port->rcv.wix = (port->rcv.wix+1) % SIRQ_LEN;
\r
50 sirq_t *sirq_open(sirq_uart_t uart, PinName tx, PinName rx, int baud)
\r
53 sirq_t *port = &sirq_ports[uart];
\r
56 serial_init(&port->uart, tx, rx);
\r
57 serial_baud(&port->uart, baud);
\r
60 serial_irq_handler(&port->uart, sirq_handler, (uint32_t)port);
\r
61 serial_irq_set(&port->uart, RxIrq, 1);
\r
66 /* Write byte to the port */
\r
67 void sirq_putc(sirq_t *port, int byte)
\r
69 port->xmt.buf[port->xmt.wix] = byte;
\r
70 port->xmt.wix = (port->xmt.wix+1) % SIRQ_LEN;
\r
71 serial_irq_set(&port->uart, TxIrq, 1);
\r
74 /* Read byte from the port */
\r
75 int sirq_getc(sirq_t *port)
\r
78 if (port->rcv.rix < port->rcv.wix) {
\r
79 byte = port->rcv.buf[port->rcv.rix];
\r
80 port->rcv.rix = (port->rcv.rix+1) % SIRQ_LEN;
\r
85 /* Buffered write */
\r
86 void sirq_write(sirq_t *port, void *data, int len)
\r
88 uint8_t *bytes = (uint8_t*)data;
\r
89 for (int i = 0; i < len; i++)
\r
90 sirq_putc(port, bytes[i]);
\r
93 /* Check if port is writable */
\r
94 int sirq_ready(sirq_t *port)
\r
96 return port->rcv.rix < port->rcv.wix;
\r
99 /* Write ASCII data to the output queue */
\r
100 void sirq_vprintf(const char *fmt, va_list ap)
\r
102 static char buf[512];
\r
103 int len = vsnprintf(buf, sizeof(buf), fmt, ap);
\r
104 for (int i = 0; i < len; i++)
\r
105 sirq_putc(&sirq_ports[0], buf[i]);
\r
108 void sirq_printf(const char *fmt, ...)
\r
112 sirq_vprintf(fmt, ap);
\r