world leader in high performance signal processing
Trace: » serial_programming

Working with Serial Devices

The serial driver for the Blackfin works like any other serial driver for Linux, and can be controlled with the termio, as defined, in the termios.h man page.

For detailed information, check out the Serial Programming HOWTO or the Serial Programming Guide for POSIX Operating Systems.

Serial ports on different devices are called different names (to ensure the proper driver is being used). Here are a few examples:

  • ttyS0 - the first serial port on the 8250 (and compatibles) - normally on your host.
  • ttyS1 - the second serial port on the 8250 (and compatibles) - normally on your host.
  • ttyUSB0 - the first USB serial device
  • ttyBF0 - the first serial port on the Blackfin (this is not always uart0, but the first serial device which is enumerated by the kernel).
  • ttyAT0 - the first serial port on the Atmel AVR32.
  • ttyPSC - the first serial port on the Freescale mpc52xx.
  • ttySA - the first serial port on the Marvel SA1100.

Every serial driver should have it's own name, and the one most people are familiar with are the hosts' 8250 driver, which exposes /dev/ttyS0. On a your embedded target - it will be different.

This code example runs, and was borrowed from, and show be able to provide a basic info on how to use/control the serial port.

/* termstuff.c - omnibus demo of various terminal operations. */
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <stdlib.h>
int main()
        struct termios oldmodes, newmodes;
        speed_t speed;
        tcflag_t iflag[] = { ICRNL, ISTRIP, BRKINT, IGNPAR, IMAXBEL, IXANY, 0 };
        char * iflagname[] = {"ICRNL", "ISTRIP", "BRKINT", "IGNPAR", "IMAXBEL",
                        "IXANY" , (char *) 0};
        char buf[81];
        int i;
        tcflag_t oflag[] = { OCRNL, OLCUC, ONLCR, 0 };
        char * oflagname[] = {"OCRNL", "OLCUC", "ONLCR", (char *) 0};
        /* Don't allow cheating. */
        if( !isatty(0))
                fprintf(stderr,"stdin must be a terminal\n");
                return 1;
        /* Terminal name functions. */
        printf("ctermid(NULL) = %s\n", ctermid(NULL));
        printf("ttyname(0) = %s\n", ttyname(0));
        /* Of course, the easy way to see how your terminal is set up is */
        system("stty -a");
        if( tcgetattr(0, &oldmodes) < 0)
                return 1;
        /* Look at some iflag settings. */
        for( i = 0; iflag[i] != NULL; i ++)
                printf("%s is %s\n",
                         iflag[i] & oldmodes.c_iflag ? "on" : "off");
        /* Turn off ICRNL and see what happens. */
        printf("Enter won't work as usual - use ^J to get a line accepted.\n");
        printf("About to read two lines:\n");
        newmodes = oldmodes;
        newmodes.c_iflag &= ~ICRNL;
        tcsetattr(0, TCSANOW, &newmodes);
        tcsetattr(0, TCSANOW, &oldmodes);
        printf("OK, ICRNL now back on.\n");
        printf("Reading another line:\n");
        /* Look at some oflag settings. */
        for( i = 0; oflag[i] != NULL; i ++)
                printf("%s is %s\n",
                         oflag[i] & oldmodes.c_oflag ? "on" : "off");
        /* Turn ONLCR off and see what happens. */
        newmodes = oldmodes;
        newmodes.c_oflag &= ~ONLCR;
        tcsetattr(0, TCSANOW, &newmodes);
        printf("There is a \\n right here =>\n");
        printf("This would be a new line\n");
        tcsetattr(0, TCSANOW, &oldmodes);
        printf("Are we back to normal?\n");
        printf("Are we?\n");
        /* Turn ECHO off. */
        newmodes = oldmodes;
        newmodes.c_lflag &= ~ECHO;
        tcsetattr(0, TCSANOW, &newmodes);
        printf("Enter something: ");
        printf("You entered %s\n", buf);
        tcsetattr(0, TCSANOW, &oldmodes);
        /* get baudrate */
        speed = cfgetospeed(&newmodes);
        printf("currently at ");
        switch (speed) {
            case B57600:  printf("57600");  break;
            case B115200: printf("115200"); break;
            default:      printf("unhandled");
        printf(" baud\n");
        /* set baudrate to 9600 */
        cfsetispeed(&newmodes, B9600);
        cfsetospeed(&newmodes, B9600);
        tcsetattr(0, TCSADRAIN, &newmodes);
        /* Turn ECHOCTL on. */
        newmodes = oldmodes;
        newmodes.c_lflag |= ECHOCTL;
        tcsetattr(0, TCSANOW, &newmodes);
        tcsetattr(0, TCSANOW, &oldmodes);
        return 0;