Using the Serial Port on the DE1-SoC

The micro USB port that can be found in the upper right corner of the DE1-SoC is connected to the HPS of the Cyclone V chip and is therefore accessible from the Linux running on the board for communication. However, it is not a real USB port but a serial port with an FTDI serial-to-USB converter. If you connect your host computer’s terminal to this serial port you will not only be presented with the boot screen but also with the actual Linux shell.

The HPS in the Cyclone V does have two of those serial ports (/dev/ttyS0 and /dev/ttyS1) but sadly the people Terasic decided to just provide us with one of them. If you need your Linux to communicate with the outside world through a serial connection then you’ll find out that you can’t do that as there is always just one application allowed to access a serial port. In our case this program is Getty which provides the shell. However, there is still a way to use the serial port of the DE1-SoC development board. This blog post will lead you through the process.

Step 1: Setting up SSH

As soon as Getty will be disabled the shell won’t be accessible through the serial port any more. An SSH connection through the ethernet port will allow accessing the shell once SSH is set up properly.
If you’re using one of the standard Linux images for the DE1-SoC board that you can download form the Terasic website then you won’t have to do anything here. SSH is enabled by default and actually used as part of the tutorial to transfer the compiled binaries to the board. The only thing that is highly recommendable to do is to set a static IP.

Step 2: Disabling Getty

Double check that your SSH connection is working properly (after rebooting!). Note: You cannot damage the board itself. Should there be a problem of any sort you can always write a new image to the microSD card and start fresh. The next thing to do is to disable Getty. Getty is launched automatically at system start-up through the /etc/inittabs file. Disable the following two lines by simply commenting them out (putting a hash in front of them):

S:2345:respawn:/sbin/getty 115200 ttyS0
1:2345:respawn:/sbin/getty 38400 tty1

Getty will now no longer be started and the serial port /dev/ttyS0 becomes available to any custom users space program that you write.

Step 3: Enjoy your serial port

Any program is now able to open the /dev/ttyS0 port. The following program demonstrates how to write to the serial port.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
 
int main(int argc, char* argv[])
{
	(void)argc;
	(void)argv;
 
	int fd;
 
	fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
	if (fd == -1) {
		printf("Couldn't open serial port\r\n");
 
		return EXIT_FAILURE;
	}
 
	char* buf = "SeriouslyEmbedded.com on Air!\r\n";
	int wr = write(fd, (void*)buf, strlen(buf));
 
	if (wr != strlen(buf)) {
		printf("Not everything has been written\r\n");
	}
 
	while (1) {
 
	}
 
	return EXIT_SUCCESS;
}

At this point I’d like to thank my friend leviathanch who suggested this workaround. Make sure to check out his blog!

Leave a Comment

Your email address will not be published.