Changes

Jump to: navigation, search
no edit summary
-----
{{notelist}}
 
== Helloworld from UART0 ==
Using the FPGA bitstream previously created, it is possible to use serial tty port on Linux. The serial port is mapped to <code>/dev/ttyPS1</code> (this is because <code>/dev/ttyPS0</code> is the console mapped to UART1).
 
Here below an example on C code for initializing and using UART0 through FPGA:
 
<pre>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <termios.h>
 
int set_interface_attribs (int fd, int speed, int parity)
{
struct termios tty;
memset (&tty, 0, sizeof tty);
if (tcgetattr (fd, &tty) != 0)
{
printf("error %d from tcgetattr", errno);
return -1;
}
 
cfsetospeed (&tty, speed);
cfsetispeed (&tty, speed);
 
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; // 8-bit chars
// disable IGNBRK for mismatched speed tests; otherwise receive break
// as \000 chars
tty.c_iflag &= ~IGNBRK; // disable break processing
tty.c_lflag = 0; // no signaling chars, no echo,
// no canonical processing
tty.c_oflag = 0; // no remapping, no delays
tty.c_cc[VMIN] = 0; // read doesn't block
tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
 
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl
 
tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls,
// enable reading
tty.c_cflag &= ~(PARENB | PARODD); // shut off parity
tty.c_cflag |= parity;
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CRTSCTS;
 
if (tcsetattr (fd, TCSANOW, &tty) != 0)
{
printf("error %d from tcsetattr", errno);
return -1;
}
return 0;
}
 
 
int main()
{
int fd;
char *portname = "/dev/ttyPS1";
 
char msg[] = "Hello World from BELK (FPGA PS0 UART)!\n\r";
 
fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC);
if (fd < 0)
{
printf("error %d opening %s: %s", errno, portname, strerror (errno));
exit(1);
}
printf(msg);
 
set_interface_attribs (fd, B115200, 0); // set speed to 115,200 bps, 8n1 (no parity)
write(fd, msg, strlen(msg));
 
exit(0);
}
 
 
</pre>
 
and then compile it:
 
<pre>
dvdk@vagrant-ubuntu-trusty-64:~/bora/rfs/belk/home/root$ $CC hello_UART0.c -o hello_UART0
</pre>
 
The program executed print out the msg string on serial console and on /dev/ttyPS1 port.
8,185
edits

Navigation menu