The code supporting the USB Mass Storage Class of devices has been added to USB Host Shield 2.0 library and is available to download on GitHub.
Mass storage devices include USB Flash drives, memory card readers, external hard drives/CD-ROMs, smartphones/tablets, and some others – almost anything that shows as a drive while connected to a PC (exceptions are digital cameras as well as some phones pretending to be digital cameras). Andrew Kroll (who made this release possible) – thank you very much!
Top 10 USB Host Shield 2.0 on Amazon
No products found.
Related Article: USB Monitor
At present, the code example, also featuring Andrew’s FAT and extended memory implementation, can only be run on “big” Arduinos such as Mega and Mega 2560. Another FAT implementation, developed by Alex Glushchenko, is being tested – there is a slight possibility that at least some functionality can be demonstrated on a regular UNO board.
On the other hand, the mass storage component can be used without a file system by simply reading/writing physical sectors; this approach can save a lot of memory. The documentation for the mass storage class code is available here.
Many hours have been spent testing the code; it should work with any device which claims to support “mass storage bulk only” transport.
While newer (less than 5 years old) won’t cause any problems, older ones could be finicky. If your device shows odd behavior with this code, please let me know – I will trade it for the good working one.
Enjoy!
HID support for USB Host Shield Library 2.0 released
HID r.2.0 released
I am pleased to announce that after a long and difficult development period Human Input Device AKA HID class support has been added to USB Host Shield Library r.2.0 and is available on gitHub – I suggest downloading the whole directory since some modifications have been also made to core files to accommodate a new class.
HID devices include popular devices like keyboards, mice, joysticks, game controllers, bar code scanners, RFID and magnetic card readers, digital scales, and UPSes, to name a few.
I previously wrote about interfacing with HID devices here, here, and here. The code examples in these articles were written for the legacy USB Host Shield library and can’t be compiled with the current revision, however, the basic principles are the same – the device is periodically polled by the host and sends back a data block called a report containing changes in device controls (buttons, switches, jog dials, etc.) since the last poll.
Even though different devices have different report formats, for a certain device, the report format is stored in the device in a data structure called report descriptor. Therefore, it is possible to learn about device controls from the device itself by parsing its report descriptor.
There is one special case where the report format is known in advance. Almost all HID keyboards and mice support the so-called boot protocol intended for communication to very simple systems like PC configuration screen when the computer runs from BIOS.
The keyboard boot protocol report consists of 8 bytes containing the state of modifier keys (CTRL, SHIFT, etc.) in the first byte, the second byte being reserved, and up to 6 key scan codes in the rest of the report.
Mouse boot protocol report consists of 3 bytes, the first of which contains the state of left, right, and middle buttons, and the other 2 store X and Y travel since the last poll.
In many cases boot protocol capabilities are more than enough for an Arduino project; for this reason, the boot protocol class is the first to be released. To demonstrate the operations of this class, 2 simple sketches have been developed, one for a mouse, and another for a keyboard.
Sketches are designed for USB Host Shield 2.0 attached to compatible Arduino board. Also, it is recommended to use external power supply while working with HID devices – I’ve seen many devices which would refuse to function when Arduino receives power from its USB port only.
The operation of the mouse sketch is quite simple: any mouse movement and/or button press causes a corresponding callback to be called. Below is the sketch output:
1234567891011121314151617 | 000200dx=2 dy=0000100dx=1 dy=00002FFdx=2 dy=-1020000R Butt Dndx=0 dy=0000000R Butt Up020000R Butt Dn030000L Butt Dn020000L Butt Up |
Sketch prints mouse reports in hex and human-readable form. For example, lines 2 and 4 show mouse movement, lines 8 and 9 show the first report when a button was pressed with no movement, the rest is just various button presses/releases.
The keyboard sketch is more advanced. It polls the keyboard, tracks key presses, converts scan codes to ASCII, and also sends output reports to turn keyboard LEDs on/off when CAPS LOCK, Num Lock, or Scroll Lock key is pressed. Here is the output:
1234567891011121314151617181920212223242526272829303132333435363738394041 | 00001A0000000000DN >1A<ASCII: w0000000000000000UP >1A<0000080000000000DN >08<ASCII: e0000000000000000UP >08<00000B0000000000DN >0B<ASCII: h00000B0700000000DN >07<ASCII: d00000B070A090000DN >0A<ASCII: gDN >09<ASCII: f0000070A09000000UP >0B<0000070900000000UP >0A<000007090B000000DN >0B<ASCII: h0000000000000000UP >07<UP >09<UP >0B<0000390000000000DN >39<00003900000000000000000000000000UP >39<0000390000000000DN >39<0000000000000000UP >39< |
HID boot devices are quite common, inexpensive, and easy to interface to Arduino using USB Host Shield. Two demo sketches should provide a good example of HID boot class usage as well as serve as templates to incorporate into other projects.
If you still have questions, please ask them in the comments – I will be glad to help. In coming weeks I’m going to write another article or two showing ways to use this new class. Stay tuned!
USB Host Shield library Version 2.0 released
What started as a quick re-factoring effort transformed into a major redevelopment, but finally all pieces fit together tightly and I am pleased to announce that initial release of USB Host Shield library ver.2.0 has been posted to github.
This new version contains several major improvements:
Only 5 Arduino pins are now required for USB Host Shield to function – 3 standard SPI pins (SCK, MISO, MOSI) and 2 remappable pins (SS and INT).
The low-level interface to MAX3421E has been re-designed. Arduino pin manipulation routines have been replaced with mechanisms inspired by Konstantin Chizhov’s C++ AVR pin templates. As a result, low-level transfers became approximately 3.5 times faster.
Also, pin reassignment can be done much easier by passing pin numbers into the MAX3421E template during instantiation.
The high-level interface to USB devices has been re-designed as well. It is now possible to connect a USB hub to the shield and have many devices on the USB bus, up to 7 daisy-chained 8-port hubs plus up to 44 devices connected to hub ports left after daisy-chaining, memory permitting.
Also, a standard mechanism of device initialization/polling/releasing has been added to enumeration.
Several minor code improvements have also been made. NAK_LIMIT is now tied to an endpoint – it is now possible to have NAK_LIMIT set to 1 for the interrupt endpoint and 32000 for the bulk endpoint of the same device simultaneously.
The control transfer function now accepts callback in order to split long chunks of data, if necessary. inTranser() function now is able to return an actual number of bytes received.
Support for several popular device classes has been added. Device initialization and event handling are now moved to a library specific to the devise class, therefore user application doesn’t need to do this and only needs to process actual device data.
Human Interface Device AKA HID. Keyboards, mice, game controllers, bar code scanners, magnetic cards,s and RFID readers – the list goes on. Initialization, polling, report descriptor parsing, as well as reading, and sending reports are possible.
Asynchronous Serial over USB AKA CDC. Devices of this class provide serial connectivity. For example, newer Arduino boards, such as UNO and Mega 2560 communicate to a PC using a CDC ACM-class device, while older boards, such as Duemilanove, use FTDI FT232 USB-to-serial converter.
3 main types of converters are supported – CDC ACM (newer Arduinos and most modern cell phones), FTDI FT232 (older Arduinos plus a ton of standalone converters ), and Prolific PL2303 (mainly used in USB cables for various cellphones).
Several other device classes are in the works and will be available soon. They include:
- Mass Storage Devices – Flash Drives, USB sticks, card readers, etc.
- PTP – a protocol used in digital cameras. The PTP code is being transferred from the current PTP project with minor changes.
- Android ADK protocol, was recently introduced in versions 3.1 and 2.3.4 of Android OS. Simple lightweight protocol for bi-directional communication with Android phone or tablet.
In addition to that, the USB hub is supported by its own class; however, in order to use it nothing special needs to be done apart from defining an instance of hub class for each hub.
Library code is compatible with current revisions of USB Host Shield, i.e. full-size shield r2.0 and Mini shield r1.1. Older shields can be made compatible with the new library by performing a simple hardware modification.
I will add a section to hardware manual eventually, in the meantime, if you still have an old shield and want to know how to mod it to make it compatible with new library, drop me a line.
It also should be noted that due to extra code needed for multi-device support binaries compiled from this library being larger than the old one – before migrating your existing project make sure it will fit into your Arduino.
A hub_demo directory under examples contains a sketch demonstrating hub functionality. The sketch goes from hub to hub initializing devices and then prints their device and configuration descriptors.
As can be seen in the title picture, I have a 24 port hub, which actually consists of four daisy-chained 7-port hubs, 3 cell phones, a couple of mice, a USB-to-serial converter, Xbee plugged into Explorer board, a digital scale, a pair of RC controllers, digital camera, an RFID reader.
The output of a sketch running this setup can be seen here.
The following lines:
- USBHub Hub1(&Usb);
- USBHub Hub2(&Usb);
- USBHub Hub3(&Usb);
- USBHub Hub4(&Usb);
create hub instances, if you need more than four (up to 7), add USBHub Hub5(&Usb);, USBHub Hub6(&Usb);, USBHub Hub7(&Usb);. If you need to address more than 16 devices, including hubs, increase USB_NUMDEVICES in Usb.h.
Except for hubs, the sketch doesn’t make any attempts to configure any devices, it can be run with any USB devices which are available.
At present, I only have one other sample sketch in the ACM directory. It is a simple terminal intended to be used with cell phones. I will post details about using it soon. In the meantime, take a look at the code, run sketches, and let me know what you think.
Original Post: https://www.circuitsathome.com/mcu/mass-storage-support-for-usb-host-library-2-0-released
Check out the homepage www.circuitsathome.com
FAQs About
What is usb2 host?
The MAX3421E-based USB Host Shield Library has been updated to version 2.0. HID devices, FTDI, ADK, ACM, PL2303, Bluetooth HID devices, SPP communication, and mass storage devices are all supported. It also works with PS3, PS4, PS5, PS Buzz, Wii, Switch Pro, and Xbox controllers.
What does a USB Host Shield do?
The Arduino USB host shield connects a USB device to the Arduino Board. Simply put, USB host shields enable Arduino boards to function as USB hosts. You can use this USB host shield to connect any USB device to the Arduino boards.
What is a host Shield?
The USB Host Shield is a low-cost development board intended for use in embedded applications that require USB Host functionality. The board supports full/low-speed USB 2.0 operation. The Arduino is the primary target platform, but it can also be used with any other micro equipped with an SPI interface.