Personal tools

How to use VisionSOM-6ULL UART interface in command line and C programs

From SomLabs Wiki

Jump to: navigation, search

How to use VisionSOM-6ULL UART interface in command line and C programs


Configuring device tree to enable UART interface

To enable UART interface you need to add (or modify) interface definition, your dts file should contain:

&uart4 {
        pinctrl-names = "default";
        pinctrl-0 = <&pinctrl_uart4>;
        dma-names = "", "";
        status = "okay";
};

In iomux section add or modify UART pin definition:

                pinctrl_uart4: uart4grp {
                        fsl,pins = <
                                MX6UL_PAD_UART4_TX_DATA__UART4_DCE_TX      0x1b0b1
                                MX6UL_PAD_UART4_RX_DATA__UART4_DCE_RX      0x1b0b1
                        >;                                              
                };

It is basic configuration, only RX and TX lines are used. Those definitions may need modifications depending on which interface and pins you want to use. You can find more informations on customizing device tree in this tutorial.

Using UART interface with minicom terminal

If you want to check if VisionSOM-6ULL UART interface works you should connect pins DIO0 (UART4-RXD) and DIO1 (UART4-TXD) of Arduino connector. Install minicom:

root@somlabs:~# apt install minicom

Start minicom (note that imx UART4 is /dev/ttymxc3, UART3 is /dev/ttymxc2 and so on):

root@somlabs:~# minicom -D /dev/ttymxc3

Now you can see that if DIO0 and DIO1 are connected then minicom displays characters you enter, if you disconnect these lines then there will be no output. To exit minicom you need to press Ctrl+A, Z, X and Enter.

Using UART interface in C program

The example below configures UART interface, sends text message and reads it back.

#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>

int configureUART(int fd, int speed, int parity)
{
        struct termios tty;
        memset (&tty, 0, sizeof tty);
        tcgetattr (fd, &tty);

        cfsetospeed (&tty, speed);
        cfsetispeed (&tty, speed);

        tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;
        tty.c_iflag &= ~IGNBRK;
        tty.c_lflag = 0;
        tty.c_oflag = 0;
        tty.c_cc[VMIN]  = 0;
        tty.c_cc[VTIME] = 15;

        tty.c_iflag &= ~(IXON | IXOFF | IXANY);

        tty.c_cflag |= (CLOCAL | CREAD);
        tty.c_cflag &= ~(PARENB | PARODD);
        tty.c_cflag |= parity;
        tty.c_cflag &= ~CSTOPB;
        tty.c_cflag &= ~CRTSCTS;

        tcsetattr (fd, TCSANOW, &tty);
}

void setBlocking (int fd, int should_block)
{
        struct termios tty;
        memset (&tty, 0, sizeof tty);
        tcgetattr (fd, &tty);

        tty.c_cc[VMIN]  = should_block ? 1 : 0;
        tty.c_cc[VTIME] = 5;
        tcsetattr (fd, TCSANOW, &tty);
}

void main() {
        char buf [50];
        int fd = open ("/dev/ttymxc3", O_RDWR | O_NOCTTY | O_SYNC);

        configureUART(fd, B9600, 0);
        setBlocking (fd, 0);

        printf("Sending mesage...\n");

        sprintf(buf, "Hi! I am VisionSOM-6ULL!\n");
        write (fd, buf, strlen(buf));

        usleep (500000);

        printf("Receiving message...\n");
        int n = read (fd, buf, sizeof(buf));
        printf("%s", buf);
}