Micro Framework USB Driver开发

来源:互联网 发布:遗传算法 java 编辑:程序博客网 时间:2024/05/17 22:24

 

为Micro Framework开发USB驱动也有一段时间了,随着开发的深入,对USB理解也渐渐清晰起来。

摘自:http://www.pin5i.com/showtopic-22328.html

 

从系统架构上来说为Micro Framework开发USB驱动有三个层面的工作。一是针对USB芯片的驱动移植(很多ARM CPU都集成了USB功能),实现MFHAL层要求的USB接口;二是开发PC平台上针对MF设备的USB驱动;三是编写应用程序(非Micro Framework应用程序),通过USB接口直接和MF设备通信(这个工作是我加的,其实完成前两步工作,就已经使MFDeployVS2008和设备正常通信了)。

下面我针对这三个层面,详细说一下我的开发工作。

一、USB驱动移植

这一步是最难的,特别是对USB没有任何了解的人来说。Micro FarmeworkUSB开发接口如下(含HAL层和PAL层的接口,其中PAL层的代码已经提供,不需要任何编程)。

1CPU_USB_GetInterruptState Function

Determines whether there are currently interrupts pending for the USB port.

2CPU_USB_Initialize Function

Initializes the client module at the driver layer.

3CPU_USB_ProtectPins Function

Puts USB pins into a known state to prevent spurious inputs.

4CPU_USB_RxEnable Function

Enables a specified USB port to receive data.

5CPU_USB_StartOutput Function

Begins sending output to a specified USB port.

6CPU_USB_Uninitialize Function

Uninitializes (shuts down) USB communications at the driver layer.

7USB_ClearEvent Function

Clears one or more USB events.

8USB_Flush Function

Flushes the buffers associated with USB ports.

9USB_GetEvent Function

Gets the state of a USB event.

10USB_Initialize Function

Initializes the USB subsystem.

11USB_Read Function

Reads a block of data from a USB port.

12USB_SetEvent Function

Sets the state of a USB event.

13USB_Uninitialize Function

Initializes the USB subsystem.

14USB_Write Function

Writes a block of data to a USB port.

其实接口什么样都无所谓,关键是首先你要让USB设备能work,让PC机能识别你的设备,USB开发的里程碑有三个,一是让PC机发现未知设备,二是让PC机识别USB设备,三是正确安装USB驱动(当然要能正常工作)。

1、第一步让USB识别你的设备,这一步可以说是最难,也可以说是最容易的一步,说它最难是因为这一步和硬件的正常工作非常相关,如果硬件工作不正常或USB接口有物理故障,这一步都很难通过,所以在开发USB驱动之前,一定确保硬件设备的USB能正常工作(比如在linuxWince环境下能正常工作)。说它容易是因为仅需要正确配置一两个寄存器,不需要什么编程就能实现这一步。这个过程不需要了解太多的USB相关知识,只需要精读该芯片的英文技术文档即可。

2、第二步让PC机识别你的USB设备,这一步是最关键的一步,也是需要大量USB知识的一步。这一步仅需要实现USB的端点0(称控制端点或默认端点)相关的功能即可。

如果没有USB基础的人,这一步必须要充电,必须要知道USB相关协议规范,否则你就会寸步难行。我这里推荐一下两个人的blog,一个是大名鼎鼎的USB研究专家圈圈,写的关于USB的文章都非常透彻,相关链接(USB入门系列之一 —— USB概述 );另一个是蔡军生的关于USB的文章,写的也非常好,对开发很有帮助(龙芯软件开发(32)-- USB协议深入分析)。其次我推荐,如果大家有条件就要购买一本周立功编著的《USB 2.0OTG规范及开发指南》,这可以说是一本USB2.0英文协议文档的翻译版,不过翻译的还不错(网上也流传了不少USB2.0中文协议文档,不过有些差强人意),可以相对比较准确地理解USB术语。对我来说,还是比较习惯读书的,在电脑上看大篇大篇的技术文档很容易看了后面忘了前面的。

这一步其实就是PC机对USB设备的盘查工作,有点类似公安局审犯人,如你是谁?什么背景?有什么能力等等。其中最关键的要问到USB设备的PIDVID,问到这一个才能安装相对应的驱动程序,当然获得相关的接口和端点配置也是非常重要的。详细的信息交互,这一步我就不多说了,协议上都说的非常清楚。这一步要说一点,就是设置地址这个环节最容易出错,PC机分配的地址收到后,一定要等返回相关ACK响应后,再修改设备的USB设备地址,否则很容易出问题。

这里推荐一下北航frank的文章《USB项目技术报告》,这个环节写的很清楚。
此外如果你手边有USB协议分析仪,那么你真是一个非常幸福的人,这一步就需要这个东东(我不太幸运,我是在调通USB驱动之后,才有的USB协议分析仪),Bus Hound工具虽然好用,但是在这一步却帮不上忙,它是要识别USB设备后才能正常监控USB数据流的,这对第三步的工作非常有帮助。


 附件: 您所在的用户组无法下载或查看附件

USB通信过程一目了然,你会发现USB协议分析仪原来这么强大)。


 附件: 您所在的用户组无法下载或查看附件

注:Micro FrameworkPAL层已实现相关通信交互的指令,你仅需要把USB通道打通即可,此外不要拘泥于MF porting kit中的USB驱动示例,要根据需要大胆一些调整相关逻辑。具体的要求请参见我以前的文章《MF Porting之USB驱动开发

第三步,安装驱动,如果第二步正确完成,PC机就会要求你安装USB驱动了。

     

 附件: 您所在的用户组无法下载或查看附件

      二、PC USB驱动开发
Micro FrameworkPorting kit开发包中已经提供了MF 设备的USB驱动源码(MFUSB_PortingKitSample),你仅需要修改一下Inf文件(主要修改相关的PIDVID,记住PIDVID仅是设备和相关驱动关联用的,除此之外没有其它任何用处),此外需要安装Windows DDK开发包,用来编译该驱动程序。相关说明如下:
To compile the USB drivers you must
1) install the DDK
2) open a command window for building (build environment must be initialized for that cmd window by calling setenv.cmd on the appropriate client directory)
3) build with the script build_usb_drivers in %SPOCLIENT%"Tools"scripts directory
  usage: build_usb_drivers.cmd DDK_INSTALLDIR
  example: build_usb_drivers.cmd c:"WINDDK"6001.18001
Output directories are under the drivers' source tree. Please move the sys files in the corresponding sub-directories in the inf folder before installing.
NOTE: the build_usb_drivers script will only build the MFUSB_PortingKitSample driver. The MFUSB_DualInterface_PortingKitSample driver is the same
as the previous one but can handle 4 endpoints instead of two and allows debugging over USB the first pair of endpoints while reading/writing
on the other pair.
    由于MF设备仅支持3个端点(012),所以要扩展功能或增加端点,就需要你修改此程序的相关源码了(当然MF设备的USB驱动你也要相应地修改)。

      三、USB应用程序开发
很多网上的此类程序大部分都是基于VC的,几乎找不到C#相关的源代码,即使有也发现文不对题,根本实现不了和设备的直接通信,网上有一个ICSharpCode.USBlib的库可以在C#中引用,据说功能还很强大,不过我在VistaXP上好像没有测试成功,枚举USB设备时老出现异常。

幸好MFDeploy程序(MF SDK中的部署工具)是用C#开发的,所以深入研究该程序后,终于从中提取出一个USB读写类(MFDeploy程序太复杂了,我研究了好长时间才粗略理清相关逻辑脉络),终于很方便的用C#操作USB设备了。


 附件: 您所在的用户组无法下载或查看附件

相关接口:

  1.     public interface IControllerLocal : IController
  2.     {
  3.         Stream OpenPort();
  4.     }
  5.     public interface IController
  6.     {
  7.         void ClosePort();
  8.         bool SendData(byte[] buf);
  9.         int Available { get; }
  10.         int ReadData(byte[] buf, int offset, int count);
  11.         void Start();
  12.         void Stop();
  13.         bool IsPortConnected { get; }
  14.         DateTime LastActivity { get; }
  15.     }
  16.     public interface IControllerHostLocal
  17.     {
  18.         Stream OpenConnection();
  19. }
复制代码

 

 

 


应用代码:


  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. using System.Collections;
  5. using System.Threading;
  6. using Microsoft.SPOT.Debugger;
  7. namespace USBTest
  8. {
  9.     public partial class frmMain : Form
  10.     {
  11.         UsbEngine usb =null;
  12.         ArrayList pds=null;
  13.         private Thread m_ReadThread;
  14.         public frmMain()
  15.         {
  16.             InitializeComponent();
  17.             btnSend.Enabled = false;
  18.             btnUpdata_Click(null, null);
  19.         }
  20.         private void btnUpdata_Click(object sender, EventArgs e)
  21.         {
  22.             pds = UsbEngine.Enumerate();
  23.             cmbUSBList.Items.Clear();
  24.             foreach (PortDefinition pd in pds)
  25.             {
  26.                 cmbUSBList.Items.Add(pd.DisplayName);
  27.             }
  28.             if (cmbUSBList.Items.Count > 0 )cmbUSBList.SelectedIndex = 0;
  29.             else btnSend.Enabled = false;
  30.         }
  31.         private void cmbUSBList_SelectedIndexChanged(object sender, EventArgs e)
  32.         {
  33.             if (usb != null)
  34.             {
  35.                 btnSend.Enabled = false;
  36.                 usb.Dispose();
  37.             }
  38.             btnSend.Enabled = true;
  39.             usb = new UsbEngine((PortDefinition)pds[cmbUSBList.SelectedIndex]);
  40.             usb.Start();
  41.             m_ReadThread = new Thread(new ThreadStart(this.ReadDataThread));
  42.             m_ReadThread.IsBackground = true;
  43.             m_ReadThread.Start();
  44.         }
  45.         private void ReadDataThread()
  46.         {
  47.             byte[] bytData = new byte[1024];
  48.             while (btnSend.Enabled)
  49.             {
  50.                 try
  51.                 {                 
  52.                     if (usb.Available> 0)
  53.                     {
  54.                         int intRet = usb.ReadData(bytData, 0, usb.Available);
  55.                         MethodInvoker method = delegate
  56.                         {
  57.                             string strData = "";
  58.                             for (int i = 0; i < intRet; i++)
  59.                             {
  60.                                 strData += bytData[i].ToString("X2") + " ";
  61.                             }
  62.                             txtRead.Text += strData;
  63.                         };
  64.                         this.txtRead.Invoke(method);
  65.                     }
  66.                     Thread.Sleep(100);
  67.                 }
  68.                 catch { }
  69.             }
  70.         }
  71.         private void btnSend_Click(object sender, EventArgs e)
  72.         {
  73.             if (usb == null || txtSend.Text.Length == 0 ) return;
  74.             try
  75.             {
  76.                 string[] strData = txtSend.Text.Split(new char[] { ' ' });
  77.                 byte[] bytData = new byte[strData.Length];
  78.                 for (int i = 0; i < bytData.Length; i++)
  79.                 {
  80.                     bytData[i] = (byte)Convert.ToInt32(strData[i], 16);
  81.                 }
  82.                 usb.SendData(bytData);
  83.             }
  84.             catch(Exception ex)
  85.             {
  86.                 txtRead.Text = "Send Error:" + ex.Message.ToString();
  87.             }
  88.         }
  89.         private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
  90.         {
  91.             if (usb == null) return;
  92.             btnSend.Enabled = false;
  93.             usb.Stop();
  94.         }     
  95.     }
  96. }
复制代码

 

现在很多USB设备都是免驱的了,就是实现了HID封装,借助该通道和USB设备通信。我对该技术非常感兴趣,有时间可以深入研究一下。有此类经验的朋友也希望能分享一下。

还有一个值得高兴的事,Micro Framework美国微软研发团队终于向我们开放了TinyCLR源码,这是一个非常值得纪念的开始,意味着我们中国的MF团队又向美国的MF开发团队走近了一步,期待我们能在MF上走的更远。正如Lorenzo所说:We are very excited about this opportunity and we are confident that it will have a huge impact on our business. This could be the beginning of a great collaboration.