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