From d1b910d58258978bbeb7c12a629515f0c4f139b2 Mon Sep 17 00:00:00 2001 From: Daniel Fekete Date: Sun, 28 May 2017 06:32:57 +0200 Subject: [PATCH] USB: mass storage: move fat12 format into independent function --- STM32/cores/arduino/stm32/BlockDevice.h | 3 ++ .../USBMassStorage/src/MassStorage.cpp | 48 ++++++++++++++++++- .../USBMassStorage/src/MassStorage.h | 4 ++ .../USBMassStorage/src/RamBlockDevice.cpp | 47 ++---------------- .../USBMassStorage/src/RamBlockDevice.h | 3 -- 5 files changed, 56 insertions(+), 49 deletions(-) diff --git a/STM32/cores/arduino/stm32/BlockDevice.h b/STM32/cores/arduino/stm32/BlockDevice.h index a8e73b8..108e418 100644 --- a/STM32/cores/arduino/stm32/BlockDevice.h +++ b/STM32/cores/arduino/stm32/BlockDevice.h @@ -23,6 +23,9 @@ #ifndef BLOCKDEVICE_H #define BLOCKDEVICE_H +#include "stdint.h" +#include "stddef.h" + class BlockDevice { public: virtual bool begin() = 0; diff --git a/STM32/libraries/USBMassStorage/src/MassStorage.cpp b/STM32/libraries/USBMassStorage/src/MassStorage.cpp index 64467fd..7960f9d 100644 --- a/STM32/libraries/USBMassStorage/src/MassStorage.cpp +++ b/STM32/libraries/USBMassStorage/src/MassStorage.cpp @@ -22,8 +22,6 @@ #include "USBDevice.h" -#include "BlockDevice.h" - #include "msc/usbd_msc.h" #include "MassStorage.h" @@ -89,3 +87,49 @@ USBD_StorageTypeDef USBD_DISK_fops = { (int8_t *)MSC_Inquirydata }; + + + +const unsigned char fat12BootSector[] = { + 0xEB, 0x3C, 0x90, //jump to boot program + 'S', 'T', 'M', '3', '2', 'F', 'A', 'T', //manufacturer description + 0x00, 0x02, // 512 bytes per block + 0x01, // blocks per allocation unit + 0x01, 0x00, // 1 reserved block for boot block + 0x01, // number of FAT + 0x10, 0x00, // max number of root dir entries + 0x42, 0x00, // number of blocks in the entire disk + 0xF8, // media descriptor + + 0x01, 0x00, // blocks for FAT + 0x00, 0x00, // for boot program + 0x00, 0x00, // for boot program + 0x00, 0x00, 0x00, 0x00, // hidden blocks + 0x00, 0x00, 0x00, 0x00, // total number of blocks + 0x00, 0x00, // for boot program + 0x00, //for boot program + 0x38, 0xD3, 0xC7, 0x53, // Disk ID + 'S', 'T', 'M', '3', '2', ' ', 'D', 'r', 'i', 'v', 'e', // partition volume label + 'F', 'A', 'T', '1', '2', ' ', ' ', ' ', // file system type +}; + +void formatFat12(uint8_t *buffer, uint32_t blockCount) { + memcpy(buffer, fat12BootSector, sizeof(fat12BootSector)); + + buffer[19] = blockCount & 0xFF; + buffer[20] = blockCount >> 8; + + buffer[510] = 0x55; + buffer[511] = 0xAA; + + + buffer[1024 + 0] = 'S'; + buffer[1024 + 1] = 'T'; + buffer[1024 + 2] = 'M'; + buffer[1024 + 3] = '3'; + buffer[1024 + 4] = '2'; + for(int i = 5; i < 11; i++) { + buffer[1024 + i] = ' '; + } + buffer[1024 + 11] = 0x8; // Volume label dir entry +} diff --git a/STM32/libraries/USBMassStorage/src/MassStorage.h b/STM32/libraries/USBMassStorage/src/MassStorage.h index d573266..a5cfbc6 100644 --- a/STM32/libraries/USBMassStorage/src/MassStorage.h +++ b/STM32/libraries/USBMassStorage/src/MassStorage.h @@ -1,6 +1,10 @@ #ifndef MASSSTORAGE_H #define MASSSTORAGE_H +#include "BlockDevice.h" + extern BlockDevice *getMassStorage(); +void formatFat12(uint8_t *buffer, uint32_t blockCount); + #endif diff --git a/STM32/libraries/USBMassStorage/src/RamBlockDevice.cpp b/STM32/libraries/USBMassStorage/src/RamBlockDevice.cpp index c01b60b..b75cbfa 100644 --- a/STM32/libraries/USBMassStorage/src/RamBlockDevice.cpp +++ b/STM32/libraries/USBMassStorage/src/RamBlockDevice.cpp @@ -20,6 +20,8 @@ SOFTWARE. */ +#include "MassStorage.h" + #include "RamBlockDevice.h" RamBlockDevice::RamBlockDevice() { @@ -32,7 +34,7 @@ RamBlockDevice::RamBlockDevice(uint32_t blockCount, bool doFormat) { this->buffer = (uint8_t *)calloc(blockCount * 512, 1); if (doFormat) { - this->formatFat12(); + formatFat12(buffer, blockCount); } } @@ -68,49 +70,6 @@ bool RamBlockDevice::writeBlocks(uint32_t block, uint8_t* src, size_t blocks) { return true; }; -const unsigned char fat12BootSector[] = { - 0xEB, 0x3C, 0x90, //jump to boot program - 'S', 'T', 'M', '3', '2', 'F', 'A', 'T', //manufacturer description - 0x00, 0x02, // 512 bytes per block - 0x01, // blocks per allocation unit - 0x01, 0x00, // 1 reserved block for boot block - 0x01, // number of FAT - 0x10, 0x00, // max number of root dir entries - 0x42, 0x00, // number of blocks in the entire disk - 0xF8, // media descriptor - - 0x01, 0x00, // blocks for FAT - 0x00, 0x00, // for boot program - 0x00, 0x00, // for boot program - 0x00, 0x00, 0x00, 0x00, // hidden blocks - 0x00, 0x00, 0x00, 0x00, // total number of blocks - 0x00, 0x00, // for boot program - 0x00, //for boot program - 0x38, 0xD3, 0xC7, 0x53, // Disk ID - 'S', 'T', 'M', '3', '2', ' ', 'D', 'r', 'i', 'v', 'e', // partition volume label - 'F', 'A', 'T', '1', '2', ' ', ' ', ' ', // file system type -}; - -void RamBlockDevice::formatFat12() { - memcpy(buffer, fat12BootSector, sizeof(fat12BootSector)); - - buffer[19] = blockCount & 0xFF; - buffer[20] = blockCount >> 8; - - buffer[510] = 0x55; - buffer[511] = 0xAA; - - - buffer[1024 + 0] = 'S'; - buffer[1024 + 1] = 'T'; - buffer[1024 + 2] = 'M'; - buffer[1024 + 3] = '3'; - buffer[1024 + 4] = '2'; - for(int i = 5; i < 11; i++) { - buffer[1024 + i] = ' '; - } - buffer[1024 + 11] = 0x8; // Volume label dir entry -} void RamBlockDevice::setReadListener(listener l) { readListener = l; diff --git a/STM32/libraries/USBMassStorage/src/RamBlockDevice.h b/STM32/libraries/USBMassStorage/src/RamBlockDevice.h index f4afd2f..cc678c4 100644 --- a/STM32/libraries/USBMassStorage/src/RamBlockDevice.h +++ b/STM32/libraries/USBMassStorage/src/RamBlockDevice.h @@ -24,9 +24,6 @@ public: void setReadListener(listener); void setWriteListener(listener); - - void formatFat12(); - uint32_t blockCount; uint8_t *buffer; private: