1
0
Fork 0
mirror of https://github.com/opentx/opentx.git synced 2025-07-24 16:55:20 +03:00

USB Massstorage code refactoring

This commit is contained in:
Bertrand Songis 2016-07-29 16:43:39 +02:00
parent 51223a5864
commit e5bbf6766b
8 changed files with 76 additions and 311 deletions

View file

@ -46,7 +46,7 @@ elseif(USB STREQUAL MASSSTORAGE)
)
set(FIRMWARE_TARGET_SRC
${FIRMWARE_TARGET_SRC}
usbd_storage_msd.cpp
../common/arm/stm32/usbd_storage_msd.cpp
)
else()
add_definitions(-DUSB_JOYSTICK)

View file

@ -19,9 +19,8 @@
*/
/* Includes ------------------------------------------------------------------*/
#include "../../opentx.h"
#include "../../thirdparty/FatFs/diskio.h"
#include "board.h"
#include "opentx.h"
#include "FatFs/diskio.h"
#if defined(__cplusplus) && !defined(SIMU)
extern "C" {
@ -30,12 +29,14 @@ extern "C" {
#include "usbd_msc_mem.h"
#include "usb_conf.h"
#if defined(BOOT)
#define STORAGE_LUN_NBR 2
#else
/* SD card only when not running bootloader */
#define STORAGE_LUN_NBR 1
enum MassstorageLuns {
STORAGE_SDCARD_LUN,
#if defined(EEPROM) && defined(BOOT)
STORAGE_EEPROM_LUN,
#endif
STORAGE_LUN_NBR
};
#define BLOCKSIZE 512
/* USB Mass storage Standard Inquiry Data */
@ -50,11 +51,11 @@ const unsigned char STORAGE_Inquirydata[] = {//36
0x00,
0x00,
0x00,
'F', 'r', 'S', 'k', 'y', ' ', ' ', ' ', /* Manufacturer : 8 bytes */
'T', 'a', 'r', 'a', 'n', 'i', 's', ' ', /* Product : 16 Bytes */
USB_MANUFACTURER, /* Manufacturer : 8 bytes */
USB_PRODUCT, /* Product : 16 Bytes */
'R', 'a', 'd', 'i', 'o', ' ', ' ', ' ',
'1', '.', '0', '0', /* Version : 4 Bytes */
#if defined(BOOT)
#if defined(EEPROM) && defined(BOOT)
/* LUN 1 */
0x00,
0x80,
@ -64,16 +65,16 @@ const unsigned char STORAGE_Inquirydata[] = {//36
0x00,
0x00,
0x00,
'F', 'r', 'S', 'k', 'y', ' ', ' ', ' ', /* Manufacturer : 8 bytes */
'T', 'a', 'r', 'a', 'n', 'i', 's', ' ', /* Product : 16 Bytes */
USB_MANUFACTURER, /* Manufacturer : 8 bytes */
USB_PRODUCT, /* Product : 16 Bytes */
'R', 'a', 'd', 'i', 'o', ' ', ' ', ' ',
'1', '.', '0' ,'0', /* Version : 4 Bytes */
#endif
};
#if defined(BOOT)
int32_t fat12Write( const uint8_t *buffer, uint16_t sector, uint16_t count ) ;
int32_t fat12Read( uint8_t *buffer, uint16_t sector, uint16_t count ) ;
#if defined(EEPROM) && defined(BOOT)
int32_t fat12Write(const uint8_t * buffer, uint16_t sector, uint16_t count);
int32_t fat12Read(uint8_t * buffer, uint16_t sector, uint16_t count );
#endif
int8_t STORAGE_Init (uint8_t lun);
@ -144,30 +145,29 @@ int8_t STORAGE_Init (uint8_t lun)
*/
int8_t STORAGE_GetCapacity (uint8_t lun, uint32_t *block_num, uint32_t *block_size)
{
#if defined(BOOT)
if (lun == 1) {
#if defined(EEPROM) && defined(BOOT)
if (lun == STORAGE_EEPROM_LUN) {
*block_size = BLOCKSIZE;
*block_num = 3 + EESIZE/BLOCKSIZE + FLASHSIZE/BLOCKSIZE;
return 0;
}
else
#endif
{
if (!SD_CARD_PRESENT())
return -1;
*block_size = BLOCKSIZE;
if (!SD_CARD_PRESENT())
return -1;
*block_size = BLOCKSIZE;
static DWORD sector_count = 0;
if (sector_count == 0) {
if (disk_ioctl(0, GET_SECTOR_COUNT, &sector_count) != RES_OK) {
sector_count = 0;
return -1;
}
static DWORD sector_count = 0;
if (sector_count == 0) {
if (disk_ioctl(0, GET_SECTOR_COUNT, &sector_count) != RES_OK) {
sector_count = 0;
return -1;
}
*block_num = sector_count;
}
*block_num = sector_count;
return 0;
}
@ -175,12 +175,13 @@ uint8_t lunReady[STORAGE_LUN_NBR];
void usbPluggedIn()
{
if (lunReady[0] == 0) {
lunReady[0] = 1;
if (lunReady[STORAGE_SDCARD_LUN] == 0) {
lunReady[STORAGE_SDCARD_LUN] = 1;
}
#if defined(BOOT)
if (lunReady[1] == 0) {
lunReady[1] = 1;
#if defined(EEPROM) && defined(BOOT)
if (lunReady[STORAGE_EEPROM_LUN] == 0) {
lunReady[STORAGE_EEPROM_LUN] = 1;
}
#endif
}
@ -192,21 +193,13 @@ void usbPluggedIn()
*/
int8_t STORAGE_IsReady (uint8_t lun)
{
#if defined(BOOT)
if (lun == 1) {
if (lunReady[1] == 0) {
return -1 ;
}
return 0 ;
#if defined(EEPROM) && defined(BOOT)
if (lun == STORAGE_EEPROM_LUN) {
return (lunReady[STORAGE_EEPROM_LUN] != 0) ? 0 : -1;
}
else
#endif
{
if (lunReady[0] == 0) {
return -1 ;
}
return SD_CARD_PRESENT() ? 0 : -1;
}
return (lunReady[STORAGE_SDCARD_LUN] != 0 && SD_CARD_PRESENT()) ? 0 : -1;
}
/**
@ -228,27 +221,18 @@ int8_t STORAGE_IsWriteProtected (uint8_t lun)
* @retval Status
*/
int8_t SD_ReadSectors(uint8_t *buff, uint32_t sector, uint32_t count);
int8_t STORAGE_Read (uint8_t lun,
uint8_t *buf,
uint32_t blk_addr,
uint16_t blk_len)
{
#if defined(BOOT)
if (lun == 1) {
if (fat12Read(buf, blk_addr, blk_len) != 0)
return -1;
#if defined(EEPROM) && defined(BOOT)
if (lun == STORAGE_EEPROM_LUN) {
return (fat12Read(buf, blk_addr, blk_len) == 0) ? 0 : -1;
}
else
#endif
{
if (SD_ReadSectors(buf, blk_addr, blk_len) != 0) {
return -1;
}
}
return 0;
return (disk_read(0, buf, blk_addr, blk_len) == RES_OK) ? 0 : -1;
}
/**
* @brief Write data to the medium
@ -259,26 +243,18 @@ int8_t STORAGE_Read (uint8_t lun,
* @retval Status
*/
int8_t SD_WriteSectors(const uint8_t *buf, uint32_t sector, uint32_t count);
int8_t STORAGE_Write (uint8_t lun,
uint8_t *buf,
uint32_t blk_addr,
uint16_t blk_len)
{
#if defined(BOOT)
if (lun == 1) {
if (fat12Write(buf, blk_addr, blk_len) != 0)
return -1;
#if defined(EEPROM) && defined(BOOT)
if (lun == STORAGE_EEPROM_LUN) {
return (fat12Write(buf, blk_addr, blk_len) == 0) ? 0 : -1;
}
else
#endif
{
if (SD_WriteSectors(buf, blk_addr, blk_len) != 0)
return -1;
}
return (0);
return (disk_write(0, buf, blk_addr, blk_len) == RES_OK) ? 0 : -1;
}
/**
@ -292,7 +268,7 @@ int8_t STORAGE_GetMaxLun (void)
return STORAGE_LUN_NBR - 1;
}
#if defined(BOOT)
#if defined(EEPROM) && defined(BOOT)
//------------------------------------------------------------------------------
/**
* FAT12 boot sector partition.
@ -460,7 +436,7 @@ typedef struct
const FATDirEntry_t g_DIRroot[16] =
{
{
{ 'T', 'A', 'R', 'A', 'N', 'I', 'S', ' '},
{ USB_PRODUCT },
{ ' ', ' ', ' '},
0x08, // Volume
0x00,
@ -702,10 +678,9 @@ const FATDirEntry_t g_DIRroot[16] =
};
// count is number of 512 byte sectors
int32_t fat12Read( uint8_t *buffer, uint16_t sector, uint16_t count )
int32_t fat12Read(uint8_t * buffer, uint16_t sector, uint16_t count)
{
while ( count )
{
while(count) {
if (sector == 0) {
memcpy(buffer, g_FATboot, BLOCKSIZE ) ;
}
@ -733,7 +708,7 @@ int32_t fat12Read( uint8_t *buffer, uint16_t sector, uint16_t count )
return 0 ;
}
int32_t fat12Write(const uint8_t *buffer, uint16_t sector, uint16_t count)
int32_t fat12Write(const uint8_t * buffer, uint16_t sector, uint16_t count)
{
enum FatWriteOperation {
FATWRITE_NONE,

View file

@ -9,7 +9,6 @@ set(LINKER_SCRIPT targets/horus/stm32f4_flash.ld)
set(FIRMWARE_DEPENDENCIES ${FIRMWARE_DEPENDENCIES} horus_bitmaps)
set(LUA_EXPORT lua_export_horus)
set(FLAVOUR horus)
add_definitions(-DUSB_NAME="FrSky Horus")
set(VIRTUAL_INPUTS YES)
set(RAMBACKUP YES)
option(DISK_CACHE "Enable SD card disk cache" YES)

View file

@ -46,13 +46,13 @@ extern "C" {
#include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/misc.h"
#if !defined(SIMU)
#include "usbd_cdc_core.h"
#include "usbd_msc_core.h"
#include "usbd_hid_core.h"
#include "usbd_usr.h"
#include "usbd_desc.h"
#include "usb_conf.h"
#include "usbd_conf.h"
#include "usbd_cdc_core.h"
#include "usbd_msc_core.h"
#include "usbd_hid_core.h"
#include "usbd_usr.h"
#include "usbd_desc.h"
#include "usb_conf.h"
#include "usbd_conf.h"
#endif
#include "hal.h"
@ -255,6 +255,9 @@ int usbPlugged(void);
void usbInit(void);
void usbDeInit(void);
void usbSerialPutc(uint8_t c);
#define USB_NAME "FrSky Horus"
#define USB_MANUFACTURER 'F', 'r', 'S', 'k', 'y', ' ', ' ', ' ' /* 8 bytes */
#define USB_PRODUCT 'H', 'o', 'r', 'u', 's', ' ', ' ', ' ' /* 8 Bytes */
#if defined(__cplusplus) && !defined(SIMU)
}

View file

@ -1,212 +0,0 @@
/*
* 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.
*/
/* Includes ------------------------------------------------------------------*/
#include "../../opentx.h"
#include "../../thirdparty/FatFs/diskio.h"
#include "board.h"
#include "sdio_sd.h"
#if defined(__cplusplus) && !defined(SIMU)
extern "C" {
#endif
#include "usbd_msc_mem.h"
#include "usb_conf.h"
/* SD card only when not running bootloader */
#define STORAGE_LUN_NBR 1
#define BLOCKSIZE 512
/* USB Mass storage Standard Inquiry Data */
const unsigned char STORAGE_Inquirydata[] = {//36
/* LUN 0 */
0x00,
0x80,
0x02,
0x02,
(USBD_STD_INQUIRY_LENGTH - 5),
0x00,
0x00,
0x00,
'F', 'r', 'S', 'k', 'y', ' ', ' ', ' ', /* Manufacturer : 8 bytes */
'H', 'o', 'r', 'u', 's', ' ', ' ', ' ', /* Product : 16 Bytes */
'R', 'a', 'd', 'i', 'o', ' ', ' ', ' ',
'1', '.', '0', '0', /* Version : 4 Bytes */
};
int8_t STORAGE_Init (uint8_t lun);
int8_t STORAGE_GetCapacity (uint8_t lun,
uint32_t *block_num,
uint32_t *block_size);
int8_t STORAGE_IsReady (uint8_t lun);
int8_t STORAGE_IsWriteProtected (uint8_t lun);
int8_t STORAGE_Read (uint8_t lun,
uint8_t *buf,
uint32_t blk_addr,
uint16_t blk_len);
int8_t STORAGE_Write (uint8_t lun,
uint8_t *buf,
uint32_t blk_addr,
uint16_t blk_len);
int8_t STORAGE_GetMaxLun (void);
USBD_STORAGE_cb_TypeDef USBD_MICRO_SDIO_fops =
{
STORAGE_Init,
STORAGE_GetCapacity,
STORAGE_IsReady,
STORAGE_IsWriteProtected,
STORAGE_Read,
STORAGE_Write,
STORAGE_GetMaxLun,
(int8_t *)STORAGE_Inquirydata,
};
USBD_STORAGE_cb_TypeDef *USBD_STORAGE_fops = &USBD_MICRO_SDIO_fops;
__IO uint32_t count = 0;
#if defined(__cplusplus) && !defined(SIMU)
}
#endif
int8_t STORAGE_Init (uint8_t lun)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = SDIO_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* TODO if no SD ... if( SD_Init() != 0)
{
return (-1);
}
*/
return (0);
}
/**
* @brief return medium capacity and block size
* @param lun : logical unit number
* @param block_num : number of physical block
* @param block_size : size of a physical block
* @retval Status
*/
int8_t STORAGE_GetCapacity (uint8_t lun, uint32_t *block_num, uint32_t *block_size)
{
if (!SD_CARD_PRESENT())
return -1;
*block_size = SDCardInfo.CardBlockSize;
*block_num = SDCardInfo.CardCapacity / SDCardInfo.CardBlockSize;
return 0;
}
uint8_t lunReady[STORAGE_LUN_NBR] ;
void usbPluggedIn()
{
if (lunReady[0] == 0) {
lunReady[0] = 1;
}
}
/**
* @brief check whether the medium is ready
* @param lun : logical unit number
* @retval Status
*/
int8_t STORAGE_IsReady (uint8_t lun)
{
if (lunReady[0] == 0)
return -1 ;
return SD_CARD_PRESENT() ? 0 : -1;
}
/**
* @brief check whether the medium is write-protected
* @param lun : logical unit number
* @retval Status
*/
int8_t STORAGE_IsWriteProtected (uint8_t lun)
{
return 0;
}
/**
* @brief Read data from the medium
* @param lun : logical unit number
* @param buf : Pointer to the buffer to save data
* @param blk_addr : address of 1st block to be read
* @param blk_len : nmber of blocks to be read
* @retval Status
*/
int8_t STORAGE_Read (uint8_t lun,
uint8_t *buf,
uint32_t blk_addr,
uint16_t blk_len)
{
if (disk_read(0, buf, blk_addr, blk_len) == RES_OK)
return 0;
else
return -1;
}
/**
* @brief Write data to the medium
* @param lun : logical unit number
* @param buf : Pointer to the buffer to write from
* @param blk_addr : address of 1st block to be written
* @param blk_len : nmber of blocks to be read
* @retval Status
*/
int8_t STORAGE_Write (uint8_t lun,
uint8_t *buf,
uint32_t blk_addr,
uint16_t blk_len)
{
if (disk_write(0, buf, blk_addr, blk_len) == RES_OK)
return 0;
else
return -1;
}
/**
* @brief Return number of supported logical unit
* @param None
* @retval number of logical unit
*/
int8_t STORAGE_GetMaxLun (void)
{
return STORAGE_LUN_NBR - 1;
}

View file

@ -317,6 +317,9 @@ int usbPlugged(void);
void usbInit(void);
void usbDeInit(void);
void usbSerialPutc(uint8_t c);
#define USB_NAME "FrSky Taranis"
#define USB_MANUFACTURER 'F', 'r', 'S', 'k', 'y', ' ', ' ', ' ' /* 8 bytes */
#define USB_PRODUCT 'T', 'a', 'r', 'a', 'n', 'i', 's', ' ' /* 8 Bytes */
#if defined(__cplusplus) && !defined(SIMU)
}

View file

@ -51,7 +51,7 @@ set(BOOTLOADER_SRC
../flash_driver.cpp
../diskio.cpp
../../common/arm/stm32/usbd_usr.cpp
../usbd_storage_msd.cpp
../../common/arm/stm32/usbd_storage_msd.cpp
../delays.c
../../common/arm/stm32/usbd_desc.c
../aspi.c

View file

@ -18,9 +18,9 @@
* GNU General Public License for more details.
*/
#include "../../opentx.h"
#include "../../thirdparty/FatFs/diskio.h"
#include "../../thirdparty/FatFs/ff.h"
#include "opentx.h"
#include "FatFs/diskio.h"
#include "FatFs/ff.h"
/* Definitions for MMC/SDC command */
#define CMD0 (0x40+0) /* GO_IDLE_STATE */
@ -720,8 +720,6 @@ int8_t SD_WriteSectors(const uint8_t *buff, uint32_t sector, uint32_t count)
return count ? -1 : 0;
}
#if _FS_READONLY == 0
DRESULT disk_write (
BYTE drv, /* Physical drive number (0) */
const BYTE *buff, /* Pointer to the data to be written */
@ -736,8 +734,6 @@ DRESULT disk_write (
TRACE_SD_CARD_EVENT((res != 0), sd_disk_write, (count << 24) + (sector & 0x00FFFFFF));
return (res != 0) ? RES_ERROR : RES_OK;
}
#endif /* _READONLY == 0 */
/*-----------------------------------------------------------------------*/
@ -798,7 +794,8 @@ DRESULT disk_ioctl (
if ((csd[0] >> 6) == 1) { /* SDC version 2.00 */
csize = csd[9] + ((WORD)csd[8] << 8) + 1;
*(DWORD*)buff = (DWORD)csize << 10;
} else { /* SDC version 1.XX or MMC*/
}
else { /* SDC version 1.XX or MMC*/
n = (csd[5] & 15) + ((csd[10] & 128) >> 7) + ((csd[9] & 3) << 1) + 2;
csize = (csd[8] >> 6) + ((WORD)csd[7] << 2) + ((WORD)(csd[6] & 3) << 10) + 1;
*(DWORD*)buff = (DWORD)csize << (n - 9);