//WMXZ-EU mtp-basic-SDIO
#include "SPI.h"
#include "SD.h"
#include "MTP.h"
#define USE_SD 1 // SDFAT based SDIO and SPI
#if USE_EVENTS==1
extern "C" int usb_init_events(void);
#else
int usb_init_events(void) {}
#endif
#if defined(__IMXRT1062__)
// following only as long usb_mtp is not included in cores
#if !__has_include("usb_mtp.h")
#include "usb1_mtp.h"
#endif
#else
#ifndef BUILTIN_SDCARD
#define BUILTIN_SDCARD 254
#endif
void usb_mtp_configure(void) {}
#endif
/**** Start device specific change area ****/
// SDClasses
#if USE_SD==1
// edit SPI to reflect your configuration (following is for T4.1)
#define SD_MOSI 11
#define SD_MISO 12
#define SD_SCK 13
#if defined (BUILTIN_SDCARD)
const char *sd_str[]={"sdio","sd1"}; // edit to reflect your configuration
const int cs[] = {BUILTIN_SDCARD,10}; // edit to reflect your configuration
#else
const char *sd_str[]={"sd1"}; // edit to reflect your configuration
const int cs[] = {10}; // edit to reflect your configuration
#endif
const int nsd = sizeof(sd_str)/sizeof(const char *);
SDClass sdx[nsd];
#endif
MTPStorage_SD storage;
MTPD mtpd(&storage);
void storage_configure()
{
// Using SD card for storage
#if USE_SD==1
#if defined SD_SCK
SPI.setMOSI(SD_MOSI);
SPI.setMISO(SD_MISO);
SPI.setSCK(SD_SCK);
#endif
for(int ii=0; ii<nsd; ii++)
{
if(cs[ii]<BUILTIN_SDCARD)
{
pinMode(cs[ii],OUTPUT); digitalWriteFast(cs[ii],HIGH);
}
if(!sdx[ii].begin(cs[ii]))
{ Serial.printf("SD/SDIO Storage %d %d %s failed or missing",ii,cs[ii],sd_str[ii]); Serial.println();
}
else
{
storage.addFilesystem(sdx[ii], sd_str[ii]);
uint64_t totalSize = sdx[ii].totalSize();
uint64_t usedSize = sdx[ii].usedSize();
Serial.printf("SDIO Storage %d %d %s ",ii,cs[ii],sd_str[ii]);
Serial.print("; total "); Serial.print(totalSize); Serial.print(" used: "); Serial.println(usedSize);
}
}
#endif
}
void setup()
{
while(!Serial);
Serial.println("MTP_test");
#if USE_EVENTS==1
usb_init_events();
#endif
#if !__has_include("usb_mtp.h")
usb_mtp_configure();
#endif
storage_configure();
}
void loop()
{
mtpd.loop();
#if USE_EVENTS==1
if(Serial.available())
{
char ch=Serial.read();
Serial.println(ch);
if(ch=='r')
{
Serial.println("Reset");
mtpd.send_DeviceResetEvent();
}
}
#endif
}