(note) Linux increases the non-standard baud rate method

Source: Internet
Author: User
Tags posix

1. Kernel modification
The kernel files involved include DRIVER/CHAR/TTY_IOCTL.C and arch/xx/include/asm/termbits.h

In the Linux kernel, the c_cflags of the struct Ktermios structure has a total of 5 bits to mark the baud rate, where bit cbaudex indicates whether the POSIX standard baud rate or the extended baud rate are used,
POSIX prescribes 16 standard baud rates for b0,b50,b75,b110,b134,b150,b200,b300,b600,b1200,b1800,b2400,b4800,b9600,b19200,b38400, but unfortunately, Right now
The USB to serial chip can work with a baud rate far greater than B38400 (e.g. PL2303 maximum baud rate can be up to 12M bps), these non-standard baud rates can only be used using the CBAUDEX flag.
There is no doubt that the 5-bit value can support up to 32 different baud rates.

For example, for pl2303, the driver supports 3m/6m/12m bps, so you can add three baud rate b3000000,b6000000,b12000000 to the kernel.
(1) Modify the DRIVER/CHAR/TTY_IOCTL.C in the baud_table[] and baud_bits[] for the following code:
static const speed_t baud_table[] =
{
0,50,75,110,134,150,200,300,600,1200,1800,2400,4800,9600,19200,38400,/*the POSIX std*/
57600,115200,
3000000,6000000,12000000/*pl2303 ext*/
};
static const tcflag_t baud_bits[] =
{
B0,b50,b75,b110,b134,b150,b200,b300,b600,b1200,b1800,b2400,b4800,b9600,b19200,b38400,b57600,b115200,/*the POSIX std*/
b3000000,b6000000,b12000000/*pl2303 ext*/
};
(2) The BXXXX definition for modifying arch/xx/include/asm/termbits.h is as follows:
#define B0 0000000/* Hang up */
#define B50 0000001
#define B75 0000002
#define B110 0000003
#define B134 0000004
#define B150 0000005
#define B200 0000006
#define B300 0000007
#define B600 0000010
#define B1200 0000011
#define B1800 0000012
#define B2400 0000013
#define B4800 0000014
#define B9600 0000015
#define B19200 0000016
#define B38400 0000017

#define BOTHER 0010000
#define B57600 0010001
#define B115200 0010002


  #define b3000000        0010003
  #define b6000000         0010004
  #define b12000000       0010005
  /* After the baud rate I did not use, but if not defined the kernel cannot compile through!*/
  #define    b230400        0010006
  #define    b460800       0010007
  #define    b500000       0010010
  #define    b576000       0010011
  #define    b921600        0010012

(3) Recompile the kernel, reboot the new kernel will support b3000000/b6000000/b12000000 baud rate
2. Application Layer Modification
Cfsetispeed () and Cfsetospeed () in libc do not support non-standard baud rates, so calling these functions returns 1, and the application needs to use the following method:
if (baud_rate <= B38400)
{
Cfsetispeed (&opt,baud_rate);
Cfsetospeed (&opt,baud_rate);
}
Else
{
Opt.c_cflag |= Cbaudex;
Baud_rate-= 4096;/*baud_rate values are 1 ~ 5, respectively, corresponding to b57600/b115200/b3000000/b6000000/b12000000*/
Cfsetispeed (&opt,baud_rate);
Cfsetospeed (&opt,baud_rate);
}

(note) Linux increases the non-standard baud rate method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.