Ktam3874/pitx software guide – Kontron KTAM3874-pITX User Manual
Page 56

KTD-S0057-I
Page 52 Linux® Programming Examples (DebianTM)
KTAM3874/pITX Software Guide
memset (&new_values, 0, sizeof (new_values));
new_values.c_cflag = CS8 | CLOCAL | CREAD;
new_values.c_iflag = IGNBRK;
new_values.c_cc[VMIN] = 1;
if (cfsetispeed (&new_values, baudrate))
return
-1;
if (cfsetospeed (&new_values, baudrate))
return
-1;
return tcsetattr (fd, TCSANOW, &new_values);
}
int uart_loopback (void)
{
char out_str[] = "the quick brown fox jumps over the lazy dog";
char in_str [64] = "\0";
if (write (fd, out_str, sizeof (out_str)) != sizeof (out_str))
return
-1;
if (tcdrain (fd))
return
-1;
read (fd, in_str, sizeof (out_str));
return memcmp (out_str, in_str, sizeof (out_str));
}
int main (void)
{
int baud[] = { 9600, 38400, 115200 };
int
i;
printf ("\nUART loopback test program\n");
for (i = 0; i < ARRAY_SIZE(baud); i++)
{
if (init_uart_interface (baud[i]))
{
close
(fd);
printf
("UART
init error!\n\n");
return
-1;
}
if (uart_loopback ())
{
close
(fd);
printf ("UART loopback test fails with baudrate %d!\n\n", baud[i]);
return
-1;
}
close
(fd);
}
printf ("UART loopback test successfully finished.\n\n");
return
0;
}