Linux串口驱动程序(4)-数据发送
Linux串口驱动程序(4)-数据发送
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,
};
ty_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->(struct tty_operations uart_ops)uart_write->uart_start->__uart_start->(struct s3c24xx_uart_port s3c24xx_serial_ports[CONFIG_SERIAL_SAMSUNG_UARTS] .ops->s3c24xx_serial_ops)->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;
// 判断x_char里面是否有数据,如果有数据把它发送然后退出
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]); // 从循环缓冲的尾部去除数据,写入S3C2410_UTXH
xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); // 调整循环缓冲的位置
port->icount.tx++;
}
if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
uart_write_wakeup(port); // 循环缓冲里面的数据小于WAKEUP_CHARS(256),则唤醒之前阻塞的发送进程
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(第14位),判断发送的fifo是否满了,如果(为1)满了要退出,没有满就从循环缓冲的尾部去除数据,写入S3C2410_UTXH。然后调整循环缓冲的位置。
4、最后完成扫尾工作,如果循环缓冲里面的数据小于WAKEUP_CHARS(256),则唤醒之前阻塞的发送进程,同时如果循环缓冲为空了,把发送中断关闭。
Linux串口驱动程序(4)-数据发送相关教程
Linux/Centos7部署SVN服务器
Linux/Centos7部署SVN服务器 Linux/Centos7部署SVN服务器 查看本机ip地址。 ifconfig 名称可以随便,协议SSH,主机为本机ip(第一步查到的)端口号22。 如下显示已连接。 yum -y install subversion mkdir -p /data/svn/repository svnadmin create /data/svn
Linux上使用Nginx步骤详解
Linux上使用Nginx步骤详解 在Linux上通过CRT安装并使用Nginx详细步骤解析。所有软件网盘链接在上一个文章中均有,有需要请自
linux修改root密码,添加新用户
linux修改root密码,添加新用户 1. 修改root密码 以root身份登陆,执行: passwd root 然后根据提示,输入新密码,再次输入新密码,系统会提示成功修改密码。 具体示例如下: [[emailprotected] ~]# passwd rootChanging password for user root.New UNIX pas
b85vista显卡驱动_在Windows Vista中回滚麻烦的设备驱动程序
b85vista显卡驱动_在Windows Vista中回滚麻烦的设备驱动程序 b85vista显卡驱动 If you’ve installed a new driver that is causing problems on your computer, you can easily roll back to the prior version of the driver with a few simple steps. 如
Linux--基本命令详解
Linux--基本命令详解 一:Linux命令基础 1.1:Shell脚本 Windows中有cmd,Linux中有Shell。 所有系统的内核都是C语言开发出来的。C语言面向过程,C++面向对象。C语言和C++同时存在。 所有语言的特性:源码文件–》编译器–》执行文件 1.2:Linux命令的分类
绑定变量窥视_如何从Linux命令行窥视二进制文件
绑定变量窥视_如何从Linux命令行窥视二进制文件 绑定变量窥视 fatmawati achmad zaenuri/ShutterstockFatmawati achmad zaenuri / Shutterstock Have a mystery file? The Linux file command will quickly tell you what type of file it is. If it’s a bin
如何使用Linux top命令(并了解其输出)
如何使用Linux top命令(并了解其输出) Fatmawati Achmad Zaenuri/ShutterstockFatmawati Achmad Zaenuri / Shutterstock The Linux top command is one of the stalwarts of Unix-like operating systems. Itsutilitarian display is packed with useful in
Linux(2.0)
Linux(2.0) Linux(2) 1.查看系统用户和用户组 查看系统用户 命令 cat /etc/passwd 结果 [[emailprotected] jack]# cat /etc/passwdroot:x:0:0:root:/root:/bin/bashjack:x:1000:1000:CentOS:/home/jack:/bin/bashstack:x:1001:1000::/home/stack:/bin/bash.