5 #include "serial_api.h"
6 #include "serial_irq.h"
16 uint8_t buf[SIRQ_LEN];
28 static sirq_t sirq_ports[SIRQ_NUM_UART];
31 void sirq_handler(uint32_t _port, SerialIrq event)
33 sirq_t *port = (sirq_t *)_port;
36 // note: mbed seems to call TxIrq even it is not enabled,
37 // so we need to manually prevent transmitting when the
38 // port is set to buffered mode.
39 if (event == TxIrq && (port->irq || !port->buffered)) {
40 if (port->xmt.rix != port->xmt.wix) {
41 int byte = port->xmt.buf[port->xmt.rix];
42 serial_putc(&port->uart, byte);
43 port->xmt.rix = (port->xmt.rix+1) % SIRQ_LEN;
45 serial_irq_set(&port->uart, TxIrq, 0);
52 int byte = serial_getc(&port->uart);
53 port->rcv.buf[port->rcv.wix] = byte;
54 port->rcv.wix = (port->rcv.wix+1) % SIRQ_LEN;
59 sirq_t *sirq_open(sirq_uart_t uart, PinName tx, PinName rx, int baud, int buffered)
62 sirq_t *port = &sirq_ports[uart];
64 // Buffered ports only transmit on demand
65 port->buffered = buffered;
68 serial_init(&port->uart, tx, rx);
69 serial_baud(&port->uart, baud);
72 serial_irq_handler(&port->uart, sirq_handler, (uint32_t)port);
73 serial_irq_set(&port->uart, RxIrq, 1);
78 /* Write byte to the port */
79 void sirq_putc(sirq_t *port, int byte)
81 port->xmt.buf[port->xmt.wix] = byte;
82 port->xmt.wix = (port->xmt.wix+1) % SIRQ_LEN;
87 /* Read byte from the port */
88 int sirq_getc(sirq_t *port)
91 if (port->rcv.rix != port->rcv.wix) {
92 byte = port->rcv.buf[port->rcv.rix];
93 port->rcv.rix = (port->rcv.rix+1) % SIRQ_LEN;
98 /* Enable transmitter */
99 void sirq_transmit(sirq_t *port)
101 if (port->xmt.rix != port->xmt.wix && !port->irq) {
103 serial_irq_set(&port->uart, TxIrq, 1);
108 void sirq_write(sirq_t *port, void *data, int len)
110 uint8_t *bytes = (uint8_t*)data;
111 for (int i = 0; i < len; i++)
112 sirq_putc(port, bytes[i]);
115 /* Check if port is writable */
116 int sirq_ready(sirq_t *port)
118 return port->rcv.rix != port->rcv.wix;
122 void sirq_debug(sirq_t *port)
124 sirq_printf("xmt - wix:%03x rix:%03x\r\n", port->xmt.wix, port->xmt.rix);
125 sirq_printf("rcv - wix:%03x rix:%03x\r\n", port->rcv.wix, port->rcv.rix);
126 sirq_printf("irq - ??\r\n");
127 sirq_printf("uart - ??\r\n");
130 /* Write ASCII data to the output queue */
131 void sirq_vprintf(const char *fmt, va_list ap)
133 static char buf[512];
134 int len = vsnprintf(buf, sizeof(buf), fmt, ap);
135 for (int i = 0; i < len; i++)
136 sirq_putc(&sirq_ports[0], buf[i]);
139 void sirq_printf(const char *fmt, ...)
143 sirq_vprintf(fmt, ap);