Skip to content

Commit

Permalink
Merge pull request #103 from skobkars/master
Browse files Browse the repository at this point in the history
SdFat32 support
  • Loading branch information
chegewara authored Jul 20, 2022
2 parents dcd6750 + 8478fc1 commit 567ffd7
Show file tree
Hide file tree
Showing 8 changed files with 358 additions and 33 deletions.
86 changes: 86 additions & 0 deletions examples/device/msc/sdfat32/sdfat32.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
* Simple MSC device with SdFatUSB file system on SD card
* author: sergsk
*/

#include "sdfatusb.h"

#if CFG_TUD_MSC

#define CS 15

SDFat2USB SdFatUSB;
File32 root;
File32 file;


bool fs_changed = false;

class MyMSCCallbacks : public MSCCallbacks {

void onInquiry(uint8_t lun, uint8_t vendor_id[8], uint8_t product_id[16], uint8_t product_rev[4]) {
memcpy(vendor_id, "Test", 4);
memcpy(product_id, "Test Device", 11);
memcpy(product_rev, "1.0", 3);
}

void onFlush(uint8_t lun) {
getDefaultCallbacks()->onFlush(lun);
fs_changed = true;
}
};


void setup() {

SdFatUSB.setCallbacks( new MyMSCCallbacks() );

if( SdFatUSB.initSD( CS ) ) {
if( SdFatUSB.begin() ) {
log_d("MSC lun 1 begin");

} else log_e("LUN 1 failed");

} else log_d("Failed to init SD");

}

void loop()
{
delay(1000);

if( SdFatUSB.isReady() && fs_changed) {

log_d("==============================================================================================");

uint16_t mdate, mtime;
char nm[64];
uint32_t sz;

root.open("/");
while( file.openNext(&root, O_RDONLY) ) {

if( file.isFile() && !file.isHidden() && file.getModifyDateTime(&mdate, &mtime) ) {
file.getName(nm, 64);

byte yy = mdate >> 9;
byte mm = ( mdate & 0b0000000111100000 ) >> 5;
byte dd = mdate & 0b11111;

byte hr = mtime >> 11;
byte mn = ( mtime & 0b0000011111100000 ) >> 5;
byte sc = (( mtime & 0b11111 ) << 1); // + dir.creationTimeTenths / 100.0;

log_d("%-64s: %8u %4d-%02d-%02d %02d:%02d:%02d", nm, file.fileSize(), 1980 + yy, mm, dd, hr, mn, sc);

}
file.close();
}
root.close();

fs_changed = false;
}

}

#endif
6 changes: 4 additions & 2 deletions src/device/msc/flashdisk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class FlashCallbacks : public MSCCallbacks {
public:
FlashCallbacks(FlashUSB* ram, wl_handle_t handle, int pdrv) { m_parent = ram; wl_handle_thandle = handle; s_pdrv = pdrv; }
~FlashCallbacks() { }
void onInquiry(uint8_t lun, uint8_t vendor_id[8], uint8_t product_id[16], uint8_t product_rev[4])
void onInquiry(uint8_t lun, uint8_t vendor_id[8], uint8_t product_id[16], uint8_t product_rev[4])
{
if (m_parent->m_private)
{
Expand Down Expand Up @@ -128,6 +128,8 @@ class FlashCallbacks : public MSCCallbacks {
return bufsize;
}
}

void onFlush(uint8_t lun) {}
};

FlashUSB::FlashUSB(bool _aes)
Expand All @@ -149,7 +151,7 @@ bool FlashUSB::begin(char* str)

bool FlashUSB::init(char* path, char* label)
{
esp_vfs_fat_mount_config_t mount_config =
esp_vfs_fat_mount_config_t mount_config =
{
.format_if_mount_failed = true,
.max_files = 5,
Expand Down
12 changes: 12 additions & 0 deletions src/device/msc/mscusb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,18 @@ TU_ATTR_WEAK int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offs
return -1;
}

TU_ATTR_WEAK void tud_msc_write10_complete_cb(uint8_t lun)
{
for (size_t i = 0; i < 4; i++)
{
if(_MSCusb[i] && _MSCusb[i]->m_callbacks && _MSCusb[i]->m_lun == lun) {
return _MSCusb[i]->m_callbacks->onFlush(lun);
}
}

return;
}

TU_ATTR_WEAK int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer, uint16_t bufsize)
{

Expand Down
5 changes: 3 additions & 2 deletions src/device/msc/ramdisk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class RAMCallbacks : public MSCCallbacks {
public:
RAMCallbacks(USBramdisk* ram) { m_parent = ram; }
~RAMCallbacks() { }
void onInquiry(uint8_t lun, uint8_t vendor_id[8], uint8_t product_id[16], uint8_t product_rev[4])
void onInquiry(uint8_t lun, uint8_t vendor_id[8], uint8_t product_id[16], uint8_t product_rev[4])
{
if (m_parent->m_private)
{
Expand Down Expand Up @@ -88,6 +88,7 @@ class RAMCallbacks : public MSCCallbacks {

return bufsize;
}
void onFlush(uint8_t lun) {}
};

USBramdisk::USBramdisk( )
Expand All @@ -114,7 +115,7 @@ bool USBramdisk::begin(char* str)
setContent(&ram_disk_demo[0][0], sizeof(ram_disk_demo));
log_e("init ram disk size: %d", sizeof(ram_disk)/sizeof(*ram_disk));
ram_disk[20] = (uint8_t)(block_count >> 8);
ram_disk[19] = (uint8_t)(block_count & 0xff);
ram_disk[19] = (uint8_t)(block_count & 0xff);
}
return MSCusb::begin(str);
}
Expand Down
5 changes: 3 additions & 2 deletions src/device/msc/sdcard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class SDCallbacks : public MSCCallbacks {
public:
SDCallbacks(SDCard2USB* ram) { m_parent = ram; }
~SDCallbacks() { }
void onInquiry(uint8_t lun, uint8_t vendor_id[8], uint8_t product_id[16], uint8_t product_rev[4])
void onInquiry(uint8_t lun, uint8_t vendor_id[8], uint8_t product_id[16], uint8_t product_rev[4])
{
if (m_parent->m_private)
{
Expand Down Expand Up @@ -76,6 +76,7 @@ class SDCallbacks : public MSCCallbacks {

return bufsize;
}
void onFlush(uint8_t lun) {}
};

SDCard2USB::SDCard2USB( )
Expand Down Expand Up @@ -111,7 +112,7 @@ bool SDCard2USB::initSD(int8_t sck, int8_t miso, int8_t mosi, int8_t ss)
Serial.println("Card Mount Failed");
return false;
}

uint8_t cardType = SD.cardType();

if(cardType == CARD_NONE){
Expand Down
116 changes: 116 additions & 0 deletions src/device/msc/sdfatusb.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#include "sdfatusb.h"

#ifdef CFG_TUD_MSC

SDFat2USB::SDFat2USB() {
MSCusb::setCallbacks( this );
}


// MSCusb

bool SDFat2USB::begin(char* str) {
assert(sdBlockCount);
assert(sdBlockSize);
return MSCusb::begin(str);
}

bool SDFat2USB::initSD(SdCsPin_t ssPin, uint32_t maxSck) {
if( !sdFat.begin(ssPin, maxSck) ) {
log_e("Card Mount Failed. Check if card is inserted and proper SS pin configured");
return false;
}
sdSSPin = ssPin;
sdSck = maxSck;
sdBlockCount = sdFat.card()->sectorCount();
sdBlockSize = sdFat.bytesPerSector();
sdCardReady = true;
return true;
}

bool SDFat2USB::initSD(SdCsPin_t ssPin) {
return initSD(ssPin, SPI_FULL_SPEED);
}

void SDFat2USB::setCapacity(uint32_t count, uint32_t size) {
sdBlockCount = count;
sdBlockSize = size;
}

void SDFat2USB::setCallbacks(MSCCallbacks* cb) {
cb->setDefaultCallbacks(this);
MSCusb::setCallbacks( cb );
}

void SDFat2USB::ready(bool ready) {
sdCardReady = ready;
}

bool SDFat2USB::isReady() {
return sdCardReady;
}


// MSCCallbacks

void SDFat2USB::onInquiry(uint8_t lun, uint8_t vendor_id[8], uint8_t product_id[16], uint8_t product_rev[4]) {
const char vid[] = "ESP32-S2";
const char pid[] = "SDFAT32";
const char rev[] = "1.0";
memcpy(vendor_id , vid, strlen(vid));
memcpy(product_id , pid, strlen(pid));
memcpy(product_rev, rev, strlen(rev));
log_v("default onInquiry");
}

bool SDFat2USB::onReady(uint8_t lun) {
log_v("default onReady");
return sdCardReady;
}

void SDFat2USB::onCapacity(uint8_t lun, uint32_t* block_count, uint16_t* block_size) {
(void) lun;
*block_count = sdBlockCount;
*block_size = sdBlockSize;
log_v("default onCapacity: disk block count: %d, block size: %d", *block_count, *block_size);
}

bool SDFat2USB::onStop(uint8_t lun, uint8_t power_condition, bool start, bool load_eject) {
(void) lun;
(void) power_condition;
if ( load_eject ) {
if (start) { // load disk storage
log_d("default start/stop load");
return sdSSPin ? initSD(sdSSPin, sdSck) : false;

} else { // unload disk storage
log_d("default start/stop unload");
sdFat.end();
ready(false);
}
}
return true;
}

int32_t SDFat2USB::onRead(uint8_t lun, uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize) {
log_v("default onRead");
(void) lun;
(void) offset;
return sdFat.card()->readSectors(lba, (uint8_t*) buffer, bufsize / sdBlockSize) ? bufsize : -1;
}

int32_t SDFat2USB::onWrite(uint8_t lun, uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize) {
log_v("default onWrite");
(void) lun;
(void) offset;
return sdFat.card()->writeSectors(lba, (uint8_t*) buffer, bufsize / sdBlockSize) ? bufsize : -1;
}

void SDFat2USB::onFlush(uint8_t lun) {
log_v("default onFlush");
(void) lun;
sdFat.card()->syncDevice();
sdFat.cacheClear();
}

#endif
Loading

0 comments on commit 567ffd7

Please sign in to comment.