In event really mode, the mbed needs to queue up relayed event messages
until the time sync messages goes out, otherwise the timer will trigger
at the wrong time.
int main(int argc, char **argv)
{
// Open serial ports
int main(int argc, char **argv)
{
// Open serial ports
- sirq_dbg = sirq_open(SIRQ_UART0, USBTX, USBRX, 115200); // to pc
- sirq_bbb = sirq_open(SIRQ_UART1, PTE0, PTE1, 115200); // to bbb
- sirq_mbed = sirq_open(SIRQ_UART2, PTD3, PTD2, 115200); // to mbed
+ sirq_dbg = sirq_open(SIRQ_UART0, USBTX, USBRX, 115200, 0); // to pc
+ sirq_bbb = sirq_open(SIRQ_UART1, PTE0, PTE1, 115200, 0); // to bbb
+ sirq_mbed = sirq_open(SIRQ_UART2, PTD3, PTD2, 115200, 1); // to mbed
// Setup timers
tdma_evt = tdma_open(TDMA_CHAN0, 3, PTC9, PullDown); // async event
// Setup timers
tdma_evt = tdma_open(TDMA_CHAN0, 3, PTC9, PullDown); // async event
/**
* Output time sync message
*/
/**
* Output time sync message
*/
head.length = sizeof(body);
head.cksum = 0; // todo
head.length = sizeof(body);
head.cksum = 0; // todo
+ sirq_write(comm_sirq_mbed, &head, sizeof(head));
+
+ // Capture transmit time
tdma_stop(comm_tdma_rcv, 0);
tdma_start(comm_tdma_xmt);
tdma_stop(comm_tdma_rcv, 0);
tdma_start(comm_tdma_xmt);
- sirq_write(comm_sirq_mbed, &head, sizeof(head));
+ sirq_transmit(comm_sirq_mbed);
tdma_stop(comm_tdma_xmt, 100);
tdma_start(comm_tdma_rcv);
tdma_stop(comm_tdma_xmt, 100);
tdma_start(comm_tdma_rcv);
sirq_write(comm_sirq_mbed, &body, sizeof(body));
sirq_write(comm_sirq_mbed, &body, sizeof(body));
+ sirq_transmit(comm_sirq_mbed);
+
// Queue next transmit time
comm_sync_due = 0;
}
// Queue next transmit time
comm_sync_due = 0;
}
if (comm_device_id == 1) {
sirq_write(comm_sirq_bbb, &head, sizeof(head));
sirq_write(comm_sirq_bbb, &body, sizeof(body));
if (comm_device_id == 1) {
sirq_write(comm_sirq_bbb, &head, sizeof(head));
sirq_write(comm_sirq_bbb, &body, sizeof(body));
+ } else if (comm_device_id > 1) {
sirq_write(comm_sirq_mbed, &head, sizeof(head));
sirq_write(comm_sirq_mbed, &body, sizeof(body));
sirq_write(comm_sirq_mbed, &head, sizeof(head));
sirq_write(comm_sirq_mbed, &body, sizeof(body));
+ } else {
+ sirq_printf("no device id, skipping event\r\n");
# Common settings
UART0 = /dev/ttyACM0
UART1 = /dev/ttyACM1
# Common settings
UART0 = /dev/ttyACM0
UART1 = /dev/ttyACM1
LDFLAGS = -lm
# Common rules
LDFLAGS = -lm
# Common rules
all: mbed.elf tester.elf control
terms:
all: mbed.elf tester.elf control
terms:
queue_t xmt;\r
queue_t rcv;\r
int irq;\r
queue_t xmt;\r
queue_t rcv;\r
int irq;\r
};\r
\r
/* Port data */\r
};\r
\r
/* Port data */\r
sirq_t *port = (sirq_t *)_port;\r
\r
// Handle transmit\r
sirq_t *port = (sirq_t *)_port;\r
\r
// Handle transmit\r
- if (event == TxIrq && 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
+ // 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
}\r
\r
// Handle receive\r
-sirq_t *sirq_open(sirq_uart_t uart, PinName tx, PinName rx, int baud)\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
{\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
// Configure port\r
serial_init(&port->uart, tx, rx);\r
serial_baud(&port->uart, baud);\r
{\r
port->xmt.buf[port->xmt.wix] = byte;\r
port->xmt.wix = (port->xmt.wix+1) % SIRQ_LEN;\r
{\r
port->xmt.buf[port->xmt.wix] = byte;\r
port->xmt.wix = (port->xmt.wix+1) % SIRQ_LEN;\r
- if (!port->irq) {\r
- port->irq = 1;\r
- serial_irq_set(&port->uart, TxIrq, 1);\r
- }\r
+ if (!port->buffered)\r
+ sirq_transmit(port);\r
}\r
\r
/* Read byte from the port */\r
}\r
\r
/* Read byte from the port */\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
/* Buffered write */\r
void sirq_write(sirq_t *port, void *data, int len)\r
{\r
typedef struct sirq_t sirq_t;
/* Open */
typedef struct sirq_t sirq_t;
/* Open */
-sirq_t *sirq_open(sirq_uart_t uart, PinName tx, PinName rx, int baud);
+sirq_t *sirq_open(sirq_uart_t uart, PinName tx, PinName rx, int baud, int buffered);
/* Read/Write */
void sirq_putc(sirq_t *port, int byte);
int sirq_getc(sirq_t *port);
void sirq_write(sirq_t *port, void *data, int len);
/* Read/Write */
void sirq_putc(sirq_t *port, int byte);
int sirq_getc(sirq_t *port);
void sirq_write(sirq_t *port, void *data, int len);
+/* Transmit */
+void sirq_transmit(sirq_t *port);
+
/* Read In */
int sirq_ready(sirq_t *port);
/* Read In */
int sirq_ready(sirq_t *port);