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");
64 pthread_setname_np(tid,
"uart");
80 int __attribute__((unused))
fd;
82 if (uart0.reg_addr != NULL) {
83 fd = ((
struct SerialPort *)uart0.reg_addr)->fd;
84 FD_SET(fd, &fds_master);
91 if (uart1.reg_addr != NULL) {
92 fd = ((
struct SerialPort *)uart1.reg_addr)->fd;
93 FD_SET(fd, &fds_master);
100 if (uart2.reg_addr != NULL) {
101 fd = ((
struct SerialPort *)uart2.reg_addr)->fd;
102 FD_SET(fd, &fds_master);
109 if (uart3.reg_addr != NULL) {
110 fd = ((
struct SerialPort *)uart3.reg_addr)->fd;
111 FD_SET(fd, &fds_master);
118 if (uart4.reg_addr != NULL) {
119 fd = ((
struct SerialPort *)uart4.reg_addr)->fd;
120 FD_SET(fd, &fds_master);
127 if (uart5.reg_addr != NULL) {
128 fd = ((
struct SerialPort *)uart5.reg_addr)->fd;
129 FD_SET(fd, &fds_master);
136 if (uart6.reg_addr != NULL) {
137 fd = ((
struct SerialPort *)uart6.reg_addr)->fd;
138 FD_SET(fd, &fds_master);
152 if (select(fdmax + 1, &fds, NULL, NULL, NULL) < 0) {
153 fprintf(stderr,
"uart_thread: select failed!");
156 if (uart0.reg_addr != NULL) {
157 fd = ((
struct SerialPort *)uart0.reg_addr)->fd;
158 if (FD_ISSET(fd, &fds)) {
164 if (uart1.reg_addr != NULL) {
165 fd = ((
struct SerialPort *)uart1.reg_addr)->fd;
166 if (FD_ISSET(fd, &fds)) {
172 if (uart2.reg_addr != NULL) {
173 fd = ((
struct SerialPort *)uart2.reg_addr)->fd;
174 if (FD_ISSET(fd, &fds)) {
180 if (uart3.reg_addr != NULL) {
181 fd = ((
struct SerialPort *)uart3.reg_addr)->fd;
182 if (FD_ISSET(fd, &fds)) {
188 if (uart4.reg_addr != NULL) {
189 fd = ((
struct SerialPort *)uart4.reg_addr)->fd;
190 if (FD_ISSET(fd, &fds)) {
196 if (uart5.reg_addr != NULL) {
197 fd = ((
struct SerialPort *)uart5.reg_addr)->fd;
198 if (FD_ISSET(fd, &fds)) {
204 if (uart6.reg_addr != NULL) {
205 fd = ((
struct SerialPort *)uart6.reg_addr)->fd;
206 if (FD_ISSET(fd, &fds)) {
240 TRACE(
"Error opening %s code %d\n", periph->
dev, ret);
274 if (periph->
reg_addr == NULL) {
return; }
281 ret = write((
int)(port->
fd), &data, 1);
282 }
while (ret < 1 && errno == EAGAIN);
285 TRACE(
"uart_put_byte: write %d failed [%d: %s]\n", data, ret, strerror(errno));
292 unsigned char c =
'D';
294 if (periph->reg_addr == NULL) {
return; }
299 pthread_mutex_lock(&uart_mutex);
301 if (read(fd, &c, 1) > 0) {
305 if (temp != periph->rx_extract_idx) {
306 periph->rx_buf[periph->rx_insert_idx] = c;
307 periph->rx_insert_idx = temp;
309 TRACE(
"uart_receive_handler: rx_buf full! discarding received byte: %x %c\n", c, c);
312 pthread_mutex_unlock(&uart_mutex);
317 pthread_mutex_lock(&uart_mutex);
320 pthread_mutex_unlock(&uart_mutex);
326 pthread_mutex_lock(&uart_mutex);
331 pthread_mutex_unlock(&uart_mutex);
336 void uart0_init(
void)
345 void uart1_init(
void)
354 void uart2_init(
void)
363 void uart3_init(
void)
372 void uart4_init(
void)
381 void uart5_init(
void)
390 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.
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
#define TRACE(fmt, args...)
void * reg_addr
UART Register.
Functions to obtain rt priority or set the nice level.
static void * uart_thread(void *data)
void serial_port_free(struct SerialPort *me)
void uart_arch_init(void)
static void uart_periph_open(struct uart_periph *periph, uint32_t baud)
static void uart_receive_handler(struct uart_periph *periph)
void uart_periph_init(struct uart_periph *p)
void uart_put_byte(struct uart_periph *p, long fd, uint8_t data)
Uart transmit implementation.
static pthread_mutex_t uart_mutex
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)