Go Down

Topic: An example of Arduino Due as an USB host mass storage (Read 11193 times) previous topic - next topic

Palliser

Dec 02, 2012, 04:32 am Last Edit: Mar 05, 2014, 11:49 pm by Palliser Reason: 1
I wanted to share an example from Atmel ASF that shows how to implement a USB host mass storage on Arduino Due with USB module.

I have upload the .bin file in github for those interested to test it. If you want to move forward with this application, I'd recommend to install Atmel Studio 6 with ASF and build/compile the Arduino Due based example called "USB Host MSC FatFS example". I could write a sketch for Arduino IDE but you have to be patient.

For this application you will need:
- Arduino board
- Flash memory card (FAT or FAT32)
- A USB to mini-USB cable
- Bossac.exe in you computer
- Download my HOST_EXAMPLE21.bin file from github

Then, follow these steps:
1. Download the file HOST_EXAMPLE21.bin from this link:
https://github.com/downloads/Palliser/Arduino-DUE/HOST_EXAMPLE21.bin


[font=Verdana]1.1.  Download the file HOST_EXAMPLE21.bin attached to this post (EDITED: March-05-2014).[/font]
2. Load HOST_EXAMPLE21.bin to Arduino Due using bossac.exe as follows:

Quote
bossac.exe --port=COM23 -U false -e -w -v -b HOST_EXAMPLE21.bin -R


Notice that I am using port 23 because that is the port in my laptop where my Due is connected.

3. After loading HOST_EXAMPLE21.bin in your Due, connect Due to a Flash Memory card (FAT/FAT32 are supported) using the USB-mini USB cable.

4. Finally disconnect the memory stick from Due and read it in your computer. The example creates in your memory card (stick) a file "uhi_msc_test.txt" that contains the text: Arduino Due Rocks!

If you want to create your own file and text you'll find the example in Atmel Studio 6 as follows:
File->New->Example Project from ASF->[Under Kit] Arduino Due X->USB Host MSC FatFS example

Regards!

Wow, this looks nice!

Can it be ported to arduino IDE easily?

pracas

@Palliser

Waiting to test this now... Did you figure out how to upload directly from Atmel Studio onto Due?
Be The Change...

Palliser

Hello pracas. That is in my pending list to-do. I got my Atmel SAM-ICE JTAG emulator/programmer last week but still having issues to connect it with Due. It's a matter of time for me but I am a kind of busy with my work and other projects. Please, keep an eye of my post:
http://arduino.cc/forum/index.php/topic,134907.msg1014628.html#msg1014628

So far for my understanding, using a JTAG is the only way to program Due through AS6. You can do it with Arduino UNO without problems thanks to avrdude.exe, but the SAM3X8E is another story. regards!

mpsufi

Dear Palliser,
                 I have been looking for the USB host mass storage for Arduino Due and I found only your forum discussing it. I am in severe need of this thing, I tried bossac.exe approach but its showing that no device is found in com port where my arduino is there. Also I didnt understand the method of Atmel Studio 6. Though I downloaded atmel studio 6 and found that example, I dont know how to load the program to my arduino coz I dont have the SAM-JTAG or whatever it is. It would be very thankful to have a program for this in Arduino IDE. I know you are working on it, atleast till then by what approach I could make USB mass storage working?

Waiting for your reply restleslly
Always a techie...

trobinso

USB host mass storage is the only example I couldn't get to work on my due.  I'm a due newbie but it never receives an sof event when I plug my 2gb scandisk cruzer.
IE: This line:
Code: [Select]
if (main_usb_sof_counter > 2000) {
  // ..NEVER HAPPENS..
}


Has any one had any luck with this?

Regards,
Travis

darighi

Hi Travis,
me too I've problems with Arduino Due Host example.
First all, I'm not able to find the HOST_EXAMPLE21 of Palliser, but no problem, I compiled the Atmel ASF example and installed it into my arduino but me too I've not soft event.
Atmel said to me to format my Pen drive as FAT32 and try again, also with other Pen drive but for now anything of positive happens :).
Good luck and please tell me if you are able to run the USB Soft software.
best regards at all
Davide

trobinso

@darighi

Thanks for the reply.

I have tried FAT16 and FAT32 with a variety of sector sizes.  I think it has nothing to do with this.  I'm convinced it has something to do with VBUS power. When I press reset on the DUE board my flash stick will power for a second and then it shuts off as soon as the Atmel host stack initializes.

It is definitely a firmware problem because the Adruino host library works but unfortunately no MSC support! Furthermore, the Atmel MSC+HID mouse usb host example doesn't work either when connected to my Logitech trackball.  This same mouse works fine with the Arduino USBHost library (MouseController Example).

Obviously no one at Atmel/Adruino bothered to test this... As you say, Palliser's example doesn't exist but even money says it never worked in the first place!

I will keep you posted; please do the same. Feel free to PM me.

Regards,
Travis
travis_robinson@comcast.net

Author of:
libUsbK (http://sourceforge.net/projects/libusbk/)
LibUsbDotNet (http://sourceforge.net/projects/libusbdotnet/)

darighi

Hi Travis
Also on EVK1101 the host msc example
doesn't work.
Atmel wrote to me that he is able to run this
software on EVK1101 but My pen drive led
doesn't light on also.
Sorry for my bad English but I hope you understood what I told
I will keep you posted too if I will find a solution.
Good work
Davide

trobinso

I finally got it to work!

Lemme go my codes and I will post a reply shortly.

Regards,
Travis

trobinso

[SOLVED]

There were 3 problems working against us.

1) src/ASF/sam\boards/arduino_due_x/arduino_due_x.h
* Un-define USB_ID_GPIO and remove any references to it to force host mode.

2) src/ASF/sam/drivers/pmc/sleep.c
* pmc_save_clock_settings() and pmc_restore_clock_settings() don't work properly in ASF 3.7.2. These functions were re-worked with code from ASF 3.5.

3) src/ui.c
* ui_usb_sof_event()
* Reads a push button that doesn't exist on a stock board.
* This creates a scenario where the device will not work the first time it's plugged or if it is already connected when the application starts.

Download my re-worked version here:
http://usb-travis.googlecode.com/files/ARDUINO_DUE_USB_HOST_HIDMSC.zip

NOTE: When you create a new project with ASF 3.7.2, use sleep.c from the code above. Hopefully Atmel will fix the bugs in the next ASF release.

sleep.c
Code: [Select]

/**
* \file
*
* \brief Sleep mode access
*
* Copyright (c) 2013 Atmel Corporation. All rights reserved.
*
* \asf_license_start
*
* \page License
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
*    this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
*    this list of conditions and the following disclaimer in the documentation
*    and/or other materials provided with the distribution.
*
* 3. The name of Atmel may not be used to endorse or promote products derived
*    from this software without specific prior written permission.
*
* 4. This software may only be redistributed and used in connection with an
*    Atmel microcontroller product.
*
* THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* \asf_license_stop
*
*/

/* [TR]
* pmc_save_clock_settings() and pmc_restore_clock_settings() don't work properly
* in ASF 3.7.2. These functions were re-worked with code from ASF 3.5.
*/

#include <compiler.h>
#include "sleep.h"

/* SAM3 and SAM4 series */
#if (SAM3S || SAM3N || SAM3XA || SAM3U || SAM4S || SAM4E)
# include "pmc.h"
# include "board.h"

/**
* Save clock settings and shutdown PLLs
*/
__always_inline static void pmc_save_clock_settings(
uint32_t *p_osc_setting,
uint32_t *p_pll0_setting,
uint32_t *p_pll1_setting,
uint32_t *p_mck_setting)
{
if (p_osc_setting) {
*p_osc_setting = PMC->CKGR_MOR;
}
if (p_pll0_setting) {
*p_pll0_setting = PMC->CKGR_PLLAR;
}
if (p_pll1_setting) {
#if (SAM3S || SAM4S)
*p_pll1_setting = PMC->CKGR_PLLBR;
#elif (SAM3U || SAM3XA)
*p_pll1_setting = PMC->CKGR_UCKR;
#else
*p_pll1_setting = 0;
#endif
}
if (p_mck_setting) {
*p_mck_setting  = PMC->PMC_MCKR;
}

// Switch MCK to Main clock (internal or external 12MHz) for fast wakeup
// If MAIN_CLK is already the source, just skip
if ((PMC->PMC_MCKR & PMC_MCKR_CSS_Msk) == PMC_MCKR_CSS_MAIN_CLK) {
return;
}
// If we have to enable the MAIN_CLK
if ((PMC->PMC_SR & PMC_SR_MOSCXTS) == 0) {
// Intend to use internal RC as source of MAIN_CLK
pmc_osc_enable_fastrc(CKGR_MOR_MOSCRCF_12_MHz);
pmc_switch_mainck_to_fastrc(CKGR_MOR_MOSCRCF_12_MHz);
}
pmc_switch_mck_to_mainck(PMC_MCKR_PRES_CLK_1);
}

/**
* Restore clock settings
*/
__always_inline static void pmc_restore_clock_setting(
uint32_t osc_setting,
uint32_t pll0_setting,
uint32_t pll1_setting,
uint32_t mck_setting)
{
uint32_t mckr;
if ((pll0_setting & CKGR_PLLAR_MULA_Msk) &&
pll0_setting != PMC->CKGR_PLLAR) {
PMC->CKGR_PLLAR = 0;
PMC->CKGR_PLLAR = CKGR_PLLAR_ONE | pll0_setting;
while (!(PMC->PMC_SR & PMC_SR_LOCKA));
}
#if (SAM3S || SAM4S)
if ((pll1_setting & CKGR_PLLBR_MULB_Msk) &&
pll1_setting != PMC->CKGR_PLLBR) {
PMC->CKGR_PLLBR = 0;
PMC->CKGR_PLLBR = pll1_setting ;
while (!(PMC->PMC_SR & PMC_SR_LOCKB));
}
#elif (SAM3U || SAM3XA)
if ((pll1_setting & CKGR_UCKR_UPLLEN) &&
pll1_setting != PMC->CKGR_UCKR) {
PMC->CKGR_UCKR = 0;
PMC->CKGR_UCKR = pll1_setting;
while (!(PMC->PMC_SR & PMC_SR_LOCKU));
}
#else
UNUSED(pll1_setting);
#endif
/* Switch to faster clock */
mckr = PMC->PMC_MCKR;
// Set PRES
PMC->PMC_MCKR = (mckr & ~PMC_MCKR_PRES_Msk)
| (mck_setting & PMC_MCKR_PRES_Msk);
while (!(PMC->PMC_SR & PMC_SR_MCKRDY));
// Set CSS and others
PMC->PMC_MCKR = mck_setting;
while (!(PMC->PMC_SR & PMC_SR_MCKRDY));
/* Shutdown fastrc */
if (0 == (osc_setting & CKGR_MOR_MOSCRCEN)) {
pmc_osc_disable_fastrc();
}
}

/** If clocks are switched to FASTRC for WAIT mode */
static volatile bool b_is_fastrc_used = false;

void pmc_sleep(int sleep_mode)
{
switch (sleep_mode) {
case SAM_PM_SMODE_SLEEP_WFI:
case SAM_PM_SMODE_SLEEP_WFE:
#if (SAM4S || SAM4E)
SCB->SCR &= (uint32_t)~SCR_SLEEPDEEP;
cpu_irq_enable();
__WFI();
break;
#else
PMC->PMC_FSMR &= (uint32_t)~PMC_FSMR_LPM;
SCB->SCR &= (uint32_t)~SCR_SLEEPDEEP;
cpu_irq_enable();
if (sleep_mode == SAM_PM_SMODE_SLEEP_WFI)
__WFI();
else
__WFE();
break;
#endif
case SAM_PM_SMODE_WAIT: {
uint32_t mor, pllr0, pllr1, mckr;
cpu_irq_disable();
b_is_fastrc_used = true;
pmc_save_clock_settings(&mor, &pllr0, &pllr1, &mckr);

/* Enter wait mode */
cpu_irq_enable();
pmc_enable_waitmode();

cpu_irq_disable();
pmc_restore_clock_setting(mor, pllr0, pllr1, mckr);
b_is_fastrc_used = false;
cpu_irq_enable();
break;
}

case SAM_PM_SMODE_BACKUP:
SCB->SCR |= SCR_SLEEPDEEP;
#if (SAM4S || SAM4E)
SUPC->SUPC_CR = SUPC_CR_KEY(0xA5u) | SUPC_CR_VROFF_STOP_VREG;
cpu_irq_enable();
__WFI() ;
#else
cpu_irq_enable();
__WFE() ;
#endif
break;
}
}

bool pmc_is_wakeup_clocks_restored(void)
{
return !b_is_fastrc_used;
}

#endif /* #if (SAM3S || SAM3N || SAM3XA || SAM3U || SAM4S || SAM4E) */

Regards,
Travis

darighi

Fantastic!!!
Travis, I thank you very much!
As soon as possible I'll try to load your beautiful work on arduino.
I think that I'll have time only friday evening and then I'll tell you how does my trial is gone.
Thanks again, well done, beautiful job!
Davide

James46

Hi Travis,

I opened your project in Atmel Studio6, but only the project folders are visible, and I could not open file main.c (or any others) in the GUI.
http://usb-travis.googlecode.com/files/ARDUINO_DUE_USB_HOST_HIDMSC.zip

I presume project folders are expected to be portable???

My ASF is version 3.5.1. Perhaps this is the problem. Do you know how to upgrade to ASF 3.7.2, 3?

Regards,
James

darighi

Hi Jaes i'm Davide and me too I've problems to open the software.
I saw that Travis Use Studio 6.1, not 6.0. May be the problem is that.
Now I'm downloading the new version, "full"  that contain ASF 3.7.2 too.
goto: Help->Check for Updates

Best Regards
Davide

darighi

sorry in the previous message I missed an 'm'.... James.
:)

Go Up