4 uart interface examples, Ktam3874/pitx software guide, 1 uart loopback test utility – Kontron KTAM3874-pITX User Manual
Page 55
KTD-S0057-I
Page 51 Linux® Programming Examples (DebianTM)
KTAM3874/pITX Software Guide
10.4 UART Interface Examples
10.4.1 UART Loopback Test Utility
A simple way to check the UART interface consists in shorting of the data lines (TXD, RXD). After that you
can use the following program with root rights (compiled on Ubuntu
TM
distribution):
/* UART loopback test program
* Copyright (c) 2013 Kontron Technology A/S
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License. */
#include <termios.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
/****** Do not forget to enable UART2 and/or UART4 in U-Boot Setup ******/
static const char *device = "/dev/ttyO4";
/* Connector J2105 pin 11 to 14 */
/*static const char *device = "/dev/ttyO2";*/
/* Connector J2105 pin 29 to 32 */
static int fd;
int init_uart_interface (int baudrate)
{
struct
termios
new_values;
fd = open (device, O_RDWR);
if (fd < 0 )
return
-1;
if (tcflush (fd, TCIOFLUSH))
return
-1;
switch (baudrate)
{
case 300
: baudrate = B300; break;
case 600
: baudrate = B600; break;
case 1200
: baudrate = B1200; break;
case 2400
: baudrate = B2400; break;
case 4800
: baudrate = B4800; break;
case 9600
: baudrate = B9600; break;
case 19200
: baudrate = B19200; break;
case 38400
: baudrate = B38400; break;
case 57600
: baudrate = B57600; break;
case 115200 : baudrate = B115200; break;
default
: return -1;
}