41 #include <sys/select.h>
43 #ifndef UART_THREAD_PRIO
44 #define UART_THREAD_PRIO 11
48 static void *
uart_thread(
void *data __attribute__((unused)));
49 static pthread_mutex_t
uart_mutex = PTHREAD_MUTEX_INITIALIZER;
52 #define TRACE(fmt,args...)
56 pthread_mutex_init(&uart_mutex, NULL);
59 if (pthread_create(&tid, NULL,
uart_thread, NULL) != 0) {
60 fprintf(stderr,
"uart_arch_init: Could not create UART reading thread.\n");
77 int __attribute__ ((unused))
fd;
79 if (uart0.reg_addr != NULL) {
80 fd = ((
struct SerialPort *)uart0.reg_addr)->fd;
81 FD_SET(fd, &fds_master);
88 if (uart1.reg_addr != NULL) {
89 fd = ((
struct SerialPort *)uart1.reg_addr)->fd;
90 FD_SET(fd, &fds_master);
97 if (uart2.reg_addr != NULL) {
98 fd = ((
struct SerialPort *)uart2.reg_addr)->fd;
99 FD_SET(fd, &fds_master);
106 if (uart3.reg_addr != NULL) {
107 fd = ((
struct SerialPort *)uart3.reg_addr)->fd;
108 FD_SET(fd, &fds_master);
115 if (uart4.reg_addr != NULL) {
116 fd = ((
struct SerialPort *)uart4.reg_addr)->fd;
117 FD_SET(fd, &fds_master);
124 if (uart5.reg_addr != NULL) {
125 fd = ((
struct SerialPort *)uart5.reg_addr)->fd;
126 FD_SET(fd, &fds_master);
133 if (uart6.reg_addr != NULL) {
134 fd = ((
struct SerialPort *)uart6.reg_addr)->fd;
135 FD_SET(fd, &fds_master);
149 if (select(fdmax + 1, &fds, NULL, NULL, NULL) < 0) {
150 fprintf(stderr,
"uart_thread: select failed!");
154 if (uart0.reg_addr != NULL) {
155 fd = ((
struct SerialPort *)uart0.reg_addr)->fd;
156 if (FD_ISSET(fd, &fds)) {
162 if (uart1.reg_addr != NULL) {
163 fd = ((
struct SerialPort *)uart1.reg_addr)->fd;
164 if (FD_ISSET(fd, &fds)) {
170 if (uart2.reg_addr != NULL) {
171 fd = ((
struct SerialPort *)uart2.reg_addr)->fd;
172 if (FD_ISSET(fd, &fds)) {
178 if (uart3.reg_addr != NULL) {
179 fd = ((
struct SerialPort *)uart3.reg_addr)->fd;
180 if (FD_ISSET(fd, &fds)) {
186 if (uart4.reg_addr != NULL) {
187 fd = ((
struct SerialPort *)uart4.reg_addr)->fd;
188 if (FD_ISSET(fd, &fds)) {
194 if (uart5.reg_addr != NULL) {
195 fd = ((
struct SerialPort *)uart5.reg_addr)->fd;
196 if (FD_ISSET(fd, &fds)) {
202 if (uart6.reg_addr != NULL) {
203 fd = ((
struct SerialPort *)uart6.reg_addr)->fd;
204 if (FD_ISSET(fd, &fds)) {
238 TRACE(
"Error opening %s code %d\n", periph->
dev, ret);
272 if (periph->
reg_addr == NULL) {
return; }
279 ret = write((
int)(port->
fd), &data, 1);
280 }
while(ret < 1 && errno == EAGAIN);
283 TRACE(
"uart_put_byte: write %d failed [%d: %s]\n", data, ret, strerror(errno));
290 unsigned char c =
'D';
292 if (periph->reg_addr == NULL) {
return; }
297 pthread_mutex_lock(&uart_mutex);
299 if (read(fd, &c, 1) > 0) {
303 if (temp != periph->rx_extract_idx) {
304 periph->rx_buf[periph->rx_insert_idx] = c;
305 periph->rx_insert_idx = temp;
308 TRACE(
"uart_receive_handler: rx_buf full! discarding received byte: %x %c\n", c, c);
311 pthread_mutex_unlock(&uart_mutex);
316 pthread_mutex_lock(&uart_mutex);
319 pthread_mutex_unlock(&uart_mutex);
325 pthread_mutex_lock(&uart_mutex);
330 pthread_mutex_unlock(&uart_mutex);
335 void uart0_init(
void)
344 void uart1_init(
void)
353 void uart2_init(
void)
362 void uart3_init(
void)
371 void uart4_init(
void)
380 void uart5_init(
void)
389 void uart6_init(
void)
#define UART_DEV_NAME_SIZE
uint8_t rx_buf[UART_RX_BUFFER_SIZE]
Receive buffer.
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
#define UART3_DEV
uart connected to SBUS input
void uart_periph_set_baudrate(struct uart_periph *p, uint32_t baud)
Set baudrate.
#define TRACE(fmt, args...)
uint8_t uart_getch(struct uart_periph *p)
int serial_port_set_bits_stop_parity(struct SerialPort *me, const int bits, const int stop, const int parity)
uint16_t uart_char_available(struct uart_periph *p)
Check UART for available chars in receive buffer.
#define UART_RX_BUFFER_SIZE
void * reg_addr
UART Register.
Functions to obtain rt priority or set the nice level.
void serial_port_free(struct SerialPort *me)
void uart_arch_init(void)
static void * uart_thread(void *data)
static void uart_receive_handler(struct uart_periph *periph)
void uart_periph_init(struct uart_periph *p)
static void uart_periph_open(struct uart_periph *periph, uint32_t baud)
void uart_put_byte(struct uart_periph *p, long fd, uint8_t data)
Uart transmit implementation.
char dev[UART_DEV_NAME_SIZE]
UART Dev (linux)
void uart_periph_set_bits_stop_parity(struct uart_periph *p, uint8_t bits, uint8_t stop, uint8_t parity)
Set parity and stop bits.
struct SerialPort * serial_port_new(void)
void serial_port_close(struct SerialPort *me)
int serial_port_open_raw(struct SerialPort *me, const char *device, speed_t speed)
int baudrate
UART Baudrate.
static int get_rt_prio(int prio)
int serial_port_set_baudrate(struct SerialPort *me, speed_t speed)
static pthread_mutex_t uart_mutex