SD Cards + USB + Microcontroller
Moderators: adafruit_support_bill, adafruit

SD Cards + USB + Microcontroller

by EasternStarGeek on Sat Dec 29, 2012 12:13 pm

Folks:

Many commercial devices using SD Flash Memory have a USB connection and appear as a mass storage device when plugged into a PC.
What must be added to a naked SD card that is normally working with a home-brew microcontroller, such as an Arduino, to make it accessible that way?

I don't necessarily need to use the USB port for anything else, like accessing the arduino bootloader port right now- I just want an easy way to hook up a PC to my gadget without having to remove the SD card and putting into a card reader.

Many thanks!
Last edited by EasternStarGeek on Sat Dec 29, 2012 12:45 pm, edited 1 time in total.
EasternStarGeek
 
Posts: 317
Joined: Mon Dec 13, 2010 1:39 pm
Location: Upper Eastern Tennessee

Re: SD Cards + USB + Microcontroller

by adafruit_support_rick on Sat Dec 29, 2012 12:36 pm

Offhand, I don't know if there is some simple gizmo you can add to the mix. I'd have to research that. But I can give you the software approach:

You need a USB driver with a Mass Storage Device profile. On an arduino, you would have to write something to support that.

Fortunately, there is Dean Camera's LUFA library, which is an excellent place to get started. I haven't used it for a mass-storage device, but I have used it for a serial client device. Very easy to work with.

The issue for you is going to be, what kind of arduino are you using?

On a Uno, the USB controller is not on the ATMega 328 chip - it's on the ATtiny co-processor. LUFA supports that chip, but you can't program it like am Arduino - you would have to use native tools: an IDE like Atmel Studio 6, or just the plain avr-gcc command-line tools. You need a ISP programmer like the USBtinyISP to load the code onto it. Once that's loaded, the ATmega would still have to communicate with the ATtiny via serial, since that's their only connection. So you would need some sort of comm protocol between the two.

On a Leonardo or Micro other ATmega32U4-based board, the USB controller is in the 32U4, and you can access it directly from a normal sketch. Pretty easy to see that this would be much easier to do on a 32U4.

Potentially, you could add something like an Arduino Micro as a dedicated USB Mass Storage client. You could either come up with some sort of comm protocol between the 'main' arduino and the Micro, or you could potentially put the Micro on the same SPI bus as the SD card. You'd have to have some sort of arbitration scheme so that both MCUs don't try to use the bus at the same time (I don't know if there would be any electrical issues with two bus masters - that would be something else to research).
Last edited by adafruit_support_rick on Sat Dec 29, 2012 12:37 pm, edited 1 time in total.
Reason: forgot LUFA link

adafruit_support_rick
 
Posts: 12827
Joined: Tue Mar 15, 2011 11:42 am
Location: Buffalo, NY

Re: SD Cards + USB + Microcontroller

by EasternStarGeek on Sat Dec 29, 2012 12:45 pm

Thank you for the advice-

I was really not thinking about a specific Arduino platform, just using that as an example.

What I actually had in mind was seeing if it is feasible to do this in hardware- whereby a specialized chip of some sort could turn the SD card into a MSD, then I could noodle around with the connections between the MSD chip and SD card to see if I could, using some glue logic, steer the SD card to the MSD chip, or to the microcontroller.

Of course, your suggestion using the LUFA with the Leonardo sounds much more elegant, but I don't know what kind of resources are required by the LUFA code, which could possibly endanger the main project.

I guess what I am asking would be to find a single-chip MSD interface solution, learn how it works, and then get creative on the HW side. I am principally a HW guy, after all!
EasternStarGeek
 
Posts: 317
Joined: Mon Dec 13, 2010 1:39 pm
Location: Upper Eastern Tennessee

Re: SD Cards + USB + Microcontroller

by paulstoffregen on Sun Dec 30, 2012 12:17 pm

Teensy 2.0 has support for this. From Arduino (with the Teensyduino add-on installed), just select USB Disk (SD card) from the Tools > USB Type menu.

https://www.adafruit.com/products/199
paulstoffregen
 
Posts: 305
Joined: Sun Oct 11, 2009 11:23 am
Location: Portland, Oregon, USA

Re: SD Cards + USB + Microcontroller

by EasternStarGeek on Sun Dec 30, 2012 2:46 pm

I think I now know in which direction I will go.
Many thanks to those who helped!
EasternStarGeek
 
Posts: 317
Joined: Mon Dec 13, 2010 1:39 pm
Location: Upper Eastern Tennessee

Re: SD Cards + USB + Microcontroller

by av8or1 on Mon May 06, 2013 12:00 pm

EasternStarGeek-

Did you ever get this working? I need this capability too, that is to use the SD card to log data but to have that data be accessible by an outside user without the need to dig in to the device and remove the SD card, put it into an SD card reader and get their data that way. So did you find a way to make the SD card appear as a MSD such that a user could connect to it and get their data over USB?

Thanks!
av8or1
 
Posts: 21
Joined: Tue Apr 23, 2013 11:29 am

Re: SD Cards + USB + Microcontroller

by fat16lib on Mon May 06, 2013 6:04 pm

See this post for the status of the Teensy 2.0 software

http://arduino.cc/forum/index.php?topic=143215.msg1077296#msg1077296

I started work towards this on Teensy 2.0. Honestly, this is still a work-in-progress. It's more difficult than I had originally thought. There are 3 rather difficult problems:
fat16lib
 
Posts: 593
Joined: Wed Dec 24, 2008 1:54 pm

Re: SD Cards + USB + Microcontroller

by av8or1 on Mon May 06, 2013 8:49 pm

fat16lib-

Your post confuses me. I had already read that thread. It didn't contain much and hasn't been updated since January. My post was to ask EasternStarGeek if he went the suggested route or if he came up with something else, and primarily, did he get anything working. If so, what?

Thanks
av8or1
 
Posts: 21
Joined: Tue Apr 23, 2013 11:29 am

Re: SD Cards + USB + Microcontroller

by fat16lib on Tue May 07, 2013 9:35 am

Sorry, I assumed that you had not read Paul Stoffregen's post that explains why is is not possible to share an SD with both USB access by a PC/MAC and read/write by an Arduino application.

The basic reason is that there is a cache in the PC/Mac file system and in the Arduino FAT library. I wrote SdFat which is also the base for the official Arduino SD library and it is not designed to have the PC/Mac also maintaining a cache.

I now realize that you probably don't need this type access. You could have two modes where either the PC has exclusive access to the SD or you are logging and SdFat has exclusive access.

Several people have made USB mass storage work on Teensy. Here is one http://elasticsheep.com/2010/04/teensy2-usb-mass-storage-with-an-sd-card/.

The Teensy++ 2.0 is probably your best bet for this app. You should post your requirement on Paul Stoffregen's Teensy forum http://forum.pjrc.com/forum.php.

Paul designed Teensy and wrote the Teensyduino software.

Also see this http://www.instructables.com/id/Music-Playing-Alarm-Clock/step6/USB-Mass-Storage-with-MicroSD/.
fat16lib
 
Posts: 593
Joined: Wed Dec 24, 2008 1:54 pm

Re: SD Cards + USB + Microcontroller

by av8or1 on Tue May 07, 2013 10:48 am

Thanks fat16lib!
av8or1
 
Posts: 21
Joined: Tue Apr 23, 2013 11:29 am

Re: SD Cards + USB + Microcontroller

by EasternStarGeek on Thu May 09, 2013 7:13 pm

Hiya,

I'm sorry- I had to shelve this project for the time being, and I have nothing new to report.
EasternStarGeek
 
Posts: 317
Joined: Mon Dec 13, 2010 1:39 pm
Location: Upper Eastern Tennessee

Re: SD Cards + USB + Microcontroller

by zachtos on Fri Oct 11, 2013 9:58 pm

USB Interface IC USB to SPI Protocol Converter,alternate to program sd card over usb
part# MCP2210-I/SO


Would this do it? I'm looking for the same, a simple way to plug in USB to program a SD card that is inside an enclosure not easily accessed. Microcontroller does not need to be on at the same time if that helps. Just a simple USB key type approach.
zachtos
 
Posts: 77
Joined: Sun May 03, 2009 8:18 pm

Re: SD Cards + USB + Microcontroller

by chuckylo on Fri Sep 26, 2014 11:28 pm

I am looking for a solution to this and I want to share my current results.

I found a chip that can do this trick: MAX14502. Unfortunately it is obsolete now. Currently I'm considering the USB2240 which is a USB SD card controller. If you keep this in reset it will tri-state all the SD lines. This will allow you to share the lines with a microcontroller. Then I want to use the USB voltage to detect if a PC has been connected (you can also use a USB charger detector to distinguish between a PC or charger). When the microcontroller detects that a host is connected it finishes any pending writes to the SD card, releases the lines and deasserts the USB2240 reset line. This chip will enumerate and the SD card will appear as a drive on the PC. I did some basic tests with a USB2240 evaluation board and so far it looks promising.

chuckylo
 
Posts: 1
Joined: Fri Sep 26, 2014 11:18 pm