栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

AM335x Linux uart 串口(rs485&rs232)无法正常通信的一种解决方法

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

AM335x Linux uart 串口(rs485&rs232)无法正常通信的一种解决方法

先大概说说我是如何解决rs485(uart4)&rs233(uart5)的通信问题。

首先,在kernel-AGV/drivers/tty/serial/omap-serial.c中确定串口的默认时钟频率。

接着,在kernel/arch/arm/mach-omap2/board-ipc335x.c和kernel/arch/arm/mach-omap2/mux33xx.c中查看uart4(和流控脚)&uart5的配置;说明一下,board-ipc335x.c这个文件是根据evm板进行的更改,与各位的板级配置文件名可能不一致。

 

 

 

 从而确认板级配置没有问题。

进一步的,查看uart_omap_port_set_rts_gpio函数的定义,将gpio设置成rts_gpio;

 重点查看drivers/tty/serial目录,找到8250.c文件。通过更改nr_uarts的赋值达成目的,

static unsigned int nr_uarts = 5; //支持uart0~5
static void __init serial8250_isa_init_ports(void)
{
	struct uart_8250_port *up;
	static int first = 1;
	int i, irqflag = 0;

	if (!first)
		return;
	first = 0;

	for (i = 0; i < nr_uarts; i++) {
		struct uart_8250_port *up = &serial8250_ports[i];

		up->port.line = i;
		spin_lock_init(&up->port.lock);

		init_timer(&up->timer);
		up->timer.function = serial8250_timeout;

		
		up->mcr_mask = ~ALPHA_KLUDGE_MCR;
		up->mcr_force = ALPHA_KLUDGE_MCR;

		up->port.ops = &serial8250_pops;
	}

	if (share_irqs)
		irqflag = IRQF_SHARED;

	for (i = 0, up = serial8250_ports;
	     i < ARRAY_SIZE(old_serial_port) && i < nr_uarts;
	     i++, up++) {
		up->port.iobase   = old_serial_port[i].port;
		up->port.irq      = irq_canonicalize(old_serial_port[i].irq);
		up->port.irqflags = old_serial_port[i].irqflags;
		up->port.uartclk  = old_serial_port[i].baud_base * 16;
		up->port.flags    = old_serial_port[i].flags;
		up->port.hub6     = old_serial_port[i].hub6;
		up->port.membase  = old_serial_port[i].iomem_base;
		up->port.iotype   = old_serial_port[i].io_type;
		up->port.regshift = old_serial_port[i].iomem_reg_shift;
		set_io_from_upio(&up->port);
		up->port.irqflags |= irqflag;
		if (serial8250_isa_config != NULL)
			serial8250_isa_config(i, &up->port, &up->capabilities);

	}
}

此时uart4&uart5虽然可以正常使用,但是rs485(uart4)仍然不能正常通讯,最后查阅documentation目录下的serial-rs485.txt;如下:

                        RS485 SERIAL COMMUNICATIONS

1. INTRODUCTION

   EIA-485, also known as TIA/EIA-485 or RS-485, is a standard defining the
   electrical characteristics of drivers and receivers for use in balanced
   digital multipoint systems.
   This standard is widely used for communications in industrial automation
   because it can be used effectively over long distances and in electrically
   noisy environments.

2. HARDWARE-RELATED CONSIDERATIONS

   Some CPUs/UARTs (e.g., Atmel AT91 or 16C950 UART) contain a built-in
   half-duplex mode capable of automatically controlling line direction by
   toggling RTS or DTR signals. That can be used to control external
   half-duplex hardware like an RS485 transceiver or any RS232-connected
   half-duplex devices like some modems.

   For these microcontrollers, the Linux driver should be made capable of
   working in both modes, and proper ioctls (see later) should be made
   available at user-level to allow switching from one mode to the other, and
   vice versa.

3. DATA STRUCTURES ALREADY AVAILABLE IN THE KERNEL

   The Linux kernel provides the serial_rs485 structure (see [1]) to handle
   RS485 communications. This data structure is used to set and configure RS485
   parameters in the platform data and in ioctls.

   The device tree can also provide RS485 boot time parameters (see [2]
   for bindings). The driver is in charge of filling this data structure from
   the values given by the device tree.

   Any driver for devices capable of working both as RS232 and RS485 should
   provide at least the following ioctls:

    - TIOCSRS485 (typically associated with number 0x542F). This ioctl is used
      to enable/disable RS485 mode from user-space

    - TIOCGRS485 (typically associated with number 0x542E). This ioctl is used
      to get RS485 mode from kernel-space (i.e., driver) to user-space.

   In other words, the serial driver should contain a code similar to the next
   one:

    static struct uart_ops atmel_pops = {
        
        .ioctl        = handle_ioctl,
    };

    static int handle_ioctl(struct uart_port *port,
        unsigned int cmd,
        unsigned long arg)
    {
        struct serial_rs485 rs485conf;

        switch (cmd) {
        case TIOCSRS485:
            if (copy_from_user(&rs485conf,
                (struct serial_rs485 *) arg,
                sizeof(rs485conf)))
                    return -EFAULT;

            
            break;

        case TIOCGRS485:
            if (copy_to_user((struct serial_rs485 *) arg,
                ...,
                sizeof(rs485conf)))
                    return -EFAULT;
            
            break;

        
        }
    }


4. USAGE FROM USER-LEVEL

   From user-level, RS485 configuration can be get/set using the previous
   ioctls. For instance, to set RS485 you can use the following code:

    #include

    
    #define TIOCGRS485      0x542E
    #define TIOCSRS485      0x542F

    
    int fd = open ("/dev/mydevice", O_RDWR);
    if (fd < 0) {
        
    }

    struct serial_rs485 rs485conf;

    
    rs485conf.flags |= SER_RS485_ENABLED;

    
    rs485conf.flags |= SER_RS485_RTS_ON_SEND;
    
    rs485conf.flags &= ~(SER_RS485_RTS_ON_SEND);

    
    rs485conf.flags |= SER_RS485_RTS_AFTER_SEND;
    
    rs485conf.flags &= ~(SER_RS485_RTS_AFTER_SEND);

    
    rs485conf.delay_rts_before_send = ...;

    
    rs485conf.delay_rts_after_send = ...;

    
    rs485conf.flags |= SER_RS485_RX_DURING_TX;

    if (ioctl (fd, TIOCSRS485, &rs485conf) < 0) {
        
    }

    

    
    if (close (fd) < 0) {
        
    }

5. REFERENCES

 [1]    include/linux/serial.h
 [2]    Documentation/devicetree/bindings/serial/rs485.txt

根据serial-rs485.txt给出的提示,在Linux下编译下面的代码,将生成的二进制可执行文件放入AM335x内核的文件系统中,执行它即可实现rs485通信(之所以需要执行该程序,是因为底层驱动配置中没有配置对应uart作为rs485的功能)。

#include   
#include   
#include   
#include   
#include   
#include 
#include   
#include   
#include   
#include 

  
#define FALSE -1  
#define TRUE 0  

#define TIOCGRS485      0x542E
#define TIOCSRS485      0x542F
  
void set_speed(int fd, int speed){  
  int speed_arr[] = { B115200, B38400, B19200, B9600, B4800, B2400, B1200, B300,B38400, B19200, B9600, B4800, B2400, B1200, B300, };  
  int name_arr[] = { 115200, 38400,  19200,  9600,  4800,  2400,  1200,  300, 38400, 19200,  9600, 4800, 2400, 1200,  300, };  
  int   i;   
  int   status;   
  struct termios   Opt;  
  tcgetattr(fd, &Opt);   
  for ( i= 0;  i < sizeof(speed_arr) / sizeof(int);  i++) {   
    if  (speed == name_arr[i]) {       
      tcflush(fd, TCIOFLUSH);       
      cfsetispeed(&Opt, speed_arr[i]);    
      cfsetospeed(&Opt, speed_arr[i]);     
      status = tcsetattr(fd, TCSANOW, &Opt);    
      if  (status != 0) {          
        perror("tcsetattr fd1");    
        return;       
      }      
      tcflush(fd,TCIOFLUSH); 
    }    
  }  
}  
  
int set_Parity(int fd,int databits,int stopbits,int parity)  
{   
    struct termios options;   
    if  ( tcgetattr( fd,&options)  !=  0) {   
        perror("SetupSerial 1");       
        return(FALSE);    
    }  
    options.c_cflag &= ~CSIZE;   
    switch (databits)   
    {     
    case 7:       
        options.c_cflag |= CS7;   
        break;  
    case 8:       
        options.c_cflag |= CS8;  
        break;     
    default:      
        fprintf(stderr,"Unsupported data sizen"); return (FALSE);    
    }  
    switch (parity)   
    {     
        case 'n':  
        case 'N':      
            options.c_cflag &= ~PARENB;     
            options.c_iflag &= ~INPCK;        
            break;    
        case 'o':     
        case 'O':       
            options.c_cflag |= (PARODD | PARENB);   
            options.c_iflag |= INPCK;                
            break;    
        case 'e':    
        case 'E':     
            options.c_cflag |= PARENB;           
            options.c_cflag &= ~PARODD;      
            options.c_iflag |= INPCK;         
            break;  
        case 'S':   
        case 's':       
            options.c_cflag &= ~PARENB;  
            options.c_cflag &= ~CSTOPB;break;    
        default:     
            fprintf(stderr,"Unsupported parityn");      
            return (FALSE);    
        }    
      
    switch (stopbits)  
    {     
        case 1:      
            options.c_cflag &= ~CSTOPB;    
            break;    
        case 2:      
            options.c_cflag |= CSTOPB;    
           break;  
        default:      
             fprintf(stderr,"Unsupported stop bitsn");    
             return (FALSE);   
    }   
       
    if (parity != 'n')     
        options.c_iflag |= INPCK;   
    tcflush(fd,TCIFLUSH);  
    options.c_cc[VTIME] = 150;   
    options.c_cc[VMIN] = 0;   
    if (tcsetattr(fd,TCSANOW,&options) != 0)     
    {   
        perror("SetupSerial 3");     
        return (FALSE);    
    }   
    return (TRUE);    
}

int read_datatty(int fd, unsigned char *rcv_buf, int TimeOut, int Len)
{
    int retval;
    fd_set rfds;
    struct timeval tv;
    int ret, pos;
    tv.tv_sec = TimeOut / 1000;  //set the rcv wait time
    tv.tv_usec = TimeOut % 1000 * 1000;  //100000us = 0.1s

    pos = 0;
    while (1)
    {
        FD_ZERO(&rfds);
        FD_SET(fd, &rfds);
        retval = select(fd + 1, &rfds, NULL, NULL, &tv);
        if (retval == -1)
        {
            perror("select()");
            break;
        }
        else if (retval)
        {
            ret = read(fd, rcv_buf + pos, 1);
            if (-1 == ret)
            {
                printf("read errorn");
                break;
            }

            pos++;
            if (Len <= pos)
            {
                break;
            }
        }
        else
        {
            printf("select_timeoutn");
            break;
        }
    }

    return pos;
}

  
int main()  
{   
    int fdO4; 
	int ret;

    fdO4 = open("/dev/ttyO4",O_RDWR);

	if(fdO4 == -1)  
    {  
        perror("serialport errorn");  
    }
    else  
    {  
        printf("open ");  
        printf("%s",ttyname(fdO4));  
        printf(" successfullyn");  
    }  
    struct serial_rs485 rs485conf;
    
	rs485conf.flags |= SER_RS485_ENABLED;
	
	rs485conf.flags |= SER_RS485_RTS_ON_SEND;
	
	rs485conf.flags &= ~(SER_RS485_RTS_AFTER_SEND);
	
	
	
	if (ioctl (fdO4, TIOCSRS485, &rs485conf) < 0) {
		 printf("ioctl Errorn");
	}
  
    set_speed(fdO4,115200);  

	if (set_Parity(fdO4,8,1,'N') == FALSE)  {  
        printf("Set Parity Errorn");  
        exit(-1);  
    }

    unsigned char recvbuf[20] = { 0 };
	const unsigned char sendbuf[] =
    { 0x08, 0x03, 0x00, 0x04, 0x00, 0x04, 0x05, 0x51 };

    ret = write(fdO4, sendbuf, 8);
    if (ret == -1)
    {
        printf("write device errorn");
    }
    else if (ret == 8)
    {
        printf("write_sucn");
    } 
    read_datatty(fdO4, recvbuf, 2000, 14);
    for(int i = 1; i < 14; i++) {
        printf("%x ",recvbuf[i]);
    }
    printf("n");

    close(fdO4);

	return 0;
}  



一阶段结束,然后我们来大致分析下uart初始化过程;

首先是uart驱动框架

uart驱动架构:

 

Linux tty subsystem:

在论述串口初始化之前,将简要说明一些重要的数据结构;顺便插一句,对于通用串口控制器,THR(发送保持寄存器)、RHR(接收保持寄存器)、IER(中断使能寄存器)、FCR(缓冲控制寄存器)、LCR(控制寄存器)、LSR(状态寄存器)、MCR(模式控制寄存器)、MSR(模式状态寄存器)

struct uart_driver : 用于描述串口结构,一个串口对应一个串口驱动

struct uart_driver {

struct module   *owner; //模块所有者

const char  *driver_name;   //驱动名

const char  *dev_name;  //设备名

int  major; //主设备号

int  minor; //次设备号

int  nr;    //支持串口个数

struct console  *cons;//控制台设备

//下面两个变量应被初始化为NULL

//tty_driver在函数register_uart_driver中被赋值,同时state也会被分配空间

//最后通过uart_add_one_port添加uart_state->uart_port

struct uart_state   *state; //串口状态,下层(串口驱动层)

struct tty_driver   *tty_driver; //tty相关

};

struct uart_port : 串口端口结构体,有几个串口就对应几个uart_port

struct uart_port {

spinlock_t  lock;

unsigned long   iobase; //io端口基地址(物理地址)

unsigned char __iomem   *membase; //io内存基地址(虚拟地址)

unsigned int    (*serial_in)(struct uart_port *, int); //串口读函数

void    (*serial_out)(struct uart_port *, int, int); //串口写方法

void    (*set_termios)(struct uart_port *,struct ktermios *new,struct ktermios *old); //串口配置方法函数

void    (*pm)(struct uart_port *, unsigned int state,unsigned int old);

unsigned int    irq;    //中断号

unsigned long   irqflags;   //中断标志

unsigned int    uartclk;   //串口时钟

unsigned int    fifosize;   //fifo大小

unsigned char   x_char;

unsigned char   regshift;   //寄存器偏移值

unsigned char   iotype; //io访问类型

unsigned char   unused1;

unsigned int    read_status_mask;

unsigned int    ignore_status_mask;

struct uart_state   *state; //uart_state结构体

struct uart_icount  icount; //串口使用计数

struct console  *cons;  //console控制台

#if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(SUPPORT_SYSRQ)

unsigned long   sysrq;

#endif

upf_t   flags;

unsigned int    mctrl;

unsigned int    timeout;

unsigned int    type; //串口类型

const struct uart_ops   *ops;   //串口操作函数集

unsigned int    custom_divisor;

unsigned int    line;   //端口号

resource_size_t mapbase; //串口寄存器基地址(物理地址)

struct device   *dev;   //设备文件

unsigned char   hub6;

unsigned char   suspended;

unsigned char   irq_wake;

unsigned char   unused[2];

void    *private_data;

};

struct uart_ops : 串口相关操作函数集

struct uart_ops {

unsigned int    (*tx_empty)(struct uart_port *);    //发送缓冲区为空

void    (*set_mctrl)(struct uart_port *, unsigned int mctrl);   //设置串口modem控制模式

unsigned int    (*get_mctrl)(struct uart_port *);   //获取串口modem控制模式

void    (*stop_tx)(struct uart_port *); //停止发送

void    (*start_tx)(struct uart_port *);    //开始发送

void    (*send_xchar)(struct uart_port *, char ch);

void    (*stop_rx)(struct uart_port *); //停止接收

void    (*enable_ms)(struct uart_port *);   //使能modem状态信息

void    (*break_ctl)(struct uart_port *, int ctl);

int (*startup)(struct uart_port *); //打开串口

void    (*shutdown)(struct uart_port *);    //关闭串口

void    (*flush_buffer)(struct uart_port *);

void    (*set_termios)(struct uart_port *, struct ktermios *new,struct ktermios *old);  //设置串口参数

void    (*set_ldisc)(struct uart_port *, int new);

void    (*pm)(struct uart_port *, unsigned int state,unsigned int oldstate);

int (*set_wake)(struct uart_port *, unsigned int state);

const char *(*type)(struct uart_port *);

void    (*release_port)(struct uart_port *);    //释放端口

int (*request_port)(struct uart_port *);    //请求端口

void    (*config_port)(struct uart_port *, int);    //配置端口

int (*verify_port)(struct uart_port *, struct serial_struct *); //校验端口

int (*ioctl)(struct uart_port *, unsigned int, unsigned long);  //控制

#ifdef CONFIG_CONSOLE_POLL

void    (*poll_put_char)(struct uart_port *, unsigned char);

int (*poll_get_char)(struct uart_port *);

#endif

};

struct uart_state : 描述串口状态信息

struct uart_state {

struct tty_port port;

int     pm_state; //电源状态

struct circ_buf xmit; //数据缓冲区

struct tasklet_struct   tlet;

struct uart_port    *uart_port;//指向对应的串口结构

};

uart初始化:

大致为:分配一个struct uart_driver用于定义并描述串口,再调用uart_register_driver注册串口驱动,最后调用uart_add_one_port添加端口到串口设备。

static struct uart_driver serial8250_reg = {

.owner                  = THIS_MODULE,

.driver_name         = “serial”,

.dev_name            = “ttyS”,

.major                   = TTY_MAJAOR,

.minor                  = 64,

.cons                    = SERIAL8250_CONSOLE,

};

static int __init serial8250_init(void)

{

       int ret;

       if (nr_uarts > UART_NR)

              nr_uarts = UART_NR;

       printk(KERN_INFO "Serial: 8250/16550 driver, "

              "%d ports, IRQ sharing %sabledn", nr_uarts,

              share_irqs ? "en" : "dis");

#ifdef CONFIG_SPARC

       ret = sunserial_register_minors(&serial8250_reg, UART_NR);

#else

       serial8250_reg.nr = UART_NR; //串口数量

       ret = uart_register_driver(&serial8250_reg); //注册串口驱动

#endif

       if (ret)

              goto out;

       serial8250_isa_devs = platform_device_alloc("serial8250",

                                              PLAT8250_DEV_LEGACY);

       if (!serial8250_isa_devs) {

              ret = -ENOMEM;

              goto unreg_uart_drv;

       }

       ret = platform_device_add(serial8250_isa_devs);

       if (ret)

              goto put_dev;

       serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev);

       ret = platform_driver_register(&serial8250_isa_driver);

       if (ret == 0)

              goto out;

       platform_device_del(serial8250_isa_devs);

put_dev:

       platform_device_put(serial8250_isa_devs);

unreg_uart_drv:

#ifdef CONFIG_SPARC

       sunserial_unregister_minors(&serial8250_reg, UART_NR);

#else

       uart_unregister_driver(&serial8250_reg);

#endif

out:

       return ret;

}

int uart_register_driver(struct uart_driver *drv)

{

       struct tty_driver *normal;

       int i, retval;

       BUG_ON(drv->state);

      

       drv->state = kzalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);

       if (!drv->state)

              goto out;

       normal = alloc_tty_driver(drv->nr);

       if (!normal)

              goto out_kfree;

       drv->tty_driver = normal; //赋值给uart_driver结构的tty_driver成员

       //对tty_driver成员指向的结构初始化

       normal->owner           = drv->owner;

       normal->driver_name  = drv->driver_name;

       normal->name            = drv->dev_name;

       normal->major            = drv->major;

       normal->minor_start   = drv->minor;

       normal->type              = TTY_DRIVER_TYPE_SERIAL;

       normal->subtype        = SERIAL_TYPE_NORMAL;

       normal->init_termios   = tty_std_termios; // drivers/tty/tty_io.c

       normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;

       normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;

       normal->flags             = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;

       normal->driver_state    = drv;

       //将tty_driver结构的normal操作指向uart_ops ,即tty_ops封装uart_ops

tty_set_operations(normal, &uart_ops);

      

       for (i = 0; i < drv->nr; i++) {

              struct uart_state *state = drv->state + i;

              struct tty_port *port = &state->port;

              tty_port_init(port);

              port->ops = &uart_port_ops;

              port->close_delay     = 500;   

              port->closing_wait    = 30000;

       }

       retval = tty_register_driver(normal);

       if (retval >= 0)

              return retval;

       put_tty_driver(normal);

out_kfree:

       kfree(drv->state);

out:

       return -ENOMEM;

}

//初始化uart_port

static void __init serial8250_isa_init_ports(void)

{

       struct uart_8250_port *up;

       static int first = 1;

       int i, irqflag = 0;

       if (!first)

              return;

       first = 0;

       for (i = 0; i < nr_uarts; i++) {

              struct uart_8250_port *up = &serial8250_ports[i];

              up->port.line = i; //串口号,i==0对应串口0

              spin_lock_init(&up->port.lock);

              init_timer(&up->timer); //初始化定时器

              up->timer.function = serial8250_timeout; //初始化定时器超时函数

             

              up->mcr_mask = ~ALPHA_KLUDGE_MCR;

              up->mcr_force = ALPHA_KLUDGE_MCR;

              up->port.ops = &serial8250_pops;

       }

       if (share_irqs) //中断是否共享

              irqflag = IRQF_SHARED;

       for (i = 0, up = serial8250_ports;

            i < ARRAY_SIZE(old_serial_port) && i < nr_uarts;

            i++, up++) {

              up->port.iobase   = old_serial_port[i].port;

              up->port.irq      = irq_canonicalize(old_serial_port[i].irq);

              up->port.irqflags = old_serial_port[i].irqflags;

              up->port.uartclk  = old_serial_port[i].baud_base * 16;

              up->port.flags    = old_serial_port[i].flags;

              up->port.hub6     = old_serial_port[i].hub6;

              up->port.membase  = old_serial_port[i].iomem_base;

              up->port.iotype   = old_serial_port[i].io_type;

              up->port.regshift = old_serial_port[i].iomem_reg_shift;

              set_io_from_upio(&up->port);

              up->port.irqflags |= irqflag;

              if (serial8250_isa_config != NULL)

                     serial8250_isa_config(i, &up->port, &up->capabilities);

       }

}

int uart_add_one_port(struct uart_driver *drv, struct uart_port *uport)

{

       struct uart_state *state;

       struct tty_port *port;

       int ret = 0;

       struct device *tty_dev;

       BUG_ON(in_interrupt());

       if (uport->line >= drv->nr)

              return -EINVAL;

       state = drv->state + uport->line;

       port = &state->port;

       mutex_lock(&port_mutex);

       mutex_lock(&port->mutex);

       if (state->uart_port) {

              ret = -EINVAL;

              goto out;

       }

       state->uart_port = uport;

       state->pm_state = -1;

       uport->cons = drv->cons;

       uport->state = state;

      

       if (!(uart_console(uport) && (uport->cons->flags & CON_ENABLED))) {

              spin_lock_init(&uport->lock);

              lockdep_set_class(&uport->lock, &port_lock_key);

       }

//进行port的自动配置,在未进行serial8250_probe()时,串口的port->iobase(io基地址)、port->mapbase(串口寄存器基地址)、port->membase(io内存基地址)都为空,所以函数进去会立即返回,即未配置成功。

//当进行完serial8250_probe()函数时,还会调用uart_add_one_port(),再到这个函数配置时就会配置成功

       uart_configure_port(drv, state, uport);

      

       tty_dev = tty_register_device(drv->tty_driver, uport->line, uport->dev);

       if (likely(!IS_ERR(tty_dev))) {

              device_init_wakeup(tty_dev, 1);

              device_set_wakeup_enable(tty_dev, 0);

       } else

              printk(KERN_ERR "Cannot register tty device on line %dn",

                     uport->line);

      

       uport->flags &= ~UPF_DEAD;

 out:

       mutex_unlock(&port->mutex);

       mutex_unlock(&port_mutex);

       return ret;

}

static void

uart_configure_port(struct uart_driver *drv, struct uart_state *state,

                  struct uart_port *port)

{

       unsigned int flags;

      

       //未调用serial8250_probe()之前,会从这里直接返回

       if (!port->iobase && !port->mapbase && !port->membase)

              return;

      

       flags = 0;

       if (port->flags & UPF_AUTO_IRQ)

              flags |= UART_CONFIG_IRQ;

       if (port->flags & UPF_BOOT_AUTOCONF) {

              if (!(port->flags & UPF_FIXED_TYPE)) { //经过probe()函数,已设置该标志,不执行               

port->type = PORT_UNKNOWN;

                     flags |= UART_CONFIG_TYPE;

              }

//调用设备的自动配置函数,即serial8250_config_port()

              port->ops->config_port(port, flags); }

       if (port->type != PORT_UNKNOWN) {

              unsigned long flags;

              uart_report_port(drv, port); //打印串口信息

             

              uart_change_pm(state, 0);

             

              spin_lock_irqsave(&port->lock, flags);

              port->ops->set_mctrl(port, port->mctrl & TIOCM_DTR);

              spin_unlock_irqrestore(&port->lock, flags);

             

              if (port->cons && !(port->cons->flags & CON_ENABLED))

                     register_console(port->cons);

             

              if (!uart_console(port))

                     uart_change_pm(state, 3);

       }

}

static int __devinit serial8250_probe(struct platform_device *dev)

{

       struct plat_serial8250_port *p = dev->dev.platform_data;

       struct uart_port port;

       int ret, i, irqflag = 0;

       memset(&port, 0, sizeof(struct uart_port));

       if (share_irqs)

              irqflag = IRQF_SHARED;

       for (i = 0; p && p->flags != 0; p++, i++) {

              port.iobase           = p->iobase;

              port.membase      = p->membase;

              port.irq                 = p->irq;

              port.irqflags          = p->irqflags;

              port.uartclk           = p->uartclk;

              port.regshift          = p->regshift;

              port.iotype            = p->iotype;

              port.flags              = p->flags;

              port.mapbase       = p->mapbase;

              port.hub6             = p->hub6;

              port.private_data   = p->private_data;

              port.type               = p->type;

              port.serial_in         = p->serial_in;

              port.serial_out       = p->serial_out;

              port.handle_irq     = p->handle_irq;

              port.set_termios    = p->set_termios;

              port.pm                = p->pm;

              port.dev         = &dev->dev;

              port.irqflags   |= irqflag;

              ret = serial8250_register_port(&port);

              if (ret < 0) {

                     dev_err(&dev->dev, "unable to register port at index %d "

                            "(IO%lx MEM%llx IRQ%d): %dn", i,

                            p->iobase, (unsigned long long)p->mapbase,

                            p->irq, ret);

              }

       }

       return 0;

}

以上,基本就是uart初始化过程,当然忽略了platform_device、platform_drive;串口中的驱动分两层,首先是基于控制台的驱动,即serial8250_console,还有一个是基于UART的驱动,相当于我们在实际使用串口时,使用的set_termois,start_tx,stop_tx,start_rx,stop_rx等函数。本文只谈论了基于uart的驱动,跳过基于console的驱动原因是uart4&uart5并不用作为控制台。

文中存在不妥之处,还请指出,谢谢!

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/913393.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号