USB Host – Writing and Reading files from an USB thumbdrive

  1. What this example do?
  2. How I must configure my ORbit16™ to work with this example?
  3. Where I can learn more about this?
  4. Download this example!

What this example do?

This demo uses the Microchip File System IO library for write/read/modify operations on an USB thumbdrive.

WARNING: I’ve not tested this demo with an USB hard-drive. If you want to try, make it at your own risk!

First time the thumbdrive is attached, the firmware searches a file named “NAME.TXT”. If this file is found, first 20 bytes are readed from it. A second file, called “ORBIT16.TXT” will be created with some text and the 20 bytes previously readed. If a file “NAME.TXT” is not found, another message will be written in the “ORBIT16.TXT” file. Finally a new line is attached at the end of the file “ORBIT16.TXT” to show the use of ‘append’ mode. Those operations are made only first time thumbdrive is attached after an ORbit16 reset.

So starts a simple usb thumbdrive data-logger: every time the button (SW2) is pressed, a counter is incremented. Actual value of the counter is stored in a binary file called “COUNT.CNT”. When counter increments, a new line with the new value of the counter is attached in a text file called “PUSHES.TXT”. If you reset ORbit16 or detach the thumbdrive the most recent value of the counter is loaded by reading “COUNT.CNT” file. This is a base for a simple data-logger.

More info:

The file ORBIT16.TXT is opened in ‘W’ (write) mode: the file is created, if a file with same name exists, it will be deleted then will be created a blank new one with the same name. So beware opening a file in ‘W’ mode if you don’t want to lose its data.

The file PUSHES.TXT is openend in ‘A’ (append) mode: if the file doesn’t exists, it will be created but if already exists it will be not deleted but the new data will be attached to it. File COUNT.CNT also is opened in ‘W’ mode. File “NAME.TXT” is opened in ‘R’ (read) mode since must be only readed.

Since the counter is a 16-bit number, in the file COUNT.CNT we’ll divide counter in two bytes (high byte and low byte) and then save those 2 bytes in the file.

Check the thumbdrive in your pc to see the magic!

How I must configure my ORbit16™ to work with this example?

First, program the ORbit16™ with USB selector in ‘Device Mode’ in order to make bootloader work. When programming has finished, you must put USB selector in ‘Host’ mode. Please refer to Jumper Settings document in the Documents Section.

You can read on a terminal software (such as Hyperterminal, with the settings 57600,8,N,1) the various debug messages. If you want to do that, a 3V TTL serial adapter is required but is not needed for main application functions.

Most of USB thumbdrives requires a small amount of current, so you can power the ORbit16™ also from USB host connector. If more power is required (>400mA). You must use external power and close the jumper VBUS_ON.

Where I can learn more about this?

My advice is to read the following free document:

  • Application Note 1045 : Implementing File I/O Functions Using Microchip’s Memory Disk Drive File System Library (english language).
    You can download this PDF here
    Reading document this will give you a lot of useful information on Filesystem and File I/O functions used in this demo.

Download this example!

USB Host - Thumbdrive demo (188 downloads)