- update ch203 family to allow to specify flash and ram size. Introduce

- add ch32v203_ro_1v0 board
- CFG_EXAMPLE_MSC_DUAL_READONLY to build msc_dual_lun for ch32v203
This commit is contained in:
hathach
2024-05-20 13:24:24 +07:00
parent 5fee292606
commit 4a5fee503b
10 changed files with 106 additions and 79 deletions

View File

@@ -1,4 +1,3 @@
mcu:CH32V20X
mcu:SAMD11 mcu:SAMD11
mcu:MKL25ZXX mcu:MKL25ZXX
family:espressif family:espressif

View File

@@ -34,9 +34,13 @@
// Some MCU doesn't have enough 8KB SRAM to store the whole disk // Some MCU doesn't have enough 8KB SRAM to store the whole disk
// We will use Flash as read-only disk with board that has // We will use Flash as read-only disk with board that has
// CFG_EXAMPLE_MSC_READONLY defined // CFG_EXAMPLE_MSC_READONLY defined
#if defined(CFG_EXAMPLE_MSC_READONLY) || defined(CFG_EXAMPLE_MSC_DUAL_READONLY)
#define MSC_CONST const
#else
#define MSC_CONST
#endif
enum enum {
{
DISK_BLOCK_NUM = 16, // 8KB is the smallest size that windows allow to mount DISK_BLOCK_NUM = 16, // 8KB is the smallest size that windows allow to mount
DISK_BLOCK_SIZE = 512 DISK_BLOCK_SIZE = 512
}; };
@@ -51,10 +55,7 @@ If you find any bugs or get any questions, feel free to file an\r\n\
issue at github.com/hathach/tinyusb" issue at github.com/hathach/tinyusb"
#ifdef CFG_EXAMPLE_MSC_READONLY MSC_CONST uint8_t msc_disk0[DISK_BLOCK_NUM][DISK_BLOCK_SIZE] =
const
#endif
uint8_t msc_disk0[DISK_BLOCK_NUM][DISK_BLOCK_SIZE] =
{ {
//------------- Block0: Boot Sector -------------// //------------- Block0: Boot Sector -------------//
// byte_per_sector = DISK_BLOCK_SIZE; fat12_sector_num_16 = DISK_BLOCK_NUM; // byte_per_sector = DISK_BLOCK_SIZE; fat12_sector_num_16 = DISK_BLOCK_NUM;
@@ -132,10 +133,7 @@ uint8_t msc_disk0[DISK_BLOCK_NUM][DISK_BLOCK_SIZE] =
If you find any bugs or get any questions, feel free to file an\r\n\ If you find any bugs or get any questions, feel free to file an\r\n\
issue at github.com/hathach/tinyusb" issue at github.com/hathach/tinyusb"
#ifdef CFG_EXAMPLE_MSC_READONLY MSC_CONST uint8_t msc_disk1[DISK_BLOCK_NUM][DISK_BLOCK_SIZE] =
const
#endif
uint8_t msc_disk1[DISK_BLOCK_NUM][DISK_BLOCK_SIZE] =
{ {
//------------- Block0: Boot Sector -------------// //------------- Block0: Boot Sector -------------//
// byte_per_sector = DISK_BLOCK_SIZE; fat12_sector_num_16 = DISK_BLOCK_NUM; // byte_per_sector = DISK_BLOCK_SIZE; fat12_sector_num_16 = DISK_BLOCK_NUM;
@@ -206,15 +204,13 @@ uint8_t msc_disk1[DISK_BLOCK_NUM][DISK_BLOCK_SIZE] =
}; };
// Invoked to determine max LUN // Invoked to determine max LUN
uint8_t tud_msc_get_maxlun_cb(void) uint8_t tud_msc_get_maxlun_cb(void) {
{
return 2; // dual LUN return 2; // dual LUN
} }
// Invoked when received SCSI_CMD_INQUIRY // Invoked when received SCSI_CMD_INQUIRY
// Application fill vendor id, product id and revision with string up to 8, 16, 4 characters respectively // Application fill vendor id, product id and revision with string up to 8, 16, 4 characters respectively
void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8], uint8_t product_id[16], uint8_t product_rev[4]) void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8], uint8_t product_id[16], uint8_t product_rev[4]) {
{
(void) lun; // use same ID for both LUNs (void) lun; // use same ID for both LUNs
const char vid[] = "TinyUSB"; const char vid[] = "TinyUSB";
@@ -228,8 +224,7 @@ void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8], uint8_t product_id[16
// Invoked when received Test Unit Ready command. // Invoked when received Test Unit Ready command.
// return true allowing host to read/write this LUN e.g SD card inserted // return true allowing host to read/write this LUN e.g SD card inserted
bool tud_msc_test_unit_ready_cb(uint8_t lun) bool tud_msc_test_unit_ready_cb(uint8_t lun) {
{
if ( lun == 1 && board_button_read() ) return false; if ( lun == 1 && board_button_read() ) return false;
return true; // RAM disk is always ready return true; // RAM disk is always ready
@@ -237,8 +232,7 @@ bool tud_msc_test_unit_ready_cb(uint8_t lun)
// Invoked when received SCSI_CMD_READ_CAPACITY_10 and SCSI_CMD_READ_FORMAT_CAPACITY to determine the disk size // Invoked when received SCSI_CMD_READ_CAPACITY_10 and SCSI_CMD_READ_FORMAT_CAPACITY to determine the disk size
// Application update block count and block size // Application update block count and block size
void tud_msc_capacity_cb(uint8_t lun, uint32_t* block_count, uint16_t* block_size) void tud_msc_capacity_cb(uint8_t lun, uint32_t* block_count, uint16_t* block_size) {
{
(void) lun; (void) lun;
*block_count = DISK_BLOCK_NUM; *block_count = DISK_BLOCK_NUM;
@@ -248,18 +242,14 @@ void tud_msc_capacity_cb(uint8_t lun, uint32_t* block_count, uint16_t* block_siz
// Invoked when received Start Stop Unit command // Invoked when received Start Stop Unit command
// - Start = 0 : stopped power mode, if load_eject = 1 : unload disk storage // - Start = 0 : stopped power mode, if load_eject = 1 : unload disk storage
// - Start = 1 : active mode, if load_eject = 1 : load disk storage // - Start = 1 : active mode, if load_eject = 1 : load disk storage
bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start, bool load_eject) bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start, bool load_eject) {
{
(void) lun; (void) lun;
(void) power_condition; (void) power_condition;
if ( load_eject ) if (load_eject) {
{ if (start) {
if (start)
{
// load disk storage // load disk storage
}else } else {
{
// unload disk storage // unload disk storage
} }
} }
@@ -269,10 +259,9 @@ bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start, boo
// Callback invoked when received READ10 command. // Callback invoked when received READ10 command.
// Copy disk's data to buffer (up to bufsize) and return number of copied bytes. // Copy disk's data to buffer (up to bufsize) and return number of copied bytes.
int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize) int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize) {
{
// out of ramdisk // out of ramdisk
if ( lba >= DISK_BLOCK_NUM ) return -1; if (lba >= DISK_BLOCK_NUM) return -1;
uint8_t const* addr = (lun ? msc_disk1[lba] : msc_disk0[lba]) + offset; uint8_t const* addr = (lun ? msc_disk1[lba] : msc_disk0[lba]) + offset;
memcpy(buffer, addr, bufsize); memcpy(buffer, addr, bufsize);
@@ -280,11 +269,10 @@ int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset, void* buff
return (int32_t) bufsize; return (int32_t) bufsize;
} }
bool tud_msc_is_writable_cb (uint8_t lun) bool tud_msc_is_writable_cb(uint8_t lun) {
{
(void) lun; (void) lun;
#ifdef CFG_EXAMPLE_MSC_READONLY #if defined(CFG_EXAMPLE_MSC_READONLY) || defined(CFG_EXAMPLE_MSC_DUAL_READONLY)
return false; return false;
#else #else
return true; return true;
@@ -293,16 +281,18 @@ bool tud_msc_is_writable_cb (uint8_t lun)
// Callback invoked when received WRITE10 command. // Callback invoked when received WRITE10 command.
// Process data in buffer to disk's storage and return number of written bytes // Process data in buffer to disk's storage and return number of written bytes
int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, uint8_t* buffer, uint32_t bufsize) int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, uint8_t* buffer, uint32_t bufsize) {
{
// out of ramdisk // out of ramdisk
if ( lba >= DISK_BLOCK_NUM ) return -1; if (lba >= DISK_BLOCK_NUM) return -1;
#ifndef CFG_EXAMPLE_MSC_READONLY #if defined(CFG_EXAMPLE_MSC_READONLY) || defined(CFG_EXAMPLE_MSC_DUAL_READONLY)
(void) lun;
(void) lba;
(void) offset;
(void) buffer;
#else
uint8_t* addr = (lun ? msc_disk1[lba] : msc_disk0[lba]) + offset; uint8_t* addr = (lun ? msc_disk1[lba] : msc_disk0[lba]) + offset;
memcpy(addr, buffer, bufsize); memcpy(addr, buffer, bufsize);
#else
(void) lun; (void) lba; (void) offset; (void) buffer;
#endif #endif
return (int32_t) bufsize; return (int32_t) bufsize;
@@ -311,8 +301,7 @@ int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, uint8_t*
// Callback invoked when received an SCSI command not in built-in list below // Callback invoked when received an SCSI command not in built-in list below
// - READ_CAPACITY10, READ_FORMAT_CAPACITY, INQUIRY, MODE_SENSE6, REQUEST_SENSE // - READ_CAPACITY10, READ_FORMAT_CAPACITY, INQUIRY, MODE_SENSE6, REQUEST_SENSE
// - READ10 and WRITE10 has their own callbacks // - READ10 and WRITE10 has their own callbacks
int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer, uint16_t bufsize) int32_t tud_msc_scsi_cb(uint8_t lun, uint8_t const scsi_cmd[16], void* buffer, uint16_t bufsize) {
{
// read10 & write10 has their own callback and MUST not be handled here // read10 & write10 has their own callback and MUST not be handled here
void const* response = NULL; void const* response = NULL;
@@ -321,8 +310,7 @@ int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer,
// most scsi handled is input // most scsi handled is input
bool in_xfer = true; bool in_xfer = true;
switch (scsi_cmd[0]) switch (scsi_cmd[0]) {
{
default: default:
// Set Sense = Invalid Command Operation // Set Sense = Invalid Command Operation
tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00); tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00);
@@ -333,15 +321,12 @@ int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer,
} }
// return resplen must not larger than bufsize // return resplen must not larger than bufsize
if ( resplen > bufsize ) resplen = bufsize; if (resplen > bufsize) resplen = bufsize;
if ( response && (resplen > 0) ) if (response && (resplen > 0)) {
{ if (in_xfer) {
if(in_xfer)
{
memcpy(buffer, response, (size_t) resplen); memcpy(buffer, response, (size_t) resplen);
}else } else {
{
// SCSI output // SCSI output
} }
} }

View File

@@ -0,0 +1,10 @@
set(MCU_VARIANT D6)
set(LD_FLASH_SIZE 64K)
set(LD_RAM_SIZE 20K)
function(update_board TARGET)
target_compile_definitions(${TARGET} PUBLIC
CFG_EXAMPLE_MSC_DUAL_READONLY
)
endfunction()

View File

@@ -0,0 +1,17 @@
#ifndef BOARD_H_
#define BOARD_H_
#ifdef __cplusplus
extern "C" {
#endif
#define LED_PORT GPIOA
#define LED_PIN GPIO_Pin_15
#define LED_STATE_ON 0
#define LED_CLOCK_EN() RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE)
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,7 @@
MCU_VARIANT = D6
CFLAGS += -DCFG_EXAMPLE_MSC_DUAL_READONLY
LDFLAGS += \
-Wl,--defsym=__flash_size=64K \
-Wl,--defsym=__ram_size=20K \

View File

@@ -1,7 +1,10 @@
set(MCU_VARIANT D6) set(MCU_VARIANT D6)
set(LD_FLASH_SIZE 64K)
set(LD_RAM_SIZE 20K)
function(update_board TARGET) function(update_board TARGET)
target_compile_definitions(${TARGET} PUBLIC target_compile_definitions(${TARGET} PUBLIC
CH32V20x_D6 CFG_EXAMPLE_MSC_DUAL_READONLY
) )
endfunction() endfunction()

View File

@@ -1,5 +1,7 @@
CFLAGS += \ MCU_VARIANT = D6
-DCH32V20x_D6
SRC_S += \ CFLAGS += -DCFG_EXAMPLE_MSC_DUAL_READONLY
$(CH32V20X_SDK_SRC)/Startup/startup_ch32v20x_D6.S
LDFLAGS += \
-Wl,--defsym=__flash_size=64K \
-Wl,--defsym=__ram_size=20K \

View File

@@ -1,7 +1,8 @@
include_guard() include_guard()
set(CH32_FAMILY ch32v20x) set(CH32_FAMILY ch32v20x)
set(SDK_DIR ${TOP}/hw/mcu/wch/${CH32_FAMILY}/EVT/EXAM/SRC) set(SDK_DIR ${TOP}/hw/mcu/wch/${CH32_FAMILY})
set(SDK_SRC_DIR ${SDK_DIR}/EVT/EXAM/SRC)
# include board specific # include board specific
include(${CMAKE_CURRENT_LIST_DIR}/boards/${BOARD}/board.cmake) include(${CMAKE_CURRENT_LIST_DIR}/boards/${BOARD}/board.cmake)
@@ -23,29 +24,30 @@ function(add_board_target BOARD_TARGET)
endif() endif()
if (NOT DEFINED LD_FILE_GNU) if (NOT DEFINED LD_FILE_GNU)
set(LD_FILE_GNU ${SDK_DIR}/Ld/Link.ld) set(LD_FILE_GNU ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/linker/${CH32_FAMILY}.ld)
endif () endif ()
set(LD_FILE_Clang ${LD_FILE_GNU}) set(LD_FILE_Clang ${LD_FILE_GNU})
if (NOT DEFINED STARTUP_FILE_GNU) if (NOT DEFINED STARTUP_FILE_GNU)
set(STARTUP_FILE_GNU ${SDK_DIR}/Startup/startup_${CH32_FAMILY}_${MCU_VARIANT}.S) set(STARTUP_FILE_GNU ${SDK_SRC_DIR}/Startup/startup_${CH32_FAMILY}_${MCU_VARIANT}.S)
endif () endif ()
set(STARTUP_FILE_Clang ${STARTUP_FILE_GNU}) set(STARTUP_FILE_Clang ${STARTUP_FILE_GNU})
add_library(${BOARD_TARGET} STATIC add_library(${BOARD_TARGET} STATIC
${SDK_DIR}/Core/core_riscv.c ${SDK_SRC_DIR}/Core/core_riscv.c
${SDK_DIR}/Peripheral/src/${CH32_FAMILY}_gpio.c ${SDK_SRC_DIR}/Peripheral/src/${CH32_FAMILY}_gpio.c
${SDK_DIR}/Peripheral/src/${CH32_FAMILY}_misc.c ${SDK_SRC_DIR}/Peripheral/src/${CH32_FAMILY}_misc.c
${SDK_DIR}/Peripheral/src/${CH32_FAMILY}_rcc.c ${SDK_SRC_DIR}/Peripheral/src/${CH32_FAMILY}_rcc.c
${SDK_DIR}/Peripheral/src/${CH32_FAMILY}_usart.c ${SDK_SRC_DIR}/Peripheral/src/${CH32_FAMILY}_usart.c
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/system_${CH32_FAMILY}.c ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/system_${CH32_FAMILY}.c
${STARTUP_FILE_${CMAKE_C_COMPILER_ID}} ${STARTUP_FILE_${CMAKE_C_COMPILER_ID}}
) )
target_include_directories(${BOARD_TARGET} PUBLIC target_include_directories(${BOARD_TARGET} PUBLIC
${SDK_DIR}/Peripheral/inc ${SDK_SRC_DIR}/Peripheral/inc
${CMAKE_CURRENT_FUNCTION_LIST_DIR} ${CMAKE_CURRENT_FUNCTION_LIST_DIR}
) )
target_compile_definitions(${BOARD_TARGET} PUBLIC target_compile_definitions(${BOARD_TARGET} PUBLIC
CH32V20x_${MCU_VARIANT}
BOARD_TUD_MAX_SPEED=OPT_MODE_FULL_SPEED BOARD_TUD_MAX_SPEED=OPT_MODE_FULL_SPEED
) )
@@ -57,6 +59,8 @@ function(add_board_target BOARD_TARGET)
) )
target_link_options(${BOARD_TARGET} PUBLIC target_link_options(${BOARD_TARGET} PUBLIC
"LINKER:--script=${LD_FILE_GNU}" "LINKER:--script=${LD_FILE_GNU}"
-Wl,--defsym=__flash_size=${LD_FLASH_SIZE}
-Wl,--defsym=__ram_size=${LD_RAM_SIZE}
-nostartfiles -nostartfiles
--specs=nosys.specs --specs=nano.specs --specs=nosys.specs --specs=nano.specs
) )

View File

@@ -7,12 +7,9 @@
# Toolchain from https://github.com/xpack-dev-tools/riscv-none-elf-gcc-xpack # Toolchain from https://github.com/xpack-dev-tools/riscv-none-elf-gcc-xpack
CROSS_COMPILE ?= riscv-none-elf- CROSS_COMPILE ?= riscv-none-elf-
# Submodules CH32_FAMILY = ch32v20x
CH32V20X_SDK = hw/mcu/wch/ch32v20x SDK_DIR = hw/mcu/wch/ch32v20x
DEPS_SUBMODULES += $(CH32V20X_SDK) SDK_SRC_DIR = $(SDK_DIR)/EVT/EXAM/SRC
# WCH-SDK paths
CH32V20X_SDK_SRC = $(CH32V20X_SDK)/EVT/EXAM/SRC
include $(TOP)/$(BOARD_PATH)/board.mk include $(TOP)/$(BOARD_PATH)/board.mk
CPU_CORE ?= rv32imac-ilp32 CPU_CORE ?= rv32imac-ilp32
@@ -23,27 +20,29 @@ CFLAGS += \
-fdata-sections \ -fdata-sections \
-ffat-lto-objects \ -ffat-lto-objects \
-flto \ -flto \
-DCH32V20x_${MCU_VARIANT} \
-DCFG_TUSB_MCU=OPT_MCU_CH32V20X \ -DCFG_TUSB_MCU=OPT_MCU_CH32V20X \
-DBOARD_TUD_MAX_SPEED=OPT_MODE_FULL_SPEED \ -DBOARD_TUD_MAX_SPEED=OPT_MODE_FULL_SPEED \
LDFLAGS_GCC += \ LDFLAGS_GCC += \
-Wl,--gc-sections \
-nostdlib -nostartfiles \ -nostdlib -nostartfiles \
--specs=nosys.specs --specs=nano.specs \ --specs=nosys.specs --specs=nano.specs \
LD_FILE = $(CH32V20X_SDK_SRC)/Ld/Link.ld LD_FILE = $(FAMILY_PATH)/linker/${CH32_FAMILY}.ld
SRC_C += \ SRC_C += \
src/portable/wch/dcd_ch32_usbfs.c \ src/portable/wch/dcd_ch32_usbfs.c \
$(CH32V20X_SDK_SRC)/Core/core_riscv.c \ $(SDK_SRC_DIR)/Core/core_riscv.c \
$(CH32V20X_SDK_SRC)/Peripheral/src/ch32v20x_gpio.c \ $(SDK_SRC_DIR)/Peripheral/src/ch32v20x_gpio.c \
$(CH32V20X_SDK_SRC)/Peripheral/src/ch32v20x_misc.c \ $(SDK_SRC_DIR)/Peripheral/src/ch32v20x_misc.c \
$(CH32V20X_SDK_SRC)/Peripheral/src/ch32v20x_rcc.c \ $(SDK_SRC_DIR)/Peripheral/src/ch32v20x_rcc.c \
$(CH32V20X_SDK_SRC)/Peripheral/src/ch32v20x_usart.c \ $(SDK_SRC_DIR)/Peripheral/src/ch32v20x_usart.c \
SRC_S += $(SDK_SRC_DIR)/Startup/startup_ch32v20x_${MCU_VARIANT}.S
INC += \ INC += \
$(TOP)/$(BOARD_PATH) \ $(TOP)/$(BOARD_PATH) \
$(TOP)/$(CH32V20X_SDK_SRC)/Peripheral/inc \ $(TOP)/$(SDK_SRC_DIR)/Peripheral/inc \
FREERTOS_PORTABLE_SRC = $(FREERTOS_PORTABLE_PATH)/RISC-V FREERTOS_PORTABLE_SRC = $(FREERTOS_PORTABLE_PATH)/RISC-V

View File

@@ -0,0 +1 @@
/* Define default values if not already defined */