[HACK] LUFA MassStorage on Atmega32U4

A shrunk down Leonardo-compatible board, thumb drive sized with native USB support. [Product info]
Post Reply
Loic
Posts: 3
Joined: Mon Apr 11, 2016 8:27 pm

[HACK] LUFA MassStorage on Atmega32U4

Post by Loic » Mon Apr 11, 2016 9:31 pm

Hello every one, 8-)

I have a pro micro arduino (clone with Atmega32U4) and I would like to use LUFA in order to make a SD card as USB.
So I did like LukeW said on this thread viewtopic.php?t=5828

When I plug my arduino, it will be seen as "MediaPlayer" (what it is supposed to do) but only after some random time (10sec-2min), and the mass storage (using a 16 GB SD card with a SPI connection) doesn't work.

Then, after another random time (10sec-2min) it is seen as a USB key (besides "MediaPlayer") but ask me to insert a disk or to format it or says "not available slot'.


It does the same if a SD card is inserted or not .

Yet, I heve modified /Lib/sd_raw_config.h (accordingly to my arduino datasheet http://cdn.sparkfun.com/datasheets/Dev/ ... o_v13b.pdf) for the pins:

...and put the SD_RAW_SDHC macro to 1

Code: Select all

**
 * \ingroup sd_raw_config
 * Controls support for SDHC cards.
 *
 * Set to 1 to support so-called SDHC memory cards, i.e. SD
 * cards with more than 2 gigabytes of memory.
 */
#define SD_RAW_SDHC 1

/**
 * @}
 */

/* defines for customisation of sd/mmc port access */
#if defined(__AVR_ATmega8__) || \
    defined(__AVR_ATmega48__) || \
    defined(__AVR_ATmega88__) || \
    defined(__AVR_ATmega168__) || \
    defined(__AVR_ATmega328__)
    #define configure_pin_mosi() DDRB |= (1 << DDB3)
    #define configure_pin_sck() DDRB |= (1 << DDB5)
    #define configure_pin_ss() DDRB |= (1 << DDB2)
    #define configure_pin_miso() DDRB &= ~(1 << DDB4)

    #define select_card() PORTB &= ~(1 << PB2)
    #define unselect_card() PORTB |= (1 << PB2)
#elif defined(__AVR_ATmega16__) || \
      defined(__AVR_ATmega32__)
    #define configure_pin_mosi() DDRB |= (1 << DDB5)
    #define configure_pin_sck() DDRB |= (1 << DDB7)
    #define configure_pin_ss() DDRB |= (1 << DDB4)
    #define configure_pin_miso() DDRB &= ~(1 << DDB6)

    #define select_card() PORTB &= ~(1 << PB4)
    #define unselect_card() PORTB |= (1 << PB4)
#elif defined(__AVR_ATmega64__) || \
      defined(__AVR_ATmega128__) || \
      defined(__AVR_ATmega169__)
    #define configure_pin_mosi() DDRB |= (1 << PB2)
    #define configure_pin_sck() DDRB |= (1 << PB1)
    #define configure_pin_ss() DDRB |= (1 << PB4)
    #define configure_pin_miso() DDRB &= ~(1 << PB3)

    #define select_card() PORTB &= ~(1 << PB0)
    #define unselect_card() PORTB |= (1 << PB0)
#elif defined(__AVR_ATmega32U4__) || \
      defined(__AVR_AT90USB1286__) || \
      defined(__AVR_AT90USB1287__)
    #define configure_pin_mosi() DDRB |= (1 << DDB2)
    #define configure_pin_sck() DDRB |= (1 << DDB1)
    #define configure_pin_ss() DDRB |= (1 << DDB0)
    #define configure_pin_miso() DDRB &= ~(1 << DDB3)

    #define select_card() PORTB &= ~(1 << PORTB0)
    #define unselect_card() PORTB |= (1 << PORTB0)
#else
    #error "no sd/mmc pin mapping available!"
#endif

// From Sparkfun OpenLog
//My 2 hour pitfall: If not using card detect or write protect, assign these values:
#define configure_pin_available() DDRC &= ~(1 << DDC4)
#define configure_pin_locked() DDRC &= ~(1 << DDC5)
#define configure_pin_available() //Do nothing
#define configure_pin_locked() //Do nothing

//#define get_pin_available() ((PINC >> PC4) & 0x01)
//#define get_pin_locked() ((PINC >> PC5) & 0x01)
#define get_pin_available() (0) //Emulate that the card is present
#define get_pin_locked() (1) //Emulate that the card is always unlocked

#if SD_RAW_SDHC
    typedef uint64_t offset_t;
#else
    typedef uint32_t offset_t;
#endif

/* configuration checks */
#if SD_RAW_WRITE_SUPPORT
#undef SD_RAW_SAVE_RAM
#define SD_RAW_SAVE_RAM 0
#else
#undef SD_RAW_WRITE_BUFFERING
#define SD_RAW_WRITE_BUFFERING 0
#endif

#ifdef __cplusplus
}
#endif

#endif


The makefile too for the microcontroller name, clock ...:


Code: Select all

# MCU name (changed for project MP3 Player)
MCU = atmega32u4


# Target board (see library "Board Types" documentation, NONE for projects not requiring
# LUFA board drivers). If USER is selected, put custom board drivers in a directory called
# "Board" inside the application directory.
# (changed for project MP3 Player)
BOARD = NONE


# Processor frequency.
#     This will define a symbol, F_CPU, in all source code files equal to the
#     processor frequency in Hz. You can then use this symbol in your source code to
#     calculate timings. Do NOT tack on a 'UL' at the end, this will be done
#     automatically to create a 32-bit value in your source code.
#
#     This will be an integer division of F_CLOCK below, as it is sourced by
#     F_CLOCK after it has run through any CPU prescalers. Note that this value
#     does not *change* the processor frequency - it should merely be updated to
#     reflect the processor speed set externally so that the code can use accurate
#     software delays.
F_CPU = 8000000


# Input clock frequency.
#     This will define a symbol, F_CLOCK, in all source code files equal to the
#     input clock frequency (before any prescaling is performed) in Hz. This value may
#     differ from F_CPU if prescaling is used on the latter, and is required as the
#     raw input clock is fed directly to the PLL sections of the AVR for high speed
#     clock generation for the USB and other AVR subsections. Do NOT tack on a 'UL'
#     at the end, this will be done automatically to create a 32-bit value in your
#     source code.
#
#     If no clock division is performed on the input clock inside the AVR (via the
#     CPU clock adjust registers or the clock division fuses), this will be equal to F_CPU.
F_CLOCK = 16000000


# Output format. (can be srec, ihex, binary)
FORMAT = ihex


# Target file name (without extension).
TARGET = MassStorage


# Object files directory
#     To put object files in current directory, use a dot (.), do NOT make
#     this an empty or blank macro!
OBJDIR = .


# Path to the LUFA library
LUFA_PATH = ./LUFA
... and for the programmer I use ( Usbasp):

Code: Select all

#---------------- Programming Options (avrdude) ----------------

# Programming hardware
# Type: avrdude -c ?
# to get a full listing.
# Frank: place appropriate method here, although you should be able to use the default Teensy++ Bootloader
#
AVRDUDE_PROGRAMMER = usbasp

# com1 = serial port. Use lpt1 to connect to parallel port.
AVRDUDE_PORT = //./com1

AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
So why it doesn't work ?????? :cry: :cry: :cry:

Thanks in advance :mrgreen:
Last edited by Loic on Fri May 27, 2016 9:07 am, edited 1 time in total.

Loic
Posts: 3
Joined: Mon Apr 11, 2016 8:27 pm

Re: LUFA MassStorage on Atmega32U4

Post by Loic » Thu May 12, 2016 6:24 pm

Up please !!! :idea:

Post Reply