📄 linux设备驱动程序学习(3-补)-linux中的循环缓冲区 - linux设备驱动程序 - tekkaman ninja.htm
字号:
style="COLOR: #000000"><FONT face=新宋体><SPAN
style="COLOR: #0000ff">unsigned</SPAN> <SPAN
style="COLOR: #0000ff">int</SPAN> kfifo_put<SPAN
style="COLOR: #0000cc">(</SPAN><SPAN
style="COLOR: #0000ff">struct</SPAN> kfifo <SPAN
style="COLOR: #0000cc">*</SPAN>fifo<SPAN
style="COLOR: #0000cc">,</SPAN><SPAN
style="COLOR: #0000ff">unsigned</SPAN> <SPAN
style="COLOR: #0000ff">char</SPAN> <SPAN
style="COLOR: #0000cc">*</SPAN>buffer<SPAN
style="COLOR: #0000cc">,</SPAN> <SPAN
style="COLOR: #0000ff">unsigned</SPAN> <SPAN
style="COLOR: #0000ff">int</SPAN> len<SPAN
style="COLOR: #0000cc">);</SPAN><BR><SPAN
style="COLOR: #0000ff">unsigned</SPAN> <SPAN
style="COLOR: #0000ff">int</SPAN>
__kfifo_put<SPAN
style="COLOR: #0000cc">(</SPAN><SPAN
style="COLOR: #0000ff">struct</SPAN> kfifo <SPAN
style="COLOR: #0000cc">*</SPAN>fifo<SPAN
style="COLOR: #0000cc">,</SPAN><SPAN
style="COLOR: #0000ff">unsigned</SPAN> <SPAN
style="COLOR: #0000ff">char</SPAN> <SPAN
style="COLOR: #0000cc">*</SPAN>buffer<SPAN
style="COLOR: #0000cc">,</SPAN> <SPAN
style="COLOR: #0000ff">unsigned</SPAN> <SPAN
style="COLOR: #0000ff">int</SPAN> len<SPAN
style="COLOR: #0000cc">)</SPAN><SPAN
style="COLOR: #0000cc">;</SPAN></FONT></SPAN></CODE></P></TD></TR></TBODY></TABLE></P>
<P><FONT face=新宋体>fifo:要写入数据的缓冲区结构体指针;</FONT></P>
<P><FONT
face=新宋体>buffer:要写入的数据指针,指向内核空间。如需要用户空间数据,之前要用copy_from_user复制数据到内核空间;</FONT></P>
<P><FONT face=新宋体>len:要写入的数据大小;</FONT></P>
<P><FONT face=新宋体><FONT color=#0000ff>返回值</FONT>
为写入缓冲区的数据字节数。</FONT></P>
<P><FONT face=新宋体>(3)从缓冲区里读出数据</FONT>
<TABLE style="BORDER-COLLAPSE: collapse"
borderColor=#999999 cellSpacing=0 cellPadding=0
width="95%" bgColor=#f1f1f1 border=1>
<TBODY>
<TR>
<TD>
<P
style="MARGIN: 5px; LINE-HEIGHT: 150%"><CODE><SPAN
style="COLOR: #000000"><FONT face=新宋体><SPAN
style="COLOR: #0000ff">unsigned</SPAN> <SPAN
style="COLOR: #0000ff">int</SPAN> kfifo_get<SPAN
style="COLOR: #0000cc">(</SPAN><SPAN
style="COLOR: #0000ff">struct</SPAN> kfifo <SPAN
style="COLOR: #0000cc">*</SPAN>fifo<SPAN
style="COLOR: #0000cc">,</SPAN> <SPAN
style="COLOR: #0000ff">unsigned</SPAN> <SPAN
style="COLOR: #0000ff">char</SPAN> <SPAN
style="COLOR: #0000cc">*</SPAN>buffer<SPAN
style="COLOR: #0000cc">,</SPAN> <SPAN
style="COLOR: #0000ff">unsigned</SPAN> <SPAN
style="COLOR: #0000ff">int</SPAN> len<SPAN
style="COLOR: #0000cc">)</SPAN><SPAN
style="COLOR: #0000cc">;</SPAN><BR><SPAN
style="COLOR: #0000ff">unsigned</SPAN> <SPAN
style="COLOR: #0000ff">int</SPAN>
__kfifo_get<SPAN
style="COLOR: #0000cc">(</SPAN><SPAN
style="COLOR: #0000ff">struct</SPAN> kfifo <SPAN
style="COLOR: #0000cc">*</SPAN>fifo<SPAN
style="COLOR: #0000cc">,</SPAN> <SPAN
style="COLOR: #0000ff">unsigned</SPAN> <SPAN
style="COLOR: #0000ff">char</SPAN> <SPAN
style="COLOR: #0000cc">*</SPAN>buffer<SPAN
style="COLOR: #0000cc">,</SPAN> <SPAN
style="COLOR: #0000ff">unsigned</SPAN> <SPAN
style="COLOR: #0000ff">int</SPAN> len<SPAN
style="COLOR: #0000cc">)</SPAN><SPAN
style="COLOR: #0000cc">;</SPAN></FONT></SPAN></CODE></P></TD></TR></TBODY></TABLE></P>
<P>参数定义和<FONT face=新宋体>kfifo_put类似。</FONT></P>
<P><FONT face=新宋体><FONT color=#0000ff>返回值</FONT>
为从缓冲区读出的数据字节数。</FONT></P>
<P><FONT face=新宋体>(4)得到缓冲区已有的数据字节数</FONT>
<TABLE style="BORDER-COLLAPSE: collapse"
borderColor=#999999 cellSpacing=0 cellPadding=0
width="95%" bgColor=#f1f1f1 border=1>
<TBODY>
<TR>
<TD>
<P
style="MARGIN: 5px; LINE-HEIGHT: 150%"><CODE><SPAN
style="COLOR: #000000"><FONT face=新宋体><SPAN
style="COLOR: #0000ff">unsigned</SPAN> <SPAN
style="COLOR: #0000ff">int</SPAN> kfifo_len<SPAN
style="COLOR: #0000cc">(</SPAN><SPAN
style="COLOR: #0000ff">struct</SPAN> kfifo <SPAN
style="COLOR: #0000cc">*</SPAN>fifo<SPAN
style="COLOR: #0000cc">)</SPAN><SPAN
style="COLOR: #0000cc">;</SPAN><BR><SPAN
style="COLOR: #0000ff">unsigned</SPAN> <SPAN
style="COLOR: #0000ff">int</SPAN>
__kfifo_len<SPAN
style="COLOR: #0000cc">(</SPAN><SPAN
style="COLOR: #0000ff">struct</SPAN> kfifo <SPAN
style="COLOR: #0000cc">*</SPAN>fifo<SPAN
style="COLOR: #0000cc">)</SPAN><SPAN
style="COLOR: #0000cc">;</SPAN></FONT></SPAN></CODE></P></TD></TR></TBODY></TABLE></P>
<P><FONT face=新宋体>fifo:要操作的缓冲区结构体指针;</FONT></P>
<P><FONT face=新宋体>函数返回缓冲区实际已有的数据字节数,内部实现十分简单,就是in
- out;</FONT></P>
<P><FONT face=新宋体><FONT color=#0000ff>返回值</FONT>
为缓冲区已有的数据字节数。</FONT></P>
<P><FONT face=新宋体>(5)清空缓冲区</FONT>
<TABLE style="BORDER-COLLAPSE: collapse"
borderColor=#999999 cellSpacing=0 cellPadding=0
width="95%" bgColor=#f1f1f1 border=1>
<TBODY>
<TR>
<TD>
<P
style="MARGIN: 5px; LINE-HEIGHT: 150%"><CODE><SPAN
style="COLOR: #000000"><FONT face=新宋体><SPAN
style="COLOR: #0000ff">void</SPAN>
__kfifo_reset<SPAN
style="COLOR: #0000cc">(</SPAN><SPAN
style="COLOR: #0000ff">struct</SPAN> kfifo <SPAN
style="COLOR: #0000cc">*</SPAN>fifo<SPAN
style="COLOR: #0000cc">)</SPAN><SPAN
style="COLOR: #0000cc">;</SPAN><BR><SPAN
style="COLOR: #0000ff">void</SPAN>
kfifo_reset<SPAN
style="COLOR: #0000cc">(</SPAN><SPAN
style="COLOR: #0000ff">struct</SPAN> kfifo <SPAN
style="COLOR: #0000cc">*</SPAN>fifo<SPAN
style="COLOR: #0000cc">)</SPAN><SPAN
style="COLOR: #0000cc">;</SPAN></FONT></SPAN></CODE></P></TD></TR></TBODY></TABLE></P>
<P><FONT face=新宋体>内部实现十分简单,就是in = out =
0。</FONT></P>
<P><FONT face=新宋体>(6)使用结束,释放缓冲区。</FONT>
<TABLE style="BORDER-COLLAPSE: collapse"
borderColor=#999999 cellSpacing=0 cellPadding=0
width="95%" bgColor=#f1f1f1 border=1>
<TBODY>
<TR>
<TD>
<P
style="MARGIN: 5px; LINE-HEIGHT: 150%"><CODE><SPAN
style="COLOR: #000000"><FONT face=新宋体><SPAN
style="COLOR: #0000ff">void</SPAN>
kfifo_free<SPAN
style="COLOR: #0000cc">(</SPAN><SPAN
style="COLOR: #0000ff">struct</SPAN> kfifo <SPAN
style="COLOR: #0000cc">*</SPAN>fifo<SPAN
style="COLOR: #0000cc">)</SPAN><SPAN
style="COLOR: #0000cc">;</SPAN></FONT></SPAN></CODE></P></TD></TR></TBODY></TABLE></P>
<P><FONT face=新宋体>所有的<SPAN lang=en-US><FONT
face=宋体>kfifo</FONT></SPAN></FONT><FONT
face="Courier New">提供的循环缓冲的函数就是这些。在理解内部实现原理的基础上才能更好的使用它,所以<FONT
color=#0000ff><STRONG>再次建议阅读源码,因为源码很简单,但是很精巧。</STRONG></FONT>
<HR id=null>
</FONT>
<P></P>
<P><FONT face=新宋体 color=#0000ff
size=4><STRONG>ARM9开发板实验</STRONG></FONT></P>
<P><STRONG><FONT face=新宋体 color=#0000ff>实验模块源码:<A
href="http://blogimg.chinaunix.net/blog/upfile2/071210215938.gz"
target=_blank><FONT face="Courier New"
size=4>scull-kfifo</FONT></A></FONT></STRONG></P>
<P><STRONG><FONT face=新宋体 color=#0000ff>测试程序源码:<A
href="http://blogimg.chinaunix.net/blog/upfile2/071210215949.gz"
target=_blank><FONT face="Courier New"
size=4>scull-kfifo-test</FONT></A></FONT></STRONG></P>
<P><STRONG><FONT color=#0000ff size=3><FONT
face=新宋体>实验现象:</FONT>
<TABLE style="BORDER-COLLAPSE: collapse"
borderColor=#999999 cellSpacing=0 cellPadding=0
width="95%" bgColor=#f1f1f1 border=1>
<TBODY>
<TR>
<TD>
<P
style="MARGIN: 5px; LINE-HEIGHT: 150%"><CODE><SPAN
style="COLOR: #000000"><FONT face=新宋体><SPAN
style="COLOR: #0000cc">[</SPAN>Tekkaman2440@SBC2440V4<SPAN
style="COLOR: #0000cc">]</SPAN><SPAN
style="COLOR: #0000cc">#</SPAN>cd <SPAN
style="COLOR: #0000cc">/</SPAN>lib<SPAN
style="COLOR: #0000cc">/</SPAN>modules<SPAN
style="COLOR: #0000cc">/</SPAN><BR><SPAN
style="COLOR: #0000cc">[</SPAN>Tekkaman2440@SBC2440V4<SPAN
style="COLOR: #0000cc">]</SPAN><SPAN
style="COLOR: #0000cc">#</SPAN>insmod
scull_kfifo<SPAN
style="COLOR: #0000cc">.</SPAN>ko<BR><SPAN
style="COLOR: #0000cc">[</SPAN>Tekkaman2440@SBC2440V4<SPAN
style="COLOR: #0000cc">]</SPAN><SPAN
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -