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

📄 实例——usb设备驱动设计(下)--山野村夫.htm

📁 USB驱动设计的步骤和主要程序框架
💻 HTM
📖 第 1 页 / 共 4 页
字号:
                              <P class=MsoNormal 
                              style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt"><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">这里使用</SPAN><SPAN 
                              lang=EN-US>DriverStudio</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">驱动程序开发工具进行开发,对于熟悉面向对象编程的软件开发员,</SPAN><SPAN 
                              lang=EN-US>DriverStudio</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">是一个良好的驱动开发工具,并且开发时间比较短。</SPAN><SPAN 
                              lang=EN-US>DriverStudio</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">工具包中的</SPAN><SPAN 
                              lang=EN-US>DriverWorks</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">提供了</SPAN><SPAN 
                              lang=EN-US>3</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">个类:</SPAN><SPAN 
                              lang=EN-US>KDriver</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">、</SPAN><SPAN 
                              lang=EN-US>KPnpDevice</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</SPAN><SPAN 
                              lang=EN-US>KPnpLowerDevice</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">,这</SPAN><SPAN 
                              lang=EN-US>3</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">个类用于实现</SPAN><SPAN 
                              lang=EN-US>WDM</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">驱动程序的框架结构。其中,</SPAN><SPAN 
                              lang=EN-US>KDriver</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">类提供设备驱动程序的基本框架结构。它负责驱动程序的初始化和将</SPAN><SPAN 
                              lang=EN-US>IRP</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">分发到目标设备对象。由于</SPAN><SPAN 
                              lang=EN-US>KDriver</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">是抽象类,必须创建一个</SPAN><SPAN 
                              lang=EN-US>KDriver</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的派生类,并重载</SPAN><SPAN 
                              lang=EN-US>DriverEntry</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">例程,在</SPAN><SPAN 
                              lang=EN-US>DriverEntry</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">例程中做一些初始化工作。每当</SPAN><SPAN 
                              lang=EN-US>Pnp</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">子系统检测到驱动程序所负责的设备时,就调用</SPAN><SPAN 
                              lang=EN-US>AddDevice</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">例程,</SPAN><SPAN 
                              lang=EN-US>UnLoad</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">例程负责最后的清除工作。</SPAN></P>
                              <P class=MsoNormal 
                              style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt"><SPAN 
                              lang=EN-US><o:p>&nbsp;</o:p></SPAN></P>
                              <P class=MsoNormal 
                              style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt"><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">对于</SPAN><SPAN 
                              lang=EN-US>KPnpDevice</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">类,它是</SPAN><SPAN 
                              lang=EN-US>KDevice</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">类的派生类,在驱动程序中只作为基类使用,它支持即插即用和电源管理,主要处理</SPAN><SPAN 
                              lang=EN-US>IRP MJ PNP</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</SPAN><SPAN 
                              lang=EN-US>IRP MJ POWER</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">请求包。</SPAN></P>
                              <P class=MsoNormal 
                              style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt"><SPAN 
                              lang=EN-US><o:p>&nbsp;</o:p></SPAN></P>
                              <P class=MsoNormal 
                              style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt"><SPAN 
                              lang=EN-US>KPnpLowerDevice</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">类提供了一个物理设备对象的模型,当驱动程序创建或初始化一个</SPAN><SPAN 
                              lang=EN-US>KPnpLowerDevice</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">类实例的时候,它就将一个设备对象连向了一个物理设备对象。</SPAN></P>
                              <P class=MsoNormal 
                              style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt"><SPAN 
                              lang=EN-US><o:p>&nbsp;</o:p></SPAN></P>
                              <P class=MsoNormal 
                              style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt"><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">除用到以上类外,开发</SPAN><SPAN 
                              lang=EN-US>USB</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">驱动程序还用到了</SPAN><SPAN 
                              lang=EN-US>DriverWorks</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">提供的</SPAN><SPAN 
                              lang=EN-US>3</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">个用于实现</SPAN><SPAN 
                              lang=EN-US>USB</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">设备操作的类:</SPAN><SPAN 
                              lang=EN-US>KUsbLowerDevice</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">、</SPAN><SPAN 
                              lang=EN-US>KUsbInterface</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</SPAN><SPAN 
                              lang=EN-US>KUsbPipe</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">类。其中,</SPAN><SPAN 
                              lang=EN-US>KPnpLowerDevice</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">实例代表端点</SPAN><SPAN 
                              lang=EN-US>O</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">,允许</SPAN><SPAN 
                              lang=EN-US>USB</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">驱动程序通过默认控制管道控制</SPAN><SPAN 
                              lang=EN-US>USB</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">设备,如配置</SPAN><SPAN 
                              lang=EN-US>USB</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">设备,传输各种控制和状态请求;</SPAN><SPAN 
                              lang=EN-US>KusbInterface</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">类的作用更多是结构上的而非功能上的,其成员函数几乎不与实际物理设备交互作用,驱动程序用这个类获取接口和管道信息;</SPAN><SPAN 
                              lang=EN-US>KusbPipe</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">类对应于管道,管道是主机和一个端点的信息连接,这个类用于初始化管道信息和管道操作控制。以下是部分驱动程序代码。</SPAN></P>
                              <P class=MsoNormal 
                              style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt"><SPAN 
                              lang=EN-US><o:p>&nbsp;</o:p></SPAN></P>
                              <DIV class=MsoNormal 
                              style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt"><SPAN 
                              lang=EN-US><SPAN 
                              style="mso-spacerun: yes">&nbsp;</SPAN>(1)</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">设备驱动程序的</SPAN><SPAN 
                              lang=EN-US>DriverEntry</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</SPAN><SPAN 
                              lang=EN-US>AddDevice</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">例程。</SPAN></DIV>
                              <DIV class=MsoNormal 
                              style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt"><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"></SPAN>&nbsp;</DIV>
                              <DIV><BR></DIV>
                              <DIV><IMG onmousewheel="return bbimg(this)" 
                              style="CURSOR: pointer" 
                              onclick=javascript:window.open(this.src); 
                              src="实例——USB设备驱动设计(下)--山野村夫.files/109447082.jpg" 
                              onload="javascript:if(this.width>665){this.resized=true;this.style.width=665;}"></DIV><BR><BR>
                              <DIV>
                              <P class=MsoNormal 
                              style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt"><SPAN 
                              lang=EN-US><SPAN 
                              style="mso-spacerun: yes">&nbsp;</SPAN>(2)</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">数据读写</SPAN><SPAN 
                              lang=EN-US>(read</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</SPAN><SPAN 
                              lang=EN-US>write)</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">例程,如果固件程序中对</SPAN><SPAN 
                              lang=EN-US>USB</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">设备配置选择的是哪个端点读写数据,驱动程序中便使用相应的那个端点来读写数据。具体的代码如下。</SPAN></P><BR></DIV>
                              <DIV><IMG onmousewheel="return bbimg(this)" 
                              style="CURSOR: pointer" 
                              onclick=javascript:window.open(this.src); 
                              src="实例——USB设备驱动设计(下)--山野村夫.files/109566503.jpg" 
                              onload="javascript:if(this.width>665){this.resized=true;this.style.width=665;}"><BR><IMG 
                              onmousewheel="return bbimg(this)" 
                              style="CURSOR: pointer" 
                              onclick=javascript:window.open(this.src); 
                              src="实例——USB设备驱动设计(下)--山野村夫.files/109707719.jpg" 
                              onload="javascript:if(this.width>665){this.resized=true;this.style.width=665;}"></DIV><BR>
                              <DIV>
                              <P class=MsoNormal 
                              style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt"><SPAN 
                              lang=EN-US><o:p>&nbsp;</o:p></SPAN></P>
                              <P class=MsoNormal 
                              style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt"><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">关于写数据的例程和上述的读数据的例程相似,只要相应地把读的函数换成写的函数即可,在此不详细列出。</SPAN></P>
                              <P class=MsoNormal 
                              style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt"><SPAN 
                              lang=EN-US><o:p>&nbsp;</o:p></SPAN></P>
                              <P class=MsoNormal 
                              style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt"><SPAN 
                              lang=EN-US>3</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">.</SPAN><SPAN 
                              lang=EN-US>USB</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">应用程序开发</SPAN></P>
                              <P class=MsoNormal 
                              style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt"><SPAN 
                              lang=EN-US><o:p>&nbsp;</o:p></SPAN></P>
                              <P class=MsoNormal 
                              style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt"><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">应用程序是使用</SPAN><SPAN 
                              lang=EN-US>Visual C++</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">开发的,主要是调用</SPAN><SPAN 
                              lang=EN-US>Windows</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">提供的一系列</SPAN><SPAN 
                              lang=EN-US>API</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数来实现与驱动程序交互。主要使用的</SPAN><SPAN 
                              lang=EN-US>WIN32API</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数有以下几个。</SPAN></P>
                              <P class=MsoNormal 
                              style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt"><SPAN 
                              lang=EN-US><o:p>&nbsp;</o:p></SPAN></P>
                              <P class=MsoNormal 
                              style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt"><SPAN 
                              lang=EN-US><SPAN 
                              style="mso-spacerun: yes">&nbsp;</SPAN>(1)CreatFile()</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">:打开设备文件;</SPAN></P>
                              <P class=MsoNormal 
                              style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt"><SPAN 
                              lang=EN-US><o:p>&nbsp;</o:p></SPAN></P>
                              <P class=MsoNormal 
                              style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt"><SPAN 
                              lang=EN-US><SPAN 
                              style="mso-spacerun: yes">&nbsp;</SPAN>(2)CloseHandle()</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">:关闭设备文件;</SPAN></P>
                              <P class=MsoNormal 
                              style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt"><SPAN 
                              lang=EN-US><o:p>&nbsp;</o:p></SPAN></P>
                              <P class=MsoNormal 
                              style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt"><SPAN 
                              lang=EN-US><SPAN 
                              style="mso-spacerun: yes">&nbsp;</SPAN>(3)ReadFile()</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">:从设备文件中读数据;</SPAN></P>
                              <P class=MsoNormal 
                              style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt"><SPAN 
                              lang=EN-US><o:p>&nbsp;</o:p></SPAN></P>
                              <P class=MsoNormal 
                              style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt"><SPAN 
                              lang=EN-US><SPAN 
                              style="mso-spacerun: yes">&nbsp;</SPAN>(4)WriteFile()</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">:向设备文件中写入数据。</SPAN></P>
                              <P class=MsoNormal 
                              style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt"><SPAN 
                              lang=EN-US><o:p>&nbsp;</o:p></SPAN></P>
                              <P class=MsoNormal 
                              style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.75pt"><SPAN 
                              lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp; 
                              </SPAN></SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">具体实现在此不详细列出。其中,应用程序界面如图</SPAN><SPAN 
                              lang=EN-US>7-1 3</SPAN><SPAN 
                              style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">所示。</SPAN></P><BR></DIV><IMG 
                              onmousewheel="return bbimg(this)" 
                              style="CURSOR: pointer" 
                              onclick=javascript:window.open(this.src); 
                              src="实例——USB设备驱动设计(下)--山野村夫.files/109265739.jpg" 
                              onload="javascript:if(this.width>665){this.resized=true;this.style.width=665;}"><BR></SPAN></TD></TR></TBODY></TABLE>

⌨️ 快捷键说明

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