1、tty数据发送调用关系
怎么样才能找到发送数据所使用的函数呢?打开uart_register_driver函数,找到里面的tty_register_driver,转到定义,这里调用了tty_fops这个结构,这几结构里就保存了读写串口的函数tty_write:
static const struct file_operations tty_fops = {
.llseek = no_llseek,
.read = tty_read,
.write = tty_write,
.poll = tty_poll,
.unlocked_ioctl = tty_ioctl,
.compat_ioctl = tty_compat_ioctl,
.open = tty_open,
.release = tty_release,
.fasync = tty_fasync,
};
tty_write的函数又调用n_tty_write函数来实现,n_tty_write又调用uart_write函数,最终找到串口驱动程序中的s3c24xx_serial_start_tx,来实现串口的方式数据
static void s3c24xx_serial_start_tx(struct uart_port *port)
{
struct s3c24xx_uart_port *ourport = to_ourport(port);
if (!tx_enabled(port)) {
if (port->flags & UPF_CONS_FLOW)
s3c24xx_serial_rx_disable(port);
enable_irq(ourport->tx_irq);
tx_enabled(port) = 1;
}
}
分析这段代码可知知道,这里做的工作是判断串口是否打开,如果没有打开,这把它打开并使能中断。这里面并没有哪些代码时实现数据发送的,那数据是在哪里发送的呢?其实是在中断函数s3c24xx_serial_tx_chars里面发送数据的!
在分析发送函数之还需要介绍一下循环缓冲,循环缓冲存放的就是需要发送的数据,中断程序s3c24xx_serial_tx_chars拿到循环缓冲里的数据,然后把它发送出去,那循环缓冲的数据又是什么时候写进去的呢?其实是在调用uart_write的时候写进去的。总结一下,发送流程如下:
应用程序调用write->tty_write->n_tty_write->uart_write->uart_start->s3c244xx_serial_start_tx(打开中断)->s3c24xx_serial_tx_chars(发送数据)。接下来分析s3c24xx_serial_tx_chars
2、串口发送函数分析
static irqreturn_t s3c24xx_serial_tx_chars(int irq, void *id)
{
struct s3c24xx_uart_port *ourport = id;
struct uart_port *port = &ourport->port;
struct circ_buf *xmit = &port->state->xmit;
int count = 256;
if (port->x_char) {
wr_regb(port, S3C2410_UTXH, port->x_char);
port->icount.tx++;
port->x_char = 0;
goto out;
}
/* if there isnt anything more to transmit, or the uart is now
* stopped, disable the uart and exit
*/
if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
s3c24xx_serial_stop_tx(port);
goto out;
}
/* try and drain the buffer... */
while (!uart_circ_empty(xmit) && count-- > 0) {
if (rd_regl(port, S3C2410_UFSTAT) & ourport->info->tx_fifofull)
break;
wr_regb(port, S3C2410_UTXH, xmit->buf[xmit->tail]);
xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
port->icount.tx++;
}
if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
uart_write_wakeup(port);
if (uart_circ_empty(xmit))
s3c24xx_serial_stop_tx(port);
out:
return IRQ_HANDLED;
}
分析代码可知:
1、判断x_char里面是否有数据,如果有数据把它发送然后退出。
2、判断循环缓冲是否为空,或者串口不允许发送,则把中断关闭
3、利用while循环发送数据,这里面循环的条件是:1循环缓冲不为空,2发送的数据量不到256。然后读取S3C2410_UFSTAT,判断发送的fifo是否满了,如果满了要退出,没有满就从循环缓冲的尾部去除数据,写入S3C2410_UTXH。然后调整循环缓冲的位置。
4、最后完成扫尾工作,如果循环缓冲里面的数据小于WAKEUP_CHARS(256),则唤醒之前阻塞的发送进程,同时如果循环缓冲为空了,把发送中断关闭。