Replies: 1 comment
-
MicroPython on ESP32 S3 doesn't support USB MSC mode. There's an open issue about it here: ESP32 S2/S3 USB MSC support? · Issue #8426 · micropython/micropython |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I am trying to develop a project to run esp32 s3 as flashdisk and save my sensor data to this flashdisk. but I had not had the opportunity to get very involved with microtransactions before. and now I can't edit the sample code the way I want. I am able to run the code and create a .txt file with this code. and I can write whatever I want in this .txt file. but I cannot assign a variable to this .txt file.
In addition, I was able to create a maximum memory of 160 kb by making minor changes. I couldn't find how to increase this space either. when i run another sample code because it could support 2mb memory.
I've included the sample code I ran below.
#if ARDUINO_USB_MODE
#warning This sketch should be used when USB is in OTG mode
void setup(){}
void loop(){}
#else
#include "USB.h"
#include "USBMSC.h"
#if ARDUINO_USB_CDC_ON_BOOT
#define HWSerial Serial0
#define USBSerial Serial
#else
#define HWSerial Serial
USBCDC USBSerial;
#endif
USBMSC MSC;
#define ROOT_DIRECTORY_SECTOR_INDEX 19 // Root dizininin sektör indeksi
#define ROOT_DIRECTORY_ENTRY_SIZE 32 // Root dizinindeki her bir girişin boyutu (byte cinsinden)
#define ROOT_DIRECTORY_ENTRY_INDEX 0 // Root dizinindeki hedef girişin indeksi
#define FILE_DATA_OFFSET 64
#define FAT_U8(v) ((v) & 0xFF)
#define FAT_U16(v) FAT_U8(v), FAT_U8((v) >> 8)
#define FAT_U32(v) FAT_U8(v), FAT_U8((v) >> 8), FAT_U8((v) >> 16), FAT_U8((v) >> 24)
#define FAT_MS2B(s,ms) FAT_U8(((((s) & 0x1) * 1000) + (ms)) / 10)
#define FAT_HMS2B(h,m,s) FAT_U8(((s) >> 1)|(((m) & 0x7) << 5)), FAT_U8((((m) >> 3) & 0x7)|((h) << 3))
#define FAT_YMD2B(y,m,d) FAT_U8(((d) & 0x1F)|(((m) & 0x7) << 5)), FAT_U8((((m) >> 3) & 0x1)|((((y) - 1980) & 0x7F) << 1))
#define FAT_TBL2B(l,h) FAT_U8(l), FAT_U8(((l >> 8) & 0xF) | ((h << 4) & 0xF0)), FAT_U8(h >> 4)
#define README_CONTENTS "Here is your file. Do whatever you want. \r\n\r\n <3<3<3<3"
static const uint32_t DISK_SECTOR_COUNT = 64 * 8; // 8KB is the smallest size that windows allow to mount
static const uint16_t DISK_SECTOR_SIZE = 512; // Should be 512
static const uint16_t DISC_SECTORS_PER_TABLE = 1; //each table sector can fit 170KB (340 sectors)
static uint8_t msc_disk[DISK_SECTOR_COUNT][DISK_SECTOR_SIZE] =
{
//------------- Block0: Boot Sector -------------//
{
// Header (62 bytes)
0xEB, 0x3C, 0x90, //jump_instruction
'M' , 'S' , 'D' , 'O' , 'S' , '5' , '.' , '0' , //oem_name
FAT_U16(DISK_SECTOR_SIZE), //bytes_per_sector
FAT_U8(1), //sectors_per_cluster
FAT_U16(1), //reserved_sectors_count
FAT_U8(1), //file_alloc_tables_num
FAT_U16(16), //max_root_dir_entries
FAT_U16(DISK_SECTOR_COUNT), //fat12_sector_num
0xF8, //media_descriptor
FAT_U16(DISC_SECTORS_PER_TABLE), //sectors_per_alloc_table;//FAT12 and FAT16
FAT_U16(1), //sectors_per_track;//A value of 0 may indicate LBA-only access
FAT_U16(1), //num_heads
FAT_U32(0), //hidden_sectors_count
FAT_U32(0), //total_sectors_32
0x00, //physical_drive_number;0x00 for (first) removable media, 0x80 for (first) fixed disk
0x00, //reserved
0x29, //extended_boot_signature;//should be 0x29
FAT_U32(0x1234), //serial_number: 0x1234 => 1234
'T' , 'i' , 'n' , 'y' , 'U' , 'S' , 'B' , ' ' , 'M' , 'S' , 'C' , //volume_label padded with spaces (0x20)
'F' , 'A' , 'T' , '1' , '2' , ' ' , ' ' , ' ' , //file_system_type padded with spaces (0x20)
},
//------------- Block1: FAT12 Table -------------//
{
FAT_TBL2B(0xFF8, 0xFFF), FAT_TBL2B(0xFFF, 0x000) // first 2 entries must be 0xFF8 0xFFF, third entry is cluster end of readme file
},
//------------- Block2: Root Directory -------------//
{
// first entry is volume label
'E' , 'S' , 'P' , '3' , '2' , 'S' , '2' , ' ' ,
'M' , 'S' , 'C' ,
0x08, //FILE_ATTR_VOLUME_LABEL
0x00,
FAT_MS2B(0,0),
FAT_HMS2B(0,0,0),
FAT_YMD2B(0,0,0),
FAT_YMD2B(0,0,0),
FAT_U16(0),
FAT_HMS2B(13,42,30), //last_modified_hms
FAT_YMD2B(2018,11,5), //last_modified_ymd
FAT_U16(0),
FAT_U32(0),
},
//------------- Block3: Readme Content -------------//
README_CONTENTS
};
static int32_t onWrite(uint32_t lba, uint32_t offset, uint8_t* buffer, uint32_t bufsize){
HWSerial.printf("MSC WRITE: lba: %u, offset: %u, bufsize: %u\n", lba, offset, bufsize);
memcpy(msc_disk[lba] + offset, buffer, bufsize);
return bufsize;
}
static int32_t onRead(uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize){
HWSerial.printf("MSC READ: lba: %u, offset: %u, bufsize: %u\n", lba, offset, bufsize);
memcpy(buffer, msc_disk[lba] + offset, bufsize);
return bufsize;
}
static bool onStartStop(uint8_t power_condition, bool start, bool load_eject) {
String myVariable = "i just wanna dance wwith you";
if (start && load_eject) {
// USB bağlandığında
// myFile.txt dosyasını oluşturma ve içine yazma işlemi
uint8_t fileEntry[32];
memset(fileEntry, 0, sizeof(fileEntry));
}
return true;
}
static void usbEventCallback(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data){
if(event_base == ARDUINO_USB_EVENTS){
arduino_usb_event_data_t * data = (arduino_usb_event_data_t*)event_data;
switch (event_id){
case ARDUINO_USB_STARTED_EVENT:
HWSerial.println("USB PLUGGED");
break;
case ARDUINO_USB_STOPPED_EVENT:
HWSerial.println("USB UNPLUGGED");
break;
case ARDUINO_USB_SUSPEND_EVENT:
HWSerial.printf("USB SUSPENDED: remote_wakeup_en: %u\n", data->suspend.remote_wakeup_en);
break;
case ARDUINO_USB_RESUME_EVENT:
HWSerial.println("USB RESUMED");
break;
}
}
void setup() {
HWSerial.begin(115200);
HWSerial.setDebugOutput(true);
USB.onEvent(usbEventCallback);
MSC.vendorID("ESP32");//max 8 chars
MSC.productID("USB_MSC");//max 16 chars
MSC.productRevision("1.0");//max 4 chars
MSC.onStartStop(onStartStop);
MSC.onRead(onRead);
MSC.onWrite(onWrite);
MSC.mediaPresent(true);
MSC.begin(DISK_SECTOR_COUNT, DISK_SECTOR_SIZE);
USBSerial.begin();
USB.begin();
}
void loop() {
// put your main code here, to run repeatedly:
}
#endif /* ARDUINO_USB_MODE */
Beta Was this translation helpful? Give feedback.
All reactions