DESK-XZ7-L/Peripherals/UART

From DAVE Developer's Wiki
Jump to: navigation, search
History
Issue Date Notes

2024/01/23

DESK-XZ7-L-1.0.1 release
2024/06/12 DESK-XZ7-L-1.1.0 release


Peripheral UART[edit | edit source]

The Zynq-7000 second UART device (UART0) is routed through the PL and it is mapped to the ttyPS1 device. This feature is available in the Vivado project example using the TCL scripts here below:

  • recreate_prj_bora_BASE.tcl
  • recreate_prj_bora_ETH1.tcl
  • recreate_prj_boralite_BASE.tcl
  • recreate_prj_boralite_ETH1.tcl
  • recreate_prj_boralite_NAND.tcl
  • recreate_prj_borax_BASE.tcl
  • recreate_prj_borax_ETH1.tcl

Accessing the peripheral[edit | edit source]

Linux messages at boot time[edit | edit source]

...
[    0.198315] e0000000.serial: ttyPS1 at MMIO 0xe0000000 (irq = 34, base_baud = 3125000) is a xuartps
[    0.207202] e0001000.serial: ttyPS0 at MMIO 0xe0001000 (irq = 35, base_baud = 3125000) is a xuartps
...

Usage with stty[edit | edit source]

N.B. UART mapping respect to ttyPSX is the following one:

UART0 (RS232) <-> ttyPS1
UART1 (Serial Port) <-> ttyPS0
root@bora:~# stty -F /dev/ttyPS1 115200 -echo
root@bora:~# cat /dev/ttyPS1 &
[1] 297
root@bora:~# echo "Test loopback" > /dev/ttyPS1
root@bora:~# Test loopback

Helloworld from UART0[edit | edit source]

On target we need to loopback pin 6 and 7 of PMOD A.

Here below an example on C code for initializing and using UART0 through FPGA:

#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("%s", msg);

	set_interface_attribs (fd, B115200, 0);  // set speed to 115,200 bps, 8n1 (no parity)
	write(fd, msg, strlen(msg));

	exit(0);
}

For compile it, please use the following instruction:

dvdk@vagrant:~$ wget https://mirror.dave.eu/desk-xz-l/desk-xz7-l-1.0.1/desk-xz7-l-1.0.1_bora_sdk.sh
--2024-01-26 11:57:04--  https://mirror.dave.eu/desk-xz-l/desk-xz7-l-1.0.1/desk-xz7-l-1.0.1_bora_sdk.sh
Resolving mirror.dave.eu (mirror.dave.eu)... 84.46.251.143
Connecting to mirror.dave.eu (mirror.dave.eu)|84.46.251.143|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 998591008 (952M) [text/x-sh]
Saving to: ‘desk-xz7-l-1.0.1_bora_sdk.sh’

desk-xz7-l-1.0.1_bora_sdk.sh                    100%[====================================================================================================>] 952.33M  10.8MB/s    in 1m 40s  

2024-01-26 11:58:43 (9.57 MB/s) - ‘desk-xz7-l-1.0.1_bora_sdk.sh’ saved [998591008/998591008]

dvdk@vagrant:~$ chmod 755 desk-xz7-l-1.0.1_bora_sdk.sh 
dvdk@vagrant:~$ ./desk-xz7-l-1.0.1_bora_sdk.sh 
PetaLinux SDK installer version 2021.2
======================================
Enter target directory for SDK (default: /opt/petalinux/2021.2): 
You are about to install the SDK to "/opt/petalinux/2021.2". Proceed [Y/n]? y
Extracting SDK.......................................................................................................................................................................................................................done
Setting it up...done
SDK has been successfully set up and is ready to be used.
Each time you wish to use the SDK in a new shell session, you need to source the environment setup script e.g.
 $ . /opt/petalinux/2021.2/environment-setup-cortexa9t2hf-neon-xilinx-linux-gnueabi
dvdk@vagrant:~$ source /opt/petalinux/2021.2/environment-setup-cortexa9t2hf-neon-xilinx-linux-gnueabi 
dvdk@vagrant:~$ vi hello_UART0.c
dvdk@vagrant:~$ $CC -O hello_UART0.c -o hello_UART0

Copy hello_UART0 on target and perform the following command:

root@bora:~# chmod u+x hello_UART0
root@bora:~# ./hello_UART0
Hello World from BELK (FPGA PS0 UART)!

Additional information[edit | edit source]

Serial ports can be used through the standard serial programming API.

For detailed information, please refer to the Serial Programming HOWTO at Serial-Programming-HOWTO