Leonardo改装为USB读卡器

前面介绍了如何用 Leonardo 实现SD卡的读写,这次介绍如何实现将其改装为 USB SD 卡读卡器。

经过搜索在 http://elasticsheep.com/2010/04/teensy2-usb-mass-storage-with-an-sd-card/  给出了一个代码。结合上面提到的硬件可以完成功能(CS Pin 直接接地,此外其他连接相同)。

使用 C:\WinAVR-20100110\进行编译:

C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC>cd\
C:\>cd C:\WinAVR-20100110\SDCardReader
C:\WinAVR-20100110\SDCardReader>setenv.bat
C:\WinAVR-20100110\SDCardReader>PATH=C:\WinAVR-20100110\bin;C:\WinAVR-20100110\utils\bin;C:\Windows\System32\wbem
C:\WinAVR-20100110\SDCardReader>cd "LowLevelMassStorage+SD"
C:\WinAVR-20100110\SDCardReader\LowLevelMassStorage+SD>make
-------- begin --------
avr-gcc (WinAVR 20100110) 4.3.3
Copyright (C) 2008 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


Size before:
AVR Memory Usage
----------------
Device: atmega32u4
Program:    9094 bytes (27.8% Full)
(.text + .data + .bootloader)
Data:        712 bytes (27.8% Full)
(.data + .bss + .noinit)
Checking for invalid events...
---- Compile Time Library Options ----
USB_DEVICE_ONLY
FIXED_CONTROL_ENDPOINT_SIZE=8
FIXED_NUM_CONFIGURATIONS=1
USE_FLASH_DESCRIPTORS
USE_STATIC_OPTIONS=(USB_DEVICE_OPT_FULLSPEED | USB_OPT_REG_ENABLED | USB_OPT_AUTO_PLL)
INTERRUPT_CONTROL_ENDPOINT
--------------------------------------
--------- Target Information ---------
AVR Model: atmega32u4
Board: USER
Clock: 16000000Hz CPU, 16000000Hz Master
--------------------------------------
Size after:
AVR Memory Usage
----------------
Device: atmega32u4

Program:    9094 bytes (27.8% Full)
(.text + .data + .bootloader)
Data:        712 bytes (27.8% Full)
(.data + .bss + .noinit)
-------- end --------

编译之后生成 MassStorage.hex 文件,我使用Arduino的上传方式进行烧写,烧写之前按下 reset  button 然后马上运行,特别注意根据你自己的实际情况选择COM,我这里是 COM7 (意思是按下 Reset 之后出现的 Bootloader 的COM号):

D:\arduino-1.8.4\hardware\tools\avr\bin\avrdude -v -Cd:\arduino-1.8.4\hardware\tools\avr\etc\avrdude.conf -patmega32u4 -cavr109 -PCOM7 -b57600 -D -V -Uflash:w:./MassStorage.hex:i

烧写之后就可以正常工作了。

写入速度

读取速度能快一点

完整的代码和Lufa 下载(根据我的测试只有特定的 Lufa版本LUFA_091223才能通过编译,所以放在一起了,另外,为了减小体积我删除了 Lufa 里面的 Demo)。

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注