⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 linux设备驱动程序学习(10)-时间、延迟及延缓操作 - linux设备驱动程序 - tekkaman ninja.htm

📁 Linux设备驱动程序学习(10)-时间、延迟及延缓操作 - Linux设备驱动程序.rar
💻 HTM
📖 第 1 页 / 共 5 页
字号:
                              <P class=programlisting><FONT color=#0000ff 
                              size=3><STRONG>&nbsp;处理器特定的寄存器</STRONG></FONT></P>
                              <P 
                              class=programlisting>&nbsp;若需测量非常短时间间隔或需非常高的精度,可以借助平台依赖的资源。许多现代处理器包含一个随时钟周期不断递增的计数寄存器,他是进行高精度的时间管理任务唯一可靠的方法。最有名的计数器寄存器是 
                              TSC ( timestamp counter), 在 x86 的 Pentium 
                              处理器开始引入并在之后所有的 CPU 中出现(包括 x86_64 平台)。它是一个 64-位 
                              寄存器,计数 CPU 的时钟周期,可从内核和用户空间读取。在包含了 
                              &lt;asm/msr.h&gt; (一个 x86-特定的头文件, 
                              它的名子代表"machine-specific 
                              registers")的代码中可使用这些宏:<BR></P>
                              <P>&nbsp;</P>
                              <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: rgb(0,0,0)">rdtsc<SPAN 
                                style="COLOR: rgb(0,0,204)">(</SPAN>low32<SPAN 
                                style="COLOR: rgb(0,0,204)">,</SPAN>high32<SPAN 
                                style="COLOR: rgb(0,0,204)">)</SPAN><SPAN 
                                style="COLOR: rgb(0,0,204)">;</SPAN><SPAN 
                                style="COLOR: rgb(255,153,0)">/*原子地读取 64-位TSC 值到 
                                2 个 32-位 变量*/</SPAN><BR>rdtscl<SPAN 
                                style="COLOR: rgb(0,0,204)">(</SPAN>low32<SPAN 
                                style="COLOR: rgb(0,0,204)">)</SPAN><SPAN 
                                style="COLOR: rgb(0,0,204)">;</SPAN><SPAN 
                                style="COLOR: rgb(255,153,0)">/*读取TSC的低32位到一个 
                                32-位 变量*/</SPAN><BR>rdtscll<SPAN 
                                style="COLOR: rgb(0,0,204)">(</SPAN>var64<SPAN 
                                style="COLOR: rgb(0,0,204)">)</SPAN><SPAN 
                                style="COLOR: rgb(0,0,204)">;</SPAN><SPAN 
                                style="COLOR: rgb(255,153,0)">/*读 64-位TSC 值到一个 
                                long long 变量*/</SPAN><BR><BR><SPAN 
                                style="COLOR: rgb(255,153,0)">/*下面的代码行测量了指令自身的执行时间:*/</SPAN><BR><SPAN 
                                style="COLOR: rgb(0,0,255)">unsigned</SPAN> 
                                <SPAN style="COLOR: rgb(0,0,255)">long</SPAN> 
                                ini<SPAN style="COLOR: rgb(0,0,204)">,</SPAN> 
                                end<SPAN 
                                style="COLOR: rgb(0,0,204)">;</SPAN><BR>rdtscl<SPAN 
                                style="COLOR: rgb(0,0,204)">(</SPAN>ini<SPAN 
                                style="COLOR: rgb(0,0,204)">)</SPAN><SPAN 
                                style="COLOR: rgb(0,0,204)">;</SPAN> rdtscl<SPAN 
                                style="COLOR: rgb(0,0,204)">(</SPAN>end<SPAN 
                                style="COLOR: rgb(0,0,204)">)</SPAN><SPAN 
                                style="COLOR: rgb(0,0,204)">;</SPAN><BR>printk<SPAN 
                                style="COLOR: rgb(0,0,204)">(</SPAN><SPAN 
                                style="COLOR: rgb(255,0,255)">"time lapse: 
                                %li\n"</SPAN><SPAN 
                                style="COLOR: rgb(0,0,204)">,</SPAN> end <SPAN 
                                style="COLOR: rgb(0,0,204)">-</SPAN> ini<SPAN 
                                style="COLOR: rgb(0,0,204)">)</SPAN><SPAN 
                                style="COLOR: rgb(0,0,204)">;</SPAN></SPAN></CODE></P></TD></TR></TBODY></TABLE>
                              <P>一些其他的平台提供相似的功能, 并且内核头文件提供一个体系无关的功能用来代替 rdtsc,称 
                              get_cycles(定义在 &lt;asm/timex.h&gt;( 由 
                              &lt;linux/timex.h&gt; 包含)),原型如下: </P>
                              <P>
                              <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: rgb(0,0,0)"><FONT face=新宋体><SPAN 
                                style="COLOR: rgb(0,0,204)">#</SPAN><SPAN 
                                style="COLOR: rgb(255,0,0)">include</SPAN> <SPAN 
                                style="COLOR: rgb(0,0,204)">&lt;</SPAN>linux<SPAN 
                                style="COLOR: rgb(0,0,204)">/</SPAN>timex<SPAN 
                                style="COLOR: rgb(0,0,204)">.</SPAN>h<SPAN 
                                style="COLOR: rgb(0,0,204)">&gt;</SPAN><BR>&nbsp;cycles_t 
                                get_cycles<SPAN 
                                style="COLOR: rgb(0,0,204)">(</SPAN><SPAN 
                                style="COLOR: rgb(0,0,255)">void</SPAN><SPAN 
                                style="COLOR: rgb(0,0,204)">)</SPAN><SPAN 
                                style="COLOR: rgb(0,0,204)">;</SPAN> <BR><SPAN 
                                style="COLOR: rgb(255,153,0)">/*这个函数在每个平台都有定义, 
                                但在没有时钟周期计数器的平台上返回 0 */</SPAN><BR><BR><SPAN 
                                style="COLOR: rgb(255,153,0)">/*由于s3c2410系列处理器上没有时钟周期计数器所以get_cycles定义如下:*/</SPAN><BR><SPAN 
                                style="COLOR: rgb(0,0,255)">typedef</SPAN> <SPAN 
                                style="COLOR: rgb(0,0,255)">unsigned</SPAN> 
                                <SPAN style="COLOR: rgb(0,0,255)">long</SPAN> 
                                cycles_t<SPAN 
                                style="COLOR: rgb(0,0,204)">;</SPAN><BR><BR><SPAN 
                                style="COLOR: rgb(0,0,255)">static</SPAN> <SPAN 
                                style="COLOR: rgb(0,0,255)">inline</SPAN> 
                                cycles_t get_cycles <SPAN 
                                style="COLOR: rgb(0,0,204)">(</SPAN><SPAN 
                                style="COLOR: rgb(0,0,255)">void</SPAN><SPAN 
                                style="COLOR: rgb(0,0,204)">)</SPAN><BR><SPAN 
                                style="COLOR: rgb(0,0,204)">{</SPAN><BR>&nbsp;&nbsp;&nbsp;&nbsp;<SPAN 
                                style="COLOR: rgb(0,0,255)">return</SPAN> 0<SPAN 
                                style="COLOR: rgb(0,0,204)">;</SPAN><BR><SPAN 
                                style="COLOR: rgb(0,0,204)">}</SPAN></FONT></SPAN></CODE></P></TD></TR></TBODY></TABLE></P>
                              <P></P>
                              <HR id=null>

                              <P></P>
                              <P></P>
                              <P><FONT color=#0000ff 
                              size=4><STRONG>获取当前时间</STRONG></FONT></P>
                              <P><FONT 
                              color=#000000>驱动一般无需知道时钟时间(用年月日、小时、分钟、秒来表达的时间),只对用户程序才需要,如 
                              cron 和 syslogd。 内核提供了一个将时钟时间转变为秒数值的函数:</FONT></P>
                              <P>&nbsp;</P>
                              <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: rgb(0,0,0)"><SPAN 
                                style="COLOR: rgb(0,0,255)">unsigned</SPAN> 
                                <SPAN 
                                style="COLOR: rgb(0,0,255)">long</SPAN><BR><SPAN 
                                style="COLOR: rgb(255,0,0)">mktime</SPAN><SPAN 
                                style="COLOR: rgb(0,0,204)">(</SPAN><SPAN 
                                style="COLOR: rgb(0,0,255)">const</SPAN> <SPAN 
                                style="COLOR: rgb(0,0,255)">unsigned</SPAN> 
                                <SPAN style="COLOR: rgb(0,0,255)">int</SPAN> 
                                year0<SPAN style="COLOR: rgb(0,0,204)">,</SPAN> 
                                <SPAN style="COLOR: rgb(0,0,255)">const</SPAN> 
                                <SPAN 
                                style="COLOR: rgb(0,0,255)">unsigned</SPAN> 
                                <SPAN style="COLOR: rgb(0,0,255)">int</SPAN> 
                                mon0<SPAN 
                                style="COLOR: rgb(0,0,204)">,</SPAN><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN 
                                style="COLOR: rgb(0,0,255)">const</SPAN> <SPAN 
                                style="COLOR: rgb(0,0,255)">unsigned</SPAN> 
                                <SPAN style="COLOR: rgb(0,0,255)">int</SPAN> 
                                day<SPAN style="COLOR: rgb(0,0,204)">,</SPAN> 
                                <SPAN style="COLOR: rgb(0,0,255)">const</SPAN> 
                                <SPAN 
                                style="COLOR: rgb(0,0,255)">unsigned</SPAN> 
                                <SPAN style="COLOR: rgb(0,0,255)">int</SPAN> 
                                hour<SPAN 
                                style="COLOR: rgb(0,0,204)">,</SPAN><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN 
                                style="COLOR: rgb(0,0,255)">const</SPAN> <SPAN 
                                style="COLOR: rgb(0,0,255)">unsigned</SPAN> 
                                <SPAN style="COLOR: rgb(0,0,255)">int</SPAN> 
                                <SPAN 
                                style="COLOR: rgb(255,0,0)">min</SPAN><SPAN 
                                style="COLOR: rgb(0,0,204)">,</SPAN> <SPAN 
                                style="COLOR: rgb(0,0,255)">const</SPAN> <SPAN 
                                style="COLOR: rgb(0,0,255)">unsigned</SPAN> 
                                <SPAN style="COLOR: rgb(0,0,255)">int</SPAN> 
                                sec<SPAN 
                                style="COLOR: rgb(0,0,204)">)</SPAN><BR><SPAN 
                                style="COLOR: rgb(0,0,204)">{</SPAN><BR>&nbsp;&nbsp;&nbsp;&nbsp;<SPAN 
                                style="COLOR: rgb(0,0,255)">unsigned</SPAN> 
                                <SPAN style="COLOR: rgb(0,0,255)">int</SPAN> mon 
                                <SPAN style="COLOR: rgb(0,0,204)">=</SPAN> 
                                mon0<SPAN style="COLOR: rgb(0,0,204)">,</SPAN> 
                                year <SPAN style="COLOR: rgb(0,0,204)">=</SPAN> 
                                year0<SPAN 
                                style="COLOR: rgb(0,0,204)">;</SPAN><BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;<SPAN 
                                style="COLOR: rgb(255,153,0)">/* 1..12 -&gt; 
                                11,12,1..10 
                                */</SPAN><BR>&nbsp;&nbsp;&nbsp;&nbsp;<SPAN 
                                style="COLOR: rgb(0,0,255)">if</SPAN> <SPAN 
                                style="COLOR: rgb(0,0,204)">(</SPAN>0 <SPAN 
                                style="COLOR: rgb(0,0,204)">&gt;</SPAN><SPAN 
                                style="COLOR: rgb(0,0,204)">=</SPAN> <SPAN 
                                style="COLOR: rgb(0,0,204)">(</SPAN><SPAN 
                                style="COLOR: rgb(0,0,255)">int</SPAN><SPAN 
                                style="COLOR: rgb(0,0,204)">)</SPAN> <SPAN 
                                style="COLOR: rgb(0,0,204)">(</SPAN>mon <SPAN 
                                style="COLOR: rgb(0,0,204)">-</SPAN><SPAN 
                                style="COLOR: rgb(0,0,204)">=</SPAN> 2<SPAN 
                                style="COLOR: rgb(0,0,204)">)</SPAN><SPAN 
                                style="COLOR: rgb(0,0,204)">)</SPAN> <SPAN 
                                style="COLOR: rgb(0,0,204)">{</SPAN><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mon 
                                <SPAN style="COLOR: rgb(0,0,204)">+</SPAN><SPAN 
                                style="COLOR: rgb(0,0,204)">=</SPAN> 12<SPAN 
                                style="COLOR: rgb(0,0,204)">;</SPAN>&nbsp;&nbsp;&nbsp;&nbsp;<SPAN 
                                style="COLOR: rgb(255,153,0)">/* Puts Feb last 
                                since it has leap day 
                                */</SPAN><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;year 
                                <SPAN style="COLOR: rgb(0,0,204)">-</SPAN><SPAN 
                                style="COLOR: rgb(0,0,204)">=</SPAN> 1<SPAN 
                                style="COLOR: rgb(0,0,204)">;</SPAN><BR>&nbsp;&nbsp;&nbsp;&nbsp;<SPAN 
                                style="COLOR: rgb(0,0,204)">}</SPAN><BR><BR>&nbsp;&nbsp;&

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -