-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from skobkars/master
SdFat32 support
- Loading branch information
Showing
8 changed files
with
358 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.