用于嵌入式设备的 USB - 设计设备驱动程序/协议栈

发布于 2024-07-13 09:52:18 字数 1445 浏览 7 评论 0原文

我的任务是为嵌入式设备编写一个设备驱动程序,该设备将通过 SPI 接口与微控制器进行通信。 最终,USB接口将用于从外部下载更新的代码并在验证阶段使用。

我的问题是,有谁知道一个好的参考设计或文档或在线教程,其中涵盖了嵌入式系统中 USB 协议栈/设备驱动程序的实现/设计? 我刚刚开始阅读 650 页的 USB v2.0 规范,目前有点令人畏惧。

仅供参考,我使用的微控制器是 Freescale 9S12。

标记

根据 Goldenmean (-AD) 的评论,我想添加以下信息:

1) 嵌入式设备使用自定义执行程序,不使用 COTS 或 RTOS。

2) 设备将使用中断来指示数据已准备好从设备检索。

3)我已经阅读了一些有关Linux的文档,但是由于我对Linux一点也不熟悉,所以目前它不是很有帮助(尽管我希望它会很快)。

4) 至少目前的设计方法是为 USB 设备编写一个设备驱动程序,然后 USB 协议层 (I/O) 将驻留在设备驱动程序之上来解释数据。 我认为这将是最好的方法,尽管我可能是错的。

编辑 - 一年后,

我只是想在它们从我的脑海中消失之前分享一些项目,以防我再也不会在 USB 设备上工作。 在开发代码并首次启动并运行时,我遇到了一些障碍。

我遇到的第一个问题是,当 USB 设备连接到主机(在我的例子中是 Windows)时,主机发出重置请求。 USB 设备将重置并清除中断使能标志。 我没有阅读足够多的文献来了解这种情况的发生,因此我从未收到设置请求中断。 我花了很长时间才弄清楚这一点。

我遇到的第二个问题是没有正确处理 Set_Configuration 的设置请求。 我正在处理它,但我没有正确处理该请求,因为当此设置请求传入时,USB 设备没有发送 ACK。我最终通过使用硬件 USB 协议分析器发现了这一点。

我还遇到了其他问题,但这是两个最大的问题,我花了很长时间才弄清楚。 我不得不担心的另一个问题是大端和小端,分别是飞思卡尔 9S12 与 USB 数据格式(英特尔)。

我最终构建了类似于我过去所做的 UART 设备驱动程序的 USB 设备驱动程序。 我已将代码发布到以下 URL。

http://lordhog.wordpress.com/2010/12/13/usb-drive

我倾向于经常使用结构,因此人们可能不喜欢它们,因为它们不像使用#defines(例如,MAX3420_SETUP_DATA_AVAIL_INT_REQR 0x20)那样门户,但我喜欢它们,因为它使代码对我来说更具可读性。 如果有人对此有疑问,请随时发送电子邮件,我可以尝试提供一些见解。 只要您知道要关注哪些领域,《USB Complete:开发人员指南》一书就会很有帮助。 这是一个简单的应用程序,仅使用低速 USB。

I have been tasked to write a device driver for an embedded device which will communicate with the micro controller via the SPI interface. Eventually, the USB interface will be used to download updated code externally and used during the verification phase.

My question is, does anyone know of a good reference design or documentation or online tutorial which covers the implementation/design of the USB protocol stack/device driver within an embedded system? I am just starting out and reading through the 650 page USB v2.0 spec is a little daunting at the moment.

Just as a FYI, the micro controller that I am using is a Freescale 9S12.

Mark

Based upon goldenmean's (-AD) comments I wanted to add the following info:

1) The embedded device uses a custom executive and makes no use of a COTS or RTOS.

2) The device will use interrupts to indicate data is ready to be retrieved from the device.

3) I have read through some of the docs regarding Linux, but since I am not at all familiar with Linux it isn't very helpful at the moment (though I am hoping it will be very quickly).

4) The design approach, for now at least, it to write a device driver for the USB device then a USB protocol layer (I/O) would reside on top of the device driver to interpret the data. I would assume this would be the best approach, though I could be wrong.

Edit - A year later

I just wanted to share a few items before they vanish from my mind in case I never work on a USB device again. I ran into a few obstacles when developing code and getting it up and running for the first.

The first problem I ran into was that when the USB device was connected to the Host (Windows in my case) was the host issues a Reset request. The USB device would reset and clear the interrupt enable flags. I didn't read the literature enough to know this was happening, thus I was never receiving the Set-Up Request Interrupt. It took me quite a while to figure this out.

The second problem I ran into was not handling the Set-Up Request for Set_Configuration properly. I was handling it, but I was not processing the request correctly in that the USB device was not sending an ACK when this Set-Up Request came in. I eventually found this out by using a hardware USB protocol analyzer.

There were other issues that I ran into, but these were the two biggest ones that took me quite a while to figure out. The other issue I had to worry about is big-endian and little-endian, Freescale 9S12 vs USB data format (Intel), respectively.

I ended up building the USB device driver similar to UART device drivers I had done in the past. I have posted the code to this at the following URL.

http://lordhog.wordpress.com/2010/12/13/usb-drive

I tend to use structures a lot, so people may not like them since they are not as portal as using #defines (e.g., MAX3420_SETUP_DATA_AVAIL_INT_REQR 0x20), but I like them since it makes the code more readable for me. If anyone has questions regarding it please feel free to e-mail and I can try to give some insight to it. The book "USB Complete: The Developer's Guide" was helpful, so long as you knew what areas to concentrate on. This was a simple application and only used low-speed USB.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

梅窗月明清似水 2024-07-20 09:52:18

在为任何接口(USB、并行端口等)编写设备驱动程序时,需要开发的代码将取决于该处理器/微控制器上是否运行任何操作系统(OS)、RTOS。
例如,如果要运行WinCE - 它将有自己的驱动程序开发套件,以及设备驱动程序开发中要遵循的步骤。 对于任何其他操作系统(如 Linux、symbian)也是如此。

如果它是一个简单的固件代码(无操作系统)来控制处理器/微控制器,那么情况就完全不同了。
因此,基于您所处的上述任一情况,您需要阅读并阅读 了解:-

1.) 处理器/微控制器开发板的硬件规格 - 寄存器文件、端口、内存布局等。

2.) USB 规格

3.) 我很快找到了几个指针。 谷歌应该成为你的朋友!
http://www.lrr.in.tum.de/Par/ arch/usb/usbdoc/ - Linux USB 设备驱动程序

http: //www.microsoft.com/technet/archive/wce/support/usbce.mspx

-AD

While writing a device driver for any interface (USB, Parallel port, etc...) the code needed to be developed would depend upon whether there is any Operating System(OS), RTOS running on that Processor/Micro controller.
e.g. if thats going to run say WinCE - It will have its own Driver development Kit , and steps to be followed in the device driver development. Same for any other OS like Linux, symbian.

If its going to be a plain firmware code(No OS) which is going to control the processor/microcontroller, then it's a different situation altogether.
So based on either of the above situation u are in, one needs to read & understand:-

1.) The Hardware Specification of the processor/micro controller development board - Register files, ports, memory layout, etc.

2.) USB spec

3.) Couple of pointers i found quickly. Google shud be ur friend!
http://www.lrr.in.tum.de/Par/arch/usb/usbdoc/ - Linux USB device driver

http://www.microsoft.com/technet/archive/wce/support/usbce.mspx

-AD

む无字情书 2024-07-20 09:52:18

我使用过 Jan Axelson 编写的 USB Complete 的早期版本。 确实非常完整。

来自编辑评论:

此通用串行总线 (USB) 接口开发人员指南现已推出第四版,涵盖了项目开发的各个方面,例如硬件设计、设备固件和主机应用软件。

I've used an earlier edition of USB Complete by Jan Axelson. Indeed very complete.

From the editorial review:

Now in its fourth edition, this developer's guide to the Universal Serial Bus (USB) interface covers all aspects of project development, such as hardware design, device firmware, and host application software.

花开半夏魅人心 2024-07-20 09:52:18

我很好奇,为什么选择9S12? 我在之前的工作中使用过它,但不太满意。

  • 它对 gcc 的支持很差,所以我们使用了 Metrowerks
    • 这对于 C 来说可能没问题,但经常会生成有问题的 C++
    • 有一个带有二进制项目文件的糟糕 IDE!
  • 9s12 也很慢,很多指令在 5 个周期内执行。
  • 也不是很省电。
  • 没有桶形移位器,使得嵌入式代码中常见的操作变得缓慢
  • 而不那么便宜。

我唯一不喜欢的是 8051。我在当前的工作中使用 ARM CortexM3,它在各方面都比 9S12 好(更快的时钟、每个时钟完成更多的工作、更少的功耗、更便宜、良好的 gcc 支持) ,32 位与 16 位)。

I'm curious, why did you pick the 9S12? I used it at a previous job, and was not pleased.

  • It had lousy gcc support so we used Metrowerks
    • which may have been okay for C, but often generated buggy C++
    • had a lousy IDE with binary project files!
  • The 9s12 was also slow, a lot of instructions executed in 5 cycles.
  • Not very power efficient, either.
  • no barrel shifter, made operations that are common in embedded code slow
  • not that cheap.

About the only thing I dislike more is an 8051. I'm using an ARM CortexM3 at my current job, it's better than a 9S12 in every way (faster clock, more work done per clock, less power consumption, cheaper, good gcc support, 32-bit vs. 16-bit).

寄风 2024-07-20 09:52:18

我不知道您打算使用哪种硬件,但假设它很灵活,STMicro 提供了一系列支持 USB/SPI 的微控制器以及可与其部件一起使用的 C 代码库。 -- 我已经使用他们的 ARM7 系列微处理器多年,并取得了巨大成功。

I don't know which hardware you're planning to use but assuming that's flexible, STMicro offers a line of microcontrollers with USB/SPI support and a library of C-code that can be used with their parts. -- I've used their ARM7 series micros for years with great success.

双手揣兜 2024-07-20 09:52:18

这是一个由德克萨斯大学教授 Jonathan Valvano 维护的优秀网站。 他在那里教授四门课程(三门本科生,一门研究生),都是关于使用 9S12 微控制器的。 他的网站包含他在所有课程中使用的所有讲义、实验手册,更重要的是,入门文件。

该网站看起来像是 90 年代的网站,但只要稍微挖掘一下,您就应该找到您需要的一切。

users.ece.utexas.edu/~valvano/

Here is an excellent site maintained by Jonathan Valvano, a professor at the University of Texas. He teaches four courses over there (three undergraduate, one graduate), all are about using a 9S12 microcontroller. His site contains all the lecture notes, lab manuals, and more importantly, starter files, that he uses for all his classes.

The website looks like it's from the 90's, but just dig around a bit and you should find everything you need.

users.ece.utexas.edu/~valvano/

风吹雪碎 2024-07-20 09:52:18

考虑将 AVR 用于您的下一个 MCU 项目,因为它具有出色的 LUFAV-USB 库。

Consider AVR for your next MCU project because of it's wonderful LUFA and V-USB libraries.

李不 2024-07-20 09:52:18

我正在开发一个使用 Atmel V71 的项目。 该处理器功能非常强大,芯片上提供的众多高端连接功能之一是 USB 引擎,它可以执行 480 Mhz 或 48Mhz(不是 USB 3.0)的设备或主机模式。 这些工具是免费的,并附带许多主机和设备 USB 示例项目,其中包含所有 USB 堆栈代码。 它支持 10 个端点,所有传输均通过 DMA 完成,因此您可以将大部分处理器能力用于其他任务。 Atmel USB 堆栈无需 RTOS 即可工作

I'm working on a project using the Atmel V71. The processor is very powerful and among lot's of high end connectivity offered on chip is a USB engine that will do device or host modes for 480 Mhz or 48Mhz (not USB 3.0). The tools are free and come with a number of host and device USB example projects with all the USB stack code right there. It supports 10 end points and all the transfers are done via DMA so you have most of the processor horsepower available for other tasks. The Atmel USB stack works without needing an RTOS

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文