1 2 Previous Next 21 Replies Latest reply on Dec 9, 2015 11:55 AM by Hungrymind

    How to enable PIN0/PIN1 for UART usage (TX/RX)

    wenlong

      After launched the Edison, I could not find the USB device under /dev/ttyS0, I could control the PIN0/PIN1 via GPIO api.

      Is PIN0/PIN1 set up as GPIO by default? and are there any methods to switch them to UART mode? thanks

       

      I've checked the "gpio_mapping_color_14ww33.1.pdf" but it is a little hard for me to figure out the relationship.

        • 1. Re: How to enable PIN0/PIN1 for UART usage (TX/RX)
          DiegoV_Intel

          Hi wenlong,

           

          I'll check "gpio_mapping_color_14ww33.1.pdf" in order to post an accurate answer to you.

           

          Regards,

          Diego.

          • 2. Re: How to enable PIN0/PIN1 for UART usage (TX/RX)
            David_J_Hunt

            Hi Wenlong,

             

            The following commands should set up the correct pin multiplexing  to enable serial mode on Pins 0 an 1. The key part is the echo of 'mode1' into 'current_pinmux', as that sets the pins into UART mode, otherwise that may be in GPIO mode.

            echo 214 > /sys/class/gpio/export 2>&1

            echo high > /sys/class/gpio/gpio214/direction

            echo low > /sys/class/gpio/gpio214/direction

             

            echo 131 > /sys/class/gpio/export 2>&1

            echo mode1 > /sys/kernel/debug/gpio_debug/gpio131/current_pinmux

            echo 249 > /sys/class/gpio/export 2>&1

            echo high > /sys/class/gpio/gpio249/direction

            echo 1 > /sys/class/gpio/gpio249/value

            echo 217 > /sys/class/gpio/export 2>&1

            echo high > /sys/class/gpio/gpio217/direction

            echo 1 > /sys/class/gpio/gpio217/value

            echo out > /sys/class/gpio/gpio131/direction

            echo 0 > /sys/class/gpio/gpio131/value

             

             

            echo 130 > /sys/class/gpio/export 2>&1

            echo mode1 > /sys/kernel/debug/gpio_debug/gpio130/current_pinmux

            echo 248 > /sys/class/gpio/export 2>&1

            echo low > /sys/class/gpio/gpio248/direction

            echo 0 > /sys/class/gpio/gpio248/value

            echo 216 > /sys/class/gpio/export 2>&1

            echo in > /sys/class/gpio/gpio216/direction

            echo in > /sys/class/gpio/gpio130/direction

             

            echo high > /sys/class/gpio/gpio214/direction

             

             

               Pins 0 and 1 on the Arduino baseboard are at /dev/ttyMFD1, and a getty can be enabled to give a longin prompt on this port by editing /etc/inittab and adding the following line:

            T1:23:respawn:/sbin/getty -L ttyMFD1 115200 vt100

             

            Then, using a USB to serial cable, you can connect to the board and log in using minicom or putty, e.g.:

            minicom -b 115200 -D /dev/ttyUSBx

            Of course, you don't have to set up a getty, if you simply want to talk to another device serially.

             

            Hope this helps,

            Dave.

            1 of 1 people found this helpful
            • 3. Re: How to enable PIN0/PIN1 for UART usage (TX/RX)
              DiegoV_Intel

              Thanks for the info! wenlong try the David_J_Hunt's suggestion and tell us what were the results.

               

              Have a nice day!

              • 4. Re: How to enable PIN0/PIN1 for UART usage (TX/RX)
                David_J_Hunt

                Oh, by the way, the cable I'm using to connect in is a TTL-232R-3V3

                • 5. Re: How to enable PIN0/PIN1 for UART usage (TX/RX)
                  wenlong

                  Hi David & DiegoV,

                  That works, thanks  a lot

                  • 6. Re: How to enable PIN0/PIN1 for UART usage (TX/RX)
                    tjfr

                    Hi all,

                     

                    Is this working on Rx and Tx sides ?

                     

                    I've executed the gpio configurations as explained by David on the Edison , and opened a "microcom -p /dev/ttyMFD1 -s 115200"

                    On the computer : "minicom -D /dev/ttyUSB0 -b 115200"

                     

                    After that:

                    Edison->Computer communication is working and Computer->Edison is not.

                    I'm trying with two different USB-TTL cables and getting same results.

                     

                    Edit: same with Arduino sketch using Serial1 (Tx (Pin 1) working not Rx (Pin 0))

                    Serial is working with Rx and Tx

                     

                    Any suggestion ?

                     

                    Thanks, Thomas

                    • 7. Re: How to enable PIN0/PIN1 for UART usage (TX/RX)
                      David_J_Hunt

                      You might need to disable hardware flow control in minicom. If it's enabled, you might only see traffic in one direction.

                       

                      While in minicom do  CTRL-A  ->  O  -> Serial Port Setup  -> F  (should show No for Hardware flow control)

                       

                      Regards,

                      Dave.

                      • 8. Re: How to enable PIN0/PIN1 for UART usage (TX/RX)
                        tjfr

                        Thanks a lot

                        (hardware flow control was off in my minicom setup only)

                         

                        Best regards,

                        Thomas

                        • 9. Re: How to enable PIN0/PIN1 for UART usage (TX/RX)
                          freshbirdtww

                          Hi,i try the method you mentioned, and i'm sure that the command is right.

                           

                          and there is the code for serial.it anything wrong?

                           

                          #include<stdio.h>

                          #include<fcntl.h>

                          #include<errno.h>

                          #include<unistd.h>

                          #include<termios.h>

                          #include<sys/select.h>

                          #define BAUDRATE 115200

                          #define DEVICE "/dev/ttyMFD1"

                          int serial_read(int fd, char *str, unsigned int len, unsigned int timeout)

                          {

                              fd_set rfds;

                              struct timeval tv;

                              int ret;                 //每次读的结果

                              int sret;                //select检测结果

                              int readlen = 0;         //实际读到的字节数

                              char *ptr;

                           

                              ptr = str;               //读指针,每次移动,因为实际读出的长度和传入参数可能存在差异

                           

                              FD_ZERO(&rfds);          //清除文件描述符集合

                              FD_SET(fd, &rfds);       //将fd加入fds文件描述符,以待下面用select方法监听

                           

                              /*传入的timeout是ms级别的单位,这里需要转换为structtimeval结构*/

                              tv.tv_sec = timeout / 1000;

                              tv.tv_usec = (timeout % 1000) * 1000;

                           

                              /*开始读*/

                              while(readlen < len)

                              {

                                  sret = select(fd + 1, &rfds, NULL, NULL, &tv);

                           

                                  if(sret == -1)

                                  {

                                      perror("select:");

                                      break;

                                  }

                                  else if(sret > 0)

                                  {

                                      ret = read(fd, ptr, 1);

                                      printf("sec: %d, usec: %d\n", tv.tv_sec, tv.tv_usec);

                                      if(ret <= -1)

                                      {

                                          perror("read err:");

                                          break;

                                      }

                           

                                      else if(ret == 0)

                                      {

                                          break;

                                      }

                           

                                      readlen += ret;

                                      ptr += ret;

                                  }

                                  else

                                  {

                                      printf("timeout! \n");

                                      break;

                                  }

                           

                              }

                           

                              return readlen;

                          }

                           

                          /* OpenPort:Open Serial port1

                          * Return the file descriptor on success or -1 on error

                          *

                          */

                          int OpenPort(void)

                          {

                              int fd;

                           

                              fd = open(DEVICE, O_RDWR | O_NOCTTY | O_NDELAY);

                              if(fd == -1)

                              {

                                  printf("OpenPort:Unable to open /dev/ttyMFD1");

                              }

                              else

                              {

                                  fcntl(fd, F_SETFL, 0);

                                  return (fd);

                              }

                          }

                          int main(int argc, char **argv)

                          {

                              int fd;

                              struct termios oldtio, newtio;

                              char buf[100] = {};

                           

                               fd = OpenPort();

                               tcgetattr(fd, &oldtio);

                               bzero(&newtio, sizeof(newtio));

                              

                               newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;

                               newtio.c_iflag = IGNPAR | ICRNL;

                               newtio.c_oflag = 0;

                               newtio.c_lflag = ICANON;

                               newtio.c_cc[VINTR] = 0; /* Ctrl-c */

                               newtio.c_cc[VQUIT] = 0; /* Ctrl-\ */

                               newtio.c_cc[VERASE] = 0; /* del */

                               newtio.c_cc[VKILL] = 0; /* @ */

                               newtio.c_cc[VEOF] = 4; /* Ctrl-d */

                               newtio.c_cc[VTIME] = 0; /* inter-character timer unused */

                                                              /* 不使用字符间的计时器 */

                               newtio.c_cc[VMIN] = 1; /* blocking read until 1 character arrives */

                                                                 /* 阻塞,直到读取到一个字符 */

                               newtio.c_cc[VSWTC] = 0; /* '\0' */

                               newtio.c_cc[VSTART] = 0; /* Ctrl-q */

                               newtio.c_cc[VSTOP] = 0; /* Ctrl-s */

                               newtio.c_cc[VSUSP] = 0; /* Ctrl-z */

                               newtio.c_cc[VEOL] = 0; /* '\0' */

                               newtio.c_cc[VREPRINT] = 0; /* Ctrl-r */

                               newtio.c_cc[VDISCARD] = 0; /* Ctrl-u */

                               newtio.c_cc[VWERASE] = 0; /* Ctrl-w */

                               newtio.c_cc[VLNEXT] = 0; /* Ctrl-v */

                               newtio.c_cc[VEOL2] = 0; /* '\0' */

                           

                               tcflush(fd, TCIFLUSH);

                               tcsetattr(fd, TCSANOW, &newtio);

                           

                              int n = serial_read(fd, buf, 1, 3000);

                           

                              //int n = read(fd, buf, 2);

                              if(n < 0)

                              {

                                  fputs("read error", stderr);

                              }

                              printf("%s\n",buf);

                          }

                          • 10. Re: How to enable PIN0/PIN1 for UART usage (TX/RX)
                            David_J_Hunt

                            freshbirdtww,

                              Have you tried talking to the board with minicom or microcom? That would make sure that your serial connection is working first.

                            Regards

                            Dave @ Emutex

                            • 11. Re: How to enable PIN0/PIN1 for UART usage (TX/RX)
                              gzyulin

                              Hi David_J_Hunt, is it possible to config other unused GPIO as additional UART? Thank you.

                              • 12. Re: How to enable PIN0/PIN1 for UART usage (TX/RX)
                                David_J_Hunt

                                gzyulin,

                                From the Edison Arduino Guide:

                                 

                                 

                                There are two UARTs available, UART1 with flow control and UART2 without flow control. The UART1 interface is available on DIG0 and DIG1 pins. UART2 is connected to an FTDI FT232R UART to USB bridge.

                                 

                                So no, it's not possible.

                                Dave @ Emutex.

                                • 13. Re: How to enable PIN0/PIN1 for UART usage (TX/RX)
                                  arfoll

                                  Instead of some magic twiddling I'd recommend using this:

                                  mraa: uart.h File Reference

                                  mraa_uart_context uart;
                                  uart = mraa_uart_init(0);

                                   

                                  Job done. And it'll work on other boards like galileo etc

                                  • 14. Re: How to enable PIN0/PIN1 for UART usage (TX/RX)
                                    David_J_Hunt

                                    Arfoll,

                                        Yes, that's a good suggestion. mraa_uart_init() is  a good way to to this now. But I don't think it was available at the time of the original post.

                                    Dave.

                                    1 2 Previous Next