-#include <MKL46Z4.h>\r
-\r
-#include <stdint.h>\r
-#include <stdio.h>\r
-#include "serial_api.h"\r
-#include "serial_irq.h"\r
-#include "timer_dma.h"\r
-\r
-/* Defines */\r
-#define SIRQ_LEN 1024\r
-\r
-/* Port structure */\r
-typedef struct {\r
- int rix;\r
- int wix;\r
- uint8_t buf[SIRQ_LEN];\r
-} queue_t;\r
-\r
-struct sirq_t {\r
- serial_t uart;\r
- queue_t xmt;\r
- queue_t rcv;\r
- int irq;\r
- int buffered;\r
-};\r
-\r
-/* Port data */\r
-static sirq_t sirq_ports[SIRQ_NUM_UART];\r
-\r
-/* Receive handler */\r
-void sirq_handler(uint32_t _port, SerialIrq event)\r
-{\r
- sirq_t *port = (sirq_t *)_port;\r
-\r
- // Handle transmit\r
- // note: mbed seems to call TxIrq even it is not enabled,\r
- // so we need to manually prevent transmitting when the\r
- // port is set to buffered mode.\r
- if (event == TxIrq && (port->irq || !port->buffered)) {\r
- if (port->xmt.rix != port->xmt.wix) {\r
- int byte = port->xmt.buf[port->xmt.rix];\r
- serial_putc(&port->uart, byte);\r
- port->xmt.rix = (port->xmt.rix+1) % SIRQ_LEN;\r
- } else {\r
- serial_irq_set(&port->uart, TxIrq, 0);\r
- port->irq = 0;\r
- }\r
- }\r
-\r
- // Handle receive\r
- if (event == RxIrq) {\r
- int byte = serial_getc(&port->uart);\r
- port->rcv.buf[port->rcv.wix] = byte;\r
- port->rcv.wix = (port->rcv.wix+1) % SIRQ_LEN;\r
- }\r
-}\r
-\r
-/* Open port */\r
-sirq_t *sirq_open(sirq_uart_t uart, PinName tx, PinName rx, int baud, int buffered)\r
-{\r
- // Allocate port\r
- sirq_t *port = &sirq_ports[uart];\r
-\r
- // Buffered ports only transmit on demand\r
- port->buffered = buffered;\r
-\r
- // Configure port\r
- serial_init(&port->uart, tx, rx);\r
- serial_baud(&port->uart, baud);\r
-\r
- // Set IRQ handlers\r
- serial_irq_handler(&port->uart, sirq_handler, (uint32_t)port);\r
- serial_irq_set(&port->uart, RxIrq, 1);\r
-\r
- return port;\r
-}\r
-\r
-/* Write byte to the port */\r
-void sirq_putc(sirq_t *port, int byte)\r
-{\r
- port->xmt.buf[port->xmt.wix] = byte;\r
- port->xmt.wix = (port->xmt.wix+1) % SIRQ_LEN;\r
- if (!port->buffered)\r
- sirq_transmit(port);\r
-}\r
-\r
-/* Read byte from the port */\r
-int sirq_getc(sirq_t *port)\r
-{\r
- int byte = 0;\r
- if (port->rcv.rix != port->rcv.wix) {\r
- byte = port->rcv.buf[port->rcv.rix];\r
- port->rcv.rix = (port->rcv.rix+1) % SIRQ_LEN;\r
- }\r
- return byte;\r
-}\r
-\r
-/* Enable transmitter */\r
-void sirq_transmit(sirq_t *port)\r
-{\r
- if (port->xmt.rix != port->xmt.wix && !port->irq) {\r
- port->irq = 1;\r
- serial_irq_set(&port->uart, TxIrq, 1);\r
- }\r
-}\r
-\r
-/* Buffered write */\r
-void sirq_write(sirq_t *port, void *data, int len)\r
-{\r
- uint8_t *bytes = (uint8_t*)data;\r
- for (int i = 0; i < len; i++)\r
- sirq_putc(port, bytes[i]);\r
-}\r
-\r
-/* Check if port is writable */\r
-int sirq_ready(sirq_t *port)\r
-{\r
- return port->rcv.rix != port->rcv.wix;\r
-}\r
-\r
-/* Debug print */\r
-void sirq_debug(sirq_t *port)\r
-{\r
- sirq_printf("xmt - wix:%03x rix:%03x\r\n", port->xmt.wix, port->xmt.rix);\r
- sirq_printf("rcv - wix:%03x rix:%03x\r\n", port->rcv.wix, port->rcv.rix);\r
- sirq_printf("irq - ??\r\n");\r
- sirq_printf("uart - ??\r\n");\r
-}\r
-\r
-/* Write ASCII data to the output queue */\r
-void sirq_vprintf(const char *fmt, va_list ap)\r
-{\r
- static char buf[512];\r
- int len = vsnprintf(buf, sizeof(buf), fmt, ap);\r
- for (int i = 0; i < len; i++)\r
- sirq_putc(&sirq_ports[0], buf[i]);\r
-}\r
-\r
-void sirq_printf(const char *fmt, ...)\r
-{\r
- va_list ap;\r
- va_start(ap, fmt);\r
- sirq_vprintf(fmt, ap);\r
- va_end(ap);\r
-}\r
+#include <MKL46Z4.h>
+
+#include <stdint.h>
+#include <stdio.h>
+#include "serial_api.h"
+#include "serial_irq.h"
+#include "timer_dma.h"
+
+/* Defines */
+#define SIRQ_LEN 1024
+
+/* Port structure */
+typedef struct {
+ int rix;
+ int wix;
+ uint8_t buf[SIRQ_LEN];
+} queue_t;
+
+struct sirq_t {
+ serial_t uart;
+ queue_t xmt;
+ queue_t rcv;
+ int irq;
+ int buffered;
+};
+
+/* Port data */
+static sirq_t sirq_ports[SIRQ_NUM_UART];
+
+/* Receive handler */
+void sirq_handler(uint32_t _port, SerialIrq event)
+{
+ sirq_t *port = (sirq_t *)_port;
+
+ // Handle transmit
+ // note: mbed seems to call TxIrq even it is not enabled,
+ // so we need to manually prevent transmitting when the
+ // port is set to buffered mode.
+ if (event == TxIrq && (port->irq || !port->buffered)) {
+ if (port->xmt.rix != port->xmt.wix) {
+ int byte = port->xmt.buf[port->xmt.rix];
+ serial_putc(&port->uart, byte);
+ port->xmt.rix = (port->xmt.rix+1) % SIRQ_LEN;
+ } else {
+ serial_irq_set(&port->uart, TxIrq, 0);
+ port->irq = 0;
+ }
+ }
+
+ // Handle receive
+ if (event == RxIrq) {
+ int byte = serial_getc(&port->uart);
+ port->rcv.buf[port->rcv.wix] = byte;
+ port->rcv.wix = (port->rcv.wix+1) % SIRQ_LEN;
+ }
+}
+
+/* Open port */
+sirq_t *sirq_open(sirq_uart_t uart, PinName tx, PinName rx, int baud, int buffered)
+{
+ // Allocate port
+ sirq_t *port = &sirq_ports[uart];
+
+ // Buffered ports only transmit on demand
+ port->buffered = buffered;
+
+ // Configure port
+ serial_init(&port->uart, tx, rx);
+ serial_baud(&port->uart, baud);
+
+ // Set IRQ handlers
+ serial_irq_handler(&port->uart, sirq_handler, (uint32_t)port);
+ serial_irq_set(&port->uart, RxIrq, 1);
+
+ return port;
+}
+
+/* Write byte to the port */
+void sirq_putc(sirq_t *port, int byte)
+{
+ port->xmt.buf[port->xmt.wix] = byte;
+ port->xmt.wix = (port->xmt.wix+1) % SIRQ_LEN;
+ if (!port->buffered)
+ sirq_transmit(port);
+}
+
+/* Read byte from the port */
+int sirq_getc(sirq_t *port)
+{
+ int byte = 0;
+ if (port->rcv.rix != port->rcv.wix) {
+ byte = port->rcv.buf[port->rcv.rix];
+ port->rcv.rix = (port->rcv.rix+1) % SIRQ_LEN;
+ }
+ return byte;
+}
+
+/* Enable transmitter */
+void sirq_transmit(sirq_t *port)
+{
+ if (port->xmt.rix != port->xmt.wix && !port->irq) {
+ port->irq = 1;
+ serial_irq_set(&port->uart, TxIrq, 1);
+ }
+}
+
+/* Buffered write */
+void sirq_write(sirq_t *port, void *data, int len)
+{
+ uint8_t *bytes = (uint8_t*)data;
+ for (int i = 0; i < len; i++)
+ sirq_putc(port, bytes[i]);
+}
+
+/* Check if port is writable */
+int sirq_ready(sirq_t *port)
+{
+ return port->rcv.rix != port->rcv.wix;
+}
+
+/* Debug print */
+void sirq_debug(sirq_t *port)
+{
+ sirq_printf("xmt - wix:%03x rix:%03x\r\n", port->xmt.wix, port->xmt.rix);
+ sirq_printf("rcv - wix:%03x rix:%03x\r\n", port->rcv.wix, port->rcv.rix);
+ sirq_printf("irq - ??\r\n");
+ sirq_printf("uart - ??\r\n");
+}
+
+/* Write ASCII data to the output queue */
+void sirq_vprintf(const char *fmt, va_list ap)
+{
+ static char buf[512];
+ int len = vsnprintf(buf, sizeof(buf), fmt, ap);
+ for (int i = 0; i < len; i++)
+ sirq_putc(&sirq_ports[0], buf[i]);
+}
+
+void sirq_printf(const char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ sirq_vprintf(fmt, ap);
+ va_end(ap);
+}