SPI.setMOSI(myMOSIpin);
SPI.setSCK(mySCKpin);
SD.begin(myCSpin);
#include <MTP.h>
#include <SdFat.h>
SdFat SD;
#elif defined(USB_MTPDISK)
#define NUM_INTERFACE 2
#define MTP_INTERFACE 0 // MTP Disk
C:\Program Files\Arduino\libraries\MTP/MTP.h: In member function 'virtual uint32_t MTPStorage_SD::Create(uint32_t, bool, const char*)':
C:\Program Files\Arduino\libraries\MTP/MTP.h:375:38: warning: large integer implicitly truncated to unsigned type [-Woverflow]
OpenFileByIndex(ret, FILE_WRITE);
^
/** If the symbol USE_FCNTL_H is nonzero, open flags for access modes O_RDONLY,
* O_WRONLY, O_RDWR and the open modifiers O_APPEND, O_CREAT, O_EXCL, O_SYNC
* will be defined by including the system file fcntl.h.
*/
#if defined(__AVR__)
// AVR fcntl.h does not define open flags.
#define USE_FCNTL_H 0
#elif defined(PLATFORM_ID)
// Particle boards - use fcntl.h.
#define USE_FCNTL_H 1
#elif defined(__arm__)
// ARM gcc defines open flags.
#define USE_FCNTL_H 1
#else // defined(__AVR__)
#define USE_FCNTL_H 0
#endif // defined(__AVR__)
#undef USE_FCNTL_H
#define USE_FCNTL_H 0
For anyone having troubles getting the MTP code from yoonghm's repository to work, I've figured out what's going wrong with it.
The "MTP Disk (Experimental)" USB type choice creates three interfaces: CDC control, CDC data, and MTP. However, the configuration descriptor that is generated claims only two interfaces to exist, and the interface descriptors themselves have numbers 0, 1, and 0 respectively. Either the incorrect count, or the duplicate interface number, is enough to get Windows (7, at least) to completely reject the device - of course without giving any indication anywhere of exactly why the device is being rejected.
To fix, you need to edit the usb_desc.h file - the one you copied from the MTP library into your Teensyduino installation. Find the section that starts with:
Just under that, find:Code:#elif defined(USB_MTPDISK)
and change the number to 3, then a few lines further down find:Code:#define NUM_INTERFACE 2
and change the number to 2.Code:#define MTP_INTERFACE 0 // MTP Disk
Listing raw device(s)
Device 0 (VID=16c0 and PID=04d1) is UNKNOWN in libmtp v1.1.10.
Please report this VID/PID and the device model to the libmtp development team
Found 1 device(s):
16c0:04d1 @ bus 3, dev 90
Attempting to connect device
Listing File Information on Device with name: Teensy
NODEID: 7
unique: 39, success, outsize: 144
unique: 40, opcode: OPEN (14), nodeid: 7, insize: 48, pid: 27177
open flags: 0x8000 /SD Card/test-fixed-point.rb
unique: 40, error: -5 (Input/output error), outsize: 16
0.388959 gp_port_read (3): Reading 18 = 0x12 bytes from port...
0.389175 gp_libusb1_read [libusb1.c:577](0): 'libusb_bulk_transfer (port->pl->dh, port->settings.usb.inep, (unsigned char*)bytes, size, &curread, port->timeout)' failed: Overflow (-8)
0.389207 gp_port_read [gphoto2-port.c:437](0): Reading 18 = 0x12 bytes from port failed: Error reading from the port (-34)
Ok, I spoke a bit too soon about that making the code work - it at least gets the device to appear, but pretty much nothing other than creating empty folders would actually work. There was a warning during compilation that I should have paid closer attention to:
FILE_WRITE had a value of 0x4202, but that parameter (and everything else within the SDFat library that deals with file flags) is only a byte. Somehow the compiler was picking up a traditional Unix definition of the file opening flags (O_CREAT and all that) that has the bits scattered throughout an int, instead of the Arduino definition that squeezes them all into a byte. I traced this down to the file Arduino\libraries\SdFat\src\SdFatConfig.h:Code:C:\Program Files\Arduino\libraries\MTP/MTP.h: In member function 'virtual uint32_t MTPStorage_SD::Create(uint32_t, bool, const char*)': C:\Program Files\Arduino\libraries\MTP/MTP.h:375:38: warning: large integer implicitly truncated to unsigned type [-Woverflow] OpenFileByIndex(ret, FILE_WRITE); ^
I have absolutely no idea what the right solution is here, so I just added a couple of lines right after that to force the Arduino definitions:Code:/** If the symbol USE_FCNTL_H is nonzero, open flags for access modes O_RDONLY, * O_WRONLY, O_RDWR and the open modifiers O_APPEND, O_CREAT, O_EXCL, O_SYNC * will be defined by including the system file fcntl.h. */ #if defined(__AVR__) // AVR fcntl.h does not define open flags. #define USE_FCNTL_H 0 #elif defined(PLATFORM_ID) // Particle boards - use fcntl.h. #define USE_FCNTL_H 1 #elif defined(__arm__) // ARM gcc defines open flags. #define USE_FCNTL_H 1 #else // defined(__AVR__) #define USE_FCNTL_H 0 #endif // defined(__AVR__)
I haven't tested this very thoroughly yet, but at least I can copy a text file to the Teensy and read it back now.Code:#undef USE_FCNTL_H #define USE_FCNTL_H 0