bool emfat_init(emfat_t *emfat, const char *label, emfat_entry_t *entries); void emfat_read(emfat_t *emfat, uint8_t *data, uint32_t sector, int num_sectors); void emfat_write(emfat_t *emfat, const uint8_t *data, uint32_t sector, int num_sectors);
static emfat_entry_t entries[] = { // name dir lvl offset size max_size user read write { "", true, 0, 0, 0, 0, 0, NULL, NULL }, // root { "autorun.inf", false, 1, 0, AUTORUN_SIZE, AUTORUN_SIZE, 0, autorun_read_proc, NULL }, // autorun.inf { "icon.ico", false, 1, 0, ICON_SIZE, ICON_SIZE, 0, icon_read_proc, NULL }, // icon.ico { "drivers", true, 1, 0, 0, 0, 0, NULL, NULL }, // drivers/ { "readme.txt", false, 2, 0, README_SIZE, README_SIZE, 0, readme_read_proc, NULL }, // drivers/readme.txt { NULL } };
void readcb(uint8_t *dest, int size, uint32_t offset, size_t userdata);
#include "usbd_msc_core.h" #include "usbd_usr.h" #include "usbd_desc.h" #include "usb_conf.h" #include "emfat.h" #define AUTORUN_SIZE 50 #define README_SIZE 21 #define ICON_SIZE 1758 const char *autorun_file = "[autorun]\r\n" "label=emfat test drive\r\n" "ICON=icon.ico\r\n"; const char *readme_file = "This is readme file\r\n"; const char icon_file[ICON_SIZE] = { 0x00,0x00,0x01,0x00,0x01,0x00,0x18, ... }; USB_OTG_CORE_HANDLE USB_OTG_dev; // emfat_t emfat; // callback void autorun_read_proc(uint8_t *dest, int size, uint32_t offset, size_t userdata); void icon_read_proc(uint8_t *dest, int size, uint32_t offset, size_t userdata); void readme_read_proc(uint8_t *dest, int size, uint32_t offset, size_t userdata); // static emfat_entry_t entries[] = { // name dir lvl offset size max_size user read write { "", true, 0, 0, 0, 0, 0, NULL, NULL }, // root { "autorun.inf", false, 1, 0, AUTORUN_SIZE, AUTORUN_SIZE, 0, autorun_read_proc, NULL }, // autorun.inf { "icon.ico", false, 1, 0, ICON_SIZE, ICON_SIZE, 0, icon_read_proc, NULL }, // icon.ico { "drivers", true, 1, 0, 0, 0, 0, NULL, NULL }, // drivers/ { "readme.txt", false, 2, 0, README_SIZE, README_SIZE, 0, readme_read_proc, NULL }, // drivers/readme.txt { NULL } }; // callback "autorun.inf" void autorun_read_proc(uint8_t *dest, int size, uint32_t offset, size_t userdata) { int len = 0; if (offset > AUTORUN_SIZE) return; if (offset + size > AUTORUN_SIZE) len = AUTORUN_SIZE - offset; else len = size; memcpy(dest, &autorun_file[offset], len); } // callback "icon.ico" void icon_read_proc(uint8_t *dest, int size, uint32_t offset, size_t userdata) { int len = 0; if (offset > ICON_SIZE) return; if (offset + size > ICON_SIZE) len = ICON_SIZE - offset; else len = size; memcpy(dest, &icon_file[offset], len); } // callback "readme.txt" void readme_read_proc(uint8_t *dest, int size, uint32_t offset, size_t userdata) { int len = 0; if (offset > README_SIZE) return; if (offset + size > README_SIZE) len = README_SIZE - offset; else len = size; memcpy(dest, &readme_file[offset], len); } // , - // int main(void) { emfat_init(&emfat, "emfat", entries); #ifdef USE_USB_OTG_HS USBD_Init(&USB_OTG_dev, USB_OTG_HS_CORE_ID, &USR_desc, &USBD_MSC_cb, &USR_cb); #else USBD_Init(&USB_OTG_dev, USB_OTG_FS_CORE_ID, &USR_desc, &USBD_MSC_cb, &USR_cb); #endif while (true) { } }
int8_t STORAGE_Read( uint8_t lun, // logical unit number uint8_t *buf, // Pointer to the buffer to save data uint32_t blk_addr, // address of 1st block to be read uint16_t blk_len) // nmber of blocks to be read { emfat_read(&emfat, buf, blk_addr, blk_len); return 0; } int8_t STORAGE_Write(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len) { emfat_write(&emfat, buf, blk_addr, blk_len); return 0; }
Source: https://habr.com/ru/post/247673/
All Articles