Skip to content

Commit

Permalink
[arch/linux][uart] put byte in queue if write was interrupted
Browse files Browse the repository at this point in the history
and only advance extract index if write was successful
  • Loading branch information
flixr committed Feb 16, 2015
1 parent 92d2b9b commit f374efe
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions sw/airborne/arch/linux/mcu_periph/uart_arch.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "serial_port.h"

Expand Down Expand Up @@ -88,6 +89,7 @@ void uart_transmit(struct uart_periph *periph, uint8_t data)
uint16_t temp = (periph->tx_insert_idx + 1) % UART_TX_BUFFER_SIZE;

if (temp == periph->tx_extract_idx) {
printf("uart tx queue full!\n");
return; // no room
}

Expand All @@ -100,12 +102,16 @@ void uart_transmit(struct uart_periph *periph, uint8_t data)
struct SerialPort *port = (struct SerialPort *)(periph->reg_addr);
int ret = write((int)(port->fd), &data, 1);
if (ret < 1) {
TRACE("w %x [%d]\n", data, ret);
TRACE("uart_transmit: write %d failed [%d: %s]\n", data, ret, strerror(errno));
/* if write was interrupted, put data into queue */
if (errno == EINTR) {
periph->tx_buf[periph->tx_insert_idx] = data;
periph->tx_insert_idx = temp;
}
}
}
}

#include <errno.h>

static inline void uart_handler(struct uart_periph *periph)
{
Expand All @@ -120,10 +126,12 @@ static inline void uart_handler(struct uart_periph *periph)
if (periph->tx_insert_idx != periph->tx_extract_idx) {
int ret = write(fd, &(periph->tx_buf[periph->tx_extract_idx]), 1);
if (ret < 1) {
TRACE("w %x [%d: %s]\n", periph->tx_buf[periph->tx_extract_idx], ret, strerror(errno));
TRACE("uart_handler: write %x failed [%d: %s]\n", periph->tx_buf[periph->tx_extract_idx], ret, strerror(errno));
}
else {
periph->tx_extract_idx++;
periph->tx_extract_idx %= UART_TX_BUFFER_SIZE;
}
periph->tx_extract_idx++;
periph->tx_extract_idx %= UART_TX_BUFFER_SIZE;
} else {
periph->tx_running = FALSE; // clear running flag
}
Expand Down

0 comments on commit f374efe

Please sign in to comment.