From 85f3bd4ed01748435e5d3182229cf23fc0ef2db6 Mon Sep 17 00:00:00 2001 From: Bertrand Songis Date: Fri, 29 Jul 2016 20:53:01 +0200 Subject: [PATCH] [X9E] Massstorage OK? --- radio/src/disk_cache.h | 2 +- .../common/arm/stm32/usbd_storage_msd.cpp | 56 +++++++++---------- radio/src/targets/horus/diskio.cpp | 8 +-- radio/src/targets/horus/sdio_sd.h | 2 + radio/src/targets/sky9x/MEDSdcard.c | 3 +- radio/src/targets/sky9x/board_lowlevel.h | 50 +++++++++-------- radio/src/targets/sky9x/massstorage.cpp | 48 +++++++--------- .../sky9x/usb/device/massstorage/MSDLun.c | 4 +- radio/src/targets/taranis/board.h | 5 +- radio/src/targets/taranis/diskio.cpp | 46 ++++++++++++++- 10 files changed, 130 insertions(+), 94 deletions(-) diff --git a/radio/src/disk_cache.h b/radio/src/disk_cache.h index ac2830e52..8fb28aa14 100644 --- a/radio/src/disk_cache.h +++ b/radio/src/disk_cache.h @@ -22,9 +22,9 @@ #define _DISK_CACHE_H_ #include "diskio.h" +#include "sdio_sd.h" // tunable parameters -#define BLOCK_SIZE 512 // sector Size in Bytes #define DISK_CACHE_BLOCKS_NUM 8 // no cache blocks #define DISK_CACHE_BLOCK_SECTORS 16 // no sectors diff --git a/radio/src/targets/common/arm/stm32/usbd_storage_msd.cpp b/radio/src/targets/common/arm/stm32/usbd_storage_msd.cpp index 1a1d8113c..6f4c71fea 100644 --- a/radio/src/targets/common/arm/stm32/usbd_storage_msd.cpp +++ b/radio/src/targets/common/arm/stm32/usbd_storage_msd.cpp @@ -37,8 +37,6 @@ enum MassstorageLuns { STORAGE_LUN_NBR }; -#define BLOCKSIZE 512 - /* USB Mass storage Standard Inquiry Data */ const unsigned char STORAGE_Inquirydata[] = {//36 @@ -147,8 +145,8 @@ int8_t STORAGE_GetCapacity (uint8_t lun, uint32_t *block_num, uint32_t *block_si { #if defined(EEPROM) && defined(BOOT) if (lun == STORAGE_EEPROM_LUN) { - *block_size = BLOCKSIZE; - *block_num = 3 + EESIZE/BLOCKSIZE + FLASHSIZE/BLOCKSIZE; + *block_size = BLOCK_SIZE; + *block_num = 3 + EESIZE/BLOCK_SIZE + FLASHSIZE/BLOCK_SIZE; return 0; } #endif @@ -156,7 +154,7 @@ int8_t STORAGE_GetCapacity (uint8_t lun, uint32_t *block_num, uint32_t *block_si if (!SD_CARD_PRESENT()) return -1; - *block_size = BLOCKSIZE; + *block_size = BLOCK_SIZE; static DWORD sector_count = 0; if (sector_count == 0) { @@ -273,7 +271,7 @@ int8_t STORAGE_GetMaxLun (void) /** * FAT12 boot sector partition. */ -const char g_FATboot[BLOCKSIZE] = +const char g_FATboot[BLOCK_SIZE] = { 0xeb, 0x3c, 0x90, // Jump instruction. 0x39, 0x58, 0x20, 0x54, 0x45, 0x41, 0x4D, 0x00, // OEM Name @@ -283,7 +281,7 @@ const char g_FATboot[BLOCKSIZE] = 0x01, // Number of FATs 0x10, 0x00, // Number of root directory entries - 3+(EESIZE/BLOCKSIZE), (FLASHSIZE/BLOCKSIZE)>>8, // Total sectors + 3+(EESIZE/BLOCK_SIZE), (FLASHSIZE/BLOCK_SIZE)>>8, // Total sectors 0xf8, // Media descriptor 0x01, 0x00, // Sectors per FAT table 0x20, 0x00, // Sectors per track @@ -332,7 +330,7 @@ const char g_FATboot[BLOCKSIZE] = }; #if defined(REV4a) -const char g_FAT[BLOCKSIZE] = +const char g_FAT[BLOCK_SIZE] = { 0xF8, 0xFF, 0xFF, 0x03, 0x40, 0x00, 0x05, 0x60, 0x00, 0x07, 0x80, 0x00, 0x09, 0xA0, 0x00, 0x0B, 0xC0, 0x00, 0x0D, 0xE0, 0x00, 0x0F, 0x00, 0x01, 0x11, 0xF0, 0xFF, 0x13, 0x40, 0x01, 0x15, 0x60, @@ -368,7 +366,7 @@ const char g_FAT[BLOCKSIZE] = 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }; #else -const char g_FAT[BLOCKSIZE] = +const char g_FAT[BLOCK_SIZE] = { 0xF8, 0xFF, 0xFF, 0x03, 0x40, 0x00, 0x05, 0x60, 0x00, 0x07, 0x80, 0x00, 0x09, 0xF0, 0xFF, 0x0B, 0xC0, 0x00, 0x0D, 0xE0, 0x00, 0x0F, 0x00, 0x01, 0x11, 0x20, 0x01, 0x13, 0x40, 0x01, 0x15, 0x60, @@ -477,7 +475,7 @@ const FATDirEntry_t g_DIRroot[16] = 0x0000, 0xA302, 0x3D55, - 0x0002 + (EESIZE/BLOCKSIZE)/8, + 0x0002 + (EESIZE/BLOCK_SIZE)/8, FLASHSIZE }, { @@ -682,26 +680,26 @@ int32_t fat12Read(uint8_t * buffer, uint16_t sector, uint16_t count) { while(count) { if (sector == 0) { - memcpy(buffer, g_FATboot, BLOCKSIZE ) ; + memcpy(buffer, g_FATboot, BLOCK_SIZE ) ; } else if (sector == 1/*Reserved sector count*/) { // FAT table. - memcpy(buffer, g_FAT, BLOCKSIZE); + memcpy(buffer, g_FAT, BLOCK_SIZE); } else if (sector == 2) { - memcpy(buffer, g_DIRroot, BLOCKSIZE ) ; + memcpy(buffer, g_DIRroot, BLOCK_SIZE ) ; } - else if (sector < 3 + (EESIZE/BLOCKSIZE)) { - eepromReadBlock(buffer, (sector-3)*BLOCKSIZE, BLOCKSIZE); + else if (sector < 3 + (EESIZE/BLOCK_SIZE)) { + eepromReadBlock(buffer, (sector-3)*BLOCK_SIZE, BLOCK_SIZE); } - else if (sector < 3 + (EESIZE/BLOCKSIZE) + (FLASHSIZE/BLOCKSIZE)) { + else if (sector < 3 + (EESIZE/BLOCK_SIZE) + (FLASHSIZE/BLOCK_SIZE)) { uint32_t address; - address = sector - 3 - (EESIZE/BLOCKSIZE); - address *= BLOCKSIZE; + address = sector - 3 - (EESIZE/BLOCK_SIZE); + address *= BLOCK_SIZE; address += FIRMWARE_ADDRESS; - memcpy(buffer, (uint8_t *)address, BLOCKSIZE); + memcpy(buffer, (uint8_t *)address, BLOCK_SIZE); } - buffer += BLOCKSIZE ; + buffer += BLOCK_SIZE ; sector++ ; count-- ; } @@ -723,7 +721,7 @@ int32_t fat12Write(const uint8_t * buffer, uint16_t sector, uint16_t count) if (sector < 3) { // reserved, read-only } - else if (sector < 3 + (EESIZE/BLOCKSIZE)) { + else if (sector < 3 + (EESIZE/BLOCK_SIZE)) { // eeprom while (count) { if (operation == FATWRITE_NONE && isEepromStart(buffer)) { @@ -731,25 +729,25 @@ int32_t fat12Write(const uint8_t * buffer, uint16_t sector, uint16_t count) operation = FATWRITE_EEPROM; } if (operation == FATWRITE_EEPROM) { - eepromWriteBlock((uint8_t *)buffer, (sector-3)*BLOCKSIZE, BLOCKSIZE); + eepromWriteBlock((uint8_t *)buffer, (sector-3)*BLOCK_SIZE, BLOCK_SIZE); } - buffer += BLOCKSIZE; + buffer += BLOCK_SIZE; sector++; count--; - if (sector-3 >= (EESIZE/BLOCKSIZE)) { + if (sector-3 >= (EESIZE/BLOCK_SIZE)) { TRACE("EEPROM end written at sector %d", sector-1); operation = FATWRITE_NONE; } } } - else if (sector < 3 + (EESIZE/BLOCKSIZE) + (FLASHSIZE/BLOCKSIZE)) { + else if (sector < 3 + (EESIZE/BLOCK_SIZE) + (FLASHSIZE/BLOCK_SIZE)) { // firmware uint32_t address; - address = sector - 3 - (EESIZE/BLOCKSIZE); - address *= BLOCKSIZE; + address = sector - 3 - (EESIZE/BLOCK_SIZE); + address *= BLOCK_SIZE; address += FIRMWARE_ADDRESS; while (count) { - for (uint32_t i=0; i= FIRMWARE_ADDRESS+BOOTLOADER_SIZE/*protect bootloader*/ && address <= FIRMWARE_ADDRESS+FLASHSIZE-FLASH_PAGESIZE) { if (address == FIRMWARE_ADDRESS+BOOTLOADER_SIZE && isFirmwareStart(buffer)) { TRACE("FIRMWARE start found in sector %d", sector); @@ -764,7 +762,7 @@ int32_t fat12Write(const uint8_t * buffer, uint16_t sector, uint16_t count) } sector++; count--; - if (sector-3-(EESIZE/BLOCKSIZE) >= (FLASHSIZE/BLOCKSIZE)) { + if (sector-3-(EESIZE/BLOCK_SIZE) >= (FLASHSIZE/BLOCK_SIZE)) { TRACE("FIRMWARE end written at sector %d", sector-1); operation = FATWRITE_NONE; } diff --git a/radio/src/targets/horus/diskio.cpp b/radio/src/targets/horus/diskio.cpp index 018045962..dfd4bf198 100644 --- a/radio/src/targets/horus/diskio.cpp +++ b/radio/src/targets/horus/diskio.cpp @@ -29,8 +29,6 @@ #include "opentx.h" #include "sdio_sd.h" -#define BLOCK_SIZE 512 /* Block Size in Bytes */ - /*-----------------------------------------------------------------------*/ /* Lock / unlock functions */ /*-----------------------------------------------------------------------*/ @@ -134,9 +132,9 @@ DRESULT __disk_read ( return RES_NOTRDY; } - if ((DWORD)buff < 0x20000000 /*|| (DWORD)buff >= 0x20030000*/ || ((DWORD)buff & 3)) { + if ((DWORD)buff < 0x20000000 || ((DWORD)buff & 3)) { TRACE("disk_read bad alignment (%p)", buff); - while(count--) { + while (count--) { res = disk_read(drv, (BYTE *)scratch, sector++, 1); if (res != RES_OK) { @@ -211,7 +209,7 @@ DRESULT __disk_write ( if (SD_Detect() != SD_PRESENT) return(RES_NOTRDY); - if ((DWORD)buff < 0x20000000 /*|| (DWORD)buff >= 0x20030000*/ || ((DWORD)buff & 3)) { + if ((DWORD)buff < 0x20000000 || ((DWORD)buff & 3)) { TRACE("disk_write bad alignment (%p)", buff); while(count--) { memcpy(scratch, buff, BLOCK_SIZE); diff --git a/radio/src/targets/horus/sdio_sd.h b/radio/src/targets/horus/sdio_sd.h index b075ee14f..122be50fc 100644 --- a/radio/src/targets/horus/sdio_sd.h +++ b/radio/src/targets/horus/sdio_sd.h @@ -24,6 +24,8 @@ #include "board.h" +#define BLOCK_SIZE 512 /* Block Size in Bytes */ + #ifdef __cplusplus extern "C" { #endif diff --git a/radio/src/targets/sky9x/MEDSdcard.c b/radio/src/targets/sky9x/MEDSdcard.c index 113c270bb..77c7a414d 100644 --- a/radio/src/targets/sky9x/MEDSdcard.c +++ b/radio/src/targets/sky9x/MEDSdcard.c @@ -34,7 +34,6 @@ /// Number of SD Slots #define NUM_SD_SLOTS 1 -#define SD_BLOCK_SIZE 512 //------------------------------------------------------------------------------ /// Checks if the device is write protected. @@ -224,7 +223,7 @@ unsigned char MEDSdcard_Initialize(Media *media, unsigned char mciID) media->handler = 0; media->flush = 0; - media->blockSize = SD_BLOCK_SIZE; + media->blockSize = BLOCK_SIZE; media->baseAddress = 0; media->size = SD_GET_BLOCKNR(); diff --git a/radio/src/targets/sky9x/board_lowlevel.h b/radio/src/targets/sky9x/board_lowlevel.h index 3f7f5be8d..4930fba43 100644 --- a/radio/src/targets/sky9x/board_lowlevel.h +++ b/radio/src/targets/sky9x/board_lowlevel.h @@ -1,27 +1,27 @@ -/* - * Copyright (C) OpenTX - * - * Based on code named - * th9x - http://code.google.com/p/th9x - * er9x - http://code.google.com/p/er9x - * gruvin9x - http://code.google.com/p/gruvin9x - * - * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2 as - * published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - */ - +/* + * Copyright (C) OpenTX + * + * Based on code named + * th9x - http://code.google.com/p/th9x + * er9x - http://code.google.com/p/er9x + * gruvin9x - http://code.google.com/p/gruvin9x + * + * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + // TODO merge this file with board_sky9x.h -#ifndef _BOARD_H_ -#define _BOARD_H_ +#ifndef _BOARD_H_ +#define _BOARD_H_ #include @@ -484,7 +484,9 @@ ///// MCI Card Detect pin. #define BOARD_SD_PIN_CD PIN_MCI_CD -#define SD_CARD_PRESENT() ( (PIOB->PIO_PDSR & PIO_PB7) == 0 ) +#define BLOCK_SIZE 512 +#define MSD_BUFFER_SIZE (12*BLOCK_SIZE) // Size of the MSD IO buffer in bytes (6K, more the better) +#define SD_CARD_PRESENT() ( (PIOB->PIO_PDSR & PIO_PB7) == 0 ) // States for initialising card #define SD_ST_ERR -1 @@ -634,4 +636,4 @@ struct t_serial_tx volatile uint16_t ready ; }; -#endif // _BOARD_H_ +#endif // _BOARD_H_ diff --git a/radio/src/targets/sky9x/massstorage.cpp b/radio/src/targets/sky9x/massstorage.cpp index 756144506..8cedee5d3 100644 --- a/radio/src/targets/sky9x/massstorage.cpp +++ b/radio/src/targets/sky9x/massstorage.cpp @@ -1,24 +1,24 @@ -/* - * Copyright (C) OpenTX - * - * Based on code named - * th9x - http://code.google.com/p/th9x - * er9x - http://code.google.com/p/er9x - * gruvin9x - http://code.google.com/p/gruvin9x - * - * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2 as - * published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - */ - -#include "../opentx.h" +/* + * Copyright (C) OpenTX + * + * Based on code named + * th9x - http://code.google.com/p/th9x + * er9x - http://code.google.com/p/er9x + * gruvin9x - http://code.google.com/p/gruvin9x + * + * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#include "opentx.h" #ifndef SIMU @@ -27,12 +27,6 @@ extern "C" { extern unsigned char MEDSdcard_Initialize(Media *media, unsigned char mciID); } -/** Size of one block in bytes. */ -#define BLOCK_SIZE 512 - -/** Size of the MSD IO buffer in bytes (6K, more the better). */ -#define MSD_BUFFER_SIZE (12*BLOCK_SIZE) - /** LUN read/write buffer. */ unsigned char msdBuffer[MSD_BUFFER_SIZE]; diff --git a/radio/src/targets/sky9x/usb/device/massstorage/MSDLun.c b/radio/src/targets/sky9x/usb/device/massstorage/MSDLun.c index 9e40abc9c..3e8b3ba9e 100644 --- a/radio/src/targets/sky9x/usb/device/massstorage/MSDLun.c +++ b/radio/src/targets/sky9x/usb/device/massstorage/MSDLun.c @@ -33,7 +33,7 @@ #include "../../../usb/device/massstorage/MSDLun.h" - +#include "board_lowlevel.h" #include "../../../usb/device/core/USBD.h" #include "debug.h" @@ -42,7 +42,7 @@ //------------------------------------------------------------------------------ /// Default LUN block size in bytes -#define DEFAULT_LUN_BLOCK_SIZE 512 +#define DEFAULT_LUN_BLOCK_SIZE BLOCK_SIZE //------------------------------------------------------------------------------ // Internal variables diff --git a/radio/src/targets/taranis/board.h b/radio/src/targets/taranis/board.h index 558d3857d..9f89427b8 100644 --- a/radio/src/targets/taranis/board.h +++ b/radio/src/targets/taranis/board.h @@ -157,6 +157,7 @@ void delay_ms(uint32_t ms); void getCPUUniqueID(char * s); // SD driver +#define BLOCK_SIZE 512 /* Block Size in Bytes */ #if !defined(SIMU) || defined(SIMU_DISKIO) uint32_t sdIsHC(void); uint32_t sdGetSpeed(void); @@ -164,8 +165,8 @@ void getCPUUniqueID(char * s); #define SD_GET_SPEED() (sdGetSpeed()) #define SD_GET_FREE_BLOCKNR() (sdGetFreeSectors()) #else - #define SD_IS_HC() (0) - #define SD_GET_SPEED() (0) + #define SD_IS_HC() (0) + #define SD_GET_SPEED() (0) #endif #if defined(SIMU) diff --git a/radio/src/targets/taranis/diskio.cpp b/radio/src/targets/taranis/diskio.cpp index aa7b63c8c..004f2d12e 100644 --- a/radio/src/targets/taranis/diskio.cpp +++ b/radio/src/targets/taranis/diskio.cpp @@ -628,12 +628,35 @@ DSTATUS disk_status ( } +#if defined(STM32F4) && !defined(BOOT) +DWORD scratch[BLOCK_SIZE / 4] __DMA; +#endif + /*-----------------------------------------------------------------------*/ /* Read Sector(s) */ /*-----------------------------------------------------------------------*/ -int8_t SD_ReadSectors(uint8_t *buff, uint32_t sector, uint32_t count) +int8_t SD_ReadSectors(uint8_t * buff, uint32_t sector, uint32_t count) { +#if defined(STM32F4) && !defined(BOOT) + if ((DWORD)buff < 0x20000000 || ((DWORD)buff & 3)) { + TRACE("disk_read bad alignment (%p)", buff); + while (count--) { + int8_t res = SD_ReadSectors((BYTE *)scratch, sector++, 1); + + if (res != 0) { + return res; + } + + memcpy(buff, scratch, BLOCK_SIZE); + + buff += BLOCK_SIZE; + } + + return 0; + } +#endif + if (!(CardType & CT_BLOCK)) sector *= 512; /* Convert to byte address if needed */ if (count == 1) { /* Single block read */ @@ -686,8 +709,27 @@ DRESULT disk_read ( /* Write Sector(s) */ /*-----------------------------------------------------------------------*/ -int8_t SD_WriteSectors(const uint8_t *buff, uint32_t sector, uint32_t count) +int8_t SD_WriteSectors(const uint8_t * buff, uint32_t sector, uint32_t count) { +#if defined(STM32F4) && !defined(BOOT) + if ((DWORD)buff < 0x20000000 || ((DWORD)buff & 3)) { + TRACE("disk_write bad alignment (%p)", buff); + while (count--) { + memcpy(scratch, buff, BLOCK_SIZE); + + int8_t res = SD_WriteSectors((const uint8_t *)scratch, sector++, 1); + + if (res != 0) { + return res; + } + + buff += BLOCK_SIZE; + } + + return 0; + } +#endif + if (!(CardType & CT_BLOCK)) sector *= 512; /* Convert to byte address if needed */ if (count == 1) { /* Single block write */