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)) {
237 TRACE(
"Error opening %s code %d\n", periph->
dev, ret);
245 if (periph->
reg_addr == NULL) {
return; }
252 ret = write((
int)(port->
fd), &data, 1);
253 }
while(ret < 1 && errno == EAGAIN);
256 TRACE(
"uart_put_byte: write %d failed [%d: %s]\n", data, ret, strerror(errno));
263 unsigned char c =
'D';
265 if (periph->reg_addr == NULL) {
return; }
270 pthread_mutex_lock(&uart_mutex);
272 if (read(fd, &c, 1) > 0) {
276 if (temp != periph->rx_extract_idx) {
277 periph->rx_buf[periph->rx_insert_idx] = c;
278 periph->rx_insert_idx = temp;
281 TRACE(
"uart_receive_handler: rx_buf full! discarding received byte: %x %c\n", c, c);
284 pthread_mutex_unlock(&uart_mutex);
289 pthread_mutex_lock(&uart_mutex);
292 pthread_mutex_unlock(&uart_mutex);
298 pthread_mutex_lock(&uart_mutex);
303 pthread_mutex_unlock(&uart_mutex);
308 void uart0_init(
void)
317 void uart1_init(
void)
326 void uart2_init(
void)
335 void uart3_init(
void)
344 void uart4_init(
void)
353 void uart5_init(
void)
362 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
void uart_periph_set_baudrate(struct uart_periph *p, uint32_t baud)
Set baudrate.
uint8_t uart_getch(struct uart_periph *p)
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_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)
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)