- add windows workaround
- add mouse acceleration (configurable in user_config.h) - add keyboard shortcuts for output configuration - after several reports, bundling pico-sdk and tinyusb to simplify building - bugfixes
This commit is contained in:
parent
3500497d96
commit
09935f5f87
|
@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.5)
|
|||
|
||||
set(PICO_SDK_FETCH_FROM_GIT off)
|
||||
set(PICO_BOARD=pico)
|
||||
set(PICO_SDK_PATH ${CMAKE_CURRENT_LIST_DIR}/pico-sdk)
|
||||
|
||||
include(pico_sdk_import.cmake)
|
||||
set(CMAKE_C_FLAGS "-Ofast -Wall -mcpu=cortex-m0plus -mtune=cortex-m0plus -funroll-loops")
|
||||
|
@ -79,6 +80,7 @@ foreach(board_role RANGE 0 1)
|
|||
|
||||
target_sources(${binary} PUBLIC ${COMMON_SOURCES})
|
||||
target_compile_definitions(${binary} PRIVATE BOARD_ROLE=${board_role} PIO_USB_USE_TINYUSB=1 PIO_USB_DP_PIN_DEFAULT=14)
|
||||
|
||||
target_include_directories(${binary} PUBLIC ${COMMON_INCLUDES})
|
||||
target_link_libraries(${binary} PUBLIC ${COMMON_LINK_LIBRARIES})
|
||||
|
||||
|
|
29
README.md
29
README.md
|
@ -52,8 +52,12 @@ It also remembers the LED state for each computer, so you can pick up exactly ho
|
|||
|
||||
## How to build
|
||||
|
||||
To avoid version mismatch and reported path issues when building, the project now bundles minimal pico sdk and tinyusb.
|
||||
|
||||
You should be able to build by running:
|
||||
|
||||
```
|
||||
PICO_BOARD=pico PICO_SDK_PATH=/your/sdk/path cmake -S . -B build
|
||||
cmake -S . -B build
|
||||
cmake --build build
|
||||
```
|
||||
|
||||
|
@ -198,6 +202,7 @@ _Firmware upgrade_
|
|||
_Usage_
|
||||
- ```Right CTRL + Right ALT``` - Toggle slower mouse mode
|
||||
- ```Right CTRL + L``` - Lock/Unlock mouse desktop switching
|
||||
- ```Right ALT + Right Shift + L``` - Lock both outputs at once (set output OS before, see shortcuts below)
|
||||
- ```Caps Lock``` - Switch between outputs
|
||||
|
||||
_Config_
|
||||
|
@ -205,6 +210,15 @@ _Config_
|
|||
- ```Right Shift + F12 + Y``` - save screen switch offset
|
||||
- ```Right Shift + F12 + S``` - turn on/off screensaver option
|
||||
|
||||
_Number of outputs_
|
||||
- ```Right Shift + Backspace + 1``` - set number of screens to 1 on current active output
|
||||
- ```Right Shift + Backspace + 2``` - set number of screens to 2 on current active output
|
||||
|
||||
_Set operating systems_
|
||||
- ```Right Shift + Backspace + 7``` - set os to Linux on current active output
|
||||
- ```Right Shift + Backspace + 8``` - set os to Windows on current active output
|
||||
- ```Right Shift + Backspace + 9``` - set os to MacOS on current active output
|
||||
|
||||
### Switch cursor height calibration
|
||||
|
||||
This step is not required, but it can be handy if your screens are not perfectly aligned or differ in size. The objective is to have the mouse pointer come out at exactly the same height.
|
||||
|
@ -215,9 +229,16 @@ Just park your mouse on the LARGER screen at the height of the smaller/lower scr
|
|||
|
||||
Repeat for the bottom border (if it's above the larger screen's border). This will get saved to flash and it should keep this calibration value from now on.
|
||||
|
||||
|
||||
### Multiple screens per output
|
||||
|
||||
Windows and Mac have issues with multiple screens and absolute positioning, so workarounds are needed (still experimental). Your main screens need to be in the middle, and secondary screen(s) on the edges. Move the mouse to the screen with multiple desktops and press *right shift + backspace + 2* if you have 2 desktops and *right shift + backspace + 7, 8 or 9* depending on your OS (Linux, Windows, Mac).
|
||||
|
||||
![Image](img/deskhop-scr.png)
|
||||
|
||||
### Other configuration
|
||||
|
||||
Mouse speed can now be configured per output screen and per axis. If you have multiple displays under Linux, your X speed is probably too fast, so you need to configure it in user_config.h and rebuild. In the future, this will be configurable without having to do that.
|
||||
Mouse speed can now be configured per output screen and per axis. If you have multiple displays under Linux, your X speed might be too fast, so you need to configure it in user_config.h and rebuild. In the future, this will be configurable without having to do that.
|
||||
|
||||
### Functional verification
|
||||
|
||||
|
@ -288,13 +309,13 @@ There are several software alternatives you can use if that works in your partic
|
|||
|
||||
## Shortcomings
|
||||
|
||||
- Windows 10 broke HID absolute coordinates behavior in KB5003637, so you can't use more than 1 screen on Windows (mouse will stay on the main screen).
|
||||
- Windows 10 broke HID absolute coordinates behavior in KB5003637, so you can't use more than 1 screen on Windows (mouse will stay on the main screen). There is an experimental workaround.
|
||||
- Code needs cleanup, some refactoring etc.
|
||||
- Not tested with a wide variety of devices, I don't know how it will work with your hardware. There is a reasonable chance things might not work out-of-the-box.
|
||||
- Advanced keyboards (with knobs, extra buttons or sliders) will probably face issues where this additional hardware doesn't work.
|
||||
- Super-modern mice with 300 buttons might see some buttons not work as expected.
|
||||
- NOTE: Both computers need to be connected and provide power to the USB for this to work (as each board gets powered by the computer it plugs into). Many desktops and laptops will provide power even when shut down nowadays. If you need to run with one board fully disconnected, you should be able to use a USB hub to plug both keyboard and mouse to a single port.
|
||||
- MacOS has issues with more than one screens, latest firmware offers an experimental workaround that fixes it (but for the time being you are required to define the output as MACOS in user_config.h, set number of screens in defaults.c and rebuild). This should be a keyboard shortcut soon with no need to rebuild.
|
||||
- MacOS has issues with more than one screens, latest firmware offers an experimental workaround that fixes it.
|
||||
|
||||
## Progress
|
||||
|
||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
After Width: | Height: | Size: 15 KiB |
|
@ -0,0 +1,61 @@
|
|||
cmake_minimum_required(VERSION 3.13)
|
||||
|
||||
# Note: this CMakeLists.txt can be used as a top-level CMakeLists.txt for the SDK itself. For all other uses
|
||||
# it is included as a subdirectory via the pico_sdk_init() method provided by pico_sdk_init.cmake
|
||||
if (NOT TARGET _pico_sdk_inclusion_marker)
|
||||
add_library(_pico_sdk_inclusion_marker INTERFACE)
|
||||
# This is a no-op unless we are the top-level CMakeLists.txt
|
||||
include(pico_sdk_init.cmake)
|
||||
|
||||
project(pico_sdk C CXX ASM)
|
||||
|
||||
string(REGEX MATCH "Clang" PICO_C_COMPILER_IS_CLANG "${CMAKE_C_COMPILER_ID}")
|
||||
string(REGEX MATCH "GNU" PICO_C_COMPILER_IS_GNU "${CMAKE_C_COMPILER_ID}")
|
||||
string(REGEX MATCH "IAR" PICO_C_COMPILER_IS_IAR "${CMAKE_C_COMPILER_ID}")
|
||||
pico_register_common_scope_var(PICO_C_COMPILER_IS_CLANG)
|
||||
pico_register_common_scope_var(PICO_C_COMPILER_IS_GNU)
|
||||
pico_register_common_scope_var(PICO_C_COMPILER_IS_IAR)
|
||||
|
||||
message("Build type is ${CMAKE_BUILD_TYPE}")
|
||||
if ("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
|
||||
if (PICO_DEOPTIMIZED_DEBUG)
|
||||
message("Using fully de-optimized debug build (set PICO_DEOPTIMIZED_DEBUG=0 to optimize)")
|
||||
else()
|
||||
message("Using regular optimized debug build (set PICO_DEOPTIMIZED_DEBUG=1 to de-optimize)")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
pico_is_top_level_project(PICO_SDK_TOP_LEVEL_PROJECT)
|
||||
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
set(CMAKE_CXX_STANDARD 11)
|
||||
|
||||
if (NOT PICO_SDK_TOP_LEVEL_PROJECT)
|
||||
set(PICO_SDK 1 PARENT_SCOPE)
|
||||
endif()
|
||||
|
||||
# allow customization
|
||||
add_sub_list_dirs(PICO_SDK_PRE_LIST_DIRS)
|
||||
add_sub_list_files(PICO_SDK_PRE_LIST_FILES)
|
||||
|
||||
add_subdirectory(tools)
|
||||
add_subdirectory(src)
|
||||
|
||||
# allow customization
|
||||
add_sub_list_dirs(PICO_SDK_POST_LIST_DIRS)
|
||||
add_sub_list_files(PICO_SDK_POST_LIST_FILES)
|
||||
|
||||
if (PICO_SDK_TOP_LEVEL_PROJECT AND NOT DEFINED PICO_SDK_TESTS_ENABLED)
|
||||
set(PICO_SDK_TESTS_ENABLED 1)
|
||||
endif()
|
||||
if (PICO_SDK_TESTS_ENABLED)
|
||||
add_subdirectory(test)
|
||||
endif ()
|
||||
|
||||
set(PICO_SDK_TESTS_ENABLED "${PICO_SDK_TESTS_ENABLED}" CACHE INTERNAL "Enable build of SDK tests")
|
||||
|
||||
if (NOT PICO_SDK_TOP_LEVEL_PROJECT)
|
||||
pico_promote_common_scope_vars()
|
||||
endif()
|
||||
endif()
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
# this is included because toolchain file sets SYSTEM_NAME=PICO
|
||||
|
||||
set_property(GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS FALSE)
|
||||
set(CMAKE_EXECUTABLE_SUFFIX .elf)
|
|
@ -0,0 +1,29 @@
|
|||
# PICO_CMAKE_CONFIG: PICO_PLATFORM, platform to build for e.g. rp2040/host, default=rp2040 or environment value, group=build
|
||||
if (DEFINED ENV{PICO_PLATFORM} AND (NOT PICO_PLATFORM))
|
||||
set(PICO_PLATFORM $ENV{PICO_PLATFORM})
|
||||
message("Using PICO_PLATFORM from environment ('${PICO_PLATFORM}')")
|
||||
else()
|
||||
if (NOT PICO_PLATFORM)
|
||||
set(PICO_PLATFORM "rp2040")
|
||||
pico_message("Defaulting PICO_PLATFORM to ${PICO_PLATFORM} since not specified.")
|
||||
else()
|
||||
message("PICO platform is ${PICO_PLATFORM}.")
|
||||
endif()
|
||||
endif ()
|
||||
|
||||
set(PICO_PLATFORM ${PICO_PLATFORM} CACHE STRING "PICO Build platform (e.g. rp2040, host)")
|
||||
|
||||
# PICO_CMAKE_CONFIG: PICO_CMAKE_PRELOAD_PLATFORM_FILE, custom CMake file to use to set up the platform environment, default=none, group=build
|
||||
set(PICO_CMAKE_PRELOAD_PLATFORM_FILE "" CACHE INTERNAL "")
|
||||
set(PICO_CMAKE_PRELOAD_PLATFORM_DIR "${CMAKE_CURRENT_LIST_DIR}/preload/platforms" CACHE INTERNAL "")
|
||||
|
||||
if (NOT PICO_CMAKE_PRELOAD_PLATFORM_FILE)
|
||||
set(PICO_CMAKE_PRELOAD_PLATFORM_FILE ${PICO_CMAKE_PRELOAD_PLATFORM_DIR}/${PICO_PLATFORM}.cmake CACHE INTERNAL "")
|
||||
endif ()
|
||||
|
||||
if (NOT EXISTS "${PICO_CMAKE_PRELOAD_PLATFORM_FILE}")
|
||||
message(FATAL_ERROR "${PICO_CMAKE_PRELOAD_PLATFORM_FILE} does not exist. \
|
||||
Either specify a valid PICO_PLATFORM (or PICO_CMAKE_PRELOAD_PLATFORM_FILE).")
|
||||
endif ()
|
||||
|
||||
include(${PICO_CMAKE_PRELOAD_PLATFORM_FILE})
|
|
@ -0,0 +1,43 @@
|
|||
# PICO_CMAKE_CONFIG: PICO_TOOLCHAIN_PATH, Path to search for compiler, default=none (i.e. search system paths), group=build
|
||||
set(PICO_TOOLCHAIN_PATH "${PICO_TOOLCHAIN_PATH}" CACHE INTERNAL "")
|
||||
|
||||
# Set a default build type if none was specified
|
||||
set(default_build_type "Release")
|
||||
|
||||
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
|
||||
message(STATUS "Defaulting build type to '${default_build_type}' since not specified.")
|
||||
set(CMAKE_BUILD_TYPE "${default_build_type}" CACHE STRING "Choose the type of build, options are: 'Debug', 'Release', 'MinSizeRel', 'RelWithDebInfo'." FORCE)
|
||||
# Set the possible values of build type for cmake-gui
|
||||
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS
|
||||
"Debug" "Release" "MinSizeRel" "RelWithDebInfo")
|
||||
endif()
|
||||
|
||||
if (CMAKE_BUILD_TYPE STREQUAL "Default")
|
||||
error("Default build type is NOT supported")
|
||||
endif()
|
||||
|
||||
# PICO_CMAKE_CONFIG: PICO_COMPILER, Optionally specifies a different compiler (other than pico_arm_gcc.cmake) - this is not yet fully supported, default=none, group=build
|
||||
# If PICO_COMPILER is specified, set toolchain file to ${PICO_COMPILER}.cmake.
|
||||
if (DEFINED PICO_COMPILER)
|
||||
if (DEFINED CMAKE_TOOLCHAIN_FILE)
|
||||
get_filename_component(toolchain "${CMAKE_TOOLCHAIN_FILE}" NAME_WE)
|
||||
if (NOT "${PICO_COMPILER}" STREQUAL "${toolchain}")
|
||||
message(WARNING "CMAKE_TOOLCHAIN_FILE is already defined to ${toolchain}.cmake, you\
|
||||
need to delete cache and reconfigure if you want to switch compiler.")
|
||||
endif ()
|
||||
else ()
|
||||
set(toolchain_dir "${CMAKE_CURRENT_LIST_DIR}/preload/toolchains")
|
||||
set(toolchain_file "${toolchain_dir}/${PICO_COMPILER}.cmake")
|
||||
if (EXISTS "${toolchain_file}")
|
||||
set(CMAKE_TOOLCHAIN_FILE "${toolchain_file}" CACHE INTERNAL "")
|
||||
else ()
|
||||
# todo improve message
|
||||
message(FATAL_ERROR "Toolchain file \"${PICO_COMPILER}.cmake\" does not exist, please\
|
||||
select one from \"cmake/toolchains\" folder.")
|
||||
endif ()
|
||||
endif ()
|
||||
message("PICO compiler is ${PICO_COMPILER}")
|
||||
endif ()
|
||||
|
||||
unset(PICO_COMPILER CACHE)
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
function(pico_message param)
|
||||
if (${ARGC} EQUAL 1)
|
||||
message("${param}")
|
||||
return()
|
||||
endif ()
|
||||
|
||||
if (NOT ${ARGC} EQUAL 2)
|
||||
message(FATAL_ERROR "Expect at most 2 arguments")
|
||||
endif ()
|
||||
message("${param}" "${ARGV1}")
|
||||
endfunction()
|
||||
|
||||
macro(assert VAR MSG)
|
||||
if (NOT ${VAR})
|
||||
message(FATAL_ERROR "${MSG}")
|
||||
endif ()
|
||||
endmacro()
|
||||
|
||||
function(pico_find_in_paths OUT PATHS NAME)
|
||||
foreach(PATH IN LISTS ${PATHS})
|
||||
if (EXISTS ${PATH}/${NAME})
|
||||
get_filename_component(FULLNAME ${PATH}/${NAME} ABSOLUTE)
|
||||
set(${OUT} ${FULLNAME} PARENT_SCOPE)
|
||||
return()
|
||||
endif()
|
||||
endforeach()
|
||||
set(${OUT} "" PARENT_SCOPE)
|
||||
endfunction()
|
|
@ -0,0 +1,7 @@
|
|||
if (NOT (DEFINED PICO_COMPILER OR DEFINED CMAKE_TOOLCHAIN_FILE))
|
||||
pico_message("Defaulting PICO platform compiler to pico_arm_gcc since not specified.")
|
||||
set(PICO_COMPILER "pico_arm_gcc")
|
||||
endif ()
|
||||
|
||||
|
||||
|
|
@ -0,0 +1 @@
|
|||
include(${CMAKE_CURRENT_LIST_DIR}/pico/pico.cmake)
|
|
@ -0,0 +1,31 @@
|
|||
# Toolchain file is processed multiple times, however, it cannot access CMake cache on some runs.
|
||||
# We store the search path in an environment variable so that we can always access it.
|
||||
if (NOT "${PICO_TOOLCHAIN_PATH}" STREQUAL "")
|
||||
set(ENV{PICO_TOOLCHAIN_PATH} "${PICO_TOOLCHAIN_PATH}")
|
||||
endif ()
|
||||
|
||||
# Find the compiler executable and store its path in a cache entry ${compiler_path}.
|
||||
# If not found, issue a fatal message and stop processing. PICO_TOOLCHAIN_PATH can be provided from
|
||||
# commandline as additional search path.
|
||||
function(pico_find_compiler compiler_path compiler_exe)
|
||||
# Search user provided path first.
|
||||
find_program(
|
||||
${compiler_path} ${compiler_exe}
|
||||
PATHS ENV PICO_TOOLCHAIN_PATH
|
||||
PATH_SUFFIXES bin
|
||||
NO_DEFAULT_PATH
|
||||
)
|
||||
|
||||
# If not then search system paths.
|
||||
if ("${${compiler_path}}" STREQUAL "${compiler_path}-NOTFOUND")
|
||||
if (DEFINED ENV{PICO_TOOLCHAIN_PATH})
|
||||
message(WARNING "PICO_TOOLCHAIN_PATH specified ($ENV{PICO_TOOLCHAIN_PATH}), but ${compiler_exe} not found there")
|
||||
endif()
|
||||
find_program(${compiler_path} ${compiler_exe})
|
||||
endif ()
|
||||
if ("${${compiler_path}}" STREQUAL "${compiler_path}-NOTFOUND")
|
||||
set(PICO_TOOLCHAIN_PATH "" CACHE PATH "Path to search for compiler.")
|
||||
message(FATAL_ERROR "Compiler '${compiler_exe}' not found, you can specify search path with\
|
||||
\"PICO_TOOLCHAIN_PATH\".")
|
||||
endif ()
|
||||
endfunction()
|
|
@ -0,0 +1,47 @@
|
|||
# NOTE: THIS IS A WIP ONLY PICO_ARM_GCC IS CURRENTLY SUPPORTED, however should work with LLVM Embedded Toolchain for ARM
|
||||
# version 14.0.0 https://github.com/ARM-software/LLVM-embedded-toolchain-for-Arm/releases/tag/release-14.0.0
|
||||
# todo there is probably a more "cmake" way of doing this going thru the standard path with our "PICO" platform
|
||||
# i.e. CMake<Lang>Information and whatnot
|
||||
include(${CMAKE_CURRENT_LIST_DIR}/find_compiler.cmake)
|
||||
|
||||
# include our Platform/pico.cmake
|
||||
set(CMAKE_SYSTEM_NAME PICO)
|
||||
set(CMAKE_SYSTEM_PROCESSOR cortex-m0plus)
|
||||
|
||||
# Find CLANG
|
||||
pico_find_compiler(PICO_COMPILER_CC clang)
|
||||
pico_find_compiler(PICO_COMPILER_CXX clang)
|
||||
#pico_find_compiler(PICO_COMPILER_ASM armasm)
|
||||
set(PICO_COMPILER_ASM "${PICO_COMPILER_CC}" CACHE INTERNAL "")
|
||||
pico_find_compiler(PICO_OBJCOPY llvm-objcopy)
|
||||
pico_find_compiler(PICO_OBJDUMP llvm-objdump)
|
||||
|
||||
# Specify the cross compiler.
|
||||
set(CMAKE_C_COMPILER ${PICO_COMPILER_CC} CACHE FILEPATH "C compiler")
|
||||
set(CMAKE_CXX_COMPILER ${PICO_COMPILER_CXX} CACHE FILEPATH "C++ compiler")
|
||||
set(CMAKE_C_OUTPUT_EXTENSION .o)
|
||||
|
||||
# todo should we be including CMakeASMInformation anyway - i guess that is host side
|
||||
set(CMAKE_ASM_COMPILER ${PICO_COMPILER_ASM} CACHE FILEPATH "ASM compiler")
|
||||
set(CMAKE_ASM_COMPILE_OBJECT "<CMAKE_ASM_COMPILER> <DEFINES> <INCLUDES> <FLAGS> -o <OBJECT> -c <SOURCE>")
|
||||
set(CMAKE_INCLUDE_FLAG_ASM "-I")
|
||||
set(CMAKE_OBJCOPY ${PICO_OBJCOPY} CACHE FILEPATH "")
|
||||
set(CMAKE_OBJDUMP ${PICO_OBJDUMP} CACHE FILEPATH "")
|
||||
|
||||
# Add target system root to cmake find path.
|
||||
get_filename_component(PICO_COMPILER_DIR "${PICO_COMPILER_CC}" DIRECTORY)
|
||||
get_filename_component(CMAKE_FIND_ROOT_PATH "${PICO_COMPILER_DIR}" DIRECTORY)
|
||||
|
||||
# Look for includes and libraries only in the target system prefix.
|
||||
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
|
||||
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
|
||||
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
|
||||
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
|
||||
|
||||
option(PICO_DEOPTIMIZED_DEBUG "Build debug builds with -O0" 0)
|
||||
|
||||
# Oz is preferred for Clang (verses CMake default -Os) see also https://gitlab.kitware.com/cmake/cmake/-/issues/22458
|
||||
set(CMAKE_C_FLAGS_MINSIZEREL "-Oz -DNDEBUG")
|
||||
|
||||
set(ARM_TOOLCHAIN_COMMON_FLAGS "--target=armv6m-none-eabi -mfloat-abi=soft -march=armv6m --sysroot ${PICO_COMPILER_DIR}/../lib/clang-runtimes/armv6m_soft_nofp")
|
||||
include(${CMAKE_CURRENT_LIST_DIR}/set_flags.cmake)
|
|
@ -0,0 +1,43 @@
|
|||
# NOTE: THIS IS A WIP ONLY PICO_ARM_GCC IS CURRENTLY SUPPORTED
|
||||
# todo there is probably a more "cmake" way of doing this going thru the standard path with our "PICO" platform
|
||||
# i.e. CMake<Lang>Information and whatnot
|
||||
include(${CMAKE_CURRENT_LIST_DIR}/find_compiler.cmake)
|
||||
|
||||
# include our Platform/PICO.cmake
|
||||
set(CMAKE_SYSTEM_NAME PICO)
|
||||
set(CMAKE_SYSTEM_PROCESSOR cortex-m0plus)
|
||||
|
||||
# Find ARMClang.
|
||||
pico_find_compiler(PICO_COMPILER_CC armclang)
|
||||
pico_find_compiler(PICO_COMPILER_CXX armclang)
|
||||
pico_find_compiler(PICO_COMPILER_ASM armasm)
|
||||
set(PICO_COMPILER_ASM "${PICO_COMPILER_ASM}" CACHE INTERNAL "")
|
||||
pico_find_compiler(PICO_OBJCOPY llvm-objcopy)
|
||||
pico_find_compiler(PICO_OBJDUMP llvm-objdump)
|
||||
|
||||
# Specify the cross compiler.
|
||||
set(CMAKE_C_COMPILER ${PICO_COMPILER_CC} CACHE FILEPATH "C compiler")
|
||||
set(CMAKE_CXX_COMPILER ${PICO_COMPILER_CXX} CACHE FILEPATH "C++ compiler")
|
||||
set(CMAKE_C_OUTPUT_EXTENSION .o)
|
||||
|
||||
# todo should we be including CMakeASMInformation anyway - i guess that is host side
|
||||
set(CMAKE_ASM_COMPILER ${PICO_COMPILER_ASM} CACHE FILEPATH "ASM compiler")
|
||||
set(CMAKE_ASM_COMPILE_OBJECT "<CMAKE_ASM_COMPILER> <DEFINES> <INCLUDES> <FLAGS> -o <OBJECT> -c <SOURCE>")
|
||||
set(CMAKE_INCLUDE_FLAG_ASM "-I")
|
||||
set(CMAKE_OBJCOPY ${PICO_OBJCOPY} CACHE FILEPATH "")
|
||||
set(CMAKE_OBJDUMP ${PICO_OBJDUMP} CACHE FILEPATH "")
|
||||
|
||||
# Add target system root to cmake find path.
|
||||
get_filename_component(PICO_COMPILER_DIR "${PICO_COMPILER_CC}" DIRECTORY)
|
||||
get_filename_component(CMAKE_FIND_ROOT_PATH "${PICO_COMPILER_DIR}" DIRECTORY)
|
||||
|
||||
# Look for includes and libraries only in the target system prefix.
|
||||
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
|
||||
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
|
||||
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
|
||||
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
|
||||
|
||||
option(PICO_DEOPTIMIZED_DEBUG "Build debug builds with -O0" 0)
|
||||
|
||||
set(ARM_TOOLCHAIN_COMMON_FLAGS " --cpu=Cortex-M0plus")
|
||||
include(${CMAKE_CURRENT_LIST_DIR}/set_flags.cmake)
|
|
@ -0,0 +1,52 @@
|
|||
# todo there is probably a more "cmake" way of doing this going thru the standard path with our "PICO" platform
|
||||
# i.e. CMake<Lang>Information and whatnot
|
||||
include(${CMAKE_CURRENT_LIST_DIR}/find_compiler.cmake)
|
||||
|
||||
# include our Platform/PICO.cmake
|
||||
set(CMAKE_SYSTEM_NAME PICO)
|
||||
set(CMAKE_SYSTEM_PROCESSOR cortex-m0plus)
|
||||
|
||||
if (NOT PICO_GCC_TRIPLE)
|
||||
if (DEFINED ENV{PICO_GCC_TRIPLE})
|
||||
set(PICO_GCC_TRIPLE $ENV{PICO_GCC_TRIPLE})
|
||||
message("PICO_GCC_TRIPLE set from environment: $ENV{PICO_GCC_TRIPLE}")
|
||||
else()
|
||||
set(PICO_GCC_TRIPLE arm-none-eabi)
|
||||
#pico_message_debug("PICO_GCC_TRIPLE defaulted to arm-none-eabi")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# Find GCC for ARM.
|
||||
pico_find_compiler(PICO_COMPILER_CC ${PICO_GCC_TRIPLE}-gcc)
|
||||
pico_find_compiler(PICO_COMPILER_CXX ${PICO_GCC_TRIPLE}-g++)
|
||||
set(PICO_COMPILER_ASM "${PICO_COMPILER_CC}" CACHE INTERNAL "")
|
||||
pico_find_compiler(PICO_OBJCOPY ${PICO_GCC_TRIPLE}-objcopy)
|
||||
pico_find_compiler(PICO_OBJDUMP ${PICO_GCC_TRIPLE}-objdump)
|
||||
|
||||
# Specify the cross compiler.
|
||||
set(CMAKE_C_COMPILER ${PICO_COMPILER_CC} CACHE FILEPATH "C compiler")
|
||||
set(CMAKE_CXX_COMPILER ${PICO_COMPILER_CXX} CACHE FILEPATH "C++ compiler")
|
||||
set(CMAKE_C_OUTPUT_EXTENSION .o)
|
||||
|
||||
# todo should we be including CMakeASMInformation anyway - i guess that is host side
|
||||
set(CMAKE_ASM_COMPILER ${PICO_COMPILER_ASM} CACHE FILEPATH "ASM compiler")
|
||||
set(CMAKE_ASM_COMPILE_OBJECT "<CMAKE_ASM_COMPILER> <DEFINES> <INCLUDES> <FLAGS> -o <OBJECT> -c <SOURCE>")
|
||||
set(CMAKE_INCLUDE_FLAG_ASM "-I")
|
||||
set(CMAKE_OBJCOPY ${PICO_OBJCOPY} CACHE FILEPATH "")
|
||||
set(CMAKE_OBJDUMP ${PICO_OBJDUMP} CACHE FILEPATH "")
|
||||
|
||||
# Add target system root to cmake find path.
|
||||
get_filename_component(PICO_COMPILER_DIR "${PICO_COMPILER_CC}" DIRECTORY)
|
||||
get_filename_component(CMAKE_FIND_ROOT_PATH "${PICO_COMPILER_DIR}" DIRECTORY)
|
||||
|
||||
# Look for includes and libraries only in the target system prefix.
|
||||
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
|
||||
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
|
||||
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
|
||||
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
|
||||
|
||||
option(PICO_DEOPTIMIZED_DEBUG "Build debug builds with -O0" 0)
|
||||
|
||||
# on ARM -mcpu should not be mixed with -march
|
||||
set(ARM_TOOLCHAIN_COMMON_FLAGS " -mcpu=cortex-m0plus -mthumb")
|
||||
include(${CMAKE_CURRENT_LIST_DIR}/set_flags.cmake)
|
|
@ -0,0 +1,19 @@
|
|||
|
||||
get_property(IS_IN_TRY_COMPILE GLOBAL PROPERTY IN_TRY_COMPILE)
|
||||
foreach(LANG IN ITEMS C CXX ASM)
|
||||
set(CMAKE_${LANG}_FLAGS_INIT "${ARM_TOOLCHAIN_COMMON_FLAGS}")
|
||||
if (PICO_DEOPTIMIZED_DEBUG)
|
||||
set(CMAKE_${LANG}_FLAGS_DEBUG_INIT "-O0")
|
||||
else()
|
||||
set(CMAKE_${LANG}_FLAGS_DEBUG_INIT "-Og")
|
||||
endif()
|
||||
set(CMAKE_${LANG}_LINK_FLAGS "-Wl,--build-id=none")
|
||||
|
||||
# try_compile is where the feature testing is done, and at that point,
|
||||
# pico_standard_link is not ready to be linked in to provide essential
|
||||
# functions like _exit. So pass -nostdlib so it doesn't link in an exit()
|
||||
# function at all.
|
||||
if(IS_IN_TRY_COMPILE)
|
||||
set(CMAKE_${LANG}_LINK_FLAGS "${CMAKE_${LANG}_LINK_FLAGS} -nostdlib")
|
||||
endif()
|
||||
endforeach()
|
|
@ -0,0 +1,21 @@
|
|||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2018, hathach (tinyusb.org)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
|
@ -0,0 +1,97 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
/** \ingroup group_board
|
||||
* \defgroup group_ansi_esc ANSI Escape Code
|
||||
* @{ */
|
||||
|
||||
#ifndef _TUSB_ANSI_ESC_CODE_H_
|
||||
#define _TUSB_ANSI_ESC_CODE_H_
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define CSI_CODE(seq) "\33[" seq
|
||||
#define CSI_SGR(x) CSI_CODE(#x) "m"
|
||||
|
||||
//------------- Cursor movement -------------//
|
||||
/** \defgroup group_ansi_cursor Cursor Movement
|
||||
* @{ */
|
||||
#define ANSI_CURSOR_UP(n) CSI_CODE(#n "A") ///< Move cursor up
|
||||
#define ANSI_CURSOR_DOWN(n) CSI_CODE(#n "B") ///< Move cursor down
|
||||
#define ANSI_CURSOR_FORWARD(n) CSI_CODE(#n "C") ///< Move cursor forward
|
||||
#define ANSI_CURSOR_BACKWARD(n) CSI_CODE(#n "D") ///< Move cursor backward
|
||||
#define ANSI_CURSOR_LINE_DOWN(n) CSI_CODE(#n "E") ///< Move cursor to the beginning of the line (n) down
|
||||
#define ANSI_CURSOR_LINE_UP(n) CSI_CODE(#n "F") ///< Move cursor to the beginning of the line (n) up
|
||||
#define ANSI_CURSOR_POSITION(n, m) CSI_CODE(#n ";" #m "H") ///< Move cursor to position (n, m)
|
||||
/** @} */
|
||||
|
||||
//------------- Screen -------------//
|
||||
/** \defgroup group_ansi_screen Screen Control
|
||||
* @{ */
|
||||
#define ANSI_ERASE_SCREEN(n) CSI_CODE(#n "J") ///< Erase the screen
|
||||
#define ANSI_ERASE_LINE(n) CSI_CODE(#n "K") ///< Erase the line (n)
|
||||
#define ANSI_SCROLL_UP(n) CSI_CODE(#n "S") ///< Scroll the whole page up (n) lines
|
||||
#define ANSI_SCROLL_DOWN(n) CSI_CODE(#n "T") ///< Scroll the whole page down (n) lines
|
||||
/** @} */
|
||||
|
||||
//------------- Text Color -------------//
|
||||
/** \defgroup group_ansi_text Text Color
|
||||
* @{ */
|
||||
#define ANSI_TEXT_BLACK CSI_SGR(30)
|
||||
#define ANSI_TEXT_RED CSI_SGR(31)
|
||||
#define ANSI_TEXT_GREEN CSI_SGR(32)
|
||||
#define ANSI_TEXT_YELLOW CSI_SGR(33)
|
||||
#define ANSI_TEXT_BLUE CSI_SGR(34)
|
||||
#define ANSI_TEXT_MAGENTA CSI_SGR(35)
|
||||
#define ANSI_TEXT_CYAN CSI_SGR(36)
|
||||
#define ANSI_TEXT_WHITE CSI_SGR(37)
|
||||
#define ANSI_TEXT_DEFAULT CSI_SGR(39)
|
||||
/** @} */
|
||||
|
||||
//------------- Background Color -------------//
|
||||
/** \defgroup group_ansi_background Background Color
|
||||
* @{ */
|
||||
#define ANSI_BG_BLACK CSI_SGR(40)
|
||||
#define ANSI_BG_RED CSI_SGR(41)
|
||||
#define ANSI_BG_GREEN CSI_SGR(42)
|
||||
#define ANSI_BG_YELLOW CSI_SGR(43)
|
||||
#define ANSI_BG_BLUE CSI_SGR(44)
|
||||
#define ANSI_BG_MAGENTA CSI_SGR(45)
|
||||
#define ANSI_BG_CYAN CSI_SGR(46)
|
||||
#define ANSI_BG_WHITE CSI_SGR(47)
|
||||
#define ANSI_BG_DEFAULT CSI_SGR(49)
|
||||
/** @} */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _TUSB_ANSI_ESC_CODE_H_ */
|
||||
|
||||
/** @} */
|
|
@ -0,0 +1,114 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2018, hathach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "board_api.h"
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// newlib read()/write() retarget
|
||||
//--------------------------------------------------------------------+
|
||||
#ifdef __ICCARM__
|
||||
#define sys_write __write
|
||||
#define sys_read __read
|
||||
#elif defined(__MSP430__) || defined(__RX__)
|
||||
#define sys_write write
|
||||
#define sys_read read
|
||||
#else
|
||||
#define sys_write _write
|
||||
#define sys_read _read
|
||||
#endif
|
||||
|
||||
#if defined(LOGGER_RTT)
|
||||
// Logging with RTT
|
||||
|
||||
// If using SES IDE, use the Syscalls/SEGGER_RTT_Syscalls_SES.c instead
|
||||
#if !(defined __SES_ARM) && !(defined __SES_RISCV) && !(defined __CROSSWORKS_ARM)
|
||||
#include "SEGGER_RTT.h"
|
||||
|
||||
TU_ATTR_USED int sys_write(int fhdl, const char *buf, size_t count) {
|
||||
(void) fhdl;
|
||||
SEGGER_RTT_Write(0, (const char *) buf, (int) count);
|
||||
return (int) count;
|
||||
}
|
||||
|
||||
TU_ATTR_USED int sys_read(int fhdl, char *buf, size_t count) {
|
||||
(void) fhdl;
|
||||
int rd = (int) SEGGER_RTT_Read(0, buf, count);
|
||||
return (rd > 0) ? rd : -1;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#elif defined(LOGGER_SWO)
|
||||
// Logging with SWO for ARM Cortex
|
||||
#include "board_mcu.h"
|
||||
|
||||
TU_ATTR_USED int sys_write (int fhdl, const char *buf, size_t count) {
|
||||
(void) fhdl;
|
||||
uint8_t const* buf8 = (uint8_t const*) buf;
|
||||
|
||||
for(size_t i=0; i<count; i++) {
|
||||
ITM_SendChar(buf8[i]);
|
||||
}
|
||||
|
||||
return (int) count;
|
||||
}
|
||||
|
||||
TU_ATTR_USED int sys_read (int fhdl, char *buf, size_t count) {
|
||||
(void) fhdl;
|
||||
(void) buf;
|
||||
(void) count;
|
||||
return 0;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
// Default logging with on-board UART
|
||||
TU_ATTR_USED int sys_write (int fhdl, const char *buf, size_t count) {
|
||||
(void) fhdl;
|
||||
return board_uart_write(buf, (int) count);
|
||||
}
|
||||
|
||||
TU_ATTR_USED int sys_read (int fhdl, char *buf, size_t count) {
|
||||
(void) fhdl;
|
||||
int rd = board_uart_read((uint8_t*) buf, (int) count);
|
||||
return (rd > 0) ? rd : -1;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
//TU_ATTR_USED int _close(int fhdl) {
|
||||
// (void) fhdl;
|
||||
// return 0;
|
||||
//}
|
||||
|
||||
//TU_ATTR_USED int _fstat(int file, struct stat *st) {
|
||||
// memset(st, 0, sizeof(*st));
|
||||
// st->st_mode = S_IFCHR;
|
||||
//}
|
||||
|
||||
int board_getchar(void) {
|
||||
char c;
|
||||
return (sys_read(0, &c, 1) > 0) ? (int) c : (-1);
|
||||
}
|
|
@ -0,0 +1,172 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#ifndef _BOARD_API_H_
|
||||
#define _BOARD_API_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "tusb.h"
|
||||
|
||||
// Define the default baudrate
|
||||
#ifndef CFG_BOARD_UART_BAUDRATE
|
||||
#define CFG_BOARD_UART_BAUDRATE 115200 ///< Default baud rate
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Board Porting API
|
||||
// For simplicity, only one LED and one Button are used
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Initialize on-board peripherals : led, button, uart and USB
|
||||
void board_init(void);
|
||||
|
||||
// Init board after tinyusb is initialized
|
||||
void board_init_after_tusb(void) TU_ATTR_WEAK;
|
||||
|
||||
// Turn LED on or off
|
||||
void board_led_write(bool state);
|
||||
|
||||
// Control led pattern using phase duration in ms.
|
||||
// For each phase, LED is toggle then repeated, board_led_task() is required to be called
|
||||
//void board_led_pattern(uint32_t const phase_ms[], uint8_t count);
|
||||
|
||||
// Get the current state of button
|
||||
// a '1' means active (pressed), a '0' means inactive.
|
||||
uint32_t board_button_read(void);
|
||||
|
||||
// Get board unique ID for USB serial number. Return number of bytes. Note max_len is typically 16
|
||||
TU_ATTR_WEAK size_t board_get_unique_id(uint8_t id[], size_t max_len);
|
||||
|
||||
// Get characters from UART. Return number of read bytes
|
||||
int board_uart_read(uint8_t *buf, int len);
|
||||
|
||||
// Send characters to UART. Return number of sent bytes
|
||||
int board_uart_write(void const *buf, int len);
|
||||
|
||||
#if CFG_TUSB_OS == OPT_OS_NONE
|
||||
// Get current milliseconds, must be implemented when no RTOS is used
|
||||
uint32_t board_millis(void);
|
||||
|
||||
#elif CFG_TUSB_OS == OPT_OS_FREERTOS
|
||||
static inline uint32_t board_millis(void) {
|
||||
return ( ( ((uint64_t) xTaskGetTickCount()) * 1000) / configTICK_RATE_HZ );
|
||||
}
|
||||
|
||||
#elif CFG_TUSB_OS == OPT_OS_MYNEWT
|
||||
static inline uint32_t board_millis(void) {
|
||||
return os_time_ticks_to_ms32( os_time_get() );
|
||||
}
|
||||
|
||||
#elif CFG_TUSB_OS == OPT_OS_PICO
|
||||
#include "pico/time.h"
|
||||
static inline uint32_t board_millis(void) {
|
||||
return to_ms_since_boot(get_absolute_time());
|
||||
}
|
||||
|
||||
#elif CFG_TUSB_OS == OPT_OS_RTTHREAD
|
||||
static inline uint32_t board_millis(void) {
|
||||
return (((uint64_t)rt_tick_get()) * 1000 / RT_TICK_PER_SECOND);
|
||||
}
|
||||
|
||||
#elif CFG_TUSB_OS == OPT_OS_CUSTOM
|
||||
// Implement your own board_millis() in any of .c file
|
||||
|
||||
#else
|
||||
#error "board_millis() is not implemented for this OS"
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Helper functions
|
||||
//--------------------------------------------------------------------+
|
||||
static inline void board_led_on(void) {
|
||||
board_led_write(true);
|
||||
}
|
||||
|
||||
static inline void board_led_off(void) {
|
||||
board_led_write(false);
|
||||
}
|
||||
|
||||
// Get USB Serial number string from unique ID if available. Return number of character.
|
||||
// Input is string descriptor from index 1 (index 0 is type + len)
|
||||
static inline size_t board_usb_get_serial(uint16_t desc_str1[], size_t max_chars) {
|
||||
uint8_t uid[16] TU_ATTR_ALIGNED(4);
|
||||
size_t uid_len;
|
||||
|
||||
if ( board_get_unique_id ) {
|
||||
uid_len = board_get_unique_id(uid, sizeof(uid));
|
||||
}else {
|
||||
// fixed serial string is 01234567889ABCDEF
|
||||
uint32_t* uid32 = (uint32_t*) (uintptr_t) uid;
|
||||
uid32[0] = 0x67452301;
|
||||
uid32[1] = 0xEFCDAB89;
|
||||
uid_len = 8;
|
||||
}
|
||||
|
||||
if ( uid_len > max_chars / 2 ) uid_len = max_chars / 2;
|
||||
|
||||
for ( size_t i = 0; i < uid_len; i++ ) {
|
||||
for ( size_t j = 0; j < 2; j++ ) {
|
||||
const char nibble_to_hex[16] = {
|
||||
'0', '1', '2', '3', '4', '5', '6', '7',
|
||||
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
|
||||
};
|
||||
uint8_t const nibble = (uid[i] >> (j * 4)) & 0xf;
|
||||
desc_str1[i * 2 + (1 - j)] = nibble_to_hex[nibble]; // UTF-16-LE
|
||||
}
|
||||
}
|
||||
|
||||
return 2 * uid_len;
|
||||
}
|
||||
|
||||
// TODO remove
|
||||
static inline void board_delay(uint32_t ms) {
|
||||
uint32_t start_ms = board_millis();
|
||||
while ( board_millis() - start_ms < ms ) {
|
||||
// take chance to run usb background
|
||||
#if CFG_TUD_ENABLED
|
||||
tud_task();
|
||||
#endif
|
||||
|
||||
#if CFG_TUH_ENABLED
|
||||
tuh_task();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
// stdio getchar() is blocking, this is non-blocking version
|
||||
int board_getchar(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -0,0 +1,178 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2020, Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef BOARD_MCU_H_
|
||||
#define BOARD_MCU_H_
|
||||
|
||||
#include "tusb_option.h"
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Low Level MCU header include. Example should be
|
||||
// platform independent and mostly doesn't need to include this file.
|
||||
// However there are still certain situation where this file is needed:
|
||||
// - FreeRTOSConfig.h to set up correct clock and NVIC interrupts for ARM Cortex
|
||||
// - SWO logging for Cortex M with ITM_SendChar() / ITM_ReceiveChar()
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Include order follows OPT_MCU_ number
|
||||
#if TU_CHECK_MCU(OPT_MCU_LPC11UXX, OPT_MCU_LPC13XX, OPT_MCU_LPC15XX) || \
|
||||
TU_CHECK_MCU(OPT_MCU_LPC175X_6X, OPT_MCU_LPC177X_8X, OPT_MCU_LPC18XX) || \
|
||||
TU_CHECK_MCU(OPT_MCU_LPC40XX, OPT_MCU_LPC43XX)
|
||||
#include "chip.h"
|
||||
|
||||
#elif TU_CHECK_MCU(OPT_MCU_LPC51UXX, OPT_MCU_LPC54XXX, OPT_MCU_LPC55XX, OPT_MCU_MCXN9)
|
||||
#include "fsl_device_registers.h"
|
||||
|
||||
#elif TU_CHECK_MCU(OPT_MCU_KINETIS_KL, OPT_MCU_KINETIS_K32L)
|
||||
#include "fsl_device_registers.h"
|
||||
|
||||
#elif CFG_TUSB_MCU == OPT_MCU_NRF5X
|
||||
#include "nrf.h"
|
||||
|
||||
#elif CFG_TUSB_MCU == OPT_MCU_SAMD11 || CFG_TUSB_MCU == OPT_MCU_SAMD21 || \
|
||||
CFG_TUSB_MCU == OPT_MCU_SAMD51 || CFG_TUSB_MCU == OPT_MCU_SAME5X || \
|
||||
CFG_TUSB_MCU == OPT_MCU_SAML22 || CFG_TUSB_MCU == OPT_MCU_SAML21
|
||||
#include "sam.h"
|
||||
|
||||
#elif CFG_TUSB_MCU == OPT_MCU_SAMG
|
||||
#undef LITTLE_ENDIAN // hack to suppress "LITTLE_ENDIAN" redefined
|
||||
#include "sam.h"
|
||||
|
||||
#elif CFG_TUSB_MCU == OPT_MCU_STM32F0
|
||||
#include "stm32f0xx.h"
|
||||
|
||||
#elif CFG_TUSB_MCU == OPT_MCU_STM32F1
|
||||
#include "stm32f1xx.h"
|
||||
|
||||
#elif CFG_TUSB_MCU == OPT_MCU_STM32F2
|
||||
#include "stm32f2xx.h"
|
||||
|
||||
#elif CFG_TUSB_MCU == OPT_MCU_STM32F3
|
||||
#include "stm32f3xx.h"
|
||||
|
||||
#elif CFG_TUSB_MCU == OPT_MCU_STM32F4
|
||||
#include "stm32f4xx.h"
|
||||
|
||||
#elif CFG_TUSB_MCU == OPT_MCU_STM32F7
|
||||
#include "stm32f7xx.h"
|
||||
|
||||
#elif CFG_TUSB_MCU == OPT_MCU_STM32G4
|
||||
#include "stm32g4xx.h"
|
||||
|
||||
#elif CFG_TUSB_MCU == OPT_MCU_STM32H5
|
||||
#include "stm32h5xx.h"
|
||||
|
||||
#elif CFG_TUSB_MCU == OPT_MCU_STM32H7
|
||||
#include "stm32h7xx.h"
|
||||
|
||||
#elif CFG_TUSB_MCU == OPT_MCU_STM32L0
|
||||
#include "stm32l0xx.h"
|
||||
|
||||
#elif CFG_TUSB_MCU == OPT_MCU_STM32L1
|
||||
#include "stm32l1xx.h"
|
||||
|
||||
#elif CFG_TUSB_MCU == OPT_MCU_STM32L4
|
||||
#include "stm32l4xx.h"
|
||||
|
||||
#elif CFG_TUSB_MCU == OPT_MCU_STM32WB
|
||||
#include "stm32wbxx.h"
|
||||
|
||||
#elif CFG_TUSB_MCU == OPT_MCU_STM32U5
|
||||
#include "stm32u5xx.h"
|
||||
|
||||
#elif CFG_TUSB_MCU == OPT_MCU_STM32G0
|
||||
#include "stm32g0xx.h"
|
||||
|
||||
#elif CFG_TUSB_MCU == OPT_MCU_CXD56
|
||||
// no header needed
|
||||
|
||||
#elif CFG_TUSB_MCU == OPT_MCU_MSP430x5xx
|
||||
#include "msp430.h"
|
||||
|
||||
#elif CFG_TUSB_MCU == OPT_MCU_MSP432E4
|
||||
#include "msp.h"
|
||||
|
||||
#elif CFG_TUSB_MCU == OPT_MCU_VALENTYUSB_EPTRI
|
||||
// no header needed
|
||||
|
||||
#elif CFG_TUSB_MCU == OPT_MCU_MIMXRT1XXX
|
||||
#include "fsl_device_registers.h"
|
||||
|
||||
#elif CFG_TUSB_MCU == OPT_MCU_NUC120
|
||||
#include "NUC100Series.h"
|
||||
|
||||
#elif CFG_TUSB_MCU == OPT_MCU_NUC121 || CFG_TUSB_MCU == OPT_MCU_NUC126
|
||||
#include "NuMicro.h"
|
||||
|
||||
#elif CFG_TUSB_MCU == OPT_MCU_NUC505
|
||||
#include "NUC505Series.h"
|
||||
|
||||
#elif CFG_TUSB_MCU == OPT_MCU_ESP32S2
|
||||
// no header needed
|
||||
|
||||
#elif CFG_TUSB_MCU == OPT_MCU_ESP32S3
|
||||
// no header needed
|
||||
|
||||
#elif CFG_TUSB_MCU == OPT_MCU_DA1469X
|
||||
#include "DA1469xAB.h"
|
||||
|
||||
#elif CFG_TUSB_MCU == OPT_MCU_RP2040
|
||||
#include "pico.h"
|
||||
|
||||
#elif CFG_TUSB_MCU == OPT_MCU_EFM32GG
|
||||
#include "em_device.h"
|
||||
|
||||
#elif CFG_TUSB_MCU == OPT_MCU_RX63X || CFG_TUSB_MCU == OPT_MCU_RX65X
|
||||
// no header needed
|
||||
|
||||
#elif CFG_TUSB_MCU == OPT_MCU_RAXXX
|
||||
#include "bsp_api.h"
|
||||
|
||||
#elif CFG_TUSB_MCU == OPT_MCU_GD32VF103
|
||||
#include "gd32vf103.h"
|
||||
|
||||
#elif CFG_TUSB_MCU == OPT_MCU_MM32F327X
|
||||
#include "mm32_device.h"
|
||||
|
||||
#elif CFG_TUSB_MCU == OPT_MCU_XMC4000
|
||||
#include "xmc_device.h"
|
||||
|
||||
#elif CFG_TUSB_MCU == OPT_MCU_TM4C123
|
||||
#include "TM4C123.h"
|
||||
|
||||
#elif CFG_TUSB_MCU == OPT_MCU_CH32F20X
|
||||
#include "ch32f20x.h"
|
||||
|
||||
#elif TU_CHECK_MCU(OPT_MCU_BCM2711, OPT_MCU_BCM2835, OPT_MCU_BCM2837)
|
||||
// no header needed
|
||||
|
||||
#else
|
||||
#error "Missing MCU header"
|
||||
#endif
|
||||
|
||||
|
||||
#endif /* BOARD_MCU_H_ */
|
|
@ -0,0 +1,480 @@
|
|||
include_guard(GLOBAL)
|
||||
|
||||
include(CMakePrintHelpers)
|
||||
|
||||
# TOP is path to root directory
|
||||
set(TOP "${CMAKE_CURRENT_LIST_DIR}/../..")
|
||||
get_filename_component(TOP ${TOP} ABSOLUTE)
|
||||
|
||||
# Default to gcc
|
||||
if (NOT DEFINED TOOLCHAIN)
|
||||
set(TOOLCHAIN gcc)
|
||||
endif ()
|
||||
|
||||
# FAMILY not defined, try to detect it from BOARD
|
||||
if (NOT DEFINED FAMILY)
|
||||
if (NOT DEFINED BOARD)
|
||||
message(FATAL_ERROR "You must set a FAMILY variable for the build (e.g. rp2040, espressif).
|
||||
You can do this via -DFAMILY=xxx on the cmake command line")
|
||||
endif ()
|
||||
|
||||
# Find path contains BOARD
|
||||
file(GLOB BOARD_PATH LIST_DIRECTORIES true
|
||||
RELATIVE ${TOP}/hw/bsp
|
||||
${TOP}/hw/bsp/*/boards/${BOARD}
|
||||
)
|
||||
if (NOT BOARD_PATH)
|
||||
message(FATAL_ERROR "Could not detect FAMILY from BOARD=${BOARD}")
|
||||
endif ()
|
||||
|
||||
# replace / with ; so that we can get the first element as FAMILY
|
||||
string(REPLACE "/" ";" BOARD_PATH ${BOARD_PATH})
|
||||
list(GET BOARD_PATH 0 FAMILY)
|
||||
endif ()
|
||||
|
||||
if (NOT EXISTS ${CMAKE_CURRENT_LIST_DIR}/${FAMILY}/family.cmake)
|
||||
message(FATAL_ERROR "Family '${FAMILY}' is not known/supported")
|
||||
endif()
|
||||
|
||||
if (NOT FAMILY STREQUAL rp2040)
|
||||
# enable LTO if supported skip rp2040
|
||||
include(CheckIPOSupported)
|
||||
check_ipo_supported(RESULT IPO_SUPPORTED)
|
||||
cmake_print_variables(IPO_SUPPORTED)
|
||||
if (IPO_SUPPORTED)
|
||||
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
set(WARNING_FLAGS_GNU
|
||||
-Wall
|
||||
-Wextra
|
||||
-Werror
|
||||
-Wfatal-errors
|
||||
-Wdouble-promotion
|
||||
-Wstrict-prototypes
|
||||
-Wstrict-overflow
|
||||
-Werror-implicit-function-declaration
|
||||
-Wfloat-equal
|
||||
-Wundef
|
||||
-Wshadow
|
||||
-Wwrite-strings
|
||||
-Wsign-compare
|
||||
-Wmissing-format-attribute
|
||||
-Wunreachable-code
|
||||
-Wcast-align
|
||||
-Wcast-function-type
|
||||
-Wcast-qual
|
||||
-Wnull-dereference
|
||||
-Wuninitialized
|
||||
-Wunused
|
||||
-Wreturn-type
|
||||
-Wredundant-decls
|
||||
)
|
||||
|
||||
set(WARNING_FLAGS_IAR "")
|
||||
|
||||
|
||||
# Filter example based on only.txt and skip.txt
|
||||
function(family_filter RESULT DIR)
|
||||
get_filename_component(DIR ${DIR} ABSOLUTE BASE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
if (EXISTS "${DIR}/only.txt")
|
||||
file(READ "${DIR}/only.txt" ONLYS)
|
||||
# Replace newlines with semicolon so that it is treated as a list by CMake
|
||||
string(REPLACE "\n" ";" ONLYS_LINES ${ONLYS})
|
||||
|
||||
# For each mcu
|
||||
foreach(MCU IN LISTS FAMILY_MCUS)
|
||||
# For each line in only.txt
|
||||
foreach(_line ${ONLYS_LINES})
|
||||
# If mcu:xxx exists for this mcu or board:xxx then include
|
||||
if (${_line} STREQUAL "mcu:${MCU}" OR ${_line} STREQUAL "board:${BOARD}")
|
||||
set(${RESULT} 1 PARENT_SCOPE)
|
||||
return()
|
||||
endif()
|
||||
endforeach()
|
||||
endforeach()
|
||||
|
||||
# Didn't find it in only file so don't build
|
||||
set(${RESULT} 0 PARENT_SCOPE)
|
||||
|
||||
elseif (EXISTS "${DIR}/skip.txt")
|
||||
file(READ "${DIR}/skip.txt" SKIPS)
|
||||
# Replace newlines with semicolon so that it is treated as a list by CMake
|
||||
string(REPLACE "\n" ";" SKIPS_LINES ${SKIPS})
|
||||
|
||||
# For each mcu
|
||||
foreach(MCU IN LISTS FAMILY_MCUS)
|
||||
# For each line in only.txt
|
||||
foreach(_line ${SKIPS_LINES})
|
||||
# If mcu:xxx exists for this mcu then skip
|
||||
if (${_line} STREQUAL "mcu:${MCU}")
|
||||
set(${RESULT} 0 PARENT_SCOPE)
|
||||
return()
|
||||
endif()
|
||||
endforeach()
|
||||
endforeach()
|
||||
|
||||
# Didn't find in skip file so build
|
||||
set(${RESULT} 1 PARENT_SCOPE)
|
||||
else()
|
||||
|
||||
# Didn't find skip or only file so build
|
||||
set(${RESULT} 1 PARENT_SCOPE)
|
||||
endif()
|
||||
endfunction()
|
||||
|
||||
|
||||
function(family_add_subdirectory DIR)
|
||||
family_filter(SHOULD_ADD "${DIR}")
|
||||
if (SHOULD_ADD)
|
||||
add_subdirectory(${DIR})
|
||||
endif()
|
||||
endfunction()
|
||||
|
||||
|
||||
function(family_get_project_name OUTPUT_NAME DIR)
|
||||
get_filename_component(SHORT_NAME ${DIR} NAME)
|
||||
set(${OUTPUT_NAME} ${TINYUSB_FAMILY_PROJECT_NAME_PREFIX}${SHORT_NAME} PARENT_SCOPE)
|
||||
endfunction()
|
||||
|
||||
|
||||
function(family_initialize_project PROJECT DIR)
|
||||
# set output suffix to .elf (skip espressif and rp2040)
|
||||
if(NOT FAMILY STREQUAL "espressif" AND NOT FAMILY STREQUAL "rp2040")
|
||||
set(CMAKE_EXECUTABLE_SUFFIX .elf PARENT_SCOPE)
|
||||
endif()
|
||||
|
||||
family_filter(ALLOWED "${DIR}")
|
||||
if (NOT ALLOWED)
|
||||
get_filename_component(SHORT_NAME ${DIR} NAME)
|
||||
message(FATAL_ERROR "${SHORT_NAME} is not supported on FAMILY=${FAMILY}")
|
||||
endif()
|
||||
endfunction()
|
||||
|
||||
|
||||
#-------------------------------------------------------------
|
||||
# Common Target Configure
|
||||
# Most families use these settings except rp2040 and espressif
|
||||
#-------------------------------------------------------------
|
||||
|
||||
# Add RTOS to example
|
||||
function(family_add_rtos TARGET RTOS)
|
||||
if (RTOS STREQUAL "freertos")
|
||||
# freertos config
|
||||
if (NOT TARGET freertos_config)
|
||||
add_library(freertos_config INTERFACE)
|
||||
target_include_directories(freertos_config INTERFACE ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/${FAMILY}/FreeRTOSConfig)
|
||||
# add board definition to freertos_config mostly for SystemCoreClock
|
||||
target_link_libraries(freertos_config INTERFACE board_${BOARD})
|
||||
endif()
|
||||
|
||||
# freertos kernel
|
||||
if (NOT TARGET freertos_kernel)
|
||||
add_subdirectory(${TOP}/lib/FreeRTOS-Kernel ${CMAKE_BINARY_DIR}/lib/freertos_kernel)
|
||||
endif ()
|
||||
|
||||
target_link_libraries(${TARGET} PUBLIC freertos_kernel)
|
||||
endif ()
|
||||
endfunction()
|
||||
|
||||
|
||||
# Add common configuration to example
|
||||
function(family_configure_common TARGET RTOS)
|
||||
family_add_rtos(${TARGET} ${RTOS})
|
||||
|
||||
string(TOUPPER ${BOARD} BOARD_UPPER)
|
||||
string(REPLACE "-" "_" BOARD_UPPER ${BOARD_UPPER})
|
||||
target_compile_definitions(${TARGET} PUBLIC
|
||||
BOARD_${BOARD_UPPER}
|
||||
)
|
||||
|
||||
# run size after build
|
||||
find_program(SIZE_EXE ${CMAKE_SIZE})
|
||||
if(NOT ${SIZE_EXE} STREQUAL SIZE_EXE-NOTFOUND)
|
||||
add_custom_command(TARGET ${TARGET} POST_BUILD
|
||||
COMMAND ${SIZE_EXE} $<TARGET_FILE:${TARGET}>
|
||||
)
|
||||
endif ()
|
||||
# Add warnings flags
|
||||
target_compile_options(${TARGET} PUBLIC ${WARNING_FLAGS_${CMAKE_C_COMPILER_ID}})
|
||||
|
||||
# Generate linker map file
|
||||
if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
|
||||
target_link_options(${TARGET} PUBLIC "LINKER:-Map=$<TARGET_FILE:${TARGET}>.map")
|
||||
if (CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 12.0)
|
||||
target_link_options(${TARGET} PUBLIC "LINKER:--no-warn-rwx-segments")
|
||||
endif ()
|
||||
endif()
|
||||
if (CMAKE_C_COMPILER_ID STREQUAL "IAR")
|
||||
target_link_options(${TARGET} PUBLIC "LINKER:--map=$<TARGET_FILE:${TARGET}>.map")
|
||||
endif()
|
||||
|
||||
|
||||
# ETM Trace option
|
||||
if (TRACE_ETM STREQUAL "1")
|
||||
target_compile_definitions(${TARGET} PUBLIC TRACE_ETM)
|
||||
endif ()
|
||||
|
||||
# LOGGER option
|
||||
if (DEFINED LOGGER)
|
||||
target_compile_definitions(${TARGET} PUBLIC LOGGER_${LOGGER})
|
||||
|
||||
# Add segger rtt to example
|
||||
if(LOGGER STREQUAL "RTT" OR LOGGER STREQUAL "rtt")
|
||||
if (NOT TARGET segger_rtt)
|
||||
add_library(segger_rtt STATIC ${TOP}/lib/SEGGER_RTT/RTT/SEGGER_RTT.c)
|
||||
target_include_directories(segger_rtt PUBLIC ${TOP}/lib/SEGGER_RTT/RTT)
|
||||
#target_compile_definitions(segger_rtt PUBLIC SEGGER_RTT_MODE_DEFAULT=SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL)
|
||||
endif()
|
||||
target_link_libraries(${TARGET} PUBLIC segger_rtt)
|
||||
endif ()
|
||||
endif ()
|
||||
endfunction()
|
||||
|
||||
|
||||
# Add tinyusb to example
|
||||
function(family_add_tinyusb TARGET OPT_MCU RTOS)
|
||||
# tinyusb target is built for each example since it depends on example's tusb_config.h
|
||||
set(TINYUSB_TARGET_PREFIX ${TARGET}-)
|
||||
add_library(${TARGET}-tinyusb_config INTERFACE)
|
||||
|
||||
# path to tusb_config.h
|
||||
target_include_directories(${TARGET}-tinyusb_config INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/src)
|
||||
target_compile_definitions(${TARGET}-tinyusb_config INTERFACE CFG_TUSB_MCU=${OPT_MCU})
|
||||
|
||||
if (DEFINED LOG)
|
||||
target_compile_definitions(${TARGET}-tinyusb_config INTERFACE CFG_TUSB_DEBUG=${LOG})
|
||||
if (LOG STREQUAL "4")
|
||||
# no inline for debug level 4
|
||||
target_compile_definitions(${TARGET}-tinyusb_config INTERFACE TU_ATTR_ALWAYS_INLINE=)
|
||||
endif ()
|
||||
endif()
|
||||
|
||||
if (RTOS STREQUAL "freertos")
|
||||
target_compile_definitions(${TARGET}-tinyusb_config INTERFACE CFG_TUSB_OS=OPT_OS_FREERTOS)
|
||||
endif ()
|
||||
|
||||
# tinyusb's CMakeList.txt
|
||||
add_subdirectory(${TOP}/src ${CMAKE_CURRENT_BINARY_DIR}/tinyusb)
|
||||
|
||||
if (RTOS STREQUAL "freertos")
|
||||
# link tinyusb with freeRTOS kernel
|
||||
target_link_libraries(${TARGET}-tinyusb PUBLIC freertos_kernel)
|
||||
endif ()
|
||||
|
||||
# use max3421 as host controller
|
||||
if (MAX3421_HOST STREQUAL "1")
|
||||
target_compile_definitions(${TARGET}-tinyusb_config INTERFACE CFG_TUH_MAX3421=1)
|
||||
target_sources(${TARGET}-tinyusb PUBLIC
|
||||
${TOP}/src/portable/analog/max3421/hcd_max3421.c
|
||||
)
|
||||
endif ()
|
||||
|
||||
endfunction()
|
||||
|
||||
|
||||
# Add bin/hex output
|
||||
function(family_add_bin_hex TARGET)
|
||||
add_custom_command(TARGET ${TARGET} POST_BUILD
|
||||
COMMAND ${CMAKE_OBJCOPY} -Obinary $<TARGET_FILE:${TARGET}> $<TARGET_FILE_DIR:${TARGET}>/${TARGET}.bin
|
||||
COMMAND ${CMAKE_OBJCOPY} -Oihex $<TARGET_FILE:${TARGET}> $<TARGET_FILE_DIR:${TARGET}>/${TARGET}.hex
|
||||
VERBATIM)
|
||||
endfunction()
|
||||
|
||||
|
||||
#----------------------------------
|
||||
# Example Target Configure (Default rule)
|
||||
# These function can be redefined in FAMILY/family.cmake
|
||||
#----------------------------------
|
||||
|
||||
function(family_configure_example TARGET RTOS)
|
||||
# empty function, should be redefined in FAMILY/family.cmake
|
||||
endfunction()
|
||||
|
||||
# Configure device example with RTOS
|
||||
function(family_configure_device_example TARGET RTOS)
|
||||
family_configure_example(${TARGET} ${RTOS})
|
||||
endfunction()
|
||||
|
||||
# Configure host example with RTOS
|
||||
function(family_configure_host_example TARGET RTOS)
|
||||
family_configure_example(${TARGET} ${RTOS})
|
||||
endfunction()
|
||||
|
||||
# Configure host + device example with RTOS
|
||||
function(family_configure_dual_usb_example TARGET RTOS)
|
||||
family_configure_example(${TARGET} ${RTOS})
|
||||
endfunction()
|
||||
|
||||
function(family_example_missing_dependency TARGET DEPENDENCY)
|
||||
message(WARNING "${DEPENDENCY} submodule needed by ${TARGET} not found, please run 'python tools/get_deps.py ${DEPENDENCY}' to fetch it")
|
||||
endfunction()
|
||||
|
||||
#----------------------------------
|
||||
# RPI specific: refactor later
|
||||
#----------------------------------
|
||||
function(family_add_default_example_warnings TARGET)
|
||||
target_compile_options(${TARGET} PUBLIC
|
||||
-Wall
|
||||
-Wextra
|
||||
-Werror
|
||||
-Wfatal-errors
|
||||
-Wdouble-promotion
|
||||
-Wfloat-equal
|
||||
# FIXME commented out because of https://github.com/raspberrypi/pico-sdk/issues/1468
|
||||
#-Wshadow
|
||||
-Wwrite-strings
|
||||
-Wsign-compare
|
||||
-Wmissing-format-attribute
|
||||
-Wunreachable-code
|
||||
-Wcast-align
|
||||
-Wcast-qual
|
||||
-Wnull-dereference
|
||||
-Wuninitialized
|
||||
-Wunused
|
||||
-Wredundant-decls
|
||||
#-Wstrict-prototypes
|
||||
#-Werror-implicit-function-declaration
|
||||
#-Wundef
|
||||
)
|
||||
|
||||
if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
|
||||
if (CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 12.0)
|
||||
target_link_options(${TARGET} PUBLIC "LINKER:--no-warn-rwx-segments")
|
||||
endif()
|
||||
|
||||
# GCC 10
|
||||
if (CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 10.0)
|
||||
target_compile_options(${TARGET} PUBLIC -Wconversion)
|
||||
endif()
|
||||
|
||||
# GCC 8
|
||||
if (CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 8.0)
|
||||
target_compile_options(${TARGET} PUBLIC -Wcast-function-type -Wstrict-overflow)
|
||||
endif()
|
||||
|
||||
# GCC 6
|
||||
if (CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 6.0)
|
||||
target_compile_options(${TARGET} PUBLIC -Wno-strict-aliasing)
|
||||
endif()
|
||||
endif()
|
||||
endfunction()
|
||||
|
||||
#----------------------------------
|
||||
# Flashing target
|
||||
#----------------------------------
|
||||
|
||||
# Add flash jlink target
|
||||
function(family_flash_jlink TARGET)
|
||||
if (NOT DEFINED JLINKEXE)
|
||||
set(JLINKEXE JLinkExe)
|
||||
endif ()
|
||||
|
||||
file(GENERATE
|
||||
OUTPUT $<TARGET_FILE_DIR:${TARGET}>/${TARGET}.jlink
|
||||
CONTENT "halt
|
||||
loadfile $<TARGET_FILE:${TARGET}>
|
||||
r
|
||||
go
|
||||
exit"
|
||||
)
|
||||
|
||||
add_custom_target(${TARGET}-jlink
|
||||
DEPENDS ${TARGET}
|
||||
COMMAND ${JLINKEXE} -device ${JLINK_DEVICE} -if swd -JTAGConf -1,-1 -speed auto -CommandFile $<TARGET_FILE_DIR:${TARGET}>/${TARGET}.jlink
|
||||
)
|
||||
endfunction()
|
||||
|
||||
|
||||
# Add flash stlink target
|
||||
function(family_flash_stlink TARGET)
|
||||
if (NOT DEFINED STM32_PROGRAMMER_CLI)
|
||||
set(STM32_PROGRAMMER_CLI STM32_Programmer_CLI)
|
||||
endif ()
|
||||
|
||||
add_custom_target(${TARGET}-stlink
|
||||
DEPENDS ${TARGET}
|
||||
COMMAND ${STM32_PROGRAMMER_CLI} --connect port=swd --write $<TARGET_FILE:${TARGET}> --go
|
||||
)
|
||||
endfunction()
|
||||
|
||||
|
||||
# Add flash openocd target
|
||||
function(family_flash_openocd TARGET CLI_OPTIONS)
|
||||
if (NOT DEFINED OPENOCD)
|
||||
set(OPENOCD openocd)
|
||||
endif ()
|
||||
|
||||
separate_arguments(CLI_OPTIONS_LIST UNIX_COMMAND ${CLI_OPTIONS})
|
||||
|
||||
# note skip verify since it has issue with rp2040
|
||||
add_custom_target(${TARGET}-openocd
|
||||
DEPENDS ${TARGET}
|
||||
COMMAND ${OPENOCD} ${CLI_OPTIONS_LIST} -c "program $<TARGET_FILE:${TARGET}> reset exit"
|
||||
VERBATIM
|
||||
)
|
||||
endfunction()
|
||||
|
||||
# Add flash pycod target
|
||||
function(family_flash_pyocd TARGET)
|
||||
if (NOT DEFINED PYOC)
|
||||
set(PYOCD pyocd)
|
||||
endif ()
|
||||
|
||||
add_custom_target(${TARGET}-pyocd
|
||||
DEPENDS ${TARGET}
|
||||
COMMAND ${PYOCD} flash -t ${PYOCD_TARGET} $<TARGET_FILE:${TARGET}>
|
||||
)
|
||||
endfunction()
|
||||
|
||||
|
||||
# Add flash using NXP's LinkServer (redserver)
|
||||
# https://www.nxp.com/design/software/development-software/mcuxpresso-software-and-tools-/linkserver-for-microcontrollers:LINKERSERVER
|
||||
function(family_flash_nxplink TARGET)
|
||||
if (NOT DEFINED LINKSERVER)
|
||||
set(LINKSERVER LinkServer)
|
||||
endif ()
|
||||
|
||||
# LinkServer has a bug that can only execute with full path otherwise it throws:
|
||||
# realpath error: No such file or directory
|
||||
execute_process(COMMAND which ${LINKSERVER} OUTPUT_VARIABLE LINKSERVER_PATH OUTPUT_STRIP_TRAILING_WHITESPACE)
|
||||
|
||||
add_custom_target(${TARGET}-nxplink
|
||||
DEPENDS ${TARGET}
|
||||
COMMAND ${LINKSERVER_PATH} flash ${NXPLINK_DEVICE} load $<TARGET_FILE:${TARGET}>
|
||||
)
|
||||
endfunction()
|
||||
|
||||
|
||||
function(family_flash_dfu_util TARGET OPTION)
|
||||
if (NOT DEFINED DFU_UTIL)
|
||||
set(DFU_UTIL dfu-util)
|
||||
endif ()
|
||||
|
||||
add_custom_target(${TARGET}-dfu-util
|
||||
DEPENDS ${TARGET}
|
||||
COMMAND ${DFU_UTIL} -R -d ${DFU_UTIL_VID_PID} -a 0 -D $<TARGET_FILE_DIR:${TARGET}>/${TARGET}.bin
|
||||
VERBATIM
|
||||
)
|
||||
endfunction()
|
||||
|
||||
#----------------------------------
|
||||
# Family specific
|
||||
#----------------------------------
|
||||
|
||||
# family specific: can override above functions
|
||||
include(${CMAKE_CURRENT_LIST_DIR}/${FAMILY}/family.cmake)
|
||||
|
||||
if (NOT FAMILY_MCUS)
|
||||
set(FAMILY_MCUS ${FAMILY})
|
||||
endif()
|
||||
|
||||
# if use max3421 as host controller, expand FAMILY_MCUS to include max3421
|
||||
if (MAX3421_HOST STREQUAL "1")
|
||||
set(FAMILY_MCUS ${FAMILY_MCUS} MAX3421)
|
||||
endif ()
|
||||
|
||||
# save it in case of re-inclusion
|
||||
set(FAMILY_MCUS ${FAMILY_MCUS} CACHE INTERNAL "")
|
|
@ -0,0 +1,92 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2021, Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#ifndef BOARD_H_
|
||||
#define BOARD_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// LED
|
||||
#ifdef PICO_DEFAULT_LED_PIN
|
||||
#define LED_PIN PICO_DEFAULT_LED_PIN
|
||||
#define LED_STATE_ON (!(PICO_DEFAULT_LED_PIN_INVERTED))
|
||||
#endif
|
||||
|
||||
// Button pin is BOOTSEL which is flash CS pin
|
||||
#define BUTTON_BOOTSEL
|
||||
#define BUTTON_STATE_ACTIVE 0
|
||||
|
||||
// UART
|
||||
#if defined(PICO_DEFAULT_UART_TX_PIN) && defined(PICO_DEFAULT_UART_RX_PIN) && \
|
||||
defined(PICO_DEFAULT_UART) && defined(LIB_PICO_STDIO_UART)
|
||||
#define UART_DEV PICO_DEFAULT_UART
|
||||
#define UART_TX_PIN PICO_DEFAULT_UART_TX_PIN
|
||||
#define UART_RX_PIN PICO_DEFAULT_UART_RX_PIN
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// PIO_USB
|
||||
// default to pin on Adafruit Feather rp2040 USB Host or Tester if defined
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// #define USE_ADAFRUIT_FEATHER_RP2040_USBHOST
|
||||
#ifdef USE_ADAFRUIT_FEATHER_RP2040_USBHOST
|
||||
#define PICO_DEFAULT_PIO_USB_DP_PIN 16
|
||||
#define PICO_DEFAULT_PIO_USB_VBUSEN_PIN 18
|
||||
#endif
|
||||
|
||||
#ifndef PICO_DEFAULT_PIO_USB_DP_PIN
|
||||
#define PICO_DEFAULT_PIO_USB_DP_PIN 20
|
||||
#endif
|
||||
|
||||
// VBUS enable pin and its active state
|
||||
#ifndef PICO_DEFAULT_PIO_USB_VBUSEN_PIN
|
||||
#define PICO_DEFAULT_PIO_USB_VBUSEN_PIN 22
|
||||
#endif
|
||||
|
||||
// VBUS enable state
|
||||
#ifndef PICO_DEFAULT_PIO_USB_VBUSEN_STATE
|
||||
#define PICO_DEFAULT_PIO_USB_VBUSEN_STATE 1
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
// USB Host MAX3421E
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
#define MAX3421_SPI PICO_DEFAULT_SPI_INSTANCE
|
||||
#define MAX3421_SCK_PIN PICO_DEFAULT_SPI_SCK_PIN
|
||||
#define MAX3421_MOSI_PIN PICO_DEFAULT_SPI_TX_PIN
|
||||
#define MAX3421_MISO_PIN PICO_DEFAULT_SPI_RX_PIN
|
||||
#define MAX3421_CS_PIN 10
|
||||
#define MAX3421_INTR_PIN 9
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BOARD_H_ */
|
|
@ -0,0 +1 @@
|
|||
# This builds with settings based purely on the current PICO_BOARD set via the SDK
|
|
@ -0,0 +1 @@
|
|||
set(PICO_BOARD pico)
|
|
@ -0,0 +1,314 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
|
||||
* Copyright (c) 2021, Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#include "pico/stdlib.h"
|
||||
#include "pico/binary_info.h"
|
||||
#include "pico/unique_id.h"
|
||||
#include "hardware/gpio.h"
|
||||
#include "hardware/sync.h"
|
||||
#include "hardware/resets.h"
|
||||
#include "hardware/structs/ioqspi.h"
|
||||
#include "hardware/structs/sio.h"
|
||||
|
||||
#include "bsp/board_api.h"
|
||||
#include "board.h"
|
||||
|
||||
#ifdef UART_DEV
|
||||
static uart_inst_t *uart_inst;
|
||||
#endif
|
||||
|
||||
#if CFG_TUH_RPI_PIO_USB || CFG_TUD_RPI_PIO_USB
|
||||
#include "pio_usb.h"
|
||||
#endif
|
||||
|
||||
#if CFG_TUH_ENABLED && CFG_TUH_MAX3421
|
||||
#include "hardware/spi.h"
|
||||
static void max3421_init(void);
|
||||
#endif
|
||||
|
||||
#ifdef BUTTON_BOOTSEL
|
||||
// This example blinks the Picoboard LED when the BOOTSEL button is pressed.
|
||||
//
|
||||
// Picoboard has a button attached to the flash CS pin, which the bootrom
|
||||
// checks, and jumps straight to the USB bootcode if the button is pressed
|
||||
// (pulling flash CS low). We can check this pin in by jumping to some code in
|
||||
// SRAM (so that the XIP interface is not required), floating the flash CS
|
||||
// pin, and observing whether it is pulled low.
|
||||
//
|
||||
// This doesn't work if others are trying to access flash at the same time,
|
||||
// e.g. XIP streamer, or the other core.
|
||||
bool __no_inline_not_in_flash_func(get_bootsel_button)(void) {
|
||||
const uint CS_PIN_INDEX = 1;
|
||||
|
||||
// Must disable interrupts, as interrupt handlers may be in flash, and we
|
||||
// are about to temporarily disable flash access!
|
||||
uint32_t flags = save_and_disable_interrupts();
|
||||
|
||||
// Set chip select to Hi-Z
|
||||
hw_write_masked(&ioqspi_hw->io[CS_PIN_INDEX].ctrl,
|
||||
GPIO_OVERRIDE_LOW << IO_QSPI_GPIO_QSPI_SS_CTRL_OEOVER_LSB,
|
||||
IO_QSPI_GPIO_QSPI_SS_CTRL_OEOVER_BITS);
|
||||
|
||||
// Note we can't call into any sleep functions in flash right now
|
||||
for (volatile int i = 0; i < 1000; ++i);
|
||||
|
||||
// The HI GPIO registers in SIO can observe and control the 6 QSPI pins.
|
||||
// Note the button pulls the pin *low* when pressed.
|
||||
bool button_state = (sio_hw->gpio_hi_in & (1u << CS_PIN_INDEX));
|
||||
|
||||
// Need to restore the state of chip select, else we are going to have a
|
||||
// bad time when we return to code in flash!
|
||||
hw_write_masked(&ioqspi_hw->io[CS_PIN_INDEX].ctrl,
|
||||
GPIO_OVERRIDE_NORMAL << IO_QSPI_GPIO_QSPI_SS_CTRL_OEOVER_LSB,
|
||||
IO_QSPI_GPIO_QSPI_SS_CTRL_OEOVER_BITS);
|
||||
|
||||
restore_interrupts(flags);
|
||||
|
||||
return button_state;
|
||||
}
|
||||
#endif
|
||||
|
||||
//------------- Segger RTT retarget -------------//
|
||||
#if defined(LOGGER_RTT)
|
||||
// Logging with RTT
|
||||
// - If RTT Control Block is not found by 'Auto Detection` try to use 'Search Range` with '0x20000000 0x10000'
|
||||
// - SWD speed is rather slow around 1000Khz
|
||||
#include "pico/stdio/driver.h"
|
||||
#include "SEGGER_RTT.h"
|
||||
|
||||
static void stdio_rtt_write (const char *buf, int length) {
|
||||
SEGGER_RTT_Write(0, buf, (unsigned) length);
|
||||
}
|
||||
|
||||
static int stdio_rtt_read (char *buf, int len) {
|
||||
return (int) SEGGER_RTT_Read(0, buf, (unsigned) len);
|
||||
}
|
||||
|
||||
static stdio_driver_t stdio_rtt = {
|
||||
.out_chars = stdio_rtt_write,
|
||||
.out_flush = NULL,
|
||||
.in_chars = stdio_rtt_read
|
||||
};
|
||||
|
||||
void stdio_rtt_init(void) {
|
||||
stdio_set_driver_enabled(&stdio_rtt, true);
|
||||
}
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
//
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
void board_init(void)
|
||||
{
|
||||
#if CFG_TUH_RPI_PIO_USB || CFG_TUD_RPI_PIO_USB
|
||||
// Set the system clock to a multiple of 120mhz for bitbanging USB with pico-usb
|
||||
set_sys_clock_khz(120000, true);
|
||||
|
||||
#ifdef PICO_DEFAULT_PIO_USB_VBUSEN_PIN
|
||||
gpio_init(PICO_DEFAULT_PIO_USB_VBUSEN_PIN);
|
||||
gpio_set_dir(PICO_DEFAULT_PIO_USB_VBUSEN_PIN, GPIO_OUT);
|
||||
gpio_put(PICO_DEFAULT_PIO_USB_VBUSEN_PIN, PICO_DEFAULT_PIO_USB_VBUSEN_STATE);
|
||||
#endif
|
||||
|
||||
// rp2040 use pico-pio-usb for host tuh_configure() can be used to passed pio configuration to the host stack
|
||||
// Note: tuh_configure() must be called before tuh_init()
|
||||
pio_usb_configuration_t pio_cfg = PIO_USB_DEFAULT_CONFIG;
|
||||
pio_cfg.pin_dp = PICO_DEFAULT_PIO_USB_DP_PIN;
|
||||
tuh_configure(BOARD_TUH_RHPORT, TUH_CFGID_RPI_PIO_USB_CONFIGURATION, &pio_cfg);
|
||||
#endif
|
||||
|
||||
#ifdef LED_PIN
|
||||
bi_decl(bi_1pin_with_name(LED_PIN, "LED"));
|
||||
gpio_init(LED_PIN);
|
||||
gpio_set_dir(LED_PIN, GPIO_OUT);
|
||||
#endif
|
||||
|
||||
// Button
|
||||
#ifndef BUTTON_BOOTSEL
|
||||
#endif
|
||||
|
||||
#ifdef UART_DEV
|
||||
bi_decl(bi_2pins_with_func(UART_TX_PIN, UART_RX_PIN, GPIO_FUNC_UART));
|
||||
uart_inst = uart_get_instance(UART_DEV);
|
||||
stdio_uart_init_full(uart_inst, CFG_BOARD_UART_BAUDRATE, UART_TX_PIN, UART_RX_PIN);
|
||||
#endif
|
||||
|
||||
#if defined(LOGGER_RTT)
|
||||
stdio_rtt_init();
|
||||
#endif
|
||||
|
||||
#if CFG_TUD_ENABLED
|
||||
// TODO probably set up device mode?
|
||||
#endif
|
||||
|
||||
#if CFG_TUH_ENABLED
|
||||
#if CFG_TUH_MAX3421
|
||||
max3421_init();
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if !CFG_TUD_ENABLED && !CFG_TUH_ENABLED
|
||||
// board test exxample, reset usb controller
|
||||
reset_block(RESETS_RESET_USBCTRL_BITS);
|
||||
unreset_block_wait(RESETS_RESET_USBCTRL_BITS);
|
||||
#endif
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Board porting API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
void board_led_write(bool state) {
|
||||
(void) state;
|
||||
|
||||
#ifdef LED_PIN
|
||||
gpio_put(LED_PIN, state ? LED_STATE_ON : (1 - LED_STATE_ON));
|
||||
#endif
|
||||
}
|
||||
|
||||
uint32_t board_button_read(void) {
|
||||
#ifdef BUTTON_BOOTSEL
|
||||
return BUTTON_STATE_ACTIVE == get_bootsel_button();
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
size_t board_get_unique_id(uint8_t id[], size_t max_len) {
|
||||
pico_unique_board_id_t pico_id;
|
||||
pico_get_unique_board_id(&pico_id);
|
||||
|
||||
size_t len = PICO_UNIQUE_BOARD_ID_SIZE_BYTES;
|
||||
if (len > max_len) len = max_len;
|
||||
|
||||
memcpy(id, pico_id.id, len);
|
||||
return len;
|
||||
}
|
||||
|
||||
int board_uart_read(uint8_t *buf, int len) {
|
||||
#ifdef UART_DEV
|
||||
int count = 0;
|
||||
while ( (count < len) && uart_is_readable(uart_inst) ) {
|
||||
buf[count] = uart_getc(uart_inst);
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
#else
|
||||
(void) buf; (void) len;
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
int board_uart_write(void const *buf, int len) {
|
||||
#ifdef UART_DEV
|
||||
char const *bufch = (char const *) buf;
|
||||
for ( int i = 0; i < len; i++ ) {
|
||||
uart_putc(uart_inst, bufch[i]);
|
||||
}
|
||||
return len;
|
||||
#else
|
||||
(void) buf; (void) len;
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
int board_getchar(void) {
|
||||
return getchar_timeout_us(0);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// USB Interrupt Handler
|
||||
// rp2040 implementation will install appropriate handler when initializing
|
||||
// tinyusb. There is no need to forward IRQ from application
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// API: SPI transfer with MAX3421E, must be implemented by application
|
||||
//--------------------------------------------------------------------+
|
||||
#if CFG_TUH_ENABLED && defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421
|
||||
|
||||
void max3421_int_handler(uint gpio, uint32_t event_mask) {
|
||||
if (!(gpio == MAX3421_INTR_PIN && event_mask & GPIO_IRQ_EDGE_FALL)) return;
|
||||
tuh_int_handler(BOARD_TUH_RHPORT, true);
|
||||
}
|
||||
|
||||
static void max3421_init(void) {
|
||||
// CS pin
|
||||
gpio_init(MAX3421_CS_PIN);
|
||||
gpio_set_dir(MAX3421_CS_PIN, GPIO_OUT);
|
||||
gpio_put(MAX3421_CS_PIN, true);
|
||||
|
||||
// Interrupt pin
|
||||
gpio_init(MAX3421_INTR_PIN);
|
||||
gpio_set_dir(MAX3421_INTR_PIN, GPIO_IN);
|
||||
gpio_pull_up(MAX3421_INTR_PIN);
|
||||
gpio_set_irq_enabled_with_callback(MAX3421_INTR_PIN, GPIO_IRQ_EDGE_FALL, true, max3421_int_handler);
|
||||
|
||||
// SPI init
|
||||
spi_init(MAX3421_SPI, 4*1000000ul);
|
||||
gpio_set_function(MAX3421_SCK_PIN, GPIO_FUNC_SPI);
|
||||
gpio_set_function(MAX3421_MOSI_PIN, GPIO_FUNC_SPI);
|
||||
gpio_set_function(MAX3421_MISO_PIN, GPIO_FUNC_SPI);
|
||||
spi_set_format(MAX3421_SPI, 8, SPI_CPOL_0, SPI_CPHA_0, SPI_MSB_FIRST);
|
||||
}
|
||||
|
||||
//// API to enable/disable MAX3421 INTR pin interrupt
|
||||
void tuh_max3421_int_api(uint8_t rhport, bool enabled) {
|
||||
(void) rhport;
|
||||
irq_set_enabled(IO_IRQ_BANK0, enabled);
|
||||
}
|
||||
|
||||
// API to control MAX3421 SPI CS
|
||||
void tuh_max3421_spi_cs_api(uint8_t rhport, bool active) {
|
||||
(void) rhport;
|
||||
gpio_put(MAX3421_CS_PIN, !active);
|
||||
}
|
||||
|
||||
// API to transfer data with MAX3421 SPI
|
||||
// Either tx_buf or rx_buf can be NULL, which means transfer is write or read only
|
||||
bool tuh_max3421_spi_xfer_api(uint8_t rhport, uint8_t const* tx_buf, uint8_t* rx_buf, size_t xfer_bytes) {
|
||||
(void) rhport;
|
||||
|
||||
if (tx_buf == NULL && rx_buf == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int ret;
|
||||
|
||||
if (tx_buf == NULL) {
|
||||
ret = spi_read_blocking(MAX3421_SPI, 0, rx_buf, xfer_bytes);
|
||||
}else if (rx_buf == NULL) {
|
||||
ret = spi_write_blocking(MAX3421_SPI, tx_buf, xfer_bytes);
|
||||
}else {
|
||||
ret = spi_write_read_blocking(spi0, tx_buf, rx_buf, xfer_bytes);
|
||||
}
|
||||
|
||||
return ret == (int) xfer_bytes;
|
||||
}
|
||||
|
||||
#endif
|
|
@ -0,0 +1,371 @@
|
|||
cmake_minimum_required(VERSION 3.13)
|
||||
include_guard(GLOBAL)
|
||||
|
||||
if (NOT BOARD)
|
||||
message("BOARD not specified, defaulting to pico_sdk")
|
||||
set(BOARD pico_sdk)
|
||||
endif()
|
||||
|
||||
# add the SDK in case we are standalone tinyusb example (noop if already present)
|
||||
include(${CMAKE_CURRENT_LIST_DIR}/pico_sdk_import.cmake)
|
||||
|
||||
# include basic family CMake functionality
|
||||
set(FAMILY_MCUS RP2040)
|
||||
set(JLINK_DEVICE rp2040_m0_0)
|
||||
set(OPENOCD_OPTION "-f interface/cmsis-dap.cfg -f target/rp2040.cfg -c \"adapter speed 5000\"")
|
||||
|
||||
include(${CMAKE_CURRENT_LIST_DIR}/boards/${BOARD}/board.cmake)
|
||||
|
||||
if (NOT PICO_TINYUSB_PATH)
|
||||
set(PICO_TINYUSB_PATH ${TOP})
|
||||
endif()
|
||||
|
||||
if (NOT TINYUSB_OPT_OS)
|
||||
set(TINYUSB_OPT_OS OPT_OS_PICO)
|
||||
endif()
|
||||
|
||||
#------------------------------------
|
||||
# Base config for both device and host; wrapped by SDK's tinyusb_common
|
||||
#------------------------------------
|
||||
add_library(tinyusb_common_base INTERFACE)
|
||||
|
||||
target_sources(tinyusb_common_base INTERFACE
|
||||
${TOP}/src/tusb.c
|
||||
${TOP}/src/common/tusb_fifo.c
|
||||
)
|
||||
|
||||
target_include_directories(tinyusb_common_base INTERFACE
|
||||
${TOP}/src
|
||||
)
|
||||
|
||||
if(DEFINED LOG)
|
||||
set(TINYUSB_DEBUG_LEVEL ${LOG})
|
||||
elseif (CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||
message("Compiling TinyUSB with CFG_TUSB_DEBUG=1")
|
||||
set(TINYUSB_DEBUG_LEVEL 1)
|
||||
else ()
|
||||
set(TINYUSB_DEBUG_LEVEL 0)
|
||||
endif()
|
||||
|
||||
target_compile_definitions(tinyusb_common_base INTERFACE
|
||||
CFG_TUSB_MCU=OPT_MCU_RP2040
|
||||
CFG_TUSB_OS=${TINYUSB_OPT_OS}
|
||||
CFG_TUSB_DEBUG=${TINYUSB_DEBUG_LEVEL}
|
||||
)
|
||||
|
||||
target_link_libraries(tinyusb_common_base INTERFACE
|
||||
hardware_structs
|
||||
hardware_irq
|
||||
hardware_resets
|
||||
pico_sync
|
||||
)
|
||||
|
||||
#------------------------------------
|
||||
# Base config for device mode; wrapped by SDK's tinyusb_device
|
||||
#------------------------------------
|
||||
add_library(tinyusb_device_base INTERFACE)
|
||||
target_sources(tinyusb_device_base INTERFACE
|
||||
${TOP}/src/portable/raspberrypi/rp2040/dcd_rp2040.c
|
||||
${TOP}/src/portable/raspberrypi/rp2040/rp2040_usb.c
|
||||
${TOP}/src/device/usbd.c
|
||||
${TOP}/src/device/usbd_control.c
|
||||
${TOP}/src/class/cdc/cdc_device.c
|
||||
${TOP}/src/class/hid/hid_device.c
|
||||
)
|
||||
|
||||
#------------------------------------
|
||||
# Base config for host mode; wrapped by SDK's tinyusb_host
|
||||
#------------------------------------
|
||||
add_library(tinyusb_host_base INTERFACE)
|
||||
target_sources(tinyusb_host_base INTERFACE
|
||||
${TOP}/src/portable/raspberrypi/rp2040/hcd_rp2040.c
|
||||
${TOP}/src/portable/raspberrypi/rp2040/rp2040_usb.c
|
||||
${TOP}/src/host/usbh.c
|
||||
${TOP}/src/host/hub.c
|
||||
${TOP}/src/class/cdc/cdc_host.c
|
||||
${TOP}/src/class/hid/hid_host.c
|
||||
${TOP}/src/class/msc/msc_host.c
|
||||
${TOP}/src/class/vendor/vendor_host.c
|
||||
)
|
||||
|
||||
# Sometimes have to do host specific actions in mostly common functions
|
||||
target_compile_definitions(tinyusb_host_base INTERFACE
|
||||
RP2040_USB_HOST_MODE=1
|
||||
)
|
||||
|
||||
#------------------------------------
|
||||
# Host MAX3421
|
||||
#------------------------------------
|
||||
add_library(tinyusb_host_max3421 INTERFACE)
|
||||
target_sources(tinyusb_host_max3421 INTERFACE
|
||||
${TOP}/src/portable/analog/max3421/hcd_max3421.c
|
||||
)
|
||||
target_compile_definitions(tinyusb_host_max3421 INTERFACE
|
||||
CFG_TUH_MAX3421=1
|
||||
)
|
||||
target_link_libraries(tinyusb_host_max3421 INTERFACE
|
||||
hardware_spi
|
||||
)
|
||||
|
||||
#------------------------------------
|
||||
# BSP & Additions
|
||||
#------------------------------------
|
||||
add_library(tinyusb_bsp INTERFACE)
|
||||
target_sources(tinyusb_bsp INTERFACE
|
||||
${TOP}/hw/bsp/rp2040/family.c
|
||||
)
|
||||
target_include_directories(tinyusb_bsp INTERFACE
|
||||
${TOP}/hw
|
||||
)
|
||||
target_link_libraries(tinyusb_bsp INTERFACE pico_unique_id)
|
||||
|
||||
# tinyusb_additions will hold our extra settings for examples
|
||||
add_library(tinyusb_additions INTERFACE)
|
||||
|
||||
target_compile_definitions(tinyusb_additions INTERFACE
|
||||
PICO_RP2040_USB_DEVICE_ENUMERATION_FIX=1
|
||||
PICO_RP2040_USB_DEVICE_UFRAME_FIX=1
|
||||
)
|
||||
|
||||
if(LOGGER STREQUAL "RTT" OR LOGGER STREQUAL "rtt")
|
||||
target_compile_definitions(tinyusb_additions INTERFACE
|
||||
LOGGER_RTT
|
||||
#SEGGER_RTT_MODE_DEFAULT=SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL
|
||||
)
|
||||
|
||||
target_sources(tinyusb_additions INTERFACE
|
||||
${TOP}/lib/SEGGER_RTT/RTT/SEGGER_RTT.c
|
||||
)
|
||||
|
||||
set_source_files_properties(${TOP}/lib/SEGGER_RTT/RTT/SEGGER_RTT.c
|
||||
PROPERTIES
|
||||
COMPILE_FLAGS "-Wno-cast-qual -Wno-cast-align -Wno-sign-conversion")
|
||||
|
||||
target_include_directories(tinyusb_additions INTERFACE
|
||||
${TOP}/lib/SEGGER_RTT/RTT
|
||||
)
|
||||
endif()
|
||||
|
||||
#------------------------------------
|
||||
# Functions
|
||||
#------------------------------------
|
||||
|
||||
function(family_configure_target TARGET RTOS)
|
||||
if (RTOS STREQUAL noos OR RTOS STREQUAL "")
|
||||
set(RTOS_SUFFIX "")
|
||||
else()
|
||||
set(RTOS_SUFFIX _${RTOS})
|
||||
endif()
|
||||
# export RTOS_SUFFIX to parent scope
|
||||
set(RTOS_SUFFIX ${RTOS_SUFFIX} PARENT_SCOPE)
|
||||
|
||||
pico_add_extra_outputs(${TARGET})
|
||||
pico_enable_stdio_uart(${TARGET} 1)
|
||||
target_link_libraries(${TARGET} PUBLIC pico_stdlib pico_bootsel_via_double_reset tinyusb_board${RTOS_SUFFIX} tinyusb_additions)
|
||||
|
||||
# family_flash_openocd(${TARGET} ${OPENOCD_OPTION})
|
||||
family_flash_jlink(${TARGET})
|
||||
endfunction()
|
||||
|
||||
|
||||
function(rp2040_family_configure_example_warnings TARGET)
|
||||
if (NOT PICO_TINYUSB_NO_EXAMPLE_WARNINGS)
|
||||
family_add_default_example_warnings(${TARGET})
|
||||
endif()
|
||||
if(CMAKE_C_COMPILER_ID STREQUAL "Clang")
|
||||
target_compile_options(${TARGET} PRIVATE -Wno-unreachable-code)
|
||||
endif()
|
||||
suppress_tinyusb_warnings()
|
||||
endfunction()
|
||||
|
||||
|
||||
function(family_configure_device_example TARGET RTOS)
|
||||
family_configure_target(${TARGET} ${RTOS})
|
||||
target_link_libraries(${TARGET} PUBLIC pico_stdlib tinyusb_device${RTOS_SUFFIX})
|
||||
rp2040_family_configure_example_warnings(${TARGET})
|
||||
endfunction()
|
||||
|
||||
|
||||
function(family_add_pico_pio_usb TARGET)
|
||||
target_link_libraries(${TARGET} PUBLIC tinyusb_pico_pio_usb)
|
||||
endfunction()
|
||||
|
||||
|
||||
# since Pico-PIO_USB compiler support may lag, and change from version to version, add a function that pico-sdk/pico-examples
|
||||
# can check (if present) in case the user has updated their TinyUSB
|
||||
function(is_compiler_supported_by_pico_pio_usb OUTVAR)
|
||||
if ((NOT CMAKE_C_COMPILER_ID STREQUAL "GNU"))
|
||||
SET(${OUTVAR} 0 PARENT_SCOPE)
|
||||
else()
|
||||
set(${OUTVAR} 1 PARENT_SCOPE)
|
||||
endif()
|
||||
endfunction()
|
||||
|
||||
function(family_configure_host_example TARGET RTOS)
|
||||
family_configure_target(${TARGET} ${RTOS})
|
||||
target_link_libraries(${TARGET} PUBLIC pico_stdlib tinyusb_host${RTOS_SUFFIX})
|
||||
rp2040_family_configure_example_warnings(${TARGET})
|
||||
|
||||
# For rp2040 enable pico-pio-usb
|
||||
if (TARGET tinyusb_pico_pio_usb)
|
||||
# Pico-PIO-USB does not compile with all pico-sdk supported compilers, so check before enabling it
|
||||
is_compiler_supported_by_pico_pio_usb(PICO_PIO_USB_COMPILER_SUPPORTED)
|
||||
if (PICO_PIO_USB_COMPILER_SUPPORTED)
|
||||
family_add_pico_pio_usb(${PROJECT})
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# for max3421 host
|
||||
if (MAX3421_HOST STREQUAL "1")
|
||||
target_link_libraries(${TARGET} PUBLIC tinyusb_host_max3421)
|
||||
endif()
|
||||
endfunction()
|
||||
|
||||
|
||||
function(family_configure_dual_usb_example TARGET RTOS)
|
||||
family_configure_target(${TARGET} ${RTOS})
|
||||
# require tinyusb_pico_pio_usb
|
||||
target_link_libraries(${TARGET} PUBLIC pico_stdlib tinyusb_device tinyusb_host tinyusb_pico_pio_usb )
|
||||
rp2040_family_configure_example_warnings(${TARGET})
|
||||
endfunction()
|
||||
|
||||
|
||||
function(check_and_add_pico_pio_usb_support)
|
||||
# check for pico_generate_pio_header (as depending on environment we may be called before SDK is
|
||||
# initialized in which case it isn't available yet), and only do the initialization once
|
||||
if (COMMAND pico_generate_pio_header AND NOT TARGET tinyusb_pico_pio_usb)
|
||||
#------------------------------------
|
||||
# PIO USB for both host and device
|
||||
#------------------------------------
|
||||
|
||||
if (NOT DEFINED PICO_PIO_USB_PATH)
|
||||
set(PICO_PIO_USB_PATH "${TOP}/hw/mcu/raspberry_pi/Pico-PIO-USB")
|
||||
endif()
|
||||
|
||||
if (EXISTS ${PICO_PIO_USB_PATH}/src/pio_usb.c)
|
||||
add_library(tinyusb_pico_pio_usb INTERFACE)
|
||||
target_sources(tinyusb_device_base INTERFACE
|
||||
${TOP}/src/portable/raspberrypi/pio_usb/dcd_pio_usb.c
|
||||
)
|
||||
target_sources(tinyusb_host_base INTERFACE
|
||||
${TOP}/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c
|
||||
)
|
||||
|
||||
target_sources(tinyusb_pico_pio_usb INTERFACE
|
||||
${PICO_PIO_USB_PATH}/src/pio_usb.c
|
||||
${PICO_PIO_USB_PATH}/src/pio_usb_host.c
|
||||
${PICO_PIO_USB_PATH}/src/pio_usb_device.c
|
||||
${PICO_PIO_USB_PATH}/src/usb_crc.c
|
||||
)
|
||||
|
||||
target_include_directories(tinyusb_pico_pio_usb INTERFACE
|
||||
${PICO_PIO_USB_PATH}/src
|
||||
)
|
||||
|
||||
target_link_libraries(tinyusb_pico_pio_usb INTERFACE
|
||||
hardware_dma
|
||||
hardware_pio
|
||||
pico_multicore
|
||||
)
|
||||
|
||||
target_compile_definitions(tinyusb_pico_pio_usb INTERFACE
|
||||
PIO_USB_USE_TINYUSB
|
||||
)
|
||||
|
||||
pico_generate_pio_header(tinyusb_pico_pio_usb ${PICO_PIO_USB_PATH}/src/usb_tx.pio)
|
||||
pico_generate_pio_header(tinyusb_pico_pio_usb ${PICO_PIO_USB_PATH}/src/usb_rx.pio)
|
||||
endif()
|
||||
endif()
|
||||
endfunction()
|
||||
|
||||
# Try to add Pico-PIO_USB support now for the case where this file is included directly
|
||||
# after Pico SDK initialization, but without using the family_ functions (as is the case
|
||||
# when included by the SDK itself)
|
||||
check_and_add_pico_pio_usb_support()
|
||||
|
||||
|
||||
function(family_initialize_project PROJECT DIR)
|
||||
# call the original version of this function from family_common.cmake
|
||||
_family_initialize_project(${PROJECT} ${DIR})
|
||||
enable_language(C CXX ASM)
|
||||
pico_sdk_init()
|
||||
|
||||
# now re-check for adding Pico-PIO_USB support now SDK is definitely available
|
||||
check_and_add_pico_pio_usb_support()
|
||||
endfunction()
|
||||
|
||||
|
||||
# This method must be called from the project scope to suppress known warnings in TinyUSB source files
|
||||
function(suppress_tinyusb_warnings)
|
||||
# some of these are pretty silly warnings only occurring in some older GCC versions 9 or prior
|
||||
if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
|
||||
if (CMAKE_C_COMPILER_VERSION VERSION_LESS 10.0)
|
||||
set(CONVERSION_WARNING_FILES
|
||||
${PICO_TINYUSB_PATH}/src/tusb.c
|
||||
${PICO_TINYUSB_PATH}/src/common/tusb_fifo.c
|
||||
${PICO_TINYUSB_PATH}/src/device/usbd.c
|
||||
${PICO_TINYUSB_PATH}/src/device/usbd_control.c
|
||||
${PICO_TINYUSB_PATH}/src/host/usbh.c
|
||||
${PICO_TINYUSB_PATH}/src/class/cdc/cdc_device.c
|
||||
${PICO_TINYUSB_PATH}/src/class/cdc/cdc_host.c
|
||||
${PICO_TINYUSB_PATH}/src/class/hid/hid_device.c
|
||||
${PICO_TINYUSB_PATH}/src/class/hid/hid_host.c
|
||||
${PICO_TINYUSB_PATH}/src/class/audio/audio_device.c
|
||||
${PICO_TINYUSB_PATH}/src/class/dfu/dfu_device.c
|
||||
${PICO_TINYUSB_PATH}/src/class/dfu/dfu_rt_device.c
|
||||
${PICO_TINYUSB_PATH}/src/class/midi/midi_device.c
|
||||
${PICO_TINYUSB_PATH}/src/class/usbtmc/usbtmc_device.c
|
||||
${PICO_TINYUSB_PATH}/src/portable/raspberrypi/rp2040/hcd_rp2040.c
|
||||
)
|
||||
foreach(SOURCE_FILE IN LISTS CONVERSION_WARNING_FILES)
|
||||
set_source_files_properties(
|
||||
${SOURCE_FILE}
|
||||
PROPERTIES
|
||||
COMPILE_FLAGS "-Wno-conversion")
|
||||
endforeach()
|
||||
endif()
|
||||
if (CMAKE_C_COMPILER_ID STREQUAL "GNU" AND CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 11.0)
|
||||
set_source_files_properties(
|
||||
${PICO_TINYUSB_PATH}/lib/fatfs/source/ff.c
|
||||
COMPILE_FLAGS "-Wno-stringop-overflow -Wno-array-bounds")
|
||||
endif()
|
||||
set_source_files_properties(
|
||||
${PICO_TINYUSB_PATH}/lib/fatfs/source/ff.c
|
||||
PROPERTIES
|
||||
COMPILE_FLAGS "-Wno-conversion -Wno-cast-qual")
|
||||
|
||||
set_source_files_properties(
|
||||
${PICO_TINYUSB_PATH}/lib/lwip/src/core/tcp_in.c
|
||||
${PICO_TINYUSB_PATH}/lib/lwip/src/core/tcp_out.c
|
||||
PROPERTIES
|
||||
COMPILE_FLAGS "-Wno-conversion")
|
||||
|
||||
set_source_files_properties(
|
||||
${PICO_TINYUSB_PATH}/lib/networking/dnserver.c
|
||||
${PICO_TINYUSB_PATH}/lib/networking/dhserver.c
|
||||
${PICO_TINYUSB_PATH}/lib/networking/rndis_reports.c
|
||||
PROPERTIES
|
||||
COMPILE_FLAGS "-Wno-conversion -Wno-sign-conversion")
|
||||
|
||||
if (TARGET tinyusb_pico_pio_usb)
|
||||
set_source_files_properties(
|
||||
${PICO_TINYUSB_PATH}/hw/mcu/raspberry_pi/Pico-PIO-USB/src/pio_usb_device.c
|
||||
${PICO_TINYUSB_PATH}/hw/mcu/raspberry_pi/Pico-PIO-USB/src/pio_usb.c
|
||||
${PICO_TINYUSB_PATH}/hw/mcu/raspberry_pi/Pico-PIO-USB/src/pio_usb_host.c
|
||||
${PICO_TINYUSB_PATH}/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c
|
||||
PROPERTIES
|
||||
COMPILE_FLAGS "-Wno-conversion -Wno-cast-qual -Wno-attributes")
|
||||
endif()
|
||||
elseif(CMAKE_C_COMPILER_ID STREQUAL "Clang")
|
||||
set_source_files_properties(
|
||||
${PICO_TINYUSB_PATH}/src/class/cdc/cdc_device.c
|
||||
COMPILE_FLAGS "-Wno-unreachable-code")
|
||||
set_source_files_properties(
|
||||
${PICO_TINYUSB_PATH}/src/class/cdc/cdc_host.c
|
||||
COMPILE_FLAGS "-Wno-unreachable-code-fallthrough")
|
||||
set_source_files_properties(
|
||||
${PICO_TINYUSB_PATH}/lib/fatfs/source/ff.c
|
||||
PROPERTIES
|
||||
COMPILE_FLAGS "-Wno-cast-qual")
|
||||
endif()
|
||||
endfunction()
|
|
@ -0,0 +1,18 @@
|
|||
JLINK_DEVICE = rp2040_m0_0
|
||||
PYOCD_TARGET = rp2040
|
||||
|
||||
DEPS_SUBMODULES += hw/mcu/raspberry_pi/Pico-PIO-USB
|
||||
|
||||
ifeq ($(DEBUG), 1)
|
||||
CMAKE_DEFSYM += -DCMAKE_BUILD_TYPE=Debug
|
||||
endif
|
||||
|
||||
$(BUILD):
|
||||
cmake -S . -B $(BUILD) -DFAMILY=$(FAMILY) -DBOARD=$(BOARD) -DPICO_BUILD_DOCS=0 $(CMAKE_DEFSYM)
|
||||
|
||||
all: $(BUILD)
|
||||
$(MAKE) -C $(BUILD)
|
||||
|
||||
flash: flash-pyocd
|
||||
flash-uf2:
|
||||
@$(CP) $(BUILD)/$(PROJECT).uf2 /media/$(USER)/RPI-RP2
|
|
@ -0,0 +1,62 @@
|
|||
# This is a copy of <PICO_SDK_PATH>/external/pico_sdk_import.cmake
|
||||
|
||||
# This can be dropped into an external project to help locate this SDK
|
||||
# It should be include()ed prior to project()
|
||||
|
||||
if (DEFINED ENV{PICO_SDK_PATH} AND (NOT PICO_SDK_PATH))
|
||||
set(PICO_SDK_PATH $ENV{PICO_SDK_PATH})
|
||||
message("Using PICO_SDK_PATH from environment ('${PICO_SDK_PATH}')")
|
||||
endif ()
|
||||
|
||||
if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT} AND (NOT PICO_SDK_FETCH_FROM_GIT))
|
||||
set(PICO_SDK_FETCH_FROM_GIT $ENV{PICO_SDK_FETCH_FROM_GIT})
|
||||
message("Using PICO_SDK_FETCH_FROM_GIT from environment ('${PICO_SDK_FETCH_FROM_GIT}')")
|
||||
endif ()
|
||||
|
||||
if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_PATH} AND (NOT PICO_SDK_FETCH_FROM_GIT_PATH))
|
||||
set(PICO_SDK_FETCH_FROM_GIT_PATH $ENV{PICO_SDK_FETCH_FROM_GIT_PATH})
|
||||
message("Using PICO_SDK_FETCH_FROM_GIT_PATH from environment ('${PICO_SDK_FETCH_FROM_GIT_PATH}')")
|
||||
endif ()
|
||||
|
||||
set(PICO_SDK_PATH "${PICO_SDK_PATH}" CACHE PATH "Path to the Raspberry Pi Pico SDK")
|
||||
set(PICO_SDK_FETCH_FROM_GIT "${PICO_SDK_FETCH_FROM_GIT}" CACHE BOOL "Set to ON to fetch copy of SDK from git if not otherwise locatable")
|
||||
set(PICO_SDK_FETCH_FROM_GIT_PATH "${PICO_SDK_FETCH_FROM_GIT_PATH}" CACHE FILEPATH "location to download SDK")
|
||||
|
||||
if (NOT PICO_SDK_PATH)
|
||||
if (PICO_SDK_FETCH_FROM_GIT)
|
||||
include(FetchContent)
|
||||
set(FETCHCONTENT_BASE_DIR_SAVE ${FETCHCONTENT_BASE_DIR})
|
||||
if (PICO_SDK_FETCH_FROM_GIT_PATH)
|
||||
get_filename_component(FETCHCONTENT_BASE_DIR "${PICO_SDK_FETCH_FROM_GIT_PATH}" REALPATH BASE_DIR "${CMAKE_SOURCE_DIR}")
|
||||
endif ()
|
||||
FetchContent_Declare(
|
||||
pico_sdk
|
||||
GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
|
||||
GIT_TAG master
|
||||
)
|
||||
if (NOT pico_sdk)
|
||||
message("Downloading Raspberry Pi Pico SDK")
|
||||
FetchContent_Populate(pico_sdk)
|
||||
set(PICO_SDK_PATH ${pico_sdk_SOURCE_DIR})
|
||||
endif ()
|
||||
set(FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR_SAVE})
|
||||
else ()
|
||||
message(FATAL_ERROR
|
||||
"SDK location was not specified. Please set PICO_SDK_PATH or set PICO_SDK_FETCH_FROM_GIT to on to fetch from git."
|
||||
)
|
||||
endif ()
|
||||
endif ()
|
||||
|
||||
get_filename_component(PICO_SDK_PATH "${PICO_SDK_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}")
|
||||
if (NOT EXISTS ${PICO_SDK_PATH})
|
||||
message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' not found")
|
||||
endif ()
|
||||
|
||||
set(PICO_SDK_INIT_CMAKE_FILE ${PICO_SDK_PATH}/pico_sdk_init.cmake)
|
||||
if (NOT EXISTS ${PICO_SDK_INIT_CMAKE_FILE})
|
||||
message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' does not appear to contain the Raspberry Pi Pico SDK")
|
||||
endif ()
|
||||
|
||||
set(PICO_SDK_PATH ${PICO_SDK_PATH} CACHE PATH "Path to the Raspberry Pi Pico SDK" FORCE)
|
||||
|
||||
include(${PICO_SDK_INIT_CMAKE_FILE})
|
|
@ -0,0 +1,3 @@
|
|||
source [find interface/cmsis-dap.cfg]
|
||||
adapter speed 5000
|
||||
source [find target/rp2040.cfg]
|
|
@ -0,0 +1,95 @@
|
|||
# TODO more docs and example on how to use this file
|
||||
# Usage: requires target tinyusb_config which expose tusb_config.h file
|
||||
# TINYUSB_TARGET_PREFIX and TINYUSB_TARGET_SUFFIX can be used to change the name of the target
|
||||
|
||||
cmake_minimum_required(VERSION 3.17)
|
||||
|
||||
# Add tinyusb to a target, if user don't want to compile tinyusb as a library
|
||||
function(add_tinyusb TARGET)
|
||||
target_sources(${TARGET} PRIVATE
|
||||
# common
|
||||
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/tusb.c
|
||||
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/common/tusb_fifo.c
|
||||
# device
|
||||
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/device/usbd.c
|
||||
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/device/usbd_control.c
|
||||
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/class/cdc/cdc_device.c
|
||||
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/class/hid/hid_device.c
|
||||
# host
|
||||
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/host/usbh.c
|
||||
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/host/hub.c
|
||||
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/class/cdc/cdc_host.c
|
||||
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/class/hid/hid_host.c
|
||||
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/class/vendor/vendor_host.c
|
||||
# typec
|
||||
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/typec/usbc.c
|
||||
)
|
||||
target_include_directories(${TARGET} PUBLIC
|
||||
${CMAKE_CURRENT_FUNCTION_LIST_DIR}
|
||||
# TODO for net driver, should be removed/changed
|
||||
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/../lib/networking
|
||||
)
|
||||
|
||||
if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
|
||||
target_compile_options(${TARGET} PRIVATE
|
||||
-Wall
|
||||
-Wextra
|
||||
-Werror
|
||||
-Wfatal-errors
|
||||
-Wdouble-promotion
|
||||
-Wstrict-prototypes
|
||||
-Wstrict-overflow
|
||||
-Werror-implicit-function-declaration
|
||||
-Wfloat-equal
|
||||
-Wundef
|
||||
-Wshadow
|
||||
-Wwrite-strings
|
||||
-Wsign-compare
|
||||
-Wmissing-format-attribute
|
||||
-Wunreachable-code
|
||||
-Wcast-align
|
||||
-Wcast-function-type
|
||||
-Wcast-qual
|
||||
-Wnull-dereference
|
||||
-Wuninitialized
|
||||
-Wunused
|
||||
-Wunused-function
|
||||
-Wreturn-type
|
||||
-Wredundant-decls
|
||||
)
|
||||
elseif (CMAKE_C_COMPILER_ID STREQUAL "IAR")
|
||||
|
||||
endif ()
|
||||
endfunction()
|
||||
|
||||
#------------------------------------
|
||||
# TinyUSB as library target
|
||||
#------------------------------------
|
||||
if (NOT DEFINED TINYUSB_TARGET)
|
||||
set(TINYUSB_TARGET "tinyusb")
|
||||
endif ()
|
||||
|
||||
set(TINYUSB_CONFIG_TARGET "${TINYUSB_TARGET}_config")
|
||||
|
||||
if (DEFINED TINYUSB_TARGET_PREFIX)
|
||||
set(TINYUSB_TARGET "${TINYUSB_TARGET_PREFIX}${TINYUSB_TARGET}")
|
||||
set(TINYUSB_CONFIG_TARGET "${TINYUSB_TARGET_PREFIX}${TINYUSB_CONFIG_TARGET}")
|
||||
endif ()
|
||||
|
||||
if (DEFINED TINYUSB_TARGET_SUFFIX)
|
||||
set(TINYUSB_TARGET "${TINYUSB_TARGET}${TINYUSB_TARGET_SUFFIX}")
|
||||
set(TINYUSB_CONFIG_TARGET "${TINYUSB_CONFIG_TARGET}${TINYUSB_TARGET_SUFFIX}")
|
||||
endif ()
|
||||
|
||||
add_library(${TINYUSB_TARGET} STATIC)
|
||||
add_tinyusb(${TINYUSB_TARGET})
|
||||
|
||||
# Check if tinyusb_config target is defined
|
||||
if (NOT TARGET ${TINYUSB_CONFIG_TARGET})
|
||||
message(FATAL_ERROR "${TINYUSB_CONFIG_TARGET} target is not defined")
|
||||
endif()
|
||||
|
||||
# Link with tinyusb_config target
|
||||
target_link_libraries(${TINYUSB_TARGET} PUBLIC
|
||||
${TINYUSB_CONFIG_TARGET}
|
||||
)
|
|
@ -0,0 +1,426 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
/** \ingroup group_class
|
||||
* \defgroup ClassDriver_CDC Communication Device Class (CDC)
|
||||
* Currently only Abstract Control Model subclass is supported
|
||||
* @{ */
|
||||
|
||||
#ifndef _TUSB_CDC_H__
|
||||
#define _TUSB_CDC_H__
|
||||
|
||||
#include "common/tusb_common.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/** \defgroup ClassDriver_CDC_Common Common Definitions
|
||||
* @{ */
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// CDC Communication Interface Class
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
/// Communication Interface Subclass Codes
|
||||
typedef enum
|
||||
{
|
||||
CDC_COMM_SUBCLASS_DIRECT_LINE_CONTROL_MODEL = 0x01 , ///< Direct Line Control Model [USBPSTN1.2]
|
||||
CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL = 0x02 , ///< Abstract Control Model [USBPSTN1.2]
|
||||
CDC_COMM_SUBCLASS_TELEPHONE_CONTROL_MODEL = 0x03 , ///< Telephone Control Model [USBPSTN1.2]
|
||||
CDC_COMM_SUBCLASS_MULTICHANNEL_CONTROL_MODEL = 0x04 , ///< Multi-Channel Control Model [USBISDN1.2]
|
||||
CDC_COMM_SUBCLASS_CAPI_CONTROL_MODEL = 0x05 , ///< CAPI Control Model [USBISDN1.2]
|
||||
CDC_COMM_SUBCLASS_ETHERNET_CONTROL_MODEL = 0x06 , ///< Ethernet Networking Control Model [USBECM1.2]
|
||||
CDC_COMM_SUBCLASS_ATM_NETWORKING_CONTROL_MODEL = 0x07 , ///< ATM Networking Control Model [USBATM1.2]
|
||||
CDC_COMM_SUBCLASS_WIRELESS_HANDSET_CONTROL_MODEL = 0x08 , ///< Wireless Handset Control Model [USBWMC1.1]
|
||||
CDC_COMM_SUBCLASS_DEVICE_MANAGEMENT = 0x09 , ///< Device Management [USBWMC1.1]
|
||||
CDC_COMM_SUBCLASS_MOBILE_DIRECT_LINE_MODEL = 0x0A , ///< Mobile Direct Line Model [USBWMC1.1]
|
||||
CDC_COMM_SUBCLASS_OBEX = 0x0B , ///< OBEX [USBWMC1.1]
|
||||
CDC_COMM_SUBCLASS_ETHERNET_EMULATION_MODEL = 0x0C , ///< Ethernet Emulation Model [USBEEM1.0]
|
||||
CDC_COMM_SUBCLASS_NETWORK_CONTROL_MODEL = 0x0D ///< Network Control Model [USBNCM1.0]
|
||||
} cdc_comm_sublcass_type_t;
|
||||
|
||||
/// Communication Interface Protocol Codes
|
||||
typedef enum
|
||||
{
|
||||
CDC_COMM_PROTOCOL_NONE = 0x00 , ///< No specific protocol
|
||||
CDC_COMM_PROTOCOL_ATCOMMAND = 0x01 , ///< AT Commands: V.250 etc
|
||||
CDC_COMM_PROTOCOL_ATCOMMAND_PCCA_101 = 0x02 , ///< AT Commands defined by PCCA-101
|
||||
CDC_COMM_PROTOCOL_ATCOMMAND_PCCA_101_AND_ANNEXO = 0x03 , ///< AT Commands defined by PCCA-101 & Annex O
|
||||
CDC_COMM_PROTOCOL_ATCOMMAND_GSM_707 = 0x04 , ///< AT Commands defined by GSM 07.07
|
||||
CDC_COMM_PROTOCOL_ATCOMMAND_3GPP_27007 = 0x05 , ///< AT Commands defined by 3GPP 27.007
|
||||
CDC_COMM_PROTOCOL_ATCOMMAND_CDMA = 0x06 , ///< AT Commands defined by TIA for CDMA
|
||||
CDC_COMM_PROTOCOL_ETHERNET_EMULATION_MODEL = 0x07 ///< Ethernet Emulation Model
|
||||
} cdc_comm_protocol_type_t;
|
||||
|
||||
//------------- SubType Descriptor in COMM Functional Descriptor -------------//
|
||||
/// Communication Interface SubType Descriptor
|
||||
typedef enum
|
||||
{
|
||||
CDC_FUNC_DESC_HEADER = 0x00 , ///< Header Functional Descriptor, which marks the beginning of the concatenated set of functional descriptors for the interface.
|
||||
CDC_FUNC_DESC_CALL_MANAGEMENT = 0x01 , ///< Call Management Functional Descriptor.
|
||||
CDC_FUNC_DESC_ABSTRACT_CONTROL_MANAGEMENT = 0x02 , ///< Abstract Control Management Functional Descriptor.
|
||||
CDC_FUNC_DESC_DIRECT_LINE_MANAGEMENT = 0x03 , ///< Direct Line Management Functional Descriptor.
|
||||
CDC_FUNC_DESC_TELEPHONE_RINGER = 0x04 , ///< Telephone Ringer Functional Descriptor.
|
||||
CDC_FUNC_DESC_TELEPHONE_CALL_AND_LINE_STATE_REPORTING_CAPACITY = 0x05 , ///< Telephone Call and Line State Reporting Capabilities Functional Descriptor.
|
||||
CDC_FUNC_DESC_UNION = 0x06 , ///< Union Functional Descriptor
|
||||
CDC_FUNC_DESC_COUNTRY_SELECTION = 0x07 , ///< Country Selection Functional Descriptor
|
||||
CDC_FUNC_DESC_TELEPHONE_OPERATIONAL_MODES = 0x08 , ///< Telephone Operational ModesFunctional Descriptor
|
||||
CDC_FUNC_DESC_USB_TERMINAL = 0x09 , ///< USB Terminal Functional Descriptor
|
||||
CDC_FUNC_DESC_NETWORK_CHANNEL_TERMINAL = 0x0A , ///< Network Channel Terminal Descriptor
|
||||
CDC_FUNC_DESC_PROTOCOL_UNIT = 0x0B , ///< Protocol Unit Functional Descriptor
|
||||
CDC_FUNC_DESC_EXTENSION_UNIT = 0x0C , ///< Extension Unit Functional Descriptor
|
||||
CDC_FUNC_DESC_MULTICHANEL_MANAGEMENT = 0x0D , ///< Multi-Channel Management Functional Descriptor
|
||||
CDC_FUNC_DESC_CAPI_CONTROL_MANAGEMENT = 0x0E , ///< CAPI Control Management Functional Descriptor
|
||||
CDC_FUNC_DESC_ETHERNET_NETWORKING = 0x0F , ///< Ethernet Networking Functional Descriptor
|
||||
CDC_FUNC_DESC_ATM_NETWORKING = 0x10 , ///< ATM Networking Functional Descriptor
|
||||
CDC_FUNC_DESC_WIRELESS_HANDSET_CONTROL_MODEL = 0x11 , ///< Wireless Handset Control Model Functional Descriptor
|
||||
CDC_FUNC_DESC_MOBILE_DIRECT_LINE_MODEL = 0x12 , ///< Mobile Direct Line Model Functional Descriptor
|
||||
CDC_FUNC_DESC_MOBILE_DIRECT_LINE_MODEL_DETAIL = 0x13 , ///< MDLM Detail Functional Descriptor
|
||||
CDC_FUNC_DESC_DEVICE_MANAGEMENT_MODEL = 0x14 , ///< Device Management Model Functional Descriptor
|
||||
CDC_FUNC_DESC_OBEX = 0x15 , ///< OBEX Functional Descriptor
|
||||
CDC_FUNC_DESC_COMMAND_SET = 0x16 , ///< Command Set Functional Descriptor
|
||||
CDC_FUNC_DESC_COMMAND_SET_DETAIL = 0x17 , ///< Command Set Detail Functional Descriptor
|
||||
CDC_FUNC_DESC_TELEPHONE_CONTROL_MODEL = 0x18 , ///< Telephone Control Model Functional Descriptor
|
||||
CDC_FUNC_DESC_OBEX_SERVICE_IDENTIFIER = 0x19 , ///< OBEX Service Identifier Functional Descriptor
|
||||
CDC_FUNC_DESC_NCM = 0x1A , ///< NCM Functional Descriptor
|
||||
}cdc_func_desc_type_t;
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// CDC Data Interface Class
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// SUBCLASS code of Data Interface is not used and should/must be zero
|
||||
|
||||
// Data Interface Protocol Codes
|
||||
typedef enum{
|
||||
CDC_DATA_PROTOCOL_ISDN_BRI = 0x30, ///< Physical interface protocol for ISDN BRI
|
||||
CDC_DATA_PROTOCOL_HDLC = 0x31, ///< HDLC
|
||||
CDC_DATA_PROTOCOL_TRANSPARENT = 0x32, ///< Transparent
|
||||
CDC_DATA_PROTOCOL_Q921_MANAGEMENT = 0x50, ///< Management protocol for Q.921 data link protocol
|
||||
CDC_DATA_PROTOCOL_Q921_DATA_LINK = 0x51, ///< Data link protocol for Q.931
|
||||
CDC_DATA_PROTOCOL_Q921_TEI_MULTIPLEXOR = 0x52, ///< TEI-multiplexor for Q.921 data link protocol
|
||||
CDC_DATA_PROTOCOL_V42BIS_DATA_COMPRESSION = 0x90, ///< Data compression procedures
|
||||
CDC_DATA_PROTOCOL_EURO_ISDN = 0x91, ///< Euro-ISDN protocol control
|
||||
CDC_DATA_PROTOCOL_V24_RATE_ADAPTION_TO_ISDN = 0x92, ///< V.24 rate adaptation to ISDN
|
||||
CDC_DATA_PROTOCOL_CAPI_COMMAND = 0x93, ///< CAPI Commands
|
||||
CDC_DATA_PROTOCOL_HOST_BASED_DRIVER = 0xFD, ///< Host based driver. Note: This protocol code should only be used in messages between host and device to identify the host driver portion of a protocol stack.
|
||||
CDC_DATA_PROTOCOL_IN_PROTOCOL_UNIT_FUNCTIONAL_DESCRIPTOR = 0xFE ///< The protocol(s) are described using a ProtocolUnit Functional Descriptors on Communications Class Interface
|
||||
}cdc_data_protocol_type_t;
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Management Element Request (Control Endpoint)
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
/// Communication Interface Management Element Request Codes
|
||||
typedef enum
|
||||
{
|
||||
CDC_REQUEST_SEND_ENCAPSULATED_COMMAND = 0x00, ///< is used to issue a command in the format of the supported control protocol of the Communications Class interface
|
||||
CDC_REQUEST_GET_ENCAPSULATED_RESPONSE = 0x01, ///< is used to request a response in the format of the supported control protocol of the Communications Class interface.
|
||||
CDC_REQUEST_SET_COMM_FEATURE = 0x02,
|
||||
CDC_REQUEST_GET_COMM_FEATURE = 0x03,
|
||||
CDC_REQUEST_CLEAR_COMM_FEATURE = 0x04,
|
||||
|
||||
CDC_REQUEST_SET_AUX_LINE_STATE = 0x10,
|
||||
CDC_REQUEST_SET_HOOK_STATE = 0x11,
|
||||
CDC_REQUEST_PULSE_SETUP = 0x12,
|
||||
CDC_REQUEST_SEND_PULSE = 0x13,
|
||||
CDC_REQUEST_SET_PULSE_TIME = 0x14,
|
||||
CDC_REQUEST_RING_AUX_JACK = 0x15,
|
||||
|
||||
CDC_REQUEST_SET_LINE_CODING = 0x20,
|
||||
CDC_REQUEST_GET_LINE_CODING = 0x21,
|
||||
CDC_REQUEST_SET_CONTROL_LINE_STATE = 0x22,
|
||||
CDC_REQUEST_SEND_BREAK = 0x23,
|
||||
|
||||
CDC_REQUEST_SET_RINGER_PARMS = 0x30,
|
||||
CDC_REQUEST_GET_RINGER_PARMS = 0x31,
|
||||
CDC_REQUEST_SET_OPERATION_PARMS = 0x32,
|
||||
CDC_REQUEST_GET_OPERATION_PARMS = 0x33,
|
||||
CDC_REQUEST_SET_LINE_PARMS = 0x34,
|
||||
CDC_REQUEST_GET_LINE_PARMS = 0x35,
|
||||
CDC_REQUEST_DIAL_DIGITS = 0x36,
|
||||
CDC_REQUEST_SET_UNIT_PARAMETER = 0x37,
|
||||
CDC_REQUEST_GET_UNIT_PARAMETER = 0x38,
|
||||
CDC_REQUEST_CLEAR_UNIT_PARAMETER = 0x39,
|
||||
CDC_REQUEST_GET_PROFILE = 0x3A,
|
||||
|
||||
CDC_REQUEST_SET_ETHERNET_MULTICAST_FILTERS = 0x40,
|
||||
CDC_REQUEST_SET_ETHERNET_POWER_MANAGEMENT_PATTERN_FILTER = 0x41,
|
||||
CDC_REQUEST_GET_ETHERNET_POWER_MANAGEMENT_PATTERN_FILTER = 0x42,
|
||||
CDC_REQUEST_SET_ETHERNET_PACKET_FILTER = 0x43,
|
||||
CDC_REQUEST_GET_ETHERNET_STATISTIC = 0x44,
|
||||
|
||||
CDC_REQUEST_SET_ATM_DATA_FORMAT = 0x50,
|
||||
CDC_REQUEST_GET_ATM_DEVICE_STATISTICS = 0x51,
|
||||
CDC_REQUEST_SET_ATM_DEFAULT_VC = 0x52,
|
||||
CDC_REQUEST_GET_ATM_VC_STATISTICS = 0x53,
|
||||
|
||||
CDC_REQUEST_MDLM_SEMANTIC_MODEL = 0x60,
|
||||
}cdc_management_request_t;
|
||||
|
||||
enum {
|
||||
CDC_CONTROL_LINE_STATE_DTR = 0x01,
|
||||
CDC_CONTROL_LINE_STATE_RTS = 0x02,
|
||||
};
|
||||
|
||||
enum {
|
||||
CDC_LINE_CODING_STOP_BITS_1 = 0, // 1 bit
|
||||
CDC_LINE_CODING_STOP_BITS_1_5 = 1, // 1.5 bits
|
||||
CDC_LINE_CODING_STOP_BITS_2 = 2, // 2 bits
|
||||
};
|
||||
|
||||
// TODO Backward compatible for typos. Maybe removed in the future release
|
||||
#define CDC_LINE_CONDING_STOP_BITS_1 CDC_LINE_CODING_STOP_BITS_1
|
||||
#define CDC_LINE_CONDING_STOP_BITS_1_5 CDC_LINE_CODING_STOP_BITS_1_5
|
||||
#define CDC_LINE_CONDING_STOP_BITS_2 CDC_LINE_CODING_STOP_BITS_2
|
||||
|
||||
enum {
|
||||
CDC_LINE_CODING_PARITY_NONE = 0,
|
||||
CDC_LINE_CODING_PARITY_ODD = 1,
|
||||
CDC_LINE_CODING_PARITY_EVEN = 2,
|
||||
CDC_LINE_CODING_PARITY_MARK = 3,
|
||||
CDC_LINE_CODING_PARITY_SPACE = 4,
|
||||
};
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Management Element Notification (Notification Endpoint)
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
/// 6.3 Notification Codes
|
||||
typedef enum
|
||||
{
|
||||
CDC_NOTIF_NETWORK_CONNECTION = 0x00, ///< This notification allows the device to notify the host about network connection status.
|
||||
CDC_NOTIF_RESPONSE_AVAILABLE = 0x01, ///< This notification allows the device to notify the hostthat a response is available. This response can be retrieved with a subsequent \ref CDC_REQUEST_GET_ENCAPSULATED_RESPONSE request.
|
||||
CDC_NOTIF_AUX_JACK_HOOK_STATE = 0x08,
|
||||
CDC_NOTIF_RING_DETECT = 0x09,
|
||||
CDC_NOTIF_SERIAL_STATE = 0x20,
|
||||
CDC_NOTIF_CALL_STATE_CHANGE = 0x28,
|
||||
CDC_NOTIF_LINE_STATE_CHANGE = 0x29,
|
||||
CDC_NOTIF_CONNECTION_SPEED_CHANGE = 0x2A, ///< This notification allows the device to inform the host-networking driver that a change in either the upstream or the downstream bit rate of the connection has occurred
|
||||
CDC_NOTIF_MDLM_SEMANTIC_MODEL_NOTIFICATION = 0x40,
|
||||
}cdc_notification_request_t;
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Class Specific Functional Descriptor (Communication Interface)
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Start of all packed definitions for compiler without per-type packed
|
||||
TU_ATTR_PACKED_BEGIN
|
||||
TU_ATTR_BIT_FIELD_ORDER_BEGIN
|
||||
|
||||
/// Header Functional Descriptor (Communication Interface)
|
||||
typedef struct TU_ATTR_PACKED
|
||||
{
|
||||
uint8_t bLength ; ///< Size of this descriptor in bytes.
|
||||
uint8_t bDescriptorType ; ///< Descriptor Type, must be Class-Specific
|
||||
uint8_t bDescriptorSubType ; ///< Descriptor SubType one of above CDC_FUNC_DESC_
|
||||
uint16_t bcdCDC ; ///< CDC release number in Binary-Coded Decimal
|
||||
}cdc_desc_func_header_t;
|
||||
|
||||
/// Union Functional Descriptor (Communication Interface)
|
||||
typedef struct TU_ATTR_PACKED
|
||||
{
|
||||
uint8_t bLength ; ///< Size of this descriptor in bytes.
|
||||
uint8_t bDescriptorType ; ///< Descriptor Type, must be Class-Specific
|
||||
uint8_t bDescriptorSubType ; ///< Descriptor SubType one of above CDC_FUCN_DESC_
|
||||
uint8_t bControlInterface ; ///< Interface number of Communication Interface
|
||||
uint8_t bSubordinateInterface ; ///< Array of Interface number of Data Interface
|
||||
}cdc_desc_func_union_t;
|
||||
|
||||
#define cdc_desc_func_union_n_t(no_slave)\
|
||||
struct TU_ATTR_PACKED { \
|
||||
uint8_t bLength ;\
|
||||
uint8_t bDescriptorType ;\
|
||||
uint8_t bDescriptorSubType ;\
|
||||
uint8_t bControlInterface ;\
|
||||
uint8_t bSubordinateInterface[no_slave] ;\
|
||||
}
|
||||
|
||||
/// Country Selection Functional Descriptor (Communication Interface)
|
||||
typedef struct TU_ATTR_PACKED
|
||||
{
|
||||
uint8_t bLength ; ///< Size of this descriptor in bytes.
|
||||
uint8_t bDescriptorType ; ///< Descriptor Type, must be Class-Specific
|
||||
uint8_t bDescriptorSubType ; ///< Descriptor SubType one of above CDC_FUCN_DESC_
|
||||
uint8_t iCountryCodeRelDate ; ///< Index of a string giving the release date for the implemented ISO 3166 Country Codes.
|
||||
uint16_t wCountryCode ; ///< Country code in the format as defined in [ISO3166], release date as specified inoffset 3 for the first supported country.
|
||||
}cdc_desc_func_country_selection_t;
|
||||
|
||||
#define cdc_desc_func_country_selection_n_t(no_country) \
|
||||
struct TU_ATTR_PACKED { \
|
||||
uint8_t bLength ;\
|
||||
uint8_t bDescriptorType ;\
|
||||
uint8_t bDescriptorSubType ;\
|
||||
uint8_t iCountryCodeRelDate ;\
|
||||
uint16_t wCountryCode[no_country] ;\
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// PUBLIC SWITCHED TELEPHONE NETWORK (PSTN) SUBCLASS
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
/// \brief Call Management Functional Descriptor
|
||||
/// \details This functional descriptor describes the processing of calls for the Communications Class interface.
|
||||
typedef struct TU_ATTR_PACKED
|
||||
{
|
||||
uint8_t bLength ; ///< Size of this descriptor in bytes.
|
||||
uint8_t bDescriptorType ; ///< Descriptor Type, must be Class-Specific
|
||||
uint8_t bDescriptorSubType ; ///< Descriptor SubType one of above CDC_FUCN_DESC_
|
||||
|
||||
struct {
|
||||
uint8_t handle_call : 1; ///< 0 - Device sends/receives call management information only over the Communications Class interface. 1 - Device can send/receive call management information over a Data Class interface.
|
||||
uint8_t send_recv_call : 1; ///< 0 - Device does not handle call management itself. 1 - Device handles call management itself.
|
||||
uint8_t TU_RESERVED : 6;
|
||||
} bmCapabilities;
|
||||
|
||||
uint8_t bDataInterface;
|
||||
}cdc_desc_func_call_management_t;
|
||||
|
||||
typedef struct TU_ATTR_PACKED
|
||||
{
|
||||
uint8_t support_comm_request : 1; ///< Device supports the request combination of Set_Comm_Feature, Clear_Comm_Feature, and Get_Comm_Feature.
|
||||
uint8_t support_line_request : 1; ///< Device supports the request combination of Set_Line_Coding, Set_Control_Line_State, Get_Line_Coding, and the notification Serial_State.
|
||||
uint8_t support_send_break : 1; ///< Device supports the request Send_Break
|
||||
uint8_t support_notification_network_connection : 1; ///< Device supports the notification Network_Connection.
|
||||
uint8_t TU_RESERVED : 4;
|
||||
}cdc_acm_capability_t;
|
||||
|
||||
TU_VERIFY_STATIC(sizeof(cdc_acm_capability_t) == 1, "mostly problem with compiler");
|
||||
|
||||
/// Abstract Control Management Functional Descriptor
|
||||
/// This functional descriptor describes the commands supported by by the Communications Class interface with SubClass code of \ref CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL
|
||||
typedef struct TU_ATTR_PACKED
|
||||
{
|
||||
uint8_t bLength ; ///< Size of this descriptor in bytes.
|
||||
uint8_t bDescriptorType ; ///< Descriptor Type, must be Class-Specific
|
||||
uint8_t bDescriptorSubType ; ///< Descriptor SubType one of above CDC_FUCN_DESC_
|
||||
cdc_acm_capability_t bmCapabilities ;
|
||||
}cdc_desc_func_acm_t;
|
||||
|
||||
/// \brief Direct Line Management Functional Descriptor
|
||||
/// \details This functional descriptor describes the commands supported by the Communications Class interface with SubClass code of \ref CDC_FUNC_DESC_DIRECT_LINE_MANAGEMENT
|
||||
typedef struct TU_ATTR_PACKED
|
||||
{
|
||||
uint8_t bLength ; ///< Size of this descriptor in bytes.
|
||||
uint8_t bDescriptorType ; ///< Descriptor Type, must be Class-Specific
|
||||
uint8_t bDescriptorSubType ; ///< Descriptor SubType one of above CDC_FUCN_DESC_
|
||||
struct {
|
||||
uint8_t require_pulse_setup : 1; ///< Device requires extra Pulse_Setup request during pulse dialing sequence to disengage holding circuit.
|
||||
uint8_t support_aux_request : 1; ///< Device supports the request combination of Set_Aux_Line_State, Ring_Aux_Jack, and notification Aux_Jack_Hook_State.
|
||||
uint8_t support_pulse_request : 1; ///< Device supports the request combination of Pulse_Setup, Send_Pulse, and Set_Pulse_Time.
|
||||
uint8_t TU_RESERVED : 5;
|
||||
} bmCapabilities;
|
||||
}cdc_desc_func_direct_line_management_t;
|
||||
|
||||
/// \brief Telephone Ringer Functional Descriptor
|
||||
/// \details The Telephone Ringer functional descriptor describes the ringer capabilities supported by the Communications Class interface,
|
||||
/// with the SubClass code of \ref CDC_COMM_SUBCLASS_TELEPHONE_CONTROL_MODEL
|
||||
typedef struct TU_ATTR_PACKED
|
||||
{
|
||||
uint8_t bLength ; ///< Size of this descriptor in bytes.
|
||||
uint8_t bDescriptorType ; ///< Descriptor Type, must be Class-Specific
|
||||
uint8_t bDescriptorSubType ; ///< Descriptor SubType one of above CDC_FUCN_DESC_
|
||||
uint8_t bRingerVolSteps ;
|
||||
uint8_t bNumRingerPatterns ;
|
||||
}cdc_desc_func_telephone_ringer_t;
|
||||
|
||||
/// \brief Telephone Operational Modes Functional Descriptor
|
||||
/// \details The Telephone Operational Modes functional descriptor describes the operational modes supported by
|
||||
/// the Communications Class interface, with the SubClass code of \ref CDC_COMM_SUBCLASS_TELEPHONE_CONTROL_MODEL
|
||||
typedef struct TU_ATTR_PACKED
|
||||
{
|
||||
uint8_t bLength ; ///< Size of this descriptor in bytes.
|
||||
uint8_t bDescriptorType ; ///< Descriptor Type, must be Class-Specific
|
||||
uint8_t bDescriptorSubType ; ///< Descriptor SubType one of above CDC_FUCN_DESC_
|
||||
struct {
|
||||
uint8_t simple_mode : 1;
|
||||
uint8_t standalone_mode : 1;
|
||||
uint8_t computer_centric_mode : 1;
|
||||
uint8_t TU_RESERVED : 5;
|
||||
} bmCapabilities;
|
||||
}cdc_desc_func_telephone_operational_modes_t;
|
||||
|
||||
/// \brief Telephone Call and Line State Reporting Capabilities Descriptor
|
||||
/// \details The Telephone Call and Line State Reporting Capabilities functional descriptor describes the abilities of a
|
||||
/// telephone device to report optional call and line states.
|
||||
typedef struct TU_ATTR_PACKED
|
||||
{
|
||||
uint8_t bLength ; ///< Size of this descriptor in bytes.
|
||||
uint8_t bDescriptorType ; ///< Descriptor Type, must be Class-Specific
|
||||
uint8_t bDescriptorSubType ; ///< Descriptor SubType one of above CDC_FUCN_DESC_
|
||||
struct {
|
||||
uint32_t interrupted_dialtone : 1; ///< 0 : Reports only dialtone (does not differentiate between normal and interrupted dialtone). 1 : Reports interrupted dialtone in addition to normal dialtone
|
||||
uint32_t ringback_busy_fastbusy : 1; ///< 0 : Reports only dialing state. 1 : Reports ringback, busy, and fast busy states.
|
||||
uint32_t caller_id : 1; ///< 0 : Does not report caller ID. 1 : Reports caller ID information.
|
||||
uint32_t incoming_distinctive : 1; ///< 0 : Reports only incoming ringing. 1 : Reports incoming distinctive ringing patterns.
|
||||
uint32_t dual_tone_multi_freq : 1; ///< 0 : Cannot report dual tone multi-frequency (DTMF) digits input remotely over the telephone line. 1 : Can report DTMF digits input remotely over the telephone line.
|
||||
uint32_t line_state_change : 1; ///< 0 : Does not support line state change notification. 1 : Does support line state change notification
|
||||
uint32_t TU_RESERVED0 : 2;
|
||||
uint32_t TU_RESERVED1 : 16;
|
||||
uint32_t TU_RESERVED2 : 8;
|
||||
} bmCapabilities;
|
||||
}cdc_desc_func_telephone_call_state_reporting_capabilities_t;
|
||||
|
||||
// TODO remove
|
||||
static inline uint8_t cdc_functional_desc_typeof(uint8_t const * p_desc)
|
||||
{
|
||||
return p_desc[2];
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Requests
|
||||
//--------------------------------------------------------------------+
|
||||
typedef struct TU_ATTR_PACKED
|
||||
{
|
||||
uint32_t bit_rate;
|
||||
uint8_t stop_bits; ///< 0: 1 stop bit - 1: 1.5 stop bits - 2: 2 stop bits
|
||||
uint8_t parity; ///< 0: None - 1: Odd - 2: Even - 3: Mark - 4: Space
|
||||
uint8_t data_bits; ///< can be 5, 6, 7, 8 or 16
|
||||
} cdc_line_coding_t;
|
||||
|
||||
TU_VERIFY_STATIC(sizeof(cdc_line_coding_t) == 7, "size is not correct");
|
||||
|
||||
typedef struct TU_ATTR_PACKED
|
||||
{
|
||||
uint16_t dtr : 1;
|
||||
uint16_t rts : 1;
|
||||
uint16_t : 6;
|
||||
uint16_t : 8;
|
||||
} cdc_line_control_state_t;
|
||||
|
||||
TU_VERIFY_STATIC(sizeof(cdc_line_control_state_t) == 2, "size is not correct");
|
||||
|
||||
TU_ATTR_PACKED_END // End of all packed definitions
|
||||
TU_ATTR_BIT_FIELD_ORDER_END
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
/** @} */
|
|
@ -0,0 +1,490 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#include "tusb_option.h"
|
||||
|
||||
#if (CFG_TUD_ENABLED && CFG_TUD_CDC)
|
||||
|
||||
#include "device/usbd.h"
|
||||
#include "device/usbd_pvt.h"
|
||||
|
||||
#include "cdc_device.h"
|
||||
|
||||
// Level where CFG_TUSB_DEBUG must be at least for this driver is logged
|
||||
#ifndef CFG_TUD_CDC_LOG_LEVEL
|
||||
#define CFG_TUD_CDC_LOG_LEVEL CFG_TUD_LOG_LEVEL
|
||||
#endif
|
||||
|
||||
#define TU_LOG_DRV(...) TU_LOG(CFG_TUD_CDC_LOG_LEVEL, __VA_ARGS__)
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// MACRO CONSTANT TYPEDEF
|
||||
//--------------------------------------------------------------------+
|
||||
enum
|
||||
{
|
||||
BULK_PACKET_SIZE = (TUD_OPT_HIGH_SPEED ? 512 : 64)
|
||||
};
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t itf_num;
|
||||
uint8_t ep_notif;
|
||||
uint8_t ep_in;
|
||||
uint8_t ep_out;
|
||||
|
||||
// Bit 0: DTR (Data Terminal Ready), Bit 1: RTS (Request to Send)
|
||||
uint8_t line_state;
|
||||
|
||||
/*------------- From this point, data is not cleared by bus reset -------------*/
|
||||
char wanted_char;
|
||||
TU_ATTR_ALIGNED(4) cdc_line_coding_t line_coding;
|
||||
|
||||
// FIFO
|
||||
tu_fifo_t rx_ff;
|
||||
tu_fifo_t tx_ff;
|
||||
|
||||
uint8_t rx_ff_buf[CFG_TUD_CDC_RX_BUFSIZE];
|
||||
uint8_t tx_ff_buf[CFG_TUD_CDC_TX_BUFSIZE];
|
||||
|
||||
OSAL_MUTEX_DEF(rx_ff_mutex);
|
||||
OSAL_MUTEX_DEF(tx_ff_mutex);
|
||||
|
||||
// Endpoint Transfer buffer
|
||||
CFG_TUSB_MEM_ALIGN uint8_t epout_buf[CFG_TUD_CDC_EP_BUFSIZE];
|
||||
CFG_TUSB_MEM_ALIGN uint8_t epin_buf[CFG_TUD_CDC_EP_BUFSIZE];
|
||||
|
||||
}cdcd_interface_t;
|
||||
|
||||
#define ITF_MEM_RESET_SIZE offsetof(cdcd_interface_t, wanted_char)
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// INTERNAL OBJECT & FUNCTION DECLARATION
|
||||
//--------------------------------------------------------------------+
|
||||
CFG_TUD_MEM_SECTION tu_static cdcd_interface_t _cdcd_itf[CFG_TUD_CDC];
|
||||
|
||||
static bool _prep_out_transaction (cdcd_interface_t* p_cdc)
|
||||
{
|
||||
uint8_t const rhport = 0;
|
||||
uint16_t available = tu_fifo_remaining(&p_cdc->rx_ff);
|
||||
|
||||
// Prepare for incoming data but only allow what we can store in the ring buffer.
|
||||
// TODO Actually we can still carry out the transfer, keeping count of received bytes
|
||||
// and slowly move it to the FIFO when read().
|
||||
// This pre-check reduces endpoint claiming
|
||||
TU_VERIFY(available >= sizeof(p_cdc->epout_buf));
|
||||
|
||||
// claim endpoint
|
||||
TU_VERIFY(usbd_edpt_claim(rhport, p_cdc->ep_out));
|
||||
|
||||
// fifo can be changed before endpoint is claimed
|
||||
available = tu_fifo_remaining(&p_cdc->rx_ff);
|
||||
|
||||
if ( available >= sizeof(p_cdc->epout_buf) )
|
||||
{
|
||||
return usbd_edpt_xfer(rhport, p_cdc->ep_out, p_cdc->epout_buf, sizeof(p_cdc->epout_buf));
|
||||
}else
|
||||
{
|
||||
// Release endpoint since we don't make any transfer
|
||||
usbd_edpt_release(rhport, p_cdc->ep_out);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// APPLICATION API
|
||||
//--------------------------------------------------------------------+
|
||||
bool tud_cdc_n_connected(uint8_t itf)
|
||||
{
|
||||
// DTR (bit 0) active is considered as connected
|
||||
return tud_ready() && tu_bit_test(_cdcd_itf[itf].line_state, 0);
|
||||
}
|
||||
|
||||
uint8_t tud_cdc_n_get_line_state (uint8_t itf)
|
||||
{
|
||||
return _cdcd_itf[itf].line_state;
|
||||
}
|
||||
|
||||
void tud_cdc_n_get_line_coding (uint8_t itf, cdc_line_coding_t* coding)
|
||||
{
|
||||
(*coding) = _cdcd_itf[itf].line_coding;
|
||||
}
|
||||
|
||||
void tud_cdc_n_set_wanted_char (uint8_t itf, char wanted)
|
||||
{
|
||||
_cdcd_itf[itf].wanted_char = wanted;
|
||||
}
|
||||
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// READ API
|
||||
//--------------------------------------------------------------------+
|
||||
uint32_t tud_cdc_n_available(uint8_t itf)
|
||||
{
|
||||
return tu_fifo_count(&_cdcd_itf[itf].rx_ff);
|
||||
}
|
||||
|
||||
uint32_t tud_cdc_n_read(uint8_t itf, void* buffer, uint32_t bufsize)
|
||||
{
|
||||
cdcd_interface_t* p_cdc = &_cdcd_itf[itf];
|
||||
uint32_t num_read = tu_fifo_read_n(&p_cdc->rx_ff, buffer, (uint16_t) TU_MIN(bufsize, UINT16_MAX));
|
||||
_prep_out_transaction(p_cdc);
|
||||
return num_read;
|
||||
}
|
||||
|
||||
bool tud_cdc_n_peek(uint8_t itf, uint8_t* chr)
|
||||
{
|
||||
return tu_fifo_peek(&_cdcd_itf[itf].rx_ff, chr);
|
||||
}
|
||||
|
||||
void tud_cdc_n_read_flush (uint8_t itf)
|
||||
{
|
||||
cdcd_interface_t* p_cdc = &_cdcd_itf[itf];
|
||||
tu_fifo_clear(&p_cdc->rx_ff);
|
||||
_prep_out_transaction(p_cdc);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// WRITE API
|
||||
//--------------------------------------------------------------------+
|
||||
uint32_t tud_cdc_n_write(uint8_t itf, void const* buffer, uint32_t bufsize)
|
||||
{
|
||||
cdcd_interface_t* p_cdc = &_cdcd_itf[itf];
|
||||
uint16_t ret = tu_fifo_write_n(&p_cdc->tx_ff, buffer, (uint16_t) TU_MIN(bufsize, UINT16_MAX));
|
||||
|
||||
// flush if queue more than packet size
|
||||
// may need to suppress -Wunreachable-code since most of the time CFG_TUD_CDC_TX_BUFSIZE < BULK_PACKET_SIZE
|
||||
if ( (tu_fifo_count(&p_cdc->tx_ff) >= BULK_PACKET_SIZE) || ((CFG_TUD_CDC_TX_BUFSIZE < BULK_PACKET_SIZE) && tu_fifo_full(&p_cdc->tx_ff)) )
|
||||
{
|
||||
tud_cdc_n_write_flush(itf);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
uint32_t tud_cdc_n_write_flush (uint8_t itf)
|
||||
{
|
||||
cdcd_interface_t* p_cdc = &_cdcd_itf[itf];
|
||||
|
||||
// Skip if usb is not ready yet
|
||||
TU_VERIFY( tud_ready(), 0 );
|
||||
|
||||
// No data to send
|
||||
if ( !tu_fifo_count(&p_cdc->tx_ff) ) return 0;
|
||||
|
||||
uint8_t const rhport = 0;
|
||||
|
||||
// Claim the endpoint
|
||||
TU_VERIFY( usbd_edpt_claim(rhport, p_cdc->ep_in), 0 );
|
||||
|
||||
// Pull data from FIFO
|
||||
uint16_t const count = tu_fifo_read_n(&p_cdc->tx_ff, p_cdc->epin_buf, sizeof(p_cdc->epin_buf));
|
||||
|
||||
if ( count )
|
||||
{
|
||||
TU_ASSERT( usbd_edpt_xfer(rhport, p_cdc->ep_in, p_cdc->epin_buf, count), 0 );
|
||||
return count;
|
||||
}else
|
||||
{
|
||||
// Release endpoint since we don't make any transfer
|
||||
// Note: data is dropped if terminal is not connected
|
||||
usbd_edpt_release(rhport, p_cdc->ep_in);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t tud_cdc_n_write_available (uint8_t itf)
|
||||
{
|
||||
return tu_fifo_remaining(&_cdcd_itf[itf].tx_ff);
|
||||
}
|
||||
|
||||
bool tud_cdc_n_write_clear (uint8_t itf)
|
||||
{
|
||||
return tu_fifo_clear(&_cdcd_itf[itf].tx_ff);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// USBD Driver API
|
||||
//--------------------------------------------------------------------+
|
||||
void cdcd_init(void)
|
||||
{
|
||||
tu_memclr(_cdcd_itf, sizeof(_cdcd_itf));
|
||||
|
||||
for(uint8_t i=0; i<CFG_TUD_CDC; i++)
|
||||
{
|
||||
cdcd_interface_t* p_cdc = &_cdcd_itf[i];
|
||||
|
||||
p_cdc->wanted_char = (char) -1;
|
||||
|
||||
// default line coding is : stop bit = 1, parity = none, data bits = 8
|
||||
p_cdc->line_coding.bit_rate = 115200;
|
||||
p_cdc->line_coding.stop_bits = 0;
|
||||
p_cdc->line_coding.parity = 0;
|
||||
p_cdc->line_coding.data_bits = 8;
|
||||
|
||||
// Config RX fifo
|
||||
tu_fifo_config(&p_cdc->rx_ff, p_cdc->rx_ff_buf, TU_ARRAY_SIZE(p_cdc->rx_ff_buf), 1, false);
|
||||
|
||||
// Config TX fifo as overwritable at initialization and will be changed to non-overwritable
|
||||
// if terminal supports DTR bit. Without DTR we do not know if data is actually polled by terminal.
|
||||
// In this way, the most current data is prioritized.
|
||||
tu_fifo_config(&p_cdc->tx_ff, p_cdc->tx_ff_buf, TU_ARRAY_SIZE(p_cdc->tx_ff_buf), 1, true);
|
||||
|
||||
tu_fifo_config_mutex(&p_cdc->rx_ff, NULL, osal_mutex_create(&p_cdc->rx_ff_mutex));
|
||||
tu_fifo_config_mutex(&p_cdc->tx_ff, osal_mutex_create(&p_cdc->tx_ff_mutex), NULL);
|
||||
}
|
||||
}
|
||||
|
||||
void cdcd_reset(uint8_t rhport)
|
||||
{
|
||||
(void) rhport;
|
||||
|
||||
for(uint8_t i=0; i<CFG_TUD_CDC; i++)
|
||||
{
|
||||
cdcd_interface_t* p_cdc = &_cdcd_itf[i];
|
||||
|
||||
tu_memclr(p_cdc, ITF_MEM_RESET_SIZE);
|
||||
tu_fifo_clear(&p_cdc->rx_ff);
|
||||
tu_fifo_clear(&p_cdc->tx_ff);
|
||||
tu_fifo_set_overwritable(&p_cdc->tx_ff, true);
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t cdcd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len)
|
||||
{
|
||||
// Only support ACM subclass
|
||||
TU_VERIFY( TUSB_CLASS_CDC == itf_desc->bInterfaceClass &&
|
||||
CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL == itf_desc->bInterfaceSubClass, 0);
|
||||
|
||||
// Find available interface
|
||||
cdcd_interface_t * p_cdc = NULL;
|
||||
for(uint8_t cdc_id=0; cdc_id<CFG_TUD_CDC; cdc_id++)
|
||||
{
|
||||
if ( _cdcd_itf[cdc_id].ep_in == 0 )
|
||||
{
|
||||
p_cdc = &_cdcd_itf[cdc_id];
|
||||
break;
|
||||
}
|
||||
}
|
||||
TU_ASSERT(p_cdc, 0);
|
||||
|
||||
//------------- Control Interface -------------//
|
||||
p_cdc->itf_num = itf_desc->bInterfaceNumber;
|
||||
|
||||
uint16_t drv_len = sizeof(tusb_desc_interface_t);
|
||||
uint8_t const * p_desc = tu_desc_next( itf_desc );
|
||||
|
||||
// Communication Functional Descriptors
|
||||
while ( TUSB_DESC_CS_INTERFACE == tu_desc_type(p_desc) && drv_len <= max_len )
|
||||
{
|
||||
drv_len += tu_desc_len(p_desc);
|
||||
p_desc = tu_desc_next(p_desc);
|
||||
}
|
||||
|
||||
if ( TUSB_DESC_ENDPOINT == tu_desc_type(p_desc) )
|
||||
{
|
||||
// notification endpoint
|
||||
tusb_desc_endpoint_t const * desc_ep = (tusb_desc_endpoint_t const *) p_desc;
|
||||
|
||||
TU_ASSERT( usbd_edpt_open(rhport, desc_ep), 0 );
|
||||
p_cdc->ep_notif = desc_ep->bEndpointAddress;
|
||||
|
||||
drv_len += tu_desc_len(p_desc);
|
||||
p_desc = tu_desc_next(p_desc);
|
||||
}
|
||||
|
||||
//------------- Data Interface (if any) -------------//
|
||||
if ( (TUSB_DESC_INTERFACE == tu_desc_type(p_desc)) &&
|
||||
(TUSB_CLASS_CDC_DATA == ((tusb_desc_interface_t const *) p_desc)->bInterfaceClass) )
|
||||
{
|
||||
// next to endpoint descriptor
|
||||
drv_len += tu_desc_len(p_desc);
|
||||
p_desc = tu_desc_next(p_desc);
|
||||
|
||||
// Open endpoint pair
|
||||
TU_ASSERT( usbd_open_edpt_pair(rhport, p_desc, 2, TUSB_XFER_BULK, &p_cdc->ep_out, &p_cdc->ep_in), 0 );
|
||||
|
||||
drv_len += 2*sizeof(tusb_desc_endpoint_t);
|
||||
}
|
||||
|
||||
// Prepare for incoming data
|
||||
_prep_out_transaction(p_cdc);
|
||||
|
||||
return drv_len;
|
||||
}
|
||||
|
||||
// Invoked when a control transfer occurred on an interface of this class
|
||||
// Driver response accordingly to the request and the transfer stage (setup/data/ack)
|
||||
// return false to stall control endpoint (e.g unsupported request)
|
||||
bool cdcd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request)
|
||||
{
|
||||
// Handle class request only
|
||||
TU_VERIFY(request->bmRequestType_bit.type == TUSB_REQ_TYPE_CLASS);
|
||||
|
||||
uint8_t itf = 0;
|
||||
cdcd_interface_t* p_cdc = _cdcd_itf;
|
||||
|
||||
// Identify which interface to use
|
||||
for ( ; ; itf++, p_cdc++)
|
||||
{
|
||||
if (itf >= TU_ARRAY_SIZE(_cdcd_itf)) return false;
|
||||
|
||||
if ( p_cdc->itf_num == request->wIndex ) break;
|
||||
}
|
||||
|
||||
switch ( request->bRequest )
|
||||
{
|
||||
case CDC_REQUEST_SET_LINE_CODING:
|
||||
if (stage == CONTROL_STAGE_SETUP)
|
||||
{
|
||||
TU_LOG_DRV(" Set Line Coding\r\n");
|
||||
tud_control_xfer(rhport, request, &p_cdc->line_coding, sizeof(cdc_line_coding_t));
|
||||
}
|
||||
else if ( stage == CONTROL_STAGE_ACK)
|
||||
{
|
||||
if ( tud_cdc_line_coding_cb ) tud_cdc_line_coding_cb(itf, &p_cdc->line_coding);
|
||||
}
|
||||
break;
|
||||
|
||||
case CDC_REQUEST_GET_LINE_CODING:
|
||||
if (stage == CONTROL_STAGE_SETUP)
|
||||
{
|
||||
TU_LOG_DRV(" Get Line Coding\r\n");
|
||||
tud_control_xfer(rhport, request, &p_cdc->line_coding, sizeof(cdc_line_coding_t));
|
||||
}
|
||||
break;
|
||||
|
||||
case CDC_REQUEST_SET_CONTROL_LINE_STATE:
|
||||
if (stage == CONTROL_STAGE_SETUP)
|
||||
{
|
||||
tud_control_status(rhport, request);
|
||||
}
|
||||
else if (stage == CONTROL_STAGE_ACK)
|
||||
{
|
||||
// CDC PSTN v1.2 section 6.3.12
|
||||
// Bit 0: Indicates if DTE is present or not.
|
||||
// This signal corresponds to V.24 signal 108/2 and RS-232 signal DTR (Data Terminal Ready)
|
||||
// Bit 1: Carrier control for half-duplex modems.
|
||||
// This signal corresponds to V.24 signal 105 and RS-232 signal RTS (Request to Send)
|
||||
bool const dtr = tu_bit_test(request->wValue, 0);
|
||||
bool const rts = tu_bit_test(request->wValue, 1);
|
||||
|
||||
p_cdc->line_state = (uint8_t) request->wValue;
|
||||
|
||||
// Disable fifo overwriting if DTR bit is set
|
||||
tu_fifo_set_overwritable(&p_cdc->tx_ff, !dtr);
|
||||
|
||||
TU_LOG_DRV(" Set Control Line State: DTR = %d, RTS = %d\r\n", dtr, rts);
|
||||
|
||||
// Invoke callback
|
||||
if ( tud_cdc_line_state_cb ) tud_cdc_line_state_cb(itf, dtr, rts);
|
||||
}
|
||||
break;
|
||||
case CDC_REQUEST_SEND_BREAK:
|
||||
if (stage == CONTROL_STAGE_SETUP)
|
||||
{
|
||||
tud_control_status(rhport, request);
|
||||
}
|
||||
else if (stage == CONTROL_STAGE_ACK)
|
||||
{
|
||||
TU_LOG_DRV(" Send Break\r\n");
|
||||
if ( tud_cdc_send_break_cb ) tud_cdc_send_break_cb(itf, request->wValue);
|
||||
}
|
||||
break;
|
||||
|
||||
default: return false; // stall unsupported request
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes)
|
||||
{
|
||||
(void) result;
|
||||
|
||||
uint8_t itf;
|
||||
cdcd_interface_t* p_cdc;
|
||||
|
||||
// Identify which interface to use
|
||||
for (itf = 0; itf < CFG_TUD_CDC; itf++)
|
||||
{
|
||||
p_cdc = &_cdcd_itf[itf];
|
||||
if ( ( ep_addr == p_cdc->ep_out ) || ( ep_addr == p_cdc->ep_in ) ) break;
|
||||
}
|
||||
TU_ASSERT(itf < CFG_TUD_CDC);
|
||||
|
||||
// Received new data
|
||||
if ( ep_addr == p_cdc->ep_out )
|
||||
{
|
||||
tu_fifo_write_n(&p_cdc->rx_ff, p_cdc->epout_buf, (uint16_t) xferred_bytes);
|
||||
|
||||
// Check for wanted char and invoke callback if needed
|
||||
if ( tud_cdc_rx_wanted_cb && (((signed char) p_cdc->wanted_char) != -1) )
|
||||
{
|
||||
for ( uint32_t i = 0; i < xferred_bytes; i++ )
|
||||
{
|
||||
if ( (p_cdc->wanted_char == p_cdc->epout_buf[i]) && !tu_fifo_empty(&p_cdc->rx_ff) )
|
||||
{
|
||||
tud_cdc_rx_wanted_cb(itf, p_cdc->wanted_char);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// invoke receive callback (if there is still data)
|
||||
if (tud_cdc_rx_cb && !tu_fifo_empty(&p_cdc->rx_ff) ) tud_cdc_rx_cb(itf);
|
||||
|
||||
// prepare for OUT transaction
|
||||
_prep_out_transaction(p_cdc);
|
||||
}
|
||||
|
||||
// Data sent to host, we continue to fetch from tx fifo to send.
|
||||
// Note: This will cause incorrect baudrate set in line coding.
|
||||
// Though maybe the baudrate is not really important !!!
|
||||
if ( ep_addr == p_cdc->ep_in )
|
||||
{
|
||||
// invoke transmit callback to possibly refill tx fifo
|
||||
if ( tud_cdc_tx_complete_cb ) tud_cdc_tx_complete_cb(itf);
|
||||
|
||||
if ( 0 == tud_cdc_n_write_flush(itf) )
|
||||
{
|
||||
// If there is no data left, a ZLP should be sent if
|
||||
// xferred_bytes is multiple of EP Packet size and not zero
|
||||
if ( !tu_fifo_count(&p_cdc->tx_ff) && xferred_bytes && (0 == (xferred_bytes & (BULK_PACKET_SIZE-1))) )
|
||||
{
|
||||
if ( usbd_edpt_claim(rhport, p_cdc->ep_in) )
|
||||
{
|
||||
usbd_edpt_xfer(rhport, p_cdc->ep_in, NULL, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// nothing to do with notif endpoint for now
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#endif
|
|
@ -0,0 +1,259 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#ifndef _TUSB_CDC_DEVICE_H_
|
||||
#define _TUSB_CDC_DEVICE_H_
|
||||
|
||||
#include "cdc.h"
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Class Driver Configuration
|
||||
//--------------------------------------------------------------------+
|
||||
#if !defined(CFG_TUD_CDC_EP_BUFSIZE) && defined(CFG_TUD_CDC_EPSIZE)
|
||||
#warning CFG_TUD_CDC_EPSIZE is renamed to CFG_TUD_CDC_EP_BUFSIZE, please update to use the new name
|
||||
#define CFG_TUD_CDC_EP_BUFSIZE CFG_TUD_CDC_EPSIZE
|
||||
#endif
|
||||
|
||||
#ifndef CFG_TUD_CDC_EP_BUFSIZE
|
||||
#define CFG_TUD_CDC_EP_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/** \addtogroup CDC_Serial Serial
|
||||
* @{
|
||||
* \defgroup CDC_Serial_Device Device
|
||||
* @{ */
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Application API (Multiple Ports)
|
||||
// CFG_TUD_CDC > 1
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Check if terminal is connected to this port
|
||||
bool tud_cdc_n_connected (uint8_t itf);
|
||||
|
||||
// Get current line state. Bit 0: DTR (Data Terminal Ready), Bit 1: RTS (Request to Send)
|
||||
uint8_t tud_cdc_n_get_line_state (uint8_t itf);
|
||||
|
||||
// Get current line encoding: bit rate, stop bits parity etc ..
|
||||
void tud_cdc_n_get_line_coding (uint8_t itf, cdc_line_coding_t* coding);
|
||||
|
||||
// Set special character that will trigger tud_cdc_rx_wanted_cb() callback on receiving
|
||||
void tud_cdc_n_set_wanted_char (uint8_t itf, char wanted);
|
||||
|
||||
// Get the number of bytes available for reading
|
||||
uint32_t tud_cdc_n_available (uint8_t itf);
|
||||
|
||||
// Read received bytes
|
||||
uint32_t tud_cdc_n_read (uint8_t itf, void* buffer, uint32_t bufsize);
|
||||
|
||||
// Read a byte, return -1 if there is none
|
||||
static inline
|
||||
int32_t tud_cdc_n_read_char (uint8_t itf);
|
||||
|
||||
// Clear the received FIFO
|
||||
void tud_cdc_n_read_flush (uint8_t itf);
|
||||
|
||||
// Get a byte from FIFO without removing it
|
||||
bool tud_cdc_n_peek (uint8_t itf, uint8_t* ui8);
|
||||
|
||||
// Write bytes to TX FIFO, data may remain in the FIFO for a while
|
||||
uint32_t tud_cdc_n_write (uint8_t itf, void const* buffer, uint32_t bufsize);
|
||||
|
||||
// Write a byte
|
||||
static inline
|
||||
uint32_t tud_cdc_n_write_char (uint8_t itf, char ch);
|
||||
|
||||
// Write a null-terminated string
|
||||
static inline
|
||||
uint32_t tud_cdc_n_write_str (uint8_t itf, char const* str);
|
||||
|
||||
// Force sending data if possible, return number of forced bytes
|
||||
uint32_t tud_cdc_n_write_flush (uint8_t itf);
|
||||
|
||||
// Return the number of bytes (characters) available for writing to TX FIFO buffer in a single n_write operation.
|
||||
uint32_t tud_cdc_n_write_available (uint8_t itf);
|
||||
|
||||
// Clear the transmit FIFO
|
||||
bool tud_cdc_n_write_clear (uint8_t itf);
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Application API (Single Port)
|
||||
//--------------------------------------------------------------------+
|
||||
static inline bool tud_cdc_connected (void);
|
||||
static inline uint8_t tud_cdc_get_line_state (void);
|
||||
static inline void tud_cdc_get_line_coding (cdc_line_coding_t* coding);
|
||||
static inline void tud_cdc_set_wanted_char (char wanted);
|
||||
|
||||
static inline uint32_t tud_cdc_available (void);
|
||||
static inline int32_t tud_cdc_read_char (void);
|
||||
static inline uint32_t tud_cdc_read (void* buffer, uint32_t bufsize);
|
||||
static inline void tud_cdc_read_flush (void);
|
||||
static inline bool tud_cdc_peek (uint8_t* ui8);
|
||||
|
||||
static inline uint32_t tud_cdc_write_char (char ch);
|
||||
static inline uint32_t tud_cdc_write (void const* buffer, uint32_t bufsize);
|
||||
static inline uint32_t tud_cdc_write_str (char const* str);
|
||||
static inline uint32_t tud_cdc_write_flush (void);
|
||||
static inline uint32_t tud_cdc_write_available (void);
|
||||
static inline bool tud_cdc_write_clear (void);
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Application Callback API (weak is optional)
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Invoked when received new data
|
||||
TU_ATTR_WEAK void tud_cdc_rx_cb(uint8_t itf);
|
||||
|
||||
// Invoked when received `wanted_char`
|
||||
TU_ATTR_WEAK void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char);
|
||||
|
||||
// Invoked when a TX is complete and therefore space becomes available in TX buffer
|
||||
TU_ATTR_WEAK void tud_cdc_tx_complete_cb(uint8_t itf);
|
||||
|
||||
// Invoked when line state DTR & RTS are changed via SET_CONTROL_LINE_STATE
|
||||
TU_ATTR_WEAK void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts);
|
||||
|
||||
// Invoked when line coding is change via SET_LINE_CODING
|
||||
TU_ATTR_WEAK void tud_cdc_line_coding_cb(uint8_t itf, cdc_line_coding_t const* p_line_coding);
|
||||
|
||||
// Invoked when received send break
|
||||
TU_ATTR_WEAK void tud_cdc_send_break_cb(uint8_t itf, uint16_t duration_ms);
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Inline Functions
|
||||
//--------------------------------------------------------------------+
|
||||
static inline int32_t tud_cdc_n_read_char (uint8_t itf)
|
||||
{
|
||||
uint8_t ch;
|
||||
return tud_cdc_n_read(itf, &ch, 1) ? (int32_t) ch : -1;
|
||||
}
|
||||
|
||||
static inline uint32_t tud_cdc_n_write_char(uint8_t itf, char ch)
|
||||
{
|
||||
return tud_cdc_n_write(itf, &ch, 1);
|
||||
}
|
||||
|
||||
static inline uint32_t tud_cdc_n_write_str (uint8_t itf, char const* str)
|
||||
{
|
||||
return tud_cdc_n_write(itf, str, strlen(str));
|
||||
}
|
||||
|
||||
static inline bool tud_cdc_connected (void)
|
||||
{
|
||||
return tud_cdc_n_connected(0);
|
||||
}
|
||||
|
||||
static inline uint8_t tud_cdc_get_line_state (void)
|
||||
{
|
||||
return tud_cdc_n_get_line_state(0);
|
||||
}
|
||||
|
||||
static inline void tud_cdc_get_line_coding (cdc_line_coding_t* coding)
|
||||
{
|
||||
tud_cdc_n_get_line_coding(0, coding);
|
||||
}
|
||||
|
||||
static inline void tud_cdc_set_wanted_char (char wanted)
|
||||
{
|
||||
tud_cdc_n_set_wanted_char(0, wanted);
|
||||
}
|
||||
|
||||
static inline uint32_t tud_cdc_available (void)
|
||||
{
|
||||
return tud_cdc_n_available(0);
|
||||
}
|
||||
|
||||
static inline int32_t tud_cdc_read_char (void)
|
||||
{
|
||||
return tud_cdc_n_read_char(0);
|
||||
}
|
||||
|
||||
static inline uint32_t tud_cdc_read (void* buffer, uint32_t bufsize)
|
||||
{
|
||||
return tud_cdc_n_read(0, buffer, bufsize);
|
||||
}
|
||||
|
||||
static inline void tud_cdc_read_flush (void)
|
||||
{
|
||||
tud_cdc_n_read_flush(0);
|
||||
}
|
||||
|
||||
static inline bool tud_cdc_peek (uint8_t* ui8)
|
||||
{
|
||||
return tud_cdc_n_peek(0, ui8);
|
||||
}
|
||||
|
||||
static inline uint32_t tud_cdc_write_char (char ch)
|
||||
{
|
||||
return tud_cdc_n_write_char(0, ch);
|
||||
}
|
||||
|
||||
static inline uint32_t tud_cdc_write (void const* buffer, uint32_t bufsize)
|
||||
{
|
||||
return tud_cdc_n_write(0, buffer, bufsize);
|
||||
}
|
||||
|
||||
static inline uint32_t tud_cdc_write_str (char const* str)
|
||||
{
|
||||
return tud_cdc_n_write_str(0, str);
|
||||
}
|
||||
|
||||
static inline uint32_t tud_cdc_write_flush (void)
|
||||
{
|
||||
return tud_cdc_n_write_flush(0);
|
||||
}
|
||||
|
||||
static inline uint32_t tud_cdc_write_available(void)
|
||||
{
|
||||
return tud_cdc_n_write_available(0);
|
||||
}
|
||||
|
||||
static inline bool tud_cdc_write_clear(void)
|
||||
{
|
||||
return tud_cdc_n_write_clear(0);
|
||||
}
|
||||
|
||||
/** @} */
|
||||
/** @} */
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// INTERNAL USBD-CLASS DRIVER API
|
||||
//--------------------------------------------------------------------+
|
||||
void cdcd_init (void);
|
||||
void cdcd_reset (uint8_t rhport);
|
||||
uint16_t cdcd_open (uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len);
|
||||
bool cdcd_control_xfer_cb (uint8_t rhport, uint8_t stage, tusb_control_request_t const * request);
|
||||
bool cdcd_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _TUSB_CDC_DEVICE_H_ */
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,204 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#ifndef _TUSB_CDC_HOST_H_
|
||||
#define _TUSB_CDC_HOST_H_
|
||||
|
||||
#include "cdc.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Class Driver Configuration
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Set Line Control state on enumeration/mounted: DTR ( bit 0), RTS (bit 1)
|
||||
#ifndef CFG_TUH_CDC_LINE_CONTROL_ON_ENUM
|
||||
#define CFG_TUH_CDC_LINE_CONTROL_ON_ENUM 0
|
||||
#endif
|
||||
|
||||
// Set Line Coding on enumeration/mounted, value for cdc_line_coding_t
|
||||
//#ifndef CFG_TUH_CDC_LINE_CODING_ON_ENUM
|
||||
//#define CFG_TUH_CDC_LINE_CODING_ON_ENUM { 115200, CDC_LINE_CODING_STOP_BITS_1, CDC_LINE_CODING_PARITY_NONE, 8 }
|
||||
//#endif
|
||||
|
||||
// RX FIFO size
|
||||
#ifndef CFG_TUH_CDC_RX_BUFSIZE
|
||||
#define CFG_TUH_CDC_RX_BUFSIZE USBH_EPSIZE_BULK_MAX
|
||||
#endif
|
||||
|
||||
// RX Endpoint size
|
||||
#ifndef CFG_TUH_CDC_RX_EPSIZE
|
||||
#define CFG_TUH_CDC_RX_EPSIZE USBH_EPSIZE_BULK_MAX
|
||||
#endif
|
||||
|
||||
// TX FIFO size
|
||||
#ifndef CFG_TUH_CDC_TX_BUFSIZE
|
||||
#define CFG_TUH_CDC_TX_BUFSIZE USBH_EPSIZE_BULK_MAX
|
||||
#endif
|
||||
|
||||
// TX Endpoint size
|
||||
#ifndef CFG_TUH_CDC_TX_EPSIZE
|
||||
#define CFG_TUH_CDC_TX_EPSIZE USBH_EPSIZE_BULK_MAX
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Application API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Get Interface index from device address + interface number
|
||||
// return TUSB_INDEX_INVALID_8 (0xFF) if not found
|
||||
uint8_t tuh_cdc_itf_get_index(uint8_t daddr, uint8_t itf_num);
|
||||
|
||||
// Get Interface information
|
||||
// return true if index is correct and interface is currently mounted
|
||||
bool tuh_cdc_itf_get_info(uint8_t idx, tuh_itf_info_t* info);
|
||||
|
||||
// Check if a interface is mounted
|
||||
bool tuh_cdc_mounted(uint8_t idx);
|
||||
|
||||
// Get current DTR status
|
||||
bool tuh_cdc_get_dtr(uint8_t idx);
|
||||
|
||||
// Get current RTS status
|
||||
bool tuh_cdc_get_rts(uint8_t idx);
|
||||
|
||||
// Check if interface is connected (DTR active)
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool tuh_cdc_connected(uint8_t idx)
|
||||
{
|
||||
return tuh_cdc_get_dtr(idx);
|
||||
}
|
||||
|
||||
// Get local (saved/cached) version of line coding.
|
||||
// This function should return correct values if tuh_cdc_set_line_coding() / tuh_cdc_get_line_coding()
|
||||
// are invoked previously or CFG_TUH_CDC_LINE_CODING_ON_ENUM is defined.
|
||||
// NOTE: This function does not make any USB transfer request to device.
|
||||
bool tuh_cdc_get_local_line_coding(uint8_t idx, cdc_line_coding_t* line_coding);
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Write API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Get the number of bytes available for writing
|
||||
uint32_t tuh_cdc_write_available(uint8_t idx);
|
||||
|
||||
// Write to cdc interface
|
||||
uint32_t tuh_cdc_write(uint8_t idx, void const* buffer, uint32_t bufsize);
|
||||
|
||||
// Force sending data if possible, return number of forced bytes
|
||||
uint32_t tuh_cdc_write_flush(uint8_t idx);
|
||||
|
||||
// Clear the transmit FIFO
|
||||
bool tuh_cdc_write_clear(uint8_t idx);
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Read API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Get the number of bytes available for reading
|
||||
uint32_t tuh_cdc_read_available(uint8_t idx);
|
||||
|
||||
// Read from cdc interface
|
||||
uint32_t tuh_cdc_read (uint8_t idx, void* buffer, uint32_t bufsize);
|
||||
|
||||
// Get a byte from RX FIFO without removing it
|
||||
bool tuh_cdc_peek(uint8_t idx, uint8_t* ch);
|
||||
|
||||
// Clear the received FIFO
|
||||
bool tuh_cdc_read_clear (uint8_t idx);
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Control Endpoint (Request) API
|
||||
// Each Function will make a USB control transfer request to/from device
|
||||
// - If complete_cb is provided, the function will return immediately and invoke
|
||||
// the callback when request is complete.
|
||||
// - If complete_cb is NULL, the function will block until request is complete.
|
||||
// - In this case, user_data should be pointed to xfer_result_t to hold the transfer result.
|
||||
// - The function will return true if transfer is successful, false otherwise.
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Request to Set Control Line State: DTR (bit 0), RTS (bit 1)
|
||||
bool tuh_cdc_set_control_line_state(uint8_t idx, uint16_t line_state, tuh_xfer_cb_t complete_cb, uintptr_t user_data);
|
||||
|
||||
// Request to set baudrate
|
||||
bool tuh_cdc_set_baudrate(uint8_t idx, uint32_t baudrate, tuh_xfer_cb_t complete_cb, uintptr_t user_data);
|
||||
|
||||
// Request to Set Line Coding (ACM only)
|
||||
// Should only use if you don't work with serial devices such as FTDI/CP210x
|
||||
bool tuh_cdc_set_line_coding(uint8_t idx, cdc_line_coding_t const* line_coding, tuh_xfer_cb_t complete_cb, uintptr_t user_data);
|
||||
|
||||
// Request to Get Line Coding (ACM only)
|
||||
// Should only use if tuh_cdc_set_line_coding() / tuh_cdc_get_line_coding() never got invoked and
|
||||
// CFG_TUH_CDC_LINE_CODING_ON_ENUM is not defined
|
||||
// bool tuh_cdc_get_line_coding(uint8_t idx, cdc_line_coding_t* coding);
|
||||
|
||||
// Connect by set both DTR, RTS
|
||||
TU_ATTR_ALWAYS_INLINE static inline
|
||||
bool tuh_cdc_connect(uint8_t idx, tuh_xfer_cb_t complete_cb, uintptr_t user_data)
|
||||
{
|
||||
return tuh_cdc_set_control_line_state(idx, CDC_CONTROL_LINE_STATE_DTR | CDC_CONTROL_LINE_STATE_RTS, complete_cb, user_data);
|
||||
}
|
||||
|
||||
// Disconnect by clear both DTR, RTS
|
||||
TU_ATTR_ALWAYS_INLINE static inline
|
||||
bool tuh_cdc_disconnect(uint8_t idx, tuh_xfer_cb_t complete_cb, uintptr_t user_data)
|
||||
{
|
||||
return tuh_cdc_set_control_line_state(idx, 0x00, complete_cb, user_data);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// CDC APPLICATION CALLBACKS
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Invoked when a device with CDC interface is mounted
|
||||
// idx is index of cdc interface in the internal pool.
|
||||
TU_ATTR_WEAK extern void tuh_cdc_mount_cb(uint8_t idx);
|
||||
|
||||
// Invoked when a device with CDC interface is unmounted
|
||||
TU_ATTR_WEAK extern void tuh_cdc_umount_cb(uint8_t idx);
|
||||
|
||||
// Invoked when received new data
|
||||
TU_ATTR_WEAK extern void tuh_cdc_rx_cb(uint8_t idx);
|
||||
|
||||
// Invoked when a TX is complete and therefore space becomes available in TX buffer
|
||||
TU_ATTR_WEAK extern void tuh_cdc_tx_complete_cb(uint8_t idx);
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Internal Class Driver API
|
||||
//--------------------------------------------------------------------+
|
||||
void cdch_init (void);
|
||||
bool cdch_open (uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *itf_desc, uint16_t max_len);
|
||||
bool cdch_set_config (uint8_t dev_addr, uint8_t itf_num);
|
||||
bool cdch_xfer_cb (uint8_t dev_addr, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes);
|
||||
void cdch_close (uint8_t dev_addr);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _TUSB_CDC_HOST_H_ */
|
|
@ -0,0 +1,301 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
/** \ingroup ClassDriver_CDC Communication Device Class (CDC)
|
||||
* \defgroup CDC_RNDIS Remote Network Driver Interface Specification (RNDIS)
|
||||
* @{
|
||||
* \defgroup CDC_RNDIS_Common Common Definitions
|
||||
* @{ */
|
||||
|
||||
#ifndef _TUSB_CDC_RNDIS_H_
|
||||
#define _TUSB_CDC_RNDIS_H_
|
||||
|
||||
#include "cdc.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifdef __CC_ARM
|
||||
#pragma diag_suppress 66 // Suppress Keil warnings #66-D: enumeration value is out of "int" range
|
||||
#endif
|
||||
|
||||
/// RNDIS Message Types
|
||||
typedef enum
|
||||
{
|
||||
RNDIS_MSG_PACKET = 0x00000001UL, ///< The host and device use this to send network data to one another.
|
||||
|
||||
RNDIS_MSG_INITIALIZE = 0x00000002UL, ///< Sent by the host to initialize the device.
|
||||
RNDIS_MSG_INITIALIZE_CMPLT = 0x80000002UL, ///< Device response to an initialize message.
|
||||
|
||||
RNDIS_MSG_HALT = 0x00000003UL, ///< Sent by the host to halt the device. This does not have a response. It is optional for the device to send this message to the host.
|
||||
|
||||
RNDIS_MSG_QUERY = 0x00000004UL, ///< Sent by the host to send a query OID.
|
||||
RNDIS_MSG_QUERY_CMPLT = 0x80000004UL, ///< Device response to a query OID.
|
||||
|
||||
RNDIS_MSG_SET = 0x00000005UL, ///< Sent by the host to send a set OID.
|
||||
RNDIS_MSG_SET_CMPLT = 0x80000005UL, ///< Device response to a set OID.
|
||||
|
||||
RNDIS_MSG_RESET = 0x00000006UL, ///< Sent by the host to perform a soft reset on the device.
|
||||
RNDIS_MSG_RESET_CMPLT = 0x80000006UL, ///< Device response to reset message.
|
||||
|
||||
RNDIS_MSG_INDICATE_STATUS = 0x00000007UL, ///< Sent by the device to indicate its status or an error when an unrecognized message is received.
|
||||
|
||||
RNDIS_MSG_KEEP_ALIVE = 0x00000008UL, ///< During idle periods, sent every few seconds by the host to check that the device is still responsive. It is optional for the device to send this message to check if the host is active.
|
||||
RNDIS_MSG_KEEP_ALIVE_CMPLT = 0x80000008UL ///< The device response to a keepalivemessage. The host can respond with this message to a keepalive message from the device when the device implements the optional KeepAliveTimer.
|
||||
}rndis_msg_type_t;
|
||||
|
||||
/// RNDIS Message Status Values
|
||||
typedef enum
|
||||
{
|
||||
RNDIS_STATUS_SUCCESS = 0x00000000UL, ///< Success
|
||||
RNDIS_STATUS_FAILURE = 0xC0000001UL, ///< Unspecified error
|
||||
RNDIS_STATUS_INVALID_DATA = 0xC0010015UL, ///< Invalid data error
|
||||
RNDIS_STATUS_NOT_SUPPORTED = 0xC00000BBUL, ///< Unsupported request error
|
||||
RNDIS_STATUS_MEDIA_CONNECT = 0x4001000BUL, ///< Device is connected to a network medium.
|
||||
RNDIS_STATUS_MEDIA_DISCONNECT = 0x4001000CUL ///< Device is disconnected from the medium.
|
||||
}rndis_msg_status_t;
|
||||
|
||||
#ifdef __CC_ARM
|
||||
#pragma diag_default 66 // return Keil 66 to normal severity
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// MESSAGE STRUCTURE
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
//------------- Initialize -------------//
|
||||
/// \brief Initialize Message
|
||||
/// \details This message MUST be sent by the host to initialize the device.
|
||||
typedef struct {
|
||||
uint32_t type ; ///< Message type, must be \ref RNDIS_MSG_INITIALIZE
|
||||
uint32_t length ; ///< Message length in bytes, must be 0x18
|
||||
uint32_t request_id ; ///< A 32-bit integer value, generated by the host, used to match the host's sent request to the response from the device.
|
||||
uint32_t major_version ; ///< The major version of the RNDIS Protocol implemented by the host.
|
||||
uint32_t minor_version ; ///< The minor version of the RNDIS Protocol implemented by the host
|
||||
uint32_t max_xfer_size ; ///< The maximum size, in bytes, of any single bus data transfer that the host expects to receive from the device.
|
||||
}rndis_msg_initialize_t;
|
||||
|
||||
/// \brief Initialize Complete Message
|
||||
/// \details This message MUST be sent by the device in response to an initialize message.
|
||||
typedef struct {
|
||||
uint32_t type ; ///< Message Type, must be \ref RNDIS_MSG_INITIALIZE_CMPLT
|
||||
uint32_t length ; ///< Message length in bytes, must be 0x30
|
||||
uint32_t request_id ; ///< A 32-bit integer value from \a request_id field of the \ref rndis_msg_initialize_t to which this message is a response.
|
||||
uint32_t status ; ///< The initialization status of the device, has value from \ref rndis_msg_status_t
|
||||
uint32_t major_version ; ///< the highest-numbered RNDIS Protocol version supported by the device.
|
||||
uint32_t minor_version ; ///< the highest-numbered RNDIS Protocol version supported by the device.
|
||||
uint32_t device_flags ; ///< MUST be set to 0x000000010. Other values are reserved for future use.
|
||||
uint32_t medium ; ///< is 0x00 for RNDIS_MEDIUM_802_3
|
||||
uint32_t max_packet_per_xfer ; ///< The maximum number of concatenated \ref RNDIS_MSG_PACKET messages that the device can handle in a single bus transfer to it. This value MUST be at least 1.
|
||||
uint32_t max_xfer_size ; ///< The maximum size, in bytes, of any single bus data transfer that the device expects to receive from the host.
|
||||
uint32_t packet_alignment_factor ; ///< The byte alignment the device expects for each RNDIS message that is part of a multimessage transfer to it. The value is specified as an exponent of 2; for example, the host uses 2<SUP>{PacketAlignmentFactor}</SUP> as the alignment value.
|
||||
uint32_t reserved[2] ;
|
||||
} rndis_msg_initialize_cmplt_t;
|
||||
|
||||
//------------- Query -------------//
|
||||
/// \brief Query Message
|
||||
/// \details This message MUST be sent by the host to query an OID.
|
||||
typedef struct {
|
||||
uint32_t type ; ///< Message Type, must be \ref RNDIS_MSG_QUERY
|
||||
uint32_t length ; ///< Message length in bytes, including the header and the \a oid_buffer
|
||||
uint32_t request_id ; ///< A 32-bit integer value, generated by the host, used to match the host's sent request to the response from the device.
|
||||
uint32_t oid ; ///< The integer value of the host operating system-defined identifier, for the parameter of the device being queried for.
|
||||
uint32_t buffer_length ; ///< The length, in bytes, of the input data required for the OID query. This MUST be set to 0 when there is no input data associated with the OID.
|
||||
uint32_t buffer_offset ; ///< The offset, in bytes, from the beginning of \a request_id field where the input data for the query is located in the message. This value MUST be set to 0 when there is no input data associated with the OID.
|
||||
uint32_t reserved ;
|
||||
uint8_t oid_buffer[] ; ///< Flexible array contains the input data supplied by the host, required for the OID query request processing by the device, as per the host NDIS specification.
|
||||
} rndis_msg_query_t, rndis_msg_set_t;
|
||||
|
||||
TU_VERIFY_STATIC(sizeof(rndis_msg_query_t) == 28, "Make sure flexible array member does not affect layout");
|
||||
|
||||
/// \brief Query Complete Message
|
||||
/// \details This message MUST be sent by the device in response to a query OID message.
|
||||
typedef struct {
|
||||
uint32_t type ; ///< Message Type, must be \ref RNDIS_MSG_QUERY_CMPLT
|
||||
uint32_t length ; ///< Message length in bytes, including the header and the \a oid_buffer
|
||||
uint32_t request_id ; ///< A 32-bit integer value from \a request_id field of the \ref rndis_msg_query_t to which this message is a response.
|
||||
uint32_t status ; ///< The status of processing for the query request, has value from \ref rndis_msg_status_t.
|
||||
uint32_t buffer_length ; ///< The length, in bytes, of the data in the response to the query. This MUST be set to 0 when there is no OIDInputBuffer.
|
||||
uint32_t buffer_offset ; ///< The offset, in bytes, from the beginning of \a request_id field where the response data for the query is located in the message. This MUST be set to 0 when there is no \ref oid_buffer.
|
||||
uint8_t oid_buffer[] ; ///< Flexible array member contains the response data to the OID query request as specified by the host.
|
||||
} rndis_msg_query_cmplt_t;
|
||||
|
||||
TU_VERIFY_STATIC(sizeof(rndis_msg_query_cmplt_t) == 24, "Make sure flexible array member does not affect layout");
|
||||
|
||||
//------------- Reset -------------//
|
||||
/// \brief Reset Message
|
||||
/// \details This message MUST be sent by the host to perform a soft reset on the device.
|
||||
typedef struct {
|
||||
uint32_t type ; ///< Message Type, must be \ref RNDIS_MSG_RESET
|
||||
uint32_t length ; ///< Message length in bytes, MUST be 0x06
|
||||
uint32_t reserved ;
|
||||
} rndis_msg_reset_t;
|
||||
|
||||
/// \brief Reset Complete Message
|
||||
/// \details This message MUST be sent by the device in response to a reset message.
|
||||
typedef struct {
|
||||
uint32_t type ; ///< Message Type, must be \ref RNDIS_MSG_RESET_CMPLT
|
||||
uint32_t length ; ///< Message length in bytes, MUST be 0x10
|
||||
uint32_t status ; ///< The status of processing for the \ref rndis_msg_reset_t, has value from \ref rndis_msg_status_t.
|
||||
uint32_t addressing_reset ; ///< This field indicates whether the addressing information, which is the multicast address list or packet filter, has been lost during the reset operation. This MUST be set to 0x00000001 if the device requires that the host to resend addressing information or MUST be set to zero otherwise.
|
||||
} rndis_msg_reset_cmplt_t;
|
||||
|
||||
//typedef struct {
|
||||
// uint32_t type;
|
||||
// uint32_t length;
|
||||
// uint32_t status;
|
||||
// uint32_t buffer_length;
|
||||
// uint32_t buffer_offset;
|
||||
// uint32_t diagnostic_status; // optional
|
||||
// uint32_t diagnostic_error_offset; // optional
|
||||
// uint32_t status_buffer[0]; // optional
|
||||
//} rndis_msg_indicate_status_t;
|
||||
|
||||
/// \brief Keep Alive Message
|
||||
/// \details This message MUST be sent by the host to check that device is still responsive. It is optional for the device to send this message to check if the host is active
|
||||
typedef struct {
|
||||
uint32_t type ; ///< Message Type
|
||||
uint32_t length ; ///< Message length in bytes, MUST be 0x10
|
||||
uint32_t request_id ;
|
||||
} rndis_msg_keep_alive_t, rndis_msg_halt_t;
|
||||
|
||||
/// \brief Set Complete Message
|
||||
/// \brief This message MUST be sent in response to a the request message
|
||||
typedef struct {
|
||||
uint32_t type ; ///< Message Type
|
||||
uint32_t length ; ///< Message length in bytes, MUST be 0x10
|
||||
uint32_t request_id ; ///< must be the same as requesting message
|
||||
uint32_t status ; ///< The status of processing for the request message request by the device to which this message is the response.
|
||||
} rndis_msg_set_cmplt_t, rndis_msg_keep_alive_cmplt_t;
|
||||
|
||||
/// \brief Packet Data Message
|
||||
/// \brief This message MUST be used by the host and the device to send network data to one another.
|
||||
typedef struct {
|
||||
uint32_t type ; ///< Message Type, must be \ref RNDIS_MSG_PACKET
|
||||
uint32_t length ; ///< Message length in bytes, The total length of this RNDIS message including the header, payload, and padding.
|
||||
uint32_t data_offset ; ///< Specifies the offset, in bytes, from the start of this \a data_offset field of this message to the start of the data. This MUST be an integer multiple of 4.
|
||||
uint32_t data_length ; ///< Specifies the number of bytes in the payload of this message.
|
||||
uint32_t out_of_band_data_offet ; ///< Specifies the offset, in bytes, of the first out-of-band data record from the start of the DataOffset field in this message. MUST be an integer multiple of 4 when out-of-band data is present or set to 0 otherwise. When there are multiple out-ofband data records, each subsequent record MUST immediately follow the previous out-of-band data record.
|
||||
uint32_t out_of_band_data_length ; ///< Specifies, in bytes, the total length of the out-of-band data.
|
||||
uint32_t num_out_of_band_data_elements ; ///< Specifies the number of out-of-band records in this message.
|
||||
uint32_t per_packet_info_offset ; ///< Specifies the offset, in bytes, of the start of per-packet-info data record from the start of the \a data_offset field in this message. MUST be an integer multiple of 4 when per-packet-info data record is present or MUST be set to 0 otherwise. When there are multiple per-packet-info data records, each subsequent record MUST immediately follow the previous record.
|
||||
uint32_t per_packet_info_length ; ///< Specifies, in bytes, the total length of per-packetinformation contained in this message.
|
||||
uint32_t reserved[2] ;
|
||||
uint32_t payload[0] ; ///< Network data contained in this message.
|
||||
|
||||
// uint8_t padding[0]
|
||||
// Additional bytes of zeros added at the end of the message to comply with
|
||||
// the internal and external padding requirements. Internal padding SHOULD be as per the
|
||||
// specification of the out-of-band data record and per-packet-info data record. The external
|
||||
//padding size SHOULD be determined based on the PacketAlignmentFactor field specification
|
||||
//in REMOTE_NDIS_INITIALIZE_CMPLT message by the device, when multiple
|
||||
//REMOTE_NDIS_PACKET_MSG messages are bundled together in a single bus-native message.
|
||||
//In this case, all but the very last REMOTE_NDIS_PACKET_MSG MUST respect the
|
||||
//PacketAlignmentFactor field.
|
||||
|
||||
// rndis_msg_packet_t [0] : (optional) more packet if multiple packet per bus transaction is supported
|
||||
} rndis_msg_packet_t;
|
||||
|
||||
|
||||
typedef struct {
|
||||
uint32_t size ; ///< Length, in bytes, of this header and appended data and padding. This value MUST be an integer multiple of 4.
|
||||
uint32_t type ; ///< MUST be as per host operating system specification.
|
||||
uint32_t offset ; ///< The byte offset from the beginning of this record to the beginning of data.
|
||||
uint32_t data[0] ; ///< Flexible array contains data
|
||||
} rndis_msg_out_of_band_data_t, rndis_msg_per_packet_info_t;
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// NDIS Object ID
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
/// NDIS Object ID
|
||||
typedef enum
|
||||
{
|
||||
//------------- General Required OIDs -------------//
|
||||
RNDIS_OID_GEN_SUPPORTED_LIST = 0x00010101, ///< List of supported OIDs
|
||||
RNDIS_OID_GEN_HARDWARE_STATUS = 0x00010102, ///< Hardware status
|
||||
RNDIS_OID_GEN_MEDIA_SUPPORTED = 0x00010103, ///< Media types supported (encoded)
|
||||
RNDIS_OID_GEN_MEDIA_IN_USE = 0x00010104, ///< Media types in use (encoded)
|
||||
RNDIS_OID_GEN_MAXIMUM_LOOKAHEAD = 0x00010105, ///<
|
||||
RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE = 0x00010106, ///< Maximum frame size in bytes
|
||||
RNDIS_OID_GEN_LINK_SPEED = 0x00010107, ///< Link speed in units of 100 bps
|
||||
RNDIS_OID_GEN_TRANSMIT_BUFFER_SPACE = 0x00010108, ///< Transmit buffer space
|
||||
RNDIS_OID_GEN_RECEIVE_BUFFER_SPACE = 0x00010109, ///< Receive buffer space
|
||||
RNDIS_OID_GEN_TRANSMIT_BLOCK_SIZE = 0x0001010A, ///< Minimum amount of storage, in bytes, that a single packet occupies in the transmit buffer space of the NIC
|
||||
RNDIS_OID_GEN_RECEIVE_BLOCK_SIZE = 0x0001010B, ///< Amount of storage, in bytes, that a single packet occupies in the receive buffer space of the NIC
|
||||
RNDIS_OID_GEN_VENDOR_ID = 0x0001010C, ///< Vendor NIC code
|
||||
RNDIS_OID_GEN_VENDOR_DESCRIPTION = 0x0001010D, ///< Vendor network card description
|
||||
RNDIS_OID_GEN_CURRENT_PACKET_FILTER = 0x0001010E, ///< Current packet filter (encoded)
|
||||
RNDIS_OID_GEN_CURRENT_LOOKAHEAD = 0x0001010F, ///< Current lookahead size in bytes
|
||||
RNDIS_OID_GEN_DRIVER_VERSION = 0x00010110, ///< NDIS version number used by the driver
|
||||
RNDIS_OID_GEN_MAXIMUM_TOTAL_SIZE = 0x00010111, ///< Maximum total packet length in bytes
|
||||
RNDIS_OID_GEN_PROTOCOL_OPTIONS = 0x00010112, ///< Optional protocol flags (encoded)
|
||||
RNDIS_OID_GEN_MAC_OPTIONS = 0x00010113, ///< Optional NIC flags (encoded)
|
||||
RNDIS_OID_GEN_MEDIA_CONNECT_STATUS = 0x00010114, ///< Whether the NIC is connected to the network
|
||||
RNDIS_OID_GEN_MAXIMUM_SEND_PACKETS = 0x00010115, ///< The maximum number of send packets the driver can accept per call to its MiniportSendPacketsfunction
|
||||
|
||||
//------------- General Optional OIDs -------------//
|
||||
RNDIS_OID_GEN_VENDOR_DRIVER_VERSION = 0x00010116, ///< Vendor-assigned version number of the driver
|
||||
RNDIS_OID_GEN_SUPPORTED_GUIDS = 0x00010117, ///< The custom GUIDs (Globally Unique Identifier) supported by the miniport driver
|
||||
RNDIS_OID_GEN_NETWORK_LAYER_ADDRESSES = 0x00010118, ///< List of network-layer addresses associated with the binding between a transport and the driver
|
||||
RNDIS_OID_GEN_TRANSPORT_HEADER_OFFSET = 0x00010119, ///< Size of packets' additional headers
|
||||
RNDIS_OID_GEN_MEDIA_CAPABILITIES = 0x00010201, ///<
|
||||
RNDIS_OID_GEN_PHYSICAL_MEDIUM = 0x00010202, ///< Physical media supported by the miniport driver (encoded)
|
||||
|
||||
//------------- 802.3 Objects (Ethernet) -------------//
|
||||
RNDIS_OID_802_3_PERMANENT_ADDRESS = 0x01010101, ///< Permanent station address
|
||||
RNDIS_OID_802_3_CURRENT_ADDRESS = 0x01010102, ///< Current station address
|
||||
RNDIS_OID_802_3_MULTICAST_LIST = 0x01010103, ///< Current multicast address list
|
||||
RNDIS_OID_802_3_MAXIMUM_LIST_SIZE = 0x01010104, ///< Maximum size of multicast address list
|
||||
} rndis_oid_type_t;
|
||||
|
||||
/// RNDIS Packet Filter Bits \ref RNDIS_OID_GEN_CURRENT_PACKET_FILTER.
|
||||
typedef enum
|
||||
{
|
||||
RNDIS_PACKET_TYPE_DIRECTED = 0x00000001, ///< Directed packets. Directed packets contain a destination address equal to the station address of the NIC.
|
||||
RNDIS_PACKET_TYPE_MULTICAST = 0x00000002, ///< Multicast address packets sent to addresses in the multicast address list.
|
||||
RNDIS_PACKET_TYPE_ALL_MULTICAST = 0x00000004, ///< All multicast address packets, not just the ones enumerated in the multicast address list.
|
||||
RNDIS_PACKET_TYPE_BROADCAST = 0x00000008, ///< Broadcast packets.
|
||||
RNDIS_PACKET_TYPE_SOURCE_ROUTING = 0x00000010, ///< All source routing packets. If the protocol driver sets this bit, the NDIS library attempts to act as a source routing bridge.
|
||||
RNDIS_PACKET_TYPE_PROMISCUOUS = 0x00000020, ///< Specifies all packets regardless of whether VLAN filtering is enabled or not and whether the VLAN identifier matches or not.
|
||||
RNDIS_PACKET_TYPE_SMT = 0x00000040, ///< SMT packets that an FDDI NIC receives.
|
||||
RNDIS_PACKET_TYPE_ALL_LOCAL = 0x00000080, ///< All packets sent by installed protocols and all packets indicated by the NIC that is identified by a given NdisBindingHandle.
|
||||
RNDIS_PACKET_TYPE_GROUP = 0x00001000, ///< Packets sent to the current group address.
|
||||
RNDIS_PACKET_TYPE_ALL_FUNCTIONAL = 0x00002000, ///< All functional address packets, not just the ones in the current functional address.
|
||||
RNDIS_PACKET_TYPE_FUNCTIONAL = 0x00004000, ///< Functional address packets sent to addresses included in the current functional address.
|
||||
RNDIS_PACKET_TYPE_MAC_FRAME = 0x00008000, ///< NIC driver frames that a Token Ring NIC receives.
|
||||
RNDIS_PACKET_TYPE_NO_LOCAL = 0x00010000,
|
||||
} rndis_packet_filter_type_t;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _TUSB_CDC_RNDIS_H_ */
|
||||
|
||||
/** @} */
|
||||
/** @} */
|
|
@ -0,0 +1,289 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#include "tusb_option.h"
|
||||
|
||||
#if (CFG_TUH_ENABLED && CFG_TUH_CDC && CFG_TUH_CDC_RNDIS)
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// INCLUDE
|
||||
//--------------------------------------------------------------------+
|
||||
#include "common/tusb_common.h"
|
||||
#include "cdc_host.h"
|
||||
#include "cdc_rndis_host.h"
|
||||
|
||||
#if 0 // TODO remove subtask related macros later
|
||||
// Sub Task
|
||||
#define OSAL_SUBTASK_BEGIN
|
||||
#define OSAL_SUBTASK_END return TUSB_ERROR_NONE;
|
||||
|
||||
#define STASK_RETURN(_error) return _error;
|
||||
#define STASK_INVOKE(_subtask, _status) (_status) = _subtask
|
||||
#define STASK_ASSERT(_cond) TU_VERIFY(_cond, TUSB_ERROR_OSAL_TASK_FAILED)
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// MACRO CONSTANT TYPEDEF
|
||||
//--------------------------------------------------------------------+
|
||||
#define RNDIS_MSG_PAYLOAD_MAX (1024*4)
|
||||
|
||||
CFG_TUH_MEM_SECTION static uint8_t msg_notification[CFG_TUH_DEVICE_MAX][8];
|
||||
CFG_TUH_MEM_SECTION CFG_TUH_MEM_ALIGN static uint8_t msg_payload[RNDIS_MSG_PAYLOAD_MAX];
|
||||
|
||||
static rndish_data_t rndish_data[CFG_TUH_DEVICE_MAX];
|
||||
|
||||
// TODO Microsoft requires message length for any get command must be at least 4096 bytes
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// INTERNAL OBJECT & FUNCTION DECLARATION
|
||||
//--------------------------------------------------------------------+
|
||||
static tusb_error_t rndis_body_subtask(void);
|
||||
static tusb_error_t send_message_get_response_subtask( uint8_t dev_addr, cdch_data_t *p_cdc,
|
||||
uint8_t * p_mess, uint32_t mess_length,
|
||||
uint8_t *p_response );
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// APPLICATION API
|
||||
//--------------------------------------------------------------------+
|
||||
tusb_error_t tusbh_cdc_rndis_get_mac_addr(uint8_t dev_addr, uint8_t mac_address[6])
|
||||
{
|
||||
TU_ASSERT( tusbh_cdc_rndis_is_mounted(dev_addr), TUSB_ERROR_CDCH_DEVICE_NOT_MOUNTED);
|
||||
TU_VERIFY( mac_address, TUSB_ERROR_INVALID_PARA);
|
||||
|
||||
memcpy(mac_address, rndish_data[dev_addr-1].mac_address, 6);
|
||||
|
||||
return TUSB_ERROR_NONE;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// IMPLEMENTATION
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// To enable the TASK_ASSERT style (quick return on false condition) in a real RTOS, a task must act as a wrapper
|
||||
// and is used mainly to call subtasks. Within a subtask return statement can be called freely, the task with
|
||||
// forever loop cannot have any return at all.
|
||||
OSAL_TASK_FUNCTION(cdch_rndis_task) (void* param;)
|
||||
{
|
||||
OSAL_TASK_BEGIN
|
||||
rndis_body_subtask();
|
||||
OSAL_TASK_END
|
||||
}
|
||||
|
||||
static tusb_error_t rndis_body_subtask(void)
|
||||
{
|
||||
static uint8_t relative_addr;
|
||||
|
||||
OSAL_SUBTASK_BEGIN
|
||||
|
||||
for (relative_addr = 0; relative_addr < CFG_TUH_DEVICE_MAX; relative_addr++)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
osal_task_delay(100);
|
||||
|
||||
OSAL_SUBTASK_END
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// RNDIS-CDC Driver API
|
||||
//--------------------------------------------------------------------+
|
||||
void rndish_init(void)
|
||||
{
|
||||
tu_memclr(rndish_data, sizeof(rndish_data_t)*CFG_TUH_DEVICE_MAX);
|
||||
|
||||
//------------- Task creation -------------//
|
||||
|
||||
//------------- semaphore creation for notification pipe -------------//
|
||||
for(uint8_t i=0; i<CFG_TUH_DEVICE_MAX; i++)
|
||||
{
|
||||
rndish_data[i].sem_notification_hdl = osal_semaphore_create( OSAL_SEM_REF(rndish_data[i].semaphore_notification) );
|
||||
}
|
||||
}
|
||||
|
||||
void rndish_close(uint8_t dev_addr)
|
||||
{
|
||||
osal_semaphore_reset( rndish_data[dev_addr-1].sem_notification_hdl );
|
||||
// tu_memclr(&rndish_data[dev_addr-1], sizeof(rndish_data_t)); TODO need to move semaphore & its handle out before memclr
|
||||
}
|
||||
|
||||
|
||||
static rndis_msg_initialize_t const msg_init =
|
||||
{
|
||||
.type = RNDIS_MSG_INITIALIZE,
|
||||
.length = sizeof(rndis_msg_initialize_t),
|
||||
.request_id = 1, // TODO should use some magic number
|
||||
.major_version = 1,
|
||||
.minor_version = 0,
|
||||
.max_xfer_size = 0x4000 // TODO mimic windows
|
||||
};
|
||||
|
||||
static rndis_msg_query_t const msg_query_permanent_addr =
|
||||
{
|
||||
.type = RNDIS_MSG_QUERY,
|
||||
.length = sizeof(rndis_msg_query_t)+6,
|
||||
.request_id = 1,
|
||||
.oid = RNDIS_OID_802_3_PERMANENT_ADDRESS,
|
||||
.buffer_length = 6,
|
||||
.buffer_offset = 20,
|
||||
};
|
||||
|
||||
static rndis_msg_set_t const msg_set_packet_filter =
|
||||
{
|
||||
.type = RNDIS_MSG_SET,
|
||||
.length = sizeof(rndis_msg_set_t)+4,
|
||||
.request_id = 1,
|
||||
.oid = RNDIS_OID_GEN_CURRENT_PACKET_FILTER,
|
||||
.buffer_length = 4,
|
||||
.buffer_offset = 20,
|
||||
};
|
||||
|
||||
tusb_error_t rndish_open_subtask(uint8_t dev_addr, cdch_data_t *p_cdc)
|
||||
{
|
||||
tusb_error_t error;
|
||||
|
||||
OSAL_SUBTASK_BEGIN
|
||||
|
||||
//------------- Message Initialize -------------//
|
||||
memcpy(msg_payload, &msg_init, sizeof(rndis_msg_initialize_t));
|
||||
STASK_INVOKE(
|
||||
send_message_get_response_subtask( dev_addr, p_cdc,
|
||||
msg_payload, sizeof(rndis_msg_initialize_t),
|
||||
msg_payload),
|
||||
error
|
||||
);
|
||||
if ( TUSB_ERROR_NONE != error ) STASK_RETURN(error);
|
||||
|
||||
// TODO currently not support multiple data packets per xfer
|
||||
rndis_msg_initialize_cmplt_t * const p_init_cmpt = (rndis_msg_initialize_cmplt_t *) msg_payload;
|
||||
STASK_ASSERT(p_init_cmpt->type == RNDIS_MSG_INITIALIZE_CMPLT && p_init_cmpt->status == RNDIS_STATUS_SUCCESS &&
|
||||
p_init_cmpt->max_packet_per_xfer == 1 && p_init_cmpt->max_xfer_size <= RNDIS_MSG_PAYLOAD_MAX);
|
||||
rndish_data[dev_addr-1].max_xfer_size = p_init_cmpt->max_xfer_size;
|
||||
|
||||
//------------- Message Query 802.3 Permanent Address -------------//
|
||||
memcpy(msg_payload, &msg_query_permanent_addr, sizeof(rndis_msg_query_t));
|
||||
tu_memclr(msg_payload + sizeof(rndis_msg_query_t), 6); // 6 bytes for MAC address
|
||||
|
||||
STASK_INVOKE(
|
||||
send_message_get_response_subtask( dev_addr, p_cdc,
|
||||
msg_payload, sizeof(rndis_msg_query_t) + 6,
|
||||
msg_payload),
|
||||
error
|
||||
);
|
||||
if ( TUSB_ERROR_NONE != error ) STASK_RETURN(error);
|
||||
|
||||
rndis_msg_query_cmplt_t * const p_query_cmpt = (rndis_msg_query_cmplt_t *) msg_payload;
|
||||
STASK_ASSERT(p_query_cmpt->type == RNDIS_MSG_QUERY_CMPLT && p_query_cmpt->status == RNDIS_STATUS_SUCCESS);
|
||||
memcpy(rndish_data[dev_addr-1].mac_address, msg_payload + 8 + p_query_cmpt->buffer_offset, 6);
|
||||
|
||||
//------------- Set OID_GEN_CURRENT_PACKET_FILTER to (DIRECTED | MULTICAST | BROADCAST) -------------//
|
||||
memcpy(msg_payload, &msg_set_packet_filter, sizeof(rndis_msg_set_t));
|
||||
tu_memclr(msg_payload + sizeof(rndis_msg_set_t), 4); // 4 bytes for filter flags
|
||||
((rndis_msg_set_t*) msg_payload)->oid_buffer[0] = (RNDIS_PACKET_TYPE_DIRECTED | RNDIS_PACKET_TYPE_MULTICAST | RNDIS_PACKET_TYPE_BROADCAST);
|
||||
|
||||
STASK_INVOKE(
|
||||
send_message_get_response_subtask( dev_addr, p_cdc,
|
||||
msg_payload, sizeof(rndis_msg_set_t) + 4,
|
||||
msg_payload),
|
||||
error
|
||||
);
|
||||
if ( TUSB_ERROR_NONE != error ) STASK_RETURN(error);
|
||||
|
||||
rndis_msg_set_cmplt_t * const p_set_cmpt = (rndis_msg_set_cmplt_t *) msg_payload;
|
||||
STASK_ASSERT(p_set_cmpt->type == RNDIS_MSG_SET_CMPLT && p_set_cmpt->status == RNDIS_STATUS_SUCCESS);
|
||||
|
||||
tusbh_cdc_rndis_mounted_cb(dev_addr);
|
||||
|
||||
OSAL_SUBTASK_END
|
||||
}
|
||||
|
||||
void rndish_xfer_isr(cdch_data_t *p_cdc, pipe_handle_t pipe_hdl, xfer_result_t event, uint32_t xferred_bytes)
|
||||
{
|
||||
if ( pipehandle_is_equal(pipe_hdl, p_cdc->pipe_notification) )
|
||||
{
|
||||
osal_semaphore_post( rndish_data[pipe_hdl.dev_addr-1].sem_notification_hdl );
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// INTERNAL & HELPER
|
||||
//--------------------------------------------------------------------+
|
||||
static tusb_error_t send_message_get_response_subtask( uint8_t dev_addr, cdch_data_t *p_cdc,
|
||||
uint8_t * p_mess, uint32_t mess_length,
|
||||
uint8_t *p_response)
|
||||
{
|
||||
tusb_error_t error;
|
||||
|
||||
OSAL_SUBTASK_BEGIN
|
||||
|
||||
//------------- Send RNDIS Control Message -------------//
|
||||
STASK_INVOKE(
|
||||
usbh_control_xfer_subtask( dev_addr, bm_request_type(TUSB_DIR_OUT, TUSB_REQ_TYPE_CLASS, TUSB_REQ_RCPT_INTERFACE),
|
||||
CDC_REQUEST_SEND_ENCAPSULATED_COMMAND, 0, p_cdc->interface_number,
|
||||
mess_length, p_mess),
|
||||
error
|
||||
);
|
||||
if ( TUSB_ERROR_NONE != error ) STASK_RETURN(error);
|
||||
|
||||
//------------- waiting for Response Available notification -------------//
|
||||
(void) usbh_edpt_xfer(p_cdc->pipe_notification, msg_notification[dev_addr-1], 8);
|
||||
osal_semaphore_wait(rndish_data[dev_addr-1].sem_notification_hdl, OSAL_TIMEOUT_NORMAL, &error);
|
||||
if ( TUSB_ERROR_NONE != error ) STASK_RETURN(error);
|
||||
STASK_ASSERT(msg_notification[dev_addr-1][0] == 1);
|
||||
|
||||
//------------- Get RNDIS Message Initialize Complete -------------//
|
||||
STASK_INVOKE(
|
||||
usbh_control_xfer_subtask( dev_addr, bm_request_type(TUSB_DIR_IN, TUSB_REQ_TYPE_CLASS, TUSB_REQ_RCPT_INTERFACE),
|
||||
CDC_REQUEST_GET_ENCAPSULATED_RESPONSE, 0, p_cdc->interface_number,
|
||||
RNDIS_MSG_PAYLOAD_MAX, p_response),
|
||||
error
|
||||
);
|
||||
if ( TUSB_ERROR_NONE != error ) STASK_RETURN(error);
|
||||
|
||||
OSAL_SUBTASK_END
|
||||
}
|
||||
|
||||
//static tusb_error_t send_process_msg_initialize_subtask(uint8_t dev_addr, cdch_data_t *p_cdc)
|
||||
//{
|
||||
// tusb_error_t error;
|
||||
//
|
||||
// OSAL_SUBTASK_BEGIN
|
||||
//
|
||||
// *((rndis_msg_initialize_t*) msg_payload) = (rndis_msg_initialize_t)
|
||||
// {
|
||||
// .type = RNDIS_MSG_INITIALIZE,
|
||||
// .length = sizeof(rndis_msg_initialize_t),
|
||||
// .request_id = 1, // TODO should use some magic number
|
||||
// .major_version = 1,
|
||||
// .minor_version = 0,
|
||||
// .max_xfer_size = 0x4000 // TODO mimic windows
|
||||
// };
|
||||
//
|
||||
//
|
||||
//
|
||||
// OSAL_SUBTASK_END
|
||||
//}
|
||||
#endif
|
|
@ -0,0 +1,63 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
/** \ingroup CDC_RNDIS
|
||||
* \defgroup CDC_RNSID_Host Host
|
||||
* @{ */
|
||||
|
||||
#ifndef _TUSB_CDC_RNDIS_HOST_H_
|
||||
#define _TUSB_CDC_RNDIS_HOST_H_
|
||||
|
||||
#include "common/tusb_common.h"
|
||||
#include "host/usbh.h"
|
||||
#include "cdc_rndis.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// INTERNAL RNDIS-CDC Driver API
|
||||
//--------------------------------------------------------------------+
|
||||
typedef struct {
|
||||
OSAL_SEM_DEF(semaphore_notification);
|
||||
osal_semaphore_handle_t sem_notification_hdl; // used to wait on notification pipe
|
||||
uint32_t max_xfer_size; // got from device's msg initialize complete
|
||||
uint8_t mac_address[6];
|
||||
}rndish_data_t;
|
||||
|
||||
void rndish_init(void);
|
||||
bool rndish_open_subtask(uint8_t dev_addr, cdch_data_t *p_cdc);
|
||||
void rndish_xfer_isr(cdch_data_t *p_cdc, pipe_handle_t pipe_hdl, xfer_result_t event, uint32_t xferred_bytes);
|
||||
void rndish_close(uint8_t dev_addr);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _TUSB_CDC_RNDIS_HOST_H_ */
|
||||
|
||||
/** @} */
|
|
@ -0,0 +1,62 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2023 Ha Thach (thach@tinyusb.org) for Adafruit Industries
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef TUSB_CP210X_H
|
||||
#define TUSB_CP210X_H
|
||||
|
||||
// Protocol details can be found at AN571: CP210x Virtual COM Port Interface
|
||||
// https://www.silabs.com/documents/public/application-notes/AN571.pdf
|
||||
|
||||
#define TU_CP210X_VID 0x10C4
|
||||
|
||||
/* Config request codes */
|
||||
#define CP210X_IFC_ENABLE 0x00
|
||||
#define CP210X_SET_BAUDDIV 0x01
|
||||
#define CP210X_GET_BAUDDIV 0x02
|
||||
#define CP210X_SET_LINE_CTL 0x03 // Set parity, data bits, stop bits
|
||||
#define CP210X_GET_LINE_CTL 0x04
|
||||
#define CP210X_SET_BREAK 0x05
|
||||
#define CP210X_IMM_CHAR 0x06
|
||||
#define CP210X_SET_MHS 0x07 // Set DTR, RTS
|
||||
#define CP210X_GET_MDMSTS 0x08 // Get modem status (DTR, RTS, CTS, DSR, RI, DCD)
|
||||
#define CP210X_SET_XON 0x09
|
||||
#define CP210X_SET_XOFF 0x0A
|
||||
#define CP210X_SET_EVENTMASK 0x0B
|
||||
#define CP210X_GET_EVENTMASK 0x0C
|
||||
#define CP210X_SET_CHAR 0x0D
|
||||
#define CP210X_GET_CHARS 0x0E
|
||||
#define CP210X_GET_PROPS 0x0F
|
||||
#define CP210X_GET_COMM_STATUS 0x10
|
||||
#define CP210X_RESET 0x11
|
||||
#define CP210X_PURGE 0x12
|
||||
#define CP210X_SET_FLOW 0x13
|
||||
#define CP210X_GET_FLOW 0x14
|
||||
#define CP210X_EMBED_EVENTS 0x15
|
||||
#define CP210X_GET_EVENTSTATE 0x16
|
||||
#define CP210X_SET_CHARS 0x19
|
||||
#define CP210X_GET_BAUDRATE 0x1D
|
||||
#define CP210X_SET_BAUDRATE 0x1E
|
||||
#define CP210X_VENDOR_SPECIFIC 0xFF // GPIO, Recipient must be Device
|
||||
|
||||
#endif //TUSB_CP210X_H
|
|
@ -0,0 +1,246 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2023 Ha Thach (thach@tinyusb.org) for Adafruit Industries
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef TUSB_FTDI_SIO_H
|
||||
#define TUSB_FTDI_SIO_H
|
||||
|
||||
// VID for matching FTDI devices
|
||||
#define TU_FTDI_VID 0x0403
|
||||
|
||||
// Commands
|
||||
#define FTDI_SIO_RESET 0 /* Reset the port */
|
||||
#define FTDI_SIO_MODEM_CTRL 1 /* Set the modem control register */
|
||||
#define FTDI_SIO_SET_FLOW_CTRL 2 /* Set flow control register */
|
||||
#define FTDI_SIO_SET_BAUD_RATE 3 /* Set baud rate */
|
||||
#define FTDI_SIO_SET_DATA 4 /* Set the data characteristics of the port */
|
||||
#define FTDI_SIO_GET_MODEM_STATUS 5 /* Retrieve current value of modem status register */
|
||||
#define FTDI_SIO_SET_EVENT_CHAR 6 /* Set the event character */
|
||||
#define FTDI_SIO_SET_ERROR_CHAR 7 /* Set the error character */
|
||||
#define FTDI_SIO_SET_LATENCY_TIMER 9 /* Set the latency timer */
|
||||
#define FTDI_SIO_GET_LATENCY_TIMER 0x0a /* Get the latency timer */
|
||||
#define FTDI_SIO_SET_BITMODE 0x0b /* Set bitbang mode */
|
||||
#define FTDI_SIO_READ_PINS 0x0c /* Read immediate value of pins */
|
||||
#define FTDI_SIO_READ_EEPROM 0x90 /* Read EEPROM */
|
||||
|
||||
/* FTDI_SIO_RESET */
|
||||
#define FTDI_SIO_RESET_SIO 0
|
||||
#define FTDI_SIO_RESET_PURGE_RX 1
|
||||
#define FTDI_SIO_RESET_PURGE_TX 2
|
||||
|
||||
/*
|
||||
* BmRequestType: 0100 0000B
|
||||
* bRequest: FTDI_SIO_RESET
|
||||
* wValue: Control Value
|
||||
* 0 = Reset SIO
|
||||
* 1 = Purge RX buffer
|
||||
* 2 = Purge TX buffer
|
||||
* wIndex: Port
|
||||
* wLength: 0
|
||||
* Data: None
|
||||
*
|
||||
* The Reset SIO command has this effect:
|
||||
*
|
||||
* Sets flow control set to 'none'
|
||||
* Event char = $0D
|
||||
* Event trigger = disabled
|
||||
* Purge RX buffer
|
||||
* Purge TX buffer
|
||||
* Clear DTR
|
||||
* Clear RTS
|
||||
* baud and data format not reset
|
||||
*
|
||||
* The Purge RX and TX buffer commands affect nothing except the buffers
|
||||
*
|
||||
*/
|
||||
|
||||
/* FTDI_SIO_MODEM_CTRL */
|
||||
/*
|
||||
* BmRequestType: 0100 0000B
|
||||
* bRequest: FTDI_SIO_MODEM_CTRL
|
||||
* wValue: ControlValue (see below)
|
||||
* wIndex: Port
|
||||
* wLength: 0
|
||||
* Data: None
|
||||
*
|
||||
* NOTE: If the device is in RTS/CTS flow control, the RTS set by this
|
||||
* command will be IGNORED without an error being returned
|
||||
* Also - you can not set DTR and RTS with one control message
|
||||
*/
|
||||
|
||||
#define FTDI_SIO_SET_DTR_MASK 0x1
|
||||
#define FTDI_SIO_SET_DTR_HIGH ((FTDI_SIO_SET_DTR_MASK << 8) | 1)
|
||||
#define FTDI_SIO_SET_DTR_LOW ((FTDI_SIO_SET_DTR_MASK << 8) | 0)
|
||||
#define FTDI_SIO_SET_RTS_MASK 0x2
|
||||
#define FTDI_SIO_SET_RTS_HIGH ((FTDI_SIO_SET_RTS_MASK << 8) | 2)
|
||||
#define FTDI_SIO_SET_RTS_LOW ((FTDI_SIO_SET_RTS_MASK << 8) | 0)
|
||||
|
||||
/*
|
||||
* ControlValue
|
||||
* B0 DTR state
|
||||
* 0 = reset
|
||||
* 1 = set
|
||||
* B1 RTS state
|
||||
* 0 = reset
|
||||
* 1 = set
|
||||
* B2..7 Reserved
|
||||
* B8 DTR state enable
|
||||
* 0 = ignore
|
||||
* 1 = use DTR state
|
||||
* B9 RTS state enable
|
||||
* 0 = ignore
|
||||
* 1 = use RTS state
|
||||
* B10..15 Reserved
|
||||
*/
|
||||
|
||||
/* FTDI_SIO_SET_FLOW_CTRL */
|
||||
#define FTDI_SIO_DISABLE_FLOW_CTRL 0x0
|
||||
#define FTDI_SIO_RTS_CTS_HS (0x1 << 8)
|
||||
#define FTDI_SIO_DTR_DSR_HS (0x2 << 8)
|
||||
#define FTDI_SIO_XON_XOFF_HS (0x4 << 8)
|
||||
|
||||
/*
|
||||
* BmRequestType: 0100 0000b
|
||||
* bRequest: FTDI_SIO_SET_FLOW_CTRL
|
||||
* wValue: Xoff/Xon
|
||||
* wIndex: Protocol/Port - hIndex is protocol / lIndex is port
|
||||
* wLength: 0
|
||||
* Data: None
|
||||
*
|
||||
* hIndex protocol is:
|
||||
* B0 Output handshaking using RTS/CTS
|
||||
* 0 = disabled
|
||||
* 1 = enabled
|
||||
* B1 Output handshaking using DTR/DSR
|
||||
* 0 = disabled
|
||||
* 1 = enabled
|
||||
* B2 Xon/Xoff handshaking
|
||||
* 0 = disabled
|
||||
* 1 = enabled
|
||||
*
|
||||
* A value of zero in the hIndex field disables handshaking
|
||||
*
|
||||
* If Xon/Xoff handshaking is specified, the hValue field should contain the
|
||||
* XOFF character and the lValue field contains the XON character.
|
||||
*/
|
||||
|
||||
/* FTDI_SIO_SET_BAUD_RATE */
|
||||
/*
|
||||
* BmRequestType: 0100 0000B
|
||||
* bRequest: FTDI_SIO_SET_BAUDRATE
|
||||
* wValue: BaudDivisor value - see below
|
||||
* wIndex: Port
|
||||
* wLength: 0
|
||||
* Data: None
|
||||
* The BaudDivisor values are calculated as follows (too complicated):
|
||||
*/
|
||||
|
||||
/* FTDI_SIO_SET_DATA */
|
||||
#define FTDI_SIO_SET_DATA_PARITY_NONE (0x0 << 8)
|
||||
#define FTDI_SIO_SET_DATA_PARITY_ODD (0x1 << 8)
|
||||
#define FTDI_SIO_SET_DATA_PARITY_EVEN (0x2 << 8)
|
||||
#define FTDI_SIO_SET_DATA_PARITY_MARK (0x3 << 8)
|
||||
#define FTDI_SIO_SET_DATA_PARITY_SPACE (0x4 << 8)
|
||||
#define FTDI_SIO_SET_DATA_STOP_BITS_1 (0x0 << 11)
|
||||
#define FTDI_SIO_SET_DATA_STOP_BITS_15 (0x1 << 11)
|
||||
#define FTDI_SIO_SET_DATA_STOP_BITS_2 (0x2 << 11)
|
||||
#define FTDI_SIO_SET_BREAK (0x1 << 14)
|
||||
|
||||
/*
|
||||
* BmRequestType: 0100 0000B
|
||||
* bRequest: FTDI_SIO_SET_DATA
|
||||
* wValue: Data characteristics (see below)
|
||||
* wIndex: Port
|
||||
* wLength: 0
|
||||
* Data: No
|
||||
*
|
||||
* Data characteristics
|
||||
*
|
||||
* B0..7 Number of data bits
|
||||
* B8..10 Parity
|
||||
* 0 = None
|
||||
* 1 = Odd
|
||||
* 2 = Even
|
||||
* 3 = Mark
|
||||
* 4 = Space
|
||||
* B11..13 Stop Bits
|
||||
* 0 = 1
|
||||
* 1 = 1.5
|
||||
* 2 = 2
|
||||
* B14
|
||||
* 1 = TX ON (break)
|
||||
* 0 = TX OFF (normal state)
|
||||
* B15 Reserved
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* DATA FORMAT
|
||||
*
|
||||
* IN Endpoint
|
||||
*
|
||||
* The device reserves the first two bytes of data on this endpoint to contain
|
||||
* the current values of the modem and line status registers. In the absence of
|
||||
* data, the device generates a message consisting of these two status bytes
|
||||
* every 40 ms
|
||||
*
|
||||
* Byte 0: Modem Status
|
||||
*
|
||||
* Offset Description
|
||||
* B0 Reserved - must be 1
|
||||
* B1 Reserved - must be 0
|
||||
* B2 Reserved - must be 0
|
||||
* B3 Reserved - must be 0
|
||||
* B4 Clear to Send (CTS)
|
||||
* B5 Data Set Ready (DSR)
|
||||
* B6 Ring Indicator (RI)
|
||||
* B7 Receive Line Signal Detect (RLSD)
|
||||
*
|
||||
* Byte 1: Line Status
|
||||
*
|
||||
* Offset Description
|
||||
* B0 Data Ready (DR)
|
||||
* B1 Overrun Error (OE)
|
||||
* B2 Parity Error (PE)
|
||||
* B3 Framing Error (FE)
|
||||
* B4 Break Interrupt (BI)
|
||||
* B5 Transmitter Holding Register (THRE)
|
||||
* B6 Transmitter Empty (TEMT)
|
||||
* B7 Error in RCVR FIFO
|
||||
*
|
||||
*/
|
||||
#define FTDI_RS0_CTS (1 << 4)
|
||||
#define FTDI_RS0_DSR (1 << 5)
|
||||
#define FTDI_RS0_RI (1 << 6)
|
||||
#define FTDI_RS0_RLSD (1 << 7)
|
||||
|
||||
#define FTDI_RS_DR 1
|
||||
#define FTDI_RS_OE (1<<1)
|
||||
#define FTDI_RS_PE (1<<2)
|
||||
#define FTDI_RS_FE (1<<3)
|
||||
#define FTDI_RS_BI (1<<4)
|
||||
#define FTDI_RS_THRE (1<<5)
|
||||
#define FTDI_RS_TEMT (1<<6)
|
||||
#define FTDI_RS_FIFO (1<<7)
|
||||
|
||||
#endif //TUSB_FTDI_SIO_H
|
Binary file not shown.
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,415 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#include "tusb_option.h"
|
||||
|
||||
#if (CFG_TUD_ENABLED && CFG_TUD_HID)
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// INCLUDE
|
||||
//--------------------------------------------------------------------+
|
||||
#include "device/usbd.h"
|
||||
#include "device/usbd_pvt.h"
|
||||
|
||||
#include "hid_device.h"
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// MACRO CONSTANT TYPEDEF
|
||||
//--------------------------------------------------------------------+
|
||||
typedef struct
|
||||
{
|
||||
uint8_t itf_num;
|
||||
uint8_t ep_in;
|
||||
uint8_t ep_out; // optional Out endpoint
|
||||
uint8_t itf_protocol; // Boot mouse or keyboard
|
||||
|
||||
uint8_t protocol_mode; // Boot (0) or Report protocol (1)
|
||||
uint8_t idle_rate; // up to application to handle idle rate
|
||||
uint16_t report_desc_len;
|
||||
|
||||
CFG_TUSB_MEM_ALIGN uint8_t epin_buf[CFG_TUD_HID_EP_BUFSIZE];
|
||||
CFG_TUSB_MEM_ALIGN uint8_t epout_buf[CFG_TUD_HID_EP_BUFSIZE];
|
||||
|
||||
// TODO save hid descriptor since host can specifically request this after enumeration
|
||||
// Note: HID descriptor may be not available from application after enumeration
|
||||
tusb_hid_descriptor_hid_t const * hid_descriptor;
|
||||
} hidd_interface_t;
|
||||
|
||||
CFG_TUD_MEM_SECTION tu_static hidd_interface_t _hidd_itf[CFG_TUD_HID];
|
||||
|
||||
/*------------- Helpers -------------*/
|
||||
static inline uint8_t get_index_by_itfnum(uint8_t itf_num)
|
||||
{
|
||||
for (uint8_t i=0; i < CFG_TUD_HID; i++ )
|
||||
{
|
||||
if ( itf_num == _hidd_itf[i].itf_num ) return i;
|
||||
}
|
||||
|
||||
return 0xFF;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// APPLICATION API
|
||||
//--------------------------------------------------------------------+
|
||||
bool tud_hid_n_ready(uint8_t instance)
|
||||
{
|
||||
uint8_t const rhport = 0;
|
||||
uint8_t const ep_in = _hidd_itf[instance].ep_in;
|
||||
return tud_ready() && (ep_in != 0) && !usbd_edpt_busy(rhport, ep_in);
|
||||
}
|
||||
|
||||
bool tud_hid_n_report(uint8_t instance, uint8_t report_id, void const* report, uint16_t len)
|
||||
{
|
||||
uint8_t const rhport = 0;
|
||||
hidd_interface_t * p_hid = &_hidd_itf[instance];
|
||||
|
||||
// claim endpoint
|
||||
TU_VERIFY( usbd_edpt_claim(rhport, p_hid->ep_in) );
|
||||
|
||||
// prepare data
|
||||
if (report_id)
|
||||
{
|
||||
p_hid->epin_buf[0] = report_id;
|
||||
TU_VERIFY(0 == tu_memcpy_s(p_hid->epin_buf+1, CFG_TUD_HID_EP_BUFSIZE-1, report, len));
|
||||
len++;
|
||||
}else
|
||||
{
|
||||
TU_VERIFY(0 == tu_memcpy_s(p_hid->epin_buf, CFG_TUD_HID_EP_BUFSIZE, report, len));
|
||||
}
|
||||
|
||||
return usbd_edpt_xfer(rhport, p_hid->ep_in, p_hid->epin_buf, len);
|
||||
}
|
||||
|
||||
uint8_t tud_hid_n_interface_protocol(uint8_t instance)
|
||||
{
|
||||
return _hidd_itf[instance].itf_protocol;
|
||||
}
|
||||
|
||||
uint8_t tud_hid_n_get_protocol(uint8_t instance)
|
||||
{
|
||||
return _hidd_itf[instance].protocol_mode;
|
||||
}
|
||||
|
||||
bool tud_hid_n_keyboard_report(uint8_t instance, uint8_t report_id, uint8_t modifier, uint8_t keycode[6])
|
||||
{
|
||||
hid_keyboard_report_t report;
|
||||
|
||||
report.modifier = modifier;
|
||||
report.reserved = 0;
|
||||
|
||||
if ( keycode )
|
||||
{
|
||||
memcpy(report.keycode, keycode, sizeof(report.keycode));
|
||||
}else
|
||||
{
|
||||
tu_memclr(report.keycode, 6);
|
||||
}
|
||||
|
||||
return tud_hid_n_report(instance, report_id, &report, sizeof(report));
|
||||
}
|
||||
|
||||
bool tud_hid_n_mouse_report(uint8_t instance, uint8_t report_id,
|
||||
uint8_t buttons, int8_t x, int8_t y, int8_t vertical, int8_t horizontal)
|
||||
{
|
||||
hid_mouse_report_t report =
|
||||
{
|
||||
.buttons = buttons,
|
||||
.x = x,
|
||||
.y = y,
|
||||
.wheel = vertical,
|
||||
.pan = horizontal
|
||||
};
|
||||
|
||||
return tud_hid_n_report(instance, report_id, &report, sizeof(report));
|
||||
}
|
||||
|
||||
bool tud_hid_n_gamepad_report(uint8_t instance, uint8_t report_id,
|
||||
int8_t x, int8_t y, int8_t z, int8_t rz, int8_t rx, int8_t ry, uint8_t hat, uint32_t buttons) {
|
||||
hid_gamepad_report_t report =
|
||||
{
|
||||
.x = x,
|
||||
.y = y,
|
||||
.z = z,
|
||||
.rz = rz,
|
||||
.rx = rx,
|
||||
.ry = ry,
|
||||
.hat = hat,
|
||||
.buttons = buttons,
|
||||
};
|
||||
|
||||
return tud_hid_n_report(instance, report_id, &report, sizeof(report));
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// USBD-CLASS API
|
||||
//--------------------------------------------------------------------+
|
||||
void hidd_init(void)
|
||||
{
|
||||
hidd_reset(0);
|
||||
}
|
||||
|
||||
void hidd_reset(uint8_t rhport)
|
||||
{
|
||||
(void) rhport;
|
||||
tu_memclr(_hidd_itf, sizeof(_hidd_itf));
|
||||
}
|
||||
|
||||
uint16_t hidd_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, uint16_t max_len)
|
||||
{
|
||||
TU_VERIFY(TUSB_CLASS_HID == desc_itf->bInterfaceClass, 0);
|
||||
|
||||
// len = interface + hid + n*endpoints
|
||||
uint16_t const drv_len =
|
||||
(uint16_t) (sizeof(tusb_desc_interface_t) + sizeof(tusb_hid_descriptor_hid_t) +
|
||||
desc_itf->bNumEndpoints * sizeof(tusb_desc_endpoint_t));
|
||||
TU_ASSERT(max_len >= drv_len, 0);
|
||||
|
||||
// Find available interface
|
||||
hidd_interface_t * p_hid = NULL;
|
||||
uint8_t hid_id;
|
||||
for(hid_id=0; hid_id<CFG_TUD_HID; hid_id++)
|
||||
{
|
||||
if ( _hidd_itf[hid_id].ep_in == 0 )
|
||||
{
|
||||
p_hid = &_hidd_itf[hid_id];
|
||||
break;
|
||||
}
|
||||
}
|
||||
TU_ASSERT(p_hid, 0);
|
||||
|
||||
uint8_t const *p_desc = (uint8_t const *) desc_itf;
|
||||
|
||||
//------------- HID descriptor -------------//
|
||||
p_desc = tu_desc_next(p_desc);
|
||||
TU_ASSERT(HID_DESC_TYPE_HID == tu_desc_type(p_desc), 0);
|
||||
p_hid->hid_descriptor = (tusb_hid_descriptor_hid_t const *) p_desc;
|
||||
|
||||
//------------- Endpoint Descriptor -------------//
|
||||
p_desc = tu_desc_next(p_desc);
|
||||
TU_ASSERT(usbd_open_edpt_pair(rhport, p_desc, desc_itf->bNumEndpoints, TUSB_XFER_INTERRUPT, &p_hid->ep_out, &p_hid->ep_in), 0);
|
||||
|
||||
if ( desc_itf->bInterfaceSubClass == HID_SUBCLASS_BOOT ) p_hid->itf_protocol = desc_itf->bInterfaceProtocol;
|
||||
|
||||
p_hid->protocol_mode = HID_PROTOCOL_REPORT; // Per Specs: default is report mode
|
||||
p_hid->itf_num = desc_itf->bInterfaceNumber;
|
||||
|
||||
// Use offsetof to avoid pointer to the odd/misaligned address
|
||||
p_hid->report_desc_len = tu_unaligned_read16((uint8_t const*) p_hid->hid_descriptor + offsetof(tusb_hid_descriptor_hid_t, wReportLength));
|
||||
|
||||
// Prepare for output endpoint
|
||||
if (p_hid->ep_out)
|
||||
{
|
||||
if ( !usbd_edpt_xfer(rhport, p_hid->ep_out, p_hid->epout_buf, sizeof(p_hid->epout_buf)) )
|
||||
{
|
||||
TU_LOG_FAILED();
|
||||
TU_BREAKPOINT();
|
||||
}
|
||||
}
|
||||
|
||||
return drv_len;
|
||||
}
|
||||
|
||||
// Invoked when a control transfer occurred on an interface of this class
|
||||
// Driver response accordingly to the request and the transfer stage (setup/data/ack)
|
||||
// return false to stall control endpoint (e.g unsupported request)
|
||||
bool hidd_control_xfer_cb (uint8_t rhport, uint8_t stage, tusb_control_request_t const * request)
|
||||
{
|
||||
TU_VERIFY(request->bmRequestType_bit.recipient == TUSB_REQ_RCPT_INTERFACE);
|
||||
|
||||
uint8_t const hid_itf = get_index_by_itfnum((uint8_t) request->wIndex);
|
||||
TU_VERIFY(hid_itf < CFG_TUD_HID);
|
||||
|
||||
hidd_interface_t* p_hid = &_hidd_itf[hid_itf];
|
||||
|
||||
if (request->bmRequestType_bit.type == TUSB_REQ_TYPE_STANDARD)
|
||||
{
|
||||
//------------- STD Request -------------//
|
||||
if ( stage == CONTROL_STAGE_SETUP )
|
||||
{
|
||||
uint8_t const desc_type = tu_u16_high(request->wValue);
|
||||
//uint8_t const desc_index = tu_u16_low (request->wValue);
|
||||
|
||||
if (request->bRequest == TUSB_REQ_GET_DESCRIPTOR && desc_type == HID_DESC_TYPE_HID)
|
||||
{
|
||||
TU_VERIFY(p_hid->hid_descriptor);
|
||||
TU_VERIFY(tud_control_xfer(rhport, request, (void*)(uintptr_t) p_hid->hid_descriptor, p_hid->hid_descriptor->bLength));
|
||||
}
|
||||
else if (request->bRequest == TUSB_REQ_GET_DESCRIPTOR && desc_type == HID_DESC_TYPE_REPORT)
|
||||
{
|
||||
uint8_t const * desc_report = tud_hid_descriptor_report_cb(hid_itf);
|
||||
tud_control_xfer(rhport, request, (void*)(uintptr_t) desc_report, p_hid->report_desc_len);
|
||||
}
|
||||
else
|
||||
{
|
||||
return false; // stall unsupported request
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (request->bmRequestType_bit.type == TUSB_REQ_TYPE_CLASS)
|
||||
{
|
||||
//------------- Class Specific Request -------------//
|
||||
switch( request->bRequest )
|
||||
{
|
||||
case HID_REQ_CONTROL_GET_REPORT:
|
||||
if ( stage == CONTROL_STAGE_SETUP )
|
||||
{
|
||||
uint8_t const report_type = tu_u16_high(request->wValue);
|
||||
uint8_t const report_id = tu_u16_low(request->wValue);
|
||||
|
||||
uint8_t* report_buf = p_hid->epin_buf;
|
||||
uint16_t req_len = tu_min16(request->wLength, CFG_TUD_HID_EP_BUFSIZE);
|
||||
|
||||
uint16_t xferlen = 0;
|
||||
|
||||
// If host request a specific Report ID, add ID to as 1 byte of response
|
||||
if ( (report_id != HID_REPORT_TYPE_INVALID) && (req_len > 1) )
|
||||
{
|
||||
*report_buf++ = report_id;
|
||||
req_len--;
|
||||
|
||||
xferlen++;
|
||||
}
|
||||
|
||||
xferlen += tud_hid_get_report_cb(hid_itf, report_id, (hid_report_type_t) report_type, report_buf, req_len);
|
||||
TU_ASSERT( xferlen > 0 );
|
||||
|
||||
tud_control_xfer(rhport, request, p_hid->epin_buf, xferlen);
|
||||
}
|
||||
break;
|
||||
|
||||
case HID_REQ_CONTROL_SET_REPORT:
|
||||
if ( stage == CONTROL_STAGE_SETUP )
|
||||
{
|
||||
TU_VERIFY(request->wLength <= sizeof(p_hid->epout_buf));
|
||||
tud_control_xfer(rhport, request, p_hid->epout_buf, request->wLength);
|
||||
}
|
||||
else if ( stage == CONTROL_STAGE_ACK )
|
||||
{
|
||||
uint8_t const report_type = tu_u16_high(request->wValue);
|
||||
uint8_t const report_id = tu_u16_low(request->wValue);
|
||||
|
||||
uint8_t const* report_buf = p_hid->epout_buf;
|
||||
uint16_t report_len = tu_min16(request->wLength, CFG_TUD_HID_EP_BUFSIZE);
|
||||
|
||||
// If host request a specific Report ID, extract report ID in buffer before invoking callback
|
||||
if ( (report_id != HID_REPORT_TYPE_INVALID) && (report_len > 1) && (report_id == report_buf[0]) )
|
||||
{
|
||||
report_buf++;
|
||||
report_len--;
|
||||
}
|
||||
|
||||
tud_hid_set_report_cb(hid_itf, report_id, (hid_report_type_t) report_type, report_buf, report_len);
|
||||
}
|
||||
break;
|
||||
|
||||
case HID_REQ_CONTROL_SET_IDLE:
|
||||
if ( stage == CONTROL_STAGE_SETUP )
|
||||
{
|
||||
p_hid->idle_rate = tu_u16_high(request->wValue);
|
||||
if ( tud_hid_set_idle_cb )
|
||||
{
|
||||
// stall request if callback return false
|
||||
TU_VERIFY( tud_hid_set_idle_cb( hid_itf, p_hid->idle_rate) );
|
||||
}
|
||||
|
||||
tud_control_status(rhport, request);
|
||||
}
|
||||
break;
|
||||
|
||||
case HID_REQ_CONTROL_GET_IDLE:
|
||||
if ( stage == CONTROL_STAGE_SETUP )
|
||||
{
|
||||
// TODO idle rate of report
|
||||
tud_control_xfer(rhport, request, &p_hid->idle_rate, 1);
|
||||
}
|
||||
break;
|
||||
|
||||
case HID_REQ_CONTROL_GET_PROTOCOL:
|
||||
if ( stage == CONTROL_STAGE_SETUP )
|
||||
{
|
||||
tud_control_xfer(rhport, request, &p_hid->protocol_mode, 1);
|
||||
}
|
||||
break;
|
||||
|
||||
case HID_REQ_CONTROL_SET_PROTOCOL:
|
||||
if ( stage == CONTROL_STAGE_SETUP )
|
||||
{
|
||||
tud_control_status(rhport, request);
|
||||
}
|
||||
else if ( stage == CONTROL_STAGE_ACK )
|
||||
{
|
||||
p_hid->protocol_mode = (uint8_t) request->wValue;
|
||||
if (tud_hid_set_protocol_cb)
|
||||
{
|
||||
tud_hid_set_protocol_cb(hid_itf, p_hid->protocol_mode);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
default: return false; // stall unsupported request
|
||||
}
|
||||
}else
|
||||
{
|
||||
return false; // stall unsupported request
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool hidd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes)
|
||||
{
|
||||
(void) result;
|
||||
|
||||
uint8_t instance = 0;
|
||||
hidd_interface_t * p_hid = _hidd_itf;
|
||||
|
||||
// Identify which interface to use
|
||||
for (instance = 0; instance < CFG_TUD_HID; instance++)
|
||||
{
|
||||
p_hid = &_hidd_itf[instance];
|
||||
if ( (ep_addr == p_hid->ep_out) || (ep_addr == p_hid->ep_in) ) break;
|
||||
}
|
||||
TU_ASSERT(instance < CFG_TUD_HID);
|
||||
|
||||
// Sent report successfully
|
||||
if (ep_addr == p_hid->ep_in)
|
||||
{
|
||||
if (tud_hid_report_complete_cb)
|
||||
{
|
||||
tud_hid_report_complete_cb(instance, p_hid->epin_buf, (uint16_t) xferred_bytes);
|
||||
}
|
||||
}
|
||||
// Received report
|
||||
else if (ep_addr == p_hid->ep_out)
|
||||
{
|
||||
tud_hid_set_report_cb(instance, 0, HID_REPORT_TYPE_INVALID, p_hid->epout_buf, (uint16_t) xferred_bytes);
|
||||
TU_ASSERT(usbd_edpt_xfer(rhport, p_hid->ep_out, p_hid->epout_buf, sizeof(p_hid->epout_buf)));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#endif
|
|
@ -0,0 +1,418 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#ifndef _TUSB_HID_DEVICE_H_
|
||||
#define _TUSB_HID_DEVICE_H_
|
||||
|
||||
#include "hid.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Class Driver Default Configure & Validation
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
#if !defined(CFG_TUD_HID_EP_BUFSIZE) & defined(CFG_TUD_HID_BUFSIZE)
|
||||
// TODO warn user to use new name later on
|
||||
// #warning CFG_TUD_HID_BUFSIZE is renamed to CFG_TUD_HID_EP_BUFSIZE, please update to use the new name
|
||||
#define CFG_TUD_HID_EP_BUFSIZE CFG_TUD_HID_BUFSIZE
|
||||
#endif
|
||||
|
||||
#ifndef CFG_TUD_HID_EP_BUFSIZE
|
||||
#define CFG_TUD_HID_EP_BUFSIZE 64
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Application API (Multiple Instances)
|
||||
// CFG_TUD_HID > 1
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Check if the interface is ready to use
|
||||
bool tud_hid_n_ready(uint8_t instance);
|
||||
|
||||
// Get interface supported protocol (bInterfaceProtocol) check out hid_interface_protocol_enum_t for possible values
|
||||
uint8_t tud_hid_n_interface_protocol(uint8_t instance);
|
||||
|
||||
// Get current active protocol: HID_PROTOCOL_BOOT (0) or HID_PROTOCOL_REPORT (1)
|
||||
uint8_t tud_hid_n_get_protocol(uint8_t instance);
|
||||
|
||||
// Send report to host
|
||||
bool tud_hid_n_report(uint8_t instance, uint8_t report_id, void const* report, uint16_t len);
|
||||
|
||||
// KEYBOARD: convenient helper to send keyboard report if application
|
||||
// use template layout report as defined by hid_keyboard_report_t
|
||||
bool tud_hid_n_keyboard_report(uint8_t instance, uint8_t report_id, uint8_t modifier, uint8_t keycode[6]);
|
||||
|
||||
// MOUSE: convenient helper to send mouse report if application
|
||||
// use template layout report as defined by hid_mouse_report_t
|
||||
bool tud_hid_n_mouse_report(uint8_t instance, uint8_t report_id, uint8_t buttons, int8_t x, int8_t y, int8_t vertical, int8_t horizontal);
|
||||
|
||||
// Gamepad: convenient helper to send gamepad report if application
|
||||
// use template layout report TUD_HID_REPORT_DESC_GAMEPAD
|
||||
bool tud_hid_n_gamepad_report(uint8_t instance, uint8_t report_id, int8_t x, int8_t y, int8_t z, int8_t rz, int8_t rx, int8_t ry, uint8_t hat, uint32_t buttons);
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Application API (Single Port)
|
||||
//--------------------------------------------------------------------+
|
||||
static inline bool tud_hid_ready(void);
|
||||
static inline uint8_t tud_hid_interface_protocol(void);
|
||||
static inline uint8_t tud_hid_get_protocol(void);
|
||||
static inline bool tud_hid_report(uint8_t report_id, void const* report, uint16_t len);
|
||||
static inline bool tud_hid_keyboard_report(uint8_t report_id, uint8_t modifier, uint8_t keycode[6]);
|
||||
static inline bool tud_hid_mouse_report(uint8_t report_id, uint8_t buttons, int8_t x, int8_t y, int8_t vertical, int8_t horizontal);
|
||||
static inline bool tud_hid_gamepad_report(uint8_t report_id, int8_t x, int8_t y, int8_t z, int8_t rz, int8_t rx, int8_t ry, uint8_t hat, uint32_t buttons);
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Callbacks (Weak is optional)
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Invoked when received GET HID REPORT DESCRIPTOR request
|
||||
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
||||
uint8_t const * tud_hid_descriptor_report_cb(uint8_t instance);
|
||||
|
||||
// Invoked when received GET_REPORT control request
|
||||
// Application must fill buffer report's content and return its length.
|
||||
// Return zero will cause the stack to STALL request
|
||||
uint16_t tud_hid_get_report_cb(uint8_t instance, uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen);
|
||||
|
||||
// Invoked when received SET_REPORT control request or
|
||||
// received data on OUT endpoint ( Report ID = 0, Type = 0 )
|
||||
void tud_hid_set_report_cb(uint8_t instance, uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize);
|
||||
|
||||
// Invoked when received SET_PROTOCOL request
|
||||
// protocol is either HID_PROTOCOL_BOOT (0) or HID_PROTOCOL_REPORT (1)
|
||||
TU_ATTR_WEAK void tud_hid_set_protocol_cb(uint8_t instance, uint8_t protocol);
|
||||
|
||||
// Invoked when received SET_IDLE request. return false will stall the request
|
||||
// - Idle Rate = 0 : only send report if there is changes, i.e skip duplication
|
||||
// - Idle Rate > 0 : skip duplication, but send at least 1 report every idle rate (in unit of 4 ms).
|
||||
TU_ATTR_WEAK bool tud_hid_set_idle_cb(uint8_t instance, uint8_t idle_rate);
|
||||
|
||||
// Invoked when sent REPORT successfully to host
|
||||
// Application can use this to send the next report
|
||||
// Note: For composite reports, report[0] is report ID
|
||||
TU_ATTR_WEAK void tud_hid_report_complete_cb(uint8_t instance, uint8_t const* report, uint16_t len);
|
||||
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Inline Functions
|
||||
//--------------------------------------------------------------------+
|
||||
static inline bool tud_hid_ready(void)
|
||||
{
|
||||
return tud_hid_n_ready(0);
|
||||
}
|
||||
|
||||
static inline uint8_t tud_hid_interface_protocol(void)
|
||||
{
|
||||
return tud_hid_n_interface_protocol(0);
|
||||
}
|
||||
|
||||
static inline uint8_t tud_hid_get_protocol(void)
|
||||
{
|
||||
return tud_hid_n_get_protocol(0);
|
||||
}
|
||||
|
||||
static inline bool tud_hid_report(uint8_t report_id, void const* report, uint16_t len)
|
||||
{
|
||||
return tud_hid_n_report(0, report_id, report, len);
|
||||
}
|
||||
|
||||
static inline bool tud_hid_keyboard_report(uint8_t report_id, uint8_t modifier, uint8_t keycode[6])
|
||||
{
|
||||
return tud_hid_n_keyboard_report(0, report_id, modifier, keycode);
|
||||
}
|
||||
|
||||
static inline bool tud_hid_mouse_report(uint8_t report_id, uint8_t buttons, int8_t x, int8_t y, int8_t vertical, int8_t horizontal)
|
||||
{
|
||||
return tud_hid_n_mouse_report(0, report_id, buttons, x, y, vertical, horizontal);
|
||||
}
|
||||
|
||||
static inline bool tud_hid_gamepad_report(uint8_t report_id, int8_t x, int8_t y, int8_t z, int8_t rz, int8_t rx, int8_t ry, uint8_t hat, uint32_t buttons)
|
||||
{
|
||||
return tud_hid_n_gamepad_report(0, report_id, x, y, z, rz, rx, ry, hat, buttons);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------+
|
||||
* HID Report Descriptor Template
|
||||
*
|
||||
* Convenient for declaring popular HID device (keyboard, mouse, consumer,
|
||||
* gamepad etc...). Templates take "HID_REPORT_ID(n)" as input, leave
|
||||
* empty if multiple reports is not used
|
||||
*
|
||||
* - Only 1 report: no parameter
|
||||
* uint8_t const report_desc[] = { TUD_HID_REPORT_DESC_KEYBOARD() };
|
||||
*
|
||||
* - Multiple Reports: "HID_REPORT_ID(ID)" must be passed to template
|
||||
* uint8_t const report_desc[] =
|
||||
* {
|
||||
* TUD_HID_REPORT_DESC_KEYBOARD( HID_REPORT_ID(1) ) ,
|
||||
* TUD_HID_REPORT_DESC_MOUSE ( HID_REPORT_ID(2) )
|
||||
* };
|
||||
*--------------------------------------------------------------------*/
|
||||
|
||||
// Keyboard Report Descriptor Template
|
||||
#define TUD_HID_REPORT_DESC_KEYBOARD(...) \
|
||||
HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\
|
||||
HID_USAGE ( HID_USAGE_DESKTOP_KEYBOARD ) ,\
|
||||
HID_COLLECTION ( HID_COLLECTION_APPLICATION ) ,\
|
||||
/* Report ID if any */\
|
||||
__VA_ARGS__ \
|
||||
/* 8 bits Modifier Keys (Shift, Control, Alt) */ \
|
||||
HID_USAGE_PAGE ( HID_USAGE_PAGE_KEYBOARD ) ,\
|
||||
HID_USAGE_MIN ( 224 ) ,\
|
||||
HID_USAGE_MAX ( 231 ) ,\
|
||||
HID_LOGICAL_MIN ( 0 ) ,\
|
||||
HID_LOGICAL_MAX ( 1 ) ,\
|
||||
HID_REPORT_COUNT ( 8 ) ,\
|
||||
HID_REPORT_SIZE ( 1 ) ,\
|
||||
HID_INPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ) ,\
|
||||
/* 8 bit reserved */ \
|
||||
HID_REPORT_COUNT ( 1 ) ,\
|
||||
HID_REPORT_SIZE ( 8 ) ,\
|
||||
HID_INPUT ( HID_CONSTANT ) ,\
|
||||
/* Output 5-bit LED Indicator Kana | Compose | ScrollLock | CapsLock | NumLock */ \
|
||||
HID_USAGE_PAGE ( HID_USAGE_PAGE_LED ) ,\
|
||||
HID_USAGE_MIN ( 1 ) ,\
|
||||
HID_USAGE_MAX ( 5 ) ,\
|
||||
HID_REPORT_COUNT ( 5 ) ,\
|
||||
HID_REPORT_SIZE ( 1 ) ,\
|
||||
HID_OUTPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ) ,\
|
||||
/* led padding */ \
|
||||
HID_REPORT_COUNT ( 1 ) ,\
|
||||
HID_REPORT_SIZE ( 3 ) ,\
|
||||
HID_OUTPUT ( HID_CONSTANT ) ,\
|
||||
/* 6-byte Keycodes */ \
|
||||
HID_USAGE_PAGE ( HID_USAGE_PAGE_KEYBOARD ) ,\
|
||||
HID_USAGE_MIN ( 0 ) ,\
|
||||
HID_USAGE_MAX_N ( 255, 2 ) ,\
|
||||
HID_LOGICAL_MIN ( 0 ) ,\
|
||||
HID_LOGICAL_MAX_N( 255, 2 ) ,\
|
||||
HID_REPORT_COUNT ( 6 ) ,\
|
||||
HID_REPORT_SIZE ( 8 ) ,\
|
||||
HID_INPUT ( HID_DATA | HID_ARRAY | HID_ABSOLUTE ) ,\
|
||||
HID_COLLECTION_END \
|
||||
|
||||
// Mouse Report Descriptor Template
|
||||
#define TUD_HID_REPORT_DESC_MOUSE(...) \
|
||||
HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\
|
||||
HID_USAGE ( HID_USAGE_DESKTOP_MOUSE ) ,\
|
||||
HID_COLLECTION ( HID_COLLECTION_APPLICATION ) ,\
|
||||
/* Report ID if any */\
|
||||
__VA_ARGS__ \
|
||||
HID_USAGE ( HID_USAGE_DESKTOP_POINTER ) ,\
|
||||
HID_COLLECTION ( HID_COLLECTION_PHYSICAL ) ,\
|
||||
HID_USAGE_PAGE ( HID_USAGE_PAGE_BUTTON ) ,\
|
||||
HID_USAGE_MIN ( 1 ) ,\
|
||||
HID_USAGE_MAX ( 5 ) ,\
|
||||
HID_LOGICAL_MIN ( 0 ) ,\
|
||||
HID_LOGICAL_MAX ( 1 ) ,\
|
||||
/* Left, Right, Middle, Backward, Forward buttons */ \
|
||||
HID_REPORT_COUNT( 5 ) ,\
|
||||
HID_REPORT_SIZE ( 1 ) ,\
|
||||
HID_INPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ) ,\
|
||||
/* 3 bit padding */ \
|
||||
HID_REPORT_COUNT( 1 ) ,\
|
||||
HID_REPORT_SIZE ( 3 ) ,\
|
||||
HID_INPUT ( HID_CONSTANT ) ,\
|
||||
HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\
|
||||
/* X, Y position [-127, 127] */ \
|
||||
HID_USAGE ( HID_USAGE_DESKTOP_X ) ,\
|
||||
HID_USAGE ( HID_USAGE_DESKTOP_Y ) ,\
|
||||
HID_LOGICAL_MIN ( 0x81 ) ,\
|
||||
HID_LOGICAL_MAX ( 0x7f ) ,\
|
||||
HID_REPORT_COUNT( 2 ) ,\
|
||||
HID_REPORT_SIZE ( 8 ) ,\
|
||||
HID_INPUT ( HID_DATA | HID_VARIABLE | HID_RELATIVE ) ,\
|
||||
/* Verital wheel scroll [-127, 127] */ \
|
||||
HID_USAGE ( HID_USAGE_DESKTOP_WHEEL ) ,\
|
||||
HID_LOGICAL_MIN ( 0x81 ) ,\
|
||||
HID_LOGICAL_MAX ( 0x7f ) ,\
|
||||
HID_REPORT_COUNT( 1 ) ,\
|
||||
HID_REPORT_SIZE ( 8 ) ,\
|
||||
HID_INPUT ( HID_DATA | HID_VARIABLE | HID_RELATIVE ) ,\
|
||||
HID_USAGE_PAGE ( HID_USAGE_PAGE_CONSUMER ), \
|
||||
/* Horizontal wheel scroll [-127, 127] */ \
|
||||
HID_USAGE_N ( HID_USAGE_CONSUMER_AC_PAN, 2 ), \
|
||||
HID_LOGICAL_MIN ( 0x81 ), \
|
||||
HID_LOGICAL_MAX ( 0x7f ), \
|
||||
HID_REPORT_COUNT( 1 ), \
|
||||
HID_REPORT_SIZE ( 8 ), \
|
||||
HID_INPUT ( HID_DATA | HID_VARIABLE | HID_RELATIVE ), \
|
||||
HID_COLLECTION_END , \
|
||||
HID_COLLECTION_END \
|
||||
|
||||
// Consumer Control Report Descriptor Template
|
||||
#define TUD_HID_REPORT_DESC_CONSUMER(...) \
|
||||
HID_USAGE_PAGE ( HID_USAGE_PAGE_CONSUMER ) ,\
|
||||
HID_USAGE ( HID_USAGE_CONSUMER_CONTROL ) ,\
|
||||
HID_COLLECTION ( HID_COLLECTION_APPLICATION ) ,\
|
||||
/* Report ID if any */\
|
||||
__VA_ARGS__ \
|
||||
HID_LOGICAL_MIN ( 0x00 ) ,\
|
||||
HID_LOGICAL_MAX_N( 0x03FF, 2 ) ,\
|
||||
HID_USAGE_MIN ( 0x00 ) ,\
|
||||
HID_USAGE_MAX_N ( 0x03FF, 2 ) ,\
|
||||
HID_REPORT_COUNT ( 1 ) ,\
|
||||
HID_REPORT_SIZE ( 16 ) ,\
|
||||
HID_INPUT ( HID_DATA | HID_ARRAY | HID_ABSOLUTE ) ,\
|
||||
HID_COLLECTION_END \
|
||||
|
||||
/* System Control Report Descriptor Template
|
||||
* 0x00 - do nothing
|
||||
* 0x01 - Power Off
|
||||
* 0x02 - Standby
|
||||
* 0x03 - Wake Host
|
||||
*/
|
||||
#define TUD_HID_REPORT_DESC_SYSTEM_CONTROL(...) \
|
||||
HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\
|
||||
HID_USAGE ( HID_USAGE_DESKTOP_SYSTEM_CONTROL ) ,\
|
||||
HID_COLLECTION ( HID_COLLECTION_APPLICATION ) ,\
|
||||
/* Report ID if any */\
|
||||
__VA_ARGS__ \
|
||||
/* 2 bit system power control */ \
|
||||
HID_LOGICAL_MIN ( 1 ) ,\
|
||||
HID_LOGICAL_MAX ( 3 ) ,\
|
||||
HID_REPORT_COUNT ( 1 ) ,\
|
||||
HID_REPORT_SIZE ( 2 ) ,\
|
||||
HID_USAGE ( HID_USAGE_DESKTOP_SYSTEM_POWER_DOWN ) ,\
|
||||
HID_USAGE ( HID_USAGE_DESKTOP_SYSTEM_SLEEP ) ,\
|
||||
HID_USAGE ( HID_USAGE_DESKTOP_SYSTEM_WAKE_UP ) ,\
|
||||
HID_INPUT ( HID_DATA | HID_ARRAY | HID_ABSOLUTE ) ,\
|
||||
/* 6 bit padding */ \
|
||||
HID_REPORT_COUNT ( 1 ) ,\
|
||||
HID_REPORT_SIZE ( 6 ) ,\
|
||||
HID_INPUT ( HID_CONSTANT ) ,\
|
||||
HID_COLLECTION_END \
|
||||
|
||||
// Gamepad Report Descriptor Template
|
||||
// with 32 buttons, 2 joysticks and 1 hat/dpad with following layout
|
||||
// | X | Y | Z | Rz | Rx | Ry (1 byte each) | hat/DPAD (1 byte) | Button Map (4 bytes) |
|
||||
#define TUD_HID_REPORT_DESC_GAMEPAD(...) \
|
||||
HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\
|
||||
HID_USAGE ( HID_USAGE_DESKTOP_GAMEPAD ) ,\
|
||||
HID_COLLECTION ( HID_COLLECTION_APPLICATION ) ,\
|
||||
/* Report ID if any */\
|
||||
__VA_ARGS__ \
|
||||
/* 8 bit X, Y, Z, Rz, Rx, Ry (min -127, max 127 ) */ \
|
||||
HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\
|
||||
HID_USAGE ( HID_USAGE_DESKTOP_X ) ,\
|
||||
HID_USAGE ( HID_USAGE_DESKTOP_Y ) ,\
|
||||
HID_USAGE ( HID_USAGE_DESKTOP_Z ) ,\
|
||||
HID_USAGE ( HID_USAGE_DESKTOP_RZ ) ,\
|
||||
HID_USAGE ( HID_USAGE_DESKTOP_RX ) ,\
|
||||
HID_USAGE ( HID_USAGE_DESKTOP_RY ) ,\
|
||||
HID_LOGICAL_MIN ( 0x81 ) ,\
|
||||
HID_LOGICAL_MAX ( 0x7f ) ,\
|
||||
HID_REPORT_COUNT ( 6 ) ,\
|
||||
HID_REPORT_SIZE ( 8 ) ,\
|
||||
HID_INPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ) ,\
|
||||
/* 8 bit DPad/Hat Button Map */ \
|
||||
HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\
|
||||
HID_USAGE ( HID_USAGE_DESKTOP_HAT_SWITCH ) ,\
|
||||
HID_LOGICAL_MIN ( 1 ) ,\
|
||||
HID_LOGICAL_MAX ( 8 ) ,\
|
||||
HID_PHYSICAL_MIN ( 0 ) ,\
|
||||
HID_PHYSICAL_MAX_N ( 315, 2 ) ,\
|
||||
HID_REPORT_COUNT ( 1 ) ,\
|
||||
HID_REPORT_SIZE ( 8 ) ,\
|
||||
HID_INPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ) ,\
|
||||
/* 32 bit Button Map */ \
|
||||
HID_USAGE_PAGE ( HID_USAGE_PAGE_BUTTON ) ,\
|
||||
HID_USAGE_MIN ( 1 ) ,\
|
||||
HID_USAGE_MAX ( 32 ) ,\
|
||||
HID_LOGICAL_MIN ( 0 ) ,\
|
||||
HID_LOGICAL_MAX ( 1 ) ,\
|
||||
HID_REPORT_COUNT ( 32 ) ,\
|
||||
HID_REPORT_SIZE ( 1 ) ,\
|
||||
HID_INPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ) ,\
|
||||
HID_COLLECTION_END \
|
||||
|
||||
// FIDO U2F Authenticator Descriptor Template
|
||||
// - 1st parameter is report size, which is 64 bytes maximum in U2F
|
||||
// - 2nd parameter is HID_REPORT_ID(n) (optional)
|
||||
#define TUD_HID_REPORT_DESC_FIDO_U2F(report_size, ...) \
|
||||
HID_USAGE_PAGE_N ( HID_USAGE_PAGE_FIDO, 2 ) ,\
|
||||
HID_USAGE ( HID_USAGE_FIDO_U2FHID ) ,\
|
||||
HID_COLLECTION ( HID_COLLECTION_APPLICATION ) ,\
|
||||
/* Report ID if any */ \
|
||||
__VA_ARGS__ \
|
||||
/* Usage Data In */ \
|
||||
HID_USAGE ( HID_USAGE_FIDO_DATA_IN ) ,\
|
||||
HID_LOGICAL_MIN ( 0 ) ,\
|
||||
HID_LOGICAL_MAX_N ( 0xff, 2 ) ,\
|
||||
HID_REPORT_SIZE ( 8 ) ,\
|
||||
HID_REPORT_COUNT ( report_size ) ,\
|
||||
HID_INPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ) ,\
|
||||
/* Usage Data Out */ \
|
||||
HID_USAGE ( HID_USAGE_FIDO_DATA_OUT ) ,\
|
||||
HID_LOGICAL_MIN ( 0 ) ,\
|
||||
HID_LOGICAL_MAX_N ( 0xff, 2 ) ,\
|
||||
HID_REPORT_SIZE ( 8 ) ,\
|
||||
HID_REPORT_COUNT ( report_size ) ,\
|
||||
HID_OUTPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ) ,\
|
||||
HID_COLLECTION_END \
|
||||
|
||||
// HID Generic Input & Output
|
||||
// - 1st parameter is report size (mandatory)
|
||||
// - 2nd parameter is report id HID_REPORT_ID(n) (optional)
|
||||
#define TUD_HID_REPORT_DESC_GENERIC_INOUT(report_size, ...) \
|
||||
HID_USAGE_PAGE_N ( HID_USAGE_PAGE_VENDOR, 2 ),\
|
||||
HID_USAGE ( 0x01 ),\
|
||||
HID_COLLECTION ( HID_COLLECTION_APPLICATION ),\
|
||||
/* Report ID if any */\
|
||||
__VA_ARGS__ \
|
||||
/* Input */ \
|
||||
HID_USAGE ( 0x02 ),\
|
||||
HID_LOGICAL_MIN ( 0x00 ),\
|
||||
HID_LOGICAL_MAX_N ( 0xff, 2 ),\
|
||||
HID_REPORT_SIZE ( 8 ),\
|
||||
HID_REPORT_COUNT( report_size ),\
|
||||
HID_INPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ),\
|
||||
/* Output */ \
|
||||
HID_USAGE ( 0x03 ),\
|
||||
HID_LOGICAL_MIN ( 0x00 ),\
|
||||
HID_LOGICAL_MAX_N ( 0xff, 2 ),\
|
||||
HID_REPORT_SIZE ( 8 ),\
|
||||
HID_REPORT_COUNT( report_size ),\
|
||||
HID_OUTPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ),\
|
||||
HID_COLLECTION_END \
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Internal Class Driver API
|
||||
//--------------------------------------------------------------------+
|
||||
void hidd_init (void);
|
||||
void hidd_reset (uint8_t rhport);
|
||||
uint16_t hidd_open (uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len);
|
||||
bool hidd_control_xfer_cb (uint8_t rhport, uint8_t stage, tusb_control_request_t const * request);
|
||||
bool hidd_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _TUSB_HID_DEVICE_H_ */
|
|
@ -0,0 +1,785 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#include "tusb_option.h"
|
||||
|
||||
#if (CFG_TUH_ENABLED && CFG_TUH_HID)
|
||||
|
||||
#include "host/usbh.h"
|
||||
#include "host/usbh_pvt.h"
|
||||
|
||||
#include "hid_host.h"
|
||||
|
||||
// Level where CFG_TUSB_DEBUG must be at least for this driver is logged
|
||||
#ifndef CFG_TUH_HID_LOG_LEVEL
|
||||
#define CFG_TUH_HID_LOG_LEVEL CFG_TUH_LOG_LEVEL
|
||||
#endif
|
||||
|
||||
#define TU_LOG_DRV(...) TU_LOG(CFG_TUH_HID_LOG_LEVEL, __VA_ARGS__)
|
||||
//--------------------------------------------------------------------+
|
||||
// MACRO CONSTANT TYPEDEF
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t daddr;
|
||||
|
||||
uint8_t itf_num;
|
||||
uint8_t ep_in;
|
||||
uint8_t ep_out;
|
||||
|
||||
uint8_t itf_protocol; // None, Keyboard, Mouse
|
||||
uint8_t protocol_mode; // Boot (0) or Report protocol (1)
|
||||
|
||||
uint8_t report_desc_type;
|
||||
uint16_t report_desc_len;
|
||||
|
||||
uint16_t epin_size;
|
||||
uint16_t epout_size;
|
||||
|
||||
CFG_TUH_MEM_ALIGN uint8_t epin_buf[CFG_TUH_HID_EPIN_BUFSIZE];
|
||||
CFG_TUH_MEM_ALIGN uint8_t epout_buf[CFG_TUH_HID_EPOUT_BUFSIZE];
|
||||
} hidh_interface_t;
|
||||
|
||||
CFG_TUH_MEM_SECTION
|
||||
tu_static hidh_interface_t _hidh_itf[CFG_TUH_HID];
|
||||
|
||||
tu_static uint8_t _hidh_default_protocol = HID_PROTOCOL_BOOT;
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Helper
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline
|
||||
hidh_interface_t* get_hid_itf(uint8_t daddr, uint8_t idx)
|
||||
{
|
||||
TU_ASSERT(daddr > 0 && idx < CFG_TUH_HID, NULL);
|
||||
hidh_interface_t* p_hid = &_hidh_itf[idx];
|
||||
return (p_hid->daddr == daddr) ? p_hid : NULL;
|
||||
}
|
||||
|
||||
// Get instance ID by endpoint address
|
||||
static uint8_t get_idx_by_epaddr(uint8_t daddr, uint8_t ep_addr)
|
||||
{
|
||||
for ( uint8_t idx = 0; idx < CFG_TUH_HID; idx++ )
|
||||
{
|
||||
hidh_interface_t const * p_hid = &_hidh_itf[idx];
|
||||
|
||||
if ( p_hid->daddr == daddr &&
|
||||
(p_hid->ep_in == ep_addr || p_hid->ep_out == ep_addr) )
|
||||
{
|
||||
return idx;
|
||||
}
|
||||
}
|
||||
|
||||
return TUSB_INDEX_INVALID_8;
|
||||
}
|
||||
|
||||
static hidh_interface_t* find_new_itf(void)
|
||||
{
|
||||
for(uint8_t i=0; i<CFG_TUH_HID; i++)
|
||||
{
|
||||
if (_hidh_itf[i].daddr == 0) return &_hidh_itf[i];
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Interface API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
uint8_t tuh_hid_itf_get_count(uint8_t daddr)
|
||||
{
|
||||
uint8_t count = 0;
|
||||
|
||||
for(uint8_t i=0; i<CFG_TUH_HID; i++)
|
||||
{
|
||||
if (_hidh_itf[i].daddr == daddr) count++;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
uint8_t tuh_hid_itf_get_total_count(void)
|
||||
{
|
||||
uint8_t count = 0;
|
||||
|
||||
for(uint8_t i=0; i<CFG_TUH_HID; i++)
|
||||
{
|
||||
if (_hidh_itf[i].daddr != 0) count++;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
bool tuh_hid_mounted(uint8_t daddr, uint8_t idx)
|
||||
{
|
||||
hidh_interface_t* p_hid = get_hid_itf(daddr, idx);
|
||||
return p_hid != NULL;
|
||||
}
|
||||
|
||||
bool tuh_hid_itf_get_info(uint8_t daddr, uint8_t idx, tuh_itf_info_t* info)
|
||||
{
|
||||
hidh_interface_t* p_hid = get_hid_itf(daddr, idx);
|
||||
TU_VERIFY(p_hid && info);
|
||||
|
||||
info->daddr = daddr;
|
||||
|
||||
// re-construct descriptor
|
||||
tusb_desc_interface_t* desc = &info->desc;
|
||||
desc->bLength = sizeof(tusb_desc_interface_t);
|
||||
desc->bDescriptorType = TUSB_DESC_INTERFACE;
|
||||
|
||||
desc->bInterfaceNumber = p_hid->itf_num;
|
||||
desc->bAlternateSetting = 0;
|
||||
desc->bNumEndpoints = (uint8_t) ((p_hid->ep_in ? 1u : 0u) + (p_hid->ep_out ? 1u : 0u));
|
||||
desc->bInterfaceClass = TUSB_CLASS_HID;
|
||||
desc->bInterfaceSubClass = (p_hid->itf_protocol ? HID_SUBCLASS_BOOT : HID_SUBCLASS_NONE);
|
||||
desc->bInterfaceProtocol = p_hid->itf_protocol;
|
||||
desc->iInterface = 0; // not used yet
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8_t tuh_hid_itf_get_index(uint8_t daddr, uint8_t itf_num)
|
||||
{
|
||||
for ( uint8_t idx = 0; idx < CFG_TUH_HID; idx++ )
|
||||
{
|
||||
hidh_interface_t const * p_hid = &_hidh_itf[idx];
|
||||
|
||||
if ( p_hid->daddr == daddr && p_hid->itf_num == itf_num) return idx;
|
||||
}
|
||||
|
||||
return TUSB_INDEX_INVALID_8;
|
||||
}
|
||||
|
||||
uint8_t tuh_hid_interface_protocol(uint8_t daddr, uint8_t idx)
|
||||
{
|
||||
hidh_interface_t* p_hid = get_hid_itf(daddr, idx);
|
||||
return p_hid ? p_hid->itf_protocol : 0;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Control Endpoint API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
uint8_t tuh_hid_get_protocol(uint8_t daddr, uint8_t idx)
|
||||
{
|
||||
hidh_interface_t* p_hid = get_hid_itf(daddr, idx);
|
||||
return p_hid ? p_hid->protocol_mode : 0;
|
||||
}
|
||||
|
||||
static void set_protocol_complete(tuh_xfer_t* xfer)
|
||||
{
|
||||
uint8_t const itf_num = (uint8_t) tu_le16toh(xfer->setup->wIndex);
|
||||
uint8_t const daddr = xfer->daddr;
|
||||
uint8_t const idx = tuh_hid_itf_get_index(daddr, itf_num);
|
||||
|
||||
hidh_interface_t* p_hid = get_hid_itf(daddr, idx);
|
||||
TU_VERIFY(p_hid, );
|
||||
|
||||
if (XFER_RESULT_SUCCESS == xfer->result)
|
||||
{
|
||||
p_hid->protocol_mode = (uint8_t) tu_le16toh(xfer->setup->wValue);
|
||||
}
|
||||
|
||||
if (tuh_hid_set_protocol_complete_cb)
|
||||
{
|
||||
tuh_hid_set_protocol_complete_cb(daddr, idx, p_hid->protocol_mode);
|
||||
}
|
||||
}
|
||||
|
||||
void tuh_hid_set_default_protocol(uint8_t protocol) {
|
||||
_hidh_default_protocol = protocol;
|
||||
}
|
||||
|
||||
static bool _hidh_set_protocol(uint8_t daddr, uint8_t itf_num, uint8_t protocol, tuh_xfer_cb_t complete_cb, uintptr_t user_data)
|
||||
{
|
||||
TU_LOG_DRV("HID Set Protocol = %d\r\n", protocol);
|
||||
|
||||
tusb_control_request_t const request =
|
||||
{
|
||||
.bmRequestType_bit =
|
||||
{
|
||||
.recipient = TUSB_REQ_RCPT_INTERFACE,
|
||||
.type = TUSB_REQ_TYPE_CLASS,
|
||||
.direction = TUSB_DIR_OUT
|
||||
},
|
||||
.bRequest = HID_REQ_CONTROL_SET_PROTOCOL,
|
||||
.wValue = protocol,
|
||||
.wIndex = itf_num,
|
||||
.wLength = 0
|
||||
};
|
||||
|
||||
tuh_xfer_t xfer =
|
||||
{
|
||||
.daddr = daddr,
|
||||
.ep_addr = 0,
|
||||
.setup = &request,
|
||||
.buffer = NULL,
|
||||
.complete_cb = complete_cb,
|
||||
.user_data = user_data
|
||||
};
|
||||
|
||||
return tuh_control_xfer(&xfer);
|
||||
}
|
||||
|
||||
bool tuh_hid_set_protocol(uint8_t daddr, uint8_t idx, uint8_t protocol)
|
||||
{
|
||||
hidh_interface_t* p_hid = get_hid_itf(daddr, idx);
|
||||
TU_VERIFY(p_hid && p_hid->itf_protocol != HID_ITF_PROTOCOL_NONE);
|
||||
|
||||
return _hidh_set_protocol(daddr, p_hid->itf_num, protocol, set_protocol_complete, 0);
|
||||
}
|
||||
|
||||
static void set_report_complete(tuh_xfer_t* xfer)
|
||||
{
|
||||
TU_LOG_DRV("HID Set Report complete\r\n");
|
||||
|
||||
if (tuh_hid_set_report_complete_cb)
|
||||
{
|
||||
uint8_t const itf_num = (uint8_t) tu_le16toh(xfer->setup->wIndex);
|
||||
uint8_t const idx = tuh_hid_itf_get_index(xfer->daddr, itf_num);
|
||||
|
||||
uint8_t const report_type = tu_u16_high(xfer->setup->wValue);
|
||||
uint8_t const report_id = tu_u16_low(xfer->setup->wValue);
|
||||
|
||||
tuh_hid_set_report_complete_cb(xfer->daddr, idx, report_id, report_type,
|
||||
(xfer->result == XFER_RESULT_SUCCESS) ? xfer->setup->wLength : 0);
|
||||
}
|
||||
}
|
||||
|
||||
bool tuh_hid_set_report(uint8_t daddr, uint8_t idx, uint8_t report_id, uint8_t report_type, void* report, uint16_t len)
|
||||
{
|
||||
hidh_interface_t* p_hid = get_hid_itf(daddr, idx);
|
||||
TU_VERIFY(p_hid);
|
||||
|
||||
TU_LOG_DRV("HID Set Report: id = %u, type = %u, len = %u\r\n", report_id, report_type, len);
|
||||
|
||||
tusb_control_request_t const request =
|
||||
{
|
||||
.bmRequestType_bit =
|
||||
{
|
||||
.recipient = TUSB_REQ_RCPT_INTERFACE,
|
||||
.type = TUSB_REQ_TYPE_CLASS,
|
||||
.direction = TUSB_DIR_OUT
|
||||
},
|
||||
.bRequest = HID_REQ_CONTROL_SET_REPORT,
|
||||
.wValue = tu_htole16(tu_u16(report_type, report_id)),
|
||||
.wIndex = tu_htole16((uint16_t)p_hid->itf_num),
|
||||
.wLength = len
|
||||
};
|
||||
|
||||
tuh_xfer_t xfer =
|
||||
{
|
||||
.daddr = daddr,
|
||||
.ep_addr = 0,
|
||||
.setup = &request,
|
||||
.buffer = report,
|
||||
.complete_cb = set_report_complete,
|
||||
.user_data = 0
|
||||
};
|
||||
|
||||
return tuh_control_xfer(&xfer);
|
||||
}
|
||||
|
||||
static bool _hidh_set_idle(uint8_t daddr, uint8_t itf_num, uint16_t idle_rate, tuh_xfer_cb_t complete_cb, uintptr_t user_data)
|
||||
{
|
||||
// SET IDLE request, device can stall if not support this request
|
||||
TU_LOG_DRV("HID Set Idle \r\n");
|
||||
|
||||
tusb_control_request_t const request =
|
||||
{
|
||||
.bmRequestType_bit =
|
||||
{
|
||||
.recipient = TUSB_REQ_RCPT_INTERFACE,
|
||||
.type = TUSB_REQ_TYPE_CLASS,
|
||||
.direction = TUSB_DIR_OUT
|
||||
},
|
||||
.bRequest = HID_REQ_CONTROL_SET_IDLE,
|
||||
.wValue = tu_htole16(idle_rate),
|
||||
.wIndex = tu_htole16((uint16_t)itf_num),
|
||||
.wLength = 0
|
||||
};
|
||||
|
||||
tuh_xfer_t xfer =
|
||||
{
|
||||
.daddr = daddr,
|
||||
.ep_addr = 0,
|
||||
.setup = &request,
|
||||
.buffer = NULL,
|
||||
.complete_cb = complete_cb,
|
||||
.user_data = user_data
|
||||
};
|
||||
|
||||
return tuh_control_xfer(&xfer);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Interrupt Endpoint API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Check if HID interface is ready to receive report
|
||||
bool tuh_hid_receive_ready(uint8_t dev_addr, uint8_t idx)
|
||||
{
|
||||
hidh_interface_t* p_hid = get_hid_itf(dev_addr, idx);
|
||||
TU_VERIFY(p_hid);
|
||||
|
||||
return !usbh_edpt_busy(dev_addr, p_hid->ep_in);
|
||||
}
|
||||
|
||||
bool tuh_hid_receive_report(uint8_t daddr, uint8_t idx)
|
||||
{
|
||||
hidh_interface_t* p_hid = get_hid_itf(daddr, idx);
|
||||
TU_VERIFY(p_hid);
|
||||
|
||||
// claim endpoint
|
||||
TU_VERIFY( usbh_edpt_claim(daddr, p_hid->ep_in) );
|
||||
|
||||
if ( !usbh_edpt_xfer(daddr, p_hid->ep_in, p_hid->epin_buf, p_hid->epin_size) )
|
||||
{
|
||||
usbh_edpt_release(daddr, p_hid->ep_in);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool tuh_hid_send_ready(uint8_t dev_addr, uint8_t idx)
|
||||
{
|
||||
hidh_interface_t* p_hid = get_hid_itf(dev_addr, idx);
|
||||
TU_VERIFY(p_hid);
|
||||
|
||||
return !usbh_edpt_busy(dev_addr, p_hid->ep_out);
|
||||
}
|
||||
|
||||
bool tuh_hid_send_report(uint8_t daddr, uint8_t idx, uint8_t report_id, const void* report, uint16_t len)
|
||||
{
|
||||
TU_LOG_DRV("HID Send Report %d\r\n", report_id);
|
||||
|
||||
hidh_interface_t* p_hid = get_hid_itf(daddr, idx);
|
||||
TU_VERIFY(p_hid);
|
||||
|
||||
if (p_hid->ep_out == 0)
|
||||
{
|
||||
// This HID does not have an out endpoint (other than control)
|
||||
return false;
|
||||
}
|
||||
else if (len > CFG_TUH_HID_EPOUT_BUFSIZE ||
|
||||
(report_id != 0 && len > (CFG_TUH_HID_EPOUT_BUFSIZE - 1)))
|
||||
{
|
||||
// ep_out buffer is not large enough to hold contents
|
||||
return false;
|
||||
}
|
||||
|
||||
// claim endpoint
|
||||
TU_VERIFY( usbh_edpt_claim(daddr, p_hid->ep_out) );
|
||||
|
||||
if (report_id == 0)
|
||||
{
|
||||
// No report ID in transmission
|
||||
memcpy(&p_hid->epout_buf[0], report, len);
|
||||
}
|
||||
else
|
||||
{
|
||||
p_hid->epout_buf[0] = report_id;
|
||||
memcpy(&p_hid->epout_buf[1], report, len);
|
||||
++len; // 1 more byte for report_id
|
||||
}
|
||||
|
||||
TU_LOG3_MEM(p_hid->epout_buf, len, 2);
|
||||
|
||||
if ( !usbh_edpt_xfer(daddr, p_hid->ep_out, p_hid->epout_buf, len) )
|
||||
{
|
||||
usbh_edpt_release(daddr, p_hid->ep_out);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// USBH API
|
||||
//--------------------------------------------------------------------+
|
||||
void hidh_init(void)
|
||||
{
|
||||
tu_memclr(_hidh_itf, sizeof(_hidh_itf));
|
||||
}
|
||||
|
||||
bool hidh_xfer_cb(uint8_t daddr, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes)
|
||||
{
|
||||
(void) result;
|
||||
|
||||
uint8_t const dir = tu_edpt_dir(ep_addr);
|
||||
uint8_t const idx = get_idx_by_epaddr(daddr, ep_addr);
|
||||
|
||||
hidh_interface_t* p_hid = get_hid_itf(daddr, idx);
|
||||
TU_VERIFY(p_hid);
|
||||
|
||||
if ( dir == TUSB_DIR_IN )
|
||||
{
|
||||
// TU_LOG_DRV(" Get Report callback (%u, %u)\r\n", daddr, idx);
|
||||
TU_LOG3_MEM(p_hid->epin_buf, xferred_bytes, 2);
|
||||
tuh_hid_report_received_cb(daddr, idx, p_hid->epin_buf, (uint16_t) xferred_bytes);
|
||||
}else
|
||||
{
|
||||
if (tuh_hid_report_sent_cb) tuh_hid_report_sent_cb(daddr, idx, p_hid->epout_buf, (uint16_t) xferred_bytes);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void hidh_close(uint8_t daddr)
|
||||
{
|
||||
for(uint8_t i=0; i<CFG_TUH_HID; i++)
|
||||
{
|
||||
hidh_interface_t* p_hid = &_hidh_itf[i];
|
||||
if (p_hid->daddr == daddr)
|
||||
{
|
||||
TU_LOG_DRV(" HIDh close addr = %u index = %u\r\n", daddr, i);
|
||||
if(tuh_hid_umount_cb) tuh_hid_umount_cb(daddr, i);
|
||||
p_hid->daddr = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Enumeration
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
bool hidh_open(uint8_t rhport, uint8_t daddr, tusb_desc_interface_t const *desc_itf, uint16_t max_len)
|
||||
{
|
||||
(void) rhport;
|
||||
(void) max_len;
|
||||
|
||||
TU_VERIFY(TUSB_CLASS_HID == desc_itf->bInterfaceClass);
|
||||
|
||||
TU_LOG_DRV("[%u] HID opening Interface %u\r\n", daddr, desc_itf->bInterfaceNumber);
|
||||
|
||||
// len = interface + hid + n*endpoints
|
||||
uint16_t const drv_len = (uint16_t) (sizeof(tusb_desc_interface_t) + sizeof(tusb_hid_descriptor_hid_t) +
|
||||
desc_itf->bNumEndpoints * sizeof(tusb_desc_endpoint_t));
|
||||
TU_ASSERT(max_len >= drv_len);
|
||||
|
||||
uint8_t const *p_desc = (uint8_t const *) desc_itf;
|
||||
|
||||
//------------- HID descriptor -------------//
|
||||
p_desc = tu_desc_next(p_desc);
|
||||
tusb_hid_descriptor_hid_t const *desc_hid = (tusb_hid_descriptor_hid_t const *) p_desc;
|
||||
TU_ASSERT(HID_DESC_TYPE_HID == desc_hid->bDescriptorType);
|
||||
|
||||
hidh_interface_t* p_hid = find_new_itf();
|
||||
TU_ASSERT(p_hid); // not enough interface, try to increase CFG_TUH_HID
|
||||
p_hid->daddr = daddr;
|
||||
|
||||
//------------- Endpoint Descriptors -------------//
|
||||
p_desc = tu_desc_next(p_desc);
|
||||
tusb_desc_endpoint_t const * desc_ep = (tusb_desc_endpoint_t const *) p_desc;
|
||||
|
||||
for(int i = 0; i < desc_itf->bNumEndpoints; i++)
|
||||
{
|
||||
TU_ASSERT(TUSB_DESC_ENDPOINT == desc_ep->bDescriptorType);
|
||||
TU_ASSERT( tuh_edpt_open(daddr, desc_ep) );
|
||||
|
||||
if(tu_edpt_dir(desc_ep->bEndpointAddress) == TUSB_DIR_IN)
|
||||
{
|
||||
p_hid->ep_in = desc_ep->bEndpointAddress;
|
||||
p_hid->epin_size = tu_edpt_packet_size(desc_ep);
|
||||
}
|
||||
else
|
||||
{
|
||||
p_hid->ep_out = desc_ep->bEndpointAddress;
|
||||
p_hid->epout_size = tu_edpt_packet_size(desc_ep);
|
||||
}
|
||||
|
||||
p_desc = tu_desc_next(p_desc);
|
||||
desc_ep = (tusb_desc_endpoint_t const *) p_desc;
|
||||
}
|
||||
|
||||
p_hid->itf_num = desc_itf->bInterfaceNumber;
|
||||
|
||||
// Assume bNumDescriptors = 1
|
||||
p_hid->report_desc_type = desc_hid->bReportType;
|
||||
p_hid->report_desc_len = tu_unaligned_read16(&desc_hid->wReportLength);
|
||||
|
||||
// Per HID Specs: default is Report protocol, though we will force Boot protocol when set_config
|
||||
p_hid->protocol_mode = _hidh_default_protocol;
|
||||
if ( HID_SUBCLASS_BOOT == desc_itf->bInterfaceSubClass )
|
||||
{
|
||||
p_hid->itf_protocol = desc_itf->bInterfaceProtocol;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Set Configure
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
enum {
|
||||
CONFG_SET_IDLE,
|
||||
CONFIG_SET_PROTOCOL,
|
||||
CONFIG_GET_REPORT_DESC,
|
||||
CONFIG_COMPLETE
|
||||
};
|
||||
|
||||
static void config_driver_mount_complete(uint8_t daddr, uint8_t idx, uint8_t const* desc_report, uint16_t desc_len);
|
||||
static void process_set_config(tuh_xfer_t* xfer);
|
||||
|
||||
bool hidh_set_config(uint8_t daddr, uint8_t itf_num)
|
||||
{
|
||||
tusb_control_request_t request;
|
||||
request.wIndex = tu_htole16((uint16_t) itf_num);
|
||||
|
||||
tuh_xfer_t xfer;
|
||||
xfer.daddr = daddr;
|
||||
xfer.result = XFER_RESULT_SUCCESS;
|
||||
xfer.setup = &request;
|
||||
xfer.user_data = CONFG_SET_IDLE;
|
||||
|
||||
// fake request to kick-off the set config process
|
||||
process_set_config(&xfer);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static void process_set_config(tuh_xfer_t* xfer)
|
||||
{
|
||||
// Stall is a valid response for SET_IDLE, sometime SET_PROTOCOL as well
|
||||
// therefore we could ignore its result
|
||||
if ( !(xfer->setup->bRequest == HID_REQ_CONTROL_SET_IDLE ||
|
||||
xfer->setup->bRequest == HID_REQ_CONTROL_SET_PROTOCOL) )
|
||||
{
|
||||
TU_ASSERT(xfer->result == XFER_RESULT_SUCCESS, );
|
||||
}
|
||||
|
||||
uintptr_t const state = xfer->user_data;
|
||||
uint8_t const itf_num = (uint8_t) tu_le16toh(xfer->setup->wIndex);
|
||||
uint8_t const daddr = xfer->daddr;
|
||||
|
||||
uint8_t const idx = tuh_hid_itf_get_index(daddr, itf_num);
|
||||
hidh_interface_t* p_hid = get_hid_itf(daddr, idx);
|
||||
TU_VERIFY(p_hid, );
|
||||
|
||||
switch(state)
|
||||
{
|
||||
case CONFG_SET_IDLE:
|
||||
{
|
||||
// Idle rate = 0 mean only report when there is changes
|
||||
const uint16_t idle_rate = 0;
|
||||
const uintptr_t next_state = (p_hid->itf_protocol != HID_ITF_PROTOCOL_NONE) ? CONFIG_SET_PROTOCOL : CONFIG_GET_REPORT_DESC;
|
||||
_hidh_set_idle(daddr, itf_num, idle_rate, process_set_config, next_state);
|
||||
}
|
||||
break;
|
||||
|
||||
case CONFIG_SET_PROTOCOL:
|
||||
_hidh_set_protocol(daddr, p_hid->itf_num, _hidh_default_protocol, process_set_config, CONFIG_GET_REPORT_DESC);
|
||||
break;
|
||||
|
||||
case CONFIG_GET_REPORT_DESC:
|
||||
// Get Report Descriptor if possible
|
||||
// using usbh enumeration buffer since report descriptor can be very long
|
||||
if( p_hid->report_desc_len > CFG_TUH_ENUMERATION_BUFSIZE )
|
||||
{
|
||||
TU_LOG_DRV("HID Skip Report Descriptor since it is too large %u bytes\r\n", p_hid->report_desc_len);
|
||||
|
||||
// Driver is mounted without report descriptor
|
||||
config_driver_mount_complete(daddr, idx, NULL, 0);
|
||||
}else
|
||||
{
|
||||
tuh_descriptor_get_hid_report(daddr, itf_num, p_hid->report_desc_type, 0, usbh_get_enum_buf(), p_hid->report_desc_len, process_set_config, CONFIG_COMPLETE);
|
||||
}
|
||||
break;
|
||||
|
||||
case CONFIG_COMPLETE:
|
||||
{
|
||||
uint8_t const* desc_report = usbh_get_enum_buf();
|
||||
uint16_t const desc_len = tu_le16toh(xfer->setup->wLength);
|
||||
|
||||
config_driver_mount_complete(daddr, idx, desc_report, desc_len);
|
||||
}
|
||||
break;
|
||||
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
|
||||
static void config_driver_mount_complete(uint8_t daddr, uint8_t idx, uint8_t const* desc_report, uint16_t desc_len)
|
||||
{
|
||||
hidh_interface_t* p_hid = get_hid_itf(daddr, idx);
|
||||
TU_VERIFY(p_hid, );
|
||||
|
||||
// enumeration is complete
|
||||
if (tuh_hid_mount_cb) tuh_hid_mount_cb(daddr, idx, desc_report, desc_len);
|
||||
|
||||
// notify usbh that driver enumeration is complete
|
||||
usbh_driver_set_config_complete(daddr, p_hid->itf_num);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Report Descriptor Parser
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
uint8_t tuh_hid_parse_report_descriptor(tuh_hid_report_info_t* report_info_arr, uint8_t arr_count, uint8_t const* desc_report, uint16_t desc_len)
|
||||
{
|
||||
// Report Item 6.2.2.2 USB HID 1.11
|
||||
union TU_ATTR_PACKED
|
||||
{
|
||||
uint8_t byte;
|
||||
struct TU_ATTR_PACKED
|
||||
{
|
||||
uint8_t size : 2;
|
||||
uint8_t type : 2;
|
||||
uint8_t tag : 4;
|
||||
};
|
||||
} header;
|
||||
|
||||
tu_memclr(report_info_arr, arr_count*sizeof(tuh_hid_report_info_t));
|
||||
|
||||
uint8_t report_num = 0;
|
||||
tuh_hid_report_info_t* info = report_info_arr;
|
||||
|
||||
// current parsed report count & size from descriptor
|
||||
// uint8_t ri_report_count = 0;
|
||||
// uint8_t ri_report_size = 0;
|
||||
|
||||
uint8_t ri_collection_depth = 0;
|
||||
|
||||
while(desc_len && report_num < arr_count)
|
||||
{
|
||||
header.byte = *desc_report++;
|
||||
desc_len--;
|
||||
|
||||
uint8_t const tag = header.tag;
|
||||
uint8_t const type = header.type;
|
||||
uint8_t const size = header.size;
|
||||
|
||||
uint8_t const data8 = desc_report[0];
|
||||
|
||||
TU_LOG(3, "tag = %d, type = %d, size = %d, data = ", tag, type, size);
|
||||
for(uint32_t i=0; i<size; i++) TU_LOG(3, "%02X ", desc_report[i]);
|
||||
TU_LOG(3, "\r\n");
|
||||
|
||||
switch(type)
|
||||
{
|
||||
case RI_TYPE_MAIN:
|
||||
switch (tag)
|
||||
{
|
||||
case RI_MAIN_INPUT: break;
|
||||
case RI_MAIN_OUTPUT: break;
|
||||
case RI_MAIN_FEATURE: break;
|
||||
|
||||
case RI_MAIN_COLLECTION:
|
||||
ri_collection_depth++;
|
||||
break;
|
||||
|
||||
case RI_MAIN_COLLECTION_END:
|
||||
ri_collection_depth--;
|
||||
if (ri_collection_depth == 0)
|
||||
{
|
||||
info++;
|
||||
report_num++;
|
||||
}
|
||||
break;
|
||||
|
||||
default: break;
|
||||
}
|
||||
break;
|
||||
|
||||
case RI_TYPE_GLOBAL:
|
||||
switch(tag)
|
||||
{
|
||||
case RI_GLOBAL_USAGE_PAGE:
|
||||
// only take in account the "usage page" before REPORT ID
|
||||
if ( ri_collection_depth == 0 ) memcpy(&info->usage_page, desc_report, size);
|
||||
break;
|
||||
|
||||
case RI_GLOBAL_LOGICAL_MIN : break;
|
||||
case RI_GLOBAL_LOGICAL_MAX : break;
|
||||
case RI_GLOBAL_PHYSICAL_MIN : break;
|
||||
case RI_GLOBAL_PHYSICAL_MAX : break;
|
||||
|
||||
case RI_GLOBAL_REPORT_ID:
|
||||
info->report_id = data8;
|
||||
break;
|
||||
|
||||
case RI_GLOBAL_REPORT_SIZE:
|
||||
// ri_report_size = data8;
|
||||
break;
|
||||
|
||||
case RI_GLOBAL_REPORT_COUNT:
|
||||
// ri_report_count = data8;
|
||||
break;
|
||||
|
||||
case RI_GLOBAL_UNIT_EXPONENT : break;
|
||||
case RI_GLOBAL_UNIT : break;
|
||||
case RI_GLOBAL_PUSH : break;
|
||||
case RI_GLOBAL_POP : break;
|
||||
|
||||
default: break;
|
||||
}
|
||||
break;
|
||||
|
||||
case RI_TYPE_LOCAL:
|
||||
switch(tag)
|
||||
{
|
||||
case RI_LOCAL_USAGE:
|
||||
// only take in account the "usage" before starting REPORT ID
|
||||
if ( ri_collection_depth == 0 ) info->usage = data8;
|
||||
break;
|
||||
|
||||
case RI_LOCAL_USAGE_MIN : break;
|
||||
case RI_LOCAL_USAGE_MAX : break;
|
||||
case RI_LOCAL_DESIGNATOR_INDEX : break;
|
||||
case RI_LOCAL_DESIGNATOR_MIN : break;
|
||||
case RI_LOCAL_DESIGNATOR_MAX : break;
|
||||
case RI_LOCAL_STRING_INDEX : break;
|
||||
case RI_LOCAL_STRING_MIN : break;
|
||||
case RI_LOCAL_STRING_MAX : break;
|
||||
case RI_LOCAL_DELIMITER : break;
|
||||
default: break;
|
||||
}
|
||||
break;
|
||||
|
||||
// error
|
||||
default: break;
|
||||
}
|
||||
|
||||
desc_report += size;
|
||||
desc_len -= size;
|
||||
}
|
||||
|
||||
for ( uint8_t i = 0; i < report_num; i++ )
|
||||
{
|
||||
info = report_info_arr+i;
|
||||
TU_LOG_DRV("%u: id = %u, usage_page = %u, usage = %u\r\n", i, info->report_id, info->usage_page, info->usage);
|
||||
}
|
||||
|
||||
return report_num;
|
||||
}
|
||||
|
||||
#endif
|
|
@ -0,0 +1,172 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#ifndef _TUSB_HID_HOST_H_
|
||||
#define _TUSB_HID_HOST_H_
|
||||
|
||||
#include "hid.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Class Driver Configuration
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// TODO Highspeed interrupt can be up to 512 bytes
|
||||
#ifndef CFG_TUH_HID_EPIN_BUFSIZE
|
||||
#define CFG_TUH_HID_EPIN_BUFSIZE 64
|
||||
#endif
|
||||
|
||||
#ifndef CFG_TUH_HID_EPOUT_BUFSIZE
|
||||
#define CFG_TUH_HID_EPOUT_BUFSIZE 64
|
||||
#endif
|
||||
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t report_id;
|
||||
uint8_t usage;
|
||||
uint16_t usage_page;
|
||||
|
||||
// TODO still use the endpoint size for now
|
||||
// uint8_t in_len; // length of IN report
|
||||
// uint8_t out_len; // length of OUT report
|
||||
} tuh_hid_report_info_t;
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Interface API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Get the total number of mounted HID interfaces of a device
|
||||
uint8_t tuh_hid_itf_get_count(uint8_t dev_addr);
|
||||
|
||||
// Get all mounted interfaces across devices
|
||||
uint8_t tuh_hid_itf_get_total_count(void);
|
||||
|
||||
// backward compatible rename
|
||||
#define tuh_hid_instance_count tuh_hid_itf_get_count
|
||||
|
||||
// Get Interface information
|
||||
bool tuh_hid_itf_get_info(uint8_t daddr, uint8_t idx, tuh_itf_info_t* itf_info);
|
||||
|
||||
// Get Interface index from device address + interface number
|
||||
// return TUSB_INDEX_INVALID_8 (0xFF) if not found
|
||||
uint8_t tuh_hid_itf_get_index(uint8_t daddr, uint8_t itf_num);
|
||||
|
||||
// Get interface supported protocol (bInterfaceProtocol) check out hid_interface_protocol_enum_t for possible values
|
||||
uint8_t tuh_hid_interface_protocol(uint8_t dev_addr, uint8_t idx);
|
||||
|
||||
// Check if HID interface is mounted
|
||||
bool tuh_hid_mounted(uint8_t dev_addr, uint8_t idx);
|
||||
|
||||
// Parse report descriptor into array of report_info struct and return number of reports.
|
||||
// For complicated report, application should write its own parser.
|
||||
uint8_t tuh_hid_parse_report_descriptor(tuh_hid_report_info_t* reports_info_arr, uint8_t arr_count, uint8_t const* desc_report, uint16_t desc_len) TU_ATTR_UNUSED;
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Control Endpoint API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Get current protocol: HID_PROTOCOL_BOOT (0) or HID_PROTOCOL_REPORT (1)
|
||||
// Note: Device will be initialized in Boot protocol for simplicity.
|
||||
// Application can use set_protocol() to switch back to Report protocol.
|
||||
uint8_t tuh_hid_get_protocol(uint8_t dev_addr, uint8_t idx);
|
||||
|
||||
// Device by default is enumerated in Boot protocol for simplicity. Application
|
||||
// can use this to modify the default protocol for next enumeration.
|
||||
void tuh_hid_set_default_protocol(uint8_t protocol);
|
||||
|
||||
// Set protocol to HID_PROTOCOL_BOOT (0) or HID_PROTOCOL_REPORT (1)
|
||||
// This function is only supported by Boot interface (tuh_n_hid_interface_protocol() != NONE)
|
||||
bool tuh_hid_set_protocol(uint8_t dev_addr, uint8_t idx, uint8_t protocol);
|
||||
|
||||
// Set Report using control endpoint
|
||||
// report_type is either Input, Output or Feature, (value from hid_report_type_t)
|
||||
bool tuh_hid_set_report(uint8_t dev_addr, uint8_t idx, uint8_t report_id, uint8_t report_type, void* report, uint16_t len);
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Interrupt Endpoint API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Check if HID interface is ready to receive report
|
||||
bool tuh_hid_receive_ready(uint8_t dev_addr, uint8_t idx);
|
||||
|
||||
// Try to receive next report on Interrupt Endpoint. Immediately return
|
||||
// - true If succeeded, tuh_hid_report_received_cb() callback will be invoked when report is available
|
||||
// - false if failed to queue the transfer e.g endpoint is busy
|
||||
bool tuh_hid_receive_report(uint8_t dev_addr, uint8_t idx);
|
||||
|
||||
// Check if HID interface is ready to send report
|
||||
bool tuh_hid_send_ready(uint8_t dev_addr, uint8_t idx);
|
||||
|
||||
// Send report using interrupt endpoint
|
||||
// If report_id > 0 (composite), it will be sent as 1st byte, then report contents. Otherwise only report content is sent.
|
||||
bool tuh_hid_send_report(uint8_t dev_addr, uint8_t idx, uint8_t report_id, const void* report, uint16_t len);
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Callbacks (Weak is optional)
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Invoked when device with hid interface is mounted
|
||||
// Report descriptor is also available for use. tuh_hid_parse_report_descriptor()
|
||||
// can be used to parse common/simple enough descriptor.
|
||||
// Note: if report descriptor length > CFG_TUH_ENUMERATION_BUFSIZE, it will be skipped
|
||||
// therefore report_desc = NULL, desc_len = 0
|
||||
TU_ATTR_WEAK void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t idx, uint8_t const* report_desc, uint16_t desc_len);
|
||||
|
||||
// Invoked when device with hid interface is un-mounted
|
||||
TU_ATTR_WEAK void tuh_hid_umount_cb(uint8_t dev_addr, uint8_t idx);
|
||||
|
||||
// Invoked when received report from device via interrupt endpoint
|
||||
// Note: if there is report ID (composite), it is 1st byte of report
|
||||
void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t idx, uint8_t const* report, uint16_t len);
|
||||
|
||||
// Invoked when sent report to device successfully via interrupt endpoint
|
||||
TU_ATTR_WEAK void tuh_hid_report_sent_cb(uint8_t dev_addr, uint8_t idx, uint8_t const* report, uint16_t len);
|
||||
|
||||
// Invoked when Sent Report to device via either control endpoint
|
||||
// len = 0 indicate there is error in the transfer e.g stalled response
|
||||
TU_ATTR_WEAK void tuh_hid_set_report_complete_cb(uint8_t dev_addr, uint8_t idx, uint8_t report_id, uint8_t report_type, uint16_t len);
|
||||
|
||||
// Invoked when Set Protocol request is complete
|
||||
TU_ATTR_WEAK void tuh_hid_set_protocol_complete_cb(uint8_t dev_addr, uint8_t idx, uint8_t protocol);
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Internal Class Driver API
|
||||
//--------------------------------------------------------------------+
|
||||
void hidh_init (void);
|
||||
bool hidh_open (uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *desc_itf, uint16_t max_len);
|
||||
bool hidh_set_config (uint8_t dev_addr, uint8_t itf_num);
|
||||
bool hidh_xfer_cb (uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes);
|
||||
void hidh_close (uint8_t dev_addr);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _TUSB_HID_HOST_H_ */
|
|
@ -0,0 +1,382 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#ifndef _TUSB_MSC_H_
|
||||
#define _TUSB_MSC_H_
|
||||
|
||||
#include "common/tusb_common.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Mass Storage Class Constant
|
||||
//--------------------------------------------------------------------+
|
||||
/// MassStorage Subclass
|
||||
typedef enum
|
||||
{
|
||||
MSC_SUBCLASS_RBC = 1 , ///< Reduced Block Commands (RBC) T10 Project 1240-D
|
||||
MSC_SUBCLASS_SFF_MMC , ///< SFF-8020i, MMC-2 (ATAPI). Typically used by a CD/DVD device
|
||||
MSC_SUBCLASS_QIC , ///< QIC-157. Typically used by a tape device
|
||||
MSC_SUBCLASS_UFI , ///< UFI. Typically used by Floppy Disk Drive (FDD) device
|
||||
MSC_SUBCLASS_SFF , ///< SFF-8070i. Can be used by Floppy Disk Drive (FDD) device
|
||||
MSC_SUBCLASS_SCSI ///< SCSI transparent command set
|
||||
}msc_subclass_type_t;
|
||||
|
||||
enum {
|
||||
MSC_CBW_SIGNATURE = 0x43425355, ///< Constant value of 43425355h (little endian)
|
||||
MSC_CSW_SIGNATURE = 0x53425355 ///< Constant value of 53425355h (little endian)
|
||||
};
|
||||
|
||||
/// \brief MassStorage Protocol.
|
||||
/// \details CBI only approved to use with full-speed floppy disk & should not used with highspeed or device other than floppy
|
||||
typedef enum
|
||||
{
|
||||
MSC_PROTOCOL_CBI = 0 , ///< Control/Bulk/Interrupt protocol (with command completion interrupt)
|
||||
MSC_PROTOCOL_CBI_NO_INTERRUPT = 1 , ///< Control/Bulk/Interrupt protocol (without command completion interrupt)
|
||||
MSC_PROTOCOL_BOT = 0x50 ///< Bulk-Only Transport
|
||||
}msc_protocol_type_t;
|
||||
|
||||
/// MassStorage Class-Specific Control Request
|
||||
typedef enum
|
||||
{
|
||||
MSC_REQ_GET_MAX_LUN = 254, ///< The Get Max LUN device request is used to determine the number of logical units supported by the device. Logical Unit Numbers on the device shall be numbered contiguously starting from LUN 0 to a maximum LUN of 15
|
||||
MSC_REQ_RESET = 255 ///< This request is used to reset the mass storage device and its associated interface. This class-specific request shall ready the device for the next CBW from the host.
|
||||
}msc_request_type_t;
|
||||
|
||||
/// \brief Command Block Status Values
|
||||
/// \details Indicates the success or failure of the command. The device shall set this byte to zero if the command completed
|
||||
/// successfully. A non-zero value shall indicate a failure during command execution according to the following
|
||||
typedef enum
|
||||
{
|
||||
MSC_CSW_STATUS_PASSED = 0 , ///< MSC_CSW_STATUS_PASSED
|
||||
MSC_CSW_STATUS_FAILED , ///< MSC_CSW_STATUS_FAILED
|
||||
MSC_CSW_STATUS_PHASE_ERROR ///< MSC_CSW_STATUS_PHASE_ERROR
|
||||
}msc_csw_status_t;
|
||||
|
||||
/// Command Block Wrapper
|
||||
typedef struct TU_ATTR_PACKED
|
||||
{
|
||||
uint32_t signature; ///< Signature that helps identify this data packet as a CBW. The signature field shall contain the value 43425355h (little endian), indicating a CBW.
|
||||
uint32_t tag; ///< Tag sent by the host. The device shall echo the contents of this field back to the host in the dCSWTagfield of the associated CSW. The dCSWTagpositively associates a CSW with the corresponding CBW.
|
||||
uint32_t total_bytes; ///< The number of bytes of data that the host expects to transfer on the Bulk-In or Bulk-Out endpoint (as indicated by the Direction bit) during the execution of this command. If this field is zero, the device and the host shall transfer no data between the CBW and the associated CSW, and the device shall ignore the value of the Direction bit in bmCBWFlags.
|
||||
uint8_t dir; ///< Bit 7 of this field define transfer direction \n - 0 : Data-Out from host to the device. \n - 1 : Data-In from the device to the host.
|
||||
uint8_t lun; ///< The device Logical Unit Number (LUN) to which the command block is being sent. For devices that support multiple LUNs, the host shall place into this field the LUN to which this command block is addressed. Otherwise, the host shall set this field to zero.
|
||||
uint8_t cmd_len; ///< The valid length of the CBWCBin bytes. This defines the valid length of the command block. The only legal values are 1 through 16
|
||||
uint8_t command[16]; ///< The command block to be executed by the device. The device shall interpret the first cmd_len bytes in this field as a command block
|
||||
}msc_cbw_t;
|
||||
|
||||
TU_VERIFY_STATIC(sizeof(msc_cbw_t) == 31, "size is not correct");
|
||||
|
||||
/// Command Status Wrapper
|
||||
typedef struct TU_ATTR_PACKED
|
||||
{
|
||||
uint32_t signature ; ///< Signature that helps identify this data packet as a CSW. The signature field shall contain the value 53425355h (little endian), indicating CSW.
|
||||
uint32_t tag ; ///< The device shall set this field to the value received in the dCBWTag of the associated CBW.
|
||||
uint32_t data_residue ; ///< For Data-Out the device shall report in the dCSWDataResidue the difference between the amount of data expected as stated in the dCBWDataTransferLength, and the actual amount of data processed by the device. For Data-In the device shall report in the dCSWDataResiduethe difference between the amount of data expected as stated in the dCBWDataTransferLengthand the actual amount of relevant data sent by the device
|
||||
uint8_t status ; ///< indicates the success or failure of the command. Values from \ref msc_csw_status_t
|
||||
}msc_csw_t;
|
||||
|
||||
TU_VERIFY_STATIC(sizeof(msc_csw_t) == 13, "size is not correct");
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// SCSI Constant
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
/// SCSI Command Operation Code
|
||||
typedef enum
|
||||
{
|
||||
SCSI_CMD_TEST_UNIT_READY = 0x00, ///< The SCSI Test Unit Ready command is used to determine if a device is ready to transfer data (read/write), i.e. if a disk has spun up, if a tape is loaded and ready etc. The device does not perform a self-test operation.
|
||||
SCSI_CMD_INQUIRY = 0x12, ///< The SCSI Inquiry command is used to obtain basic information from a target device.
|
||||
SCSI_CMD_MODE_SELECT_6 = 0x15, ///< provides a means for the application client to specify medium, logical unit, or peripheral device parameters to the device server. Device servers that implement the MODE SELECT(6) command shall also implement the MODE SENSE(6) command. Application clients should issue MODE SENSE(6) prior to each MODE SELECT(6) to determine supported mode pages, page lengths, and other parameters.
|
||||
SCSI_CMD_MODE_SENSE_6 = 0x1A, ///< provides a means for a device server to report parameters to an application client. It is a complementary command to the MODE SELECT(6) command. Device servers that implement the MODE SENSE(6) command shall also implement the MODE SELECT(6) command.
|
||||
SCSI_CMD_START_STOP_UNIT = 0x1B,
|
||||
SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL = 0x1E,
|
||||
SCSI_CMD_READ_CAPACITY_10 = 0x25, ///< The SCSI Read Capacity command is used to obtain data capacity information from a target device.
|
||||
SCSI_CMD_REQUEST_SENSE = 0x03, ///< The SCSI Request Sense command is part of the SCSI computer protocol standard. This command is used to obtain sense data -- status/error information -- from a target device.
|
||||
SCSI_CMD_READ_FORMAT_CAPACITY = 0x23, ///< The command allows the Host to request a list of the possible format capacities for an installed writable media. This command also has the capability to report the writable capacity for a media when it is installed
|
||||
SCSI_CMD_READ_10 = 0x28, ///< The READ (10) command requests that the device server read the specified logical block(s) and transfer them to the data-in buffer.
|
||||
SCSI_CMD_WRITE_10 = 0x2A, ///< The WRITE (10) command requests that the device server transfer the specified logical block(s) from the data-out buffer and write them.
|
||||
}scsi_cmd_type_t;
|
||||
|
||||
/// SCSI Sense Key
|
||||
typedef enum
|
||||
{
|
||||
SCSI_SENSE_NONE = 0x00, ///< no specific Sense Key. This would be the case for a successful command
|
||||
SCSI_SENSE_RECOVERED_ERROR = 0x01, ///< Indicates the last command completed successfully with some recovery action performed by the disc drive.
|
||||
SCSI_SENSE_NOT_READY = 0x02, ///< Indicates the logical unit addressed cannot be accessed.
|
||||
SCSI_SENSE_MEDIUM_ERROR = 0x03, ///< Indicates the command terminated with a non-recovered error condition.
|
||||
SCSI_SENSE_HARDWARE_ERROR = 0x04, ///< Indicates the disc drive detected a nonrecoverable hardware failure while performing the command or during a self test.
|
||||
SCSI_SENSE_ILLEGAL_REQUEST = 0x05, ///< Indicates an illegal parameter in the command descriptor block or in the additional parameters
|
||||
SCSI_SENSE_UNIT_ATTENTION = 0x06, ///< Indicates the disc drive may have been reset.
|
||||
SCSI_SENSE_DATA_PROTECT = 0x07, ///< Indicates that a command that reads or writes the medium was attempted on a block that is protected from this operation. The read or write operation is not performed.
|
||||
SCSI_SENSE_FIRMWARE_ERROR = 0x08, ///< Vendor specific sense key.
|
||||
SCSI_SENSE_ABORTED_COMMAND = 0x0b, ///< Indicates the disc drive aborted the command.
|
||||
SCSI_SENSE_EQUAL = 0x0c, ///< Indicates a SEARCH DATA command has satisfied an equal comparison.
|
||||
SCSI_SENSE_VOLUME_OVERFLOW = 0x0d, ///< Indicates a buffered peripheral device has reached the end of medium partition and data remains in the buffer that has not been written to the medium.
|
||||
SCSI_SENSE_MISCOMPARE = 0x0e ///< Indicates that the source data did not match the data read from the medium.
|
||||
}scsi_sense_key_type_t;
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// SCSI Primary Command (SPC-4)
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
/// SCSI Test Unit Ready Command
|
||||
typedef struct TU_ATTR_PACKED
|
||||
{
|
||||
uint8_t cmd_code ; ///< SCSI OpCode for \ref SCSI_CMD_TEST_UNIT_READY
|
||||
uint8_t lun ; ///< Logical Unit
|
||||
uint8_t reserved[3] ;
|
||||
uint8_t control ;
|
||||
} scsi_test_unit_ready_t;
|
||||
|
||||
TU_VERIFY_STATIC(sizeof(scsi_test_unit_ready_t) == 6, "size is not correct");
|
||||
|
||||
/// SCSI Inquiry Command
|
||||
typedef struct TU_ATTR_PACKED
|
||||
{
|
||||
uint8_t cmd_code ; ///< SCSI OpCode for \ref SCSI_CMD_INQUIRY
|
||||
uint8_t reserved1 ;
|
||||
uint8_t page_code ;
|
||||
uint8_t reserved2 ;
|
||||
uint8_t alloc_length ; ///< specifies the maximum number of bytes that USB host has allocated in the Data-In Buffer. An allocation length of zero specifies that no data shall be transferred.
|
||||
uint8_t control ;
|
||||
} scsi_inquiry_t, scsi_request_sense_t;
|
||||
|
||||
TU_VERIFY_STATIC(sizeof(scsi_inquiry_t) == 6, "size is not correct");
|
||||
|
||||
/// SCSI Inquiry Response Data
|
||||
typedef struct TU_ATTR_PACKED
|
||||
{
|
||||
uint8_t peripheral_device_type : 5;
|
||||
uint8_t peripheral_qualifier : 3;
|
||||
|
||||
uint8_t : 7;
|
||||
uint8_t is_removable : 1;
|
||||
|
||||
uint8_t version;
|
||||
|
||||
uint8_t response_data_format : 4;
|
||||
uint8_t hierarchical_support : 1;
|
||||
uint8_t normal_aca : 1;
|
||||
uint8_t : 2;
|
||||
|
||||
uint8_t additional_length;
|
||||
|
||||
uint8_t protect : 1;
|
||||
uint8_t : 2;
|
||||
uint8_t third_party_copy : 1;
|
||||
uint8_t target_port_group_support : 2;
|
||||
uint8_t access_control_coordinator : 1;
|
||||
uint8_t scc_support : 1;
|
||||
|
||||
uint8_t addr16 : 1;
|
||||
uint8_t : 3;
|
||||
uint8_t multi_port : 1;
|
||||
uint8_t : 1; // vendor specific
|
||||
uint8_t enclosure_service : 1;
|
||||
uint8_t : 1;
|
||||
|
||||
uint8_t : 1; // vendor specific
|
||||
uint8_t cmd_que : 1;
|
||||
uint8_t : 2;
|
||||
uint8_t sync : 1;
|
||||
uint8_t wbus16 : 1;
|
||||
uint8_t : 2;
|
||||
|
||||
uint8_t vendor_id[8] ; ///< 8 bytes of ASCII data identifying the vendor of the product.
|
||||
uint8_t product_id[16]; ///< 16 bytes of ASCII data defined by the vendor.
|
||||
uint8_t product_rev[4]; ///< 4 bytes of ASCII data defined by the vendor.
|
||||
} scsi_inquiry_resp_t;
|
||||
|
||||
TU_VERIFY_STATIC(sizeof(scsi_inquiry_resp_t) == 36, "size is not correct");
|
||||
|
||||
|
||||
typedef struct TU_ATTR_PACKED
|
||||
{
|
||||
uint8_t response_code : 7; ///< 70h - current errors, Fixed Format 71h - deferred errors, Fixed Format
|
||||
uint8_t valid : 1;
|
||||
|
||||
uint8_t reserved;
|
||||
|
||||
uint8_t sense_key : 4;
|
||||
uint8_t : 1;
|
||||
uint8_t ili : 1; ///< Incorrect length indicator
|
||||
uint8_t end_of_medium : 1;
|
||||
uint8_t filemark : 1;
|
||||
|
||||
uint32_t information;
|
||||
uint8_t add_sense_len;
|
||||
uint32_t command_specific_info;
|
||||
uint8_t add_sense_code;
|
||||
uint8_t add_sense_qualifier;
|
||||
uint8_t field_replaceable_unit_code;
|
||||
|
||||
uint8_t sense_key_specific[3]; ///< sense key specific valid bit is bit 7 of key[0], aka MSB in Big Endian layout
|
||||
|
||||
} scsi_sense_fixed_resp_t;
|
||||
|
||||
TU_VERIFY_STATIC(sizeof(scsi_sense_fixed_resp_t) == 18, "size is not correct");
|
||||
|
||||
typedef struct TU_ATTR_PACKED
|
||||
{
|
||||
uint8_t cmd_code ; ///< SCSI OpCode for \ref SCSI_CMD_MODE_SENSE_6
|
||||
|
||||
uint8_t : 3;
|
||||
uint8_t disable_block_descriptor : 1;
|
||||
uint8_t : 4;
|
||||
|
||||
uint8_t page_code : 6;
|
||||
uint8_t page_control : 2;
|
||||
|
||||
uint8_t subpage_code;
|
||||
uint8_t alloc_length;
|
||||
uint8_t control;
|
||||
} scsi_mode_sense6_t;
|
||||
|
||||
TU_VERIFY_STATIC( sizeof(scsi_mode_sense6_t) == 6, "size is not correct");
|
||||
|
||||
// This is only a Mode parameter header(6).
|
||||
typedef struct TU_ATTR_PACKED
|
||||
{
|
||||
uint8_t data_len;
|
||||
uint8_t medium_type;
|
||||
|
||||
uint8_t reserved : 7;
|
||||
bool write_protected : 1;
|
||||
|
||||
uint8_t block_descriptor_len;
|
||||
} scsi_mode_sense6_resp_t;
|
||||
|
||||
TU_VERIFY_STATIC( sizeof(scsi_mode_sense6_resp_t) == 4, "size is not correct");
|
||||
|
||||
typedef struct TU_ATTR_PACKED
|
||||
{
|
||||
uint8_t cmd_code; ///< SCSI OpCode for \ref SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL
|
||||
uint8_t reserved[3];
|
||||
uint8_t prohibit_removal;
|
||||
uint8_t control;
|
||||
} scsi_prevent_allow_medium_removal_t;
|
||||
|
||||
TU_VERIFY_STATIC( sizeof(scsi_prevent_allow_medium_removal_t) == 6, "size is not correct");
|
||||
|
||||
typedef struct TU_ATTR_PACKED
|
||||
{
|
||||
uint8_t cmd_code;
|
||||
|
||||
uint8_t immded : 1;
|
||||
uint8_t : 7;
|
||||
|
||||
uint8_t TU_RESERVED;
|
||||
|
||||
uint8_t power_condition_mod : 4;
|
||||
uint8_t : 4;
|
||||
|
||||
uint8_t start : 1;
|
||||
uint8_t load_eject : 1;
|
||||
uint8_t no_flush : 1;
|
||||
uint8_t : 1;
|
||||
uint8_t power_condition : 4;
|
||||
|
||||
uint8_t control;
|
||||
} scsi_start_stop_unit_t;
|
||||
|
||||
TU_VERIFY_STATIC( sizeof(scsi_start_stop_unit_t) == 6, "size is not correct");
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// SCSI MMC
|
||||
//--------------------------------------------------------------------+
|
||||
/// SCSI Read Format Capacity: Write Capacity
|
||||
typedef struct TU_ATTR_PACKED
|
||||
{
|
||||
uint8_t cmd_code;
|
||||
uint8_t reserved[6];
|
||||
uint16_t alloc_length;
|
||||
uint8_t control;
|
||||
} scsi_read_format_capacity_t;
|
||||
|
||||
TU_VERIFY_STATIC( sizeof(scsi_read_format_capacity_t) == 10, "size is not correct");
|
||||
|
||||
typedef struct TU_ATTR_PACKED{
|
||||
uint8_t reserved[3];
|
||||
uint8_t list_length; /// must be 8*n, length in bytes of formattable capacity descriptor followed it.
|
||||
|
||||
uint32_t block_num; /// Number of Logical Blocks
|
||||
uint8_t descriptor_type; // 00: reserved, 01 unformatted media , 10 Formatted media, 11 No media present
|
||||
|
||||
uint8_t reserved2;
|
||||
uint16_t block_size_u16;
|
||||
|
||||
} scsi_read_format_capacity_data_t;
|
||||
|
||||
TU_VERIFY_STATIC( sizeof(scsi_read_format_capacity_data_t) == 12, "size is not correct");
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// SCSI Block Command (SBC-3)
|
||||
// NOTE: All data in SCSI command are in Big Endian
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
/// SCSI Read Capacity 10 Command: Read Capacity
|
||||
typedef struct TU_ATTR_PACKED
|
||||
{
|
||||
uint8_t cmd_code ; ///< SCSI OpCode for \ref SCSI_CMD_READ_CAPACITY_10
|
||||
uint8_t reserved1 ;
|
||||
uint32_t lba ; ///< The first Logical Block Address (LBA) accessed by this command
|
||||
uint16_t reserved2 ;
|
||||
uint8_t partial_medium_indicator ;
|
||||
uint8_t control ;
|
||||
} scsi_read_capacity10_t;
|
||||
|
||||
TU_VERIFY_STATIC(sizeof(scsi_read_capacity10_t) == 10, "size is not correct");
|
||||
|
||||
/// SCSI Read Capacity 10 Response Data
|
||||
typedef struct {
|
||||
uint32_t last_lba ; ///< The last Logical Block Address of the device
|
||||
uint32_t block_size ; ///< Block size in bytes
|
||||
} scsi_read_capacity10_resp_t;
|
||||
|
||||
TU_VERIFY_STATIC(sizeof(scsi_read_capacity10_resp_t) == 8, "size is not correct");
|
||||
|
||||
/// SCSI Read 10 Command
|
||||
typedef struct TU_ATTR_PACKED
|
||||
{
|
||||
uint8_t cmd_code ; ///< SCSI OpCode
|
||||
uint8_t reserved ; // has LUN according to wiki
|
||||
uint32_t lba ; ///< The first Logical Block Address (LBA) accessed by this command
|
||||
uint8_t reserved2 ;
|
||||
uint16_t block_count ; ///< Number of Blocks used by this command
|
||||
uint8_t control ;
|
||||
} scsi_read10_t, scsi_write10_t;
|
||||
|
||||
TU_VERIFY_STATIC(sizeof(scsi_read10_t) == 10, "size is not correct");
|
||||
TU_VERIFY_STATIC(sizeof(scsi_write10_t) == 10, "size is not correct");
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _TUSB_MSC_H_ */
|
|
@ -0,0 +1,955 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#include "tusb_option.h"
|
||||
|
||||
#if (CFG_TUD_ENABLED && CFG_TUD_MSC)
|
||||
|
||||
#include "device/dcd.h" // for faking dcd_event_xfer_complete
|
||||
#include "device/usbd.h"
|
||||
#include "device/usbd_pvt.h"
|
||||
|
||||
#include "msc_device.h"
|
||||
|
||||
// Level where CFG_TUSB_DEBUG must be at least for this driver is logged
|
||||
#ifndef CFG_TUD_MSC_LOG_LEVEL
|
||||
#define CFG_TUD_MSC_LOG_LEVEL CFG_TUD_LOG_LEVEL
|
||||
#endif
|
||||
|
||||
#define TU_LOG_DRV(...) TU_LOG(CFG_TUD_MSC_LOG_LEVEL, __VA_ARGS__)
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// MACRO CONSTANT TYPEDEF
|
||||
//--------------------------------------------------------------------+
|
||||
enum
|
||||
{
|
||||
MSC_STAGE_CMD = 0,
|
||||
MSC_STAGE_DATA,
|
||||
MSC_STAGE_STATUS,
|
||||
MSC_STAGE_STATUS_SENT,
|
||||
MSC_STAGE_NEED_RESET,
|
||||
};
|
||||
|
||||
typedef struct
|
||||
{
|
||||
// TODO optimize alignment
|
||||
CFG_TUSB_MEM_ALIGN msc_cbw_t cbw;
|
||||
CFG_TUSB_MEM_ALIGN msc_csw_t csw;
|
||||
|
||||
uint8_t itf_num;
|
||||
uint8_t ep_in;
|
||||
uint8_t ep_out;
|
||||
|
||||
// Bulk Only Transfer (BOT) Protocol
|
||||
uint8_t stage;
|
||||
uint32_t total_len; // byte to be transferred, can be smaller than total_bytes in cbw
|
||||
uint32_t xferred_len; // numbered of bytes transferred so far in the Data Stage
|
||||
|
||||
// Sense Response Data
|
||||
uint8_t sense_key;
|
||||
uint8_t add_sense_code;
|
||||
uint8_t add_sense_qualifier;
|
||||
}mscd_interface_t;
|
||||
|
||||
CFG_TUD_MEM_SECTION CFG_TUSB_MEM_ALIGN tu_static mscd_interface_t _mscd_itf;
|
||||
CFG_TUD_MEM_SECTION CFG_TUSB_MEM_ALIGN tu_static uint8_t _mscd_buf[CFG_TUD_MSC_EP_BUFSIZE];
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// INTERNAL OBJECT & FUNCTION DECLARATION
|
||||
//--------------------------------------------------------------------+
|
||||
static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_t* buffer, uint32_t bufsize);
|
||||
static void proc_read10_cmd(uint8_t rhport, mscd_interface_t* p_msc);
|
||||
|
||||
static void proc_write10_cmd(uint8_t rhport, mscd_interface_t* p_msc);
|
||||
static void proc_write10_new_data(uint8_t rhport, mscd_interface_t* p_msc, uint32_t xferred_bytes);
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool is_data_in(uint8_t dir)
|
||||
{
|
||||
return tu_bit_test(dir, 7);
|
||||
}
|
||||
|
||||
static inline bool send_csw(uint8_t rhport, mscd_interface_t* p_msc)
|
||||
{
|
||||
// Data residue is always = host expect - actual transferred
|
||||
p_msc->csw.data_residue = p_msc->cbw.total_bytes - p_msc->xferred_len;
|
||||
|
||||
p_msc->stage = MSC_STAGE_STATUS_SENT;
|
||||
return usbd_edpt_xfer(rhport, p_msc->ep_in , (uint8_t*) &p_msc->csw, sizeof(msc_csw_t));
|
||||
}
|
||||
|
||||
static inline bool prepare_cbw(uint8_t rhport, mscd_interface_t* p_msc)
|
||||
{
|
||||
p_msc->stage = MSC_STAGE_CMD;
|
||||
return usbd_edpt_xfer(rhport, p_msc->ep_out, (uint8_t*) &p_msc->cbw, sizeof(msc_cbw_t));
|
||||
}
|
||||
|
||||
static void fail_scsi_op(uint8_t rhport, mscd_interface_t* p_msc, uint8_t status)
|
||||
{
|
||||
msc_cbw_t const * p_cbw = &p_msc->cbw;
|
||||
msc_csw_t * p_csw = &p_msc->csw;
|
||||
|
||||
p_csw->status = status;
|
||||
p_csw->data_residue = p_msc->cbw.total_bytes - p_msc->xferred_len;
|
||||
p_msc->stage = MSC_STAGE_STATUS;
|
||||
|
||||
// failed but sense key is not set: default to Illegal Request
|
||||
if ( p_msc->sense_key == 0 ) tud_msc_set_sense(p_cbw->lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00);
|
||||
|
||||
// If there is data stage and not yet complete, stall it
|
||||
if ( p_cbw->total_bytes && p_csw->data_residue )
|
||||
{
|
||||
if ( is_data_in(p_cbw->dir) )
|
||||
{
|
||||
usbd_edpt_stall(rhport, p_msc->ep_in);
|
||||
}
|
||||
else
|
||||
{
|
||||
usbd_edpt_stall(rhport, p_msc->ep_out);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static inline uint32_t rdwr10_get_lba(uint8_t const command[])
|
||||
{
|
||||
// use offsetof to avoid pointer to the odd/unaligned address
|
||||
uint32_t const lba = tu_unaligned_read32(command + offsetof(scsi_write10_t, lba));
|
||||
|
||||
// lba is in Big Endian
|
||||
return tu_ntohl(lba);
|
||||
}
|
||||
|
||||
static inline uint16_t rdwr10_get_blockcount(msc_cbw_t const* cbw)
|
||||
{
|
||||
uint16_t const block_count = tu_unaligned_read16(cbw->command + offsetof(scsi_write10_t, block_count));
|
||||
return tu_ntohs(block_count);
|
||||
}
|
||||
|
||||
static inline uint16_t rdwr10_get_blocksize(msc_cbw_t const* cbw)
|
||||
{
|
||||
// first extract block count in the command
|
||||
uint16_t const block_count = rdwr10_get_blockcount(cbw);
|
||||
|
||||
// invalid block count
|
||||
if (block_count == 0) return 0;
|
||||
|
||||
return (uint16_t) (cbw->total_bytes / block_count);
|
||||
}
|
||||
|
||||
uint8_t rdwr10_validate_cmd(msc_cbw_t const* cbw)
|
||||
{
|
||||
uint8_t status = MSC_CSW_STATUS_PASSED;
|
||||
uint16_t const block_count = rdwr10_get_blockcount(cbw);
|
||||
|
||||
if ( cbw->total_bytes == 0 )
|
||||
{
|
||||
if ( block_count )
|
||||
{
|
||||
TU_LOG_DRV(" SCSI case 2 (Hn < Di) or case 3 (Hn < Do) \r\n");
|
||||
status = MSC_CSW_STATUS_PHASE_ERROR;
|
||||
}else
|
||||
{
|
||||
// no data transfer, only exist in complaint test suite
|
||||
}
|
||||
}else
|
||||
{
|
||||
if ( SCSI_CMD_READ_10 == cbw->command[0] && !is_data_in(cbw->dir) )
|
||||
{
|
||||
TU_LOG_DRV(" SCSI case 10 (Ho <> Di)\r\n");
|
||||
status = MSC_CSW_STATUS_PHASE_ERROR;
|
||||
}
|
||||
else if ( SCSI_CMD_WRITE_10 == cbw->command[0] && is_data_in(cbw->dir) )
|
||||
{
|
||||
TU_LOG_DRV(" SCSI case 8 (Hi <> Do)\r\n");
|
||||
status = MSC_CSW_STATUS_PHASE_ERROR;
|
||||
}
|
||||
else if ( 0 == block_count )
|
||||
{
|
||||
TU_LOG_DRV(" SCSI case 4 Hi > Dn (READ10) or case 9 Ho > Dn (WRITE10) \r\n");
|
||||
status = MSC_CSW_STATUS_FAILED;
|
||||
}
|
||||
else if ( cbw->total_bytes / block_count == 0 )
|
||||
{
|
||||
TU_LOG_DRV(" Computed block size = 0. SCSI case 7 Hi < Di (READ10) or case 13 Ho < Do (WRIT10)\r\n");
|
||||
status = MSC_CSW_STATUS_PHASE_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Debug
|
||||
//--------------------------------------------------------------------+
|
||||
#if CFG_TUSB_DEBUG >= 2
|
||||
|
||||
TU_ATTR_UNUSED tu_static tu_lookup_entry_t const _msc_scsi_cmd_lookup[] =
|
||||
{
|
||||
{ .key = SCSI_CMD_TEST_UNIT_READY , .data = "Test Unit Ready" },
|
||||
{ .key = SCSI_CMD_INQUIRY , .data = "Inquiry" },
|
||||
{ .key = SCSI_CMD_MODE_SELECT_6 , .data = "Mode_Select 6" },
|
||||
{ .key = SCSI_CMD_MODE_SENSE_6 , .data = "Mode_Sense 6" },
|
||||
{ .key = SCSI_CMD_START_STOP_UNIT , .data = "Start Stop Unit" },
|
||||
{ .key = SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL , .data = "Prevent/Allow Medium Removal" },
|
||||
{ .key = SCSI_CMD_READ_CAPACITY_10 , .data = "Read Capacity10" },
|
||||
{ .key = SCSI_CMD_REQUEST_SENSE , .data = "Request Sense" },
|
||||
{ .key = SCSI_CMD_READ_FORMAT_CAPACITY , .data = "Read Format Capacity" },
|
||||
{ .key = SCSI_CMD_READ_10 , .data = "Read10" },
|
||||
{ .key = SCSI_CMD_WRITE_10 , .data = "Write10" }
|
||||
};
|
||||
|
||||
TU_ATTR_UNUSED tu_static tu_lookup_table_t const _msc_scsi_cmd_table =
|
||||
{
|
||||
.count = TU_ARRAY_SIZE(_msc_scsi_cmd_lookup),
|
||||
.items = _msc_scsi_cmd_lookup
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// APPLICATION API
|
||||
//--------------------------------------------------------------------+
|
||||
bool tud_msc_set_sense(uint8_t lun, uint8_t sense_key, uint8_t add_sense_code, uint8_t add_sense_qualifier)
|
||||
{
|
||||
(void) lun;
|
||||
|
||||
_mscd_itf.sense_key = sense_key;
|
||||
_mscd_itf.add_sense_code = add_sense_code;
|
||||
_mscd_itf.add_sense_qualifier = add_sense_qualifier;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static inline void set_sense_medium_not_present(uint8_t lun)
|
||||
{
|
||||
// default sense is NOT READY, MEDIUM NOT PRESENT
|
||||
tud_msc_set_sense(lun, SCSI_SENSE_NOT_READY, 0x3A, 0x00);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// USBD Driver API
|
||||
//--------------------------------------------------------------------+
|
||||
void mscd_init(void)
|
||||
{
|
||||
tu_memclr(&_mscd_itf, sizeof(mscd_interface_t));
|
||||
}
|
||||
|
||||
void mscd_reset(uint8_t rhport)
|
||||
{
|
||||
(void) rhport;
|
||||
tu_memclr(&_mscd_itf, sizeof(mscd_interface_t));
|
||||
}
|
||||
|
||||
uint16_t mscd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len)
|
||||
{
|
||||
// only support SCSI's BOT protocol
|
||||
TU_VERIFY(TUSB_CLASS_MSC == itf_desc->bInterfaceClass &&
|
||||
MSC_SUBCLASS_SCSI == itf_desc->bInterfaceSubClass &&
|
||||
MSC_PROTOCOL_BOT == itf_desc->bInterfaceProtocol, 0);
|
||||
|
||||
// msc driver length is fixed
|
||||
uint16_t const drv_len = sizeof(tusb_desc_interface_t) + 2*sizeof(tusb_desc_endpoint_t);
|
||||
|
||||
// Max length must be at least 1 interface + 2 endpoints
|
||||
TU_ASSERT(max_len >= drv_len, 0);
|
||||
|
||||
mscd_interface_t * p_msc = &_mscd_itf;
|
||||
p_msc->itf_num = itf_desc->bInterfaceNumber;
|
||||
|
||||
// Open endpoint pair
|
||||
TU_ASSERT( usbd_open_edpt_pair(rhport, tu_desc_next(itf_desc), 2, TUSB_XFER_BULK, &p_msc->ep_out, &p_msc->ep_in), 0 );
|
||||
|
||||
// Prepare for Command Block Wrapper
|
||||
TU_ASSERT( prepare_cbw(rhport, p_msc), drv_len);
|
||||
|
||||
return drv_len;
|
||||
}
|
||||
|
||||
static void proc_bot_reset(mscd_interface_t* p_msc)
|
||||
{
|
||||
p_msc->stage = MSC_STAGE_CMD;
|
||||
p_msc->total_len = 0;
|
||||
p_msc->xferred_len = 0;
|
||||
|
||||
p_msc->sense_key = 0;
|
||||
p_msc->add_sense_code = 0;
|
||||
p_msc->add_sense_qualifier = 0;
|
||||
}
|
||||
|
||||
// Invoked when a control transfer occurred on an interface of this class
|
||||
// Driver response accordingly to the request and the transfer stage (setup/data/ack)
|
||||
// return false to stall control endpoint (e.g unsupported request)
|
||||
bool mscd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request)
|
||||
{
|
||||
// nothing to do with DATA & ACK stage
|
||||
if (stage != CONTROL_STAGE_SETUP) return true;
|
||||
|
||||
mscd_interface_t* p_msc = &_mscd_itf;
|
||||
|
||||
// Clear Endpoint Feature (stall) for recovery
|
||||
if ( TUSB_REQ_TYPE_STANDARD == request->bmRequestType_bit.type &&
|
||||
TUSB_REQ_RCPT_ENDPOINT == request->bmRequestType_bit.recipient &&
|
||||
TUSB_REQ_CLEAR_FEATURE == request->bRequest &&
|
||||
TUSB_REQ_FEATURE_EDPT_HALT == request->wValue )
|
||||
{
|
||||
uint8_t const ep_addr = tu_u16_low(request->wIndex);
|
||||
|
||||
if ( p_msc->stage == MSC_STAGE_NEED_RESET )
|
||||
{
|
||||
// reset recovery is required to recover from this stage
|
||||
// Clear Stall request cannot resolve this -> continue to stall endpoint
|
||||
usbd_edpt_stall(rhport, ep_addr);
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( ep_addr == p_msc->ep_in )
|
||||
{
|
||||
if ( p_msc->stage == MSC_STAGE_STATUS )
|
||||
{
|
||||
// resume sending SCSI status if we are in this stage previously before stalled
|
||||
TU_ASSERT( send_csw(rhport, p_msc) );
|
||||
}
|
||||
}
|
||||
else if ( ep_addr == p_msc->ep_out )
|
||||
{
|
||||
if ( p_msc->stage == MSC_STAGE_CMD )
|
||||
{
|
||||
// part of reset recovery (probably due to invalid CBW) -> prepare for new command
|
||||
// Note: skip if already queued previously
|
||||
if ( usbd_edpt_ready(rhport, p_msc->ep_out) )
|
||||
{
|
||||
TU_ASSERT( prepare_cbw(rhport, p_msc) );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// From this point only handle class request only
|
||||
TU_VERIFY(request->bmRequestType_bit.type == TUSB_REQ_TYPE_CLASS);
|
||||
|
||||
switch ( request->bRequest )
|
||||
{
|
||||
case MSC_REQ_RESET:
|
||||
TU_LOG_DRV(" MSC BOT Reset\r\n");
|
||||
TU_VERIFY(request->wValue == 0 && request->wLength == 0);
|
||||
|
||||
// driver state reset
|
||||
proc_bot_reset(p_msc);
|
||||
|
||||
tud_control_status(rhport, request);
|
||||
break;
|
||||
|
||||
case MSC_REQ_GET_MAX_LUN:
|
||||
{
|
||||
TU_LOG_DRV(" MSC Get Max Lun\r\n");
|
||||
TU_VERIFY(request->wValue == 0 && request->wLength == 1);
|
||||
|
||||
uint8_t maxlun = 1;
|
||||
if (tud_msc_get_maxlun_cb) maxlun = tud_msc_get_maxlun_cb();
|
||||
TU_VERIFY(maxlun);
|
||||
|
||||
// MAX LUN is minus 1 by specs
|
||||
maxlun--;
|
||||
|
||||
tud_control_xfer(rhport, request, &maxlun, 1);
|
||||
}
|
||||
break;
|
||||
|
||||
default: return false; // stall unsupported request
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes)
|
||||
{
|
||||
(void) event;
|
||||
|
||||
mscd_interface_t* p_msc = &_mscd_itf;
|
||||
msc_cbw_t const * p_cbw = &p_msc->cbw;
|
||||
msc_csw_t * p_csw = &p_msc->csw;
|
||||
|
||||
switch (p_msc->stage)
|
||||
{
|
||||
case MSC_STAGE_CMD:
|
||||
//------------- new CBW received -------------//
|
||||
// Complete IN while waiting for CMD is usually Status of previous SCSI op, ignore it
|
||||
if(ep_addr != p_msc->ep_out) return true;
|
||||
|
||||
if ( !(xferred_bytes == sizeof(msc_cbw_t) && p_cbw->signature == MSC_CBW_SIGNATURE) )
|
||||
{
|
||||
TU_LOG_DRV(" SCSI CBW is not valid\r\n");
|
||||
|
||||
// BOT 6.6.1 If CBW is not valid stall both endpoints until reset recovery
|
||||
p_msc->stage = MSC_STAGE_NEED_RESET;
|
||||
|
||||
// invalid CBW stall both endpoints
|
||||
usbd_edpt_stall(rhport, p_msc->ep_in);
|
||||
usbd_edpt_stall(rhport, p_msc->ep_out);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
TU_LOG_DRV(" SCSI Command [Lun%u]: %s\r\n", p_cbw->lun, tu_lookup_find(&_msc_scsi_cmd_table, p_cbw->command[0]));
|
||||
//TU_LOG_MEM(MSC_DEBUG, p_cbw, xferred_bytes, 2);
|
||||
|
||||
p_csw->signature = MSC_CSW_SIGNATURE;
|
||||
p_csw->tag = p_cbw->tag;
|
||||
p_csw->data_residue = 0;
|
||||
p_csw->status = MSC_CSW_STATUS_PASSED;
|
||||
|
||||
/*------------- Parse command and prepare DATA -------------*/
|
||||
p_msc->stage = MSC_STAGE_DATA;
|
||||
p_msc->total_len = p_cbw->total_bytes;
|
||||
p_msc->xferred_len = 0;
|
||||
|
||||
// Read10 or Write10
|
||||
if ( (SCSI_CMD_READ_10 == p_cbw->command[0]) || (SCSI_CMD_WRITE_10 == p_cbw->command[0]) )
|
||||
{
|
||||
uint8_t const status = rdwr10_validate_cmd(p_cbw);
|
||||
|
||||
if ( status != MSC_CSW_STATUS_PASSED)
|
||||
{
|
||||
fail_scsi_op(rhport, p_msc, status);
|
||||
}else if ( p_cbw->total_bytes )
|
||||
{
|
||||
if (SCSI_CMD_READ_10 == p_cbw->command[0])
|
||||
{
|
||||
proc_read10_cmd(rhport, p_msc);
|
||||
}else
|
||||
{
|
||||
proc_write10_cmd(rhport, p_msc);
|
||||
}
|
||||
}else
|
||||
{
|
||||
// no data transfer, only exist in complaint test suite
|
||||
p_msc->stage = MSC_STAGE_STATUS;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// For other SCSI commands
|
||||
// 1. OUT : queue transfer (invoke app callback after done)
|
||||
// 2. IN & Zero: Process if is built-in, else Invoke app callback. Skip DATA if zero length
|
||||
if ( (p_cbw->total_bytes > 0 ) && !is_data_in(p_cbw->dir) )
|
||||
{
|
||||
if (p_cbw->total_bytes > sizeof(_mscd_buf))
|
||||
{
|
||||
TU_LOG_DRV(" SCSI reject non READ10/WRITE10 with large data\r\n");
|
||||
fail_scsi_op(rhport, p_msc, MSC_CSW_STATUS_FAILED);
|
||||
}else
|
||||
{
|
||||
// Didn't check for case 9 (Ho > Dn), which requires examining scsi command first
|
||||
// but it is OK to just receive data then responded with failed status
|
||||
TU_ASSERT( usbd_edpt_xfer(rhport, p_msc->ep_out, _mscd_buf, (uint16_t) p_msc->total_len) );
|
||||
}
|
||||
}else
|
||||
{
|
||||
// First process if it is a built-in commands
|
||||
int32_t resplen = proc_builtin_scsi(p_cbw->lun, p_cbw->command, _mscd_buf, sizeof(_mscd_buf));
|
||||
|
||||
// Invoke user callback if not built-in
|
||||
if ( (resplen < 0) && (p_msc->sense_key == 0) )
|
||||
{
|
||||
resplen = tud_msc_scsi_cb(p_cbw->lun, p_cbw->command, _mscd_buf, (uint16_t) p_msc->total_len);
|
||||
}
|
||||
|
||||
if ( resplen < 0 )
|
||||
{
|
||||
// unsupported command
|
||||
TU_LOG_DRV(" SCSI unsupported or failed command\r\n");
|
||||
fail_scsi_op(rhport, p_msc, MSC_CSW_STATUS_FAILED);
|
||||
}
|
||||
else if (resplen == 0)
|
||||
{
|
||||
if (p_cbw->total_bytes)
|
||||
{
|
||||
// 6.7 The 13 Cases: case 4 (Hi > Dn)
|
||||
// TU_LOG(MSC_DEBUG, " SCSI case 4 (Hi > Dn): %lu\r\n", p_cbw->total_bytes);
|
||||
fail_scsi_op(rhport, p_msc, MSC_CSW_STATUS_FAILED);
|
||||
}else
|
||||
{
|
||||
// case 1 Hn = Dn: all good
|
||||
p_msc->stage = MSC_STAGE_STATUS;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( p_cbw->total_bytes == 0 )
|
||||
{
|
||||
// 6.7 The 13 Cases: case 2 (Hn < Di)
|
||||
// TU_LOG(MSC_DEBUG, " SCSI case 2 (Hn < Di): %lu\r\n", p_cbw->total_bytes);
|
||||
fail_scsi_op(rhport, p_msc, MSC_CSW_STATUS_FAILED);
|
||||
}else
|
||||
{
|
||||
// cannot return more than host expect
|
||||
p_msc->total_len = tu_min32((uint32_t) resplen, p_cbw->total_bytes);
|
||||
TU_ASSERT( usbd_edpt_xfer(rhport, p_msc->ep_in, _mscd_buf, (uint16_t) p_msc->total_len) );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case MSC_STAGE_DATA:
|
||||
TU_LOG_DRV(" SCSI Data [Lun%u]\r\n", p_cbw->lun);
|
||||
//TU_LOG_MEM(MSC_DEBUG, _mscd_buf, xferred_bytes, 2);
|
||||
|
||||
if (SCSI_CMD_READ_10 == p_cbw->command[0])
|
||||
{
|
||||
p_msc->xferred_len += xferred_bytes;
|
||||
|
||||
if ( p_msc->xferred_len >= p_msc->total_len )
|
||||
{
|
||||
// Data Stage is complete
|
||||
p_msc->stage = MSC_STAGE_STATUS;
|
||||
}else
|
||||
{
|
||||
proc_read10_cmd(rhport, p_msc);
|
||||
}
|
||||
}
|
||||
else if (SCSI_CMD_WRITE_10 == p_cbw->command[0])
|
||||
{
|
||||
proc_write10_new_data(rhport, p_msc, xferred_bytes);
|
||||
}
|
||||
else
|
||||
{
|
||||
p_msc->xferred_len += xferred_bytes;
|
||||
|
||||
// OUT transfer, invoke callback if needed
|
||||
if ( !is_data_in(p_cbw->dir) )
|
||||
{
|
||||
int32_t cb_result = tud_msc_scsi_cb(p_cbw->lun, p_cbw->command, _mscd_buf, (uint16_t) p_msc->total_len);
|
||||
|
||||
if ( cb_result < 0 )
|
||||
{
|
||||
// unsupported command
|
||||
TU_LOG_DRV(" SCSI unsupported command\r\n");
|
||||
fail_scsi_op(rhport, p_msc, MSC_CSW_STATUS_FAILED);
|
||||
}else
|
||||
{
|
||||
// TODO haven't implement this scenario any further yet
|
||||
}
|
||||
}
|
||||
|
||||
if ( p_msc->xferred_len >= p_msc->total_len )
|
||||
{
|
||||
// Data Stage is complete
|
||||
p_msc->stage = MSC_STAGE_STATUS;
|
||||
}
|
||||
else
|
||||
{
|
||||
// This scenario with command that take more than one transfer is already rejected at Command stage
|
||||
TU_BREAKPOINT();
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case MSC_STAGE_STATUS:
|
||||
// processed immediately after this switch, supposedly to be empty
|
||||
break;
|
||||
|
||||
case MSC_STAGE_STATUS_SENT:
|
||||
// Wait for the Status phase to complete
|
||||
if( (ep_addr == p_msc->ep_in) && (xferred_bytes == sizeof(msc_csw_t)) )
|
||||
{
|
||||
TU_LOG_DRV(" SCSI Status [Lun%u] = %u\r\n", p_cbw->lun, p_csw->status);
|
||||
// TU_LOG_MEM(MSC_DEBUG, p_csw, xferred_bytes, 2);
|
||||
|
||||
// Invoke complete callback if defined
|
||||
// Note: There is racing issue with samd51 + qspi flash testing with arduino
|
||||
// if complete_cb() is invoked after queuing the status.
|
||||
switch(p_cbw->command[0])
|
||||
{
|
||||
case SCSI_CMD_READ_10:
|
||||
if ( tud_msc_read10_complete_cb ) tud_msc_read10_complete_cb(p_cbw->lun);
|
||||
break;
|
||||
|
||||
case SCSI_CMD_WRITE_10:
|
||||
if ( tud_msc_write10_complete_cb ) tud_msc_write10_complete_cb(p_cbw->lun);
|
||||
break;
|
||||
|
||||
default:
|
||||
if ( tud_msc_scsi_complete_cb ) tud_msc_scsi_complete_cb(p_cbw->lun, p_cbw->command);
|
||||
break;
|
||||
}
|
||||
|
||||
TU_ASSERT( prepare_cbw(rhport, p_msc) );
|
||||
}else
|
||||
{
|
||||
// Any xfer ended here is consider unknown error, ignore it
|
||||
TU_LOG1(" Warning expect SCSI Status but received unknown data\r\n");
|
||||
}
|
||||
break;
|
||||
|
||||
default : break;
|
||||
}
|
||||
|
||||
if ( p_msc->stage == MSC_STAGE_STATUS )
|
||||
{
|
||||
// skip status if epin is currently stalled, will do it when received Clear Stall request
|
||||
if ( !usbd_edpt_stalled(rhport, p_msc->ep_in) )
|
||||
{
|
||||
if ( (p_cbw->total_bytes > p_msc->xferred_len) && is_data_in(p_cbw->dir) )
|
||||
{
|
||||
// 6.7 The 13 Cases: case 5 (Hi > Di): STALL before status
|
||||
// TU_LOG(MSC_DEBUG, " SCSI case 5 (Hi > Di): %lu > %lu\r\n", p_cbw->total_bytes, p_msc->xferred_len);
|
||||
usbd_edpt_stall(rhport, p_msc->ep_in);
|
||||
}else
|
||||
{
|
||||
TU_ASSERT( send_csw(rhport, p_msc) );
|
||||
}
|
||||
}
|
||||
|
||||
#if TU_CHECK_MCU(OPT_MCU_CXD56)
|
||||
// WORKAROUND: cxd56 has its own nuttx usb stack which does not forward Set/ClearFeature(Endpoint) to DCD.
|
||||
// There is no way for us to know when EP is un-stall, therefore we will unconditionally un-stall here and
|
||||
// hope everything will work
|
||||
if ( usbd_edpt_stalled(rhport, p_msc->ep_in) )
|
||||
{
|
||||
usbd_edpt_clear_stall(rhport, p_msc->ep_in);
|
||||
send_csw(rhport, p_msc);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* SCSI Command Process
|
||||
*------------------------------------------------------------------*/
|
||||
|
||||
// return response's length (copied to buffer). Negative if it is not an built-in command or indicate Failed status (CSW)
|
||||
// In case of a failed status, sense key must be set for reason of failure
|
||||
static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_t* buffer, uint32_t bufsize)
|
||||
{
|
||||
(void) bufsize; // TODO refractor later
|
||||
int32_t resplen;
|
||||
|
||||
mscd_interface_t* p_msc = &_mscd_itf;
|
||||
|
||||
switch ( scsi_cmd[0] )
|
||||
{
|
||||
case SCSI_CMD_TEST_UNIT_READY:
|
||||
resplen = 0;
|
||||
if ( !tud_msc_test_unit_ready_cb(lun) )
|
||||
{
|
||||
// Failed status response
|
||||
resplen = - 1;
|
||||
|
||||
// set default sense if not set by callback
|
||||
if ( p_msc->sense_key == 0 ) set_sense_medium_not_present(lun);
|
||||
}
|
||||
break;
|
||||
|
||||
case SCSI_CMD_START_STOP_UNIT:
|
||||
resplen = 0;
|
||||
|
||||
if (tud_msc_start_stop_cb)
|
||||
{
|
||||
scsi_start_stop_unit_t const * start_stop = (scsi_start_stop_unit_t const *) scsi_cmd;
|
||||
if ( !tud_msc_start_stop_cb(lun, start_stop->power_condition, start_stop->start, start_stop->load_eject) )
|
||||
{
|
||||
// Failed status response
|
||||
resplen = - 1;
|
||||
|
||||
// set default sense if not set by callback
|
||||
if ( p_msc->sense_key == 0 ) set_sense_medium_not_present(lun);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case SCSI_CMD_READ_CAPACITY_10:
|
||||
{
|
||||
uint32_t block_count;
|
||||
uint32_t block_size;
|
||||
uint16_t block_size_u16;
|
||||
|
||||
tud_msc_capacity_cb(lun, &block_count, &block_size_u16);
|
||||
block_size = (uint32_t) block_size_u16;
|
||||
|
||||
// Invalid block size/count from callback, possibly unit is not ready
|
||||
// stall this request, set sense key to NOT READY
|
||||
if (block_count == 0 || block_size == 0)
|
||||
{
|
||||
resplen = -1;
|
||||
|
||||
// set default sense if not set by callback
|
||||
if ( p_msc->sense_key == 0 ) set_sense_medium_not_present(lun);
|
||||
}else
|
||||
{
|
||||
scsi_read_capacity10_resp_t read_capa10;
|
||||
|
||||
read_capa10.last_lba = tu_htonl(block_count-1);
|
||||
read_capa10.block_size = tu_htonl(block_size);
|
||||
|
||||
resplen = sizeof(read_capa10);
|
||||
TU_VERIFY(0 == tu_memcpy_s(buffer, bufsize, &read_capa10, (size_t) resplen));
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case SCSI_CMD_READ_FORMAT_CAPACITY:
|
||||
{
|
||||
scsi_read_format_capacity_data_t read_fmt_capa =
|
||||
{
|
||||
.list_length = 8,
|
||||
.block_num = 0,
|
||||
.descriptor_type = 2, // formatted media
|
||||
.block_size_u16 = 0
|
||||
};
|
||||
|
||||
uint32_t block_count;
|
||||
uint16_t block_size;
|
||||
|
||||
tud_msc_capacity_cb(lun, &block_count, &block_size);
|
||||
|
||||
// Invalid block size/count from callback, possibly unit is not ready
|
||||
// stall this request, set sense key to NOT READY
|
||||
if (block_count == 0 || block_size == 0)
|
||||
{
|
||||
resplen = -1;
|
||||
|
||||
// set default sense if not set by callback
|
||||
if ( p_msc->sense_key == 0 ) set_sense_medium_not_present(lun);
|
||||
}else
|
||||
{
|
||||
read_fmt_capa.block_num = tu_htonl(block_count);
|
||||
read_fmt_capa.block_size_u16 = tu_htons(block_size);
|
||||
|
||||
resplen = sizeof(read_fmt_capa);
|
||||
TU_VERIFY(0 == tu_memcpy_s(buffer, bufsize, &read_fmt_capa, (size_t) resplen));
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case SCSI_CMD_INQUIRY:
|
||||
{
|
||||
scsi_inquiry_resp_t inquiry_rsp =
|
||||
{
|
||||
.is_removable = 1,
|
||||
.version = 2,
|
||||
.response_data_format = 2,
|
||||
.additional_length = sizeof(scsi_inquiry_resp_t) - 5,
|
||||
};
|
||||
|
||||
// vendor_id, product_id, product_rev is space padded string
|
||||
memset(inquiry_rsp.vendor_id , ' ', sizeof(inquiry_rsp.vendor_id));
|
||||
memset(inquiry_rsp.product_id , ' ', sizeof(inquiry_rsp.product_id));
|
||||
memset(inquiry_rsp.product_rev, ' ', sizeof(inquiry_rsp.product_rev));
|
||||
|
||||
tud_msc_inquiry_cb(lun, inquiry_rsp.vendor_id, inquiry_rsp.product_id, inquiry_rsp.product_rev);
|
||||
|
||||
resplen = sizeof(inquiry_rsp);
|
||||
TU_VERIFY(0 == tu_memcpy_s(buffer, bufsize, &inquiry_rsp, (size_t) resplen));
|
||||
}
|
||||
break;
|
||||
|
||||
case SCSI_CMD_MODE_SENSE_6:
|
||||
{
|
||||
scsi_mode_sense6_resp_t mode_resp =
|
||||
{
|
||||
.data_len = 3,
|
||||
.medium_type = 0,
|
||||
.write_protected = false,
|
||||
.reserved = 0,
|
||||
.block_descriptor_len = 0 // no block descriptor are included
|
||||
};
|
||||
|
||||
bool writable = true;
|
||||
if ( tud_msc_is_writable_cb )
|
||||
{
|
||||
writable = tud_msc_is_writable_cb(lun);
|
||||
}
|
||||
|
||||
mode_resp.write_protected = !writable;
|
||||
|
||||
resplen = sizeof(mode_resp);
|
||||
TU_VERIFY(0 == tu_memcpy_s(buffer, bufsize, &mode_resp, (size_t) resplen));
|
||||
}
|
||||
break;
|
||||
|
||||
case SCSI_CMD_REQUEST_SENSE:
|
||||
{
|
||||
scsi_sense_fixed_resp_t sense_rsp =
|
||||
{
|
||||
.response_code = 0x70, // current, fixed format
|
||||
.valid = 1
|
||||
};
|
||||
|
||||
sense_rsp.add_sense_len = sizeof(scsi_sense_fixed_resp_t) - 8;
|
||||
sense_rsp.sense_key = (uint8_t) (p_msc->sense_key & 0x0F);
|
||||
sense_rsp.add_sense_code = p_msc->add_sense_code;
|
||||
sense_rsp.add_sense_qualifier = p_msc->add_sense_qualifier;
|
||||
|
||||
resplen = sizeof(sense_rsp);
|
||||
TU_VERIFY(0 == tu_memcpy_s(buffer, bufsize, &sense_rsp, (size_t) resplen));
|
||||
|
||||
// request sense callback could overwrite the sense data
|
||||
if (tud_msc_request_sense_cb)
|
||||
{
|
||||
resplen = tud_msc_request_sense_cb(lun, buffer, (uint16_t) bufsize);
|
||||
}
|
||||
|
||||
// Clear sense data after copy
|
||||
tud_msc_set_sense(lun, 0, 0, 0);
|
||||
}
|
||||
break;
|
||||
|
||||
default: resplen = -1; break;
|
||||
}
|
||||
|
||||
return resplen;
|
||||
}
|
||||
|
||||
static void proc_read10_cmd(uint8_t rhport, mscd_interface_t* p_msc)
|
||||
{
|
||||
msc_cbw_t const * p_cbw = &p_msc->cbw;
|
||||
|
||||
// block size already verified not zero
|
||||
uint16_t const block_sz = rdwr10_get_blocksize(p_cbw);
|
||||
|
||||
// Adjust lba with transferred bytes
|
||||
uint32_t const lba = rdwr10_get_lba(p_cbw->command) + (p_msc->xferred_len / block_sz);
|
||||
|
||||
// remaining bytes capped at class buffer
|
||||
int32_t nbytes = (int32_t) tu_min32(sizeof(_mscd_buf), p_cbw->total_bytes-p_msc->xferred_len);
|
||||
|
||||
// Application can consume smaller bytes
|
||||
uint32_t const offset = p_msc->xferred_len % block_sz;
|
||||
nbytes = tud_msc_read10_cb(p_cbw->lun, lba, offset, _mscd_buf, (uint32_t) nbytes);
|
||||
|
||||
if ( nbytes < 0 )
|
||||
{
|
||||
// negative means error -> endpoint is stalled & status in CSW set to failed
|
||||
TU_LOG_DRV(" tud_msc_read10_cb() return -1\r\n");
|
||||
|
||||
// set sense
|
||||
set_sense_medium_not_present(p_cbw->lun);
|
||||
|
||||
fail_scsi_op(rhport, p_msc, MSC_CSW_STATUS_FAILED);
|
||||
}
|
||||
else if ( nbytes == 0 )
|
||||
{
|
||||
// zero means not ready -> simulate an transfer complete so that this driver callback will fired again
|
||||
dcd_event_xfer_complete(rhport, p_msc->ep_in, 0, XFER_RESULT_SUCCESS, false);
|
||||
}
|
||||
else
|
||||
{
|
||||
TU_ASSERT( usbd_edpt_xfer(rhport, p_msc->ep_in, _mscd_buf, (uint16_t) nbytes), );
|
||||
}
|
||||
}
|
||||
|
||||
static void proc_write10_cmd(uint8_t rhport, mscd_interface_t* p_msc)
|
||||
{
|
||||
msc_cbw_t const * p_cbw = &p_msc->cbw;
|
||||
bool writable = true;
|
||||
|
||||
if ( tud_msc_is_writable_cb )
|
||||
{
|
||||
writable = tud_msc_is_writable_cb(p_cbw->lun);
|
||||
}
|
||||
|
||||
if ( !writable )
|
||||
{
|
||||
// Not writable, complete this SCSI op with error
|
||||
// Sense = Write protected
|
||||
tud_msc_set_sense(p_cbw->lun, SCSI_SENSE_DATA_PROTECT, 0x27, 0x00);
|
||||
fail_scsi_op(rhport, p_msc, MSC_CSW_STATUS_FAILED);
|
||||
return;
|
||||
}
|
||||
|
||||
// remaining bytes capped at class buffer
|
||||
uint16_t nbytes = (uint16_t) tu_min32(sizeof(_mscd_buf), p_cbw->total_bytes-p_msc->xferred_len);
|
||||
|
||||
// Write10 callback will be called later when usb transfer complete
|
||||
TU_ASSERT( usbd_edpt_xfer(rhport, p_msc->ep_out, _mscd_buf, nbytes), );
|
||||
}
|
||||
|
||||
// process new data arrived from WRITE10
|
||||
static void proc_write10_new_data(uint8_t rhport, mscd_interface_t* p_msc, uint32_t xferred_bytes)
|
||||
{
|
||||
msc_cbw_t const * p_cbw = &p_msc->cbw;
|
||||
|
||||
// block size already verified not zero
|
||||
uint16_t const block_sz = rdwr10_get_blocksize(p_cbw);
|
||||
|
||||
// Adjust lba with transferred bytes
|
||||
uint32_t const lba = rdwr10_get_lba(p_cbw->command) + (p_msc->xferred_len / block_sz);
|
||||
|
||||
// Invoke callback to consume new data
|
||||
uint32_t const offset = p_msc->xferred_len % block_sz;
|
||||
int32_t nbytes = tud_msc_write10_cb(p_cbw->lun, lba, offset, _mscd_buf, xferred_bytes);
|
||||
|
||||
if ( nbytes < 0 )
|
||||
{
|
||||
// negative means error -> failed this scsi op
|
||||
TU_LOG_DRV(" tud_msc_write10_cb() return -1\r\n");
|
||||
|
||||
// update actual byte before failed
|
||||
p_msc->xferred_len += xferred_bytes;
|
||||
|
||||
// Set sense
|
||||
set_sense_medium_not_present(p_cbw->lun);
|
||||
|
||||
fail_scsi_op(rhport, p_msc, MSC_CSW_STATUS_FAILED);
|
||||
}else
|
||||
{
|
||||
// Application consume less than what we got (including zero)
|
||||
if ( (uint32_t) nbytes < xferred_bytes )
|
||||
{
|
||||
uint32_t const left_over = xferred_bytes - (uint32_t) nbytes;
|
||||
if ( nbytes > 0 )
|
||||
{
|
||||
p_msc->xferred_len += (uint16_t) nbytes;
|
||||
memmove(_mscd_buf, _mscd_buf+nbytes, left_over);
|
||||
}
|
||||
|
||||
// simulate an transfer complete with adjusted parameters --> callback will be invoked with adjusted parameter
|
||||
dcd_event_xfer_complete(rhport, p_msc->ep_out, left_over, XFER_RESULT_SUCCESS, false);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Application consume all bytes in our buffer
|
||||
p_msc->xferred_len += xferred_bytes;
|
||||
|
||||
if ( p_msc->xferred_len >= p_msc->total_len )
|
||||
{
|
||||
// Data Stage is complete
|
||||
p_msc->stage = MSC_STAGE_STATUS;
|
||||
}else
|
||||
{
|
||||
// prepare to receive more data from host
|
||||
proc_write10_cmd(rhport, p_msc);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
|
@ -0,0 +1,162 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#ifndef _TUSB_MSC_DEVICE_H_
|
||||
#define _TUSB_MSC_DEVICE_H_
|
||||
|
||||
#include "common/tusb_common.h"
|
||||
#include "msc.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Class Driver Configuration
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
#if !defined(CFG_TUD_MSC_EP_BUFSIZE) & defined(CFG_TUD_MSC_BUFSIZE)
|
||||
// TODO warn user to use new name later on
|
||||
// #warning CFG_TUD_MSC_BUFSIZE is renamed to CFG_TUD_MSC_EP_BUFSIZE, please update to use the new name
|
||||
#define CFG_TUD_MSC_EP_BUFSIZE CFG_TUD_MSC_BUFSIZE
|
||||
#endif
|
||||
|
||||
#ifndef CFG_TUD_MSC_EP_BUFSIZE
|
||||
#error CFG_TUD_MSC_EP_BUFSIZE must be defined, value of a block size should work well, the more the better
|
||||
#endif
|
||||
|
||||
TU_VERIFY_STATIC(CFG_TUD_MSC_EP_BUFSIZE < UINT16_MAX, "Size is not correct");
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Application API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Set SCSI sense response
|
||||
bool tud_msc_set_sense(uint8_t lun, uint8_t sense_key, uint8_t add_sense_code, uint8_t add_sense_qualifier);
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Application Callbacks (WEAK is optional)
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Invoked when received SCSI READ10 command
|
||||
// - Address = lba * BLOCK_SIZE + offset
|
||||
// - offset is only needed if CFG_TUD_MSC_EP_BUFSIZE is smaller than BLOCK_SIZE.
|
||||
//
|
||||
// - Application fill the buffer (up to bufsize) with address contents and return number of read byte. If
|
||||
// - read < bufsize : These bytes are transferred first and callback invoked again for remaining data.
|
||||
//
|
||||
// - read == 0 : Indicate application is not ready yet e.g disk I/O busy.
|
||||
// Callback invoked again with the same parameters later on.
|
||||
//
|
||||
// - read < 0 : Indicate application error e.g invalid address. This request will be STALLed
|
||||
// and return failed status in command status wrapper phase.
|
||||
int32_t tud_msc_read10_cb (uint8_t lun, uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize);
|
||||
|
||||
// Invoked when received SCSI WRITE10 command
|
||||
// - Address = lba * BLOCK_SIZE + offset
|
||||
// - offset is only needed if CFG_TUD_MSC_EP_BUFSIZE is smaller than BLOCK_SIZE.
|
||||
//
|
||||
// - Application write data from buffer to address contents (up to bufsize) and return number of written byte. If
|
||||
// - write < bufsize : callback invoked again with remaining data later on.
|
||||
//
|
||||
// - write == 0 : Indicate application is not ready yet e.g disk I/O busy.
|
||||
// Callback invoked again with the same parameters later on.
|
||||
//
|
||||
// - write < 0 : Indicate application error e.g invalid address. This request will be STALLed
|
||||
// and return failed status in command status wrapper phase.
|
||||
//
|
||||
// TODO change buffer to const uint8_t*
|
||||
int32_t tud_msc_write10_cb (uint8_t lun, uint32_t lba, uint32_t offset, uint8_t* buffer, uint32_t bufsize);
|
||||
|
||||
// Invoked when received SCSI_CMD_INQUIRY
|
||||
// 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]);
|
||||
|
||||
// Invoked when received Test Unit Ready command.
|
||||
// return true allowing host to read/write this LUN e.g SD card inserted
|
||||
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
|
||||
// Application update block count and block size
|
||||
void tud_msc_capacity_cb(uint8_t lun, uint32_t* block_count, uint16_t* block_size);
|
||||
|
||||
/**
|
||||
* Invoked when received an SCSI command not in built-in list below.
|
||||
* - READ_CAPACITY10, READ_FORMAT_CAPACITY, INQUIRY, TEST_UNIT_READY, START_STOP_UNIT, MODE_SENSE6, REQUEST_SENSE
|
||||
* - READ10 and WRITE10 has their own callbacks
|
||||
*
|
||||
* \param[in] lun Logical unit number
|
||||
* \param[in] scsi_cmd SCSI command contents which application must examine to response accordingly
|
||||
* \param[out] buffer Buffer for SCSI Data Stage.
|
||||
* - For INPUT: application must fill this with response.
|
||||
* - For OUTPUT it holds the Data from host
|
||||
* \param[in] bufsize Buffer's length.
|
||||
*
|
||||
* \return Actual bytes processed, can be zero for no-data command.
|
||||
* \retval negative Indicate error e.g unsupported command, tinyusb will \b STALL the corresponding
|
||||
* endpoint and return failed status in command status wrapper phase.
|
||||
*/
|
||||
int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer, uint16_t bufsize);
|
||||
|
||||
/*------------- Optional callbacks -------------*/
|
||||
|
||||
// Invoked when received GET_MAX_LUN request, required for multiple LUNs implementation
|
||||
TU_ATTR_WEAK uint8_t tud_msc_get_maxlun_cb(void);
|
||||
|
||||
// Invoked when received Start Stop Unit command
|
||||
// - Start = 0 : stopped power mode, if load_eject = 1 : unload disk storage
|
||||
// - Start = 1 : active mode, if load_eject = 1 : load disk storage
|
||||
TU_ATTR_WEAK bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start, bool load_eject);
|
||||
|
||||
// Invoked when received REQUEST_SENSE
|
||||
TU_ATTR_WEAK int32_t tud_msc_request_sense_cb(uint8_t lun, void* buffer, uint16_t bufsize);
|
||||
|
||||
// Invoked when Read10 command is complete
|
||||
TU_ATTR_WEAK void tud_msc_read10_complete_cb(uint8_t lun);
|
||||
|
||||
// Invoke when Write10 command is complete, can be used to flush flash caching
|
||||
TU_ATTR_WEAK void tud_msc_write10_complete_cb(uint8_t lun);
|
||||
|
||||
// Invoked when command in tud_msc_scsi_cb is complete
|
||||
TU_ATTR_WEAK void tud_msc_scsi_complete_cb(uint8_t lun, uint8_t const scsi_cmd[16]);
|
||||
|
||||
// Invoked to check if device is writable as part of SCSI WRITE10
|
||||
TU_ATTR_WEAK bool tud_msc_is_writable_cb(uint8_t lun);
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Internal Class Driver API
|
||||
//--------------------------------------------------------------------+
|
||||
void mscd_init (void);
|
||||
void mscd_reset (uint8_t rhport);
|
||||
uint16_t mscd_open (uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len);
|
||||
bool mscd_control_xfer_cb (uint8_t rhport, uint8_t stage, tusb_control_request_t const * p_request);
|
||||
bool mscd_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _TUSB_MSC_DEVICE_H_ */
|
|
@ -0,0 +1,497 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#include "tusb_option.h"
|
||||
|
||||
#if CFG_TUH_ENABLED && CFG_TUH_MSC
|
||||
|
||||
#include "host/usbh.h"
|
||||
#include "host/usbh_pvt.h"
|
||||
|
||||
#include "msc_host.h"
|
||||
|
||||
// Level where CFG_TUSB_DEBUG must be at least for this driver is logged
|
||||
#ifndef CFG_TUH_MSC_LOG_LEVEL
|
||||
#define CFG_TUH_MSC_LOG_LEVEL CFG_TUH_LOG_LEVEL
|
||||
#endif
|
||||
|
||||
#define TU_LOG_DRV(...) TU_LOG(CFG_TUH_MSC_LOG_LEVEL, __VA_ARGS__)
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// MACRO CONSTANT TYPEDEF
|
||||
//--------------------------------------------------------------------+
|
||||
enum {
|
||||
MSC_STAGE_IDLE = 0,
|
||||
MSC_STAGE_CMD,
|
||||
MSC_STAGE_DATA,
|
||||
MSC_STAGE_STATUS,
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
uint8_t itf_num;
|
||||
uint8_t ep_in;
|
||||
uint8_t ep_out;
|
||||
|
||||
uint8_t max_lun;
|
||||
|
||||
volatile bool configured; // Receive SET_CONFIGURE
|
||||
volatile bool mounted; // Enumeration is complete
|
||||
|
||||
struct {
|
||||
uint32_t block_size;
|
||||
uint32_t block_count;
|
||||
} capacity[CFG_TUH_MSC_MAXLUN];
|
||||
|
||||
//------------- SCSI -------------//
|
||||
uint8_t stage;
|
||||
void* buffer;
|
||||
tuh_msc_complete_cb_t complete_cb;
|
||||
uintptr_t complete_arg;
|
||||
|
||||
CFG_TUH_MEM_ALIGN msc_cbw_t cbw;
|
||||
CFG_TUH_MEM_ALIGN msc_csw_t csw;
|
||||
} msch_interface_t;
|
||||
|
||||
CFG_TUH_MEM_SECTION static msch_interface_t _msch_itf[CFG_TUH_DEVICE_MAX];
|
||||
|
||||
// buffer used to read scsi information when mounted
|
||||
// largest response data currently is inquiry TODO Inquiry is not part of enum anymore
|
||||
CFG_TUH_MEM_SECTION CFG_TUH_MEM_ALIGN
|
||||
static uint8_t _msch_buffer[sizeof(scsi_inquiry_resp_t)];
|
||||
|
||||
// FIXME potential nul reference
|
||||
TU_ATTR_ALWAYS_INLINE
|
||||
static inline msch_interface_t* get_itf(uint8_t dev_addr) {
|
||||
return &_msch_itf[dev_addr - 1];
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// PUBLIC API
|
||||
//--------------------------------------------------------------------+
|
||||
uint8_t tuh_msc_get_maxlun(uint8_t dev_addr) {
|
||||
msch_interface_t* p_msc = get_itf(dev_addr);
|
||||
return p_msc->max_lun;
|
||||
}
|
||||
|
||||
uint32_t tuh_msc_get_block_count(uint8_t dev_addr, uint8_t lun) {
|
||||
msch_interface_t* p_msc = get_itf(dev_addr);
|
||||
return p_msc->capacity[lun].block_count;
|
||||
}
|
||||
|
||||
uint32_t tuh_msc_get_block_size(uint8_t dev_addr, uint8_t lun) {
|
||||
msch_interface_t* p_msc = get_itf(dev_addr);
|
||||
return p_msc->capacity[lun].block_size;
|
||||
}
|
||||
|
||||
bool tuh_msc_mounted(uint8_t dev_addr) {
|
||||
msch_interface_t* p_msc = get_itf(dev_addr);
|
||||
return p_msc->mounted;
|
||||
}
|
||||
|
||||
bool tuh_msc_ready(uint8_t dev_addr) {
|
||||
msch_interface_t* p_msc = get_itf(dev_addr);
|
||||
return p_msc->mounted && !usbh_edpt_busy(dev_addr, p_msc->ep_in) && !usbh_edpt_busy(dev_addr, p_msc->ep_out);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// PUBLIC API: SCSI COMMAND
|
||||
//--------------------------------------------------------------------+
|
||||
static inline void cbw_init(msc_cbw_t* cbw, uint8_t lun) {
|
||||
tu_memclr(cbw, sizeof(msc_cbw_t));
|
||||
cbw->signature = MSC_CBW_SIGNATURE;
|
||||
cbw->tag = 0x54555342; // TUSB
|
||||
cbw->lun = lun;
|
||||
}
|
||||
|
||||
bool tuh_msc_scsi_command(uint8_t daddr, msc_cbw_t const* cbw, void* data,
|
||||
tuh_msc_complete_cb_t complete_cb, uintptr_t arg) {
|
||||
msch_interface_t* p_msc = get_itf(daddr);
|
||||
TU_VERIFY(p_msc->configured);
|
||||
|
||||
// claim endpoint
|
||||
TU_VERIFY(usbh_edpt_claim(daddr, p_msc->ep_out));
|
||||
|
||||
p_msc->cbw = *cbw;
|
||||
p_msc->stage = MSC_STAGE_CMD;
|
||||
p_msc->buffer = data;
|
||||
p_msc->complete_cb = complete_cb;
|
||||
p_msc->complete_arg = arg;
|
||||
|
||||
if (!usbh_edpt_xfer(daddr, p_msc->ep_out, (uint8_t*) &p_msc->cbw, sizeof(msc_cbw_t))) {
|
||||
usbh_edpt_release(daddr, p_msc->ep_out);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool tuh_msc_read_capacity(uint8_t dev_addr, uint8_t lun, scsi_read_capacity10_resp_t* response,
|
||||
tuh_msc_complete_cb_t complete_cb, uintptr_t arg) {
|
||||
msch_interface_t* p_msc = get_itf(dev_addr);
|
||||
TU_VERIFY(p_msc->configured);
|
||||
|
||||
msc_cbw_t cbw;
|
||||
cbw_init(&cbw, lun);
|
||||
|
||||
cbw.total_bytes = sizeof(scsi_read_capacity10_resp_t);
|
||||
cbw.dir = TUSB_DIR_IN_MASK;
|
||||
cbw.cmd_len = sizeof(scsi_read_capacity10_t);
|
||||
cbw.command[0] = SCSI_CMD_READ_CAPACITY_10;
|
||||
|
||||
return tuh_msc_scsi_command(dev_addr, &cbw, response, complete_cb, arg);
|
||||
}
|
||||
|
||||
bool tuh_msc_inquiry(uint8_t dev_addr, uint8_t lun, scsi_inquiry_resp_t* response,
|
||||
tuh_msc_complete_cb_t complete_cb, uintptr_t arg) {
|
||||
msch_interface_t* p_msc = get_itf(dev_addr);
|
||||
TU_VERIFY(p_msc->mounted);
|
||||
|
||||
msc_cbw_t cbw;
|
||||
cbw_init(&cbw, lun);
|
||||
|
||||
cbw.total_bytes = sizeof(scsi_inquiry_resp_t);
|
||||
cbw.dir = TUSB_DIR_IN_MASK;
|
||||
cbw.cmd_len = sizeof(scsi_inquiry_t);
|
||||
|
||||
scsi_inquiry_t const cmd_inquiry = {
|
||||
.cmd_code = SCSI_CMD_INQUIRY,
|
||||
.alloc_length = sizeof(scsi_inquiry_resp_t)
|
||||
};
|
||||
memcpy(cbw.command, &cmd_inquiry, cbw.cmd_len);
|
||||
|
||||
return tuh_msc_scsi_command(dev_addr, &cbw, response, complete_cb, arg);
|
||||
}
|
||||
|
||||
bool tuh_msc_test_unit_ready(uint8_t dev_addr, uint8_t lun, tuh_msc_complete_cb_t complete_cb, uintptr_t arg) {
|
||||
msch_interface_t* p_msc = get_itf(dev_addr);
|
||||
TU_VERIFY(p_msc->configured);
|
||||
|
||||
msc_cbw_t cbw;
|
||||
cbw_init(&cbw, lun);
|
||||
|
||||
cbw.total_bytes = 0;
|
||||
cbw.dir = TUSB_DIR_OUT;
|
||||
cbw.cmd_len = sizeof(scsi_test_unit_ready_t);
|
||||
cbw.command[0] = SCSI_CMD_TEST_UNIT_READY;
|
||||
cbw.command[1] = lun; // according to wiki TODO need verification
|
||||
|
||||
return tuh_msc_scsi_command(dev_addr, &cbw, NULL, complete_cb, arg);
|
||||
}
|
||||
|
||||
bool tuh_msc_request_sense(uint8_t dev_addr, uint8_t lun, void* response,
|
||||
tuh_msc_complete_cb_t complete_cb, uintptr_t arg) {
|
||||
msc_cbw_t cbw;
|
||||
cbw_init(&cbw, lun);
|
||||
|
||||
cbw.total_bytes = 18; // TODO sense response
|
||||
cbw.dir = TUSB_DIR_IN_MASK;
|
||||
cbw.cmd_len = sizeof(scsi_request_sense_t);
|
||||
|
||||
scsi_request_sense_t const cmd_request_sense = {
|
||||
.cmd_code = SCSI_CMD_REQUEST_SENSE,
|
||||
.alloc_length = 18
|
||||
};
|
||||
memcpy(cbw.command, &cmd_request_sense, cbw.cmd_len);
|
||||
|
||||
return tuh_msc_scsi_command(dev_addr, &cbw, response, complete_cb, arg);
|
||||
}
|
||||
|
||||
bool tuh_msc_read10(uint8_t dev_addr, uint8_t lun, void* buffer, uint32_t lba, uint16_t block_count,
|
||||
tuh_msc_complete_cb_t complete_cb, uintptr_t arg) {
|
||||
msch_interface_t* p_msc = get_itf(dev_addr);
|
||||
TU_VERIFY(p_msc->mounted);
|
||||
|
||||
msc_cbw_t cbw;
|
||||
cbw_init(&cbw, lun);
|
||||
|
||||
cbw.total_bytes = block_count * p_msc->capacity[lun].block_size;
|
||||
cbw.dir = TUSB_DIR_IN_MASK;
|
||||
cbw.cmd_len = sizeof(scsi_read10_t);
|
||||
|
||||
scsi_read10_t const cmd_read10 = {
|
||||
.cmd_code = SCSI_CMD_READ_10,
|
||||
.lba = tu_htonl(lba),
|
||||
.block_count = tu_htons(block_count)
|
||||
};
|
||||
memcpy(cbw.command, &cmd_read10, cbw.cmd_len);
|
||||
|
||||
return tuh_msc_scsi_command(dev_addr, &cbw, buffer, complete_cb, arg);
|
||||
}
|
||||
|
||||
bool tuh_msc_write10(uint8_t dev_addr, uint8_t lun, void const* buffer, uint32_t lba, uint16_t block_count,
|
||||
tuh_msc_complete_cb_t complete_cb, uintptr_t arg) {
|
||||
msch_interface_t* p_msc = get_itf(dev_addr);
|
||||
TU_VERIFY(p_msc->mounted);
|
||||
|
||||
msc_cbw_t cbw;
|
||||
cbw_init(&cbw, lun);
|
||||
|
||||
cbw.total_bytes = block_count * p_msc->capacity[lun].block_size;
|
||||
cbw.dir = TUSB_DIR_OUT;
|
||||
cbw.cmd_len = sizeof(scsi_write10_t);
|
||||
|
||||
scsi_write10_t const cmd_write10 = {
|
||||
.cmd_code = SCSI_CMD_WRITE_10,
|
||||
.lba = tu_htonl(lba),
|
||||
.block_count = tu_htons(block_count)
|
||||
};
|
||||
memcpy(cbw.command, &cmd_write10, cbw.cmd_len);
|
||||
|
||||
return tuh_msc_scsi_command(dev_addr, &cbw, (void*) (uintptr_t) buffer, complete_cb, arg);
|
||||
}
|
||||
|
||||
#if 0
|
||||
// MSC interface Reset (not used now)
|
||||
bool tuh_msc_reset(uint8_t dev_addr) {
|
||||
tusb_control_request_t const new_request = {
|
||||
.bmRequestType_bit = {
|
||||
.recipient = TUSB_REQ_RCPT_INTERFACE,
|
||||
.type = TUSB_REQ_TYPE_CLASS,
|
||||
.direction = TUSB_DIR_OUT
|
||||
},
|
||||
.bRequest = MSC_REQ_RESET,
|
||||
.wValue = 0,
|
||||
.wIndex = p_msc->itf_num,
|
||||
.wLength = 0
|
||||
};
|
||||
TU_ASSERT( usbh_control_xfer( dev_addr, &new_request, NULL ) );
|
||||
}
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// CLASS-USBH API
|
||||
//--------------------------------------------------------------------+
|
||||
void msch_init(void) {
|
||||
tu_memclr(_msch_itf, sizeof(_msch_itf));
|
||||
}
|
||||
|
||||
void msch_close(uint8_t dev_addr) {
|
||||
TU_VERIFY(dev_addr <= CFG_TUH_DEVICE_MAX,);
|
||||
msch_interface_t* p_msc = get_itf(dev_addr);
|
||||
TU_VERIFY(p_msc->configured,);
|
||||
|
||||
TU_LOG_DRV(" MSCh close addr = %d\r\n", dev_addr);
|
||||
|
||||
// invoke Application Callback
|
||||
if (p_msc->mounted) {
|
||||
if (tuh_msc_umount_cb) tuh_msc_umount_cb(dev_addr);
|
||||
}
|
||||
|
||||
tu_memclr(p_msc, sizeof(msch_interface_t));
|
||||
}
|
||||
|
||||
bool msch_xfer_cb(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes) {
|
||||
msch_interface_t* p_msc = get_itf(dev_addr);
|
||||
msc_cbw_t const * cbw = &p_msc->cbw;
|
||||
msc_csw_t * csw = &p_msc->csw;
|
||||
|
||||
switch (p_msc->stage) {
|
||||
case MSC_STAGE_CMD:
|
||||
// Must be Command Block
|
||||
TU_ASSERT(ep_addr == p_msc->ep_out && event == XFER_RESULT_SUCCESS && xferred_bytes == sizeof(msc_cbw_t));
|
||||
|
||||
if (cbw->total_bytes && p_msc->buffer) {
|
||||
// Data stage if any
|
||||
p_msc->stage = MSC_STAGE_DATA;
|
||||
uint8_t const ep_data = (cbw->dir & TUSB_DIR_IN_MASK) ? p_msc->ep_in : p_msc->ep_out;
|
||||
TU_ASSERT(usbh_edpt_xfer(dev_addr, ep_data, p_msc->buffer, (uint16_t) cbw->total_bytes));
|
||||
} else {
|
||||
// Status stage
|
||||
p_msc->stage = MSC_STAGE_STATUS;
|
||||
TU_ASSERT(usbh_edpt_xfer(dev_addr, p_msc->ep_in, (uint8_t*) &p_msc->csw, (uint16_t) sizeof(msc_csw_t)));
|
||||
}
|
||||
break;
|
||||
|
||||
case MSC_STAGE_DATA:
|
||||
// Status stage
|
||||
p_msc->stage = MSC_STAGE_STATUS;
|
||||
TU_ASSERT(usbh_edpt_xfer(dev_addr, p_msc->ep_in, (uint8_t*) &p_msc->csw, (uint16_t) sizeof(msc_csw_t)));
|
||||
break;
|
||||
|
||||
case MSC_STAGE_STATUS:
|
||||
// SCSI op is complete
|
||||
p_msc->stage = MSC_STAGE_IDLE;
|
||||
|
||||
if (p_msc->complete_cb) {
|
||||
tuh_msc_complete_data_t const cb_data = {
|
||||
.cbw = cbw,
|
||||
.csw = csw,
|
||||
.scsi_data = p_msc->buffer,
|
||||
.user_arg = p_msc->complete_arg
|
||||
};
|
||||
p_msc->complete_cb(dev_addr, &cb_data);
|
||||
}
|
||||
break;
|
||||
|
||||
// unknown state
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// MSC Enumeration
|
||||
//--------------------------------------------------------------------+
|
||||
static void config_get_maxlun_complete(tuh_xfer_t* xfer);
|
||||
static bool config_test_unit_ready_complete(uint8_t dev_addr, tuh_msc_complete_data_t const* cb_data);
|
||||
static bool config_request_sense_complete(uint8_t dev_addr, tuh_msc_complete_data_t const* cb_data);
|
||||
static bool config_read_capacity_complete(uint8_t dev_addr, tuh_msc_complete_data_t const* cb_data);
|
||||
|
||||
bool msch_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const* desc_itf, uint16_t max_len) {
|
||||
(void) rhport;
|
||||
TU_VERIFY (MSC_SUBCLASS_SCSI == desc_itf->bInterfaceSubClass &&
|
||||
MSC_PROTOCOL_BOT == desc_itf->bInterfaceProtocol);
|
||||
|
||||
// msc driver length is fixed
|
||||
uint16_t const drv_len = (uint16_t) (sizeof(tusb_desc_interface_t) +
|
||||
desc_itf->bNumEndpoints * sizeof(tusb_desc_endpoint_t));
|
||||
TU_ASSERT(drv_len <= max_len);
|
||||
|
||||
msch_interface_t* p_msc = get_itf(dev_addr);
|
||||
tusb_desc_endpoint_t const* ep_desc = (tusb_desc_endpoint_t const*) tu_desc_next(desc_itf);
|
||||
|
||||
for (uint32_t i = 0; i < 2; i++) {
|
||||
TU_ASSERT(TUSB_DESC_ENDPOINT == ep_desc->bDescriptorType && TUSB_XFER_BULK == ep_desc->bmAttributes.xfer);
|
||||
TU_ASSERT(tuh_edpt_open(dev_addr, ep_desc));
|
||||
|
||||
if (TUSB_DIR_IN == tu_edpt_dir(ep_desc->bEndpointAddress)) {
|
||||
p_msc->ep_in = ep_desc->bEndpointAddress;
|
||||
} else {
|
||||
p_msc->ep_out = ep_desc->bEndpointAddress;
|
||||
}
|
||||
|
||||
ep_desc = (tusb_desc_endpoint_t const*) tu_desc_next(ep_desc);
|
||||
}
|
||||
|
||||
p_msc->itf_num = desc_itf->bInterfaceNumber;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool msch_set_config(uint8_t dev_addr, uint8_t itf_num) {
|
||||
msch_interface_t* p_msc = get_itf(dev_addr);
|
||||
TU_ASSERT(p_msc->itf_num == itf_num);
|
||||
|
||||
p_msc->configured = true;
|
||||
|
||||
//------------- Get Max Lun -------------//
|
||||
TU_LOG_DRV("MSC Get Max Lun\r\n");
|
||||
tusb_control_request_t const request = {
|
||||
.bmRequestType_bit = {
|
||||
.recipient = TUSB_REQ_RCPT_INTERFACE,
|
||||
.type = TUSB_REQ_TYPE_CLASS,
|
||||
.direction = TUSB_DIR_IN
|
||||
},
|
||||
.bRequest = MSC_REQ_GET_MAX_LUN,
|
||||
.wValue = 0,
|
||||
.wIndex = itf_num,
|
||||
.wLength = 1
|
||||
};
|
||||
|
||||
tuh_xfer_t xfer = {
|
||||
.daddr = dev_addr,
|
||||
.ep_addr = 0,
|
||||
.setup = &request,
|
||||
.buffer = _msch_buffer,
|
||||
.complete_cb = config_get_maxlun_complete,
|
||||
.user_data = 0
|
||||
};
|
||||
TU_ASSERT(tuh_control_xfer(&xfer));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static void config_get_maxlun_complete(tuh_xfer_t* xfer) {
|
||||
uint8_t const daddr = xfer->daddr;
|
||||
msch_interface_t* p_msc = get_itf(daddr);
|
||||
|
||||
// STALL means zero
|
||||
p_msc->max_lun = (XFER_RESULT_SUCCESS == xfer->result) ? _msch_buffer[0] : 0;
|
||||
p_msc->max_lun++; // MAX LUN is minus 1 by specs
|
||||
|
||||
TU_LOG_DRV(" Max LUN = %u\r\n", p_msc->max_lun);
|
||||
|
||||
// TODO multiple LUN support
|
||||
TU_LOG_DRV("SCSI Test Unit Ready\r\n");
|
||||
uint8_t const lun = 0;
|
||||
tuh_msc_test_unit_ready(daddr, lun, config_test_unit_ready_complete, 0);
|
||||
}
|
||||
|
||||
static bool config_test_unit_ready_complete(uint8_t dev_addr, tuh_msc_complete_data_t const* cb_data) {
|
||||
msc_cbw_t const* cbw = cb_data->cbw;
|
||||
msc_csw_t const* csw = cb_data->csw;
|
||||
|
||||
if (csw->status == 0) {
|
||||
// Unit is ready, read its capacity
|
||||
TU_LOG_DRV("SCSI Read Capacity\r\n");
|
||||
tuh_msc_read_capacity(dev_addr, cbw->lun, (scsi_read_capacity10_resp_t*) ((void*) _msch_buffer),
|
||||
config_read_capacity_complete, 0);
|
||||
} else {
|
||||
// Note: During enumeration, some device fails Test Unit Ready and require a few retries
|
||||
// with Request Sense to start working !!
|
||||
// TODO limit number of retries
|
||||
TU_LOG_DRV("SCSI Request Sense\r\n");
|
||||
TU_ASSERT(tuh_msc_request_sense(dev_addr, cbw->lun, _msch_buffer, config_request_sense_complete, 0));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool config_request_sense_complete(uint8_t dev_addr, tuh_msc_complete_data_t const* cb_data) {
|
||||
msc_cbw_t const* cbw = cb_data->cbw;
|
||||
msc_csw_t const* csw = cb_data->csw;
|
||||
|
||||
TU_ASSERT(csw->status == 0);
|
||||
TU_ASSERT(tuh_msc_test_unit_ready(dev_addr, cbw->lun, config_test_unit_ready_complete, 0));
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool config_read_capacity_complete(uint8_t dev_addr, tuh_msc_complete_data_t const* cb_data) {
|
||||
msc_cbw_t const* cbw = cb_data->cbw;
|
||||
msc_csw_t const* csw = cb_data->csw;
|
||||
|
||||
TU_ASSERT(csw->status == 0);
|
||||
|
||||
msch_interface_t* p_msc = get_itf(dev_addr);
|
||||
|
||||
// Capacity response field: Block size and Last LBA are both Big-Endian
|
||||
scsi_read_capacity10_resp_t* resp = (scsi_read_capacity10_resp_t*) ((void*) _msch_buffer);
|
||||
p_msc->capacity[cbw->lun].block_count = tu_ntohl(resp->last_lba) + 1;
|
||||
p_msc->capacity[cbw->lun].block_size = tu_ntohl(resp->block_size);
|
||||
|
||||
// Mark enumeration is complete
|
||||
p_msc->mounted = true;
|
||||
if (tuh_msc_mount_cb) tuh_msc_mount_cb(dev_addr);
|
||||
|
||||
// notify usbh that driver enumeration is complete
|
||||
usbh_driver_set_config_complete(dev_addr, p_msc->itf_num);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#endif
|
|
@ -0,0 +1,126 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#ifndef TUSB_MSC_HOST_H_
|
||||
#define TUSB_MSC_HOST_H_
|
||||
|
||||
#include "msc.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Class Driver Configuration
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
#ifndef CFG_TUH_MSC_MAXLUN
|
||||
#define CFG_TUH_MSC_MAXLUN 4
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
msc_cbw_t const* cbw; // SCSI command
|
||||
msc_csw_t const* csw; // SCSI status
|
||||
void* scsi_data; // SCSI Data
|
||||
uintptr_t user_arg; // user argument
|
||||
}tuh_msc_complete_data_t;
|
||||
|
||||
typedef bool (*tuh_msc_complete_cb_t)(uint8_t dev_addr, tuh_msc_complete_data_t const* cb_data);
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Application API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Check if device supports MassStorage interface.
|
||||
// This function true after tuh_msc_mounted_cb() and false after tuh_msc_unmounted_cb()
|
||||
bool tuh_msc_mounted(uint8_t dev_addr);
|
||||
|
||||
// Check if the interface is currently ready or busy transferring data
|
||||
bool tuh_msc_ready(uint8_t dev_addr);
|
||||
|
||||
// Get Max Lun
|
||||
uint8_t tuh_msc_get_maxlun(uint8_t dev_addr);
|
||||
|
||||
// Get number of block
|
||||
uint32_t tuh_msc_get_block_count(uint8_t dev_addr, uint8_t lun);
|
||||
|
||||
// Get block size in bytes
|
||||
uint32_t tuh_msc_get_block_size(uint8_t dev_addr, uint8_t lun);
|
||||
|
||||
// Perform a full SCSI command (cbw, data, csw) in non-blocking manner.
|
||||
// Complete callback is invoked when SCSI op is complete.
|
||||
// return true if success, false if there is already pending operation.
|
||||
bool tuh_msc_scsi_command(uint8_t daddr, msc_cbw_t const* cbw, void* data, tuh_msc_complete_cb_t complete_cb, uintptr_t arg);
|
||||
|
||||
// Perform SCSI Inquiry command
|
||||
// Complete callback is invoked when SCSI op is complete.
|
||||
bool tuh_msc_inquiry(uint8_t dev_addr, uint8_t lun, scsi_inquiry_resp_t* response, tuh_msc_complete_cb_t complete_cb, uintptr_t arg);
|
||||
|
||||
// Perform SCSI Test Unit Ready command
|
||||
// Complete callback is invoked when SCSI op is complete.
|
||||
bool tuh_msc_test_unit_ready(uint8_t dev_addr, uint8_t lun, tuh_msc_complete_cb_t complete_cb, uintptr_t arg);
|
||||
|
||||
// Perform SCSI Request Sense 10 command
|
||||
// Complete callback is invoked when SCSI op is complete.
|
||||
bool tuh_msc_request_sense(uint8_t dev_addr, uint8_t lun, void *response, tuh_msc_complete_cb_t complete_cb, uintptr_t arg);
|
||||
|
||||
// Perform SCSI Read 10 command. Read n blocks starting from LBA to buffer
|
||||
// Complete callback is invoked when SCSI op is complete.
|
||||
bool tuh_msc_read10(uint8_t dev_addr, uint8_t lun, void * buffer, uint32_t lba, uint16_t block_count, tuh_msc_complete_cb_t complete_cb, uintptr_t arg);
|
||||
|
||||
// Perform SCSI Write 10 command. Write n blocks starting from LBA to device
|
||||
// Complete callback is invoked when SCSI op is complete.
|
||||
bool tuh_msc_write10(uint8_t dev_addr, uint8_t lun, void const * buffer, uint32_t lba, uint16_t block_count, tuh_msc_complete_cb_t complete_cb, uintptr_t arg);
|
||||
|
||||
// Perform SCSI Read Capacity 10 command
|
||||
// Complete callback is invoked when SCSI op is complete.
|
||||
// Note: during enumeration, host stack already carried out this request. Application can retrieve capacity by
|
||||
// simply call tuh_msc_get_block_count() and tuh_msc_get_block_size()
|
||||
bool tuh_msc_read_capacity(uint8_t dev_addr, uint8_t lun, scsi_read_capacity10_resp_t* response, tuh_msc_complete_cb_t complete_cb, uintptr_t arg);
|
||||
|
||||
//------------- Application Callback -------------//
|
||||
|
||||
// Invoked when a device with MassStorage interface is mounted
|
||||
TU_ATTR_WEAK void tuh_msc_mount_cb(uint8_t dev_addr);
|
||||
|
||||
// Invoked when a device with MassStorage interface is unmounted
|
||||
TU_ATTR_WEAK void tuh_msc_umount_cb(uint8_t dev_addr);
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Internal Class Driver API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
void msch_init (void);
|
||||
bool msch_open (uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *desc_itf, uint16_t max_len);
|
||||
bool msch_set_config (uint8_t dev_addr, uint8_t itf_num);
|
||||
void msch_close (uint8_t dev_addr);
|
||||
bool msch_xfer_cb (uint8_t dev_addr, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -0,0 +1,287 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#include "tusb_option.h"
|
||||
|
||||
#if (CFG_TUD_ENABLED && CFG_TUD_VENDOR)
|
||||
|
||||
#include "device/usbd.h"
|
||||
#include "device/usbd_pvt.h"
|
||||
|
||||
#include "vendor_device.h"
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// MACRO CONSTANT TYPEDEF
|
||||
//--------------------------------------------------------------------+
|
||||
typedef struct
|
||||
{
|
||||
uint8_t itf_num;
|
||||
uint8_t ep_in;
|
||||
uint8_t ep_out;
|
||||
|
||||
/*------------- From this point, data is not cleared by bus reset -------------*/
|
||||
tu_fifo_t rx_ff;
|
||||
tu_fifo_t tx_ff;
|
||||
|
||||
uint8_t rx_ff_buf[CFG_TUD_VENDOR_RX_BUFSIZE];
|
||||
uint8_t tx_ff_buf[CFG_TUD_VENDOR_TX_BUFSIZE];
|
||||
|
||||
#if CFG_FIFO_MUTEX
|
||||
osal_mutex_def_t rx_ff_mutex;
|
||||
osal_mutex_def_t tx_ff_mutex;
|
||||
#endif
|
||||
|
||||
// Endpoint Transfer buffer
|
||||
CFG_TUSB_MEM_ALIGN uint8_t epout_buf[CFG_TUD_VENDOR_EPSIZE];
|
||||
CFG_TUSB_MEM_ALIGN uint8_t epin_buf[CFG_TUD_VENDOR_EPSIZE];
|
||||
} vendord_interface_t;
|
||||
|
||||
CFG_TUD_MEM_SECTION tu_static vendord_interface_t _vendord_itf[CFG_TUD_VENDOR];
|
||||
|
||||
#define ITF_MEM_RESET_SIZE offsetof(vendord_interface_t, rx_ff)
|
||||
|
||||
|
||||
bool tud_vendor_n_mounted (uint8_t itf)
|
||||
{
|
||||
return _vendord_itf[itf].ep_in && _vendord_itf[itf].ep_out;
|
||||
}
|
||||
|
||||
uint32_t tud_vendor_n_available (uint8_t itf)
|
||||
{
|
||||
return tu_fifo_count(&_vendord_itf[itf].rx_ff);
|
||||
}
|
||||
|
||||
bool tud_vendor_n_peek(uint8_t itf, uint8_t* u8)
|
||||
{
|
||||
return tu_fifo_peek(&_vendord_itf[itf].rx_ff, u8);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Read API
|
||||
//--------------------------------------------------------------------+
|
||||
static void _prep_out_transaction (vendord_interface_t* p_itf)
|
||||
{
|
||||
uint8_t const rhport = 0;
|
||||
|
||||
// claim endpoint
|
||||
TU_VERIFY(usbd_edpt_claim(rhport, p_itf->ep_out), );
|
||||
|
||||
// Prepare for incoming data but only allow what we can store in the ring buffer.
|
||||
uint16_t max_read = tu_fifo_remaining(&p_itf->rx_ff);
|
||||
if ( max_read >= CFG_TUD_VENDOR_EPSIZE )
|
||||
{
|
||||
usbd_edpt_xfer(rhport, p_itf->ep_out, p_itf->epout_buf, CFG_TUD_VENDOR_EPSIZE);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Release endpoint since we don't make any transfer
|
||||
usbd_edpt_release(rhport, p_itf->ep_out);
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t tud_vendor_n_read (uint8_t itf, void* buffer, uint32_t bufsize)
|
||||
{
|
||||
vendord_interface_t* p_itf = &_vendord_itf[itf];
|
||||
uint32_t num_read = tu_fifo_read_n(&p_itf->rx_ff, buffer, (uint16_t) bufsize);
|
||||
_prep_out_transaction(p_itf);
|
||||
return num_read;
|
||||
}
|
||||
|
||||
void tud_vendor_n_read_flush (uint8_t itf)
|
||||
{
|
||||
vendord_interface_t* p_itf = &_vendord_itf[itf];
|
||||
tu_fifo_clear(&p_itf->rx_ff);
|
||||
_prep_out_transaction(p_itf);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Write API
|
||||
//--------------------------------------------------------------------+
|
||||
uint32_t tud_vendor_n_write (uint8_t itf, void const* buffer, uint32_t bufsize)
|
||||
{
|
||||
vendord_interface_t* p_itf = &_vendord_itf[itf];
|
||||
uint16_t ret = tu_fifo_write_n(&p_itf->tx_ff, buffer, (uint16_t) bufsize);
|
||||
|
||||
// flush if queue more than packet size
|
||||
if (tu_fifo_count(&p_itf->tx_ff) >= CFG_TUD_VENDOR_EPSIZE) {
|
||||
tud_vendor_n_write_flush(itf);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
uint32_t tud_vendor_n_write_flush (uint8_t itf)
|
||||
{
|
||||
vendord_interface_t* p_itf = &_vendord_itf[itf];
|
||||
|
||||
// Skip if usb is not ready yet
|
||||
TU_VERIFY( tud_ready(), 0 );
|
||||
|
||||
// No data to send
|
||||
if ( !tu_fifo_count(&p_itf->tx_ff) ) return 0;
|
||||
|
||||
uint8_t const rhport = 0;
|
||||
|
||||
// Claim the endpoint
|
||||
TU_VERIFY( usbd_edpt_claim(rhport, p_itf->ep_in), 0 );
|
||||
|
||||
// Pull data from FIFO
|
||||
uint16_t const count = tu_fifo_read_n(&p_itf->tx_ff, p_itf->epin_buf, sizeof(p_itf->epin_buf));
|
||||
|
||||
if ( count )
|
||||
{
|
||||
TU_ASSERT( usbd_edpt_xfer(rhport, p_itf->ep_in, p_itf->epin_buf, count), 0 );
|
||||
return count;
|
||||
}else
|
||||
{
|
||||
// Release endpoint since we don't make any transfer
|
||||
// Note: data is dropped if terminal is not connected
|
||||
usbd_edpt_release(rhport, p_itf->ep_in);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t tud_vendor_n_write_available (uint8_t itf)
|
||||
{
|
||||
return tu_fifo_remaining(&_vendord_itf[itf].tx_ff);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// USBD Driver API
|
||||
//--------------------------------------------------------------------+
|
||||
void vendord_init(void)
|
||||
{
|
||||
tu_memclr(_vendord_itf, sizeof(_vendord_itf));
|
||||
|
||||
for(uint8_t i=0; i<CFG_TUD_VENDOR; i++)
|
||||
{
|
||||
vendord_interface_t* p_itf = &_vendord_itf[i];
|
||||
|
||||
// config fifo
|
||||
tu_fifo_config(&p_itf->rx_ff, p_itf->rx_ff_buf, CFG_TUD_VENDOR_RX_BUFSIZE, 1, false);
|
||||
tu_fifo_config(&p_itf->tx_ff, p_itf->tx_ff_buf, CFG_TUD_VENDOR_TX_BUFSIZE, 1, false);
|
||||
|
||||
#if CFG_FIFO_MUTEX
|
||||
tu_fifo_config_mutex(&p_itf->rx_ff, NULL, osal_mutex_create(&p_itf->rx_ff_mutex));
|
||||
tu_fifo_config_mutex(&p_itf->tx_ff, osal_mutex_create(&p_itf->tx_ff_mutex), NULL);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
void vendord_reset(uint8_t rhport)
|
||||
{
|
||||
(void) rhport;
|
||||
|
||||
for(uint8_t i=0; i<CFG_TUD_VENDOR; i++)
|
||||
{
|
||||
vendord_interface_t* p_itf = &_vendord_itf[i];
|
||||
|
||||
tu_memclr(p_itf, ITF_MEM_RESET_SIZE);
|
||||
tu_fifo_clear(&p_itf->rx_ff);
|
||||
tu_fifo_clear(&p_itf->tx_ff);
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t vendord_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, uint16_t max_len)
|
||||
{
|
||||
TU_VERIFY(TUSB_CLASS_VENDOR_SPECIFIC == desc_itf->bInterfaceClass, 0);
|
||||
|
||||
uint8_t const * p_desc = tu_desc_next(desc_itf);
|
||||
uint8_t const * desc_end = p_desc + max_len;
|
||||
|
||||
// Find available interface
|
||||
vendord_interface_t* p_vendor = NULL;
|
||||
for(uint8_t i=0; i<CFG_TUD_VENDOR; i++)
|
||||
{
|
||||
if ( _vendord_itf[i].ep_in == 0 && _vendord_itf[i].ep_out == 0 )
|
||||
{
|
||||
p_vendor = &_vendord_itf[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
TU_VERIFY(p_vendor, 0);
|
||||
|
||||
p_vendor->itf_num = desc_itf->bInterfaceNumber;
|
||||
if (desc_itf->bNumEndpoints)
|
||||
{
|
||||
// skip non-endpoint descriptors
|
||||
while ( (TUSB_DESC_ENDPOINT != tu_desc_type(p_desc)) && (p_desc < desc_end) )
|
||||
{
|
||||
p_desc = tu_desc_next(p_desc);
|
||||
}
|
||||
|
||||
// Open endpoint pair with usbd helper
|
||||
TU_ASSERT(usbd_open_edpt_pair(rhport, p_desc, desc_itf->bNumEndpoints, TUSB_XFER_BULK, &p_vendor->ep_out, &p_vendor->ep_in), 0);
|
||||
|
||||
p_desc += desc_itf->bNumEndpoints*sizeof(tusb_desc_endpoint_t);
|
||||
|
||||
// Prepare for incoming data
|
||||
if ( p_vendor->ep_out )
|
||||
{
|
||||
_prep_out_transaction(p_vendor);
|
||||
}
|
||||
|
||||
if ( p_vendor->ep_in ) tud_vendor_n_write_flush((uint8_t)(p_vendor - _vendord_itf));
|
||||
}
|
||||
|
||||
return (uint16_t) ((uintptr_t) p_desc - (uintptr_t) desc_itf);
|
||||
}
|
||||
|
||||
bool vendord_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes)
|
||||
{
|
||||
(void) rhport;
|
||||
(void) result;
|
||||
|
||||
uint8_t itf = 0;
|
||||
vendord_interface_t* p_itf = _vendord_itf;
|
||||
|
||||
for ( ; ; itf++, p_itf++)
|
||||
{
|
||||
if (itf >= TU_ARRAY_SIZE(_vendord_itf)) return false;
|
||||
|
||||
if ( ( ep_addr == p_itf->ep_out ) || ( ep_addr == p_itf->ep_in ) ) break;
|
||||
}
|
||||
|
||||
if ( ep_addr == p_itf->ep_out )
|
||||
{
|
||||
// Receive new data
|
||||
tu_fifo_write_n(&p_itf->rx_ff, p_itf->epout_buf, (uint16_t) xferred_bytes);
|
||||
|
||||
// Invoked callback if any
|
||||
if (tud_vendor_rx_cb) tud_vendor_rx_cb(itf);
|
||||
|
||||
_prep_out_transaction(p_itf);
|
||||
}
|
||||
else if ( ep_addr == p_itf->ep_in )
|
||||
{
|
||||
if (tud_vendor_tx_cb) tud_vendor_tx_cb(itf, (uint16_t) xferred_bytes);
|
||||
// Send complete, try to send more if possible
|
||||
tud_vendor_n_write_flush(itf);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#endif
|
|
@ -0,0 +1,150 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#ifndef _TUSB_VENDOR_DEVICE_H_
|
||||
#define _TUSB_VENDOR_DEVICE_H_
|
||||
|
||||
#include "common/tusb_common.h"
|
||||
|
||||
#ifndef CFG_TUD_VENDOR_EPSIZE
|
||||
#define CFG_TUD_VENDOR_EPSIZE 64
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Application API (Multiple Interfaces)
|
||||
//--------------------------------------------------------------------+
|
||||
bool tud_vendor_n_mounted (uint8_t itf);
|
||||
|
||||
uint32_t tud_vendor_n_available (uint8_t itf);
|
||||
uint32_t tud_vendor_n_read (uint8_t itf, void* buffer, uint32_t bufsize);
|
||||
bool tud_vendor_n_peek (uint8_t itf, uint8_t* ui8);
|
||||
void tud_vendor_n_read_flush (uint8_t itf);
|
||||
|
||||
uint32_t tud_vendor_n_write (uint8_t itf, void const* buffer, uint32_t bufsize);
|
||||
uint32_t tud_vendor_n_write_flush (uint8_t itf);
|
||||
uint32_t tud_vendor_n_write_available (uint8_t itf);
|
||||
|
||||
static inline uint32_t tud_vendor_n_write_str (uint8_t itf, char const* str);
|
||||
|
||||
// backward compatible
|
||||
#define tud_vendor_n_flush(itf) tud_vendor_n_write_flush(itf)
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Application API (Single Port)
|
||||
//--------------------------------------------------------------------+
|
||||
static inline bool tud_vendor_mounted (void);
|
||||
static inline uint32_t tud_vendor_available (void);
|
||||
static inline uint32_t tud_vendor_read (void* buffer, uint32_t bufsize);
|
||||
static inline bool tud_vendor_peek (uint8_t* ui8);
|
||||
static inline void tud_vendor_read_flush (void);
|
||||
static inline uint32_t tud_vendor_write (void const* buffer, uint32_t bufsize);
|
||||
static inline uint32_t tud_vendor_write_str (char const* str);
|
||||
static inline uint32_t tud_vendor_write_available (void);
|
||||
static inline uint32_t tud_vendor_write_flush (void);
|
||||
|
||||
// backward compatible
|
||||
#define tud_vendor_flush() tud_vendor_write_flush()
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Application Callback API (weak is optional)
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Invoked when received new data
|
||||
TU_ATTR_WEAK void tud_vendor_rx_cb(uint8_t itf);
|
||||
// Invoked when last rx transfer finished
|
||||
TU_ATTR_WEAK void tud_vendor_tx_cb(uint8_t itf, uint32_t sent_bytes);
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Inline Functions
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
static inline uint32_t tud_vendor_n_write_str (uint8_t itf, char const* str)
|
||||
{
|
||||
return tud_vendor_n_write(itf, str, strlen(str));
|
||||
}
|
||||
|
||||
static inline bool tud_vendor_mounted (void)
|
||||
{
|
||||
return tud_vendor_n_mounted(0);
|
||||
}
|
||||
|
||||
static inline uint32_t tud_vendor_available (void)
|
||||
{
|
||||
return tud_vendor_n_available(0);
|
||||
}
|
||||
|
||||
static inline uint32_t tud_vendor_read (void* buffer, uint32_t bufsize)
|
||||
{
|
||||
return tud_vendor_n_read(0, buffer, bufsize);
|
||||
}
|
||||
|
||||
static inline bool tud_vendor_peek (uint8_t* ui8)
|
||||
{
|
||||
return tud_vendor_n_peek(0, ui8);
|
||||
}
|
||||
|
||||
static inline void tud_vendor_read_flush(void)
|
||||
{
|
||||
tud_vendor_n_read_flush(0);
|
||||
}
|
||||
|
||||
static inline uint32_t tud_vendor_write (void const* buffer, uint32_t bufsize)
|
||||
{
|
||||
return tud_vendor_n_write(0, buffer, bufsize);
|
||||
}
|
||||
|
||||
static inline uint32_t tud_vendor_write_flush (void)
|
||||
{
|
||||
return tud_vendor_n_write_flush(0);
|
||||
}
|
||||
|
||||
static inline uint32_t tud_vendor_write_str (char const* str)
|
||||
{
|
||||
return tud_vendor_n_write_str(0, str);
|
||||
}
|
||||
|
||||
static inline uint32_t tud_vendor_write_available (void)
|
||||
{
|
||||
return tud_vendor_n_write_available(0);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Internal Class Driver API
|
||||
//--------------------------------------------------------------------+
|
||||
void vendord_init(void);
|
||||
void vendord_reset(uint8_t rhport);
|
||||
uint16_t vendord_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len);
|
||||
bool vendord_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _TUSB_VENDOR_DEVICE_H_ */
|
|
@ -0,0 +1,146 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#include "tusb_option.h"
|
||||
|
||||
#if (CFG_TUH_ENABLED && CFG_TUH_VENDOR)
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// INCLUDE
|
||||
//--------------------------------------------------------------------+
|
||||
#include "host/usbh.h"
|
||||
#include "vendor_host.h"
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// MACRO CONSTANT TYPEDEF
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// INTERNAL OBJECT & FUNCTION DECLARATION
|
||||
//--------------------------------------------------------------------+
|
||||
custom_interface_info_t custom_interface[CFG_TUH_DEVICE_MAX];
|
||||
|
||||
static tusb_error_t cush_validate_paras(uint8_t dev_addr, uint16_t vendor_id, uint16_t product_id, void * p_buffer, uint16_t length)
|
||||
{
|
||||
if ( !tusbh_custom_is_mounted(dev_addr, vendor_id, product_id) )
|
||||
{
|
||||
return TUSB_ERROR_DEVICE_NOT_READY;
|
||||
}
|
||||
|
||||
TU_ASSERT( p_buffer != NULL && length != 0, TUSB_ERROR_INVALID_PARA);
|
||||
|
||||
return TUSB_ERROR_NONE;
|
||||
}
|
||||
//--------------------------------------------------------------------+
|
||||
// APPLICATION API (need to check parameters)
|
||||
//--------------------------------------------------------------------+
|
||||
tusb_error_t tusbh_custom_read(uint8_t dev_addr, uint16_t vendor_id, uint16_t product_id, void * p_buffer, uint16_t length)
|
||||
{
|
||||
TU_ASSERT_ERR( cush_validate_paras(dev_addr, vendor_id, product_id, p_buffer, length) );
|
||||
|
||||
if ( !hcd_pipe_is_idle(custom_interface[dev_addr-1].pipe_in) )
|
||||
{
|
||||
return TUSB_ERROR_INTERFACE_IS_BUSY;
|
||||
}
|
||||
|
||||
(void) usbh_edpt_xfer( custom_interface[dev_addr-1].pipe_in, p_buffer, length);
|
||||
|
||||
return TUSB_ERROR_NONE;
|
||||
}
|
||||
|
||||
tusb_error_t tusbh_custom_write(uint8_t dev_addr, uint16_t vendor_id, uint16_t product_id, void const * p_data, uint16_t length)
|
||||
{
|
||||
TU_ASSERT_ERR( cush_validate_paras(dev_addr, vendor_id, product_id, p_data, length) );
|
||||
|
||||
if ( !hcd_pipe_is_idle(custom_interface[dev_addr-1].pipe_out) )
|
||||
{
|
||||
return TUSB_ERROR_INTERFACE_IS_BUSY;
|
||||
}
|
||||
|
||||
(void) usbh_edpt_xfer( custom_interface[dev_addr-1].pipe_out, p_data, length);
|
||||
|
||||
return TUSB_ERROR_NONE;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// USBH-CLASS API
|
||||
//--------------------------------------------------------------------+
|
||||
void cush_init(void)
|
||||
{
|
||||
tu_memclr(&custom_interface, sizeof(custom_interface_info_t) * CFG_TUH_DEVICE_MAX);
|
||||
}
|
||||
|
||||
tusb_error_t cush_open_subtask(uint8_t dev_addr, tusb_desc_interface_t const *p_interface_desc, uint16_t *p_length)
|
||||
{
|
||||
// FIXME quick hack to test lpc1k custom class with 2 bulk endpoints
|
||||
uint8_t const *p_desc = (uint8_t const *) p_interface_desc;
|
||||
p_desc = tu_desc_next(p_desc);
|
||||
|
||||
//------------- Bulk Endpoints Descriptor -------------//
|
||||
for(uint32_t i=0; i<2; i++)
|
||||
{
|
||||
tusb_desc_endpoint_t const *p_endpoint = (tusb_desc_endpoint_t const *) p_desc;
|
||||
TU_ASSERT(TUSB_DESC_ENDPOINT == p_endpoint->bDescriptorType, TUSB_ERROR_INVALID_PARA);
|
||||
|
||||
pipe_handle_t * p_pipe_hdl = ( p_endpoint->bEndpointAddress & TUSB_DIR_IN_MASK ) ?
|
||||
&custom_interface[dev_addr-1].pipe_in : &custom_interface[dev_addr-1].pipe_out;
|
||||
*p_pipe_hdl = usbh_edpt_open(dev_addr, p_endpoint, TUSB_CLASS_VENDOR_SPECIFIC);
|
||||
TU_ASSERT ( pipehandle_is_valid(*p_pipe_hdl), TUSB_ERROR_HCD_OPEN_PIPE_FAILED );
|
||||
|
||||
p_desc = tu_desc_next(p_desc);
|
||||
}
|
||||
|
||||
(*p_length) = sizeof(tusb_desc_interface_t) + 2*sizeof(tusb_desc_endpoint_t);
|
||||
return TUSB_ERROR_NONE;
|
||||
}
|
||||
|
||||
void cush_isr(pipe_handle_t pipe_hdl, xfer_result_t event)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void cush_close(uint8_t dev_addr)
|
||||
{
|
||||
tusb_error_t err1, err2;
|
||||
custom_interface_info_t * p_interface = &custom_interface[dev_addr-1];
|
||||
|
||||
// TODO re-consider to check pipe valid before calling pipe_close
|
||||
if( pipehandle_is_valid( p_interface->pipe_in ) )
|
||||
{
|
||||
err1 = hcd_pipe_close( p_interface->pipe_in );
|
||||
}
|
||||
|
||||
if ( pipehandle_is_valid( p_interface->pipe_out ) )
|
||||
{
|
||||
err2 = hcd_pipe_close( p_interface->pipe_out );
|
||||
}
|
||||
|
||||
tu_memclr(p_interface, sizeof(custom_interface_info_t));
|
||||
|
||||
TU_ASSERT(err1 == TUSB_ERROR_NONE && err2 == TUSB_ERROR_NONE, (void) 0 );
|
||||
}
|
||||
|
||||
#endif
|
|
@ -0,0 +1,67 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#ifndef _TUSB_VENDOR_HOST_H_
|
||||
#define _TUSB_VENDOR_HOST_H_
|
||||
|
||||
#include "common/tusb_common.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
pipe_handle_t pipe_in;
|
||||
pipe_handle_t pipe_out;
|
||||
}custom_interface_info_t;
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// USBH-CLASS DRIVER API
|
||||
//--------------------------------------------------------------------+
|
||||
static inline bool tusbh_custom_is_mounted(uint8_t dev_addr, uint16_t vendor_id, uint16_t product_id)
|
||||
{
|
||||
(void) vendor_id; // TODO check this later
|
||||
(void) product_id;
|
||||
// return (tusbh_device_get_mounted_class_flag(dev_addr) & TU_BIT(TUSB_CLASS_MAPPED_INDEX_END-1) ) != 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool tusbh_custom_read(uint8_t dev_addr, uint16_t vendor_id, uint16_t product_id, void * p_buffer, uint16_t length);
|
||||
bool tusbh_custom_write(uint8_t dev_addr, uint16_t vendor_id, uint16_t product_id, void const * p_data, uint16_t length);
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Internal Class Driver API
|
||||
//--------------------------------------------------------------------+
|
||||
void cush_init(void);
|
||||
bool cush_open_subtask(uint8_t dev_addr, tusb_desc_interface_t const *p_interface_desc, uint16_t *p_length);
|
||||
void cush_isr(pipe_handle_t pipe_hdl, xfer_result_t event);
|
||||
void cush_close(uint8_t dev_addr);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _TUSB_VENDOR_HOST_H_ */
|
|
@ -0,0 +1,314 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#ifndef _TUSB_COMMON_H_
|
||||
#define _TUSB_COMMON_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Macros Helper
|
||||
//--------------------------------------------------------------------+
|
||||
#define TU_ARRAY_SIZE(_arr) ( sizeof(_arr) / sizeof(_arr[0]) )
|
||||
#define TU_MIN(_x, _y) ( ( (_x) < (_y) ) ? (_x) : (_y) )
|
||||
#define TU_MAX(_x, _y) ( ( (_x) > (_y) ) ? (_x) : (_y) )
|
||||
|
||||
#define TU_U16(_high, _low) ((uint16_t) (((_high) << 8) | (_low)))
|
||||
#define TU_U16_HIGH(_u16) ((uint8_t) (((_u16) >> 8) & 0x00ff))
|
||||
#define TU_U16_LOW(_u16) ((uint8_t) ((_u16) & 0x00ff))
|
||||
#define U16_TO_U8S_BE(_u16) TU_U16_HIGH(_u16), TU_U16_LOW(_u16)
|
||||
#define U16_TO_U8S_LE(_u16) TU_U16_LOW(_u16), TU_U16_HIGH(_u16)
|
||||
|
||||
#define TU_U32_BYTE3(_u32) ((uint8_t) ((((uint32_t) _u32) >> 24) & 0x000000ff)) // MSB
|
||||
#define TU_U32_BYTE2(_u32) ((uint8_t) ((((uint32_t) _u32) >> 16) & 0x000000ff))
|
||||
#define TU_U32_BYTE1(_u32) ((uint8_t) ((((uint32_t) _u32) >> 8) & 0x000000ff))
|
||||
#define TU_U32_BYTE0(_u32) ((uint8_t) (((uint32_t) _u32) & 0x000000ff)) // LSB
|
||||
|
||||
#define U32_TO_U8S_BE(_u32) TU_U32_BYTE3(_u32), TU_U32_BYTE2(_u32), TU_U32_BYTE1(_u32), TU_U32_BYTE0(_u32)
|
||||
#define U32_TO_U8S_LE(_u32) TU_U32_BYTE0(_u32), TU_U32_BYTE1(_u32), TU_U32_BYTE2(_u32), TU_U32_BYTE3(_u32)
|
||||
|
||||
#define TU_BIT(n) (1UL << (n))
|
||||
|
||||
// Generate a mask with bit from high (31) to low (0) set, e.g TU_GENMASK(3, 0) = 0b1111
|
||||
#define TU_GENMASK(h, l) ( (UINT32_MAX << (l)) & (UINT32_MAX >> (31 - (h))) )
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Includes
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Standard Headers
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Tinyusb Common Headers
|
||||
#include "tusb_option.h"
|
||||
#include "tusb_compiler.h"
|
||||
#include "tusb_verify.h"
|
||||
#include "tusb_types.h"
|
||||
#include "tusb_debug.h"
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Optional API implemented by application if needed
|
||||
// TODO move to a more ovious place/file
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// flush data cache
|
||||
TU_ATTR_WEAK extern void tusb_app_dcache_flush(uintptr_t addr, uint32_t data_size);
|
||||
|
||||
// invalidate data cache
|
||||
TU_ATTR_WEAK extern void tusb_app_dcache_invalidate(uintptr_t addr, uint32_t data_size);
|
||||
|
||||
// Optional physical <-> virtual address translation
|
||||
TU_ATTR_WEAK extern void* tusb_app_virt_to_phys(void *virt_addr);
|
||||
TU_ATTR_WEAK extern void* tusb_app_phys_to_virt(void *phys_addr);
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Internal Inline Functions
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
//------------- Mem -------------//
|
||||
#define tu_memclr(buffer, size) memset((buffer), 0, (size))
|
||||
#define tu_varclr(_var) tu_memclr(_var, sizeof(*(_var)))
|
||||
|
||||
// This is a backport of memset_s from c11
|
||||
TU_ATTR_ALWAYS_INLINE static inline int tu_memset_s(void *dest, size_t destsz, int ch, size_t count) {
|
||||
// TODO may check if desst and src is not NULL
|
||||
if ( count > destsz ) {
|
||||
return -1;
|
||||
}
|
||||
memset(dest, ch, count);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// This is a backport of memcpy_s from c11
|
||||
TU_ATTR_ALWAYS_INLINE static inline int tu_memcpy_s(void *dest, size_t destsz, const void *src, size_t count) {
|
||||
// TODO may check if desst and src is not NULL
|
||||
if ( count > destsz ) {
|
||||
return -1;
|
||||
}
|
||||
memcpy(dest, src, count);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
//------------- Bytes -------------//
|
||||
TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_u32(uint8_t b3, uint8_t b2, uint8_t b1, uint8_t b0) {
|
||||
return ( ((uint32_t) b3) << 24) | ( ((uint32_t) b2) << 16) | ( ((uint32_t) b1) << 8) | b0;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline uint16_t tu_u16(uint8_t high, uint8_t low) {
|
||||
return (uint16_t) ((((uint16_t) high) << 8) | low);
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_u32_byte3(uint32_t ui32) { return TU_U32_BYTE3(ui32); }
|
||||
TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_u32_byte2(uint32_t ui32) { return TU_U32_BYTE2(ui32); }
|
||||
TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_u32_byte1(uint32_t ui32) { return TU_U32_BYTE1(ui32); }
|
||||
TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_u32_byte0(uint32_t ui32) { return TU_U32_BYTE0(ui32); }
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline uint16_t tu_u32_high16(uint32_t ui32) { return (uint16_t) (ui32 >> 16); }
|
||||
TU_ATTR_ALWAYS_INLINE static inline uint16_t tu_u32_low16 (uint32_t ui32) { return (uint16_t) (ui32 & 0x0000ffffu); }
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_u16_high(uint16_t ui16) { return TU_U16_HIGH(ui16); }
|
||||
TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_u16_low (uint16_t ui16) { return TU_U16_LOW(ui16); }
|
||||
|
||||
//------------- Bits -------------//
|
||||
TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_bit_set (uint32_t value, uint8_t pos) { return value | TU_BIT(pos); }
|
||||
TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_bit_clear(uint32_t value, uint8_t pos) { return value & (~TU_BIT(pos)); }
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool tu_bit_test (uint32_t value, uint8_t pos) { return (value & TU_BIT(pos)) ? true : false; }
|
||||
|
||||
//------------- Min -------------//
|
||||
TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_min8 (uint8_t x, uint8_t y ) { return (x < y) ? x : y; }
|
||||
TU_ATTR_ALWAYS_INLINE static inline uint16_t tu_min16 (uint16_t x, uint16_t y) { return (x < y) ? x : y; }
|
||||
TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_min32 (uint32_t x, uint32_t y) { return (x < y) ? x : y; }
|
||||
|
||||
//------------- Max -------------//
|
||||
TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_max8 (uint8_t x, uint8_t y ) { return (x > y) ? x : y; }
|
||||
TU_ATTR_ALWAYS_INLINE static inline uint16_t tu_max16 (uint16_t x, uint16_t y) { return (x > y) ? x : y; }
|
||||
TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_max32 (uint32_t x, uint32_t y) { return (x > y) ? x : y; }
|
||||
|
||||
//------------- Align -------------//
|
||||
TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_align(uint32_t value, uint32_t alignment) {
|
||||
return value & ((uint32_t) ~(alignment-1));
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_align4 (uint32_t value) { return (value & 0xFFFFFFFCUL); }
|
||||
TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_align8 (uint32_t value) { return (value & 0xFFFFFFF8UL); }
|
||||
TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_align16 (uint32_t value) { return (value & 0xFFFFFFF0UL); }
|
||||
TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_align32 (uint32_t value) { return (value & 0xFFFFFFE0UL); }
|
||||
TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_align4k (uint32_t value) { return (value & 0xFFFFF000UL); }
|
||||
TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_offset4k(uint32_t value) { return (value & 0xFFFUL); }
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool tu_is_aligned32(uint32_t value) { return (value & 0x1FUL) == 0; }
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool tu_is_aligned64(uint64_t value) { return (value & 0x3FUL) == 0; }
|
||||
|
||||
//------------- Mathematics -------------//
|
||||
TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_div_ceil(uint32_t v, uint32_t d) { return (v + d -1)/d; }
|
||||
|
||||
// log2 of a value is its MSB's position
|
||||
// TODO use clz TODO remove
|
||||
static inline uint8_t tu_log2(uint32_t value)
|
||||
{
|
||||
uint8_t result = 0;
|
||||
while (value >>= 1) { result++; }
|
||||
return result;
|
||||
}
|
||||
|
||||
//static inline uint8_t tu_log2(uint32_t value)
|
||||
//{
|
||||
// return sizeof(uint32_t) * CHAR_BIT - __builtin_clz(x) - 1;
|
||||
//}
|
||||
|
||||
static inline bool tu_is_power_of_two(uint32_t value)
|
||||
{
|
||||
return (value != 0) && ((value & (value - 1)) == 0);
|
||||
}
|
||||
|
||||
//------------- Unaligned Access -------------//
|
||||
#if TUP_ARCH_STRICT_ALIGN
|
||||
|
||||
// Rely on compiler to generate correct code for unaligned access
|
||||
typedef struct { uint16_t val; } TU_ATTR_PACKED tu_unaligned_uint16_t;
|
||||
typedef struct { uint32_t val; } TU_ATTR_PACKED tu_unaligned_uint32_t;
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_unaligned_read32(const void* mem)
|
||||
{
|
||||
tu_unaligned_uint32_t const* ua32 = (tu_unaligned_uint32_t const*) mem;
|
||||
return ua32->val;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline void tu_unaligned_write32(void* mem, uint32_t value)
|
||||
{
|
||||
tu_unaligned_uint32_t* ua32 = (tu_unaligned_uint32_t*) mem;
|
||||
ua32->val = value;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline uint16_t tu_unaligned_read16(const void* mem)
|
||||
{
|
||||
tu_unaligned_uint16_t const* ua16 = (tu_unaligned_uint16_t const*) mem;
|
||||
return ua16->val;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline void tu_unaligned_write16(void* mem, uint16_t value)
|
||||
{
|
||||
tu_unaligned_uint16_t* ua16 = (tu_unaligned_uint16_t*) mem;
|
||||
ua16->val = value;
|
||||
}
|
||||
|
||||
#elif TUP_MCU_STRICT_ALIGN
|
||||
|
||||
// MCU such as LPC_IP3511 Highspeed cannot access unaligned memory on USB_RAM although it is ARM M4.
|
||||
// We have to manually pick up bytes since tu_unaligned_uint32_t will still generate unaligned code
|
||||
// NOTE: volatile cast to memory to prevent compiler to optimize and generate unaligned code
|
||||
// TODO Big Endian may need minor changes
|
||||
TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_unaligned_read32(const void* mem)
|
||||
{
|
||||
volatile uint8_t const* buf8 = (uint8_t const*) mem;
|
||||
return tu_u32(buf8[3], buf8[2], buf8[1], buf8[0]);
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline void tu_unaligned_write32(void* mem, uint32_t value)
|
||||
{
|
||||
volatile uint8_t* buf8 = (uint8_t*) mem;
|
||||
buf8[0] = tu_u32_byte0(value);
|
||||
buf8[1] = tu_u32_byte1(value);
|
||||
buf8[2] = tu_u32_byte2(value);
|
||||
buf8[3] = tu_u32_byte3(value);
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline uint16_t tu_unaligned_read16(const void* mem)
|
||||
{
|
||||
volatile uint8_t const* buf8 = (uint8_t const*) mem;
|
||||
return tu_u16(buf8[1], buf8[0]);
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline void tu_unaligned_write16(void* mem, uint16_t value)
|
||||
{
|
||||
volatile uint8_t* buf8 = (uint8_t*) mem;
|
||||
buf8[0] = tu_u16_low(value);
|
||||
buf8[1] = tu_u16_high(value);
|
||||
}
|
||||
|
||||
|
||||
#else
|
||||
|
||||
// MCU that could access unaligned memory natively
|
||||
TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_unaligned_read32(const void *mem) {
|
||||
return *((uint32_t const *) mem);
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline uint16_t tu_unaligned_read16(const void *mem) {
|
||||
return *((uint16_t const *) mem);
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline void tu_unaligned_write32(void *mem, uint32_t value) {
|
||||
*((uint32_t *) mem) = value;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline void tu_unaligned_write16(void *mem, uint16_t value) {
|
||||
*((uint16_t *) mem) = value;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
// To be removed
|
||||
//------------- Binary constant -------------//
|
||||
#if defined(__GNUC__) && !defined(__CC_ARM)
|
||||
|
||||
#define TU_BIN8(x) ((uint8_t) (0b##x))
|
||||
#define TU_BIN16(b1, b2) ((uint16_t) (0b##b1##b2))
|
||||
#define TU_BIN32(b1, b2, b3, b4) ((uint32_t) (0b##b1##b2##b3##b4))
|
||||
|
||||
#else
|
||||
|
||||
// internal macro of B8, B16, B32
|
||||
#define _B8__(x) (((x&0x0000000FUL)?1:0) \
|
||||
+((x&0x000000F0UL)?2:0) \
|
||||
+((x&0x00000F00UL)?4:0) \
|
||||
+((x&0x0000F000UL)?8:0) \
|
||||
+((x&0x000F0000UL)?16:0) \
|
||||
+((x&0x00F00000UL)?32:0) \
|
||||
+((x&0x0F000000UL)?64:0) \
|
||||
+((x&0xF0000000UL)?128:0))
|
||||
|
||||
#define TU_BIN8(d) ((uint8_t) _B8__(0x##d##UL))
|
||||
#define TU_BIN16(dmsb,dlsb) (((uint16_t)TU_BIN8(dmsb)<<8) + TU_BIN8(dlsb))
|
||||
#define TU_BIN32(dmsb,db2,db3,dlsb) \
|
||||
(((uint32_t)TU_BIN8(dmsb)<<24) \
|
||||
+ ((uint32_t)TU_BIN8(db2)<<16) \
|
||||
+ ((uint32_t)TU_BIN8(db3)<<8) \
|
||||
+ TU_BIN8(dlsb))
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _TUSB_COMMON_H_ */
|
|
@ -0,0 +1,298 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
/** \ingroup Group_Common
|
||||
* \defgroup Group_Compiler Compiler
|
||||
* \brief Group_Compiler brief
|
||||
* @{ */
|
||||
|
||||
#ifndef _TUSB_COMPILER_H_
|
||||
#define _TUSB_COMPILER_H_
|
||||
|
||||
#define TU_TOKEN(x) x
|
||||
#define TU_STRING(x) #x ///< stringify without expand
|
||||
#define TU_XSTRING(x) TU_STRING(x) ///< expand then stringify
|
||||
|
||||
#define TU_STRCAT(a, b) a##b ///< concat without expand
|
||||
#define TU_STRCAT3(a, b, c) a##b##c ///< concat without expand
|
||||
|
||||
#define TU_XSTRCAT(a, b) TU_STRCAT(a, b) ///< expand then concat
|
||||
#define TU_XSTRCAT3(a, b, c) TU_STRCAT3(a, b, c) ///< expand then concat 3 tokens
|
||||
|
||||
#define TU_INCLUDE_PATH(_dir,_file) TU_XSTRING( TU_TOKEN(_dir)TU_TOKEN(_file) )
|
||||
|
||||
#if defined __COUNTER__ && __COUNTER__ != __COUNTER__
|
||||
#define _TU_COUNTER_ __COUNTER__
|
||||
#else
|
||||
#define _TU_COUNTER_ __LINE__
|
||||
#endif
|
||||
|
||||
// Compile-time Assert
|
||||
#if defined (__cplusplus) && __cplusplus >= 201103L
|
||||
#define TU_VERIFY_STATIC static_assert
|
||||
#elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
|
||||
#define TU_VERIFY_STATIC _Static_assert
|
||||
#elif defined(__CCRX__)
|
||||
#define TU_VERIFY_STATIC(const_expr, _mess) typedef char TU_XSTRCAT(Line, __LINE__)[(const_expr) ? 1 : 0];
|
||||
#else
|
||||
#define TU_VERIFY_STATIC(const_expr, _mess) enum { TU_XSTRCAT(_verify_static_, _TU_COUNTER_) = 1/(!!(const_expr)) }
|
||||
#endif
|
||||
|
||||
/* --------------------- Fuzzing types -------------------------------------- */
|
||||
#ifdef _FUZZ
|
||||
#define tu_static static __thread
|
||||
#else
|
||||
#define tu_static static
|
||||
#endif
|
||||
|
||||
// for declaration of reserved field, make use of _TU_COUNTER_
|
||||
#define TU_RESERVED TU_XSTRCAT(reserved, _TU_COUNTER_)
|
||||
|
||||
#define TU_LITTLE_ENDIAN (0x12u)
|
||||
#define TU_BIG_ENDIAN (0x21u)
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* Count number of arguments of __VA_ARGS__
|
||||
* - reference https://stackoverflow.com/questions/2124339/c-preprocessor-va-args-number-of-arguments
|
||||
* - _GET_NTH_ARG() takes args >= N (64) but only expand to Nth one (64th)
|
||||
* - _RSEQ_N() is reverse sequential to N to add padding to have
|
||||
* Nth position is the same as the number of arguments
|
||||
* - ##__VA_ARGS__ is used to deal with 0 paramerter (swallows comma)
|
||||
*------------------------------------------------------------------*/
|
||||
#if !defined(__CCRX__)
|
||||
#define TU_ARGS_NUM(...) _TU_NARG(_0, ##__VA_ARGS__, _RSEQ_N())
|
||||
#else
|
||||
#define TU_ARGS_NUM(...) _TU_NARG(_0, __VA_ARGS__, _RSEQ_N())
|
||||
#endif
|
||||
|
||||
#define _TU_NARG(...) _GET_NTH_ARG(__VA_ARGS__)
|
||||
#define _GET_NTH_ARG( \
|
||||
_1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \
|
||||
_11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
|
||||
_21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
|
||||
_31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
|
||||
_41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
|
||||
_51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
|
||||
_61,_62,_63,N,...) N
|
||||
#define _RSEQ_N() \
|
||||
62,61,60, \
|
||||
59,58,57,56,55,54,53,52,51,50, \
|
||||
49,48,47,46,45,44,43,42,41,40, \
|
||||
39,38,37,36,35,34,33,32,31,30, \
|
||||
29,28,27,26,25,24,23,22,21,20, \
|
||||
19,18,17,16,15,14,13,12,11,10, \
|
||||
9,8,7,6,5,4,3,2,1,0
|
||||
|
||||
// Apply an macro X to each of the arguments with an separated of choice
|
||||
#define TU_ARGS_APPLY(_X, _s, ...) TU_XSTRCAT(_TU_ARGS_APPLY_, TU_ARGS_NUM(__VA_ARGS__))(_X, _s, __VA_ARGS__)
|
||||
|
||||
#define _TU_ARGS_APPLY_1(_X, _s, _a1) _X(_a1)
|
||||
#define _TU_ARGS_APPLY_2(_X, _s, _a1, _a2) _X(_a1) _s _X(_a2)
|
||||
#define _TU_ARGS_APPLY_3(_X, _s, _a1, _a2, _a3) _X(_a1) _s _TU_ARGS_APPLY_2(_X, _s, _a2, _a3)
|
||||
#define _TU_ARGS_APPLY_4(_X, _s, _a1, _a2, _a3, _a4) _X(_a1) _s _TU_ARGS_APPLY_3(_X, _s, _a2, _a3, _a4)
|
||||
#define _TU_ARGS_APPLY_5(_X, _s, _a1, _a2, _a3, _a4, _a5) _X(_a1) _s _TU_ARGS_APPLY_4(_X, _s, _a2, _a3, _a4, _a5)
|
||||
#define _TU_ARGS_APPLY_6(_X, _s, _a1, _a2, _a3, _a4, _a5, _a6) _X(_a1) _s _TU_ARGS_APPLY_5(_X, _s, _a2, _a3, _a4, _a5, _a6)
|
||||
#define _TU_ARGS_APPLY_7(_X, _s, _a1, _a2, _a3, _a4, _a5, _a6, _a7) _X(_a1) _s _TU_ARGS_APPLY_6(_X, _s, _a2, _a3, _a4, _a5, _a6, _a7)
|
||||
#define _TU_ARGS_APPLY_8(_X, _s, _a1, _a2, _a3, _a4, _a5, _a6, _a7, _a8) _X(_a1) _s _TU_ARGS_APPLY_7(_X, _s, _a2, _a3, _a4, _a5, _a6, _a7, _a8)
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Compiler porting with Attribute and Endian
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// TODO refactor since __attribute__ is supported across many compiler
|
||||
#if defined(__GNUC__)
|
||||
#define TU_ATTR_ALIGNED(Bytes) __attribute__ ((aligned(Bytes)))
|
||||
#define TU_ATTR_SECTION(sec_name) __attribute__ ((section(#sec_name)))
|
||||
#define TU_ATTR_PACKED __attribute__ ((packed))
|
||||
#define TU_ATTR_WEAK __attribute__ ((weak))
|
||||
#ifndef TU_ATTR_ALWAYS_INLINE // allow to override for debug
|
||||
#define TU_ATTR_ALWAYS_INLINE __attribute__ ((always_inline))
|
||||
#endif
|
||||
#define TU_ATTR_DEPRECATED(mess) __attribute__ ((deprecated(mess))) // warn if function with this attribute is used
|
||||
#define TU_ATTR_UNUSED __attribute__ ((unused)) // Function/Variable is meant to be possibly unused
|
||||
#define TU_ATTR_USED __attribute__ ((used)) // Function/Variable is meant to be used
|
||||
|
||||
#define TU_ATTR_PACKED_BEGIN
|
||||
#define TU_ATTR_PACKED_END
|
||||
#define TU_ATTR_BIT_FIELD_ORDER_BEGIN
|
||||
#define TU_ATTR_BIT_FIELD_ORDER_END
|
||||
|
||||
#if __GNUC__ < 5
|
||||
#define TU_ATTR_FALLTHROUGH do {} while (0) /* fallthrough */
|
||||
#else
|
||||
#if __has_attribute(__fallthrough__)
|
||||
#define TU_ATTR_FALLTHROUGH __attribute__((fallthrough))
|
||||
#else
|
||||
#define TU_ATTR_FALLTHROUGH do {} while (0) /* fallthrough */
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// Endian conversion use well-known host to network (big endian) naming
|
||||
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
||||
#define TU_BYTE_ORDER TU_LITTLE_ENDIAN
|
||||
#else
|
||||
#define TU_BYTE_ORDER TU_BIG_ENDIAN
|
||||
#endif
|
||||
|
||||
// Unfortunately XC16 doesn't provide builtins for 32bit endian conversion
|
||||
#if defined(__XC16)
|
||||
#define TU_BSWAP16(u16) (__builtin_swap(u16))
|
||||
#define TU_BSWAP32(u32) ((((u32) & 0xff000000) >> 24) | \
|
||||
(((u32) & 0x00ff0000) >> 8) | \
|
||||
(((u32) & 0x0000ff00) << 8) | \
|
||||
(((u32) & 0x000000ff) << 24))
|
||||
#else
|
||||
#define TU_BSWAP16(u16) (__builtin_bswap16(u16))
|
||||
#define TU_BSWAP32(u32) (__builtin_bswap32(u32))
|
||||
#endif
|
||||
|
||||
#ifndef __ARMCC_VERSION
|
||||
// List of obsolete callback function that is renamed and should not be defined.
|
||||
// Put it here since only gcc support this pragma
|
||||
#pragma GCC poison tud_vendor_control_request_cb
|
||||
#endif
|
||||
|
||||
#elif defined(__TI_COMPILER_VERSION__)
|
||||
#define TU_ATTR_ALIGNED(Bytes) __attribute__ ((aligned(Bytes)))
|
||||
#define TU_ATTR_SECTION(sec_name) __attribute__ ((section(#sec_name)))
|
||||
#define TU_ATTR_PACKED __attribute__ ((packed))
|
||||
#define TU_ATTR_WEAK __attribute__ ((weak))
|
||||
#define TU_ATTR_ALWAYS_INLINE __attribute__ ((always_inline))
|
||||
#define TU_ATTR_DEPRECATED(mess) __attribute__ ((deprecated(mess))) // warn if function with this attribute is used
|
||||
#define TU_ATTR_UNUSED __attribute__ ((unused)) // Function/Variable is meant to be possibly unused
|
||||
#define TU_ATTR_USED __attribute__ ((used))
|
||||
#define TU_ATTR_FALLTHROUGH __attribute__((fallthrough))
|
||||
|
||||
#define TU_ATTR_PACKED_BEGIN
|
||||
#define TU_ATTR_PACKED_END
|
||||
#define TU_ATTR_BIT_FIELD_ORDER_BEGIN
|
||||
#define TU_ATTR_BIT_FIELD_ORDER_END
|
||||
|
||||
// __BYTE_ORDER is defined in the TI ARM compiler, but not MSP430 (which is little endian)
|
||||
#if ((__BYTE_ORDER__) == (__ORDER_LITTLE_ENDIAN__)) || defined(__MSP430__)
|
||||
#define TU_BYTE_ORDER TU_LITTLE_ENDIAN
|
||||
#else
|
||||
#define TU_BYTE_ORDER TU_BIG_ENDIAN
|
||||
#endif
|
||||
|
||||
#define TU_BSWAP16(u16) (__builtin_bswap16(u16))
|
||||
#define TU_BSWAP32(u32) (__builtin_bswap32(u32))
|
||||
|
||||
#elif defined(__ICCARM__)
|
||||
#include <intrinsics.h>
|
||||
#define TU_ATTR_ALIGNED(Bytes) __attribute__ ((aligned(Bytes)))
|
||||
#define TU_ATTR_SECTION(sec_name) __attribute__ ((section(#sec_name)))
|
||||
#define TU_ATTR_PACKED __attribute__ ((packed))
|
||||
#define TU_ATTR_WEAK __attribute__ ((weak))
|
||||
#ifndef TU_ATTR_ALWAYS_INLINE // allow to override for debug
|
||||
#define TU_ATTR_ALWAYS_INLINE __attribute__ ((always_inline))
|
||||
#endif
|
||||
#define TU_ATTR_DEPRECATED(mess) __attribute__ ((deprecated(mess))) // warn if function with this attribute is used
|
||||
#define TU_ATTR_UNUSED __attribute__ ((unused)) // Function/Variable is meant to be possibly unused
|
||||
#define TU_ATTR_USED __attribute__ ((used)) // Function/Variable is meant to be used
|
||||
#define TU_ATTR_FALLTHROUGH do {} while (0) /* fallthrough */
|
||||
|
||||
#define TU_ATTR_PACKED_BEGIN
|
||||
#define TU_ATTR_PACKED_END
|
||||
#define TU_ATTR_BIT_FIELD_ORDER_BEGIN
|
||||
#define TU_ATTR_BIT_FIELD_ORDER_END
|
||||
|
||||
// Endian conversion use well-known host to network (big endian) naming
|
||||
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
||||
#define TU_BYTE_ORDER TU_LITTLE_ENDIAN
|
||||
#else
|
||||
#define TU_BYTE_ORDER TU_BIG_ENDIAN
|
||||
#endif
|
||||
|
||||
#define TU_BSWAP16(u16) (__iar_builtin_REV16(u16))
|
||||
#define TU_BSWAP32(u32) (__iar_builtin_REV(u32))
|
||||
|
||||
#elif defined(__CCRX__)
|
||||
#define TU_ATTR_ALIGNED(Bytes)
|
||||
#define TU_ATTR_SECTION(sec_name)
|
||||
#define TU_ATTR_PACKED
|
||||
#define TU_ATTR_WEAK
|
||||
#define TU_ATTR_ALWAYS_INLINE
|
||||
#define TU_ATTR_DEPRECATED(mess)
|
||||
#define TU_ATTR_UNUSED
|
||||
#define TU_ATTR_USED
|
||||
#define TU_ATTR_FALLTHROUGH do {} while (0) /* fallthrough */
|
||||
|
||||
#define TU_ATTR_PACKED_BEGIN _Pragma("pack")
|
||||
#define TU_ATTR_PACKED_END _Pragma("packoption")
|
||||
#define TU_ATTR_BIT_FIELD_ORDER_BEGIN _Pragma("bit_order right")
|
||||
#define TU_ATTR_BIT_FIELD_ORDER_END _Pragma("bit_order")
|
||||
|
||||
// Endian conversion use well-known host to network (big endian) naming
|
||||
#if defined(__LIT)
|
||||
#define TU_BYTE_ORDER TU_LITTLE_ENDIAN
|
||||
#else
|
||||
#define TU_BYTE_ORDER TU_BIG_ENDIAN
|
||||
#endif
|
||||
|
||||
#define TU_BSWAP16(u16) ((unsigned short)_builtin_revw((unsigned long)u16))
|
||||
#define TU_BSWAP32(u32) (_builtin_revl(u32))
|
||||
|
||||
#else
|
||||
#error "Compiler attribute porting is required"
|
||||
#endif
|
||||
|
||||
|
||||
#if (TU_BYTE_ORDER == TU_LITTLE_ENDIAN)
|
||||
|
||||
#define tu_htons(u16) (TU_BSWAP16(u16))
|
||||
#define tu_ntohs(u16) (TU_BSWAP16(u16))
|
||||
|
||||
#define tu_htonl(u32) (TU_BSWAP32(u32))
|
||||
#define tu_ntohl(u32) (TU_BSWAP32(u32))
|
||||
|
||||
#define tu_htole16(u16) (u16)
|
||||
#define tu_le16toh(u16) (u16)
|
||||
|
||||
#define tu_htole32(u32) (u32)
|
||||
#define tu_le32toh(u32) (u32)
|
||||
|
||||
#elif (TU_BYTE_ORDER == TU_BIG_ENDIAN)
|
||||
|
||||
#define tu_htons(u16) (u16)
|
||||
#define tu_ntohs(u16) (u16)
|
||||
|
||||
#define tu_htonl(u32) (u32)
|
||||
#define tu_ntohl(u32) (u32)
|
||||
|
||||
#define tu_htole16(u16) (TU_BSWAP16(u16))
|
||||
#define tu_le16toh(u16) (TU_BSWAP16(u16))
|
||||
|
||||
#define tu_htole32(u32) (TU_BSWAP32(u32))
|
||||
#define tu_le32toh(u32) (TU_BSWAP32(u32))
|
||||
|
||||
#else
|
||||
#error Byte order is undefined
|
||||
#endif
|
||||
|
||||
#endif /* _TUSB_COMPILER_H_ */
|
||||
|
||||
/// @}
|
|
@ -0,0 +1,170 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2022, Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#ifndef _TUSB_DEBUG_H_
|
||||
#define _TUSB_DEBUG_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Debug
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// CFG_TUSB_DEBUG for debugging
|
||||
// 0 : no debug
|
||||
// 1 : print error
|
||||
// 2 : print warning
|
||||
// 3 : print info
|
||||
#if CFG_TUSB_DEBUG
|
||||
|
||||
// Enum to String for debugging purposes
|
||||
#if CFG_TUSB_DEBUG >= CFG_TUH_LOG_LEVEL || CFG_TUSB_DEBUG >= CFG_TUD_LOG_LEVEL
|
||||
extern char const* const tu_str_speed[];
|
||||
extern char const* const tu_str_std_request[];
|
||||
extern char const* const tu_str_xfer_result[];
|
||||
#endif
|
||||
|
||||
void tu_print_mem(void const *buf, uint32_t count, uint8_t indent);
|
||||
|
||||
#ifdef CFG_TUSB_DEBUG_PRINTF
|
||||
extern int CFG_TUSB_DEBUG_PRINTF(const char *format, ...);
|
||||
#define tu_printf CFG_TUSB_DEBUG_PRINTF
|
||||
#else
|
||||
#define tu_printf printf
|
||||
#endif
|
||||
|
||||
static inline void tu_print_buf(uint8_t const* buf, uint32_t bufsize) {
|
||||
for(uint32_t i=0; i<bufsize; i++) tu_printf("%02X ", buf[i]);
|
||||
}
|
||||
|
||||
// Log with Level
|
||||
#define TU_LOG(n, ...) TU_XSTRCAT(TU_LOG, n)(__VA_ARGS__)
|
||||
#define TU_LOG_MEM(n, ...) TU_XSTRCAT3(TU_LOG, n, _MEM)(__VA_ARGS__)
|
||||
#define TU_LOG_BUF(n, ...) TU_XSTRCAT3(TU_LOG, n, _BUF)(__VA_ARGS__)
|
||||
#define TU_LOG_INT(n, ...) TU_XSTRCAT3(TU_LOG, n, _INT)(__VA_ARGS__)
|
||||
#define TU_LOG_HEX(n, ...) TU_XSTRCAT3(TU_LOG, n, _HEX)(__VA_ARGS__)
|
||||
#define TU_LOG_LOCATION() tu_printf("%s: %d:\r\n", __PRETTY_FUNCTION__, __LINE__)
|
||||
#define TU_LOG_FAILED() tu_printf("%s: %d: Failed\r\n", __PRETTY_FUNCTION__, __LINE__)
|
||||
|
||||
// Log Level 1: Error
|
||||
#define TU_LOG1 tu_printf
|
||||
#define TU_LOG1_MEM tu_print_mem
|
||||
#define TU_LOG1_BUF(_x, _n) tu_print_buf((uint8_t const*)(_x), _n)
|
||||
#define TU_LOG1_INT(_x) tu_printf(#_x " = %ld\r\n", (unsigned long) (_x) )
|
||||
#define TU_LOG1_HEX(_x) tu_printf(#_x " = %lX\r\n", (unsigned long) (_x) )
|
||||
|
||||
// Log Level 2: Warn
|
||||
#if CFG_TUSB_DEBUG >= 2
|
||||
#define TU_LOG2 TU_LOG1
|
||||
#define TU_LOG2_MEM TU_LOG1_MEM
|
||||
#define TU_LOG2_BUF TU_LOG1_BUF
|
||||
#define TU_LOG2_INT TU_LOG1_INT
|
||||
#define TU_LOG2_HEX TU_LOG1_HEX
|
||||
#endif
|
||||
|
||||
// Log Level 3: Info
|
||||
#if CFG_TUSB_DEBUG >= 3
|
||||
#define TU_LOG3 TU_LOG1
|
||||
#define TU_LOG3_MEM TU_LOG1_MEM
|
||||
#define TU_LOG3_BUF TU_LOG1_BUF
|
||||
#define TU_LOG3_INT TU_LOG1_INT
|
||||
#define TU_LOG3_HEX TU_LOG1_HEX
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
uint32_t key;
|
||||
const char* data;
|
||||
} tu_lookup_entry_t;
|
||||
|
||||
typedef struct {
|
||||
uint16_t count;
|
||||
tu_lookup_entry_t const* items;
|
||||
} tu_lookup_table_t;
|
||||
|
||||
static inline const char* tu_lookup_find(tu_lookup_table_t const* p_table, uint32_t key) {
|
||||
tu_static char not_found[11];
|
||||
|
||||
for(uint16_t i=0; i<p_table->count; i++) {
|
||||
if (p_table->items[i].key == key) return p_table->items[i].data;
|
||||
}
|
||||
|
||||
// not found return the key value in hex
|
||||
snprintf(not_found, sizeof(not_found), "0x%08lX", (unsigned long) key);
|
||||
|
||||
return not_found;
|
||||
}
|
||||
|
||||
#endif // CFG_TUSB_DEBUG
|
||||
|
||||
#ifndef TU_LOG
|
||||
#define TU_LOG(n, ...)
|
||||
#define TU_LOG_MEM(n, ...)
|
||||
#define TU_LOG_BUF(n, ...)
|
||||
#define TU_LOG_INT(n, ...)
|
||||
#define TU_LOG_HEX(n, ...)
|
||||
#define TU_LOG_LOCATION()
|
||||
#define TU_LOG_FAILED()
|
||||
#endif
|
||||
|
||||
// TODO replace all TU_LOGn with TU_LOG(n)
|
||||
|
||||
#define TU_LOG0(...)
|
||||
#define TU_LOG0_MEM(...)
|
||||
#define TU_LOG0_BUF(...)
|
||||
#define TU_LOG0_INT(...)
|
||||
#define TU_LOG0_HEX(...)
|
||||
|
||||
#ifndef TU_LOG1
|
||||
#define TU_LOG1(...)
|
||||
#define TU_LOG1_MEM(...)
|
||||
#define TU_LOG1_BUF(...)
|
||||
#define TU_LOG1_INT(...)
|
||||
#define TU_LOG1_HEX(...)
|
||||
#endif
|
||||
|
||||
#ifndef TU_LOG2
|
||||
#define TU_LOG2(...)
|
||||
#define TU_LOG2_MEM(...)
|
||||
#define TU_LOG2_BUF(...)
|
||||
#define TU_LOG2_INT(...)
|
||||
#define TU_LOG2_HEX(...)
|
||||
#endif
|
||||
|
||||
#ifndef TU_LOG3
|
||||
#define TU_LOG3(...)
|
||||
#define TU_LOG3_MEM(...)
|
||||
#define TU_LOG3_BUF(...)
|
||||
#define TU_LOG3_INT(...)
|
||||
#define TU_LOG3_HEX(...)
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _TUSB_DEBUG_H_ */
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,206 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
||||
* Copyright (c) 2020 Reinhard Panhuber - rework to unmasked pointers
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#ifndef _TUSB_FIFO_H_
|
||||
#define _TUSB_FIFO_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// Due to the use of unmasked pointers, this FIFO does not suffer from losing
|
||||
// one item slice. Furthermore, write and read operations are completely
|
||||
// decoupled as write and read functions do not modify a common state. Henceforth,
|
||||
// writing or reading from the FIFO within an ISR is safe as long as no other
|
||||
// process (thread or ISR) interferes.
|
||||
// Also, this FIFO is ready to be used in combination with a DMA as the write and
|
||||
// read pointers can be updated from within a DMA ISR. Overflows are detectable
|
||||
// within a certain number (see tu_fifo_overflow()).
|
||||
|
||||
#include "common/tusb_common.h"
|
||||
#include "osal/osal.h"
|
||||
|
||||
// mutex is only needed for RTOS
|
||||
// for OS None, we don't get preempted
|
||||
#define CFG_FIFO_MUTEX OSAL_MUTEX_REQUIRED
|
||||
|
||||
/* Write/Read index is always in the range of:
|
||||
* 0 .. 2*depth-1
|
||||
* The extra window allow us to determine the fifo state of empty or full with only 2 indices
|
||||
* Following are examples with depth = 3
|
||||
*
|
||||
* - empty: W = R
|
||||
* |
|
||||
* -------------------------
|
||||
* | 0 | RW| 2 | 3 | 4 | 5 |
|
||||
*
|
||||
* - full 1: W > R
|
||||
* |
|
||||
* -------------------------
|
||||
* | 0 | R | 2 | 3 | W | 5 |
|
||||
*
|
||||
* - full 2: W < R
|
||||
* |
|
||||
* -------------------------
|
||||
* | 0 | 1 | W | 3 | 4 | R |
|
||||
*
|
||||
* - Number of items in the fifo can be determined in either cases:
|
||||
* - case W >= R: Count = W - R
|
||||
* - case W < R: Count = 2*depth - (R - W)
|
||||
*
|
||||
* In non-overwritable mode, computed Count (in above 2 cases) is at most equal to depth.
|
||||
* However, in over-writable mode, write index can be repeatedly increased and count can be
|
||||
* temporarily larger than depth (overflowed condition) e.g
|
||||
*
|
||||
* - Overflowed 1: write(3), write(1)
|
||||
* In this case we will adjust Read index when read()/peek() is called so that count = depth.
|
||||
* |
|
||||
* -------------------------
|
||||
* | R | 1 | 2 | 3 | W | 5 |
|
||||
*
|
||||
* - Double Overflowed i.e index is out of allowed range [0,2*depth)
|
||||
* This occurs when we continue to write after 1st overflowed to 2nd overflowed. e.g:
|
||||
* write(3), write(1), write(2)
|
||||
* This must be prevented since it will cause unrecoverable state, in above example
|
||||
* if not handled the fifo will be empty instead of continue-to-be full. Since we must not modify
|
||||
* read index in write() function, which cause race condition. We will re-position write index so that
|
||||
* after data is written it is a full fifo i.e W = depth - R
|
||||
*
|
||||
* re-position W = 1 before write(2)
|
||||
* Note: we should also move data from mem[3] to read index as well, but deliberately skipped here
|
||||
* since it is an expensive operation !!!
|
||||
* |
|
||||
* -------------------------
|
||||
* | R | W | 2 | 3 | 4 | 5 |
|
||||
*
|
||||
* perform write(2), result is still a full fifo.
|
||||
*
|
||||
* |
|
||||
* -------------------------
|
||||
* | R | 1 | 2 | W | 4 | 5 |
|
||||
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
uint8_t* buffer ; // buffer pointer
|
||||
uint16_t depth ; // max items
|
||||
|
||||
struct TU_ATTR_PACKED {
|
||||
uint16_t item_size : 15; // size of each item
|
||||
bool overwritable : 1 ; // ovwerwritable when full
|
||||
};
|
||||
|
||||
volatile uint16_t wr_idx ; // write index
|
||||
volatile uint16_t rd_idx ; // read index
|
||||
|
||||
#if OSAL_MUTEX_REQUIRED
|
||||
osal_mutex_t mutex_wr;
|
||||
osal_mutex_t mutex_rd;
|
||||
#endif
|
||||
|
||||
} tu_fifo_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint16_t len_lin ; ///< linear length in item size
|
||||
uint16_t len_wrap ; ///< wrapped length in item size
|
||||
void * ptr_lin ; ///< linear part start pointer
|
||||
void * ptr_wrap ; ///< wrapped part start pointer
|
||||
} tu_fifo_buffer_info_t;
|
||||
|
||||
#define TU_FIFO_INIT(_buffer, _depth, _type, _overwritable) \
|
||||
{ \
|
||||
.buffer = _buffer, \
|
||||
.depth = _depth, \
|
||||
.item_size = sizeof(_type), \
|
||||
.overwritable = _overwritable, \
|
||||
}
|
||||
|
||||
#define TU_FIFO_DEF(_name, _depth, _type, _overwritable) \
|
||||
uint8_t _name##_buf[_depth*sizeof(_type)]; \
|
||||
tu_fifo_t _name = TU_FIFO_INIT(_name##_buf, _depth, _type, _overwritable)
|
||||
|
||||
|
||||
bool tu_fifo_set_overwritable(tu_fifo_t *f, bool overwritable);
|
||||
bool tu_fifo_clear(tu_fifo_t *f);
|
||||
bool tu_fifo_config(tu_fifo_t *f, void* buffer, uint16_t depth, uint16_t item_size, bool overwritable);
|
||||
|
||||
#if OSAL_MUTEX_REQUIRED
|
||||
TU_ATTR_ALWAYS_INLINE static inline
|
||||
void tu_fifo_config_mutex(tu_fifo_t *f, osal_mutex_t wr_mutex, osal_mutex_t rd_mutex)
|
||||
{
|
||||
f->mutex_wr = wr_mutex;
|
||||
f->mutex_rd = rd_mutex;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#define tu_fifo_config_mutex(_f, _wr_mutex, _rd_mutex)
|
||||
|
||||
#endif
|
||||
|
||||
bool tu_fifo_write (tu_fifo_t* f, void const * p_data);
|
||||
uint16_t tu_fifo_write_n (tu_fifo_t* f, void const * p_data, uint16_t n);
|
||||
uint16_t tu_fifo_write_n_const_addr_full_words (tu_fifo_t* f, const void * data, uint16_t n);
|
||||
|
||||
bool tu_fifo_read (tu_fifo_t* f, void * p_buffer);
|
||||
uint16_t tu_fifo_read_n (tu_fifo_t* f, void * p_buffer, uint16_t n);
|
||||
uint16_t tu_fifo_read_n_const_addr_full_words (tu_fifo_t* f, void * buffer, uint16_t n);
|
||||
|
||||
bool tu_fifo_peek (tu_fifo_t* f, void * p_buffer);
|
||||
uint16_t tu_fifo_peek_n (tu_fifo_t* f, void * p_buffer, uint16_t n);
|
||||
|
||||
uint16_t tu_fifo_count (tu_fifo_t* f);
|
||||
uint16_t tu_fifo_remaining (tu_fifo_t* f);
|
||||
bool tu_fifo_empty (tu_fifo_t* f);
|
||||
bool tu_fifo_full (tu_fifo_t* f);
|
||||
bool tu_fifo_overflowed (tu_fifo_t* f);
|
||||
void tu_fifo_correct_read_pointer (tu_fifo_t* f);
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline
|
||||
uint16_t tu_fifo_depth(tu_fifo_t* f)
|
||||
{
|
||||
return f->depth;
|
||||
}
|
||||
|
||||
// Pointer modifications intended to be used in combinations with DMAs.
|
||||
// USE WITH CARE - NO SAFETY CHECKS CONDUCTED HERE! NOT MUTEX PROTECTED!
|
||||
void tu_fifo_advance_write_pointer(tu_fifo_t *f, uint16_t n);
|
||||
void tu_fifo_advance_read_pointer (tu_fifo_t *f, uint16_t n);
|
||||
|
||||
// If you want to read/write from/to the FIFO by use of a DMA, you may need to conduct two copies
|
||||
// to handle a possible wrapping part. These functions deliver a pointer to start
|
||||
// reading/writing from/to and a valid linear length along which no wrap occurs.
|
||||
void tu_fifo_get_read_info (tu_fifo_t *f, tu_fifo_buffer_info_t *info);
|
||||
void tu_fifo_get_write_info(tu_fifo_t *f, tu_fifo_buffer_info_t *info);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _TUSB_FIFO_H_ */
|
|
@ -0,0 +1,437 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2021, Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#ifndef TUSB_MCU_H_
|
||||
#define TUSB_MCU_H_
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Port/Platform Specific
|
||||
// TUP stand for TinyUSB Port/Platform (can be renamed)
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
//------------- Unaligned Memory Access -------------//
|
||||
|
||||
#ifdef __ARM_ARCH
|
||||
// ARM Architecture set __ARM_FEATURE_UNALIGNED to 1 for mcu supports unaligned access
|
||||
#if defined(__ARM_FEATURE_UNALIGNED) && __ARM_FEATURE_UNALIGNED == 1
|
||||
#define TUP_ARCH_STRICT_ALIGN 0
|
||||
#else
|
||||
#define TUP_ARCH_STRICT_ALIGN 1
|
||||
#endif
|
||||
#else
|
||||
// TODO default to strict align for others
|
||||
// Should investigate other architecture such as risv, xtensa, mips for optimal setting
|
||||
#define TUP_ARCH_STRICT_ALIGN 1
|
||||
#endif
|
||||
|
||||
/* USB Controller Attributes for Device, Host or MCU (both)
|
||||
* - ENDPOINT_MAX: max (logical) number of endpoint
|
||||
* - ENDPOINT_EXCLUSIVE_NUMBER: endpoint number with different direction IN and OUT aren't allowed,
|
||||
* e.g EP1 OUT & EP1 IN cannot exist together
|
||||
* - RHPORT_HIGHSPEED: support highspeed with on-chip PHY
|
||||
*/
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// NXP
|
||||
//--------------------------------------------------------------------+
|
||||
#if TU_CHECK_MCU(OPT_MCU_LPC11UXX, OPT_MCU_LPC13XX, OPT_MCU_LPC15XX)
|
||||
#define TUP_USBIP_IP3511
|
||||
#define TUP_DCD_ENDPOINT_MAX 5
|
||||
|
||||
#elif TU_CHECK_MCU(OPT_MCU_LPC175X_6X, OPT_MCU_LPC177X_8X, OPT_MCU_LPC40XX)
|
||||
#define TUP_DCD_ENDPOINT_MAX 16
|
||||
#define TUP_USBIP_OHCI
|
||||
#define TUP_OHCI_RHPORTS 2
|
||||
|
||||
#elif TU_CHECK_MCU(OPT_MCU_LPC51UXX)
|
||||
#define TUP_USBIP_IP3511
|
||||
#define TUP_DCD_ENDPOINT_MAX 5
|
||||
|
||||
#elif TU_CHECK_MCU(OPT_MCU_LPC54)
|
||||
// TODO USB0 has 5, USB1 has 6
|
||||
#define TUP_USBIP_IP3511
|
||||
#define TUP_DCD_ENDPOINT_MAX 6
|
||||
|
||||
#elif TU_CHECK_MCU(OPT_MCU_LPC55)
|
||||
// TODO USB0 has 5, USB1 has 6
|
||||
#define TUP_USBIP_IP3511
|
||||
#define TUP_DCD_ENDPOINT_MAX 6
|
||||
|
||||
#elif TU_CHECK_MCU(OPT_MCU_LPC18XX, OPT_MCU_LPC43XX)
|
||||
// USB0 has 6 with HS PHY, USB1 has 4 only FS
|
||||
#define TUP_USBIP_CHIPIDEA_HS
|
||||
#define TUP_USBIP_EHCI
|
||||
|
||||
#define TUP_DCD_ENDPOINT_MAX 6
|
||||
#define TUP_RHPORT_HIGHSPEED 1
|
||||
|
||||
#elif TU_CHECK_MCU(OPT_MCU_MCXN9)
|
||||
// USB0 is chipidea FS
|
||||
#define TUP_USBIP_CHIPIDEA_FS
|
||||
#define TUP_USBIP_CHIPIDEA_FS_MCX
|
||||
|
||||
// USB1 is chipidea HS
|
||||
#define TUP_USBIP_CHIPIDEA_HS
|
||||
#define TUP_USBIP_EHCI
|
||||
|
||||
#define TUP_DCD_ENDPOINT_MAX 8
|
||||
#define TUP_RHPORT_HIGHSPEED 1
|
||||
|
||||
#elif TU_CHECK_MCU(OPT_MCU_MIMXRT1XXX)
|
||||
#define TUP_USBIP_CHIPIDEA_HS
|
||||
#define TUP_USBIP_EHCI
|
||||
|
||||
#define TUP_DCD_ENDPOINT_MAX 8
|
||||
#define TUP_RHPORT_HIGHSPEED 1
|
||||
|
||||
#elif TU_CHECK_MCU(OPT_MCU_KINETIS_KL, OPT_MCU_KINETIS_K32L)
|
||||
#define TUP_USBIP_CHIPIDEA_FS
|
||||
#define TUP_USBIP_CHIPIDEA_FS_KINETIS
|
||||
#define TUP_DCD_ENDPOINT_MAX 16
|
||||
|
||||
#elif TU_CHECK_MCU(OPT_MCU_MM32F327X)
|
||||
#define TUP_DCD_ENDPOINT_MAX 16
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Nordic
|
||||
//--------------------------------------------------------------------+
|
||||
#elif TU_CHECK_MCU(OPT_MCU_NRF5X)
|
||||
// 8 CBI + 1 ISO
|
||||
#define TUP_DCD_ENDPOINT_MAX 9
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Microchip
|
||||
//--------------------------------------------------------------------+
|
||||
#elif TU_CHECK_MCU(OPT_MCU_SAMD21, OPT_MCU_SAMD51, OPT_MCU_SAME5X) || \
|
||||
TU_CHECK_MCU(OPT_MCU_SAMD11, OPT_MCU_SAML21, OPT_MCU_SAML22)
|
||||
#define TUP_DCD_ENDPOINT_MAX 8
|
||||
|
||||
#elif TU_CHECK_MCU(OPT_MCU_SAMG)
|
||||
#define TUP_DCD_ENDPOINT_MAX 6
|
||||
#define TUP_DCD_ENDPOINT_EXCLUSIVE_NUMBER
|
||||
|
||||
#elif TU_CHECK_MCU(OPT_MCU_SAMX7X)
|
||||
#define TUP_DCD_ENDPOINT_MAX 10
|
||||
#define TUP_RHPORT_HIGHSPEED 1
|
||||
#define TUP_DCD_ENDPOINT_EXCLUSIVE_NUMBER
|
||||
|
||||
#elif TU_CHECK_MCU(OPT_MCU_PIC32MZ)
|
||||
#define TUP_DCD_ENDPOINT_MAX 8
|
||||
#define TUP_DCD_ENDPOINT_EXCLUSIVE_NUMBER
|
||||
|
||||
#elif TU_CHECK_MCU(OPT_MCU_PIC32MX, OPT_MCU_PIC32MM, OPT_MCU_PIC32MK) || \
|
||||
TU_CHECK_MCU(OPT_MCU_PIC24, OPT_MCU_DSPIC33)
|
||||
#define TUP_DCD_ENDPOINT_MAX 16
|
||||
#define TUP_DCD_ENDPOINT_EXCLUSIVE_NUMBER
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// ST
|
||||
//--------------------------------------------------------------------+
|
||||
#elif TU_CHECK_MCU(OPT_MCU_STM32F0)
|
||||
#define TUP_USBIP_FSDEV
|
||||
#define TUP_USBIP_FSDEV_STM32
|
||||
#define TUP_DCD_ENDPOINT_MAX 8
|
||||
|
||||
#elif TU_CHECK_MCU(OPT_MCU_STM32F1)
|
||||
// - F102, F103 use fsdev
|
||||
// - F105, F107 use dwc2
|
||||
#if defined (STM32F105x8) || defined (STM32F105xB) || defined (STM32F105xC) || \
|
||||
defined (STM32F107xB) || defined (STM32F107xC)
|
||||
#define TUP_USBIP_DWC2
|
||||
#define TUP_USBIP_DWC2_STM32
|
||||
|
||||
#define TUP_DCD_ENDPOINT_MAX 4
|
||||
#elif defined(STM32F102x6) || defined(STM32F102xB) || \
|
||||
defined(STM32F103x6) || defined(STM32F103xB) || defined(STM32F103xE) || defined(STM32F103xG)
|
||||
#define TUP_USBIP_FSDEV
|
||||
#define TUP_USBIP_FSDEV_STM32
|
||||
#define TUP_DCD_ENDPOINT_MAX 8
|
||||
#else
|
||||
#error "Unsupported STM32F1 mcu"
|
||||
#endif
|
||||
|
||||
#elif TU_CHECK_MCU(OPT_MCU_STM32F2)
|
||||
#define TUP_USBIP_DWC2
|
||||
#define TUP_USBIP_DWC2_STM32
|
||||
|
||||
// FS has 4 ep, HS has 5 ep
|
||||
#define TUP_DCD_ENDPOINT_MAX 6
|
||||
|
||||
#elif TU_CHECK_MCU(OPT_MCU_STM32F3)
|
||||
#define TUP_USBIP_FSDEV
|
||||
#define TUP_USBIP_FSDEV_STM32
|
||||
#define TUP_DCD_ENDPOINT_MAX 8
|
||||
|
||||
#elif TU_CHECK_MCU(OPT_MCU_STM32F4)
|
||||
#define TUP_USBIP_DWC2
|
||||
#define TUP_USBIP_DWC2_STM32
|
||||
|
||||
// For most mcu, FS has 4, HS has 6. TODO 446/469/479 HS has 9
|
||||
#define TUP_DCD_ENDPOINT_MAX 6
|
||||
|
||||
#elif TU_CHECK_MCU(OPT_MCU_STM32F7)
|
||||
#define TUP_USBIP_DWC2
|
||||
#define TUP_USBIP_DWC2_STM32
|
||||
|
||||
// FS has 6, HS has 9
|
||||
#define TUP_DCD_ENDPOINT_MAX 9
|
||||
|
||||
// MCU with on-chip HS Phy
|
||||
#if defined(STM32F723xx) || defined(STM32F730xx) || defined(STM32F733xx)
|
||||
#define TUP_RHPORT_HIGHSPEED 1 // Port0: FS, Port1: HS
|
||||
#endif
|
||||
|
||||
#elif TU_CHECK_MCU(OPT_MCU_STM32H7)
|
||||
#define TUP_USBIP_DWC2
|
||||
#define TUP_USBIP_DWC2_STM32
|
||||
|
||||
#define TUP_DCD_ENDPOINT_MAX 9
|
||||
|
||||
#elif TU_CHECK_MCU(OPT_MCU_STM32H5)
|
||||
#define TUP_USBIP_FSDEV
|
||||
#define TUP_USBIP_FSDEV_STM32
|
||||
#define TUP_DCD_ENDPOINT_MAX 8
|
||||
|
||||
#elif TU_CHECK_MCU(OPT_MCU_STM32G4)
|
||||
// Device controller
|
||||
#define TUP_USBIP_FSDEV
|
||||
#define TUP_USBIP_FSDEV_STM32
|
||||
|
||||
// TypeC controller
|
||||
#define TUP_USBIP_TYPEC_STM32
|
||||
#define TUP_DCD_ENDPOINT_MAX 8
|
||||
#define TUP_TYPEC_RHPORTS_NUM 1
|
||||
|
||||
#elif TU_CHECK_MCU(OPT_MCU_STM32G0)
|
||||
#define TUP_USBIP_FSDEV
|
||||
#define TUP_USBIP_FSDEV_STM32
|
||||
#define TUP_DCD_ENDPOINT_MAX 8
|
||||
|
||||
#elif TU_CHECK_MCU(OPT_MCU_STM32L0, OPT_MCU_STM32L1)
|
||||
#define TUP_USBIP_FSDEV
|
||||
#define TUP_USBIP_FSDEV_STM32
|
||||
#define TUP_DCD_ENDPOINT_MAX 8
|
||||
|
||||
#elif TU_CHECK_MCU(OPT_MCU_STM32L4)
|
||||
// - L4x2, L4x3 use fsdev
|
||||
// - L4x4, L4x6, L4x7, L4x9 use dwc2
|
||||
#if defined (STM32L475xx) || defined (STM32L476xx) || \
|
||||
defined (STM32L485xx) || defined (STM32L486xx) || defined (STM32L496xx) || \
|
||||
defined (STM32L4A6xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) || \
|
||||
defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || \
|
||||
defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
|
||||
#define TUP_USBIP_DWC2
|
||||
#define TUP_USBIP_DWC2_STM32
|
||||
|
||||
#define TUP_DCD_ENDPOINT_MAX 6
|
||||
#elif defined(STM32L412xx) || defined(STM32L422xx) || defined(STM32L432xx) || defined(STM32L433xx) || \
|
||||
defined(STM32L442xx) || defined(STM32L443xx) || defined(STM32L452xx) || defined(STM32L462xx)
|
||||
#define TUP_USBIP_FSDEV
|
||||
#define TUP_USBIP_FSDEV_STM32
|
||||
#define TUP_DCD_ENDPOINT_MAX 8
|
||||
#else
|
||||
#error "Unsupported STM32L4 mcu"
|
||||
#endif
|
||||
|
||||
#elif TU_CHECK_MCU(OPT_MCU_STM32WB)
|
||||
#define TUP_USBIP_FSDEV
|
||||
#define TUP_USBIP_FSDEV_STM32
|
||||
#define TUP_DCD_ENDPOINT_MAX 8
|
||||
|
||||
#elif TU_CHECK_MCU(OPT_MCU_STM32U5)
|
||||
#define TUP_USBIP_DWC2
|
||||
#define TUP_USBIP_DWC2_STM32
|
||||
|
||||
// U59x/5Ax/5Fx/5Gx are highspeed with built-in HS PHY
|
||||
#if defined(STM32U595xx) || defined(STM32U599xx) || defined(STM32U5A5xx) || defined(STM32U5A9xx) || \
|
||||
defined(STM32U5F7xx) || defined(STM32U5F9xx) || defined(STM32U5G7xx) || defined(STM32U5G9xx)
|
||||
#define TUP_DCD_ENDPOINT_MAX 9
|
||||
#define TUP_RHPORT_HIGHSPEED 1
|
||||
#else
|
||||
#define TUP_DCD_ENDPOINT_MAX 6
|
||||
#endif
|
||||
|
||||
#elif TU_CHECK_MCU(OPT_MCU_STM32L5)
|
||||
#define TUP_USBIP_FSDEV
|
||||
#define TUP_USBIP_FSDEV_STM32
|
||||
#define TUP_DCD_ENDPOINT_MAX 8
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Sony
|
||||
//--------------------------------------------------------------------+
|
||||
#elif TU_CHECK_MCU(OPT_MCU_CXD56)
|
||||
#define TUP_DCD_ENDPOINT_MAX 7
|
||||
#define TUP_RHPORT_HIGHSPEED 1
|
||||
#define TUP_DCD_ENDPOINT_EXCLUSIVE_NUMBER
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// TI
|
||||
//--------------------------------------------------------------------+
|
||||
#elif TU_CHECK_MCU(OPT_MCU_MSP430x5xx)
|
||||
#define TUP_DCD_ENDPOINT_MAX 8
|
||||
|
||||
#elif TU_CHECK_MCU(OPT_MCU_MSP432E4, OPT_MCU_TM4C123, OPT_MCU_TM4C129)
|
||||
#define TUP_DCD_ENDPOINT_MAX 8
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// ValentyUSB (Litex)
|
||||
//--------------------------------------------------------------------+
|
||||
#elif TU_CHECK_MCU(OPT_MCU_VALENTYUSB_EPTRI)
|
||||
#define TUP_DCD_ENDPOINT_MAX 16
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Nuvoton
|
||||
//--------------------------------------------------------------------+
|
||||
#elif TU_CHECK_MCU(OPT_MCU_NUC121, OPT_MCU_NUC126)
|
||||
#define TUP_DCD_ENDPOINT_MAX 8
|
||||
|
||||
#elif TU_CHECK_MCU(OPT_MCU_NUC120)
|
||||
#define TUP_DCD_ENDPOINT_MAX 6
|
||||
|
||||
#elif TU_CHECK_MCU(OPT_MCU_NUC505)
|
||||
#define TUP_DCD_ENDPOINT_MAX 12
|
||||
#define TUP_RHPORT_HIGHSPEED 1
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Espressif
|
||||
//--------------------------------------------------------------------+
|
||||
#elif TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
|
||||
#define TUP_USBIP_DWC2
|
||||
#define TUP_DCD_ENDPOINT_MAX 6
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Dialog
|
||||
//--------------------------------------------------------------------+
|
||||
#elif TU_CHECK_MCU(OPT_MCU_DA1469X)
|
||||
#define TUP_DCD_ENDPOINT_MAX 4
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Raspberry Pi
|
||||
//--------------------------------------------------------------------+
|
||||
#elif TU_CHECK_MCU(OPT_MCU_RP2040)
|
||||
#define TUP_DCD_ENDPOINT_MAX 16
|
||||
|
||||
#define TU_ATTR_FAST_FUNC __attribute__((section(".time_critical.tinyusb")))
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Silabs
|
||||
//--------------------------------------------------------------------+
|
||||
#elif TU_CHECK_MCU(OPT_MCU_EFM32GG)
|
||||
#define TUP_USBIP_DWC2
|
||||
#define TUP_DCD_ENDPOINT_MAX 7
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Renesas
|
||||
//--------------------------------------------------------------------+
|
||||
#elif TU_CHECK_MCU(OPT_MCU_RX63X, OPT_MCU_RX65X, OPT_MCU_RX72N, OPT_MCU_RAXXX)
|
||||
#define TUP_USBIP_RUSB2
|
||||
#define TUP_DCD_ENDPOINT_MAX 10
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// GigaDevice
|
||||
//--------------------------------------------------------------------+
|
||||
#elif TU_CHECK_MCU(OPT_MCU_GD32VF103)
|
||||
#define TUP_USBIP_DWC2
|
||||
#define TUP_DCD_ENDPOINT_MAX 4
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Broadcom
|
||||
//--------------------------------------------------------------------+
|
||||
#elif TU_CHECK_MCU(OPT_MCU_BCM2711, OPT_MCU_BCM2835, OPT_MCU_BCM2837)
|
||||
#define TUP_USBIP_DWC2
|
||||
#define TUP_DCD_ENDPOINT_MAX 8
|
||||
#define TUP_RHPORT_HIGHSPEED 1
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Infineon
|
||||
//--------------------------------------------------------------------+
|
||||
#elif TU_CHECK_MCU(OPT_MCU_XMC4000)
|
||||
#define TUP_USBIP_DWC2
|
||||
#define TUP_DCD_ENDPOINT_MAX 8
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// BridgeTek
|
||||
//--------------------------------------------------------------------+
|
||||
#elif TU_CHECK_MCU(OPT_MCU_FT90X)
|
||||
#define TUP_DCD_ENDPOINT_MAX 8
|
||||
#define TUP_RHPORT_HIGHSPEED 1
|
||||
|
||||
#elif TU_CHECK_MCU(OPT_MCU_FT93X)
|
||||
#define TUP_DCD_ENDPOINT_MAX 16
|
||||
#define TUP_RHPORT_HIGHSPEED 1
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Allwinner
|
||||
//--------------------------------------------------------------------+
|
||||
#elif TU_CHECK_MCU(OPT_MCU_F1C100S)
|
||||
#define TUP_DCD_ENDPOINT_MAX 4
|
||||
|
||||
//------------- WCH -------------//
|
||||
#elif TU_CHECK_MCU(OPT_MCU_CH32V307)
|
||||
#define TUP_DCD_ENDPOINT_MAX 16
|
||||
#define TUP_RHPORT_HIGHSPEED 1
|
||||
|
||||
#elif TU_CHECK_MCU(OPT_MCU_CH32F20X)
|
||||
#define TUP_DCD_ENDPOINT_MAX 16
|
||||
#define TUP_RHPORT_HIGHSPEED 1
|
||||
#endif
|
||||
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// External USB controller
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
#if defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421
|
||||
#ifndef CFG_TUH_MAX3421_ENDPOINT_TOTAL
|
||||
#define CFG_TUH_MAX3421_ENDPOINT_TOTAL (8 + 4*(CFG_TUH_DEVICE_MAX-1))
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Default Values
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
#ifndef TUP_MCU_MULTIPLE_CORE
|
||||
#define TUP_MCU_MULTIPLE_CORE 0
|
||||
#endif
|
||||
|
||||
#ifndef TUP_DCD_ENDPOINT_MAX
|
||||
#warning "TUP_DCD_ENDPOINT_MAX is not defined for this MCU, default to 8"
|
||||
#define TUP_DCD_ENDPOINT_MAX 8
|
||||
#endif
|
||||
|
||||
// Default to fullspeed if not defined
|
||||
#ifndef TUP_RHPORT_HIGHSPEED
|
||||
#define TUP_RHPORT_HIGHSPEED 0
|
||||
#endif
|
||||
|
||||
// fast function, normally mean placing function in SRAM
|
||||
#ifndef TU_ATTR_FAST_FUNC
|
||||
#define TU_ATTR_FAST_FUNC
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -0,0 +1,178 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2022, Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef _TUSB_PRIVATE_H_
|
||||
#define _TUSB_PRIVATE_H_
|
||||
|
||||
// Internal Helper used by Host and Device Stack
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct TU_ATTR_PACKED
|
||||
{
|
||||
volatile uint8_t busy : 1;
|
||||
volatile uint8_t stalled : 1;
|
||||
volatile uint8_t claimed : 1;
|
||||
}tu_edpt_state_t;
|
||||
|
||||
typedef struct {
|
||||
bool is_host; // host or device most
|
||||
union {
|
||||
uint8_t daddr;
|
||||
uint8_t rhport;
|
||||
uint8_t hwid;
|
||||
};
|
||||
uint8_t ep_addr;
|
||||
uint8_t ep_speed;
|
||||
|
||||
uint16_t ep_packetsize;
|
||||
uint16_t ep_bufsize;
|
||||
|
||||
// TODO xfer_fifo can skip this buffer
|
||||
uint8_t* ep_buf;
|
||||
|
||||
tu_fifo_t ff;
|
||||
|
||||
// mutex: read if ep rx, write if e tx
|
||||
OSAL_MUTEX_DEF(ff_mutex);
|
||||
|
||||
}tu_edpt_stream_t;
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Endpoint
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Check if endpoint descriptor is valid per USB specs
|
||||
bool tu_edpt_validate(tusb_desc_endpoint_t const * desc_ep, tusb_speed_t speed);
|
||||
|
||||
// Bind all endpoint of a interface descriptor to class driver
|
||||
void tu_edpt_bind_driver(uint8_t ep2drv[][2], tusb_desc_interface_t const* p_desc, uint16_t desc_len, uint8_t driver_id);
|
||||
|
||||
// Calculate total length of n interfaces (depending on IAD)
|
||||
uint16_t tu_desc_get_interface_total_len(tusb_desc_interface_t const* desc_itf, uint8_t itf_count, uint16_t max_len);
|
||||
|
||||
// Claim an endpoint with provided mutex
|
||||
bool tu_edpt_claim(tu_edpt_state_t* ep_state, osal_mutex_t mutex);
|
||||
|
||||
// Release an endpoint with provided mutex
|
||||
bool tu_edpt_release(tu_edpt_state_t* ep_state, osal_mutex_t mutex);
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Endpoint Stream
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Init an stream, should only be called once
|
||||
bool tu_edpt_stream_init(tu_edpt_stream_t* s, bool is_host, bool is_tx, bool overwritable,
|
||||
void* ff_buf, uint16_t ff_bufsize, uint8_t* ep_buf, uint16_t ep_bufsize);
|
||||
|
||||
// Open an stream for an endpoint
|
||||
// hwid is either device address (host mode) or rhport (device mode)
|
||||
TU_ATTR_ALWAYS_INLINE static inline
|
||||
void tu_edpt_stream_open(tu_edpt_stream_t* s, uint8_t hwid, tusb_desc_endpoint_t const *desc_ep)
|
||||
{
|
||||
tu_fifo_clear(&s->ff);
|
||||
s->hwid = hwid;
|
||||
s->ep_addr = desc_ep->bEndpointAddress;
|
||||
s->ep_packetsize = tu_edpt_packet_size(desc_ep);
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline
|
||||
void tu_edpt_stream_close(tu_edpt_stream_t* s)
|
||||
{
|
||||
s->hwid = 0;
|
||||
s->ep_addr = 0;
|
||||
}
|
||||
|
||||
// Clear fifo
|
||||
TU_ATTR_ALWAYS_INLINE static inline
|
||||
bool tu_edpt_stream_clear(tu_edpt_stream_t* s)
|
||||
{
|
||||
return tu_fifo_clear(&s->ff);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Stream Write
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Write to stream
|
||||
uint32_t tu_edpt_stream_write(tu_edpt_stream_t* s, void const *buffer, uint32_t bufsize);
|
||||
|
||||
// Start an usb transfer if endpoint is not busy
|
||||
uint32_t tu_edpt_stream_write_xfer(tu_edpt_stream_t* s);
|
||||
|
||||
// Start an zero-length packet if needed
|
||||
bool tu_edpt_stream_write_zlp_if_needed(tu_edpt_stream_t* s, uint32_t last_xferred_bytes);
|
||||
|
||||
// Get the number of bytes available for writing
|
||||
TU_ATTR_ALWAYS_INLINE static inline
|
||||
uint32_t tu_edpt_stream_write_available(tu_edpt_stream_t* s)
|
||||
{
|
||||
return (uint32_t) tu_fifo_remaining(&s->ff);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Stream Read
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Read from stream
|
||||
uint32_t tu_edpt_stream_read(tu_edpt_stream_t* s, void* buffer, uint32_t bufsize);
|
||||
|
||||
// Start an usb transfer if endpoint is not busy
|
||||
uint32_t tu_edpt_stream_read_xfer(tu_edpt_stream_t* s);
|
||||
|
||||
// Must be called in the transfer complete callback
|
||||
TU_ATTR_ALWAYS_INLINE static inline
|
||||
void tu_edpt_stream_read_xfer_complete(tu_edpt_stream_t* s, uint32_t xferred_bytes) {
|
||||
tu_fifo_write_n(&s->ff, s->ep_buf, (uint16_t) xferred_bytes);
|
||||
}
|
||||
|
||||
// Same as tu_edpt_stream_read_xfer_complete but skip the first n bytes
|
||||
TU_ATTR_ALWAYS_INLINE static inline
|
||||
void tu_edpt_stream_read_xfer_complete_offset(tu_edpt_stream_t* s, uint32_t xferred_bytes, uint32_t skip_offset) {
|
||||
if (skip_offset < xferred_bytes) {
|
||||
tu_fifo_write_n(&s->ff, s->ep_buf + skip_offset, (uint16_t) (xferred_bytes - skip_offset));
|
||||
}
|
||||
}
|
||||
|
||||
// Get the number of bytes available for reading
|
||||
TU_ATTR_ALWAYS_INLINE static inline
|
||||
uint32_t tu_edpt_stream_read_available(tu_edpt_stream_t* s) {
|
||||
return (uint32_t) tu_fifo_count(&s->ff);
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline
|
||||
bool tu_edpt_stream_peek(tu_edpt_stream_t* s, uint8_t* ch) {
|
||||
return tu_fifo_peek(&s->ff, ch);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _TUSB_PRIVATE_H_ */
|
|
@ -0,0 +1,585 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
/** \ingroup group_usb_definitions
|
||||
* \defgroup USBDef_Type USB Types
|
||||
* @{ */
|
||||
|
||||
#ifndef _TUSB_TYPES_H_
|
||||
#define _TUSB_TYPES_H_
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include "tusb_compiler.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* CONSTANTS
|
||||
*------------------------------------------------------------------*/
|
||||
|
||||
/// defined base on EHCI specs value for Endpoint Speed
|
||||
typedef enum
|
||||
{
|
||||
TUSB_SPEED_FULL = 0,
|
||||
TUSB_SPEED_LOW = 1,
|
||||
TUSB_SPEED_HIGH = 2,
|
||||
TUSB_SPEED_INVALID = 0xff,
|
||||
}tusb_speed_t;
|
||||
|
||||
/// defined base on USB Specs Endpoint's bmAttributes
|
||||
typedef enum
|
||||
{
|
||||
TUSB_XFER_CONTROL = 0 ,
|
||||
TUSB_XFER_ISOCHRONOUS ,
|
||||
TUSB_XFER_BULK ,
|
||||
TUSB_XFER_INTERRUPT
|
||||
}tusb_xfer_type_t;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
TUSB_DIR_OUT = 0,
|
||||
TUSB_DIR_IN = 1,
|
||||
|
||||
TUSB_DIR_IN_MASK = 0x80
|
||||
}tusb_dir_t;
|
||||
|
||||
enum
|
||||
{
|
||||
TUSB_EPSIZE_BULK_FS = 64,
|
||||
TUSB_EPSIZE_BULK_HS= 512,
|
||||
|
||||
TUSB_EPSIZE_ISO_FS_MAX = 1023,
|
||||
TUSB_EPSIZE_ISO_HS_MAX = 1024,
|
||||
};
|
||||
|
||||
/// Isochronous End Point Attributes
|
||||
typedef enum
|
||||
{
|
||||
TUSB_ISO_EP_ATT_NO_SYNC = 0x00,
|
||||
TUSB_ISO_EP_ATT_ASYNCHRONOUS = 0x04,
|
||||
TUSB_ISO_EP_ATT_ADAPTIVE = 0x08,
|
||||
TUSB_ISO_EP_ATT_SYNCHRONOUS = 0x0C,
|
||||
TUSB_ISO_EP_ATT_DATA = 0x00, ///< Data End Point
|
||||
TUSB_ISO_EP_ATT_EXPLICIT_FB = 0x10, ///< Feedback End Point
|
||||
TUSB_ISO_EP_ATT_IMPLICIT_FB = 0x20, ///< Data endpoint that also serves as an implicit feedback
|
||||
}tusb_iso_ep_attribute_t;
|
||||
|
||||
/// USB Descriptor Types
|
||||
typedef enum
|
||||
{
|
||||
TUSB_DESC_DEVICE = 0x01,
|
||||
TUSB_DESC_CONFIGURATION = 0x02,
|
||||
TUSB_DESC_STRING = 0x03,
|
||||
TUSB_DESC_INTERFACE = 0x04,
|
||||
TUSB_DESC_ENDPOINT = 0x05,
|
||||
TUSB_DESC_DEVICE_QUALIFIER = 0x06,
|
||||
TUSB_DESC_OTHER_SPEED_CONFIG = 0x07,
|
||||
TUSB_DESC_INTERFACE_POWER = 0x08,
|
||||
TUSB_DESC_OTG = 0x09,
|
||||
TUSB_DESC_DEBUG = 0x0A,
|
||||
TUSB_DESC_INTERFACE_ASSOCIATION = 0x0B,
|
||||
|
||||
TUSB_DESC_BOS = 0x0F,
|
||||
TUSB_DESC_DEVICE_CAPABILITY = 0x10,
|
||||
|
||||
TUSB_DESC_FUNCTIONAL = 0x21,
|
||||
|
||||
// Class Specific Descriptor
|
||||
TUSB_DESC_CS_DEVICE = 0x21,
|
||||
TUSB_DESC_CS_CONFIGURATION = 0x22,
|
||||
TUSB_DESC_CS_STRING = 0x23,
|
||||
TUSB_DESC_CS_INTERFACE = 0x24,
|
||||
TUSB_DESC_CS_ENDPOINT = 0x25,
|
||||
|
||||
TUSB_DESC_SUPERSPEED_ENDPOINT_COMPANION = 0x30,
|
||||
TUSB_DESC_SUPERSPEED_ISO_ENDPOINT_COMPANION = 0x31
|
||||
}tusb_desc_type_t;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
TUSB_REQ_GET_STATUS = 0 ,
|
||||
TUSB_REQ_CLEAR_FEATURE = 1 ,
|
||||
TUSB_REQ_RESERVED = 2 ,
|
||||
TUSB_REQ_SET_FEATURE = 3 ,
|
||||
TUSB_REQ_RESERVED2 = 4 ,
|
||||
TUSB_REQ_SET_ADDRESS = 5 ,
|
||||
TUSB_REQ_GET_DESCRIPTOR = 6 ,
|
||||
TUSB_REQ_SET_DESCRIPTOR = 7 ,
|
||||
TUSB_REQ_GET_CONFIGURATION = 8 ,
|
||||
TUSB_REQ_SET_CONFIGURATION = 9 ,
|
||||
TUSB_REQ_GET_INTERFACE = 10 ,
|
||||
TUSB_REQ_SET_INTERFACE = 11 ,
|
||||
TUSB_REQ_SYNCH_FRAME = 12
|
||||
}tusb_request_code_t;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
TUSB_REQ_FEATURE_EDPT_HALT = 0,
|
||||
TUSB_REQ_FEATURE_REMOTE_WAKEUP = 1,
|
||||
TUSB_REQ_FEATURE_TEST_MODE = 2
|
||||
}tusb_request_feature_selector_t;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
TUSB_REQ_TYPE_STANDARD = 0,
|
||||
TUSB_REQ_TYPE_CLASS,
|
||||
TUSB_REQ_TYPE_VENDOR,
|
||||
TUSB_REQ_TYPE_INVALID
|
||||
} tusb_request_type_t;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
TUSB_REQ_RCPT_DEVICE =0,
|
||||
TUSB_REQ_RCPT_INTERFACE,
|
||||
TUSB_REQ_RCPT_ENDPOINT,
|
||||
TUSB_REQ_RCPT_OTHER
|
||||
} tusb_request_recipient_t;
|
||||
|
||||
// https://www.usb.org/defined-class-codes
|
||||
typedef enum
|
||||
{
|
||||
TUSB_CLASS_UNSPECIFIED = 0 ,
|
||||
TUSB_CLASS_AUDIO = 1 ,
|
||||
TUSB_CLASS_CDC = 2 ,
|
||||
TUSB_CLASS_HID = 3 ,
|
||||
TUSB_CLASS_RESERVED_4 = 4 ,
|
||||
TUSB_CLASS_PHYSICAL = 5 ,
|
||||
TUSB_CLASS_IMAGE = 6 ,
|
||||
TUSB_CLASS_PRINTER = 7 ,
|
||||
TUSB_CLASS_MSC = 8 ,
|
||||
TUSB_CLASS_HUB = 9 ,
|
||||
TUSB_CLASS_CDC_DATA = 10 ,
|
||||
TUSB_CLASS_SMART_CARD = 11 ,
|
||||
TUSB_CLASS_RESERVED_12 = 12 ,
|
||||
TUSB_CLASS_CONTENT_SECURITY = 13 ,
|
||||
TUSB_CLASS_VIDEO = 14 ,
|
||||
TUSB_CLASS_PERSONAL_HEALTHCARE = 15 ,
|
||||
TUSB_CLASS_AUDIO_VIDEO = 16 ,
|
||||
|
||||
TUSB_CLASS_DIAGNOSTIC = 0xDC ,
|
||||
TUSB_CLASS_WIRELESS_CONTROLLER = 0xE0 ,
|
||||
TUSB_CLASS_MISC = 0xEF ,
|
||||
TUSB_CLASS_APPLICATION_SPECIFIC = 0xFE ,
|
||||
TUSB_CLASS_VENDOR_SPECIFIC = 0xFF
|
||||
}tusb_class_code_t;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
MISC_SUBCLASS_COMMON = 2
|
||||
}misc_subclass_type_t;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
MISC_PROTOCOL_IAD = 1
|
||||
}misc_protocol_type_t;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
APP_SUBCLASS_USBTMC = 0x03,
|
||||
APP_SUBCLASS_DFU_RUNTIME = 0x01
|
||||
} app_subclass_type_t;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
DEVICE_CAPABILITY_WIRELESS_USB = 0x01,
|
||||
DEVICE_CAPABILITY_USB20_EXTENSION = 0x02,
|
||||
DEVICE_CAPABILITY_SUPERSPEED_USB = 0x03,
|
||||
DEVICE_CAPABILITY_CONTAINER_id = 0x04,
|
||||
DEVICE_CAPABILITY_PLATFORM = 0x05,
|
||||
DEVICE_CAPABILITY_POWER_DELIVERY = 0x06,
|
||||
DEVICE_CAPABILITY_BATTERY_INFO = 0x07,
|
||||
DEVICE_CAPABILITY_PD_CONSUMER_PORT = 0x08,
|
||||
DEVICE_CAPABILITY_PD_PROVIDER_PORT = 0x09,
|
||||
DEVICE_CAPABILITY_SUPERSPEED_PLUS = 0x0A,
|
||||
DEVICE_CAPABILITY_PRECESION_TIME_MEASUREMENT = 0x0B,
|
||||
DEVICE_CAPABILITY_WIRELESS_USB_EXT = 0x0C,
|
||||
DEVICE_CAPABILITY_BILLBOARD = 0x0D,
|
||||
DEVICE_CAPABILITY_AUTHENTICATION = 0x0E,
|
||||
DEVICE_CAPABILITY_BILLBOARD_EX = 0x0F,
|
||||
DEVICE_CAPABILITY_CONFIGURATION_SUMMARY = 0x10
|
||||
}device_capability_type_t;
|
||||
|
||||
enum {
|
||||
TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP = TU_BIT(5),
|
||||
TUSB_DESC_CONFIG_ATT_SELF_POWERED = TU_BIT(6),
|
||||
};
|
||||
|
||||
#define TUSB_DESC_CONFIG_POWER_MA(x) ((x)/2)
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
//
|
||||
//--------------------------------------------------------------------+
|
||||
typedef enum
|
||||
{
|
||||
XFER_RESULT_SUCCESS = 0,
|
||||
XFER_RESULT_FAILED,
|
||||
XFER_RESULT_STALLED,
|
||||
XFER_RESULT_TIMEOUT,
|
||||
XFER_RESULT_INVALID
|
||||
}xfer_result_t;
|
||||
|
||||
enum // TODO remove
|
||||
{
|
||||
DESC_OFFSET_LEN = 0,
|
||||
DESC_OFFSET_TYPE = 1
|
||||
};
|
||||
|
||||
enum
|
||||
{
|
||||
INTERFACE_INVALID_NUMBER = 0xff
|
||||
};
|
||||
|
||||
typedef enum
|
||||
{
|
||||
MS_OS_20_SET_HEADER_DESCRIPTOR = 0x00,
|
||||
MS_OS_20_SUBSET_HEADER_CONFIGURATION = 0x01,
|
||||
MS_OS_20_SUBSET_HEADER_FUNCTION = 0x02,
|
||||
MS_OS_20_FEATURE_COMPATBLE_ID = 0x03,
|
||||
MS_OS_20_FEATURE_REG_PROPERTY = 0x04,
|
||||
MS_OS_20_FEATURE_MIN_RESUME_TIME = 0x05,
|
||||
MS_OS_20_FEATURE_MODEL_ID = 0x06,
|
||||
MS_OS_20_FEATURE_CCGP_DEVICE = 0x07,
|
||||
MS_OS_20_FEATURE_VENDOR_REVISION = 0x08
|
||||
} microsoft_os_20_type_t;
|
||||
|
||||
enum
|
||||
{
|
||||
CONTROL_STAGE_IDLE,
|
||||
CONTROL_STAGE_SETUP,
|
||||
CONTROL_STAGE_DATA,
|
||||
CONTROL_STAGE_ACK
|
||||
};
|
||||
|
||||
enum
|
||||
{
|
||||
TUSB_INDEX_INVALID_8 = 0xFFu
|
||||
};
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// USB Descriptors
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Start of all packed definitions for compiler without per-type packed
|
||||
TU_ATTR_PACKED_BEGIN
|
||||
TU_ATTR_BIT_FIELD_ORDER_BEGIN
|
||||
|
||||
/// USB Device Descriptor
|
||||
typedef struct TU_ATTR_PACKED
|
||||
{
|
||||
uint8_t bLength ; ///< Size of this descriptor in bytes.
|
||||
uint8_t bDescriptorType ; ///< DEVICE Descriptor Type.
|
||||
uint16_t bcdUSB ; ///< BUSB Specification Release Number in Binary-Coded Decimal (i.e., 2.10 is 210H). This field identifies the release of the USB Specification with which the device and its descriptors are compliant.
|
||||
|
||||
uint8_t bDeviceClass ; ///< Class code (assigned by the USB-IF). \li If this field is reset to zero, each interface within a configuration specifies its own class information and the various interfaces operate independently. \li If this field is set to a value between 1 and FEH, the device supports different class specifications on different interfaces and the interfaces may not operate independently. This value identifies the class definition used for the aggregate interfaces. \li If this field is set to FFH, the device class is vendor-specific.
|
||||
uint8_t bDeviceSubClass ; ///< Subclass code (assigned by the USB-IF). These codes are qualified by the value of the bDeviceClass field. \li If the bDeviceClass field is reset to zero, this field must also be reset to zero. \li If the bDeviceClass field is not set to FFH, all values are reserved for assignment by the USB-IF.
|
||||
uint8_t bDeviceProtocol ; ///< Protocol code (assigned by the USB-IF). These codes are qualified by the value of the bDeviceClass and the bDeviceSubClass fields. If a device supports class-specific protocols on a device basis as opposed to an interface basis, this code identifies the protocols that the device uses as defined by the specification of the device class. \li If this field is reset to zero, the device does not use class-specific protocols on a device basis. However, it may use classspecific protocols on an interface basis. \li If this field is set to FFH, the device uses a vendor-specific protocol on a device basis.
|
||||
uint8_t bMaxPacketSize0 ; ///< Maximum packet size for endpoint zero (only 8, 16, 32, or 64 are valid). For HS devices is fixed to 64.
|
||||
|
||||
uint16_t idVendor ; ///< Vendor ID (assigned by the USB-IF).
|
||||
uint16_t idProduct ; ///< Product ID (assigned by the manufacturer).
|
||||
uint16_t bcdDevice ; ///< Device release number in binary-coded decimal.
|
||||
uint8_t iManufacturer ; ///< Index of string descriptor describing manufacturer.
|
||||
uint8_t iProduct ; ///< Index of string descriptor describing product.
|
||||
uint8_t iSerialNumber ; ///< Index of string descriptor describing the device's serial number.
|
||||
|
||||
uint8_t bNumConfigurations ; ///< Number of possible configurations.
|
||||
} tusb_desc_device_t;
|
||||
|
||||
TU_VERIFY_STATIC( sizeof(tusb_desc_device_t) == 18, "size is not correct");
|
||||
|
||||
// USB Binary Device Object Store (BOS) Descriptor
|
||||
typedef struct TU_ATTR_PACKED
|
||||
{
|
||||
uint8_t bLength ; ///< Size of this descriptor in bytes
|
||||
uint8_t bDescriptorType ; ///< CONFIGURATION Descriptor Type
|
||||
uint16_t wTotalLength ; ///< Total length of data returned for this descriptor
|
||||
uint8_t bNumDeviceCaps ; ///< Number of device capability descriptors in the BOS
|
||||
} tusb_desc_bos_t;
|
||||
|
||||
TU_VERIFY_STATIC( sizeof(tusb_desc_bos_t) == 5, "size is not correct");
|
||||
|
||||
/// USB Configuration Descriptor
|
||||
typedef struct TU_ATTR_PACKED
|
||||
{
|
||||
uint8_t bLength ; ///< Size of this descriptor in bytes
|
||||
uint8_t bDescriptorType ; ///< CONFIGURATION Descriptor Type
|
||||
uint16_t wTotalLength ; ///< Total length of data returned for this configuration. Includes the combined length of all descriptors (configuration, interface, endpoint, and class- or vendor-specific) returned for this configuration.
|
||||
|
||||
uint8_t bNumInterfaces ; ///< Number of interfaces supported by this configuration
|
||||
uint8_t bConfigurationValue ; ///< Value to use as an argument to the SetConfiguration() request to select this configuration.
|
||||
uint8_t iConfiguration ; ///< Index of string descriptor describing this configuration
|
||||
uint8_t bmAttributes ; ///< Configuration characteristics \n D7: Reserved (set to one)\n D6: Self-powered \n D5: Remote Wakeup \n D4...0: Reserved (reset to zero) \n D7 is reserved and must be set to one for historical reasons. \n A device configuration that uses power from the bus and a local source reports a non-zero value in bMaxPower to indicate the amount of bus power required and sets D6. The actual power source at runtime may be determined using the GetStatus(DEVICE) request (see USB 2.0 spec Section 9.4.5). \n If a device configuration supports remote wakeup, D5 is set to one.
|
||||
uint8_t bMaxPower ; ///< Maximum power consumption of the USB device from the bus in this specific configuration when the device is fully operational. Expressed in 2 mA units (i.e., 50 = 100 mA).
|
||||
} tusb_desc_configuration_t;
|
||||
|
||||
TU_VERIFY_STATIC( sizeof(tusb_desc_configuration_t) == 9, "size is not correct");
|
||||
|
||||
/// USB Interface Descriptor
|
||||
typedef struct TU_ATTR_PACKED
|
||||
{
|
||||
uint8_t bLength ; ///< Size of this descriptor in bytes
|
||||
uint8_t bDescriptorType ; ///< INTERFACE Descriptor Type
|
||||
|
||||
uint8_t bInterfaceNumber ; ///< Number of this interface. Zero-based value identifying the index in the array of concurrent interfaces supported by this configuration.
|
||||
uint8_t bAlternateSetting ; ///< Value used to select this alternate setting for the interface identified in the prior field
|
||||
uint8_t bNumEndpoints ; ///< Number of endpoints used by this interface (excluding endpoint zero). If this value is zero, this interface only uses the Default Control Pipe.
|
||||
uint8_t bInterfaceClass ; ///< Class code (assigned by the USB-IF). \li A value of zero is reserved for future standardization. \li If this field is set to FFH, the interface class is vendor-specific. \li All other values are reserved for assignment by the USB-IF.
|
||||
uint8_t bInterfaceSubClass ; ///< Subclass code (assigned by the USB-IF). \n These codes are qualified by the value of the bInterfaceClass field. \li If the bInterfaceClass field is reset to zero, this field must also be reset to zero. \li If the bInterfaceClass field is not set to FFH, all values are reserved for assignment by the USB-IF.
|
||||
uint8_t bInterfaceProtocol ; ///< Protocol code (assigned by the USB). \n These codes are qualified by the value of the bInterfaceClass and the bInterfaceSubClass fields. If an interface supports class-specific requests, this code identifies the protocols that the device uses as defined by the specification of the device class. \li If this field is reset to zero, the device does not use a class-specific protocol on this interface. \li If this field is set to FFH, the device uses a vendor-specific protocol for this interface.
|
||||
uint8_t iInterface ; ///< Index of string descriptor describing this interface
|
||||
} tusb_desc_interface_t;
|
||||
|
||||
TU_VERIFY_STATIC( sizeof(tusb_desc_interface_t) == 9, "size is not correct");
|
||||
|
||||
/// USB Endpoint Descriptor
|
||||
typedef struct TU_ATTR_PACKED
|
||||
{
|
||||
uint8_t bLength ; // Size of this descriptor in bytes
|
||||
uint8_t bDescriptorType ; // ENDPOINT Descriptor Type
|
||||
|
||||
uint8_t bEndpointAddress ; // The address of the endpoint
|
||||
|
||||
struct TU_ATTR_PACKED {
|
||||
uint8_t xfer : 2; // Control, ISO, Bulk, Interrupt
|
||||
uint8_t sync : 2; // None, Asynchronous, Adaptive, Synchronous
|
||||
uint8_t usage : 2; // Data, Feedback, Implicit feedback
|
||||
uint8_t : 2;
|
||||
} bmAttributes;
|
||||
|
||||
uint16_t wMaxPacketSize ; // Bit 10..0 : max packet size, bit 12..11 additional transaction per highspeed micro-frame
|
||||
uint8_t bInterval ; // Polling interval, in frames or microframes depending on the operating speed
|
||||
} tusb_desc_endpoint_t;
|
||||
|
||||
TU_VERIFY_STATIC( sizeof(tusb_desc_endpoint_t) == 7, "size is not correct");
|
||||
|
||||
/// USB Other Speed Configuration Descriptor
|
||||
typedef struct TU_ATTR_PACKED
|
||||
{
|
||||
uint8_t bLength ; ///< Size of descriptor
|
||||
uint8_t bDescriptorType ; ///< Other_speed_Configuration Type
|
||||
uint16_t wTotalLength ; ///< Total length of data returned
|
||||
|
||||
uint8_t bNumInterfaces ; ///< Number of interfaces supported by this speed configuration
|
||||
uint8_t bConfigurationValue ; ///< Value to use to select configuration
|
||||
uint8_t iConfiguration ; ///< Index of string descriptor
|
||||
uint8_t bmAttributes ; ///< Same as Configuration descriptor
|
||||
uint8_t bMaxPower ; ///< Same as Configuration descriptor
|
||||
} tusb_desc_other_speed_t;
|
||||
|
||||
/// USB Device Qualifier Descriptor
|
||||
typedef struct TU_ATTR_PACKED
|
||||
{
|
||||
uint8_t bLength ; ///< Size of descriptor
|
||||
uint8_t bDescriptorType ; ///< Device Qualifier Type
|
||||
uint16_t bcdUSB ; ///< USB specification version number (e.g., 0200H for V2.00)
|
||||
|
||||
uint8_t bDeviceClass ; ///< Class Code
|
||||
uint8_t bDeviceSubClass ; ///< SubClass Code
|
||||
uint8_t bDeviceProtocol ; ///< Protocol Code
|
||||
|
||||
uint8_t bMaxPacketSize0 ; ///< Maximum packet size for other speed
|
||||
uint8_t bNumConfigurations ; ///< Number of Other-speed Configurations
|
||||
uint8_t bReserved ; ///< Reserved for future use, must be zero
|
||||
} tusb_desc_device_qualifier_t;
|
||||
|
||||
TU_VERIFY_STATIC( sizeof(tusb_desc_device_qualifier_t) == 10, "size is not correct");
|
||||
|
||||
/// USB Interface Association Descriptor (IAD ECN)
|
||||
typedef struct TU_ATTR_PACKED
|
||||
{
|
||||
uint8_t bLength ; ///< Size of descriptor
|
||||
uint8_t bDescriptorType ; ///< Other_speed_Configuration Type
|
||||
|
||||
uint8_t bFirstInterface ; ///< Index of the first associated interface.
|
||||
uint8_t bInterfaceCount ; ///< Total number of associated interfaces.
|
||||
|
||||
uint8_t bFunctionClass ; ///< Interface class ID.
|
||||
uint8_t bFunctionSubClass ; ///< Interface subclass ID.
|
||||
uint8_t bFunctionProtocol ; ///< Interface protocol ID.
|
||||
|
||||
uint8_t iFunction ; ///< Index of the string descriptor describing the interface association.
|
||||
} tusb_desc_interface_assoc_t;
|
||||
|
||||
// USB String Descriptor
|
||||
typedef struct TU_ATTR_PACKED
|
||||
{
|
||||
uint8_t bLength ; ///< Size of this descriptor in bytes
|
||||
uint8_t bDescriptorType ; ///< Descriptor Type
|
||||
uint16_t unicode_string[];
|
||||
} tusb_desc_string_t;
|
||||
|
||||
// USB Binary Device Object Store (BOS)
|
||||
typedef struct TU_ATTR_PACKED
|
||||
{
|
||||
uint8_t bLength;
|
||||
uint8_t bDescriptorType ;
|
||||
uint8_t bDevCapabilityType;
|
||||
uint8_t bReserved;
|
||||
uint8_t PlatformCapabilityUUID[16];
|
||||
uint8_t CapabilityData[];
|
||||
} tusb_desc_bos_platform_t;
|
||||
|
||||
// USB WebuSB URL Descriptor
|
||||
typedef struct TU_ATTR_PACKED
|
||||
{
|
||||
uint8_t bLength;
|
||||
uint8_t bDescriptorType;
|
||||
uint8_t bScheme;
|
||||
char url[];
|
||||
} tusb_desc_webusb_url_t;
|
||||
|
||||
// DFU Functional Descriptor
|
||||
typedef struct TU_ATTR_PACKED
|
||||
{
|
||||
uint8_t bLength;
|
||||
uint8_t bDescriptorType;
|
||||
|
||||
union {
|
||||
struct TU_ATTR_PACKED {
|
||||
uint8_t bitCanDnload : 1;
|
||||
uint8_t bitCanUpload : 1;
|
||||
uint8_t bitManifestationTolerant : 1;
|
||||
uint8_t bitWillDetach : 1;
|
||||
uint8_t reserved : 4;
|
||||
} bmAttributes;
|
||||
|
||||
uint8_t bAttributes;
|
||||
};
|
||||
|
||||
uint16_t wDetachTimeOut;
|
||||
uint16_t wTransferSize;
|
||||
uint16_t bcdDFUVersion;
|
||||
} tusb_desc_dfu_functional_t;
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
//
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
typedef struct TU_ATTR_PACKED{
|
||||
union {
|
||||
struct TU_ATTR_PACKED {
|
||||
uint8_t recipient : 5; ///< Recipient type tusb_request_recipient_t.
|
||||
uint8_t type : 2; ///< Request type tusb_request_type_t.
|
||||
uint8_t direction : 1; ///< Direction type. tusb_dir_t
|
||||
} bmRequestType_bit;
|
||||
|
||||
uint8_t bmRequestType;
|
||||
};
|
||||
|
||||
uint8_t bRequest;
|
||||
uint16_t wValue;
|
||||
uint16_t wIndex;
|
||||
uint16_t wLength;
|
||||
} tusb_control_request_t;
|
||||
|
||||
TU_VERIFY_STATIC( sizeof(tusb_control_request_t) == 8, "size is not correct");
|
||||
|
||||
|
||||
TU_ATTR_PACKED_END // End of all packed definitions
|
||||
TU_ATTR_BIT_FIELD_ORDER_END
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Endpoint helper
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Get direction from Endpoint address
|
||||
TU_ATTR_ALWAYS_INLINE static inline tusb_dir_t tu_edpt_dir(uint8_t addr)
|
||||
{
|
||||
return (addr & TUSB_DIR_IN_MASK) ? TUSB_DIR_IN : TUSB_DIR_OUT;
|
||||
}
|
||||
|
||||
// Get Endpoint number from address
|
||||
TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_edpt_number(uint8_t addr)
|
||||
{
|
||||
return (uint8_t)(addr & (~TUSB_DIR_IN_MASK));
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_edpt_addr(uint8_t num, uint8_t dir)
|
||||
{
|
||||
return (uint8_t)(num | (dir ? TUSB_DIR_IN_MASK : 0));
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline uint16_t tu_edpt_packet_size(tusb_desc_endpoint_t const* desc_ep)
|
||||
{
|
||||
return tu_le16toh(desc_ep->wMaxPacketSize) & TU_GENMASK(10, 0);
|
||||
}
|
||||
|
||||
#if CFG_TUSB_DEBUG
|
||||
TU_ATTR_ALWAYS_INLINE static inline const char *tu_edpt_dir_str(tusb_dir_t dir)
|
||||
{
|
||||
tu_static const char *str[] = {"out", "in"};
|
||||
return str[dir];
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline const char *tu_edpt_type_str(tusb_xfer_type_t t)
|
||||
{
|
||||
tu_static const char *str[] = {"control", "isochronous", "bulk", "interrupt"};
|
||||
return str[t];
|
||||
}
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Descriptor helper
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// return next descriptor
|
||||
TU_ATTR_ALWAYS_INLINE static inline uint8_t const * tu_desc_next(void const* desc)
|
||||
{
|
||||
uint8_t const* desc8 = (uint8_t const*) desc;
|
||||
return desc8 + desc8[DESC_OFFSET_LEN];
|
||||
}
|
||||
|
||||
// get descriptor type
|
||||
TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_desc_type(void const* desc)
|
||||
{
|
||||
return ((uint8_t const*) desc)[DESC_OFFSET_TYPE];
|
||||
}
|
||||
|
||||
// get descriptor length
|
||||
TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_desc_len(void const* desc)
|
||||
{
|
||||
return ((uint8_t const*) desc)[DESC_OFFSET_LEN];
|
||||
}
|
||||
|
||||
// find descriptor that match byte1 (type)
|
||||
uint8_t const * tu_desc_find(uint8_t const* desc, uint8_t const* end, uint8_t byte1);
|
||||
|
||||
// find descriptor that match byte1 (type) and byte2
|
||||
uint8_t const * tu_desc_find2(uint8_t const* desc, uint8_t const* end, uint8_t byte1, uint8_t byte2);
|
||||
|
||||
// find descriptor that match byte1 (type) and byte2
|
||||
uint8_t const * tu_desc_find3(uint8_t const* desc, uint8_t const* end, uint8_t byte1, uint8_t byte2, uint8_t byte3);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _TUSB_TYPES_H_ */
|
||||
|
||||
/** @} */
|
|
@ -0,0 +1,136 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
#ifndef TUSB_VERIFY_H_
|
||||
#define TUSB_VERIFY_H_
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include "tusb_option.h"
|
||||
#include "tusb_compiler.h"
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* This file use an advanced macro technique to mimic the default parameter
|
||||
* as C++ for the sake of code simplicity. Beware of a headache macro
|
||||
* manipulation that you are told to stay away.
|
||||
*
|
||||
* This contains macros for both VERIFY and ASSERT:
|
||||
*
|
||||
* VERIFY: Used when there is an error condition which is not the
|
||||
* fault of the MCU. For example, bounds checking on data
|
||||
* sent to the micro over USB should use this function.
|
||||
* Another example is checking for buffer overflows, where
|
||||
* returning from the active function causes a NAK.
|
||||
*
|
||||
* ASSERT: Used for error conditions that are caused by MCU firmware
|
||||
* bugs. This is used to discover bugs in the code more
|
||||
* quickly. One example would be adding assertions in library
|
||||
* function calls to confirm a function's (untainted)
|
||||
* parameters are valid.
|
||||
*
|
||||
* The difference in behavior is that ASSERT triggers a breakpoint while
|
||||
* verify does not.
|
||||
*
|
||||
* #define TU_VERIFY(cond) if(cond) return false;
|
||||
* #define TU_VERIFY(cond,ret) if(cond) return ret;
|
||||
*
|
||||
* #define TU_ASSERT(cond) if(cond) {_MESS_FAILED(); TU_BREAKPOINT(), return false;}
|
||||
* #define TU_ASSERT(cond,ret) if(cond) {_MESS_FAILED(); TU_BREAKPOINT(), return ret;}
|
||||
*------------------------------------------------------------------*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// TU_VERIFY Helper
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
#if CFG_TUSB_DEBUG
|
||||
#include <stdio.h>
|
||||
#define _MESS_FAILED() tu_printf("%s %d: ASSERT FAILED\r\n", __func__, __LINE__)
|
||||
#else
|
||||
#define _MESS_FAILED() do {} while (0)
|
||||
#endif
|
||||
|
||||
// Halt CPU (breakpoint) when hitting error, only apply for Cortex M3, M4, M7, M33. M55
|
||||
#if defined(__ARM_ARCH_7M__) || defined (__ARM_ARCH_7EM__) || defined(__ARM_ARCH_8M_MAIN__) || defined(__ARM_ARCH_8_1M_MAIN__)
|
||||
#define TU_BREAKPOINT() do \
|
||||
{ \
|
||||
volatile uint32_t* ARM_CM_DHCSR = ((volatile uint32_t*) 0xE000EDF0UL); /* Cortex M CoreDebug->DHCSR */ \
|
||||
if ( (*ARM_CM_DHCSR) & 1UL ) __asm("BKPT #0\n"); /* Only halt mcu if debugger is attached */ \
|
||||
} while(0)
|
||||
|
||||
#elif defined(__riscv)
|
||||
#define TU_BREAKPOINT() do { __asm("ebreak\n"); } while(0)
|
||||
|
||||
#elif defined(_mips)
|
||||
#define TU_BREAKPOINT() do { __asm("sdbbp 0"); } while (0)
|
||||
|
||||
#else
|
||||
#define TU_BREAKPOINT() do {} while (0)
|
||||
#endif
|
||||
|
||||
// Helper to implement optional parameter for TU_VERIFY Macro family
|
||||
#define _GET_3RD_ARG(arg1, arg2, arg3, ...) arg3
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* TU_VERIFY
|
||||
* - TU_VERIFY_1ARGS : return false if failed
|
||||
* - TU_VERIFY_2ARGS : return provided value if failed
|
||||
*------------------------------------------------------------------*/
|
||||
#define TU_VERIFY_DEFINE(_cond, _ret) \
|
||||
do { \
|
||||
if ( !(_cond) ) { return _ret; } \
|
||||
} while(0)
|
||||
|
||||
#define TU_VERIFY_1ARGS(_cond) TU_VERIFY_DEFINE(_cond, false)
|
||||
#define TU_VERIFY_2ARGS(_cond, _ret) TU_VERIFY_DEFINE(_cond, _ret)
|
||||
|
||||
#define TU_VERIFY(...) _GET_3RD_ARG(__VA_ARGS__, TU_VERIFY_2ARGS, TU_VERIFY_1ARGS, _dummy)(__VA_ARGS__)
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* ASSERT
|
||||
* basically TU_VERIFY with TU_BREAKPOINT() as handler
|
||||
* - 1 arg : return false if failed
|
||||
* - 2 arg : return error if failed
|
||||
*------------------------------------------------------------------*/
|
||||
#define TU_ASSERT_DEFINE(_cond, _ret) \
|
||||
do { \
|
||||
if ( !(_cond) ) { _MESS_FAILED(); TU_BREAKPOINT(); return _ret; } \
|
||||
} while(0)
|
||||
|
||||
#define TU_ASSERT_1ARGS(_cond) TU_ASSERT_DEFINE(_cond, false)
|
||||
#define TU_ASSERT_2ARGS(_cond, _ret) TU_ASSERT_DEFINE(_cond, _ret)
|
||||
|
||||
#ifndef TU_ASSERT
|
||||
#define TU_ASSERT(...) _GET_3RD_ARG(__VA_ARGS__, TU_ASSERT_2ARGS, TU_ASSERT_1ARGS, _dummy)(__VA_ARGS__)
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -0,0 +1,239 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#ifndef _TUSB_DCD_H_
|
||||
#define _TUSB_DCD_H_
|
||||
|
||||
#include "common/tusb_common.h"
|
||||
#include "osal/osal.h"
|
||||
#include "common/tusb_fifo.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Configuration
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
#ifndef CFG_TUD_ENDPPOINT_MAX
|
||||
#define CFG_TUD_ENDPPOINT_MAX TUP_DCD_ENDPOINT_MAX
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// MACRO CONSTANT TYPEDEF PROTYPES
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
typedef enum {
|
||||
DCD_EVENT_INVALID = 0,
|
||||
DCD_EVENT_BUS_RESET,
|
||||
DCD_EVENT_UNPLUGGED,
|
||||
DCD_EVENT_SOF,
|
||||
DCD_EVENT_SUSPEND, // TODO LPM Sleep L1 support
|
||||
DCD_EVENT_RESUME,
|
||||
|
||||
DCD_EVENT_SETUP_RECEIVED,
|
||||
DCD_EVENT_XFER_COMPLETE,
|
||||
|
||||
// Not an DCD event, just a convenient way to defer ISR function
|
||||
USBD_EVENT_FUNC_CALL,
|
||||
|
||||
DCD_EVENT_COUNT
|
||||
} dcd_eventid_t;
|
||||
|
||||
typedef struct TU_ATTR_ALIGNED(4) {
|
||||
uint8_t rhport;
|
||||
uint8_t event_id;
|
||||
|
||||
union {
|
||||
// BUS RESET
|
||||
struct {
|
||||
tusb_speed_t speed;
|
||||
} bus_reset;
|
||||
|
||||
// SOF
|
||||
struct {
|
||||
uint32_t frame_count;
|
||||
}sof;
|
||||
|
||||
// SETUP_RECEIVED
|
||||
tusb_control_request_t setup_received;
|
||||
|
||||
// XFER_COMPLETE
|
||||
struct {
|
||||
uint8_t ep_addr;
|
||||
uint8_t result;
|
||||
uint32_t len;
|
||||
}xfer_complete;
|
||||
|
||||
// FUNC_CALL
|
||||
struct {
|
||||
void (*func) (void*);
|
||||
void* param;
|
||||
}func_call;
|
||||
};
|
||||
} dcd_event_t;
|
||||
|
||||
//TU_VERIFY_STATIC(sizeof(dcd_event_t) <= 12, "size is not correct");
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Memory API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// clean/flush data cache: write cache -> memory.
|
||||
// Required before an DMA TX transfer to make sure data is in memory
|
||||
void dcd_dcache_clean(void const* addr, uint32_t data_size) TU_ATTR_WEAK;
|
||||
|
||||
// invalidate data cache: mark cache as invalid, next read will read from memory
|
||||
// Required BOTH before and after an DMA RX transfer
|
||||
void dcd_dcache_invalidate(void const* addr, uint32_t data_size) TU_ATTR_WEAK;
|
||||
|
||||
// clean and invalidate data cache
|
||||
// Required before an DMA transfer where memory is both read/write by DMA
|
||||
void dcd_dcache_clean_invalidate(void const* addr, uint32_t data_size) TU_ATTR_WEAK;
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Controller API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Initialize controller to device mode
|
||||
void dcd_init(uint8_t rhport);
|
||||
|
||||
// Interrupt Handler
|
||||
void dcd_int_handler(uint8_t rhport);
|
||||
|
||||
// Enable device interrupt
|
||||
void dcd_int_enable (uint8_t rhport);
|
||||
|
||||
// Disable device interrupt
|
||||
void dcd_int_disable(uint8_t rhport);
|
||||
|
||||
// Receive Set Address request, mcu port must also include status IN response
|
||||
void dcd_set_address(uint8_t rhport, uint8_t dev_addr);
|
||||
|
||||
// Wake up host
|
||||
void dcd_remote_wakeup(uint8_t rhport);
|
||||
|
||||
// Connect by enabling internal pull-up resistor on D+/D-
|
||||
void dcd_connect(uint8_t rhport) TU_ATTR_WEAK;
|
||||
|
||||
// Disconnect by disabling internal pull-up resistor on D+/D-
|
||||
void dcd_disconnect(uint8_t rhport) TU_ATTR_WEAK;
|
||||
|
||||
// Enable/Disable Start-of-frame interrupt. Default is disabled
|
||||
void dcd_sof_enable(uint8_t rhport, bool en);
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Endpoint API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Invoked when a control transfer's status stage is complete.
|
||||
// May help DCD to prepare for next control transfer, this API is optional.
|
||||
void dcd_edpt0_status_complete(uint8_t rhport, tusb_control_request_t const * request);
|
||||
|
||||
// Configure endpoint's registers according to descriptor
|
||||
bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * desc_ep);
|
||||
|
||||
// Close all non-control endpoints, cancel all pending transfers if any.
|
||||
// Invoked when switching from a non-zero Configuration by SET_CONFIGURE therefore
|
||||
// required for multiple configuration support.
|
||||
void dcd_edpt_close_all (uint8_t rhport);
|
||||
|
||||
// Close an endpoint.
|
||||
// Since it is weak, caller must TU_ASSERT this function's existence before calling it.
|
||||
void dcd_edpt_close (uint8_t rhport, uint8_t ep_addr) TU_ATTR_WEAK;
|
||||
|
||||
// Submit a transfer, When complete dcd_event_xfer_complete() is invoked to notify the stack
|
||||
bool dcd_edpt_xfer (uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes);
|
||||
|
||||
// Submit an transfer using fifo, When complete dcd_event_xfer_complete() is invoked to notify the stack
|
||||
// This API is optional, may be useful for register-based for transferring data.
|
||||
bool dcd_edpt_xfer_fifo (uint8_t rhport, uint8_t ep_addr, tu_fifo_t * ff, uint16_t total_bytes) TU_ATTR_WEAK;
|
||||
|
||||
// Stall endpoint, any queuing transfer should be removed from endpoint
|
||||
void dcd_edpt_stall (uint8_t rhport, uint8_t ep_addr);
|
||||
|
||||
// clear stall, data toggle is also reset to DATA0
|
||||
// This API never calls with control endpoints, since it is auto cleared when receiving setup packet
|
||||
void dcd_edpt_clear_stall (uint8_t rhport, uint8_t ep_addr);
|
||||
|
||||
// Allocate packet buffer used by ISO endpoints
|
||||
// Some MCU need manual packet buffer allocation, we allocate the largest size to avoid clustering
|
||||
TU_ATTR_WEAK bool dcd_edpt_iso_alloc(uint8_t rhport, uint8_t ep_addr, uint16_t largest_packet_size);
|
||||
|
||||
// Configure and enable an ISO endpoint according to descriptor
|
||||
TU_ATTR_WEAK bool dcd_edpt_iso_activate(uint8_t rhport, tusb_desc_endpoint_t const * p_endpoint_desc);
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Event API (implemented by stack)
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Called by DCD to notify device stack
|
||||
extern void dcd_event_handler(dcd_event_t const * event, bool in_isr);
|
||||
|
||||
// helper to send bus signal event
|
||||
TU_ATTR_ALWAYS_INLINE static inline void dcd_event_bus_signal (uint8_t rhport, dcd_eventid_t eid, bool in_isr) {
|
||||
dcd_event_t event = { .rhport = rhport, .event_id = eid };
|
||||
dcd_event_handler(&event, in_isr);
|
||||
}
|
||||
|
||||
// helper to send bus reset event
|
||||
TU_ATTR_ALWAYS_INLINE static inline void dcd_event_bus_reset (uint8_t rhport, tusb_speed_t speed, bool in_isr) {
|
||||
dcd_event_t event = { .rhport = rhport, .event_id = DCD_EVENT_BUS_RESET };
|
||||
event.bus_reset.speed = speed;
|
||||
dcd_event_handler(&event, in_isr);
|
||||
}
|
||||
|
||||
// helper to send setup received
|
||||
TU_ATTR_ALWAYS_INLINE static inline void dcd_event_setup_received(uint8_t rhport, uint8_t const * setup, bool in_isr) {
|
||||
dcd_event_t event = { .rhport = rhport, .event_id = DCD_EVENT_SETUP_RECEIVED };
|
||||
memcpy(&event.setup_received, setup, sizeof(tusb_control_request_t));
|
||||
|
||||
dcd_event_handler(&event, in_isr);
|
||||
}
|
||||
|
||||
// helper to send transfer complete event
|
||||
TU_ATTR_ALWAYS_INLINE static inline void dcd_event_xfer_complete (uint8_t rhport, uint8_t ep_addr, uint32_t xferred_bytes, uint8_t result, bool in_isr) {
|
||||
dcd_event_t event = { .rhport = rhport, .event_id = DCD_EVENT_XFER_COMPLETE };
|
||||
|
||||
event.xfer_complete.ep_addr = ep_addr;
|
||||
event.xfer_complete.len = xferred_bytes;
|
||||
event.xfer_complete.result = result;
|
||||
|
||||
dcd_event_handler(&event, in_isr);
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline void dcd_event_sof(uint8_t rhport, uint32_t frame_count, bool in_isr) {
|
||||
dcd_event_t event = { .rhport = rhport, .event_id = DCD_EVENT_SOF };
|
||||
event.sof.frame_count = frame_count;
|
||||
dcd_event_handler(&event, in_isr);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _TUSB_DCD_H_ */
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,864 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#ifndef _TUSB_USBD_H_
|
||||
#define _TUSB_USBD_H_
|
||||
|
||||
#include "common/tusb_common.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Application API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Init device stack
|
||||
bool tud_init (uint8_t rhport);
|
||||
|
||||
// Check if device stack is already initialized
|
||||
bool tud_inited(void);
|
||||
|
||||
// Task function should be called in main/rtos loop, extended version of tud_task()
|
||||
// - timeout_ms: millisecond to wait, zero = no wait, 0xFFFFFFFF = wait forever
|
||||
// - in_isr: if function is called in ISR
|
||||
void tud_task_ext(uint32_t timeout_ms, bool in_isr);
|
||||
|
||||
// Task function should be called in main/rtos loop
|
||||
TU_ATTR_ALWAYS_INLINE static inline
|
||||
void tud_task (void) {
|
||||
tud_task_ext(UINT32_MAX, false);
|
||||
}
|
||||
|
||||
// Check if there is pending events need processing by tud_task()
|
||||
bool tud_task_event_ready(void);
|
||||
|
||||
#ifndef _TUSB_DCD_H_
|
||||
extern void dcd_int_handler(uint8_t rhport);
|
||||
#endif
|
||||
|
||||
// Interrupt handler, name alias to DCD
|
||||
#define tud_int_handler dcd_int_handler
|
||||
|
||||
// Get current bus speed
|
||||
tusb_speed_t tud_speed_get(void);
|
||||
|
||||
// Check if device is connected (may not mounted/configured yet)
|
||||
// True if just got out of Bus Reset and received the very first data from host
|
||||
bool tud_connected(void);
|
||||
|
||||
// Check if device is connected and configured
|
||||
bool tud_mounted(void);
|
||||
|
||||
// Check if device is suspended
|
||||
bool tud_suspended(void);
|
||||
|
||||
// Check if device is ready to transfer
|
||||
TU_ATTR_ALWAYS_INLINE static inline
|
||||
bool tud_ready(void) {
|
||||
return tud_mounted() && !tud_suspended();
|
||||
}
|
||||
|
||||
// Remote wake up host, only if suspended and enabled by host
|
||||
bool tud_remote_wakeup(void);
|
||||
|
||||
// Enable pull-up resistor on D+ D-
|
||||
// Return false on unsupported MCUs
|
||||
bool tud_disconnect(void);
|
||||
|
||||
// Disable pull-up resistor on D+ D-
|
||||
// Return false on unsupported MCUs
|
||||
bool tud_connect(void);
|
||||
|
||||
// Carry out Data and Status stage of control transfer
|
||||
// - If len = 0, it is equivalent to sending status only
|
||||
// - If len > wLength : it will be truncated
|
||||
bool tud_control_xfer(uint8_t rhport, tusb_control_request_t const * request, void* buffer, uint16_t len);
|
||||
|
||||
// Send STATUS (zero length) packet
|
||||
bool tud_control_status(uint8_t rhport, tusb_control_request_t const * request);
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Application Callbacks (WEAK is optional)
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Invoked when received GET DEVICE DESCRIPTOR request
|
||||
// Application return pointer to descriptor
|
||||
uint8_t const * tud_descriptor_device_cb(void);
|
||||
|
||||
// Invoked when received GET CONFIGURATION DESCRIPTOR request
|
||||
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
||||
uint8_t const * tud_descriptor_configuration_cb(uint8_t index);
|
||||
|
||||
// Invoked when received GET STRING DESCRIPTOR request
|
||||
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
||||
uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid);
|
||||
|
||||
// Invoked when received GET BOS DESCRIPTOR request
|
||||
// Application return pointer to descriptor
|
||||
TU_ATTR_WEAK uint8_t const * tud_descriptor_bos_cb(void);
|
||||
|
||||
// Invoked when received GET DEVICE QUALIFIER DESCRIPTOR request
|
||||
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete.
|
||||
// device_qualifier descriptor describes information about a high-speed capable device that would
|
||||
// change if the device were operating at the other speed. If not highspeed capable stall this request.
|
||||
TU_ATTR_WEAK uint8_t const* tud_descriptor_device_qualifier_cb(void);
|
||||
|
||||
// Invoked when received GET OTHER SEED CONFIGURATION DESCRIPTOR request
|
||||
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
||||
// Configuration descriptor in the other speed e.g if high speed then this is for full speed and vice versa
|
||||
TU_ATTR_WEAK uint8_t const* tud_descriptor_other_speed_configuration_cb(uint8_t index);
|
||||
|
||||
// Invoked when device is mounted (configured)
|
||||
TU_ATTR_WEAK void tud_mount_cb(void);
|
||||
|
||||
// Invoked when device is unmounted
|
||||
TU_ATTR_WEAK void tud_umount_cb(void);
|
||||
|
||||
// Invoked when usb bus is suspended
|
||||
// Within 7ms, device must draw an average of current less than 2.5 mA from bus
|
||||
TU_ATTR_WEAK void tud_suspend_cb(bool remote_wakeup_en);
|
||||
|
||||
// Invoked when usb bus is resumed
|
||||
TU_ATTR_WEAK void tud_resume_cb(void);
|
||||
|
||||
// Invoked when there is a new usb event, which need to be processed by tud_task()/tud_task_ext()
|
||||
void tud_event_hook_cb(uint8_t rhport, uint32_t eventid, bool in_isr);
|
||||
|
||||
// Invoked when received control request with VENDOR TYPE
|
||||
TU_ATTR_WEAK bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request);
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Binary Device Object Store (BOS) Descriptor Templates
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
#define TUD_BOS_DESC_LEN 5
|
||||
|
||||
// total length, number of device caps
|
||||
#define TUD_BOS_DESCRIPTOR(_total_len, _caps_num) \
|
||||
5, TUSB_DESC_BOS, U16_TO_U8S_LE(_total_len), _caps_num
|
||||
|
||||
// Device Capability Platform 128-bit UUID + Data
|
||||
#define TUD_BOS_PLATFORM_DESCRIPTOR(...) \
|
||||
4+TU_ARGS_NUM(__VA_ARGS__), TUSB_DESC_DEVICE_CAPABILITY, DEVICE_CAPABILITY_PLATFORM, 0x00, __VA_ARGS__
|
||||
|
||||
//------------- WebUSB BOS Platform -------------//
|
||||
|
||||
// Descriptor Length
|
||||
#define TUD_BOS_WEBUSB_DESC_LEN 24
|
||||
|
||||
// Vendor Code, iLandingPage
|
||||
#define TUD_BOS_WEBUSB_DESCRIPTOR(_vendor_code, _ipage) \
|
||||
TUD_BOS_PLATFORM_DESCRIPTOR(TUD_BOS_WEBUSB_UUID, U16_TO_U8S_LE(0x0100), _vendor_code, _ipage)
|
||||
|
||||
#define TUD_BOS_WEBUSB_UUID \
|
||||
0x38, 0xB6, 0x08, 0x34, 0xA9, 0x09, 0xA0, 0x47, \
|
||||
0x8B, 0xFD, 0xA0, 0x76, 0x88, 0x15, 0xB6, 0x65
|
||||
|
||||
//------------- Microsoft OS 2.0 Platform -------------//
|
||||
#define TUD_BOS_MICROSOFT_OS_DESC_LEN 28
|
||||
|
||||
// Total Length of descriptor set, vendor code
|
||||
#define TUD_BOS_MS_OS_20_DESCRIPTOR(_desc_set_len, _vendor_code) \
|
||||
TUD_BOS_PLATFORM_DESCRIPTOR(TUD_BOS_MS_OS_20_UUID, U32_TO_U8S_LE(0x06030000), U16_TO_U8S_LE(_desc_set_len), _vendor_code, 0)
|
||||
|
||||
#define TUD_BOS_MS_OS_20_UUID \
|
||||
0xDF, 0x60, 0xDD, 0xD8, 0x89, 0x45, 0xC7, 0x4C, \
|
||||
0x9C, 0xD2, 0x65, 0x9D, 0x9E, 0x64, 0x8A, 0x9F
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Configuration Descriptor Templates
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
#define TUD_CONFIG_DESC_LEN (9)
|
||||
|
||||
// Config number, interface count, string index, total length, attribute, power in mA
|
||||
#define TUD_CONFIG_DESCRIPTOR(config_num, _itfcount, _stridx, _total_len, _attribute, _power_ma) \
|
||||
9, TUSB_DESC_CONFIGURATION, U16_TO_U8S_LE(_total_len), _itfcount, config_num, _stridx, TU_BIT(7) | _attribute, (_power_ma)/2
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// CDC Descriptor Templates
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Length of template descriptor: 66 bytes
|
||||
#define TUD_CDC_DESC_LEN (8+9+5+5+4+5+7+9+7+7)
|
||||
|
||||
// CDC Descriptor Template
|
||||
// Interface number, string index, EP notification address and size, EP data address (out, in) and size.
|
||||
#define TUD_CDC_DESCRIPTOR(_itfnum, _stridx, _ep_notif, _ep_notif_size, _epout, _epin, _epsize) \
|
||||
/* Interface Associate */\
|
||||
8, TUSB_DESC_INTERFACE_ASSOCIATION, _itfnum, 2, TUSB_CLASS_CDC, CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL, CDC_COMM_PROTOCOL_NONE, 0,\
|
||||
/* CDC Control Interface */\
|
||||
9, TUSB_DESC_INTERFACE, _itfnum, 0, 1, TUSB_CLASS_CDC, CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL, CDC_COMM_PROTOCOL_NONE, _stridx,\
|
||||
/* CDC Header */\
|
||||
5, TUSB_DESC_CS_INTERFACE, CDC_FUNC_DESC_HEADER, U16_TO_U8S_LE(0x0120),\
|
||||
/* CDC Call */\
|
||||
5, TUSB_DESC_CS_INTERFACE, CDC_FUNC_DESC_CALL_MANAGEMENT, 0, (uint8_t)((_itfnum) + 1),\
|
||||
/* CDC ACM: support line request */\
|
||||
4, TUSB_DESC_CS_INTERFACE, CDC_FUNC_DESC_ABSTRACT_CONTROL_MANAGEMENT, 2,\
|
||||
/* CDC Union */\
|
||||
5, TUSB_DESC_CS_INTERFACE, CDC_FUNC_DESC_UNION, _itfnum, (uint8_t)((_itfnum) + 1),\
|
||||
/* Endpoint Notification */\
|
||||
7, TUSB_DESC_ENDPOINT, _ep_notif, TUSB_XFER_INTERRUPT, U16_TO_U8S_LE(_ep_notif_size), 16,\
|
||||
/* CDC Data Interface */\
|
||||
9, TUSB_DESC_INTERFACE, (uint8_t)((_itfnum)+1), 0, 2, TUSB_CLASS_CDC_DATA, 0, 0, 0,\
|
||||
/* Endpoint Out */\
|
||||
7, TUSB_DESC_ENDPOINT, _epout, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0,\
|
||||
/* Endpoint In */\
|
||||
7, TUSB_DESC_ENDPOINT, _epin, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// MSC Descriptor Templates
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Length of template descriptor: 23 bytes
|
||||
#define TUD_MSC_DESC_LEN (9 + 7 + 7)
|
||||
|
||||
// Interface number, string index, EP Out & EP In address, EP size
|
||||
#define TUD_MSC_DESCRIPTOR(_itfnum, _stridx, _epout, _epin, _epsize) \
|
||||
/* Interface */\
|
||||
9, TUSB_DESC_INTERFACE, _itfnum, 0, 2, TUSB_CLASS_MSC, MSC_SUBCLASS_SCSI, MSC_PROTOCOL_BOT, _stridx,\
|
||||
/* Endpoint Out */\
|
||||
7, TUSB_DESC_ENDPOINT, _epout, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0,\
|
||||
/* Endpoint In */\
|
||||
7, TUSB_DESC_ENDPOINT, _epin, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0
|
||||
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// HID Descriptor Templates
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Length of template descriptor: 25 bytes
|
||||
#define TUD_HID_DESC_LEN (9 + 9 + 7)
|
||||
|
||||
// HID Input only descriptor
|
||||
// Interface number, string index, protocol, report descriptor len, EP In address, size & polling interval
|
||||
#define TUD_HID_DESCRIPTOR(_itfnum, _stridx, _boot_protocol, _report_desc_len, _epin, _epsize, _ep_interval) \
|
||||
/* Interface */\
|
||||
9, TUSB_DESC_INTERFACE, _itfnum, 0, 1, TUSB_CLASS_HID, (uint8_t)((_boot_protocol) ? (uint8_t)HID_SUBCLASS_BOOT : 0), _boot_protocol, _stridx,\
|
||||
/* HID descriptor */\
|
||||
9, HID_DESC_TYPE_HID, U16_TO_U8S_LE(0x0111), 0, 1, HID_DESC_TYPE_REPORT, U16_TO_U8S_LE(_report_desc_len),\
|
||||
/* Endpoint In */\
|
||||
7, TUSB_DESC_ENDPOINT, _epin, TUSB_XFER_INTERRUPT, U16_TO_U8S_LE(_epsize), _ep_interval
|
||||
|
||||
// Length of template descriptor: 32 bytes
|
||||
#define TUD_HID_INOUT_DESC_LEN (9 + 9 + 7 + 7)
|
||||
|
||||
// HID Input & Output descriptor
|
||||
// Interface number, string index, protocol, report descriptor len, EP OUT & IN address, size & polling interval
|
||||
#define TUD_HID_INOUT_DESCRIPTOR(_itfnum, _stridx, _boot_protocol, _report_desc_len, _epout, _epin, _epsize, _ep_interval) \
|
||||
/* Interface */\
|
||||
9, TUSB_DESC_INTERFACE, _itfnum, 0, 2, TUSB_CLASS_HID, (uint8_t)((_boot_protocol) ? (uint8_t)HID_SUBCLASS_BOOT : 0), _boot_protocol, _stridx,\
|
||||
/* HID descriptor */\
|
||||
9, HID_DESC_TYPE_HID, U16_TO_U8S_LE(0x0111), 0, 1, HID_DESC_TYPE_REPORT, U16_TO_U8S_LE(_report_desc_len),\
|
||||
/* Endpoint Out */\
|
||||
7, TUSB_DESC_ENDPOINT, _epout, TUSB_XFER_INTERRUPT, U16_TO_U8S_LE(_epsize), _ep_interval, \
|
||||
/* Endpoint In */\
|
||||
7, TUSB_DESC_ENDPOINT, _epin, TUSB_XFER_INTERRUPT, U16_TO_U8S_LE(_epsize), _ep_interval
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// MIDI Descriptor Templates
|
||||
// Note: MIDI v1.0 is based on Audio v1.0
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
#define TUD_MIDI_DESC_HEAD_LEN (9 + 9 + 9 + 7)
|
||||
#define TUD_MIDI_DESC_HEAD(_itfnum, _stridx, _numcables) \
|
||||
/* Audio Control (AC) Interface */\
|
||||
9, TUSB_DESC_INTERFACE, _itfnum, 0, 0, TUSB_CLASS_AUDIO, AUDIO_SUBCLASS_CONTROL, AUDIO_FUNC_PROTOCOL_CODE_UNDEF, _stridx,\
|
||||
/* AC Header */\
|
||||
9, TUSB_DESC_CS_INTERFACE, AUDIO_CS_AC_INTERFACE_HEADER, U16_TO_U8S_LE(0x0100), U16_TO_U8S_LE(0x0009), 1, (uint8_t)((_itfnum) + 1),\
|
||||
/* MIDI Streaming (MS) Interface */\
|
||||
9, TUSB_DESC_INTERFACE, (uint8_t)((_itfnum) + 1), 0, 2, TUSB_CLASS_AUDIO, AUDIO_SUBCLASS_MIDI_STREAMING, AUDIO_FUNC_PROTOCOL_CODE_UNDEF, 0,\
|
||||
/* MS Header */\
|
||||
7, TUSB_DESC_CS_INTERFACE, MIDI_CS_INTERFACE_HEADER, U16_TO_U8S_LE(0x0100), U16_TO_U8S_LE(7 + (_numcables) * TUD_MIDI_DESC_JACK_LEN + 2 * TUD_MIDI_DESC_EP_LEN(_numcables))
|
||||
|
||||
#define TUD_MIDI_JACKID_IN_EMB(_cablenum) \
|
||||
(uint8_t)(((_cablenum) - 1) * 4 + 1)
|
||||
|
||||
#define TUD_MIDI_JACKID_IN_EXT(_cablenum) \
|
||||
(uint8_t)(((_cablenum) - 1) * 4 + 2)
|
||||
|
||||
#define TUD_MIDI_JACKID_OUT_EMB(_cablenum) \
|
||||
(uint8_t)(((_cablenum) - 1) * 4 + 3)
|
||||
|
||||
#define TUD_MIDI_JACKID_OUT_EXT(_cablenum) \
|
||||
(uint8_t)(((_cablenum) - 1) * 4 + 4)
|
||||
|
||||
#define TUD_MIDI_DESC_JACK_LEN (6 + 6 + 9 + 9)
|
||||
#define TUD_MIDI_DESC_JACK_DESC(_cablenum, _stridx) \
|
||||
/* MS In Jack (Embedded) */\
|
||||
6, TUSB_DESC_CS_INTERFACE, MIDI_CS_INTERFACE_IN_JACK, MIDI_JACK_EMBEDDED, TUD_MIDI_JACKID_IN_EMB(_cablenum), _stridx,\
|
||||
/* MS In Jack (External) */\
|
||||
6, TUSB_DESC_CS_INTERFACE, MIDI_CS_INTERFACE_IN_JACK, MIDI_JACK_EXTERNAL, TUD_MIDI_JACKID_IN_EXT(_cablenum), _stridx,\
|
||||
/* MS Out Jack (Embedded), connected to In Jack External */\
|
||||
9, TUSB_DESC_CS_INTERFACE, MIDI_CS_INTERFACE_OUT_JACK, MIDI_JACK_EMBEDDED, TUD_MIDI_JACKID_OUT_EMB(_cablenum), 1, TUD_MIDI_JACKID_IN_EXT(_cablenum), 1, _stridx,\
|
||||
/* MS Out Jack (External), connected to In Jack Embedded */\
|
||||
9, TUSB_DESC_CS_INTERFACE, MIDI_CS_INTERFACE_OUT_JACK, MIDI_JACK_EXTERNAL, TUD_MIDI_JACKID_OUT_EXT(_cablenum), 1, TUD_MIDI_JACKID_IN_EMB(_cablenum), 1, _stridx
|
||||
|
||||
#define TUD_MIDI_DESC_JACK(_cablenum) TUD_MIDI_DESC_JACK_DESC(_cablenum, 0)
|
||||
|
||||
#define TUD_MIDI_DESC_EP_LEN(_numcables) (9 + 4 + (_numcables))
|
||||
#define TUD_MIDI_DESC_EP(_epout, _epsize, _numcables) \
|
||||
/* Endpoint: Note Audio v1.0's endpoint has 9 bytes instead of 7 */\
|
||||
9, TUSB_DESC_ENDPOINT, _epout, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0, 0, 0, \
|
||||
/* MS Endpoint (connected to embedded jack) */\
|
||||
(uint8_t)(4 + (_numcables)), TUSB_DESC_CS_ENDPOINT, MIDI_CS_ENDPOINT_GENERAL, _numcables
|
||||
|
||||
// Length of template descriptor (88 bytes)
|
||||
#define TUD_MIDI_DESC_LEN (TUD_MIDI_DESC_HEAD_LEN + TUD_MIDI_DESC_JACK_LEN + TUD_MIDI_DESC_EP_LEN(1) * 2)
|
||||
|
||||
// MIDI simple descriptor
|
||||
// - 1 Embedded Jack In connected to 1 External Jack Out
|
||||
// - 1 Embedded Jack out connected to 1 External Jack In
|
||||
#define TUD_MIDI_DESCRIPTOR(_itfnum, _stridx, _epout, _epin, _epsize) \
|
||||
TUD_MIDI_DESC_HEAD(_itfnum, _stridx, 1),\
|
||||
TUD_MIDI_DESC_JACK_DESC(1, 0),\
|
||||
TUD_MIDI_DESC_EP(_epout, _epsize, 1),\
|
||||
TUD_MIDI_JACKID_IN_EMB(1),\
|
||||
TUD_MIDI_DESC_EP(_epin, _epsize, 1),\
|
||||
TUD_MIDI_JACKID_OUT_EMB(1)
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Audio v2.0 Descriptor Templates
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
/* Standard Interface Association Descriptor (IAD) */
|
||||
#define TUD_AUDIO_DESC_IAD_LEN 8
|
||||
#define TUD_AUDIO_DESC_IAD(_firstitf, _nitfs, _stridx) \
|
||||
TUD_AUDIO_DESC_IAD_LEN, TUSB_DESC_INTERFACE_ASSOCIATION, _firstitf, _nitfs, TUSB_CLASS_AUDIO, AUDIO_FUNCTION_SUBCLASS_UNDEFINED, AUDIO_FUNC_PROTOCOL_CODE_V2, _stridx
|
||||
|
||||
/* Standard AC Interface Descriptor(4.7.1) */
|
||||
#define TUD_AUDIO_DESC_STD_AC_LEN 9
|
||||
#define TUD_AUDIO_DESC_STD_AC(_itfnum, _nEPs, _stridx) /* _nEPs is 0 or 1 */\
|
||||
TUD_AUDIO_DESC_STD_AC_LEN, TUSB_DESC_INTERFACE, _itfnum, /* fixed to zero */ 0x00, _nEPs, TUSB_CLASS_AUDIO, AUDIO_SUBCLASS_CONTROL, AUDIO_INT_PROTOCOL_CODE_V2, _stridx
|
||||
|
||||
/* Class-Specific AC Interface Header Descriptor(4.7.2) */
|
||||
#define TUD_AUDIO_DESC_CS_AC_LEN 9
|
||||
#define TUD_AUDIO_DESC_CS_AC(_bcdADC, _category, _totallen, _ctrl) /* _bcdADC : Audio Device Class Specification Release Number in Binary-Coded Decimal, _category : see audio_function_t, _totallen : Total number of bytes returned for the class-specific AudioControl interface i.e. Clock Source, Unit and Terminal descriptors - Do not include TUD_AUDIO_DESC_CS_AC_LEN, we already do this here*/ \
|
||||
TUD_AUDIO_DESC_CS_AC_LEN, TUSB_DESC_CS_INTERFACE, AUDIO_CS_AC_INTERFACE_HEADER, U16_TO_U8S_LE(_bcdADC), _category, U16_TO_U8S_LE(_totallen + TUD_AUDIO_DESC_CS_AC_LEN), _ctrl
|
||||
|
||||
/* Clock Source Descriptor(4.7.2.1) */
|
||||
#define TUD_AUDIO_DESC_CLK_SRC_LEN 8
|
||||
#define TUD_AUDIO_DESC_CLK_SRC(_clkid, _attr, _ctrl, _assocTerm, _stridx) \
|
||||
TUD_AUDIO_DESC_CLK_SRC_LEN, TUSB_DESC_CS_INTERFACE, AUDIO_CS_AC_INTERFACE_CLOCK_SOURCE, _clkid, _attr, _ctrl, _assocTerm, _stridx
|
||||
|
||||
/* Input Terminal Descriptor(4.7.2.4) */
|
||||
#define TUD_AUDIO_DESC_INPUT_TERM_LEN 17
|
||||
#define TUD_AUDIO_DESC_INPUT_TERM(_termid, _termtype, _assocTerm, _clkid, _nchannelslogical, _channelcfg, _idxchannelnames, _ctrl, _stridx) \
|
||||
TUD_AUDIO_DESC_INPUT_TERM_LEN, TUSB_DESC_CS_INTERFACE, AUDIO_CS_AC_INTERFACE_INPUT_TERMINAL, _termid, U16_TO_U8S_LE(_termtype), _assocTerm, _clkid, _nchannelslogical, U32_TO_U8S_LE(_channelcfg), _idxchannelnames, U16_TO_U8S_LE(_ctrl), _stridx
|
||||
|
||||
/* Output Terminal Descriptor(4.7.2.5) */
|
||||
#define TUD_AUDIO_DESC_OUTPUT_TERM_LEN 12
|
||||
#define TUD_AUDIO_DESC_OUTPUT_TERM(_termid, _termtype, _assocTerm, _srcid, _clkid, _ctrl, _stridx) \
|
||||
TUD_AUDIO_DESC_OUTPUT_TERM_LEN, TUSB_DESC_CS_INTERFACE, AUDIO_CS_AC_INTERFACE_OUTPUT_TERMINAL, _termid, U16_TO_U8S_LE(_termtype), _assocTerm, _srcid, _clkid, U16_TO_U8S_LE(_ctrl), _stridx
|
||||
|
||||
/* Feature Unit Descriptor(4.7.2.8) */
|
||||
// 1 - Channel
|
||||
#define TUD_AUDIO_DESC_FEATURE_UNIT_ONE_CHANNEL_LEN 6+(1+1)*4
|
||||
#define TUD_AUDIO_DESC_FEATURE_UNIT_ONE_CHANNEL(_unitid, _srcid, _ctrlch0master, _ctrlch1, _stridx) \
|
||||
TUD_AUDIO_DESC_FEATURE_UNIT_ONE_CHANNEL_LEN, TUSB_DESC_CS_INTERFACE, AUDIO_CS_AC_INTERFACE_FEATURE_UNIT, _unitid, _srcid, U32_TO_U8S_LE(_ctrlch0master), U32_TO_U8S_LE(_ctrlch1), _stridx
|
||||
|
||||
// 2 - Channels
|
||||
#define TUD_AUDIO_DESC_FEATURE_UNIT_TWO_CHANNEL_LEN (6+(2+1)*4)
|
||||
#define TUD_AUDIO_DESC_FEATURE_UNIT_TWO_CHANNEL(_unitid, _srcid, _ctrlch0master, _ctrlch1, _ctrlch2, _stridx) \
|
||||
TUD_AUDIO_DESC_FEATURE_UNIT_TWO_CHANNEL_LEN, TUSB_DESC_CS_INTERFACE, AUDIO_CS_AC_INTERFACE_FEATURE_UNIT, _unitid, _srcid, U32_TO_U8S_LE(_ctrlch0master), U32_TO_U8S_LE(_ctrlch1), U32_TO_U8S_LE(_ctrlch2), _stridx
|
||||
// 4 - Channels
|
||||
#define TUD_AUDIO_DESC_FEATURE_UNIT_FOUR_CHANNEL_LEN (6+(4+1)*4)
|
||||
#define TUD_AUDIO_DESC_FEATURE_UNIT_FOUR_CHANNEL(_unitid, _srcid, _ctrlch0master, _ctrlch1, _ctrlch2, _ctrlch3, _ctrlch4, _stridx) \
|
||||
TUD_AUDIO_DESC_FEATURE_UNIT_FOUR_CHANNEL_LEN, TUSB_DESC_CS_INTERFACE, AUDIO_CS_AC_INTERFACE_FEATURE_UNIT, _unitid, _srcid, U32_TO_U8S_LE(_ctrlch0master), U32_TO_U8S_LE(_ctrlch1), U32_TO_U8S_LE(_ctrlch2), U32_TO_U8S_LE(_ctrlch3), U32_TO_U8S_LE(_ctrlch4), _stridx
|
||||
|
||||
// For more channels, add definitions here
|
||||
|
||||
/* Standard AS Interface Descriptor(4.9.1) */
|
||||
#define TUD_AUDIO_DESC_STD_AS_INT_LEN 9
|
||||
#define TUD_AUDIO_DESC_STD_AS_INT(_itfnum, _altset, _nEPs, _stridx) \
|
||||
TUD_AUDIO_DESC_STD_AS_INT_LEN, TUSB_DESC_INTERFACE, _itfnum, _altset, _nEPs, TUSB_CLASS_AUDIO, AUDIO_SUBCLASS_STREAMING, AUDIO_INT_PROTOCOL_CODE_V2, _stridx
|
||||
|
||||
/* Class-Specific AS Interface Descriptor(4.9.2) */
|
||||
#define TUD_AUDIO_DESC_CS_AS_INT_LEN 16
|
||||
#define TUD_AUDIO_DESC_CS_AS_INT(_termid, _ctrl, _formattype, _formats, _nchannelsphysical, _channelcfg, _stridx) \
|
||||
TUD_AUDIO_DESC_CS_AS_INT_LEN, TUSB_DESC_CS_INTERFACE, AUDIO_CS_AS_INTERFACE_AS_GENERAL, _termid, _ctrl, _formattype, U32_TO_U8S_LE(_formats), _nchannelsphysical, U32_TO_U8S_LE(_channelcfg), _stridx
|
||||
|
||||
/* Type I Format Type Descriptor(2.3.1.6 - Audio Formats) */
|
||||
#define TUD_AUDIO_DESC_TYPE_I_FORMAT_LEN 6
|
||||
#define TUD_AUDIO_DESC_TYPE_I_FORMAT(_subslotsize, _bitresolution) /* _subslotsize is number of bytes per sample (i.e. subslot) and can be 1,2,3, or 4 */\
|
||||
TUD_AUDIO_DESC_TYPE_I_FORMAT_LEN, TUSB_DESC_CS_INTERFACE, AUDIO_CS_AS_INTERFACE_FORMAT_TYPE, AUDIO_FORMAT_TYPE_I, _subslotsize, _bitresolution
|
||||
|
||||
/* Standard AS Isochronous Audio Data Endpoint Descriptor(4.10.1.1) */
|
||||
#define TUD_AUDIO_DESC_STD_AS_ISO_EP_LEN 7
|
||||
#define TUD_AUDIO_DESC_STD_AS_ISO_EP(_ep, _attr, _maxEPsize, _interval) \
|
||||
TUD_AUDIO_DESC_STD_AS_ISO_EP_LEN, TUSB_DESC_ENDPOINT, _ep, _attr, U16_TO_U8S_LE(_maxEPsize), _interval
|
||||
|
||||
/* Class-Specific AS Isochronous Audio Data Endpoint Descriptor(4.10.1.2) */
|
||||
#define TUD_AUDIO_DESC_CS_AS_ISO_EP_LEN 8
|
||||
#define TUD_AUDIO_DESC_CS_AS_ISO_EP(_attr, _ctrl, _lockdelayunit, _lockdelay) \
|
||||
TUD_AUDIO_DESC_CS_AS_ISO_EP_LEN, TUSB_DESC_CS_ENDPOINT, AUDIO_CS_EP_SUBTYPE_GENERAL, _attr, _ctrl, _lockdelayunit, U16_TO_U8S_LE(_lockdelay)
|
||||
|
||||
/* Standard AS Isochronous Feedback Endpoint Descriptor(4.10.2.1) */
|
||||
#define TUD_AUDIO_DESC_STD_AS_ISO_FB_EP_LEN 7
|
||||
#define TUD_AUDIO_DESC_STD_AS_ISO_FB_EP(_ep, _interval) \
|
||||
TUD_AUDIO_DESC_STD_AS_ISO_FB_EP_LEN, TUSB_DESC_ENDPOINT, _ep, (uint8_t) (TUSB_XFER_ISOCHRONOUS | TUSB_ISO_EP_ATT_NO_SYNC | TUSB_ISO_EP_ATT_EXPLICIT_FB), U16_TO_U8S_LE(4), _interval
|
||||
|
||||
// AUDIO simple descriptor (UAC2) for 1 microphone input
|
||||
// - 1 Input Terminal, 1 Feature Unit (Mute and Volume Control), 1 Output Terminal, 1 Clock Source
|
||||
|
||||
#define TUD_AUDIO_MIC_ONE_CH_DESC_LEN (TUD_AUDIO_DESC_IAD_LEN\
|
||||
+ TUD_AUDIO_DESC_STD_AC_LEN\
|
||||
+ TUD_AUDIO_DESC_CS_AC_LEN\
|
||||
+ TUD_AUDIO_DESC_CLK_SRC_LEN\
|
||||
+ TUD_AUDIO_DESC_INPUT_TERM_LEN\
|
||||
+ TUD_AUDIO_DESC_OUTPUT_TERM_LEN\
|
||||
+ TUD_AUDIO_DESC_FEATURE_UNIT_ONE_CHANNEL_LEN\
|
||||
+ TUD_AUDIO_DESC_STD_AS_INT_LEN\
|
||||
+ TUD_AUDIO_DESC_STD_AS_INT_LEN\
|
||||
+ TUD_AUDIO_DESC_CS_AS_INT_LEN\
|
||||
+ TUD_AUDIO_DESC_TYPE_I_FORMAT_LEN\
|
||||
+ TUD_AUDIO_DESC_STD_AS_ISO_EP_LEN\
|
||||
+ TUD_AUDIO_DESC_CS_AS_ISO_EP_LEN)
|
||||
|
||||
#define TUD_AUDIO_MIC_ONE_CH_DESC_N_AS_INT 1 // Number of AS interfaces
|
||||
|
||||
#define TUD_AUDIO_MIC_ONE_CH_DESCRIPTOR(_itfnum, _stridx, _nBytesPerSample, _nBitsUsedPerSample, _epin, _epsize) \
|
||||
/* Standard Interface Association Descriptor (IAD) */\
|
||||
TUD_AUDIO_DESC_IAD(/*_firstitf*/ _itfnum, /*_nitfs*/ 0x02, /*_stridx*/ 0x00),\
|
||||
/* Standard AC Interface Descriptor(4.7.1) */\
|
||||
TUD_AUDIO_DESC_STD_AC(/*_itfnum*/ _itfnum, /*_nEPs*/ 0x00, /*_stridx*/ _stridx),\
|
||||
/* Class-Specific AC Interface Header Descriptor(4.7.2) */\
|
||||
TUD_AUDIO_DESC_CS_AC(/*_bcdADC*/ 0x0200, /*_category*/ AUDIO_FUNC_MICROPHONE, /*_totallen*/ TUD_AUDIO_DESC_CLK_SRC_LEN+TUD_AUDIO_DESC_INPUT_TERM_LEN+TUD_AUDIO_DESC_OUTPUT_TERM_LEN+TUD_AUDIO_DESC_FEATURE_UNIT_ONE_CHANNEL_LEN, /*_ctrl*/ AUDIO_CS_AS_INTERFACE_CTRL_LATENCY_POS),\
|
||||
/* Clock Source Descriptor(4.7.2.1) */\
|
||||
TUD_AUDIO_DESC_CLK_SRC(/*_clkid*/ 0x04, /*_attr*/ AUDIO_CLOCK_SOURCE_ATT_INT_FIX_CLK, /*_ctrl*/ (AUDIO_CTRL_R << AUDIO_CLOCK_SOURCE_CTRL_CLK_FRQ_POS), /*_assocTerm*/ 0x01, /*_stridx*/ 0x00),\
|
||||
/* Input Terminal Descriptor(4.7.2.4) */\
|
||||
TUD_AUDIO_DESC_INPUT_TERM(/*_termid*/ 0x01, /*_termtype*/ AUDIO_TERM_TYPE_IN_GENERIC_MIC, /*_assocTerm*/ 0x03, /*_clkid*/ 0x04, /*_nchannelslogical*/ 0x01, /*_channelcfg*/ AUDIO_CHANNEL_CONFIG_NON_PREDEFINED, /*_idxchannelnames*/ 0x00, /*_ctrl*/ AUDIO_CTRL_R << AUDIO_IN_TERM_CTRL_CONNECTOR_POS, /*_stridx*/ 0x00),\
|
||||
/* Output Terminal Descriptor(4.7.2.5) */\
|
||||
TUD_AUDIO_DESC_OUTPUT_TERM(/*_termid*/ 0x03, /*_termtype*/ AUDIO_TERM_TYPE_USB_STREAMING, /*_assocTerm*/ 0x01, /*_srcid*/ 0x02, /*_clkid*/ 0x04, /*_ctrl*/ 0x0000, /*_stridx*/ 0x00),\
|
||||
/* Feature Unit Descriptor(4.7.2.8) */\
|
||||
TUD_AUDIO_DESC_FEATURE_UNIT_ONE_CHANNEL(/*_unitid*/ 0x02, /*_srcid*/ 0x01, /*_ctrlch0master*/ AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_MUTE_POS | AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_VOLUME_POS, /*_ctrlch1*/ AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_MUTE_POS | AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_VOLUME_POS, /*_stridx*/ 0x00),\
|
||||
/* Standard AS Interface Descriptor(4.9.1) */\
|
||||
/* Interface 1, Alternate 0 - default alternate setting with 0 bandwidth */\
|
||||
TUD_AUDIO_DESC_STD_AS_INT(/*_itfnum*/ (uint8_t)((_itfnum)+1), /*_altset*/ 0x00, /*_nEPs*/ 0x00, /*_stridx*/ 0x00),\
|
||||
/* Standard AS Interface Descriptor(4.9.1) */\
|
||||
/* Interface 1, Alternate 1 - alternate interface for data streaming */\
|
||||
TUD_AUDIO_DESC_STD_AS_INT(/*_itfnum*/ (uint8_t)((_itfnum)+1), /*_altset*/ 0x01, /*_nEPs*/ 0x01, /*_stridx*/ 0x00),\
|
||||
/* Class-Specific AS Interface Descriptor(4.9.2) */\
|
||||
TUD_AUDIO_DESC_CS_AS_INT(/*_termid*/ 0x03, /*_ctrl*/ AUDIO_CTRL_NONE, /*_formattype*/ AUDIO_FORMAT_TYPE_I, /*_formats*/ AUDIO_DATA_FORMAT_TYPE_I_PCM, /*_nchannelsphysical*/ 0x01, /*_channelcfg*/ AUDIO_CHANNEL_CONFIG_NON_PREDEFINED, /*_stridx*/ 0x00),\
|
||||
/* Type I Format Type Descriptor(2.3.1.6 - Audio Formats) */\
|
||||
TUD_AUDIO_DESC_TYPE_I_FORMAT(_nBytesPerSample, _nBitsUsedPerSample),\
|
||||
/* Standard AS Isochronous Audio Data Endpoint Descriptor(4.10.1.1) */\
|
||||
TUD_AUDIO_DESC_STD_AS_ISO_EP(/*_ep*/ _epin, /*_attr*/ (uint8_t) (TUSB_XFER_ISOCHRONOUS | TUSB_ISO_EP_ATT_ASYNCHRONOUS | TUSB_ISO_EP_ATT_DATA), /*_maxEPsize*/ _epsize, /*_interval*/ 0x01),\
|
||||
/* Class-Specific AS Isochronous Audio Data Endpoint Descriptor(4.10.1.2) */\
|
||||
TUD_AUDIO_DESC_CS_AS_ISO_EP(/*_attr*/ AUDIO_CS_AS_ISO_DATA_EP_ATT_NON_MAX_PACKETS_OK, /*_ctrl*/ AUDIO_CTRL_NONE, /*_lockdelayunit*/ AUDIO_CS_AS_ISO_DATA_EP_LOCK_DELAY_UNIT_UNDEFINED, /*_lockdelay*/ 0x0000)
|
||||
|
||||
// AUDIO simple descriptor (UAC2) for 4 microphone input
|
||||
// - 1 Input Terminal, 1 Feature Unit (Mute and Volume Control), 1 Output Terminal, 1 Clock Source
|
||||
|
||||
#define TUD_AUDIO_MIC_FOUR_CH_DESC_LEN (TUD_AUDIO_DESC_IAD_LEN\
|
||||
+ TUD_AUDIO_DESC_STD_AC_LEN\
|
||||
+ TUD_AUDIO_DESC_CS_AC_LEN\
|
||||
+ TUD_AUDIO_DESC_CLK_SRC_LEN\
|
||||
+ TUD_AUDIO_DESC_INPUT_TERM_LEN\
|
||||
+ TUD_AUDIO_DESC_OUTPUT_TERM_LEN\
|
||||
+ TUD_AUDIO_DESC_FEATURE_UNIT_FOUR_CHANNEL_LEN\
|
||||
+ TUD_AUDIO_DESC_STD_AS_INT_LEN\
|
||||
+ TUD_AUDIO_DESC_STD_AS_INT_LEN\
|
||||
+ TUD_AUDIO_DESC_CS_AS_INT_LEN\
|
||||
+ TUD_AUDIO_DESC_TYPE_I_FORMAT_LEN\
|
||||
+ TUD_AUDIO_DESC_STD_AS_ISO_EP_LEN\
|
||||
+ TUD_AUDIO_DESC_CS_AS_ISO_EP_LEN)
|
||||
|
||||
#define TUD_AUDIO_MIC_FOUR_CH_DESC_N_AS_INT 1 // Number of AS interfaces
|
||||
|
||||
#define TUD_AUDIO_MIC_FOUR_CH_DESCRIPTOR(_itfnum, _stridx, _nBytesPerSample, _nBitsUsedPerSample, _epin, _epsize) \
|
||||
/* Standard Interface Association Descriptor (IAD) */\
|
||||
TUD_AUDIO_DESC_IAD(/*_firstitf*/ _itfnum, /*_nitfs*/ 0x02, /*_stridx*/ 0x00),\
|
||||
/* Standard AC Interface Descriptor(4.7.1) */\
|
||||
TUD_AUDIO_DESC_STD_AC(/*_itfnum*/ _itfnum, /*_nEPs*/ 0x00, /*_stridx*/ _stridx),\
|
||||
/* Class-Specific AC Interface Header Descriptor(4.7.2) */\
|
||||
TUD_AUDIO_DESC_CS_AC(/*_bcdADC*/ 0x0200, /*_category*/ AUDIO_FUNC_MICROPHONE, /*_totallen*/ TUD_AUDIO_DESC_CLK_SRC_LEN+TUD_AUDIO_DESC_INPUT_TERM_LEN+TUD_AUDIO_DESC_OUTPUT_TERM_LEN+TUD_AUDIO_DESC_FEATURE_UNIT_FOUR_CHANNEL_LEN, /*_ctrl*/ AUDIO_CS_AS_INTERFACE_CTRL_LATENCY_POS),\
|
||||
/* Clock Source Descriptor(4.7.2.1) */\
|
||||
TUD_AUDIO_DESC_CLK_SRC(/*_clkid*/ 0x04, /*_attr*/ AUDIO_CLOCK_SOURCE_ATT_INT_FIX_CLK, /*_ctrl*/ (AUDIO_CTRL_R << AUDIO_CLOCK_SOURCE_CTRL_CLK_FRQ_POS), /*_assocTerm*/ 0x01, /*_stridx*/ 0x00),\
|
||||
/* Input Terminal Descriptor(4.7.2.4) */\
|
||||
TUD_AUDIO_DESC_INPUT_TERM(/*_termid*/ 0x01, /*_termtype*/ AUDIO_TERM_TYPE_IN_GENERIC_MIC, /*_assocTerm*/ 0x03, /*_clkid*/ 0x04, /*_nchannelslogical*/ 0x04, /*_channelcfg*/ AUDIO_CHANNEL_CONFIG_NON_PREDEFINED, /*_idxchannelnames*/ 0x00, /*_ctrl*/ AUDIO_CTRL_R << AUDIO_IN_TERM_CTRL_CONNECTOR_POS, /*_stridx*/ 0x00),\
|
||||
/* Output Terminal Descriptor(4.7.2.5) */\
|
||||
TUD_AUDIO_DESC_OUTPUT_TERM(/*_termid*/ 0x03, /*_termtype*/ AUDIO_TERM_TYPE_USB_STREAMING, /*_assocTerm*/ 0x01, /*_srcid*/ 0x02, /*_clkid*/ 0x04, /*_ctrl*/ 0x0000, /*_stridx*/ 0x00),\
|
||||
/* Feature Unit Descriptor(4.7.2.8) */\
|
||||
TUD_AUDIO_DESC_FEATURE_UNIT_FOUR_CHANNEL(/*_unitid*/ 0x02, /*_srcid*/ 0x01, /*_ctrlch0master*/ AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_MUTE_POS | AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_VOLUME_POS, /*_ctrlch1*/ AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_MUTE_POS | AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_VOLUME_POS, /*_ctrlch2*/ AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_MUTE_POS | AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_VOLUME_POS, /*_ctrlch3*/ AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_MUTE_POS | AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_VOLUME_POS, /*_ctrlch4*/ AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_MUTE_POS | AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_VOLUME_POS, /*_stridx*/ 0x00),\
|
||||
/* Standard AS Interface Descriptor(4.9.1) */\
|
||||
/* Interface 1, Alternate 0 - default alternate setting with 0 bandwidth */\
|
||||
TUD_AUDIO_DESC_STD_AS_INT(/*_itfnum*/ (uint8_t)((_itfnum)+1), /*_altset*/ 0x00, /*_nEPs*/ 0x00, /*_stridx*/ 0x00),\
|
||||
/* Standard AS Interface Descriptor(4.9.1) */\
|
||||
/* Interface 1, Alternate 1 - alternate interface for data streaming */\
|
||||
TUD_AUDIO_DESC_STD_AS_INT(/*_itfnum*/ (uint8_t)((_itfnum)+1), /*_altset*/ 0x01, /*_nEPs*/ 0x01, /*_stridx*/ 0x00),\
|
||||
/* Class-Specific AS Interface Descriptor(4.9.2) */\
|
||||
TUD_AUDIO_DESC_CS_AS_INT(/*_termid*/ 0x03, /*_ctrl*/ AUDIO_CTRL_NONE, /*_formattype*/ AUDIO_FORMAT_TYPE_I, /*_formats*/ AUDIO_DATA_FORMAT_TYPE_I_PCM, /*_nchannelsphysical*/ 0x04, /*_channelcfg*/ AUDIO_CHANNEL_CONFIG_NON_PREDEFINED, /*_stridx*/ 0x00),\
|
||||
/* Type I Format Type Descriptor(2.3.1.6 - Audio Formats) */\
|
||||
TUD_AUDIO_DESC_TYPE_I_FORMAT(_nBytesPerSample, _nBitsUsedPerSample),\
|
||||
/* Standard AS Isochronous Audio Data Endpoint Descriptor(4.10.1.1) */\
|
||||
TUD_AUDIO_DESC_STD_AS_ISO_EP(/*_ep*/ _epin, /*_attr*/ (uint8_t) (TUSB_XFER_ISOCHRONOUS | TUSB_ISO_EP_ATT_ASYNCHRONOUS | TUSB_ISO_EP_ATT_DATA), /*_maxEPsize*/ _epsize, /*_interval*/ 0x01),\
|
||||
/* Class-Specific AS Isochronous Audio Data Endpoint Descriptor(4.10.1.2) */\
|
||||
TUD_AUDIO_DESC_CS_AS_ISO_EP(/*_attr*/ AUDIO_CS_AS_ISO_DATA_EP_ATT_NON_MAX_PACKETS_OK, /*_ctrl*/ AUDIO_CTRL_NONE, /*_lockdelayunit*/ AUDIO_CS_AS_ISO_DATA_EP_LOCK_DELAY_UNIT_UNDEFINED, /*_lockdelay*/ 0x0000)
|
||||
|
||||
// AUDIO simple descriptor (UAC2) for mono speaker
|
||||
// - 1 Input Terminal, 2 Feature Unit (Mute and Volume Control), 3 Output Terminal, 4 Clock Source
|
||||
|
||||
#define TUD_AUDIO_SPEAKER_MONO_FB_DESC_LEN (TUD_AUDIO_DESC_IAD_LEN\
|
||||
+ TUD_AUDIO_DESC_STD_AC_LEN\
|
||||
+ TUD_AUDIO_DESC_CS_AC_LEN\
|
||||
+ TUD_AUDIO_DESC_CLK_SRC_LEN\
|
||||
+ TUD_AUDIO_DESC_INPUT_TERM_LEN\
|
||||
+ TUD_AUDIO_DESC_OUTPUT_TERM_LEN\
|
||||
+ TUD_AUDIO_DESC_FEATURE_UNIT_ONE_CHANNEL_LEN\
|
||||
+ TUD_AUDIO_DESC_STD_AS_INT_LEN\
|
||||
+ TUD_AUDIO_DESC_STD_AS_INT_LEN\
|
||||
+ TUD_AUDIO_DESC_CS_AS_INT_LEN\
|
||||
+ TUD_AUDIO_DESC_TYPE_I_FORMAT_LEN\
|
||||
+ TUD_AUDIO_DESC_STD_AS_ISO_EP_LEN\
|
||||
+ TUD_AUDIO_DESC_CS_AS_ISO_EP_LEN\
|
||||
+ TUD_AUDIO_DESC_STD_AS_ISO_FB_EP_LEN)
|
||||
|
||||
#define TUD_AUDIO_SPEAKER_MONO_FB_DESCRIPTOR(_itfnum, _stridx, _nBytesPerSample, _nBitsUsedPerSample, _epout, _epsize, _epfb) \
|
||||
/* Standard Interface Association Descriptor (IAD) */\
|
||||
TUD_AUDIO_DESC_IAD(/*_firstitf*/ _itfnum, /*_nitfs*/ 0x02, /*_stridx*/ 0x00),\
|
||||
/* Standard AC Interface Descriptor(4.7.1) */\
|
||||
TUD_AUDIO_DESC_STD_AC(/*_itfnum*/ _itfnum, /*_nEPs*/ 0x00, /*_stridx*/ _stridx),\
|
||||
/* Class-Specific AC Interface Header Descriptor(4.7.2) */\
|
||||
TUD_AUDIO_DESC_CS_AC(/*_bcdADC*/ 0x0200, /*_category*/ AUDIO_FUNC_DESKTOP_SPEAKER, /*_totallen*/ TUD_AUDIO_DESC_CLK_SRC_LEN+TUD_AUDIO_DESC_INPUT_TERM_LEN+TUD_AUDIO_DESC_OUTPUT_TERM_LEN+TUD_AUDIO_DESC_FEATURE_UNIT_ONE_CHANNEL_LEN, /*_ctrl*/ AUDIO_CS_AS_INTERFACE_CTRL_LATENCY_POS),\
|
||||
/* Clock Source Descriptor(4.7.2.1) */\
|
||||
TUD_AUDIO_DESC_CLK_SRC(/*_clkid*/ 0x04, /*_attr*/ AUDIO_CLOCK_SOURCE_ATT_INT_FIX_CLK, /*_ctrl*/ (AUDIO_CTRL_R << AUDIO_CLOCK_SOURCE_CTRL_CLK_FRQ_POS), /*_assocTerm*/ 0x01, /*_stridx*/ 0x00),\
|
||||
/* Input Terminal Descriptor(4.7.2.4) */\
|
||||
TUD_AUDIO_DESC_INPUT_TERM(/*_termid*/ 0x01, /*_termtype*/ AUDIO_TERM_TYPE_USB_STREAMING, /*_assocTerm*/ 0x00, /*_clkid*/ 0x04, /*_nchannelslogical*/ 0x01, /*_channelcfg*/ AUDIO_CHANNEL_CONFIG_NON_PREDEFINED, /*_idxchannelnames*/ 0x00, /*_ctrl*/ 0 * (AUDIO_CTRL_R << AUDIO_IN_TERM_CTRL_CONNECTOR_POS), /*_stridx*/ 0x00),\
|
||||
/* Output Terminal Descriptor(4.7.2.5) */\
|
||||
TUD_AUDIO_DESC_OUTPUT_TERM(/*_termid*/ 0x03, /*_termtype*/ AUDIO_TERM_TYPE_OUT_DESKTOP_SPEAKER, /*_assocTerm*/ 0x01, /*_srcid*/ 0x02, /*_clkid*/ 0x04, /*_ctrl*/ 0x0000, /*_stridx*/ 0x00),\
|
||||
/* Feature Unit Descriptor(4.7.2.8) */\
|
||||
TUD_AUDIO_DESC_FEATURE_UNIT_ONE_CHANNEL(/*_unitid*/ 0x02, /*_srcid*/ 0x01, /*_ctrlch0master*/ 0 * (AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_MUTE_POS | AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_VOLUME_POS), /*_ctrlch1*/ 0 * (AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_MUTE_POS | AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_VOLUME_POS), /*_stridx*/ 0x00),\
|
||||
/* Standard AS Interface Descriptor(4.9.1) */\
|
||||
/* Interface 1, Alternate 0 - default alternate setting with 0 bandwidth */\
|
||||
TUD_AUDIO_DESC_STD_AS_INT(/*_itfnum*/ (uint8_t)((_itfnum) + 1), /*_altset*/ 0x00, /*_nEPs*/ 0x00, /*_stridx*/ 0x00),\
|
||||
/* Standard AS Interface Descriptor(4.9.1) */\
|
||||
/* Interface 1, Alternate 1 - alternate interface for data streaming */\
|
||||
TUD_AUDIO_DESC_STD_AS_INT(/*_itfnum*/ (uint8_t)((_itfnum) + 1), /*_altset*/ 0x01, /*_nEPs*/ 0x02, /*_stridx*/ 0x00),\
|
||||
/* Class-Specific AS Interface Descriptor(4.9.2) */\
|
||||
TUD_AUDIO_DESC_CS_AS_INT(/*_termid*/ 0x01, /*_ctrl*/ AUDIO_CTRL_NONE, /*_formattype*/ AUDIO_FORMAT_TYPE_I, /*_formats*/ AUDIO_DATA_FORMAT_TYPE_I_PCM, /*_nchannelsphysical*/ 0x01, /*_channelcfg*/ AUDIO_CHANNEL_CONFIG_NON_PREDEFINED, /*_stridx*/ 0x00),\
|
||||
/* Type I Format Type Descriptor(2.3.1.6 - Audio Formats) */\
|
||||
TUD_AUDIO_DESC_TYPE_I_FORMAT(_nBytesPerSample, _nBitsUsedPerSample),\
|
||||
/* Standard AS Isochronous Audio Data Endpoint Descriptor(4.10.1.1) */\
|
||||
TUD_AUDIO_DESC_STD_AS_ISO_EP(/*_ep*/ _epout, /*_attr*/ (uint8_t) (TUSB_XFER_ISOCHRONOUS | TUSB_ISO_EP_ATT_ASYNCHRONOUS | TUSB_ISO_EP_ATT_DATA), /*_maxEPsize*/ _epsize, /*_interval*/ 0x01),\
|
||||
/* Class-Specific AS Isochronous Audio Data Endpoint Descriptor(4.10.1.2) */\
|
||||
TUD_AUDIO_DESC_CS_AS_ISO_EP(/*_attr*/ AUDIO_CS_AS_ISO_DATA_EP_ATT_NON_MAX_PACKETS_OK, /*_ctrl*/ AUDIO_CTRL_NONE, /*_lockdelayunit*/ AUDIO_CS_AS_ISO_DATA_EP_LOCK_DELAY_UNIT_UNDEFINED, /*_lockdelay*/ 0x0000),\
|
||||
/* Standard AS Isochronous Feedback Endpoint Descriptor(4.10.2.1) */\
|
||||
TUD_AUDIO_DESC_STD_AS_ISO_FB_EP(/*_ep*/ _epfb, /*_interval*/ 1)\
|
||||
|
||||
// Calculate wMaxPacketSize of Endpoints
|
||||
#define TUD_AUDIO_EP_SIZE(_maxFrequency, _nBytesPerSample, _nChannels) \
|
||||
((((_maxFrequency + (TUD_OPT_HIGH_SPEED ? 7999 : 999)) / (TUD_OPT_HIGH_SPEED ? 8000 : 1000)) + 1) * _nBytesPerSample * _nChannels)
|
||||
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// USBTMC/USB488 Descriptor Templates
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
#define TUD_USBTMC_APP_CLASS (TUSB_CLASS_APPLICATION_SPECIFIC)
|
||||
#define TUD_USBTMC_APP_SUBCLASS 0x03u
|
||||
|
||||
#define TUD_USBTMC_PROTOCOL_STD 0x00u
|
||||
#define TUD_USBTMC_PROTOCOL_USB488 0x01u
|
||||
|
||||
// Interface number, number of endpoints, EP string index, USB_TMC_PROTOCOL*, bulk-out endpoint ID,
|
||||
// bulk-in endpoint ID
|
||||
#define TUD_USBTMC_IF_DESCRIPTOR(_itfnum, _bNumEndpoints, _stridx, _itfProtocol) \
|
||||
/* Interface */ \
|
||||
0x09, TUSB_DESC_INTERFACE, _itfnum, 0x00, _bNumEndpoints, TUD_USBTMC_APP_CLASS, TUD_USBTMC_APP_SUBCLASS, _itfProtocol, _stridx
|
||||
|
||||
#define TUD_USBTMC_IF_DESCRIPTOR_LEN 9u
|
||||
|
||||
#define TUD_USBTMC_BULK_DESCRIPTORS(_epout, _epin, _bulk_epsize) \
|
||||
/* Endpoint Out */ \
|
||||
7, TUSB_DESC_ENDPOINT, _epout, TUSB_XFER_BULK, U16_TO_U8S_LE(_bulk_epsize), 0u, \
|
||||
/* Endpoint In */ \
|
||||
7, TUSB_DESC_ENDPOINT, _epin, TUSB_XFER_BULK, U16_TO_U8S_LE(_bulk_epsize), 0u
|
||||
|
||||
#define TUD_USBTMC_BULK_DESCRIPTORS_LEN (7u+7u)
|
||||
|
||||
/* optional interrupt endpoint */ \
|
||||
// _int_pollingInterval : for LS/FS, expressed in frames (1ms each). 16 may be a good number?
|
||||
#define TUD_USBTMC_INT_DESCRIPTOR(_ep_interrupt, _ep_interrupt_size, _int_pollingInterval ) \
|
||||
7, TUSB_DESC_ENDPOINT, _ep_interrupt, TUSB_XFER_INTERRUPT, U16_TO_U8S_LE(_ep_interrupt_size), _int_pollingInterval
|
||||
|
||||
#define TUD_USBTMC_INT_DESCRIPTOR_LEN (7u)
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Vendor Descriptor Templates
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
#define TUD_VENDOR_DESC_LEN (9+7+7)
|
||||
|
||||
// Interface number, string index, EP Out & IN address, EP size
|
||||
#define TUD_VENDOR_DESCRIPTOR(_itfnum, _stridx, _epout, _epin, _epsize) \
|
||||
/* Interface */\
|
||||
9, TUSB_DESC_INTERFACE, _itfnum, 0, 2, TUSB_CLASS_VENDOR_SPECIFIC, 0x00, 0x00, _stridx,\
|
||||
/* Endpoint Out */\
|
||||
7, TUSB_DESC_ENDPOINT, _epout, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0,\
|
||||
/* Endpoint In */\
|
||||
7, TUSB_DESC_ENDPOINT, _epin, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// DFU Runtime Descriptor Templates
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
#define TUD_DFU_APP_CLASS (TUSB_CLASS_APPLICATION_SPECIFIC)
|
||||
#define TUD_DFU_APP_SUBCLASS (APP_SUBCLASS_DFU_RUNTIME)
|
||||
|
||||
// Length of template descriptr: 18 bytes
|
||||
#define TUD_DFU_RT_DESC_LEN (9 + 9)
|
||||
|
||||
// DFU runtime descriptor
|
||||
// Interface number, string index, attributes, detach timeout, transfer size
|
||||
#define TUD_DFU_RT_DESCRIPTOR(_itfnum, _stridx, _attr, _timeout, _xfer_size) \
|
||||
/* Interface */ \
|
||||
9, TUSB_DESC_INTERFACE, _itfnum, 0, 0, TUD_DFU_APP_CLASS, TUD_DFU_APP_SUBCLASS, DFU_PROTOCOL_RT, _stridx, \
|
||||
/* Function */ \
|
||||
9, DFU_DESC_FUNCTIONAL, _attr, U16_TO_U8S_LE(_timeout), U16_TO_U8S_LE(_xfer_size), U16_TO_U8S_LE(0x0101)
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// DFU Descriptor Templates
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Length of template descriptor: 9 bytes + number of alternatives * 9
|
||||
#define TUD_DFU_DESC_LEN(_alt_count) (9 + (_alt_count) * 9)
|
||||
|
||||
// Interface number, Alternate count, starting string index, attributes, detach timeout, transfer size
|
||||
// Note: Alternate count must be numeric or macro, string index is increased by one for each Alt interface
|
||||
#define TUD_DFU_DESCRIPTOR(_itfnum, _alt_count, _stridx, _attr, _timeout, _xfer_size) \
|
||||
TU_XSTRCAT(_TUD_DFU_ALT_,_alt_count)(_itfnum, 0, _stridx), \
|
||||
/* Function */ \
|
||||
9, DFU_DESC_FUNCTIONAL, _attr, U16_TO_U8S_LE(_timeout), U16_TO_U8S_LE(_xfer_size), U16_TO_U8S_LE(0x0101)
|
||||
|
||||
#define _TUD_DFU_ALT(_itfnum, _alt, _stridx) \
|
||||
/* Interface */ \
|
||||
9, TUSB_DESC_INTERFACE, _itfnum, _alt, 0, TUD_DFU_APP_CLASS, TUD_DFU_APP_SUBCLASS, DFU_PROTOCOL_DFU, _stridx
|
||||
|
||||
#define _TUD_DFU_ALT_1(_itfnum, _alt_count, _stridx) \
|
||||
_TUD_DFU_ALT(_itfnum, _alt_count, _stridx)
|
||||
|
||||
#define _TUD_DFU_ALT_2(_itfnum, _alt_count, _stridx) \
|
||||
_TUD_DFU_ALT(_itfnum, _alt_count, _stridx), \
|
||||
_TUD_DFU_ALT_1(_itfnum, _alt_count+1, _stridx+1)
|
||||
|
||||
#define _TUD_DFU_ALT_3(_itfnum, _alt_count, _stridx) \
|
||||
_TUD_DFU_ALT(_itfnum, _alt_count, _stridx), \
|
||||
_TUD_DFU_ALT_2(_itfnum, _alt_count+1, _stridx+1)
|
||||
|
||||
#define _TUD_DFU_ALT_4(_itfnum, _alt_count, _stridx) \
|
||||
_TUD_DFU_ALT(_itfnum, _alt_count, _stridx), \
|
||||
_TUD_DFU_ALT_3(_itfnum, _alt_count+1, _stridx+1)
|
||||
|
||||
#define _TUD_DFU_ALT_5(_itfnum, _alt_count, _stridx) \
|
||||
_TUD_DFU_ALT(_itfnum, _alt_count, _stridx), \
|
||||
_TUD_DFU_ALT_4(_itfnum, _alt_count+1, _stridx+1)
|
||||
|
||||
#define _TUD_DFU_ALT_6(_itfnum, _alt_count, _stridx) \
|
||||
_TUD_DFU_ALT(_itfnum, _alt_count, _stridx), \
|
||||
_TUD_DFU_ALT_5(_itfnum, _alt_count+1, _stridx+1)
|
||||
|
||||
#define _TUD_DFU_ALT_7(_itfnum, _alt_count, _stridx) \
|
||||
_TUD_DFU_ALT(_itfnum, _alt_count, _stridx), \
|
||||
_TUD_DFU_ALT_6(_itfnum, _alt_count+1, _stridx+1)
|
||||
|
||||
#define _TUD_DFU_ALT_8(_itfnum, _alt_count, _stridx) \
|
||||
_TUD_DFU_ALT(_itfnum, _alt_count, _stridx), \
|
||||
_TUD_DFU_ALT_7(_itfnum, _alt_count+1, _stridx+1)
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// CDC-ECM Descriptor Templates
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Length of template descriptor: 71 bytes
|
||||
#define TUD_CDC_ECM_DESC_LEN (8+9+5+5+13+7+9+9+7+7)
|
||||
|
||||
// CDC-ECM Descriptor Template
|
||||
// Interface number, description string index, MAC address string index, EP notification address and size, EP data address (out, in), and size, max segment size.
|
||||
#define TUD_CDC_ECM_DESCRIPTOR(_itfnum, _desc_stridx, _mac_stridx, _ep_notif, _ep_notif_size, _epout, _epin, _epsize, _maxsegmentsize) \
|
||||
/* Interface Association */\
|
||||
8, TUSB_DESC_INTERFACE_ASSOCIATION, _itfnum, 2, TUSB_CLASS_CDC, CDC_COMM_SUBCLASS_ETHERNET_CONTROL_MODEL, 0, 0,\
|
||||
/* CDC Control Interface */\
|
||||
9, TUSB_DESC_INTERFACE, _itfnum, 0, 1, TUSB_CLASS_CDC, CDC_COMM_SUBCLASS_ETHERNET_CONTROL_MODEL, 0, _desc_stridx,\
|
||||
/* CDC-ECM Header */\
|
||||
5, TUSB_DESC_CS_INTERFACE, CDC_FUNC_DESC_HEADER, U16_TO_U8S_LE(0x0120),\
|
||||
/* CDC-ECM Union */\
|
||||
5, TUSB_DESC_CS_INTERFACE, CDC_FUNC_DESC_UNION, _itfnum, (uint8_t)((_itfnum) + 1),\
|
||||
/* CDC-ECM Functional Descriptor */\
|
||||
13, TUSB_DESC_CS_INTERFACE, CDC_FUNC_DESC_ETHERNET_NETWORKING, _mac_stridx, 0, 0, 0, 0, U16_TO_U8S_LE(_maxsegmentsize), U16_TO_U8S_LE(0), 0,\
|
||||
/* Endpoint Notification */\
|
||||
7, TUSB_DESC_ENDPOINT, _ep_notif, TUSB_XFER_INTERRUPT, U16_TO_U8S_LE(_ep_notif_size), 1,\
|
||||
/* CDC Data Interface (default inactive) */\
|
||||
9, TUSB_DESC_INTERFACE, (uint8_t)((_itfnum)+1), 0, 0, TUSB_CLASS_CDC_DATA, 0, 0, 0,\
|
||||
/* CDC Data Interface (alternative active) */\
|
||||
9, TUSB_DESC_INTERFACE, (uint8_t)((_itfnum)+1), 1, 2, TUSB_CLASS_CDC_DATA, 0, 0, 0,\
|
||||
/* Endpoint In */\
|
||||
7, TUSB_DESC_ENDPOINT, _epin, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0,\
|
||||
/* Endpoint Out */\
|
||||
7, TUSB_DESC_ENDPOINT, _epout, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// RNDIS Descriptor Templates
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
#if 0
|
||||
/* Windows XP */
|
||||
#define TUD_RNDIS_ITF_CLASS TUSB_CLASS_CDC
|
||||
#define TUD_RNDIS_ITF_SUBCLASS CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL
|
||||
#define TUD_RNDIS_ITF_PROTOCOL 0xFF /* CDC_COMM_PROTOCOL_MICROSOFT_RNDIS */
|
||||
#else
|
||||
/* Windows 7+ */
|
||||
#define TUD_RNDIS_ITF_CLASS TUSB_CLASS_WIRELESS_CONTROLLER
|
||||
#define TUD_RNDIS_ITF_SUBCLASS 0x01
|
||||
#define TUD_RNDIS_ITF_PROTOCOL 0x03
|
||||
#endif
|
||||
|
||||
// Length of template descriptor: 66 bytes
|
||||
#define TUD_RNDIS_DESC_LEN (8+9+5+5+4+5+7+9+7+7)
|
||||
|
||||
// RNDIS Descriptor Template
|
||||
// Interface number, string index, EP notification address and size, EP data address (out, in) and size.
|
||||
#define TUD_RNDIS_DESCRIPTOR(_itfnum, _stridx, _ep_notif, _ep_notif_size, _epout, _epin, _epsize) \
|
||||
/* Interface Association */\
|
||||
8, TUSB_DESC_INTERFACE_ASSOCIATION, _itfnum, 2, TUD_RNDIS_ITF_CLASS, TUD_RNDIS_ITF_SUBCLASS, TUD_RNDIS_ITF_PROTOCOL, 0,\
|
||||
/* CDC Control Interface */\
|
||||
9, TUSB_DESC_INTERFACE, _itfnum, 0, 1, TUD_RNDIS_ITF_CLASS, TUD_RNDIS_ITF_SUBCLASS, TUD_RNDIS_ITF_PROTOCOL, _stridx,\
|
||||
/* CDC-ACM Header */\
|
||||
5, TUSB_DESC_CS_INTERFACE, CDC_FUNC_DESC_HEADER, U16_TO_U8S_LE(0x0110),\
|
||||
/* CDC Call Management */\
|
||||
5, TUSB_DESC_CS_INTERFACE, CDC_FUNC_DESC_CALL_MANAGEMENT, 0, (uint8_t)((_itfnum) + 1),\
|
||||
/* ACM */\
|
||||
4, TUSB_DESC_CS_INTERFACE, CDC_FUNC_DESC_ABSTRACT_CONTROL_MANAGEMENT, 0,\
|
||||
/* CDC Union */\
|
||||
5, TUSB_DESC_CS_INTERFACE, CDC_FUNC_DESC_UNION, _itfnum, (uint8_t)((_itfnum) + 1),\
|
||||
/* Endpoint Notification */\
|
||||
7, TUSB_DESC_ENDPOINT, _ep_notif, TUSB_XFER_INTERRUPT, U16_TO_U8S_LE(_ep_notif_size), 1,\
|
||||
/* CDC Data Interface */\
|
||||
9, TUSB_DESC_INTERFACE, (uint8_t)((_itfnum)+1), 0, 2, TUSB_CLASS_CDC_DATA, 0, 0, 0,\
|
||||
/* Endpoint In */\
|
||||
7, TUSB_DESC_ENDPOINT, _epin, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0,\
|
||||
/* Endpoint Out */\
|
||||
7, TUSB_DESC_ENDPOINT, _epout, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Bluetooth Radio Descriptor Templates
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
#define TUD_BT_APP_CLASS (TUSB_CLASS_WIRELESS_CONTROLLER)
|
||||
#define TUD_BT_APP_SUBCLASS 0x01
|
||||
#define TUD_BT_PROTOCOL_PRIMARY_CONTROLLER 0x01
|
||||
#define TUD_BT_PROTOCOL_AMP_CONTROLLER 0x02
|
||||
|
||||
// Length of template descriptor: 38 bytes + number of ISO alternatives * 23
|
||||
#define TUD_BTH_DESC_LEN (8 + 9 + 7 + 7 + 7 + (CFG_TUD_BTH_ISO_ALT_COUNT) * (9 + 7 + 7))
|
||||
|
||||
/* Primary Interface */
|
||||
#define TUD_BTH_PRI_ITF(_itfnum, _stridx, _ep_evt, _ep_evt_size, _ep_evt_interval, _ep_in, _ep_out, _ep_size) \
|
||||
9, TUSB_DESC_INTERFACE, _itfnum, 0, 3, TUD_BT_APP_CLASS, TUD_BT_APP_SUBCLASS, TUD_BT_PROTOCOL_PRIMARY_CONTROLLER, _stridx, \
|
||||
/* Endpoint In for events */ \
|
||||
7, TUSB_DESC_ENDPOINT, _ep_evt, TUSB_XFER_INTERRUPT, U16_TO_U8S_LE(_ep_evt_size), _ep_evt_interval, \
|
||||
/* Endpoint In for ACL data */ \
|
||||
7, TUSB_DESC_ENDPOINT, _ep_in, TUSB_XFER_BULK, U16_TO_U8S_LE(_ep_size), 1, \
|
||||
/* Endpoint Out for ACL data */ \
|
||||
7, TUSB_DESC_ENDPOINT, _ep_out, TUSB_XFER_BULK, U16_TO_U8S_LE(_ep_size), 1
|
||||
|
||||
#define TUD_BTH_ISO_ITF(_itfnum, _alt, _ep_in, _ep_out, _n) ,\
|
||||
/* Interface with 2 endpoints */ \
|
||||
9, TUSB_DESC_INTERFACE, _itfnum, _alt, 2, TUD_BT_APP_CLASS, TUD_BT_APP_SUBCLASS, TUD_BT_PROTOCOL_PRIMARY_CONTROLLER, 0, \
|
||||
/* Isochronous endpoints */ \
|
||||
7, TUSB_DESC_ENDPOINT, _ep_in, TUSB_XFER_ISOCHRONOUS, U16_TO_U8S_LE(_n), 1, \
|
||||
7, TUSB_DESC_ENDPOINT, _ep_out, TUSB_XFER_ISOCHRONOUS, U16_TO_U8S_LE(_n), 1
|
||||
|
||||
#define _FIRST(a, ...) a
|
||||
#define _REST(a, ...) __VA_ARGS__
|
||||
|
||||
#define TUD_BTH_ISO_ITF_0(_itfnum, ...)
|
||||
#define TUD_BTH_ISO_ITF_1(_itfnum, _ep_in, _ep_out, ...) TUD_BTH_ISO_ITF(_itfnum, (CFG_TUD_BTH_ISO_ALT_COUNT) - 1, _ep_in, _ep_out, _FIRST(__VA_ARGS__))
|
||||
#define TUD_BTH_ISO_ITF_2(_itfnum, _ep_in, _ep_out, ...) TUD_BTH_ISO_ITF(_itfnum, (CFG_TUD_BTH_ISO_ALT_COUNT) - 2, _ep_in, _ep_out, _FIRST(__VA_ARGS__)) \
|
||||
TUD_BTH_ISO_ITF_1(_itfnum, _ep_in, _ep_out, _REST(__VA_ARGS__))
|
||||
#define TUD_BTH_ISO_ITF_3(_itfnum, _ep_in, _ep_out, ...) TUD_BTH_ISO_ITF(_itfnum, (CFG_TUD_BTH_ISO_ALT_COUNT) - 3, _ep_in, _ep_out, _FIRST(__VA_ARGS__)) \
|
||||
TUD_BTH_ISO_ITF_2(_itfnum, _ep_in, _ep_out, _REST(__VA_ARGS__))
|
||||
#define TUD_BTH_ISO_ITF_4(_itfnum, _ep_in, _ep_out, ...) TUD_BTH_ISO_ITF(_itfnum, (CFG_TUD_BTH_ISO_ALT_COUNT) - 4, _ep_in, _ep_out, _FIRST(__VA_ARGS__)) \
|
||||
TUD_BTH_ISO_ITF_3(_itfnum, _ep_in, _ep_out, _REST(__VA_ARGS__))
|
||||
#define TUD_BTH_ISO_ITF_5(_itfnum, _ep_in, _ep_out, ...) TUD_BTH_ISO_ITF(_itfnum, (CFG_TUD_BTH_ISO_ALT_COUNT) - 5, _ep_in, _ep_out, _FIRST(__VA_ARGS__)) \
|
||||
TUD_BTH_ISO_ITF_4(_itfnum, _ep_in, _ep_out, _REST(__VA_ARGS__))
|
||||
#define TUD_BTH_ISO_ITF_6(_itfnum, _ep_in, _ep_out, ...) TUD_BTH_ISO_ITF(_itfnum, (CFG_TUD_BTH_ISO_ALT_COUNT) - 6, _ep_in, _ep_out, _FIRST(__VA_ARGS__)) \
|
||||
TUD_BTH_ISO_ITF_5(_itfnum, _ep_in, _ep_out, _REST(__VA_ARGS__))
|
||||
|
||||
#define TUD_BTH_ISO_ITFS(_itfnum, _ep_in, _ep_out, ...) \
|
||||
TU_XSTRCAT(TUD_BTH_ISO_ITF_, CFG_TUD_BTH_ISO_ALT_COUNT)(_itfnum, _ep_in, _ep_out, __VA_ARGS__)
|
||||
|
||||
// BT Primary controller descriptor
|
||||
// Interface number, string index, attributes, event endpoint, event endpoint size, interval, data in, data out, data endpoint size, iso endpoint sizes
|
||||
// TODO BTH should also use IAD like CDC for composite device
|
||||
#define TUD_BTH_DESCRIPTOR(_itfnum, _stridx, _ep_evt, _ep_evt_size, _ep_evt_interval, _ep_in, _ep_out, _ep_size,...) \
|
||||
/* Interface Associate */\
|
||||
8, TUSB_DESC_INTERFACE_ASSOCIATION, _itfnum, 2, TUD_BT_APP_CLASS, TUD_BT_APP_SUBCLASS, TUD_BT_PROTOCOL_PRIMARY_CONTROLLER, 0,\
|
||||
TUD_BTH_PRI_ITF(_itfnum, _stridx, _ep_evt, _ep_evt_size, _ep_evt_interval, _ep_in, _ep_out, _ep_size) \
|
||||
TUD_BTH_ISO_ITFS(_itfnum + 1, _ep_in + 1, _ep_out + 1, __VA_ARGS__)
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// CDC-NCM Descriptor Templates
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Length of template descriptor
|
||||
#define TUD_CDC_NCM_DESC_LEN (8+9+5+5+13+6+7+9+9+7+7)
|
||||
|
||||
// CDC-ECM Descriptor Template
|
||||
// Interface number, description string index, MAC address string index, EP notification address and size, EP data address (out, in), and size, max segment size.
|
||||
#define TUD_CDC_NCM_DESCRIPTOR(_itfnum, _desc_stridx, _mac_stridx, _ep_notif, _ep_notif_size, _epout, _epin, _epsize, _maxsegmentsize) \
|
||||
/* Interface Association */\
|
||||
8, TUSB_DESC_INTERFACE_ASSOCIATION, _itfnum, 2, TUSB_CLASS_CDC, CDC_COMM_SUBCLASS_NETWORK_CONTROL_MODEL, 0, 0,\
|
||||
/* CDC Control Interface */\
|
||||
9, TUSB_DESC_INTERFACE, _itfnum, 0, 1, TUSB_CLASS_CDC, CDC_COMM_SUBCLASS_NETWORK_CONTROL_MODEL, 0, _desc_stridx,\
|
||||
/* CDC-NCM Header */\
|
||||
5, TUSB_DESC_CS_INTERFACE, CDC_FUNC_DESC_HEADER, U16_TO_U8S_LE(0x0110),\
|
||||
/* CDC-NCM Union */\
|
||||
5, TUSB_DESC_CS_INTERFACE, CDC_FUNC_DESC_UNION, _itfnum, (uint8_t)((_itfnum) + 1),\
|
||||
/* CDC-NCM Functional Descriptor */\
|
||||
13, TUSB_DESC_CS_INTERFACE, CDC_FUNC_DESC_ETHERNET_NETWORKING, _mac_stridx, 0, 0, 0, 0, U16_TO_U8S_LE(_maxsegmentsize), U16_TO_U8S_LE(0), 0, \
|
||||
/* CDC-NCM Functional Descriptor */\
|
||||
6, TUSB_DESC_CS_INTERFACE, CDC_FUNC_DESC_NCM, U16_TO_U8S_LE(0x0100), 0, \
|
||||
/* Endpoint Notification */\
|
||||
7, TUSB_DESC_ENDPOINT, _ep_notif, TUSB_XFER_INTERRUPT, U16_TO_U8S_LE(_ep_notif_size), 50,\
|
||||
/* CDC Data Interface (default inactive) */\
|
||||
9, TUSB_DESC_INTERFACE, (uint8_t)((_itfnum)+1), 0, 0, TUSB_CLASS_CDC_DATA, 0, NCM_DATA_PROTOCOL_NETWORK_TRANSFER_BLOCK, 0,\
|
||||
/* CDC Data Interface (alternative active) */\
|
||||
9, TUSB_DESC_INTERFACE, (uint8_t)((_itfnum)+1), 1, 2, TUSB_CLASS_CDC_DATA, 0, NCM_DATA_PROTOCOL_NETWORK_TRANSFER_BLOCK, 0,\
|
||||
/* Endpoint In */\
|
||||
7, TUSB_DESC_ENDPOINT, _epin, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0,\
|
||||
/* Endpoint Out */\
|
||||
7, TUSB_DESC_ENDPOINT, _epout, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _TUSB_USBD_H_ */
|
||||
|
||||
/** @} */
|
|
@ -0,0 +1,222 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#include "tusb_option.h"
|
||||
|
||||
#if CFG_TUD_ENABLED
|
||||
|
||||
#include "dcd.h"
|
||||
#include "tusb.h"
|
||||
#include "device/usbd_pvt.h"
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Callback weak stubs (called if application does not provide)
|
||||
//--------------------------------------------------------------------+
|
||||
TU_ATTR_WEAK void dcd_edpt0_status_complete(uint8_t rhport, tusb_control_request_t const* request) {
|
||||
(void) rhport;
|
||||
(void) request;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// MACRO CONSTANT TYPEDEF
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
#if CFG_TUSB_DEBUG >= CFG_TUD_LOG_LEVEL
|
||||
extern void usbd_driver_print_control_complete_name(usbd_control_xfer_cb_t callback);
|
||||
#endif
|
||||
|
||||
enum {
|
||||
EDPT_CTRL_OUT = 0x00,
|
||||
EDPT_CTRL_IN = 0x80
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
tusb_control_request_t request;
|
||||
uint8_t* buffer;
|
||||
uint16_t data_len;
|
||||
uint16_t total_xferred;
|
||||
usbd_control_xfer_cb_t complete_cb;
|
||||
} usbd_control_xfer_t;
|
||||
|
||||
tu_static usbd_control_xfer_t _ctrl_xfer;
|
||||
|
||||
CFG_TUD_MEM_SECTION CFG_TUSB_MEM_ALIGN
|
||||
tu_static uint8_t _usbd_ctrl_buf[CFG_TUD_ENDPOINT0_SIZE];
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Application API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Queue ZLP status transaction
|
||||
static inline bool _status_stage_xact(uint8_t rhport, tusb_control_request_t const* request) {
|
||||
// Opposite to endpoint in Data Phase
|
||||
uint8_t const ep_addr = request->bmRequestType_bit.direction ? EDPT_CTRL_OUT : EDPT_CTRL_IN;
|
||||
return usbd_edpt_xfer(rhport, ep_addr, NULL, 0);
|
||||
}
|
||||
|
||||
// Status phase
|
||||
bool tud_control_status(uint8_t rhport, tusb_control_request_t const* request) {
|
||||
_ctrl_xfer.request = (*request);
|
||||
_ctrl_xfer.buffer = NULL;
|
||||
_ctrl_xfer.total_xferred = 0;
|
||||
_ctrl_xfer.data_len = 0;
|
||||
|
||||
return _status_stage_xact(rhport, request);
|
||||
}
|
||||
|
||||
// Queue a transaction in Data Stage
|
||||
// Each transaction has up to Endpoint0's max packet size.
|
||||
// This function can also transfer an zero-length packet
|
||||
static bool _data_stage_xact(uint8_t rhport) {
|
||||
uint16_t const xact_len = tu_min16(_ctrl_xfer.data_len - _ctrl_xfer.total_xferred,
|
||||
CFG_TUD_ENDPOINT0_SIZE);
|
||||
|
||||
uint8_t ep_addr = EDPT_CTRL_OUT;
|
||||
|
||||
if (_ctrl_xfer.request.bmRequestType_bit.direction == TUSB_DIR_IN) {
|
||||
ep_addr = EDPT_CTRL_IN;
|
||||
if (xact_len) {
|
||||
TU_VERIFY(0 == tu_memcpy_s(_usbd_ctrl_buf, CFG_TUD_ENDPOINT0_SIZE, _ctrl_xfer.buffer, xact_len));
|
||||
}
|
||||
}
|
||||
|
||||
return usbd_edpt_xfer(rhport, ep_addr, xact_len ? _usbd_ctrl_buf : NULL, xact_len);
|
||||
}
|
||||
|
||||
// Transmit data to/from the control endpoint.
|
||||
// If the request's wLength is zero, a status packet is sent instead.
|
||||
bool tud_control_xfer(uint8_t rhport, tusb_control_request_t const* request, void* buffer, uint16_t len) {
|
||||
_ctrl_xfer.request = (*request);
|
||||
_ctrl_xfer.buffer = (uint8_t*) buffer;
|
||||
_ctrl_xfer.total_xferred = 0U;
|
||||
_ctrl_xfer.data_len = tu_min16(len, request->wLength);
|
||||
|
||||
if (request->wLength > 0U) {
|
||||
if (_ctrl_xfer.data_len > 0U) {
|
||||
TU_ASSERT(buffer);
|
||||
}
|
||||
|
||||
// TU_LOG2(" Control total data length is %u bytes\r\n", _ctrl_xfer.data_len);
|
||||
|
||||
// Data stage
|
||||
TU_ASSERT(_data_stage_xact(rhport));
|
||||
} else {
|
||||
// Status stage
|
||||
TU_ASSERT(_status_stage_xact(rhport, request));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// USBD API
|
||||
//--------------------------------------------------------------------+
|
||||
void usbd_control_reset(void);
|
||||
void usbd_control_set_request(tusb_control_request_t const* request);
|
||||
void usbd_control_set_complete_callback(usbd_control_xfer_cb_t fp);
|
||||
bool usbd_control_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes);
|
||||
|
||||
void usbd_control_reset(void) {
|
||||
tu_varclr(&_ctrl_xfer);
|
||||
}
|
||||
|
||||
// Set complete callback
|
||||
void usbd_control_set_complete_callback(usbd_control_xfer_cb_t fp) {
|
||||
_ctrl_xfer.complete_cb = fp;
|
||||
}
|
||||
|
||||
// for dcd_set_address where DCD is responsible for status response
|
||||
void usbd_control_set_request(tusb_control_request_t const* request) {
|
||||
_ctrl_xfer.request = (*request);
|
||||
_ctrl_xfer.buffer = NULL;
|
||||
_ctrl_xfer.total_xferred = 0;
|
||||
_ctrl_xfer.data_len = 0;
|
||||
}
|
||||
|
||||
// callback when a transaction complete on
|
||||
// - DATA stage of control endpoint or
|
||||
// - Status stage
|
||||
bool usbd_control_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) {
|
||||
(void) result;
|
||||
|
||||
// Endpoint Address is opposite to direction bit, this is Status Stage complete event
|
||||
if (tu_edpt_dir(ep_addr) != _ctrl_xfer.request.bmRequestType_bit.direction) {
|
||||
TU_ASSERT(0 == xferred_bytes);
|
||||
|
||||
// invoke optional dcd hook if available
|
||||
dcd_edpt0_status_complete(rhport, &_ctrl_xfer.request);
|
||||
|
||||
if (_ctrl_xfer.complete_cb) {
|
||||
// TODO refactor with usbd_driver_print_control_complete_name
|
||||
_ctrl_xfer.complete_cb(rhport, CONTROL_STAGE_ACK, &_ctrl_xfer.request);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if (_ctrl_xfer.request.bmRequestType_bit.direction == TUSB_DIR_OUT) {
|
||||
TU_VERIFY(_ctrl_xfer.buffer);
|
||||
memcpy(_ctrl_xfer.buffer, _usbd_ctrl_buf, xferred_bytes);
|
||||
TU_LOG_MEM(CFG_TUD_LOG_LEVEL, _usbd_ctrl_buf, xferred_bytes, 2);
|
||||
}
|
||||
|
||||
_ctrl_xfer.total_xferred += (uint16_t) xferred_bytes;
|
||||
_ctrl_xfer.buffer += xferred_bytes;
|
||||
|
||||
// Data Stage is complete when all request's length are transferred or
|
||||
// a short packet is sent including zero-length packet.
|
||||
if ((_ctrl_xfer.request.wLength == _ctrl_xfer.total_xferred) ||
|
||||
(xferred_bytes < CFG_TUD_ENDPOINT0_SIZE)) {
|
||||
// DATA stage is complete
|
||||
bool is_ok = true;
|
||||
|
||||
// invoke complete callback if set
|
||||
// callback can still stall control in status phase e.g out data does not make sense
|
||||
if (_ctrl_xfer.complete_cb) {
|
||||
#if CFG_TUSB_DEBUG >= CFG_TUD_LOG_LEVEL
|
||||
usbd_driver_print_control_complete_name(_ctrl_xfer.complete_cb);
|
||||
#endif
|
||||
|
||||
is_ok = _ctrl_xfer.complete_cb(rhport, CONTROL_STAGE_DATA, &_ctrl_xfer.request);
|
||||
}
|
||||
|
||||
if (is_ok) {
|
||||
// Send status
|
||||
TU_ASSERT(_status_stage_xact(rhport, &_ctrl_xfer.request));
|
||||
} else {
|
||||
// Stall both IN and OUT control endpoint
|
||||
dcd_edpt_stall(rhport, EDPT_CTRL_OUT);
|
||||
dcd_edpt_stall(rhport, EDPT_CTRL_IN);
|
||||
}
|
||||
} else {
|
||||
// More data to transfer
|
||||
TU_ASSERT(_data_stage_xact(rhport));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#endif
|
|
@ -0,0 +1,126 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
#ifndef _TUSB_USBD_PVT_H_
|
||||
#define _TUSB_USBD_PVT_H_
|
||||
|
||||
#include "osal/osal.h"
|
||||
#include "common/tusb_fifo.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define TU_LOG_USBD(...) TU_LOG(CFG_TUD_LOG_LEVEL, __VA_ARGS__)
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Class Driver API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
typedef struct {
|
||||
#if CFG_TUSB_DEBUG >= CFG_TUD_LOG_LEVEL
|
||||
char const* name;
|
||||
#endif
|
||||
|
||||
void (* init ) (void);
|
||||
void (* reset ) (uint8_t rhport);
|
||||
uint16_t (* open ) (uint8_t rhport, tusb_desc_interface_t const * desc_intf, uint16_t max_len);
|
||||
bool (* control_xfer_cb ) (uint8_t rhport, uint8_t stage, tusb_control_request_t const * request);
|
||||
bool (* xfer_cb ) (uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes);
|
||||
void (* sof ) (uint8_t rhport, uint32_t frame_count); // optional
|
||||
} usbd_class_driver_t;
|
||||
|
||||
// Invoked when initializing device stack to get additional class drivers.
|
||||
// Can be implemented by application to extend/overwrite class driver support.
|
||||
// Note: The drivers array must be accessible at all time when stack is active
|
||||
usbd_class_driver_t const* usbd_app_driver_get_cb(uint8_t* driver_count) TU_ATTR_WEAK;
|
||||
|
||||
typedef bool (*usbd_control_xfer_cb_t)(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request);
|
||||
|
||||
void usbd_int_set(bool enabled);
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// USBD Endpoint API
|
||||
// Note: rhport should be 0 since device stack only support 1 rhport for now
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Open an endpoint
|
||||
bool usbd_edpt_open(uint8_t rhport, tusb_desc_endpoint_t const * desc_ep);
|
||||
|
||||
// Close an endpoint
|
||||
void usbd_edpt_close(uint8_t rhport, uint8_t ep_addr);
|
||||
|
||||
// Submit a usb transfer
|
||||
bool usbd_edpt_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes);
|
||||
|
||||
// Submit a usb ISO transfer by use of a FIFO (ring buffer) - all bytes in FIFO get transmitted
|
||||
bool usbd_edpt_xfer_fifo(uint8_t rhport, uint8_t ep_addr, tu_fifo_t * ff, uint16_t total_bytes);
|
||||
|
||||
// Claim an endpoint before submitting a transfer.
|
||||
// If caller does not make any transfer, it must release endpoint for others.
|
||||
bool usbd_edpt_claim(uint8_t rhport, uint8_t ep_addr);
|
||||
|
||||
// Release claimed endpoint without submitting a transfer
|
||||
bool usbd_edpt_release(uint8_t rhport, uint8_t ep_addr);
|
||||
|
||||
// Check if endpoint is busy transferring
|
||||
bool usbd_edpt_busy(uint8_t rhport, uint8_t ep_addr);
|
||||
|
||||
// Stall endpoint
|
||||
void usbd_edpt_stall(uint8_t rhport, uint8_t ep_addr);
|
||||
|
||||
// Clear stalled endpoint
|
||||
void usbd_edpt_clear_stall(uint8_t rhport, uint8_t ep_addr);
|
||||
|
||||
// Check if endpoint is stalled
|
||||
bool usbd_edpt_stalled(uint8_t rhport, uint8_t ep_addr);
|
||||
|
||||
// Allocate packet buffer used by ISO endpoints
|
||||
bool usbd_edpt_iso_alloc(uint8_t rhport, uint8_t ep_addr, uint16_t largest_packet_size);
|
||||
|
||||
// Configure and enable an ISO endpoint according to descriptor
|
||||
bool usbd_edpt_iso_activate(uint8_t rhport, tusb_desc_endpoint_t const * p_endpoint_desc);
|
||||
|
||||
// Check if endpoint is ready (not busy and not stalled)
|
||||
TU_ATTR_ALWAYS_INLINE static inline
|
||||
bool usbd_edpt_ready(uint8_t rhport, uint8_t ep_addr) {
|
||||
return !usbd_edpt_busy(rhport, ep_addr) && !usbd_edpt_stalled(rhport, ep_addr);
|
||||
}
|
||||
|
||||
// Enable SOF interrupt
|
||||
void usbd_sof_enable(uint8_t rhport, bool en);
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* Helper
|
||||
*------------------------------------------------------------------*/
|
||||
|
||||
bool usbd_open_edpt_pair(uint8_t rhport, uint8_t const* p_desc, uint8_t ep_count, uint8_t xfer_type, uint8_t* ep_out, uint8_t* ep_in);
|
||||
void usbd_defer_func(osal_task_func_t func, void *param, bool in_isr);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -0,0 +1,242 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#ifndef _TUSB_HCD_H_
|
||||
#define _TUSB_HCD_H_
|
||||
|
||||
#include "common/tusb_common.h"
|
||||
#include "osal/osal.h"
|
||||
#include "common/tusb_fifo.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Configuration
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Max number of endpoints pair per device
|
||||
// TODO optimize memory usage
|
||||
#ifndef CFG_TUH_ENDPOINT_MAX
|
||||
#define CFG_TUH_ENDPOINT_MAX 16
|
||||
// #ifdef TUP_HCD_ENDPOINT_MAX
|
||||
// #define CFG_TUH_ENDPPOINT_MAX TUP_HCD_ENDPOINT_MAX
|
||||
// #else
|
||||
// #define
|
||||
// #endif
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// MACRO CONSTANT TYPEDEF
|
||||
//--------------------------------------------------------------------+
|
||||
typedef enum
|
||||
{
|
||||
HCD_EVENT_DEVICE_ATTACH,
|
||||
HCD_EVENT_DEVICE_REMOVE,
|
||||
HCD_EVENT_XFER_COMPLETE,
|
||||
|
||||
// Not an HCD event, just a convenient way to defer ISR function
|
||||
USBH_EVENT_FUNC_CALL,
|
||||
|
||||
HCD_EVENT_COUNT
|
||||
} hcd_eventid_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t rhport;
|
||||
uint8_t event_id;
|
||||
uint8_t dev_addr;
|
||||
|
||||
union
|
||||
{
|
||||
// Attach, Remove
|
||||
struct {
|
||||
uint8_t hub_addr;
|
||||
uint8_t hub_port;
|
||||
uint8_t speed;
|
||||
} connection;
|
||||
|
||||
// XFER_COMPLETE
|
||||
struct {
|
||||
uint8_t ep_addr;
|
||||
uint8_t result;
|
||||
uint32_t len;
|
||||
} xfer_complete;
|
||||
|
||||
// FUNC_CALL
|
||||
struct {
|
||||
void (*func) (void*);
|
||||
void* param;
|
||||
}func_call;
|
||||
};
|
||||
|
||||
} hcd_event_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t rhport;
|
||||
uint8_t hub_addr;
|
||||
uint8_t hub_port;
|
||||
uint8_t speed;
|
||||
} hcd_devtree_info_t;
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Memory API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// clean/flush data cache: write cache -> memory.
|
||||
// Required before an DMA TX transfer to make sure data is in memory
|
||||
bool hcd_dcache_clean(void const* addr, uint32_t data_size) TU_ATTR_WEAK;
|
||||
|
||||
// invalidate data cache: mark cache as invalid, next read will read from memory
|
||||
// Required BOTH before and after an DMA RX transfer
|
||||
bool hcd_dcache_invalidate(void const* addr, uint32_t data_size) TU_ATTR_WEAK;
|
||||
|
||||
// clean and invalidate data cache
|
||||
// Required before an DMA transfer where memory is both read/write by DMA
|
||||
bool hcd_dcache_clean_invalidate(void const* addr, uint32_t data_size) TU_ATTR_WEAK;
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Controller API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// optional hcd configuration, called by tuh_configure()
|
||||
bool hcd_configure(uint8_t rhport, uint32_t cfg_id, const void* cfg_param) TU_ATTR_WEAK;
|
||||
|
||||
// Initialize controller to host mode
|
||||
bool hcd_init(uint8_t rhport);
|
||||
|
||||
// Interrupt Handler
|
||||
void hcd_int_handler(uint8_t rhport, bool in_isr);
|
||||
|
||||
// Enable USB interrupt
|
||||
void hcd_int_enable (uint8_t rhport);
|
||||
|
||||
// Disable USB interrupt
|
||||
void hcd_int_disable(uint8_t rhport);
|
||||
|
||||
// Get frame number (1ms)
|
||||
uint32_t hcd_frame_number(uint8_t rhport);
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Port API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Get the current connect status of roothub port
|
||||
bool hcd_port_connect_status(uint8_t rhport);
|
||||
|
||||
// Reset USB bus on the port. Return immediately, bus reset sequence may not be complete.
|
||||
// Some port would require hcd_port_reset_end() to be invoked after 10ms to complete the reset sequence.
|
||||
void hcd_port_reset(uint8_t rhport);
|
||||
|
||||
// Complete bus reset sequence, may be required by some controllers
|
||||
void hcd_port_reset_end(uint8_t rhport);
|
||||
|
||||
// Get port link speed
|
||||
tusb_speed_t hcd_port_speed_get(uint8_t rhport);
|
||||
|
||||
// HCD closes all opened endpoints belong to this device
|
||||
void hcd_device_close(uint8_t rhport, uint8_t dev_addr);
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Endpoints API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Open an endpoint
|
||||
bool hcd_edpt_open(uint8_t rhport, uint8_t daddr, tusb_desc_endpoint_t const * ep_desc);
|
||||
|
||||
// Submit a transfer, when complete hcd_event_xfer_complete() must be invoked
|
||||
bool hcd_edpt_xfer(uint8_t rhport, uint8_t daddr, uint8_t ep_addr, uint8_t * buffer, uint16_t buflen);
|
||||
|
||||
// Abort a queued transfer. Note: it can only abort transfer that has not been started
|
||||
// Return true if a queued transfer is aborted, false if there is no transfer to abort
|
||||
bool hcd_edpt_abort_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr);
|
||||
|
||||
// Submit a special transfer to send 8-byte Setup Packet, when complete hcd_event_xfer_complete() must be invoked
|
||||
bool hcd_setup_send(uint8_t rhport, uint8_t daddr, uint8_t const setup_packet[8]);
|
||||
|
||||
// clear stall, data toggle is also reset to DATA0
|
||||
bool hcd_edpt_clear_stall(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr);
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// USBH implemented API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Get device tree information of a device
|
||||
// USB device tree can be complicated and manged by USBH, this help HCD to retrieve
|
||||
// needed topology info to carry out its work
|
||||
extern void hcd_devtree_get_info(uint8_t dev_addr, hcd_devtree_info_t* devtree_info);
|
||||
|
||||
//------------- Event API -------------//
|
||||
|
||||
// Called by HCD to notify stack
|
||||
extern void hcd_event_handler(hcd_event_t const* event, bool in_isr);
|
||||
|
||||
// Helper to send device attach event
|
||||
TU_ATTR_ALWAYS_INLINE static inline
|
||||
void hcd_event_device_attach(uint8_t rhport, bool in_isr) {
|
||||
hcd_event_t event;
|
||||
event.rhport = rhport;
|
||||
event.event_id = HCD_EVENT_DEVICE_ATTACH;
|
||||
event.connection.hub_addr = 0;
|
||||
event.connection.hub_port = 0;
|
||||
|
||||
hcd_event_handler(&event, in_isr);
|
||||
}
|
||||
|
||||
// Helper to send device removal event
|
||||
TU_ATTR_ALWAYS_INLINE static inline
|
||||
void hcd_event_device_remove(uint8_t rhport, bool in_isr) {
|
||||
hcd_event_t event;
|
||||
event.rhport = rhport;
|
||||
event.event_id = HCD_EVENT_DEVICE_REMOVE;
|
||||
event.connection.hub_addr = 0;
|
||||
event.connection.hub_port = 0;
|
||||
|
||||
hcd_event_handler(&event, in_isr);
|
||||
}
|
||||
|
||||
// Helper to send USB transfer event
|
||||
TU_ATTR_ALWAYS_INLINE static inline
|
||||
void hcd_event_xfer_complete(uint8_t dev_addr, uint8_t ep_addr, uint32_t xferred_bytes, xfer_result_t result, bool in_isr) {
|
||||
hcd_event_t event = {
|
||||
.rhport = 0, // TODO correct rhport
|
||||
.event_id = HCD_EVENT_XFER_COMPLETE,
|
||||
.dev_addr = dev_addr,
|
||||
};
|
||||
event.xfer_complete.ep_addr = ep_addr;
|
||||
event.xfer_complete.result = result;
|
||||
event.xfer_complete.len = xferred_bytes;
|
||||
|
||||
hcd_event_handler(&event, in_isr);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _TUSB_HCD_H_ */
|
|
@ -0,0 +1,500 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#include "tusb_option.h"
|
||||
|
||||
#if (CFG_TUH_ENABLED && CFG_TUH_HUB)
|
||||
|
||||
#include "hcd.h"
|
||||
#include "usbh.h"
|
||||
#include "usbh_pvt.h"
|
||||
#include "hub.h"
|
||||
|
||||
// Debug level, TUSB_CFG_DEBUG must be at least this level for debug message
|
||||
#define HUB_DEBUG 2
|
||||
#define TU_LOG_DRV(...) TU_LOG(HUB_DEBUG, __VA_ARGS__)
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// MACRO CONSTANT TYPEDEF
|
||||
//--------------------------------------------------------------------+
|
||||
typedef struct
|
||||
{
|
||||
uint8_t itf_num;
|
||||
uint8_t ep_in;
|
||||
uint8_t port_count;
|
||||
|
||||
CFG_TUH_MEM_ALIGN uint8_t status_change;
|
||||
CFG_TUH_MEM_ALIGN hub_port_status_response_t port_status;
|
||||
CFG_TUH_MEM_ALIGN hub_status_response_t hub_status;
|
||||
} hub_interface_t;
|
||||
|
||||
CFG_TUH_MEM_SECTION static hub_interface_t hub_data[CFG_TUH_HUB];
|
||||
CFG_TUH_MEM_SECTION CFG_TUH_MEM_ALIGN static uint8_t _hub_buffer[sizeof(descriptor_hub_desc_t)];
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE
|
||||
static inline hub_interface_t* get_itf(uint8_t dev_addr)
|
||||
{
|
||||
return &hub_data[dev_addr-1-CFG_TUH_DEVICE_MAX];
|
||||
}
|
||||
|
||||
#if CFG_TUSB_DEBUG >= 2
|
||||
static char const* const _hub_feature_str[] =
|
||||
{
|
||||
[HUB_FEATURE_PORT_CONNECTION ] = "PORT_CONNECTION",
|
||||
[HUB_FEATURE_PORT_ENABLE ] = "PORT_ENABLE",
|
||||
[HUB_FEATURE_PORT_SUSPEND ] = "PORT_SUSPEND",
|
||||
[HUB_FEATURE_PORT_OVER_CURRENT ] = "PORT_OVER_CURRENT",
|
||||
[HUB_FEATURE_PORT_RESET ] = "PORT_RESET",
|
||||
[HUB_FEATURE_PORT_POWER ] = "PORT_POWER",
|
||||
[HUB_FEATURE_PORT_LOW_SPEED ] = "PORT_LOW_SPEED",
|
||||
[HUB_FEATURE_PORT_CONNECTION_CHANGE ] = "PORT_CONNECTION_CHANGE",
|
||||
[HUB_FEATURE_PORT_ENABLE_CHANGE ] = "PORT_ENABLE_CHANGE",
|
||||
[HUB_FEATURE_PORT_SUSPEND_CHANGE ] = "PORT_SUSPEND_CHANGE",
|
||||
[HUB_FEATURE_PORT_OVER_CURRENT_CHANGE ] = "PORT_OVER_CURRENT_CHANGE",
|
||||
[HUB_FEATURE_PORT_RESET_CHANGE ] = "PORT_RESET_CHANGE",
|
||||
[HUB_FEATURE_PORT_TEST ] = "PORT_TEST",
|
||||
[HUB_FEATURE_PORT_INDICATOR ] = "PORT_INDICATOR",
|
||||
};
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// HUB
|
||||
//--------------------------------------------------------------------+
|
||||
bool hub_port_clear_feature(uint8_t hub_addr, uint8_t hub_port, uint8_t feature,
|
||||
tuh_xfer_cb_t complete_cb, uintptr_t user_data)
|
||||
{
|
||||
tusb_control_request_t const request =
|
||||
{
|
||||
.bmRequestType_bit =
|
||||
{
|
||||
.recipient = (hub_port == 0) ? TUSB_REQ_RCPT_DEVICE : TUSB_REQ_RCPT_OTHER,
|
||||
.type = TUSB_REQ_TYPE_CLASS,
|
||||
.direction = TUSB_DIR_OUT
|
||||
},
|
||||
.bRequest = HUB_REQUEST_CLEAR_FEATURE,
|
||||
.wValue = feature,
|
||||
.wIndex = hub_port,
|
||||
.wLength = 0
|
||||
};
|
||||
|
||||
tuh_xfer_t xfer =
|
||||
{
|
||||
.daddr = hub_addr,
|
||||
.ep_addr = 0,
|
||||
.setup = &request,
|
||||
.buffer = NULL,
|
||||
.complete_cb = complete_cb,
|
||||
.user_data = user_data
|
||||
};
|
||||
|
||||
TU_LOG2("HUB Clear Feature: %s, addr = %u port = %u\r\n", _hub_feature_str[feature], hub_addr, hub_port);
|
||||
TU_ASSERT( tuh_control_xfer(&xfer) );
|
||||
return true;
|
||||
}
|
||||
|
||||
bool hub_port_set_feature(uint8_t hub_addr, uint8_t hub_port, uint8_t feature,
|
||||
tuh_xfer_cb_t complete_cb, uintptr_t user_data)
|
||||
{
|
||||
tusb_control_request_t const request =
|
||||
{
|
||||
.bmRequestType_bit =
|
||||
{
|
||||
.recipient = (hub_port == 0) ? TUSB_REQ_RCPT_DEVICE : TUSB_REQ_RCPT_OTHER,
|
||||
.type = TUSB_REQ_TYPE_CLASS,
|
||||
.direction = TUSB_DIR_OUT
|
||||
},
|
||||
.bRequest = HUB_REQUEST_SET_FEATURE,
|
||||
.wValue = feature,
|
||||
.wIndex = hub_port,
|
||||
.wLength = 0
|
||||
};
|
||||
|
||||
tuh_xfer_t xfer =
|
||||
{
|
||||
.daddr = hub_addr,
|
||||
.ep_addr = 0,
|
||||
.setup = &request,
|
||||
.buffer = NULL,
|
||||
.complete_cb = complete_cb,
|
||||
.user_data = user_data
|
||||
};
|
||||
|
||||
TU_LOG2("HUB Set Feature: %s, addr = %u port = %u\r\n", _hub_feature_str[feature], hub_addr, hub_port);
|
||||
TU_ASSERT( tuh_control_xfer(&xfer) );
|
||||
return true;
|
||||
}
|
||||
|
||||
bool hub_port_get_status(uint8_t hub_addr, uint8_t hub_port, void* resp,
|
||||
tuh_xfer_cb_t complete_cb, uintptr_t user_data)
|
||||
{
|
||||
tusb_control_request_t const request =
|
||||
{
|
||||
.bmRequestType_bit =
|
||||
{
|
||||
.recipient = (hub_port == 0) ? TUSB_REQ_RCPT_DEVICE : TUSB_REQ_RCPT_OTHER,
|
||||
.type = TUSB_REQ_TYPE_CLASS,
|
||||
.direction = TUSB_DIR_IN
|
||||
},
|
||||
.bRequest = HUB_REQUEST_GET_STATUS,
|
||||
.wValue = 0,
|
||||
.wIndex = hub_port,
|
||||
.wLength = 4
|
||||
};
|
||||
|
||||
tuh_xfer_t xfer =
|
||||
{
|
||||
.daddr = hub_addr,
|
||||
.ep_addr = 0,
|
||||
.setup = &request,
|
||||
.buffer = resp,
|
||||
.complete_cb = complete_cb,
|
||||
.user_data = user_data
|
||||
};
|
||||
|
||||
TU_LOG2("HUB Get Port Status: addr = %u port = %u\r\n", hub_addr, hub_port);
|
||||
TU_VERIFY( tuh_control_xfer(&xfer) );
|
||||
return true;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// CLASS-USBH API (don't require to verify parameters)
|
||||
//--------------------------------------------------------------------+
|
||||
void hub_init(void)
|
||||
{
|
||||
tu_memclr(hub_data, sizeof(hub_data));
|
||||
}
|
||||
|
||||
bool hub_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *itf_desc, uint16_t max_len)
|
||||
{
|
||||
(void) rhport;
|
||||
|
||||
TU_VERIFY(TUSB_CLASS_HUB == itf_desc->bInterfaceClass &&
|
||||
0 == itf_desc->bInterfaceSubClass);
|
||||
|
||||
// hub driver does not support multiple TT yet
|
||||
TU_VERIFY(itf_desc->bInterfaceProtocol <= 1);
|
||||
|
||||
// msc driver length is fixed
|
||||
uint16_t const drv_len = sizeof(tusb_desc_interface_t) + sizeof(tusb_desc_endpoint_t);
|
||||
TU_ASSERT(drv_len <= max_len);
|
||||
|
||||
//------------- Interrupt Status endpoint -------------//
|
||||
tusb_desc_endpoint_t const *desc_ep = (tusb_desc_endpoint_t const *) tu_desc_next(itf_desc);
|
||||
|
||||
TU_ASSERT(TUSB_DESC_ENDPOINT == desc_ep->bDescriptorType &&
|
||||
TUSB_XFER_INTERRUPT == desc_ep->bmAttributes.xfer, 0);
|
||||
|
||||
TU_ASSERT(tuh_edpt_open(dev_addr, desc_ep));
|
||||
|
||||
hub_interface_t* p_hub = get_itf(dev_addr);
|
||||
|
||||
p_hub->itf_num = itf_desc->bInterfaceNumber;
|
||||
p_hub->ep_in = desc_ep->bEndpointAddress;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void hub_close(uint8_t dev_addr)
|
||||
{
|
||||
TU_VERIFY(dev_addr > CFG_TUH_DEVICE_MAX, );
|
||||
hub_interface_t* p_hub = get_itf(dev_addr);
|
||||
|
||||
if (p_hub->ep_in) {
|
||||
TU_LOG_DRV(" HUB close addr = %d\r\n", dev_addr);
|
||||
tu_memclr(p_hub, sizeof( hub_interface_t));
|
||||
}
|
||||
}
|
||||
|
||||
bool hub_edpt_status_xfer(uint8_t dev_addr)
|
||||
{
|
||||
hub_interface_t* hub_itf = get_itf(dev_addr);
|
||||
return usbh_edpt_xfer(dev_addr, hub_itf->ep_in, &hub_itf->status_change, 1);
|
||||
}
|
||||
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Set Configure
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
static void config_set_port_power (tuh_xfer_t* xfer);
|
||||
static void config_port_power_complete (tuh_xfer_t* xfer);
|
||||
|
||||
bool hub_set_config(uint8_t dev_addr, uint8_t itf_num)
|
||||
{
|
||||
hub_interface_t* p_hub = get_itf(dev_addr);
|
||||
TU_ASSERT(itf_num == p_hub->itf_num);
|
||||
|
||||
// Get Hub Descriptor
|
||||
tusb_control_request_t const request =
|
||||
{
|
||||
.bmRequestType_bit =
|
||||
{
|
||||
.recipient = TUSB_REQ_RCPT_DEVICE,
|
||||
.type = TUSB_REQ_TYPE_CLASS,
|
||||
.direction = TUSB_DIR_IN
|
||||
},
|
||||
.bRequest = HUB_REQUEST_GET_DESCRIPTOR,
|
||||
.wValue = 0,
|
||||
.wIndex = 0,
|
||||
.wLength = sizeof(descriptor_hub_desc_t)
|
||||
};
|
||||
|
||||
tuh_xfer_t xfer =
|
||||
{
|
||||
.daddr = dev_addr,
|
||||
.ep_addr = 0,
|
||||
.setup = &request,
|
||||
.buffer = _hub_buffer,
|
||||
.complete_cb = config_set_port_power,
|
||||
.user_data = 0
|
||||
};
|
||||
|
||||
TU_ASSERT( tuh_control_xfer(&xfer) );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static void config_set_port_power (tuh_xfer_t* xfer)
|
||||
{
|
||||
TU_ASSERT(XFER_RESULT_SUCCESS == xfer->result, );
|
||||
|
||||
uint8_t const daddr = xfer->daddr;
|
||||
hub_interface_t* p_hub = get_itf(daddr);
|
||||
|
||||
// only use number of ports in hub descriptor
|
||||
descriptor_hub_desc_t const* desc_hub = (descriptor_hub_desc_t const*) _hub_buffer;
|
||||
p_hub->port_count = desc_hub->bNbrPorts;
|
||||
|
||||
// May need to GET_STATUS
|
||||
|
||||
// Set Port Power to be able to detect connection, starting with port 1
|
||||
uint8_t const hub_port = 1;
|
||||
hub_port_set_feature(daddr, hub_port, HUB_FEATURE_PORT_POWER, config_port_power_complete, 0);
|
||||
}
|
||||
|
||||
static void config_port_power_complete (tuh_xfer_t* xfer)
|
||||
{
|
||||
TU_ASSERT(XFER_RESULT_SUCCESS == xfer->result, );
|
||||
|
||||
uint8_t const daddr = xfer->daddr;
|
||||
hub_interface_t* p_hub = get_itf(daddr);
|
||||
|
||||
if (xfer->setup->wIndex == p_hub->port_count)
|
||||
{
|
||||
// All ports are power -> queue notification status endpoint and
|
||||
// complete the SET CONFIGURATION
|
||||
TU_ASSERT( usbh_edpt_xfer(daddr, p_hub->ep_in, &p_hub->status_change, 1), );
|
||||
|
||||
usbh_driver_set_config_complete(daddr, p_hub->itf_num);
|
||||
}else
|
||||
{
|
||||
// power next port
|
||||
uint8_t const hub_port = (uint8_t) (xfer->setup->wIndex + 1);
|
||||
hub_port_set_feature(daddr, hub_port, HUB_FEATURE_PORT_POWER, config_port_power_complete, 0);
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Connection Changes
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
static void hub_port_get_status_complete (tuh_xfer_t* xfer);
|
||||
static void hub_get_status_complete (tuh_xfer_t* xfer);
|
||||
static void connection_clear_conn_change_complete (tuh_xfer_t* xfer);
|
||||
static void connection_port_reset_complete (tuh_xfer_t* xfer);
|
||||
|
||||
// callback as response of interrupt endpoint polling
|
||||
bool hub_xfer_cb(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) {
|
||||
(void) xferred_bytes; // TODO can be more than 1 for hub with lots of ports
|
||||
(void) ep_addr;
|
||||
TU_VERIFY(result == XFER_RESULT_SUCCESS);
|
||||
|
||||
hub_interface_t* p_hub = get_itf(dev_addr);
|
||||
|
||||
uint8_t const status_change = p_hub->status_change;
|
||||
TU_LOG2(" Hub Status Change = 0x%02X\r\n", status_change);
|
||||
|
||||
if ( status_change == 0 ) {
|
||||
// The status change event was neither for the hub, nor for any of its ports.
|
||||
// This shouldn't happen, but it does with some devices.
|
||||
// Initiate the next interrupt poll here.
|
||||
return hub_edpt_status_xfer(dev_addr);
|
||||
}
|
||||
|
||||
if (tu_bit_test(status_change, 0)) {
|
||||
// Hub bit 0 is for the hub device events
|
||||
if (hub_port_get_status(dev_addr, 0, &p_hub->hub_status, hub_get_status_complete, 0) == false) {
|
||||
//Hub status control transfer failed, retry
|
||||
hub_edpt_status_xfer(dev_addr);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// Hub bits 1 to n are hub port events
|
||||
for (uint8_t port=1; port <= p_hub->port_count; port++) {
|
||||
if ( tu_bit_test(status_change, port) ) {
|
||||
if (hub_port_get_status(dev_addr, port, &p_hub->port_status, hub_port_get_status_complete, 0) == false) {
|
||||
//Hub status control transfer failed, retry
|
||||
hub_edpt_status_xfer(dev_addr);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// NOTE: next status transfer is queued by usbh.c after handling this request
|
||||
return true;
|
||||
}
|
||||
|
||||
static void hub_clear_feature_complete_stub(tuh_xfer_t* xfer)
|
||||
{
|
||||
TU_ASSERT(xfer->result == XFER_RESULT_SUCCESS, );
|
||||
hub_edpt_status_xfer(xfer->daddr);
|
||||
}
|
||||
|
||||
static void hub_get_status_complete (tuh_xfer_t* xfer)
|
||||
{
|
||||
TU_ASSERT(xfer->result == XFER_RESULT_SUCCESS, );
|
||||
|
||||
uint8_t const daddr = xfer->daddr;
|
||||
hub_interface_t* p_hub = get_itf(daddr);
|
||||
uint8_t const port_num = (uint8_t) tu_le16toh(xfer->setup->wIndex);
|
||||
TU_ASSERT(port_num == 0 , );
|
||||
|
||||
TU_LOG2("HUB Got hub status, addr = %u, status = %04x\r\n", daddr, p_hub->hub_status.change.value);
|
||||
|
||||
if (p_hub->hub_status.change.local_power_source)
|
||||
{
|
||||
TU_LOG2("HUB Local Power Change, addr = %u\r\n", daddr);
|
||||
hub_port_clear_feature(daddr, port_num, HUB_FEATURE_HUB_LOCAL_POWER_CHANGE, hub_clear_feature_complete_stub, 0);
|
||||
}
|
||||
else if (p_hub->hub_status.change.over_current)
|
||||
{
|
||||
TU_LOG1("HUB Over Current, addr = %u\r\n", daddr);
|
||||
hub_port_clear_feature(daddr, port_num, HUB_FEATURE_HUB_OVER_CURRENT_CHANGE, hub_clear_feature_complete_stub, 0);
|
||||
}
|
||||
}
|
||||
|
||||
static void hub_port_get_status_complete (tuh_xfer_t* xfer)
|
||||
{
|
||||
TU_ASSERT(xfer->result == XFER_RESULT_SUCCESS, );
|
||||
|
||||
uint8_t const daddr = xfer->daddr;
|
||||
hub_interface_t* p_hub = get_itf(daddr);
|
||||
uint8_t const port_num = (uint8_t) tu_le16toh(xfer->setup->wIndex);
|
||||
|
||||
// Connection change
|
||||
if (p_hub->port_status.change.connection)
|
||||
{
|
||||
// Port is powered and enabled
|
||||
//TU_VERIFY(port_status.status_current.port_power && port_status.status_current.port_enable, );
|
||||
|
||||
// Acknowledge Port Connection Change
|
||||
hub_port_clear_feature(daddr, port_num, HUB_FEATURE_PORT_CONNECTION_CHANGE, connection_clear_conn_change_complete, 0);
|
||||
}else
|
||||
{
|
||||
// Clear other port status change interrupts. TODO Not currently handled - just cleared.
|
||||
if (p_hub->port_status.change.port_enable)
|
||||
{
|
||||
hub_port_clear_feature(daddr, port_num, HUB_FEATURE_PORT_ENABLE_CHANGE, hub_clear_feature_complete_stub, 0);
|
||||
}
|
||||
else if (p_hub->port_status.change.suspend)
|
||||
{
|
||||
hub_port_clear_feature(daddr, port_num, HUB_FEATURE_PORT_SUSPEND_CHANGE, hub_clear_feature_complete_stub, 0);
|
||||
}
|
||||
else if (p_hub->port_status.change.over_current)
|
||||
{
|
||||
hub_port_clear_feature(daddr, port_num, HUB_FEATURE_PORT_OVER_CURRENT_CHANGE, hub_clear_feature_complete_stub, 0);
|
||||
}
|
||||
else if (p_hub->port_status.change.reset)
|
||||
{
|
||||
hub_port_clear_feature(daddr, port_num, HUB_FEATURE_PORT_RESET_CHANGE, hub_clear_feature_complete_stub, 0);
|
||||
}
|
||||
// Other changes are: L1 state
|
||||
// TODO clear change
|
||||
|
||||
else
|
||||
{
|
||||
// prepare for next hub status
|
||||
// TODO continue with status_change, or maybe we can do it again with status
|
||||
hub_edpt_status_xfer(daddr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void connection_clear_conn_change_complete (tuh_xfer_t* xfer)
|
||||
{
|
||||
TU_ASSERT(xfer->result == XFER_RESULT_SUCCESS, );
|
||||
|
||||
uint8_t const daddr = xfer->daddr;
|
||||
hub_interface_t* p_hub = get_itf(daddr);
|
||||
uint8_t const port_num = (uint8_t) tu_le16toh(xfer->setup->wIndex);
|
||||
|
||||
if ( p_hub->port_status.status.connection )
|
||||
{
|
||||
// Reset port if attach event
|
||||
hub_port_reset(daddr, port_num, connection_port_reset_complete, 0);
|
||||
}else
|
||||
{
|
||||
// submit detach event
|
||||
hcd_event_t event =
|
||||
{
|
||||
.rhport = usbh_get_rhport(daddr),
|
||||
.event_id = HCD_EVENT_DEVICE_REMOVE,
|
||||
.connection =
|
||||
{
|
||||
.hub_addr = daddr,
|
||||
.hub_port = port_num
|
||||
}
|
||||
};
|
||||
|
||||
hcd_event_handler(&event, false);
|
||||
}
|
||||
}
|
||||
|
||||
static void connection_port_reset_complete (tuh_xfer_t* xfer)
|
||||
{
|
||||
TU_ASSERT(xfer->result == XFER_RESULT_SUCCESS, );
|
||||
|
||||
uint8_t const daddr = xfer->daddr;
|
||||
// hub_interface_t* p_hub = get_itf(daddr);
|
||||
uint8_t const port_num = (uint8_t) tu_le16toh(xfer->setup->wIndex);
|
||||
|
||||
// submit attach event
|
||||
hcd_event_t event =
|
||||
{
|
||||
.rhport = usbh_get_rhport(daddr),
|
||||
.event_id = HCD_EVENT_DEVICE_ATTACH,
|
||||
.connection =
|
||||
{
|
||||
.hub_addr = daddr,
|
||||
.hub_port = port_num
|
||||
}
|
||||
};
|
||||
|
||||
hcd_event_handler(&event, false);
|
||||
}
|
||||
|
||||
#endif
|
|
@ -0,0 +1,219 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
/** \ingroup group_class
|
||||
* \defgroup ClassDriver_Hub Hub (Host only)
|
||||
* \details Like most PC's OS, Hub support is completely hidden from Application. In fact, application cannot determine whether
|
||||
* a device is mounted directly via roothub or via a hub's port. All Hub-related procedures are performed and managed
|
||||
* by tinyusb stack. Unless you are trying to develop the stack itself, there are nothing else can be used by Application.
|
||||
* \note Due to my laziness, only 1-level of Hub is supported. In other way, the stack cannot mount a hub via another hub.
|
||||
* @{
|
||||
*/
|
||||
|
||||
#ifndef _TUSB_HUB_H_
|
||||
#define _TUSB_HUB_H_
|
||||
|
||||
#include "common/tusb_common.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
//D1...D0: Logical Power Switching Mode
|
||||
//00: Ganged power switching (all ports’power at
|
||||
//once)
|
||||
//01: Individual port power switching
|
||||
//1X: Reserved. Used only on 1.0 compliant hubs
|
||||
//that implement no power switching
|
||||
//D2: Identifies a Compound Device
|
||||
//0: Hub is not part of a compound device.
|
||||
//1: Hub is part of a compound device.
|
||||
//D4...D3: Over-current Protection Mode
|
||||
//00: Global Over-current Protection. The hub
|
||||
//reports over-current as a summation of all
|
||||
//ports’current draw, without a breakdown of
|
||||
//individual port over-current status.
|
||||
//01: Individual Port Over-current Protection. The
|
||||
//hub reports over-current on a per-port basis.
|
||||
//Each port has an over-current status.
|
||||
//1X: No Over-current Protection. This option is
|
||||
//allowed only for bus-powered hubs that do not
|
||||
//implement over-current protection.
|
||||
//
|
||||
//D6...D5: TT Think TIme
|
||||
//00: TT requires at most 8 FS bit times of inter
|
||||
//transaction gap on a full-/low-speed
|
||||
//downstream bus.
|
||||
//01: TT requires at most 16 FS bit times.
|
||||
//10: TT requires at most 24 FS bit times.
|
||||
//11: TT requires at most 32 FS bit times.
|
||||
//D7: Port Indicators Supported
|
||||
//0: Port Indicators are not supported on its
|
||||
//downstream facing ports and the
|
||||
//PORT_INDICATOR request has no effect.
|
||||
//1: Port Indicators are supported on its
|
||||
//downstream facing ports and the
|
||||
//PORT_INDICATOR request controls the
|
||||
//indicators. See Section 11.5.3.
|
||||
//D15...D8: Reserved
|
||||
|
||||
typedef struct TU_ATTR_PACKED{
|
||||
uint8_t bLength ; ///< Size of descriptor
|
||||
uint8_t bDescriptorType ; ///< Other_speed_Configuration Type
|
||||
uint8_t bNbrPorts;
|
||||
uint16_t wHubCharacteristics;
|
||||
uint8_t bPwrOn2PwrGood;
|
||||
uint8_t bHubContrCurrent;
|
||||
uint8_t DeviceRemovable; // bitmap each bit for a port (from bit1)
|
||||
uint8_t PortPwrCtrlMask; // just for compatibility, should be 0xff
|
||||
} descriptor_hub_desc_t;
|
||||
|
||||
TU_VERIFY_STATIC( sizeof(descriptor_hub_desc_t) == 9, "size is not correct");
|
||||
|
||||
enum {
|
||||
HUB_REQUEST_GET_STATUS = 0 ,
|
||||
HUB_REQUEST_CLEAR_FEATURE = 1 ,
|
||||
|
||||
HUB_REQUEST_SET_FEATURE = 3 ,
|
||||
|
||||
HUB_REQUEST_GET_DESCRIPTOR = 6 ,
|
||||
HUB_REQUEST_SET_DESCRIPTOR = 7 ,
|
||||
HUB_REQUEST_CLEAR_TT_BUFFER = 8 ,
|
||||
HUB_REQUEST_RESET_TT = 9 ,
|
||||
HUB_REQUEST_GET_TT_STATE = 10 ,
|
||||
HUB_REQUEST_STOP_TT = 11
|
||||
};
|
||||
|
||||
enum {
|
||||
HUB_FEATURE_HUB_LOCAL_POWER_CHANGE = 0,
|
||||
HUB_FEATURE_HUB_OVER_CURRENT_CHANGE
|
||||
};
|
||||
|
||||
enum{
|
||||
HUB_FEATURE_PORT_CONNECTION = 0,
|
||||
HUB_FEATURE_PORT_ENABLE = 1,
|
||||
HUB_FEATURE_PORT_SUSPEND = 2,
|
||||
HUB_FEATURE_PORT_OVER_CURRENT = 3,
|
||||
HUB_FEATURE_PORT_RESET = 4,
|
||||
|
||||
HUB_FEATURE_PORT_POWER = 8,
|
||||
HUB_FEATURE_PORT_LOW_SPEED = 9,
|
||||
|
||||
HUB_FEATURE_PORT_CONNECTION_CHANGE = 16,
|
||||
HUB_FEATURE_PORT_ENABLE_CHANGE = 17,
|
||||
HUB_FEATURE_PORT_SUSPEND_CHANGE = 18,
|
||||
HUB_FEATURE_PORT_OVER_CURRENT_CHANGE = 19,
|
||||
HUB_FEATURE_PORT_RESET_CHANGE = 20,
|
||||
HUB_FEATURE_PORT_TEST = 21,
|
||||
HUB_FEATURE_PORT_INDICATOR = 22
|
||||
};
|
||||
|
||||
// data in response of HUB_REQUEST_GET_STATUS, wIndex = 0 (hub)
|
||||
typedef struct {
|
||||
union{
|
||||
struct TU_ATTR_PACKED {
|
||||
uint16_t local_power_source : 1;
|
||||
uint16_t over_current : 1;
|
||||
uint16_t : 14;
|
||||
};
|
||||
|
||||
uint16_t value;
|
||||
} status, change;
|
||||
} hub_status_response_t;
|
||||
|
||||
TU_VERIFY_STATIC( sizeof(hub_status_response_t) == 4, "size is not correct");
|
||||
|
||||
// data in response of HUB_REQUEST_GET_STATUS, wIndex = Port num
|
||||
typedef struct {
|
||||
union {
|
||||
struct TU_ATTR_PACKED {
|
||||
uint16_t connection : 1;
|
||||
uint16_t port_enable : 1;
|
||||
uint16_t suspend : 1;
|
||||
uint16_t over_current : 1;
|
||||
uint16_t reset : 1;
|
||||
|
||||
uint16_t : 3;
|
||||
uint16_t port_power : 1;
|
||||
uint16_t low_speed : 1;
|
||||
uint16_t high_speed : 1;
|
||||
uint16_t port_test_mode : 1;
|
||||
uint16_t port_indicator_control : 1;
|
||||
uint16_t TU_RESERVED : 3;
|
||||
};
|
||||
|
||||
uint16_t value;
|
||||
} status, change;
|
||||
} hub_port_status_response_t;
|
||||
|
||||
TU_VERIFY_STATIC( sizeof(hub_port_status_response_t) == 4, "size is not correct");
|
||||
|
||||
// Clear feature
|
||||
bool hub_port_clear_feature (uint8_t hub_addr, uint8_t hub_port, uint8_t feature,
|
||||
tuh_xfer_cb_t complete_cb, uintptr_t user_data);
|
||||
|
||||
// Set feature
|
||||
bool hub_port_set_feature (uint8_t hub_addr, uint8_t hub_port, uint8_t feature,
|
||||
tuh_xfer_cb_t complete_cb, uintptr_t user_data);
|
||||
|
||||
// Get port status
|
||||
bool hub_port_get_status (uint8_t hub_addr, uint8_t hub_port, void* resp,
|
||||
tuh_xfer_cb_t complete_cb, uintptr_t user_data);
|
||||
|
||||
// Get status from Interrupt endpoint
|
||||
bool hub_edpt_status_xfer(uint8_t dev_addr);
|
||||
|
||||
// Reset a port
|
||||
static inline bool hub_port_reset(uint8_t hub_addr, uint8_t hub_port,
|
||||
tuh_xfer_cb_t complete_cb, uintptr_t user_data)
|
||||
{
|
||||
return hub_port_set_feature(hub_addr, hub_port, HUB_FEATURE_PORT_RESET, complete_cb, user_data);
|
||||
}
|
||||
|
||||
// Clear Reset Change
|
||||
static inline bool hub_port_clear_reset_change(uint8_t hub_addr, uint8_t hub_port,
|
||||
tuh_xfer_cb_t complete_cb, uintptr_t user_data)
|
||||
{
|
||||
return hub_port_clear_feature(hub_addr, hub_port, HUB_FEATURE_PORT_RESET_CHANGE, complete_cb, user_data);
|
||||
}
|
||||
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Internal Class Driver API
|
||||
//--------------------------------------------------------------------+
|
||||
void hub_init (void);
|
||||
bool hub_open (uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *itf_desc, uint16_t max_len);
|
||||
bool hub_set_config (uint8_t dev_addr, uint8_t itf_num);
|
||||
bool hub_xfer_cb (uint8_t dev_addr, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes);
|
||||
void hub_close (uint8_t dev_addr);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _TUSB_HUB_H_ */
|
||||
|
||||
/** @} */
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,293 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#ifndef _TUSB_USBH_H_
|
||||
#define _TUSB_USBH_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "common/tusb_common.h"
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// MACRO CONSTANT TYPEDEF
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// forward declaration
|
||||
struct tuh_xfer_s;
|
||||
typedef struct tuh_xfer_s tuh_xfer_t;
|
||||
|
||||
typedef void (*tuh_xfer_cb_t)(tuh_xfer_t* xfer);
|
||||
|
||||
// Note1: layout and order of this will be changed in near future
|
||||
// it is advised to initialize it using member name
|
||||
// Note2: not all field is available/meaningful in callback,
|
||||
// some info is not saved by usbh to save SRAM
|
||||
struct tuh_xfer_s {
|
||||
uint8_t daddr;
|
||||
uint8_t ep_addr;
|
||||
uint8_t TU_RESERVED; // reserved
|
||||
xfer_result_t result;
|
||||
|
||||
uint32_t actual_len; // excluding setup packet
|
||||
|
||||
union {
|
||||
tusb_control_request_t const* setup; // setup packet pointer if control transfer
|
||||
uint32_t buflen; // expected length if not control transfer (not available in callback)
|
||||
};
|
||||
|
||||
uint8_t* buffer; // not available in callback if not control transfer
|
||||
tuh_xfer_cb_t complete_cb;
|
||||
uintptr_t user_data;
|
||||
|
||||
// uint32_t timeout_ms; // place holder, not supported yet
|
||||
};
|
||||
|
||||
// Subject to change
|
||||
typedef struct {
|
||||
uint8_t daddr;
|
||||
tusb_desc_interface_t desc;
|
||||
} tuh_itf_info_t;
|
||||
|
||||
// ConfigID for tuh_config()
|
||||
enum {
|
||||
TUH_CFGID_RPI_PIO_USB_CONFIGURATION = OPT_MCU_RP2040 << 8 // cfg_param: pio_usb_configuration_t
|
||||
};
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// APPLICATION CALLBACK
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
//TU_ATTR_WEAK uint8_t tuh_attach_cb (tusb_desc_device_t const *desc_device);
|
||||
|
||||
// Invoked when a device is mounted (configured)
|
||||
TU_ATTR_WEAK void tuh_mount_cb (uint8_t daddr);
|
||||
|
||||
// Invoked when a device failed to mount during enumeration process
|
||||
// TU_ATTR_WEAK void tuh_mount_failed_cb (uint8_t daddr);
|
||||
|
||||
// Invoked when a device is unmounted (detached)
|
||||
TU_ATTR_WEAK void tuh_umount_cb(uint8_t daddr);
|
||||
|
||||
// Invoked when there is a new usb event, which need to be processed by tuh_task()/tuh_task_ext()
|
||||
void tuh_event_hook_cb(uint8_t rhport, uint32_t eventid, bool in_isr);
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// APPLICATION API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Configure host stack behavior with dynamic or port-specific parameters.
|
||||
// Should be called before tuh_init()
|
||||
// - cfg_id : configure ID (TBD)
|
||||
// - cfg_param: configure data, structure depends on the ID
|
||||
bool tuh_configure(uint8_t rhport, uint32_t cfg_id, const void* cfg_param);
|
||||
|
||||
// Init host stack
|
||||
bool tuh_init(uint8_t rhport);
|
||||
|
||||
// Check if host stack is already initialized with any roothub ports
|
||||
bool tuh_inited(void);
|
||||
|
||||
// Task function should be called in main/rtos loop, extended version of tuh_task()
|
||||
// - timeout_ms: millisecond to wait, zero = no wait, 0xFFFFFFFF = wait forever
|
||||
// - in_isr: if function is called in ISR
|
||||
void tuh_task_ext(uint32_t timeout_ms, bool in_isr);
|
||||
|
||||
// Task function should be called in main/rtos loop
|
||||
TU_ATTR_ALWAYS_INLINE static inline
|
||||
void tuh_task(void) {
|
||||
tuh_task_ext(UINT32_MAX, false);
|
||||
}
|
||||
|
||||
// Check if there is pending events need processing by tuh_task()
|
||||
bool tuh_task_event_ready(void);
|
||||
|
||||
#ifndef _TUSB_HCD_H_
|
||||
extern void hcd_int_handler(uint8_t rhport, bool in_isr);
|
||||
#endif
|
||||
|
||||
// Interrupt handler alias to HCD with in_isr as optional parameter
|
||||
// - tuh_int_handler(rhport) --> hcd_int_handler(rhport, true)
|
||||
// - tuh_int_handler(rhport, in_isr) --> hcd_int_handler(rhport, in_isr)
|
||||
// Note: this is similar to TU_VERIFY(), _GET_3RD_ARG() is defined in tusb_verify.h
|
||||
#define _tuh_int_handler_1arg(_rhport) hcd_int_handler(_rhport, true)
|
||||
#define _tuh_int_hanlder_2arg(_rhport, _in_isr) hcd_int_handler(_rhport, _in_isr)
|
||||
#define tuh_int_handler(...) _GET_3RD_ARG(__VA_ARGS__, _tuh_int_hanlder_2arg, _tuh_int_handler_1arg, _dummy)(__VA_ARGS__)
|
||||
|
||||
// Check if roothub port is initialized and active as a host
|
||||
bool tuh_rhport_is_active(uint8_t rhport);
|
||||
|
||||
// Assert/de-assert Bus Reset signal to roothub port. USB specs: it should last 10-50ms
|
||||
bool tuh_rhport_reset_bus(uint8_t rhport, bool active);
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Device API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Get VID/PID of device
|
||||
bool tuh_vid_pid_get(uint8_t daddr, uint16_t* vid, uint16_t* pid);
|
||||
|
||||
// Get speed of device
|
||||
tusb_speed_t tuh_speed_get(uint8_t daddr);
|
||||
|
||||
// Check if device is connected and configured
|
||||
bool tuh_mounted(uint8_t daddr);
|
||||
|
||||
// Check if device is suspended
|
||||
TU_ATTR_ALWAYS_INLINE static inline
|
||||
bool tuh_suspended(uint8_t daddr) {
|
||||
// TODO implement suspend & resume on host
|
||||
(void) daddr;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if device is ready to communicate with
|
||||
TU_ATTR_ALWAYS_INLINE static inline
|
||||
bool tuh_ready(uint8_t daddr) {
|
||||
return tuh_mounted(daddr) && !tuh_suspended(daddr);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Transfer API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Submit a control transfer
|
||||
// - async: complete callback invoked when finished.
|
||||
// - sync : blocking if complete callback is NULL.
|
||||
bool tuh_control_xfer(tuh_xfer_t* xfer);
|
||||
|
||||
// Submit a bulk/interrupt transfer
|
||||
// - async: complete callback invoked when finished.
|
||||
// - sync : blocking if complete callback is NULL.
|
||||
bool tuh_edpt_xfer(tuh_xfer_t* xfer);
|
||||
|
||||
// Open a non-control endpoint
|
||||
bool tuh_edpt_open(uint8_t daddr, tusb_desc_endpoint_t const * desc_ep);
|
||||
|
||||
// Abort a queued transfer. Note: it can only abort transfer that has not been started
|
||||
// Return true if a queued transfer is aborted, false if there is no transfer to abort
|
||||
bool tuh_edpt_abort_xfer(uint8_t daddr, uint8_t ep_addr);
|
||||
|
||||
// Set Configuration (control transfer)
|
||||
// config_num = 0 will un-configure device. Note: config_num = config_descriptor_index + 1
|
||||
// true on success, false if there is on-going control transfer or incorrect parameters
|
||||
// if complete_cb == NULL i.e blocking, user_data should be pointed to xfer_reuslt_t*
|
||||
bool tuh_configuration_set(uint8_t daddr, uint8_t config_num,
|
||||
tuh_xfer_cb_t complete_cb, uintptr_t user_data);
|
||||
|
||||
// Set Interface (control transfer)
|
||||
// true on success, false if there is on-going control transfer or incorrect parameters
|
||||
// if complete_cb == NULL i.e blocking, user_data should be pointed to xfer_reuslt_t*
|
||||
bool tuh_interface_set(uint8_t daddr, uint8_t itf_num, uint8_t itf_alt,
|
||||
tuh_xfer_cb_t complete_cb, uintptr_t user_data);
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Descriptors Asynchronous (non-blocking)
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Get an descriptor (control transfer)
|
||||
// true on success, false if there is on-going control transfer or incorrect parameters
|
||||
bool tuh_descriptor_get(uint8_t daddr, uint8_t type, uint8_t index, void* buffer, uint16_t len,
|
||||
tuh_xfer_cb_t complete_cb, uintptr_t user_data);
|
||||
|
||||
// Get device descriptor (control transfer)
|
||||
// true on success, false if there is on-going control transfer or incorrect parameters
|
||||
bool tuh_descriptor_get_device(uint8_t daddr, void* buffer, uint16_t len,
|
||||
tuh_xfer_cb_t complete_cb, uintptr_t user_data);
|
||||
|
||||
// Get configuration descriptor (control transfer)
|
||||
// true on success, false if there is on-going control transfer or incorrect parameters
|
||||
bool tuh_descriptor_get_configuration(uint8_t daddr, uint8_t index, void* buffer, uint16_t len,
|
||||
tuh_xfer_cb_t complete_cb, uintptr_t user_data);
|
||||
|
||||
// Get HID report descriptor (control transfer)
|
||||
// true on success, false if there is on-going control transfer or incorrect parameters
|
||||
bool tuh_descriptor_get_hid_report(uint8_t daddr, uint8_t itf_num, uint8_t desc_type, uint8_t index, void* buffer, uint16_t len,
|
||||
tuh_xfer_cb_t complete_cb, uintptr_t user_data);
|
||||
|
||||
// Get string descriptor (control transfer)
|
||||
// true on success, false if there is on-going control transfer or incorrect parameters
|
||||
// Blocking if complete callback is NULL, in this case 'user_data' must contain xfer_result_t variable
|
||||
bool tuh_descriptor_get_string(uint8_t daddr, uint8_t index, uint16_t language_id, void* buffer, uint16_t len,
|
||||
tuh_xfer_cb_t complete_cb, uintptr_t user_data);
|
||||
|
||||
// Get manufacturer string descriptor (control transfer)
|
||||
// true on success, false if there is on-going control transfer or incorrect parameters
|
||||
bool tuh_descriptor_get_manufacturer_string(uint8_t daddr, uint16_t language_id, void* buffer, uint16_t len,
|
||||
tuh_xfer_cb_t complete_cb, uintptr_t user_data);
|
||||
|
||||
// Get product string descriptor (control transfer)
|
||||
// true on success, false if there is on-going control transfer or incorrect parameters
|
||||
bool tuh_descriptor_get_product_string(uint8_t daddr, uint16_t language_id, void* buffer, uint16_t len,
|
||||
tuh_xfer_cb_t complete_cb, uintptr_t user_data);
|
||||
|
||||
// Get serial string descriptor (control transfer)
|
||||
// true on success, false if there is on-going control transfer or incorrect parameters
|
||||
bool tuh_descriptor_get_serial_string(uint8_t daddr, uint16_t language_id, void* buffer, uint16_t len,
|
||||
tuh_xfer_cb_t complete_cb, uintptr_t user_data);
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Descriptors Synchronous (blocking)
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Sync (blocking) version of tuh_descriptor_get()
|
||||
// return transfer result
|
||||
uint8_t tuh_descriptor_get_sync(uint8_t daddr, uint8_t type, uint8_t index, void* buffer, uint16_t len);
|
||||
|
||||
// Sync (blocking) version of tuh_descriptor_get_device()
|
||||
// return transfer result
|
||||
uint8_t tuh_descriptor_get_device_sync(uint8_t daddr, void* buffer, uint16_t len);
|
||||
|
||||
// Sync (blocking) version of tuh_descriptor_get_configuration()
|
||||
// return transfer result
|
||||
uint8_t tuh_descriptor_get_configuration_sync(uint8_t daddr, uint8_t index, void* buffer, uint16_t len);
|
||||
|
||||
// Sync (blocking) version of tuh_descriptor_get_hid_report()
|
||||
// return transfer result
|
||||
uint8_t tuh_descriptor_get_hid_report_sync(uint8_t daddr, uint8_t itf_num, uint8_t desc_type, uint8_t index, void* buffer, uint16_t len);
|
||||
|
||||
// Sync (blocking) version of tuh_descriptor_get_string()
|
||||
// return transfer result
|
||||
uint8_t tuh_descriptor_get_string_sync(uint8_t daddr, uint8_t index, uint16_t language_id, void* buffer, uint16_t len);
|
||||
|
||||
// Sync (blocking) version of tuh_descriptor_get_manufacturer_string()
|
||||
// return transfer result
|
||||
uint8_t tuh_descriptor_get_manufacturer_string_sync(uint8_t daddr, uint16_t language_id, void* buffer, uint16_t len);
|
||||
|
||||
// Sync (blocking) version of tuh_descriptor_get_product_string()
|
||||
// return transfer result
|
||||
uint8_t tuh_descriptor_get_product_string_sync(uint8_t daddr, uint16_t language_id, void* buffer, uint16_t len);
|
||||
|
||||
// Sync (blocking) version of tuh_descriptor_get_serial_string()
|
||||
// return transfer result
|
||||
uint8_t tuh_descriptor_get_serial_string_sync(uint8_t daddr, uint16_t language_id, void* buffer, uint16_t len);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -0,0 +1,103 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2021, Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#ifndef _TUSB_USBH_PVT_H_
|
||||
#define _TUSB_USBH_PVT_H_
|
||||
|
||||
#include "osal/osal.h"
|
||||
#include "common/tusb_fifo.h"
|
||||
#include "common/tusb_private.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define TU_LOG_USBH(...) TU_LOG(CFG_TUH_LOG_LEVEL, __VA_ARGS__)
|
||||
|
||||
enum {
|
||||
USBH_EPSIZE_BULK_MAX = (TUH_OPT_HIGH_SPEED ? TUSB_EPSIZE_BULK_HS : TUSB_EPSIZE_BULK_FS)
|
||||
};
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Class Driver API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
typedef struct {
|
||||
#if CFG_TUSB_DEBUG >= CFG_TUH_LOG_LEVEL
|
||||
char const* name;
|
||||
#endif
|
||||
|
||||
void (* const init )(void);
|
||||
bool (* const open )(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const * itf_desc, uint16_t max_len);
|
||||
bool (* const set_config )(uint8_t dev_addr, uint8_t itf_num);
|
||||
bool (* const xfer_cb )(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes);
|
||||
void (* const close )(uint8_t dev_addr);
|
||||
} usbh_class_driver_t;
|
||||
|
||||
// Invoked when initializing host stack to get additional class drivers.
|
||||
// Can be implemented by application to extend/overwrite class driver support.
|
||||
// Note: The drivers array must be accessible at all time when stack is active
|
||||
usbh_class_driver_t const* usbh_app_driver_get_cb(uint8_t* driver_count) TU_ATTR_WEAK;
|
||||
|
||||
// Call by class driver to tell USBH that it has complete the enumeration
|
||||
void usbh_driver_set_config_complete(uint8_t dev_addr, uint8_t itf_num);
|
||||
|
||||
uint8_t usbh_get_rhport(uint8_t dev_addr);
|
||||
|
||||
uint8_t* usbh_get_enum_buf(void);
|
||||
|
||||
void usbh_int_set(bool enabled);
|
||||
|
||||
void usbh_defer_func(osal_task_func_t func, void *param, bool in_isr);
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// USBH Endpoint API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Submit a usb transfer with callback support, require CFG_TUH_API_EDPT_XFER
|
||||
bool usbh_edpt_xfer_with_callback(uint8_t dev_addr, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes,
|
||||
tuh_xfer_cb_t complete_cb, uintptr_t user_data);
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE
|
||||
static inline bool usbh_edpt_xfer(uint8_t dev_addr, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes) {
|
||||
return usbh_edpt_xfer_with_callback(dev_addr, ep_addr, buffer, total_bytes, NULL, 0);
|
||||
}
|
||||
|
||||
// Claim an endpoint before submitting a transfer.
|
||||
// If caller does not make any transfer, it must release endpoint for others.
|
||||
bool usbh_edpt_claim(uint8_t dev_addr, uint8_t ep_addr);
|
||||
|
||||
// Release claimed endpoint without submitting a transfer
|
||||
bool usbh_edpt_release(uint8_t dev_addr, uint8_t ep_addr);
|
||||
|
||||
// Check if endpoint transferring is complete
|
||||
bool usbh_edpt_busy(uint8_t dev_addr, uint8_t ep_addr);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -0,0 +1,96 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#ifndef _TUSB_OSAL_H_
|
||||
#define _TUSB_OSAL_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "common/tusb_common.h"
|
||||
|
||||
typedef void (*osal_task_func_t)( void * );
|
||||
|
||||
// Timeout
|
||||
#define OSAL_TIMEOUT_NOTIMEOUT (0) // Return immediately
|
||||
#define OSAL_TIMEOUT_NORMAL (10) // Default timeout
|
||||
#define OSAL_TIMEOUT_WAIT_FOREVER (UINT32_MAX) // Wait forever
|
||||
#define OSAL_TIMEOUT_CONTROL_XFER OSAL_TIMEOUT_WAIT_FOREVER
|
||||
|
||||
// Mutex is required when using a preempted RTOS or MCU has multiple cores
|
||||
#if (CFG_TUSB_OS == OPT_OS_NONE) && !TUP_MCU_MULTIPLE_CORE
|
||||
#define OSAL_MUTEX_REQUIRED 0
|
||||
#define OSAL_MUTEX_DEF(_name) uint8_t :0
|
||||
#else
|
||||
#define OSAL_MUTEX_REQUIRED 1
|
||||
#define OSAL_MUTEX_DEF(_name) osal_mutex_def_t _name
|
||||
#endif
|
||||
|
||||
// OS thin implementation
|
||||
#if CFG_TUSB_OS == OPT_OS_NONE
|
||||
#include "osal_none.h"
|
||||
#elif CFG_TUSB_OS == OPT_OS_FREERTOS
|
||||
#include "osal_freertos.h"
|
||||
#elif CFG_TUSB_OS == OPT_OS_MYNEWT
|
||||
#include "osal_mynewt.h"
|
||||
#elif CFG_TUSB_OS == OPT_OS_PICO
|
||||
#include "osal_pico.h"
|
||||
#elif CFG_TUSB_OS == OPT_OS_RTTHREAD
|
||||
#include "osal_rtthread.h"
|
||||
#elif CFG_TUSB_OS == OPT_OS_RTX4
|
||||
#include "osal_rtx4.h"
|
||||
#elif CFG_TUSB_OS == OPT_OS_CUSTOM
|
||||
#include "tusb_os_custom.h" // implemented by application
|
||||
#else
|
||||
#error OS is not supported yet
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// OSAL Porting API
|
||||
// Should be implemented as static inline function in osal_port.h header
|
||||
/*
|
||||
osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef);
|
||||
bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr);
|
||||
bool osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec);
|
||||
void osal_semaphore_reset(osal_semaphore_t sem_hdl); // TODO removed
|
||||
|
||||
osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef);
|
||||
bool osal_mutex_lock (osal_mutex_t sem_hdl, uint32_t msec);
|
||||
bool osal_mutex_unlock(osal_mutex_t mutex_hdl);
|
||||
|
||||
osal_queue_t osal_queue_create(osal_queue_def_t* qdef);
|
||||
bool osal_queue_receive(osal_queue_t qhdl, void* data, uint32_t msec);
|
||||
bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in_isr);
|
||||
bool osal_queue_empty(osal_queue_t qhdl);
|
||||
*/
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _TUSB_OSAL_H_ */
|
|
@ -0,0 +1,214 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#ifndef _TUSB_OSAL_FREERTOS_H_
|
||||
#define _TUSB_OSAL_FREERTOS_H_
|
||||
|
||||
// FreeRTOS Headers
|
||||
#include TU_INCLUDE_PATH(CFG_TUSB_OS_INC_PATH,FreeRTOS.h)
|
||||
#include TU_INCLUDE_PATH(CFG_TUSB_OS_INC_PATH,semphr.h)
|
||||
#include TU_INCLUDE_PATH(CFG_TUSB_OS_INC_PATH,queue.h)
|
||||
#include TU_INCLUDE_PATH(CFG_TUSB_OS_INC_PATH,task.h)
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// MACRO CONSTANT TYPEDEF PROTYPES
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
#if configSUPPORT_STATIC_ALLOCATION
|
||||
typedef StaticSemaphore_t osal_semaphore_def_t;
|
||||
typedef StaticSemaphore_t osal_mutex_def_t;
|
||||
#else
|
||||
// not used therefore defined to smallest possible type to save space
|
||||
typedef uint8_t osal_semaphore_def_t;
|
||||
typedef uint8_t osal_mutex_def_t;
|
||||
#endif
|
||||
|
||||
typedef SemaphoreHandle_t osal_semaphore_t;
|
||||
typedef SemaphoreHandle_t osal_mutex_t;
|
||||
typedef QueueHandle_t osal_queue_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint16_t depth;
|
||||
uint16_t item_sz;
|
||||
void* buf;
|
||||
|
||||
#if defined(configQUEUE_REGISTRY_SIZE) && (configQUEUE_REGISTRY_SIZE>0)
|
||||
char const* name;
|
||||
#endif
|
||||
|
||||
#if configSUPPORT_STATIC_ALLOCATION
|
||||
StaticQueue_t sq;
|
||||
#endif
|
||||
} osal_queue_def_t;
|
||||
|
||||
#if defined(configQUEUE_REGISTRY_SIZE) && (configQUEUE_REGISTRY_SIZE>0)
|
||||
#define _OSAL_Q_NAME(_name) .name = #_name
|
||||
#else
|
||||
#define _OSAL_Q_NAME(_name)
|
||||
#endif
|
||||
|
||||
// _int_set is not used with an RTOS
|
||||
#define OSAL_QUEUE_DEF(_int_set, _name, _depth, _type) \
|
||||
static _type _name##_##buf[_depth];\
|
||||
osal_queue_def_t _name = { .depth = _depth, .item_sz = sizeof(_type), .buf = _name##_##buf, _OSAL_Q_NAME(_name) };
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// TASK API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline uint32_t _osal_ms2tick(uint32_t msec) {
|
||||
if ( msec == OSAL_TIMEOUT_WAIT_FOREVER ) return portMAX_DELAY;
|
||||
if ( msec == 0 ) return 0;
|
||||
|
||||
uint32_t ticks = pdMS_TO_TICKS(msec);
|
||||
|
||||
// configTICK_RATE_HZ is less than 1000 and 1 tick > 1 ms
|
||||
// we still need to delay at least 1 tick
|
||||
if ( ticks == 0 ) ticks = 1;
|
||||
|
||||
return ticks;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline void osal_task_delay(uint32_t msec) {
|
||||
vTaskDelay(pdMS_TO_TICKS(msec));
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Semaphore API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t *semdef) {
|
||||
#if configSUPPORT_STATIC_ALLOCATION
|
||||
return xSemaphoreCreateBinaryStatic(semdef);
|
||||
#else
|
||||
(void) semdef;
|
||||
return xSemaphoreCreateBinary();
|
||||
#endif
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) {
|
||||
if ( !in_isr ) {
|
||||
return xSemaphoreGive(sem_hdl) != 0;
|
||||
} else {
|
||||
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||
BaseType_t res = xSemaphoreGiveFromISR(sem_hdl, &xHigherPriorityTaskWoken);
|
||||
|
||||
#if CFG_TUSB_MCU == OPT_MCU_ESP32S2 || CFG_TUSB_MCU == OPT_MCU_ESP32S3
|
||||
// not needed after https://github.com/espressif/esp-idf/commit/c5fd79547ac9b7bae06fa660e9f814d18d3390b7
|
||||
if ( xHigherPriorityTaskWoken ) portYIELD_FROM_ISR();
|
||||
#else
|
||||
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
|
||||
#endif
|
||||
|
||||
return res != 0;
|
||||
}
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec) {
|
||||
return xSemaphoreTake(sem_hdl, _osal_ms2tick(msec));
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline void osal_semaphore_reset(osal_semaphore_t const sem_hdl) {
|
||||
xQueueReset(sem_hdl);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// MUTEX API (priority inheritance)
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t *mdef) {
|
||||
#if configSUPPORT_STATIC_ALLOCATION
|
||||
return xSemaphoreCreateMutexStatic(mdef);
|
||||
#else
|
||||
(void) mdef;
|
||||
return xSemaphoreCreateMutex();
|
||||
#endif
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_lock(osal_mutex_t mutex_hdl, uint32_t msec) {
|
||||
return osal_semaphore_wait(mutex_hdl, msec);
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl) {
|
||||
return xSemaphoreGive(mutex_hdl);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// QUEUE API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef) {
|
||||
osal_queue_t q;
|
||||
|
||||
#if configSUPPORT_STATIC_ALLOCATION
|
||||
q = xQueueCreateStatic(qdef->depth, qdef->item_sz, (uint8_t*) qdef->buf, &qdef->sq);
|
||||
#else
|
||||
q = xQueueCreate(qdef->depth, qdef->item_sz);
|
||||
#endif
|
||||
|
||||
#if defined(configQUEUE_REGISTRY_SIZE) && (configQUEUE_REGISTRY_SIZE>0)
|
||||
vQueueAddToRegistry(q, qdef->name);
|
||||
#endif
|
||||
|
||||
return q;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_receive(osal_queue_t qhdl, void* data, uint32_t msec) {
|
||||
return xQueueReceive(qhdl, data, _osal_ms2tick(msec));
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_send(osal_queue_t qhdl, void const *data, bool in_isr) {
|
||||
if ( !in_isr ) {
|
||||
return xQueueSendToBack(qhdl, data, OSAL_TIMEOUT_WAIT_FOREVER) != 0;
|
||||
} else {
|
||||
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||
BaseType_t res = xQueueSendToBackFromISR(qhdl, data, &xHigherPriorityTaskWoken);
|
||||
|
||||
#if CFG_TUSB_MCU == OPT_MCU_ESP32S2 || CFG_TUSB_MCU == OPT_MCU_ESP32S3
|
||||
// not needed after https://github.com/espressif/esp-idf/commit/c5fd79547ac9b7bae06fa660e9f814d18d3390b7 (IDF v5)
|
||||
if ( xHigherPriorityTaskWoken ) portYIELD_FROM_ISR();
|
||||
#else
|
||||
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
|
||||
#endif
|
||||
|
||||
return res != 0;
|
||||
}
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_empty(osal_queue_t qhdl) {
|
||||
return uxQueueMessagesWaiting(qhdl) == 0;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -0,0 +1,176 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#ifndef OSAL_MYNEWT_H_
|
||||
#define OSAL_MYNEWT_H_
|
||||
|
||||
#include "os/os.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// TASK API
|
||||
//--------------------------------------------------------------------+
|
||||
TU_ATTR_ALWAYS_INLINE static inline void osal_task_delay(uint32_t msec)
|
||||
{
|
||||
os_time_delay( os_time_ms_to_ticks32(msec) );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Semaphore API
|
||||
//--------------------------------------------------------------------+
|
||||
typedef struct os_sem osal_semaphore_def_t;
|
||||
typedef struct os_sem* osal_semaphore_t;
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef)
|
||||
{
|
||||
return (os_sem_init(semdef, 0) == OS_OK) ? (osal_semaphore_t) semdef : NULL;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr)
|
||||
{
|
||||
(void) in_isr;
|
||||
return os_sem_release(sem_hdl) == OS_OK;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec)
|
||||
{
|
||||
uint32_t const ticks = (msec == OSAL_TIMEOUT_WAIT_FOREVER) ? OS_TIMEOUT_NEVER : os_time_ms_to_ticks32(msec);
|
||||
return os_sem_pend(sem_hdl, ticks) == OS_OK;
|
||||
}
|
||||
|
||||
static inline void osal_semaphore_reset(osal_semaphore_t sem_hdl)
|
||||
{
|
||||
// TODO implement later
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// MUTEX API (priority inheritance)
|
||||
//--------------------------------------------------------------------+
|
||||
typedef struct os_mutex osal_mutex_def_t;
|
||||
typedef struct os_mutex* osal_mutex_t;
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef)
|
||||
{
|
||||
return (os_mutex_init(mdef) == OS_OK) ? (osal_mutex_t) mdef : NULL;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_lock(osal_mutex_t mutex_hdl, uint32_t msec)
|
||||
{
|
||||
uint32_t const ticks = (msec == OSAL_TIMEOUT_WAIT_FOREVER) ? OS_TIMEOUT_NEVER : os_time_ms_to_ticks32(msec);
|
||||
return os_mutex_pend(mutex_hdl, ticks) == OS_OK;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl)
|
||||
{
|
||||
return os_mutex_release(mutex_hdl) == OS_OK;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// QUEUE API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// role device/host is used by OS NONE for mutex (disable usb isr) only
|
||||
#define OSAL_QUEUE_DEF(_int_set, _name, _depth, _type) \
|
||||
static _type _name##_##buf[_depth];\
|
||||
static struct os_event _name##_##evbuf[_depth];\
|
||||
osal_queue_def_t _name = { .depth = _depth, .item_sz = sizeof(_type), .buf = _name##_##buf, .evbuf = _name##_##evbuf};\
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint16_t depth;
|
||||
uint16_t item_sz;
|
||||
void* buf;
|
||||
void* evbuf;
|
||||
|
||||
struct os_mempool mpool;
|
||||
struct os_mempool epool;
|
||||
|
||||
struct os_eventq evq;
|
||||
}osal_queue_def_t;
|
||||
|
||||
typedef osal_queue_def_t* osal_queue_t;
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef)
|
||||
{
|
||||
if ( OS_OK != os_mempool_init(&qdef->mpool, qdef->depth, qdef->item_sz, qdef->buf, "usbd queue") ) return NULL;
|
||||
if ( OS_OK != os_mempool_init(&qdef->epool, qdef->depth, sizeof(struct os_event), qdef->evbuf, "usbd evqueue") ) return NULL;
|
||||
|
||||
os_eventq_init(&qdef->evq);
|
||||
return (osal_queue_t) qdef;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_receive(osal_queue_t qhdl, void* data, uint32_t msec)
|
||||
{
|
||||
(void) msec; // os_eventq_get() does not take timeout, always behave as msec = WAIT_FOREVER
|
||||
|
||||
struct os_event* ev;
|
||||
ev = os_eventq_get(&qhdl->evq);
|
||||
|
||||
memcpy(data, ev->ev_arg, qhdl->item_sz); // copy message
|
||||
os_memblock_put(&qhdl->mpool, ev->ev_arg); // put back mem block
|
||||
os_memblock_put(&qhdl->epool, ev); // put back ev block
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static inline bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in_isr)
|
||||
{
|
||||
(void) in_isr;
|
||||
|
||||
// get a block from mem pool for data
|
||||
void* ptr = os_memblock_get(&qhdl->mpool);
|
||||
if (!ptr) return false;
|
||||
memcpy(ptr, data, qhdl->item_sz);
|
||||
|
||||
// get a block from event pool to put into queue
|
||||
struct os_event* ev = (struct os_event*) os_memblock_get(&qhdl->epool);
|
||||
if (!ev)
|
||||
{
|
||||
os_memblock_put(&qhdl->mpool, ptr);
|
||||
return false;
|
||||
}
|
||||
tu_memclr(ev, sizeof(struct os_event));
|
||||
ev->ev_arg = ptr;
|
||||
|
||||
os_eventq_put(&qhdl->evq, ev);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_empty(osal_queue_t qhdl)
|
||||
{
|
||||
return STAILQ_EMPTY(&qhdl->evq.evq_list);
|
||||
}
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* OSAL_MYNEWT_H_ */
|
|
@ -0,0 +1,181 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#ifndef TUSB_OSAL_NONE_H_
|
||||
#define TUSB_OSAL_NONE_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// TASK API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
#if CFG_TUH_ENABLED
|
||||
// currently only needed/available in host mode
|
||||
TU_ATTR_WEAK void osal_task_delay(uint32_t msec);
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Binary Semaphore API
|
||||
//--------------------------------------------------------------------+
|
||||
typedef struct {
|
||||
volatile uint16_t count;
|
||||
} osal_semaphore_def_t;
|
||||
|
||||
typedef osal_semaphore_def_t* osal_semaphore_t;
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef) {
|
||||
semdef->count = 0;
|
||||
return semdef;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) {
|
||||
(void) in_isr;
|
||||
sem_hdl->count++;
|
||||
return true;
|
||||
}
|
||||
|
||||
// TODO blocking for now
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec) {
|
||||
(void) msec;
|
||||
|
||||
while (sem_hdl->count == 0) {}
|
||||
sem_hdl->count--;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline void osal_semaphore_reset(osal_semaphore_t sem_hdl) {
|
||||
sem_hdl->count = 0;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// MUTEX API
|
||||
// Within tinyusb, mutex is never used in ISR context
|
||||
//--------------------------------------------------------------------+
|
||||
typedef osal_semaphore_def_t osal_mutex_def_t;
|
||||
typedef osal_semaphore_t osal_mutex_t;
|
||||
|
||||
#if OSAL_MUTEX_REQUIRED
|
||||
// Note: multiple cores MCUs usually do provide IPC API for mutex
|
||||
// or we can use std atomic function
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef) {
|
||||
mdef->count = 1;
|
||||
return mdef;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_lock (osal_mutex_t mutex_hdl, uint32_t msec) {
|
||||
return osal_semaphore_wait(mutex_hdl, msec);
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl) {
|
||||
return osal_semaphore_post(mutex_hdl, false);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#define osal_mutex_create(_mdef) (NULL)
|
||||
#define osal_mutex_lock(_mutex_hdl, _ms) (true)
|
||||
#define osal_mutex_unlock(_mutex_hdl) (true)
|
||||
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// QUEUE API
|
||||
//--------------------------------------------------------------------+
|
||||
#include "common/tusb_fifo.h"
|
||||
|
||||
typedef struct {
|
||||
void (* interrupt_set)(bool);
|
||||
tu_fifo_t ff;
|
||||
} osal_queue_def_t;
|
||||
|
||||
typedef osal_queue_def_t* osal_queue_t;
|
||||
|
||||
// _int_set is used as mutex in OS NONE (disable/enable USB ISR)
|
||||
#define OSAL_QUEUE_DEF(_int_set, _name, _depth, _type) \
|
||||
uint8_t _name##_buf[_depth*sizeof(_type)]; \
|
||||
osal_queue_def_t _name = { \
|
||||
.interrupt_set = _int_set, \
|
||||
.ff = TU_FIFO_INIT(_name##_buf, _depth, _type, false) \
|
||||
}
|
||||
|
||||
// lock queue by disable USB interrupt
|
||||
TU_ATTR_ALWAYS_INLINE static inline void _osal_q_lock(osal_queue_t qhdl) {
|
||||
// disable dcd/hcd interrupt
|
||||
qhdl->interrupt_set(false);
|
||||
}
|
||||
|
||||
// unlock queue
|
||||
TU_ATTR_ALWAYS_INLINE static inline void _osal_q_unlock(osal_queue_t qhdl) {
|
||||
// enable dcd/hcd interrupt
|
||||
qhdl->interrupt_set(true);
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef) {
|
||||
tu_fifo_clear(&qdef->ff);
|
||||
return (osal_queue_t) qdef;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_receive(osal_queue_t qhdl, void* data, uint32_t msec) {
|
||||
(void) msec; // not used, always behave as msec = 0
|
||||
|
||||
_osal_q_lock(qhdl);
|
||||
bool success = tu_fifo_read(&qhdl->ff, data);
|
||||
_osal_q_unlock(qhdl);
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_send(osal_queue_t qhdl, void const* data, bool in_isr) {
|
||||
if (!in_isr) {
|
||||
_osal_q_lock(qhdl);
|
||||
}
|
||||
|
||||
bool success = tu_fifo_write(&qhdl->ff, data);
|
||||
|
||||
if (!in_isr) {
|
||||
_osal_q_unlock(qhdl);
|
||||
}
|
||||
|
||||
TU_ASSERT(success);
|
||||
return success;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_empty(osal_queue_t qhdl) {
|
||||
// Skip queue lock/unlock since this function is primarily called
|
||||
// with interrupt disabled before going into low power mode
|
||||
return tu_fifo_empty(&qhdl->ff);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -0,0 +1,184 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#ifndef _TUSB_OSAL_PICO_H_
|
||||
#define _TUSB_OSAL_PICO_H_
|
||||
|
||||
#include "pico/time.h"
|
||||
#include "pico/sem.h"
|
||||
#include "pico/mutex.h"
|
||||
#include "pico/critical_section.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// TASK API
|
||||
//--------------------------------------------------------------------+
|
||||
TU_ATTR_ALWAYS_INLINE static inline void osal_task_delay(uint32_t msec)
|
||||
{
|
||||
sleep_ms(msec);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Binary Semaphore API
|
||||
//--------------------------------------------------------------------+
|
||||
typedef struct semaphore osal_semaphore_def_t, *osal_semaphore_t;
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef)
|
||||
{
|
||||
sem_init(semdef, 0, 255);
|
||||
return semdef;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr)
|
||||
{
|
||||
(void) in_isr;
|
||||
sem_release(sem_hdl);
|
||||
return true;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_wait (osal_semaphore_t sem_hdl, uint32_t msec)
|
||||
{
|
||||
return sem_acquire_timeout_ms(sem_hdl, msec);
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline void osal_semaphore_reset(osal_semaphore_t sem_hdl)
|
||||
{
|
||||
sem_reset(sem_hdl, 0);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// MUTEX API
|
||||
// Within tinyusb, mutex is never used in ISR context
|
||||
//--------------------------------------------------------------------+
|
||||
typedef struct mutex osal_mutex_def_t, *osal_mutex_t;
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef)
|
||||
{
|
||||
mutex_init(mdef);
|
||||
return mdef;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_lock (osal_mutex_t mutex_hdl, uint32_t msec)
|
||||
{
|
||||
return mutex_enter_timeout_ms(mutex_hdl, msec);
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl)
|
||||
{
|
||||
mutex_exit(mutex_hdl);
|
||||
return true;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// QUEUE API
|
||||
//--------------------------------------------------------------------+
|
||||
#include "common/tusb_fifo.h"
|
||||
|
||||
typedef struct
|
||||
{
|
||||
tu_fifo_t ff;
|
||||
struct critical_section critsec; // osal_queue may be used in IRQs, so need critical section
|
||||
} osal_queue_def_t;
|
||||
|
||||
typedef osal_queue_def_t* osal_queue_t;
|
||||
|
||||
// role device/host is used by OS NONE for mutex (disable usb isr) only
|
||||
#define OSAL_QUEUE_DEF(_int_set, _name, _depth, _type) \
|
||||
uint8_t _name##_buf[_depth*sizeof(_type)]; \
|
||||
osal_queue_def_t _name = { \
|
||||
.ff = TU_FIFO_INIT(_name##_buf, _depth, _type, false) \
|
||||
}
|
||||
|
||||
// lock queue by disable USB interrupt
|
||||
TU_ATTR_ALWAYS_INLINE static inline void _osal_q_lock(osal_queue_t qhdl)
|
||||
{
|
||||
critical_section_enter_blocking(&qhdl->critsec);
|
||||
}
|
||||
|
||||
// unlock queue
|
||||
TU_ATTR_ALWAYS_INLINE static inline void _osal_q_unlock(osal_queue_t qhdl)
|
||||
{
|
||||
critical_section_exit(&qhdl->critsec);
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef)
|
||||
{
|
||||
critical_section_init(&qdef->critsec);
|
||||
tu_fifo_clear(&qdef->ff);
|
||||
return (osal_queue_t) qdef;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_receive(osal_queue_t qhdl, void* data, uint32_t msec)
|
||||
{
|
||||
(void) msec; // not used, always behave as msec = 0
|
||||
|
||||
// TODO: revisit... docs say that mutexes are never used from IRQ context,
|
||||
// however osal_queue_recieve may be. therefore my assumption is that
|
||||
// the fifo mutex is not populated for queues used from an IRQ context
|
||||
//assert(!qhdl->ff.mutex);
|
||||
|
||||
_osal_q_lock(qhdl);
|
||||
bool success = tu_fifo_read(&qhdl->ff, data);
|
||||
_osal_q_unlock(qhdl);
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in_isr)
|
||||
{
|
||||
// TODO: revisit... docs say that mutexes are never used from IRQ context,
|
||||
// however osal_queue_recieve may be. therefore my assumption is that
|
||||
// the fifo mutex is not populated for queues used from an IRQ context
|
||||
//assert(!qhdl->ff.mutex);
|
||||
(void) in_isr;
|
||||
|
||||
_osal_q_lock(qhdl);
|
||||
bool success = tu_fifo_write(&qhdl->ff, data);
|
||||
_osal_q_unlock(qhdl);
|
||||
|
||||
TU_ASSERT(success);
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_empty(osal_queue_t qhdl)
|
||||
{
|
||||
// TODO: revisit; whether this is true or not currently, tu_fifo_empty is a single
|
||||
// volatile read.
|
||||
|
||||
// Skip queue lock/unlock since this function is primarily called
|
||||
// with interrupt disabled before going into low power mode
|
||||
return tu_fifo_empty(&qhdl->ff);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _TUSB_OSAL_PICO_H_ */
|
|
@ -0,0 +1,132 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2020 tfx2001 (2479727366@qq.com)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#ifndef _TUSB_OSAL_RTTHREAD_H_
|
||||
#define _TUSB_OSAL_RTTHREAD_H_
|
||||
|
||||
// RT-Thread Headers
|
||||
#include "rtthread.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// TASK API
|
||||
//--------------------------------------------------------------------+
|
||||
TU_ATTR_ALWAYS_INLINE static inline void osal_task_delay(uint32_t msec) {
|
||||
rt_thread_mdelay(msec);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Semaphore API
|
||||
//--------------------------------------------------------------------+
|
||||
typedef struct rt_semaphore osal_semaphore_def_t;
|
||||
typedef rt_sem_t osal_semaphore_t;
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline osal_semaphore_t
|
||||
osal_semaphore_create(osal_semaphore_def_t *semdef) {
|
||||
rt_sem_init(semdef, "tusb", 0, RT_IPC_FLAG_PRIO);
|
||||
return semdef;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) {
|
||||
(void) in_isr;
|
||||
return rt_sem_release(sem_hdl) == RT_EOK;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec) {
|
||||
return rt_sem_take(sem_hdl, rt_tick_from_millisecond((rt_int32_t) msec)) == RT_EOK;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline void osal_semaphore_reset(osal_semaphore_t const sem_hdl) {
|
||||
rt_sem_control(sem_hdl, RT_IPC_CMD_RESET, 0);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// MUTEX API (priority inheritance)
|
||||
//--------------------------------------------------------------------+
|
||||
typedef struct rt_mutex osal_mutex_def_t;
|
||||
typedef rt_mutex_t osal_mutex_t;
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t *mdef) {
|
||||
rt_mutex_init(mdef, "tusb", RT_IPC_FLAG_PRIO);
|
||||
return mdef;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_lock(osal_mutex_t mutex_hdl, uint32_t msec) {
|
||||
return rt_mutex_take(mutex_hdl, rt_tick_from_millisecond((rt_int32_t) msec)) == RT_EOK;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl) {
|
||||
return rt_mutex_release(mutex_hdl) == RT_EOK;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// QUEUE API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// role device/host is used by OS NONE for mutex (disable usb isr) only
|
||||
#define OSAL_QUEUE_DEF(_int_set, _name, _depth, _type) \
|
||||
static _type _name##_##buf[_depth]; \
|
||||
osal_queue_def_t _name = { .depth = _depth, .item_sz = sizeof(_type), .buf = _name##_##buf };
|
||||
|
||||
typedef struct {
|
||||
uint16_t depth;
|
||||
uint16_t item_sz;
|
||||
void *buf;
|
||||
|
||||
struct rt_messagequeue sq;
|
||||
} osal_queue_def_t;
|
||||
|
||||
typedef rt_mq_t osal_queue_t;
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline osal_queue_t osal_queue_create(osal_queue_def_t *qdef) {
|
||||
rt_mq_init(&(qdef->sq), "tusb", qdef->buf, qdef->item_sz,
|
||||
qdef->item_sz * qdef->depth, RT_IPC_FLAG_PRIO);
|
||||
return &(qdef->sq);
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_receive(osal_queue_t qhdl, void *data, uint32_t msec) {
|
||||
|
||||
rt_tick_t tick = rt_tick_from_millisecond((rt_int32_t) msec);
|
||||
return rt_mq_recv(qhdl, data, qhdl->msg_size, tick) == RT_EOK;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_send(osal_queue_t qhdl, void const *data, bool in_isr) {
|
||||
(void) in_isr;
|
||||
return rt_mq_send(qhdl, (void *)data, qhdl->msg_size) == RT_EOK;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_empty(osal_queue_t qhdl) {
|
||||
return (qhdl->entry) == 0;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _TUSB_OSAL_RTTHREAD_H_ */
|
|
@ -0,0 +1,170 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2021 Tian Yunhao (t123yh)
|
||||
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#ifndef _TUSB_OSAL_RTX4_H_
|
||||
#define _TUSB_OSAL_RTX4_H_
|
||||
|
||||
#include <rtl.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// TASK API
|
||||
//--------------------------------------------------------------------+
|
||||
TU_ATTR_ALWAYS_INLINE static inline void osal_task_delay(uint32_t msec)
|
||||
{
|
||||
uint16_t hi = msec >> 16;
|
||||
uint16_t lo = msec;
|
||||
while (hi--) {
|
||||
os_dly_wait(0xFFFE);
|
||||
}
|
||||
os_dly_wait(lo);
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline uint16_t msec2wait(uint32_t msec) {
|
||||
if (msec == OSAL_TIMEOUT_WAIT_FOREVER)
|
||||
return 0xFFFF;
|
||||
else if (msec >= 0xFFFE)
|
||||
return 0xFFFE;
|
||||
else
|
||||
return msec;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Semaphore API
|
||||
//--------------------------------------------------------------------+
|
||||
typedef OS_SEM osal_semaphore_def_t;
|
||||
typedef OS_ID osal_semaphore_t;
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline OS_ID osal_semaphore_create(osal_semaphore_def_t* semdef) {
|
||||
os_sem_init(semdef, 0);
|
||||
return semdef;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) {
|
||||
if ( !in_isr ) {
|
||||
os_sem_send(sem_hdl);
|
||||
} else {
|
||||
isr_sem_send(sem_hdl);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_wait (osal_semaphore_t sem_hdl, uint32_t msec) {
|
||||
return os_sem_wait(sem_hdl, msec2wait(msec)) != OS_R_TMO;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline void osal_semaphore_reset(osal_semaphore_t const sem_hdl) {
|
||||
// TODO: implement
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// MUTEX API (priority inheritance)
|
||||
//--------------------------------------------------------------------+
|
||||
typedef OS_MUT osal_mutex_def_t;
|
||||
typedef OS_ID osal_mutex_t;
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef)
|
||||
{
|
||||
os_mut_init(mdef);
|
||||
return mdef;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_lock (osal_mutex_t mutex_hdl, uint32_t msec)
|
||||
{
|
||||
return os_mut_wait(mutex_hdl, msec2wait(msec)) != OS_R_TMO;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl)
|
||||
{
|
||||
return os_mut_release(mutex_hdl) == OS_R_OK;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// QUEUE API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// role device/host is used by OS NONE for mutex (disable usb isr) only
|
||||
#define OSAL_QUEUE_DEF(_int_set, _name, _depth, _type) \
|
||||
os_mbx_declare(_name##__mbox, _depth); \
|
||||
_declare_box(_name##__pool, sizeof(_type), _depth); \
|
||||
osal_queue_def_t _name = { .depth = _depth, .item_sz = sizeof(_type), .pool = _name##__pool, .mbox = _name##__mbox };
|
||||
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint16_t depth;
|
||||
uint16_t item_sz;
|
||||
U32* pool;
|
||||
U32* mbox;
|
||||
}osal_queue_def_t;
|
||||
|
||||
typedef osal_queue_def_t* osal_queue_t;
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef)
|
||||
{
|
||||
os_mbx_init(qdef->mbox, (qdef->depth + 4) * 4);
|
||||
_init_box(qdef->pool, ((qdef->item_sz+3)/4)*(qdef->depth) + 3, qdef->item_sz);
|
||||
return qdef;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_receive(osal_queue_t qhdl, void* data, uint32_t msec)
|
||||
{
|
||||
void* buf;
|
||||
os_mbx_wait(qhdl->mbox, &buf, msec2wait(msec));
|
||||
memcpy(data, buf, qhdl->item_sz);
|
||||
_free_box(qhdl->pool, buf);
|
||||
return true;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in_isr)
|
||||
{
|
||||
void* buf = _alloc_box(qhdl->pool);
|
||||
memcpy(buf, data, qhdl->item_sz);
|
||||
if ( !in_isr )
|
||||
{
|
||||
os_mbx_send(qhdl->mbox, buf, 0xFFFF);
|
||||
}
|
||||
else
|
||||
{
|
||||
isr_mbx_send(qhdl->mbox, buf);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_empty(osal_queue_t qhdl)
|
||||
{
|
||||
return os_mbx_check(qhdl->mbox) == qhdl->depth;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -0,0 +1,210 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2018, hathach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#include "tusb_option.h"
|
||||
|
||||
#if CFG_TUD_ENABLED && (CFG_TUSB_MCU == OPT_MCU_RP2040) && CFG_TUD_RPI_PIO_USB
|
||||
|
||||
#include "pico.h"
|
||||
#include "pio_usb.h"
|
||||
#include "pio_usb_ll.h"
|
||||
|
||||
#include "device/dcd.h"
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// MACRO TYPEDEF CONSTANT ENUM DECLARATION
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
#define RHPORT_OFFSET 1
|
||||
#define RHPORT_PIO(_x) ((_x)-RHPORT_OFFSET)
|
||||
|
||||
//------------- -------------//
|
||||
static usb_device_t *usb_device = NULL;
|
||||
static usb_descriptor_buffers_t desc;
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* Device API
|
||||
*------------------------------------------------------------------*/
|
||||
|
||||
// Initialize controller to device mode
|
||||
void dcd_init (uint8_t rhport)
|
||||
{
|
||||
(void) rhport;
|
||||
|
||||
static pio_usb_configuration_t config = PIO_USB_DEFAULT_CONFIG;
|
||||
usb_device = pio_usb_device_init(&config, &desc);
|
||||
}
|
||||
|
||||
// Enable device interrupt
|
||||
void dcd_int_enable (uint8_t rhport)
|
||||
{
|
||||
(void) rhport;
|
||||
}
|
||||
|
||||
// Disable device interrupt
|
||||
void dcd_int_disable (uint8_t rhport)
|
||||
{
|
||||
(void) rhport;
|
||||
}
|
||||
|
||||
// Receive Set Address request, mcu port must also include status IN response
|
||||
void dcd_set_address (uint8_t rhport, uint8_t dev_addr)
|
||||
{
|
||||
// must be called before queuing status
|
||||
pio_usb_device_set_address(dev_addr);
|
||||
dcd_edpt_xfer(rhport, 0x80, NULL, 0);
|
||||
}
|
||||
|
||||
// Wake up host
|
||||
void dcd_remote_wakeup (uint8_t rhport)
|
||||
{
|
||||
(void) rhport;
|
||||
}
|
||||
|
||||
// Connect by enabling internal pull-up resistor on D+/D-
|
||||
void dcd_connect(uint8_t rhport)
|
||||
{
|
||||
(void) rhport;
|
||||
}
|
||||
|
||||
// Disconnect by disabling internal pull-up resistor on D+/D-
|
||||
void dcd_disconnect(uint8_t rhport)
|
||||
{
|
||||
(void) rhport;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Endpoint API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Configure endpoint's registers according to descriptor
|
||||
bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * desc_ep)
|
||||
{
|
||||
(void) rhport;
|
||||
return pio_usb_device_endpoint_open((uint8_t const*) desc_ep);
|
||||
}
|
||||
|
||||
void dcd_edpt_close_all (uint8_t rhport)
|
||||
{
|
||||
(void) rhport;
|
||||
}
|
||||
|
||||
// Submit a transfer, When complete dcd_event_xfer_complete() is invoked to notify the stack
|
||||
bool dcd_edpt_xfer (uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes)
|
||||
{
|
||||
(void) rhport;
|
||||
endpoint_t *ep = pio_usb_device_get_endpoint_by_address(ep_addr);
|
||||
return pio_usb_ll_transfer_start(ep, buffer, total_bytes);
|
||||
}
|
||||
|
||||
// Submit a transfer where is managed by FIFO, When complete dcd_event_xfer_complete() is invoked to notify the stack - optional, however, must be listed in usbd.c
|
||||
//bool dcd_edpt_xfer_fifo (uint8_t rhport, uint8_t ep_addr, tu_fifo_t * ff, uint16_t total_bytes)
|
||||
//{
|
||||
// (void) rhport;
|
||||
// (void) ep_addr;
|
||||
// (void) ff;
|
||||
// (void) total_bytes;
|
||||
// return false;
|
||||
//}
|
||||
|
||||
// Stall endpoint
|
||||
void dcd_edpt_stall (uint8_t rhport, uint8_t ep_addr)
|
||||
{
|
||||
(void) rhport;
|
||||
endpoint_t *ep = pio_usb_device_get_endpoint_by_address(ep_addr);
|
||||
ep->has_transfer = false;
|
||||
ep->stalled = true;
|
||||
}
|
||||
|
||||
// clear stall, data toggle is also reset to DATA0
|
||||
void dcd_edpt_clear_stall (uint8_t rhport, uint8_t ep_addr)
|
||||
{
|
||||
(void) rhport;
|
||||
endpoint_t *ep = pio_usb_device_get_endpoint_by_address(ep_addr);
|
||||
ep->data_id = 0;
|
||||
ep->stalled = false;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
//
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
static void __no_inline_not_in_flash_func(handle_endpoint_irq)(uint8_t tu_rhport, xfer_result_t result, volatile uint32_t* ep_reg)
|
||||
{
|
||||
const uint32_t ep_all = *ep_reg;
|
||||
|
||||
for(uint8_t ep_idx = 0; ep_idx < PIO_USB_EP_POOL_CNT; ep_idx++)
|
||||
{
|
||||
uint32_t const mask = (1u << ep_idx);
|
||||
|
||||
if (ep_all & mask)
|
||||
{
|
||||
endpoint_t* ep = PIO_USB_ENDPOINT(ep_idx);
|
||||
dcd_event_xfer_complete(tu_rhport, ep->ep_num, ep->actual_len, result, true);
|
||||
}
|
||||
}
|
||||
|
||||
// clear all
|
||||
(*ep_reg) &= ~ep_all;
|
||||
}
|
||||
|
||||
// IRQ Handler
|
||||
void __no_inline_not_in_flash_func(pio_usb_device_irq_handler)(uint8_t root_id)
|
||||
{
|
||||
uint8_t const tu_rhport = root_id + 1;
|
||||
root_port_t* rport = PIO_USB_ROOT_PORT(root_id);
|
||||
uint32_t const ints = rport->ints;
|
||||
|
||||
if (ints & PIO_USB_INTS_RESET_END_BITS)
|
||||
{
|
||||
dcd_event_bus_reset(tu_rhport, TUSB_SPEED_FULL, true);
|
||||
}
|
||||
|
||||
if (ints & PIO_USB_INTS_SETUP_REQ_BITS)
|
||||
{
|
||||
dcd_event_setup_received(tu_rhport, rport->setup_packet, true);
|
||||
}
|
||||
|
||||
if ( ints & PIO_USB_INTS_ENDPOINT_COMPLETE_BITS )
|
||||
{
|
||||
handle_endpoint_irq(tu_rhport, XFER_RESULT_SUCCESS, &rport->ep_complete);
|
||||
}
|
||||
|
||||
if ( ints & PIO_USB_INTS_ENDPOINT_STALLED_BITS )
|
||||
{
|
||||
handle_endpoint_irq(tu_rhport, XFER_RESULT_STALLED, &rport->ep_stalled);
|
||||
}
|
||||
|
||||
if ( ints & PIO_USB_INTS_ENDPOINT_ERROR_BITS )
|
||||
{
|
||||
handle_endpoint_irq(tu_rhport, XFER_RESULT_FAILED, &rport->ep_error);
|
||||
}
|
||||
|
||||
// clear all
|
||||
rport->ints &= ~ints;
|
||||
}
|
||||
|
||||
#endif
|
|
@ -0,0 +1,209 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2021 Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#include "tusb_option.h"
|
||||
|
||||
#if CFG_TUH_ENABLED && (CFG_TUSB_MCU == OPT_MCU_RP2040) && CFG_TUH_RPI_PIO_USB
|
||||
|
||||
#include "pico.h"
|
||||
#include "pio_usb.h"
|
||||
#include "pio_usb_ll.h"
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// INCLUDE
|
||||
//--------------------------------------------------------------------+
|
||||
#include "osal/osal.h"
|
||||
|
||||
#include "host/hcd.h"
|
||||
#include "host/usbh.h"
|
||||
|
||||
#define RHPORT_OFFSET 1
|
||||
#define RHPORT_PIO(_x) ((_x)-RHPORT_OFFSET)
|
||||
|
||||
static pio_usb_configuration_t pio_host_cfg = PIO_USB_DEFAULT_CONFIG;
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// HCD API
|
||||
//--------------------------------------------------------------------+
|
||||
bool hcd_configure(uint8_t rhport, uint32_t cfg_id, const void *cfg_param) {
|
||||
(void) rhport;
|
||||
TU_VERIFY(cfg_id == TUH_CFGID_RPI_PIO_USB_CONFIGURATION);
|
||||
memcpy(&pio_host_cfg, cfg_param, sizeof(pio_usb_configuration_t));
|
||||
return true;
|
||||
}
|
||||
|
||||
bool hcd_init(uint8_t rhport) {
|
||||
(void) rhport;
|
||||
|
||||
// To run USB SOF interrupt in core1, call this init in core1
|
||||
pio_usb_host_init(&pio_host_cfg);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void hcd_port_reset(uint8_t rhport) {
|
||||
uint8_t const pio_rhport = RHPORT_PIO(rhport);
|
||||
pio_usb_host_port_reset_start(pio_rhport);
|
||||
}
|
||||
|
||||
void hcd_port_reset_end(uint8_t rhport) {
|
||||
uint8_t const pio_rhport = RHPORT_PIO(rhport);
|
||||
pio_usb_host_port_reset_end(pio_rhport);
|
||||
}
|
||||
|
||||
bool hcd_port_connect_status(uint8_t rhport) {
|
||||
uint8_t const pio_rhport = RHPORT_PIO(rhport);
|
||||
|
||||
root_port_t *root = PIO_USB_ROOT_PORT(pio_rhport);
|
||||
port_pin_status_t line_state = pio_usb_bus_get_line_state(root);
|
||||
|
||||
return line_state != PORT_PIN_SE0;
|
||||
}
|
||||
|
||||
tusb_speed_t hcd_port_speed_get(uint8_t rhport) {
|
||||
// TODO determine link speed
|
||||
uint8_t const pio_rhport = RHPORT_PIO(rhport);
|
||||
return PIO_USB_ROOT_PORT(pio_rhport)->is_fullspeed ? TUSB_SPEED_FULL : TUSB_SPEED_LOW;
|
||||
}
|
||||
|
||||
// Close all opened endpoint belong to this device
|
||||
void hcd_device_close(uint8_t rhport, uint8_t dev_addr) {
|
||||
uint8_t const pio_rhport = RHPORT_PIO(rhport);
|
||||
pio_usb_host_close_device(pio_rhport, dev_addr);
|
||||
}
|
||||
|
||||
uint32_t hcd_frame_number(uint8_t rhport) {
|
||||
(void) rhport;
|
||||
return pio_usb_host_get_frame_number();
|
||||
}
|
||||
|
||||
void hcd_int_enable(uint8_t rhport) {
|
||||
(void) rhport;
|
||||
}
|
||||
|
||||
void hcd_int_disable(uint8_t rhport) {
|
||||
(void) rhport;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Endpoint API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
bool hcd_edpt_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_endpoint_t const *desc_ep) {
|
||||
hcd_devtree_info_t dev_tree;
|
||||
hcd_devtree_get_info(dev_addr, &dev_tree);
|
||||
bool const need_pre = (dev_tree.hub_addr && dev_tree.speed == TUSB_SPEED_LOW);
|
||||
|
||||
uint8_t const pio_rhport = RHPORT_PIO(rhport);
|
||||
return pio_usb_host_endpoint_open(pio_rhport, dev_addr, (uint8_t const *) desc_ep, need_pre);
|
||||
}
|
||||
|
||||
bool hcd_edpt_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr, uint8_t *buffer, uint16_t buflen) {
|
||||
uint8_t const pio_rhport = RHPORT_PIO(rhport);
|
||||
return pio_usb_host_endpoint_transfer(pio_rhport, dev_addr, ep_addr, buffer, buflen);
|
||||
}
|
||||
|
||||
bool hcd_edpt_abort_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr) {
|
||||
uint8_t const pio_rhport = RHPORT_PIO(rhport);
|
||||
return pio_usb_host_endpoint_abort_transfer(pio_rhport, dev_addr, ep_addr);
|
||||
}
|
||||
|
||||
bool hcd_setup_send(uint8_t rhport, uint8_t dev_addr, uint8_t const setup_packet[8]) {
|
||||
uint8_t const pio_rhport = RHPORT_PIO(rhport);
|
||||
return pio_usb_host_send_setup(pio_rhport, dev_addr, setup_packet);
|
||||
}
|
||||
|
||||
//bool hcd_edpt_busy(uint8_t dev_addr, uint8_t ep_addr)
|
||||
//{
|
||||
// // EPX is shared, so multiple device addresses and endpoint addresses share that
|
||||
// // so if any transfer is active on epx, we are busy. Interrupt endpoints have their own
|
||||
// // EPX so ep->active will only be busy if there is a pending transfer on that interrupt endpoint
|
||||
// // on that device
|
||||
// pico_trace("hcd_edpt_busy dev addr %d ep_addr 0x%x\r\n", dev_addr, ep_addr);
|
||||
// struct hw_endpoint *ep = get_dev_ep(dev_addr, ep_addr);
|
||||
// assert(ep);
|
||||
// bool busy = ep->active;
|
||||
// pico_trace("busy == %d\r\n", busy);
|
||||
// return busy;
|
||||
//}
|
||||
|
||||
bool hcd_edpt_clear_stall(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr) {
|
||||
(void) rhport;
|
||||
(void) dev_addr;
|
||||
(void) ep_addr;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static void __no_inline_not_in_flash_func(handle_endpoint_irq)(root_port_t *rport, xfer_result_t result,
|
||||
volatile uint32_t *ep_reg) {
|
||||
(void) rport;
|
||||
const uint32_t ep_all = *ep_reg;
|
||||
|
||||
for ( uint8_t ep_idx = 0; ep_idx < PIO_USB_EP_POOL_CNT; ep_idx++ ) {
|
||||
uint32_t const mask = (1u << ep_idx);
|
||||
|
||||
if ( ep_all & mask ) {
|
||||
endpoint_t * ep = PIO_USB_ENDPOINT(ep_idx);
|
||||
hcd_event_xfer_complete(ep->dev_addr, ep->ep_num, ep->actual_len, result, true);
|
||||
}
|
||||
}
|
||||
|
||||
// clear all
|
||||
(*ep_reg) &= ~ep_all;
|
||||
}
|
||||
|
||||
// IRQ Handler
|
||||
void __no_inline_not_in_flash_func(pio_usb_host_irq_handler)(uint8_t root_id) {
|
||||
uint8_t const tu_rhport = root_id + 1;
|
||||
root_port_t *rport = PIO_USB_ROOT_PORT(root_id);
|
||||
uint32_t const ints = rport->ints;
|
||||
|
||||
if ( ints & PIO_USB_INTS_ENDPOINT_COMPLETE_BITS ) {
|
||||
handle_endpoint_irq(rport, XFER_RESULT_SUCCESS, &rport->ep_complete);
|
||||
}
|
||||
|
||||
if ( ints & PIO_USB_INTS_ENDPOINT_STALLED_BITS ) {
|
||||
handle_endpoint_irq(rport, XFER_RESULT_STALLED, &rport->ep_stalled);
|
||||
}
|
||||
|
||||
if ( ints & PIO_USB_INTS_ENDPOINT_ERROR_BITS ) {
|
||||
handle_endpoint_irq(rport, XFER_RESULT_FAILED, &rport->ep_error);
|
||||
}
|
||||
|
||||
if ( ints & PIO_USB_INTS_CONNECT_BITS ) {
|
||||
hcd_event_device_attach(tu_rhport, true);
|
||||
}
|
||||
|
||||
if ( ints & PIO_USB_INTS_DISCONNECT_BITS ) {
|
||||
hcd_event_device_remove(tu_rhport, true);
|
||||
}
|
||||
|
||||
// clear all
|
||||
rport->ints &= ~ints;
|
||||
}
|
||||
|
||||
#endif
|
|
@ -0,0 +1,578 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#include "tusb_option.h"
|
||||
|
||||
#if CFG_TUD_ENABLED && (CFG_TUSB_MCU == OPT_MCU_RP2040) && !CFG_TUD_RPI_PIO_USB
|
||||
|
||||
#include "pico.h"
|
||||
#include "hardware/sync.h"
|
||||
#include "rp2040_usb.h"
|
||||
|
||||
#if TUD_OPT_RP2040_USB_DEVICE_ENUMERATION_FIX
|
||||
#include "pico/fix/rp2040_usb_device_enumeration.h"
|
||||
#endif
|
||||
|
||||
#include "device/dcd.h"
|
||||
|
||||
// Current implementation force vbus detection as always present, causing device think it is always plugged into host.
|
||||
// Therefore it cannot detect disconnect event, mistaken it as suspend.
|
||||
// Note: won't work if change to 0 (for now)
|
||||
#define FORCE_VBUS_DETECT 1
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* Low level controller
|
||||
*------------------------------------------------------------------*/
|
||||
|
||||
// Init these in dcd_init
|
||||
static uint8_t *next_buffer_ptr;
|
||||
|
||||
// USB_MAX_ENDPOINTS Endpoints, direction TUSB_DIR_OUT for out and TUSB_DIR_IN for in.
|
||||
static struct hw_endpoint hw_endpoints[USB_MAX_ENDPOINTS][2];
|
||||
|
||||
// SOF may be used by remote wakeup as RESUME, this indicate whether SOF is actually used by usbd
|
||||
static bool _sof_enable = false;
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline struct hw_endpoint *hw_endpoint_get_by_num(uint8_t num, tusb_dir_t dir)
|
||||
{
|
||||
return &hw_endpoints[num][dir];
|
||||
}
|
||||
|
||||
static struct hw_endpoint *hw_endpoint_get_by_addr(uint8_t ep_addr)
|
||||
{
|
||||
uint8_t num = tu_edpt_number(ep_addr);
|
||||
tusb_dir_t dir = tu_edpt_dir(ep_addr);
|
||||
return hw_endpoint_get_by_num(num, dir);
|
||||
}
|
||||
|
||||
static void _hw_endpoint_alloc(struct hw_endpoint *ep, uint8_t transfer_type)
|
||||
{
|
||||
// size must be multiple of 64
|
||||
uint size = tu_div_ceil(ep->wMaxPacketSize, 64) * 64u;
|
||||
|
||||
// double buffered Bulk endpoint
|
||||
if ( transfer_type == TUSB_XFER_BULK )
|
||||
{
|
||||
size *= 2u;
|
||||
}
|
||||
|
||||
ep->hw_data_buf = next_buffer_ptr;
|
||||
next_buffer_ptr += size;
|
||||
|
||||
assert(((uintptr_t )next_buffer_ptr & 0b111111u) == 0);
|
||||
uint dpram_offset = hw_data_offset(ep->hw_data_buf);
|
||||
hard_assert(hw_data_offset(next_buffer_ptr) <= USB_DPRAM_MAX);
|
||||
|
||||
pico_info(" Allocated %d bytes at offset 0x%x (0x%p)\r\n", size, dpram_offset, ep->hw_data_buf);
|
||||
|
||||
// Fill in endpoint control register with buffer offset
|
||||
uint32_t const reg = EP_CTRL_ENABLE_BITS | ((uint)transfer_type << EP_CTRL_BUFFER_TYPE_LSB) | dpram_offset;
|
||||
|
||||
*ep->endpoint_control = reg;
|
||||
}
|
||||
|
||||
static void _hw_endpoint_close(struct hw_endpoint *ep)
|
||||
{
|
||||
// Clear hardware registers and then zero the struct
|
||||
// Clears endpoint enable
|
||||
*ep->endpoint_control = 0;
|
||||
// Clears buffer available, etc
|
||||
*ep->buffer_control = 0;
|
||||
// Clear any endpoint state
|
||||
memset(ep, 0, sizeof(struct hw_endpoint));
|
||||
|
||||
// Reclaim buffer space if all endpoints are closed
|
||||
bool reclaim_buffers = true;
|
||||
for ( uint8_t i = 1; i < USB_MAX_ENDPOINTS; i++ )
|
||||
{
|
||||
if (hw_endpoint_get_by_num(i, TUSB_DIR_OUT)->hw_data_buf != NULL || hw_endpoint_get_by_num(i, TUSB_DIR_IN)->hw_data_buf != NULL)
|
||||
{
|
||||
reclaim_buffers = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (reclaim_buffers)
|
||||
{
|
||||
next_buffer_ptr = &usb_dpram->epx_data[0];
|
||||
}
|
||||
}
|
||||
|
||||
static void hw_endpoint_close(uint8_t ep_addr)
|
||||
{
|
||||
struct hw_endpoint *ep = hw_endpoint_get_by_addr(ep_addr);
|
||||
_hw_endpoint_close(ep);
|
||||
}
|
||||
|
||||
static void hw_endpoint_init(uint8_t ep_addr, uint16_t wMaxPacketSize, uint8_t transfer_type)
|
||||
{
|
||||
struct hw_endpoint *ep = hw_endpoint_get_by_addr(ep_addr);
|
||||
|
||||
const uint8_t num = tu_edpt_number(ep_addr);
|
||||
const tusb_dir_t dir = tu_edpt_dir(ep_addr);
|
||||
|
||||
ep->ep_addr = ep_addr;
|
||||
|
||||
// For device, IN is a tx transfer and OUT is an rx transfer
|
||||
ep->rx = (dir == TUSB_DIR_OUT);
|
||||
|
||||
ep->next_pid = 0u;
|
||||
ep->wMaxPacketSize = wMaxPacketSize;
|
||||
ep->transfer_type = transfer_type;
|
||||
|
||||
// Every endpoint has a buffer control register in dpram
|
||||
if ( dir == TUSB_DIR_IN )
|
||||
{
|
||||
ep->buffer_control = &usb_dpram->ep_buf_ctrl[num].in;
|
||||
}
|
||||
else
|
||||
{
|
||||
ep->buffer_control = &usb_dpram->ep_buf_ctrl[num].out;
|
||||
}
|
||||
|
||||
// Clear existing buffer control state
|
||||
*ep->buffer_control = 0;
|
||||
|
||||
if ( num == 0 )
|
||||
{
|
||||
// EP0 has no endpoint control register because the buffer offsets are fixed
|
||||
ep->endpoint_control = NULL;
|
||||
|
||||
// Buffer offset is fixed (also double buffered)
|
||||
ep->hw_data_buf = (uint8_t*) &usb_dpram->ep0_buf_a[0];
|
||||
}
|
||||
else
|
||||
{
|
||||
// Set the endpoint control register (starts at EP1, hence num-1)
|
||||
if ( dir == TUSB_DIR_IN )
|
||||
{
|
||||
ep->endpoint_control = &usb_dpram->ep_ctrl[num - 1].in;
|
||||
}
|
||||
else
|
||||
{
|
||||
ep->endpoint_control = &usb_dpram->ep_ctrl[num - 1].out;
|
||||
}
|
||||
|
||||
// alloc a buffer and fill in endpoint control register
|
||||
_hw_endpoint_alloc(ep, transfer_type);
|
||||
}
|
||||
}
|
||||
|
||||
static void hw_endpoint_xfer(uint8_t ep_addr, uint8_t *buffer, uint16_t total_bytes)
|
||||
{
|
||||
struct hw_endpoint *ep = hw_endpoint_get_by_addr(ep_addr);
|
||||
hw_endpoint_xfer_start(ep, buffer, total_bytes);
|
||||
}
|
||||
|
||||
static void __tusb_irq_path_func(hw_handle_buff_status)(void)
|
||||
{
|
||||
uint32_t remaining_buffers = usb_hw->buf_status;
|
||||
pico_trace("buf_status = 0x%08lx\r\n", remaining_buffers);
|
||||
uint bit = 1u;
|
||||
for (uint8_t i = 0; remaining_buffers && i < USB_MAX_ENDPOINTS * 2; i++)
|
||||
{
|
||||
if (remaining_buffers & bit)
|
||||
{
|
||||
// clear this in advance
|
||||
usb_hw_clear->buf_status = bit;
|
||||
|
||||
// IN transfer for even i, OUT transfer for odd i
|
||||
struct hw_endpoint *ep = hw_endpoint_get_by_num(i >> 1u, (i & 1u) ? TUSB_DIR_OUT : TUSB_DIR_IN);
|
||||
|
||||
// Continue xfer
|
||||
bool done = hw_endpoint_xfer_continue(ep);
|
||||
if (done)
|
||||
{
|
||||
// Notify
|
||||
dcd_event_xfer_complete(0, ep->ep_addr, ep->xferred_len, XFER_RESULT_SUCCESS, true);
|
||||
hw_endpoint_reset_transfer(ep);
|
||||
}
|
||||
remaining_buffers &= ~bit;
|
||||
}
|
||||
bit <<= 1u;
|
||||
}
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline void reset_ep0_pid(void)
|
||||
{
|
||||
// If we have finished this transfer on EP0 set pid back to 1 for next
|
||||
// setup transfer. Also clear a stall in case
|
||||
uint8_t addrs[] = {0x0, 0x80};
|
||||
for (uint i = 0 ; i < TU_ARRAY_SIZE(addrs); i++)
|
||||
{
|
||||
struct hw_endpoint *ep = hw_endpoint_get_by_addr(addrs[i]);
|
||||
ep->next_pid = 1u;
|
||||
}
|
||||
}
|
||||
|
||||
static void __tusb_irq_path_func(reset_non_control_endpoints)(void)
|
||||
{
|
||||
// Disable all non-control
|
||||
for ( uint8_t i = 0; i < USB_MAX_ENDPOINTS-1; i++ )
|
||||
{
|
||||
usb_dpram->ep_ctrl[i].in = 0;
|
||||
usb_dpram->ep_ctrl[i].out = 0;
|
||||
}
|
||||
|
||||
// clear non-control hw endpoints
|
||||
tu_memclr(hw_endpoints[1], sizeof(hw_endpoints) - 2*sizeof(hw_endpoint_t));
|
||||
|
||||
// reclaim buffer space
|
||||
next_buffer_ptr = &usb_dpram->epx_data[0];
|
||||
}
|
||||
|
||||
static void __tusb_irq_path_func(dcd_rp2040_irq)(void)
|
||||
{
|
||||
uint32_t const status = usb_hw->ints;
|
||||
uint32_t handled = 0;
|
||||
|
||||
if ( status & USB_INTF_DEV_SOF_BITS )
|
||||
{
|
||||
bool keep_sof_alive = false;
|
||||
|
||||
handled |= USB_INTF_DEV_SOF_BITS;
|
||||
|
||||
#if TUD_OPT_RP2040_USB_DEVICE_UFRAME_FIX
|
||||
// Errata 15 workaround for Device Bulk-In endpoint
|
||||
e15_last_sof = time_us_32();
|
||||
|
||||
for ( uint8_t i = 0; i < USB_MAX_ENDPOINTS; i++ )
|
||||
{
|
||||
struct hw_endpoint * ep = hw_endpoint_get_by_num(i, TUSB_DIR_IN);
|
||||
|
||||
// Active Bulk IN endpoint requires SOF
|
||||
if ( (ep->transfer_type == TUSB_XFER_BULK) && ep->active )
|
||||
{
|
||||
keep_sof_alive = true;
|
||||
|
||||
hw_endpoint_lock_update(ep, 1);
|
||||
|
||||
// Deferred enable?
|
||||
if ( ep->pending )
|
||||
{
|
||||
ep->pending = 0;
|
||||
hw_endpoint_start_next_buffer(ep);
|
||||
}
|
||||
|
||||
hw_endpoint_lock_update(ep, -1);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
// disable SOF interrupt if it is used for RESUME in remote wakeup
|
||||
if ( !keep_sof_alive && !_sof_enable ) usb_hw_clear->inte = USB_INTS_DEV_SOF_BITS;
|
||||
|
||||
dcd_event_sof(0, usb_hw->sof_rd & USB_SOF_RD_BITS, true);
|
||||
}
|
||||
|
||||
// xfer events are handled before setup req. So if a transfer completes immediately
|
||||
// before closing the EP, the events will be delivered in same order.
|
||||
if ( status & USB_INTS_BUFF_STATUS_BITS )
|
||||
{
|
||||
handled |= USB_INTS_BUFF_STATUS_BITS;
|
||||
hw_handle_buff_status();
|
||||
}
|
||||
|
||||
if ( status & USB_INTS_SETUP_REQ_BITS )
|
||||
{
|
||||
handled |= USB_INTS_SETUP_REQ_BITS;
|
||||
uint8_t const * setup = remove_volatile_cast(uint8_t const*, &usb_dpram->setup_packet);
|
||||
|
||||
// reset pid to both 1 (data and ack)
|
||||
reset_ep0_pid();
|
||||
|
||||
// Pass setup packet to tiny usb
|
||||
dcd_event_setup_received(0, setup, true);
|
||||
usb_hw_clear->sie_status = USB_SIE_STATUS_SETUP_REC_BITS;
|
||||
}
|
||||
|
||||
#if FORCE_VBUS_DETECT == 0
|
||||
// Since we force VBUS detect On, device will always think it is connected and
|
||||
// couldn't distinguish between disconnect and suspend
|
||||
if (status & USB_INTS_DEV_CONN_DIS_BITS)
|
||||
{
|
||||
handled |= USB_INTS_DEV_CONN_DIS_BITS;
|
||||
|
||||
if ( usb_hw->sie_status & USB_SIE_STATUS_CONNECTED_BITS )
|
||||
{
|
||||
// Connected: nothing to do
|
||||
}else
|
||||
{
|
||||
// Disconnected
|
||||
dcd_event_bus_signal(0, DCD_EVENT_UNPLUGGED, true);
|
||||
}
|
||||
|
||||
usb_hw_clear->sie_status = USB_SIE_STATUS_CONNECTED_BITS;
|
||||
}
|
||||
#endif
|
||||
|
||||
// SE0 for 2.5 us or more (will last at least 10ms)
|
||||
if ( status & USB_INTS_BUS_RESET_BITS )
|
||||
{
|
||||
pico_trace("BUS RESET\r\n");
|
||||
|
||||
handled |= USB_INTS_BUS_RESET_BITS;
|
||||
|
||||
usb_hw->dev_addr_ctrl = 0;
|
||||
reset_non_control_endpoints();
|
||||
dcd_event_bus_reset(0, TUSB_SPEED_FULL, true);
|
||||
usb_hw_clear->sie_status = USB_SIE_STATUS_BUS_RESET_BITS;
|
||||
|
||||
#if TUD_OPT_RP2040_USB_DEVICE_ENUMERATION_FIX
|
||||
// Only run enumeration workaround if pull up is enabled
|
||||
if ( usb_hw->sie_ctrl & USB_SIE_CTRL_PULLUP_EN_BITS ) rp2040_usb_device_enumeration_fix();
|
||||
#endif
|
||||
}
|
||||
|
||||
/* Note from pico datasheet 4.1.2.6.4 (v1.2)
|
||||
* If you enable the suspend interrupt, it is likely you will see a suspend interrupt when
|
||||
* the device is first connected but the bus is idle. The bus can be idle for a few ms before
|
||||
* the host begins sending start of frame packets. You will also see a suspend interrupt
|
||||
* when the device is disconnected if you do not have a VBUS detect circuit connected. This is
|
||||
* because without VBUS detection, it is impossible to tell the difference between
|
||||
* being disconnected and suspended.
|
||||
*/
|
||||
if ( status & USB_INTS_DEV_SUSPEND_BITS )
|
||||
{
|
||||
handled |= USB_INTS_DEV_SUSPEND_BITS;
|
||||
dcd_event_bus_signal(0, DCD_EVENT_SUSPEND, true);
|
||||
usb_hw_clear->sie_status = USB_SIE_STATUS_SUSPENDED_BITS;
|
||||
}
|
||||
|
||||
if ( status & USB_INTS_DEV_RESUME_FROM_HOST_BITS )
|
||||
{
|
||||
handled |= USB_INTS_DEV_RESUME_FROM_HOST_BITS;
|
||||
dcd_event_bus_signal(0, DCD_EVENT_RESUME, true);
|
||||
usb_hw_clear->sie_status = USB_SIE_STATUS_RESUME_BITS;
|
||||
}
|
||||
|
||||
if ( status ^ handled )
|
||||
{
|
||||
panic("Unhandled IRQ 0x%x\n", (uint) (status ^ handled));
|
||||
}
|
||||
}
|
||||
|
||||
#define USB_INTS_ERROR_BITS ( \
|
||||
USB_INTS_ERROR_DATA_SEQ_BITS | \
|
||||
USB_INTS_ERROR_BIT_STUFF_BITS | \
|
||||
USB_INTS_ERROR_CRC_BITS | \
|
||||
USB_INTS_ERROR_RX_OVERFLOW_BITS | \
|
||||
USB_INTS_ERROR_RX_TIMEOUT_BITS)
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* Controller API
|
||||
*------------------------------------------------------------------*/
|
||||
|
||||
// older SDK
|
||||
#ifndef PICO_SHARED_IRQ_HANDLER_HIGHEST_ORDER_PRIORITY
|
||||
#define PICO_SHARED_IRQ_HANDLER_HIGHEST_ORDER_PRIORITY 0xff
|
||||
#endif
|
||||
|
||||
void dcd_init (uint8_t rhport)
|
||||
{
|
||||
assert(rhport == 0);
|
||||
|
||||
// Reset hardware to default state
|
||||
rp2040_usb_init();
|
||||
|
||||
#if FORCE_VBUS_DETECT
|
||||
// Force VBUS detect so the device thinks it is plugged into a host
|
||||
usb_hw->pwr = USB_USB_PWR_VBUS_DETECT_BITS | USB_USB_PWR_VBUS_DETECT_OVERRIDE_EN_BITS;
|
||||
#endif
|
||||
|
||||
irq_add_shared_handler(USBCTRL_IRQ, dcd_rp2040_irq, PICO_SHARED_IRQ_HANDLER_HIGHEST_ORDER_PRIORITY);
|
||||
|
||||
// Init control endpoints
|
||||
tu_memclr(hw_endpoints[0], 2*sizeof(hw_endpoint_t));
|
||||
hw_endpoint_init(0x0, 64, TUSB_XFER_CONTROL);
|
||||
hw_endpoint_init(0x80, 64, TUSB_XFER_CONTROL);
|
||||
|
||||
// Init non-control endpoints
|
||||
reset_non_control_endpoints();
|
||||
|
||||
// Initializes the USB peripheral for device mode and enables it.
|
||||
// Don't need to enable the pull up here. Force VBUS
|
||||
usb_hw->main_ctrl = USB_MAIN_CTRL_CONTROLLER_EN_BITS;
|
||||
|
||||
// Enable individual controller IRQS here. Processor interrupt enable will be used
|
||||
// for the global interrupt enable...
|
||||
// Note: Force VBUS detect cause disconnection not detectable
|
||||
usb_hw->sie_ctrl = USB_SIE_CTRL_EP0_INT_1BUF_BITS;
|
||||
usb_hw->inte = USB_INTS_BUFF_STATUS_BITS | USB_INTS_BUS_RESET_BITS | USB_INTS_SETUP_REQ_BITS |
|
||||
USB_INTS_DEV_SUSPEND_BITS | USB_INTS_DEV_RESUME_FROM_HOST_BITS |
|
||||
(FORCE_VBUS_DETECT ? 0 : USB_INTS_DEV_CONN_DIS_BITS);
|
||||
|
||||
dcd_connect(rhport);
|
||||
}
|
||||
|
||||
void dcd_int_enable(__unused uint8_t rhport)
|
||||
{
|
||||
assert(rhport == 0);
|
||||
irq_set_enabled(USBCTRL_IRQ, true);
|
||||
}
|
||||
|
||||
void dcd_int_disable(__unused uint8_t rhport)
|
||||
{
|
||||
assert(rhport == 0);
|
||||
irq_set_enabled(USBCTRL_IRQ, false);
|
||||
}
|
||||
|
||||
void dcd_set_address (__unused uint8_t rhport, __unused uint8_t dev_addr)
|
||||
{
|
||||
assert(rhport == 0);
|
||||
|
||||
// Can't set device address in hardware until status xfer has complete
|
||||
// Send 0len complete response on EP0 IN
|
||||
hw_endpoint_xfer(0x80, NULL, 0);
|
||||
}
|
||||
|
||||
void dcd_remote_wakeup(__unused uint8_t rhport)
|
||||
{
|
||||
pico_info("dcd_remote_wakeup %d\n", rhport);
|
||||
assert(rhport == 0);
|
||||
|
||||
// since RESUME interrupt is not triggered if we are the one initiate
|
||||
// briefly enable SOF to notify usbd when bus is ready
|
||||
usb_hw_set->inte = USB_INTS_DEV_SOF_BITS;
|
||||
usb_hw_set->sie_ctrl = USB_SIE_CTRL_RESUME_BITS;
|
||||
}
|
||||
|
||||
// disconnect by disabling internal pull-up resistor on D+/D-
|
||||
void dcd_disconnect(__unused uint8_t rhport)
|
||||
{
|
||||
(void) rhport;
|
||||
usb_hw_clear->sie_ctrl = USB_SIE_CTRL_PULLUP_EN_BITS;
|
||||
}
|
||||
|
||||
// connect by enabling internal pull-up resistor on D+/D-
|
||||
void dcd_connect(__unused uint8_t rhport)
|
||||
{
|
||||
(void) rhport;
|
||||
usb_hw_set->sie_ctrl = USB_SIE_CTRL_PULLUP_EN_BITS;
|
||||
}
|
||||
|
||||
void dcd_sof_enable(uint8_t rhport, bool en)
|
||||
{
|
||||
(void) rhport;
|
||||
|
||||
_sof_enable = en;
|
||||
|
||||
if (en)
|
||||
{
|
||||
usb_hw_set->inte = USB_INTS_DEV_SOF_BITS;
|
||||
}else
|
||||
{
|
||||
// Don't clear immediately if the SOF workaround is in use.
|
||||
// The SOF handler will conditionally disable the interrupt.
|
||||
#if !TUD_OPT_RP2040_USB_DEVICE_UFRAME_FIX
|
||||
usb_hw_clear->inte = USB_INTS_DEV_SOF_BITS;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* DCD Endpoint port
|
||||
*------------------------------------------------------------------*/
|
||||
|
||||
void dcd_edpt0_status_complete(uint8_t rhport, tusb_control_request_t const * request)
|
||||
{
|
||||
(void) rhport;
|
||||
|
||||
if ( request->bmRequestType_bit.recipient == TUSB_REQ_RCPT_DEVICE &&
|
||||
request->bmRequestType_bit.type == TUSB_REQ_TYPE_STANDARD &&
|
||||
request->bRequest == TUSB_REQ_SET_ADDRESS )
|
||||
{
|
||||
usb_hw->dev_addr_ctrl = (uint8_t) request->wValue;
|
||||
}
|
||||
}
|
||||
|
||||
bool dcd_edpt_open (__unused uint8_t rhport, tusb_desc_endpoint_t const * desc_edpt)
|
||||
{
|
||||
assert(rhport == 0);
|
||||
hw_endpoint_init(desc_edpt->bEndpointAddress, tu_edpt_packet_size(desc_edpt), desc_edpt->bmAttributes.xfer);
|
||||
return true;
|
||||
}
|
||||
|
||||
void dcd_edpt_close_all (uint8_t rhport)
|
||||
{
|
||||
(void) rhport;
|
||||
|
||||
// may need to use EP Abort
|
||||
reset_non_control_endpoints();
|
||||
}
|
||||
|
||||
bool dcd_edpt_xfer(__unused uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes)
|
||||
{
|
||||
assert(rhport == 0);
|
||||
hw_endpoint_xfer(ep_addr, buffer, total_bytes);
|
||||
return true;
|
||||
}
|
||||
|
||||
void dcd_edpt_stall(uint8_t rhport, uint8_t ep_addr)
|
||||
{
|
||||
(void) rhport;
|
||||
|
||||
if ( tu_edpt_number(ep_addr) == 0 )
|
||||
{
|
||||
// A stall on EP0 has to be armed so it can be cleared on the next setup packet
|
||||
usb_hw_set->ep_stall_arm = (tu_edpt_dir(ep_addr) == TUSB_DIR_IN) ? USB_EP_STALL_ARM_EP0_IN_BITS : USB_EP_STALL_ARM_EP0_OUT_BITS;
|
||||
}
|
||||
|
||||
struct hw_endpoint *ep = hw_endpoint_get_by_addr(ep_addr);
|
||||
|
||||
// stall and clear current pending buffer
|
||||
// may need to use EP_ABORT
|
||||
_hw_endpoint_buffer_control_set_value32(ep, USB_BUF_CTRL_STALL);
|
||||
}
|
||||
|
||||
void dcd_edpt_clear_stall(uint8_t rhport, uint8_t ep_addr)
|
||||
{
|
||||
(void) rhport;
|
||||
|
||||
if (tu_edpt_number(ep_addr))
|
||||
{
|
||||
struct hw_endpoint *ep = hw_endpoint_get_by_addr(ep_addr);
|
||||
|
||||
// clear stall also reset toggle to DATA0, ready for next transfer
|
||||
ep->next_pid = 0;
|
||||
_hw_endpoint_buffer_control_clear_mask32(ep, USB_BUF_CTRL_STALL);
|
||||
}
|
||||
}
|
||||
|
||||
void dcd_edpt_close (uint8_t rhport, uint8_t ep_addr)
|
||||
{
|
||||
(void) rhport;
|
||||
|
||||
pico_trace("dcd_edpt_close %02x\r\n", ep_addr);
|
||||
hw_endpoint_close(ep_addr);
|
||||
}
|
||||
|
||||
void __tusb_irq_path_func(dcd_int_handler)(uint8_t rhport)
|
||||
{
|
||||
(void) rhport;
|
||||
dcd_rp2040_irq();
|
||||
}
|
||||
|
||||
#endif
|
|
@ -0,0 +1,637 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
|
||||
* Copyright (c) 2021 Ha Thach (tinyusb.org) for Double Buffered
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#include "tusb_option.h"
|
||||
|
||||
#if CFG_TUH_ENABLED && (CFG_TUSB_MCU == OPT_MCU_RP2040) && !CFG_TUH_RPI_PIO_USB && !CFG_TUH_MAX3421
|
||||
|
||||
#include "pico.h"
|
||||
#include "rp2040_usb.h"
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// INCLUDE
|
||||
//--------------------------------------------------------------------+
|
||||
#include "osal/osal.h"
|
||||
|
||||
#include "host/hcd.h"
|
||||
#include "host/usbh.h"
|
||||
|
||||
// port 0 is native USB port, other is counted as software PIO
|
||||
#define RHPORT_NATIVE 0
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Low level rp2040 controller functions
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
#ifndef PICO_USB_HOST_INTERRUPT_ENDPOINTS
|
||||
#define PICO_USB_HOST_INTERRUPT_ENDPOINTS (USB_MAX_ENDPOINTS - 1)
|
||||
#endif
|
||||
static_assert(PICO_USB_HOST_INTERRUPT_ENDPOINTS <= USB_MAX_ENDPOINTS, "");
|
||||
|
||||
// Host mode uses one shared endpoint register for non-interrupt endpoint
|
||||
static struct hw_endpoint ep_pool[1 + PICO_USB_HOST_INTERRUPT_ENDPOINTS];
|
||||
#define epx (ep_pool[0])
|
||||
|
||||
// Flags we set by default in sie_ctrl (we add other bits on top)
|
||||
enum {
|
||||
SIE_CTRL_BASE = USB_SIE_CTRL_SOF_EN_BITS | USB_SIE_CTRL_KEEP_ALIVE_EN_BITS |
|
||||
USB_SIE_CTRL_PULLDOWN_EN_BITS | USB_SIE_CTRL_EP0_INT_1BUF_BITS
|
||||
};
|
||||
|
||||
static struct hw_endpoint *get_dev_ep(uint8_t dev_addr, uint8_t ep_addr)
|
||||
{
|
||||
uint8_t num = tu_edpt_number(ep_addr);
|
||||
if ( num == 0 ) return &epx;
|
||||
|
||||
for ( uint32_t i = 1; i < TU_ARRAY_SIZE(ep_pool); i++ )
|
||||
{
|
||||
struct hw_endpoint *ep = &ep_pool[i];
|
||||
if ( ep->configured && (ep->dev_addr == dev_addr) && (ep->ep_addr == ep_addr) ) return ep;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline uint8_t dev_speed(void)
|
||||
{
|
||||
return (usb_hw->sie_status & USB_SIE_STATUS_SPEED_BITS) >> USB_SIE_STATUS_SPEED_LSB;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool need_pre(uint8_t dev_addr)
|
||||
{
|
||||
// If this device is different to the speed of the root device
|
||||
// (i.e. is a low speed device on a full speed hub) then need pre
|
||||
return hcd_port_speed_get(0) != tuh_speed_get(dev_addr);
|
||||
}
|
||||
|
||||
static void __tusb_irq_path_func(hw_xfer_complete)(struct hw_endpoint *ep, xfer_result_t xfer_result)
|
||||
{
|
||||
// Mark transfer as done before we tell the tinyusb stack
|
||||
uint8_t dev_addr = ep->dev_addr;
|
||||
uint8_t ep_addr = ep->ep_addr;
|
||||
uint xferred_len = ep->xferred_len;
|
||||
hw_endpoint_reset_transfer(ep);
|
||||
hcd_event_xfer_complete(dev_addr, ep_addr, xferred_len, xfer_result, true);
|
||||
}
|
||||
|
||||
static void __tusb_irq_path_func(_handle_buff_status_bit)(uint bit, struct hw_endpoint *ep)
|
||||
{
|
||||
usb_hw_clear->buf_status = bit;
|
||||
// EP may have been stalled?
|
||||
assert(ep->active);
|
||||
bool done = hw_endpoint_xfer_continue(ep);
|
||||
if ( done )
|
||||
{
|
||||
hw_xfer_complete(ep, XFER_RESULT_SUCCESS);
|
||||
}
|
||||
}
|
||||
|
||||
static void __tusb_irq_path_func(hw_handle_buff_status)(void)
|
||||
{
|
||||
uint32_t remaining_buffers = usb_hw->buf_status;
|
||||
pico_trace("buf_status 0x%08x\n", remaining_buffers);
|
||||
|
||||
// Check EPX first
|
||||
uint bit = 0b1;
|
||||
if ( remaining_buffers & bit )
|
||||
{
|
||||
remaining_buffers &= ~bit;
|
||||
struct hw_endpoint * ep = &epx;
|
||||
|
||||
uint32_t ep_ctrl = *ep->endpoint_control;
|
||||
if ( ep_ctrl & EP_CTRL_DOUBLE_BUFFERED_BITS )
|
||||
{
|
||||
TU_LOG(3, "Double Buffered: ");
|
||||
}
|
||||
else
|
||||
{
|
||||
TU_LOG(3, "Single Buffered: ");
|
||||
}
|
||||
TU_LOG_HEX(3, ep_ctrl);
|
||||
|
||||
_handle_buff_status_bit(bit, ep);
|
||||
}
|
||||
|
||||
// Check "interrupt" (asynchronous) endpoints for both IN and OUT
|
||||
for ( uint i = 1; i <= USB_HOST_INTERRUPT_ENDPOINTS && remaining_buffers; i++ )
|
||||
{
|
||||
// EPX is bit 0 & 1
|
||||
// IEP1 IN is bit 2
|
||||
// IEP1 OUT is bit 3
|
||||
// IEP2 IN is bit 4
|
||||
// IEP2 OUT is bit 5
|
||||
// IEP3 IN is bit 6
|
||||
// IEP3 OUT is bit 7
|
||||
// etc
|
||||
for ( uint j = 0; j < 2; j++ )
|
||||
{
|
||||
bit = 1 << (i * 2 + j);
|
||||
if ( remaining_buffers & bit )
|
||||
{
|
||||
remaining_buffers &= ~bit;
|
||||
_handle_buff_status_bit(bit, &ep_pool[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( remaining_buffers )
|
||||
{
|
||||
panic("Unhandled buffer %d\n", remaining_buffers);
|
||||
}
|
||||
}
|
||||
|
||||
static void __tusb_irq_path_func(hw_trans_complete)(void)
|
||||
{
|
||||
if (usb_hw->sie_ctrl & USB_SIE_CTRL_SEND_SETUP_BITS)
|
||||
{
|
||||
pico_trace("Sent setup packet\n");
|
||||
struct hw_endpoint *ep = &epx;
|
||||
assert(ep->active);
|
||||
// Set transferred length to 8 for a setup packet
|
||||
ep->xferred_len = 8;
|
||||
hw_xfer_complete(ep, XFER_RESULT_SUCCESS);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Don't care. Will handle this in buff status
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
static void __tusb_irq_path_func(hcd_rp2040_irq)(void)
|
||||
{
|
||||
uint32_t status = usb_hw->ints;
|
||||
uint32_t handled = 0;
|
||||
|
||||
if ( status & USB_INTS_HOST_CONN_DIS_BITS )
|
||||
{
|
||||
handled |= USB_INTS_HOST_CONN_DIS_BITS;
|
||||
|
||||
if ( dev_speed() )
|
||||
{
|
||||
hcd_event_device_attach(RHPORT_NATIVE, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
hcd_event_device_remove(RHPORT_NATIVE, true);
|
||||
}
|
||||
|
||||
// Clear speed change interrupt
|
||||
usb_hw_clear->sie_status = USB_SIE_STATUS_SPEED_BITS;
|
||||
}
|
||||
|
||||
if ( status & USB_INTS_STALL_BITS )
|
||||
{
|
||||
// We have rx'd a stall from the device
|
||||
// NOTE THIS SHOULD HAVE PRIORITY OVER BUFF_STATUS
|
||||
// AND TRANS_COMPLETE as the stall is an alternative response
|
||||
// to one of those events
|
||||
pico_trace("Stall REC\n");
|
||||
handled |= USB_INTS_STALL_BITS;
|
||||
usb_hw_clear->sie_status = USB_SIE_STATUS_STALL_REC_BITS;
|
||||
hw_xfer_complete(&epx, XFER_RESULT_STALLED);
|
||||
}
|
||||
|
||||
if ( status & USB_INTS_BUFF_STATUS_BITS )
|
||||
{
|
||||
handled |= USB_INTS_BUFF_STATUS_BITS;
|
||||
TU_LOG(2, "Buffer complete\r\n");
|
||||
hw_handle_buff_status();
|
||||
}
|
||||
|
||||
if ( status & USB_INTS_TRANS_COMPLETE_BITS )
|
||||
{
|
||||
handled |= USB_INTS_TRANS_COMPLETE_BITS;
|
||||
usb_hw_clear->sie_status = USB_SIE_STATUS_TRANS_COMPLETE_BITS;
|
||||
TU_LOG(2, "Transfer complete\r\n");
|
||||
hw_trans_complete();
|
||||
}
|
||||
|
||||
if ( status & USB_INTS_ERROR_RX_TIMEOUT_BITS )
|
||||
{
|
||||
handled |= USB_INTS_ERROR_RX_TIMEOUT_BITS;
|
||||
usb_hw_clear->sie_status = USB_SIE_STATUS_RX_TIMEOUT_BITS;
|
||||
}
|
||||
|
||||
if ( status & USB_INTS_ERROR_DATA_SEQ_BITS )
|
||||
{
|
||||
usb_hw_clear->sie_status = USB_SIE_STATUS_DATA_SEQ_ERROR_BITS;
|
||||
TU_LOG(3, " Seq Error: [0] = 0x%04u [1] = 0x%04x\r\n",
|
||||
tu_u32_low16(*epx.buffer_control),
|
||||
tu_u32_high16(*epx.buffer_control));
|
||||
panic("Data Seq Error \n");
|
||||
}
|
||||
|
||||
if ( status ^ handled )
|
||||
{
|
||||
panic("Unhandled IRQ 0x%x\n", (uint) (status ^ handled));
|
||||
}
|
||||
}
|
||||
|
||||
void __tusb_irq_path_func(hcd_int_handler)(uint8_t rhport, bool in_isr) {
|
||||
(void) rhport;
|
||||
(void) in_isr;
|
||||
hcd_rp2040_irq();
|
||||
}
|
||||
|
||||
static struct hw_endpoint *_next_free_interrupt_ep(void)
|
||||
{
|
||||
struct hw_endpoint * ep = NULL;
|
||||
for ( uint i = 1; i < TU_ARRAY_SIZE(ep_pool); i++ )
|
||||
{
|
||||
ep = &ep_pool[i];
|
||||
if ( !ep->configured )
|
||||
{
|
||||
// Will be configured by _hw_endpoint_init / _hw_endpoint_allocate
|
||||
ep->interrupt_num = (uint8_t) (i - 1);
|
||||
return ep;
|
||||
}
|
||||
}
|
||||
return ep;
|
||||
}
|
||||
|
||||
static struct hw_endpoint *_hw_endpoint_allocate(uint8_t transfer_type)
|
||||
{
|
||||
struct hw_endpoint * ep = NULL;
|
||||
|
||||
if ( transfer_type != TUSB_XFER_CONTROL )
|
||||
{
|
||||
// Note: even though datasheet name these "Interrupt" endpoints. These are actually
|
||||
// "Asynchronous" endpoints and can be used for other type such as: Bulk (ISO need confirmation)
|
||||
ep = _next_free_interrupt_ep();
|
||||
pico_info("Allocate %s ep %d\n", tu_edpt_type_str(transfer_type), ep->interrupt_num);
|
||||
assert(ep);
|
||||
ep->buffer_control = &usbh_dpram->int_ep_buffer_ctrl[ep->interrupt_num].ctrl;
|
||||
ep->endpoint_control = &usbh_dpram->int_ep_ctrl[ep->interrupt_num].ctrl;
|
||||
// 0 for epx (double buffered): TODO increase to 1024 for ISO
|
||||
// 2x64 for intep0
|
||||
// 3x64 for intep1
|
||||
// etc
|
||||
ep->hw_data_buf = &usbh_dpram->epx_data[64 * (ep->interrupt_num + 2)];
|
||||
}
|
||||
else
|
||||
{
|
||||
ep = &epx;
|
||||
ep->buffer_control = &usbh_dpram->epx_buf_ctrl;
|
||||
ep->endpoint_control = &usbh_dpram->epx_ctrl;
|
||||
ep->hw_data_buf = &usbh_dpram->epx_data[0];
|
||||
}
|
||||
|
||||
return ep;
|
||||
}
|
||||
|
||||
static void _hw_endpoint_init(struct hw_endpoint *ep, uint8_t dev_addr, uint8_t ep_addr, uint16_t wMaxPacketSize, uint8_t transfer_type, uint8_t bmInterval)
|
||||
{
|
||||
// Already has data buffer, endpoint control, and buffer control allocated at this point
|
||||
assert(ep->endpoint_control);
|
||||
assert(ep->buffer_control);
|
||||
assert(ep->hw_data_buf);
|
||||
|
||||
uint8_t const num = tu_edpt_number(ep_addr);
|
||||
tusb_dir_t const dir = tu_edpt_dir(ep_addr);
|
||||
|
||||
ep->ep_addr = ep_addr;
|
||||
ep->dev_addr = dev_addr;
|
||||
|
||||
// For host, IN to host == RX, anything else rx == false
|
||||
ep->rx = (dir == TUSB_DIR_IN);
|
||||
|
||||
// Response to a setup packet on EP0 starts with pid of 1
|
||||
ep->next_pid = (num == 0 ? 1u : 0u);
|
||||
ep->wMaxPacketSize = wMaxPacketSize;
|
||||
ep->transfer_type = transfer_type;
|
||||
|
||||
pico_trace("hw_endpoint_init dev %d ep %d %s xfer %d\n", ep->dev_addr, tu_edpt_number(ep->ep_addr),
|
||||
ep_dir_string[tu_edpt_dir(ep->ep_addr)], ep->transfer_type);
|
||||
pico_trace("dev %d ep %d %s setup buffer @ 0x%p\n", ep->dev_addr, tu_edpt_number(ep->ep_addr),
|
||||
ep_dir_string[tu_edpt_dir(ep->ep_addr)], ep->hw_data_buf);
|
||||
uint dpram_offset = hw_data_offset(ep->hw_data_buf);
|
||||
// Bits 0-5 should be 0
|
||||
assert(!(dpram_offset & 0b111111));
|
||||
|
||||
// Fill in endpoint control register with buffer offset
|
||||
uint32_t ep_reg = EP_CTRL_ENABLE_BITS
|
||||
| EP_CTRL_INTERRUPT_PER_BUFFER
|
||||
| (ep->transfer_type << EP_CTRL_BUFFER_TYPE_LSB)
|
||||
| dpram_offset;
|
||||
if ( bmInterval )
|
||||
{
|
||||
ep_reg |= (uint32_t) ((bmInterval - 1) << EP_CTRL_HOST_INTERRUPT_INTERVAL_LSB);
|
||||
}
|
||||
*ep->endpoint_control = ep_reg;
|
||||
pico_trace("endpoint control (0x%p) <- 0x%x\n", ep->endpoint_control, ep_reg);
|
||||
ep->configured = true;
|
||||
|
||||
if ( ep != &epx )
|
||||
{
|
||||
// Endpoint has its own addr_endp and interrupt bits to be setup!
|
||||
// This is an interrupt/async endpoint. so need to set up ADDR_ENDP register with:
|
||||
// - device address
|
||||
// - endpoint number / direction
|
||||
// - preamble
|
||||
uint32_t reg = (uint32_t) (dev_addr | (num << USB_ADDR_ENDP1_ENDPOINT_LSB));
|
||||
|
||||
if ( dir == TUSB_DIR_OUT )
|
||||
{
|
||||
reg |= USB_ADDR_ENDP1_INTEP_DIR_BITS;
|
||||
}
|
||||
|
||||
if ( need_pre(dev_addr) )
|
||||
{
|
||||
reg |= USB_ADDR_ENDP1_INTEP_PREAMBLE_BITS;
|
||||
}
|
||||
usb_hw->int_ep_addr_ctrl[ep->interrupt_num] = reg;
|
||||
|
||||
// Finally, enable interrupt that endpoint
|
||||
usb_hw_set->int_ep_ctrl = 1 << (ep->interrupt_num + 1);
|
||||
|
||||
// If it's an interrupt endpoint we need to set up the buffer control
|
||||
// register
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// HCD API
|
||||
//--------------------------------------------------------------------+
|
||||
bool hcd_init(uint8_t rhport)
|
||||
{
|
||||
(void) rhport;
|
||||
pico_trace("hcd_init %d\n", rhport);
|
||||
assert(rhport == 0);
|
||||
|
||||
// Reset any previous state
|
||||
rp2040_usb_init();
|
||||
|
||||
// Force VBUS detect to always present, for now we assume vbus is always provided (without using VBUS En)
|
||||
usb_hw->pwr = USB_USB_PWR_VBUS_DETECT_BITS | USB_USB_PWR_VBUS_DETECT_OVERRIDE_EN_BITS;
|
||||
|
||||
// Remove shared irq if it was previously added so as not to fill up shared irq slots
|
||||
irq_remove_handler(USBCTRL_IRQ, hcd_rp2040_irq);
|
||||
|
||||
irq_add_shared_handler(USBCTRL_IRQ, hcd_rp2040_irq, PICO_SHARED_IRQ_HANDLER_HIGHEST_ORDER_PRIORITY);
|
||||
|
||||
// clear epx and interrupt eps
|
||||
memset(&ep_pool, 0, sizeof(ep_pool));
|
||||
|
||||
// Enable in host mode with SOF / Keep alive on
|
||||
usb_hw->main_ctrl = USB_MAIN_CTRL_CONTROLLER_EN_BITS | USB_MAIN_CTRL_HOST_NDEVICE_BITS;
|
||||
usb_hw->sie_ctrl = SIE_CTRL_BASE;
|
||||
usb_hw->inte = USB_INTE_BUFF_STATUS_BITS |
|
||||
USB_INTE_HOST_CONN_DIS_BITS |
|
||||
USB_INTE_HOST_RESUME_BITS |
|
||||
USB_INTE_STALL_BITS |
|
||||
USB_INTE_TRANS_COMPLETE_BITS |
|
||||
USB_INTE_ERROR_RX_TIMEOUT_BITS |
|
||||
USB_INTE_ERROR_DATA_SEQ_BITS ;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void hcd_port_reset(uint8_t rhport)
|
||||
{
|
||||
(void) rhport;
|
||||
pico_trace("hcd_port_reset\n");
|
||||
assert(rhport == 0);
|
||||
// TODO: Nothing to do here yet. Perhaps need to reset some state?
|
||||
}
|
||||
|
||||
void hcd_port_reset_end(uint8_t rhport)
|
||||
{
|
||||
(void) rhport;
|
||||
}
|
||||
|
||||
bool hcd_port_connect_status(uint8_t rhport)
|
||||
{
|
||||
(void) rhport;
|
||||
pico_trace("hcd_port_connect_status\n");
|
||||
assert(rhport == 0);
|
||||
return usb_hw->sie_status & USB_SIE_STATUS_SPEED_BITS;
|
||||
}
|
||||
|
||||
tusb_speed_t hcd_port_speed_get(uint8_t rhport)
|
||||
{
|
||||
(void) rhport;
|
||||
assert(rhport == 0);
|
||||
|
||||
// TODO: Should enumval this register
|
||||
switch ( dev_speed() )
|
||||
{
|
||||
case 1:
|
||||
return TUSB_SPEED_LOW;
|
||||
case 2:
|
||||
return TUSB_SPEED_FULL;
|
||||
default:
|
||||
panic("Invalid speed\n");
|
||||
// return TUSB_SPEED_INVALID;
|
||||
}
|
||||
}
|
||||
|
||||
// Close all opened endpoint belong to this device
|
||||
void hcd_device_close(uint8_t rhport, uint8_t dev_addr)
|
||||
{
|
||||
pico_trace("hcd_device_close %d\n", dev_addr);
|
||||
(void) rhport;
|
||||
|
||||
if (dev_addr == 0) return;
|
||||
|
||||
for (size_t i = 1; i < TU_ARRAY_SIZE(ep_pool); i++)
|
||||
{
|
||||
hw_endpoint_t* ep = &ep_pool[i];
|
||||
|
||||
if (ep->dev_addr == dev_addr && ep->configured)
|
||||
{
|
||||
// in case it is an interrupt endpoint, disable it
|
||||
usb_hw_clear->int_ep_ctrl = (1 << (ep->interrupt_num + 1));
|
||||
usb_hw->int_ep_addr_ctrl[ep->interrupt_num] = 0;
|
||||
|
||||
// unconfigure the endpoint
|
||||
ep->configured = false;
|
||||
*ep->endpoint_control = 0;
|
||||
*ep->buffer_control = 0;
|
||||
hw_endpoint_reset_transfer(ep);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t hcd_frame_number(uint8_t rhport)
|
||||
{
|
||||
(void) rhport;
|
||||
return usb_hw->sof_rd;
|
||||
}
|
||||
|
||||
void hcd_int_enable(uint8_t rhport)
|
||||
{
|
||||
(void) rhport;
|
||||
assert(rhport == 0);
|
||||
irq_set_enabled(USBCTRL_IRQ, true);
|
||||
}
|
||||
|
||||
void hcd_int_disable(uint8_t rhport)
|
||||
{
|
||||
(void) rhport;
|
||||
// todo we should check this is disabling from the correct core; note currently this is never called
|
||||
assert(rhport == 0);
|
||||
irq_set_enabled(USBCTRL_IRQ, false);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Endpoint API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
bool hcd_edpt_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_endpoint_t const * ep_desc)
|
||||
{
|
||||
(void) rhport;
|
||||
|
||||
pico_trace("hcd_edpt_open dev_addr %d, ep_addr %d\n", dev_addr, ep_desc->bEndpointAddress);
|
||||
|
||||
// Allocated differently based on if it's an interrupt endpoint or not
|
||||
struct hw_endpoint *ep = _hw_endpoint_allocate(ep_desc->bmAttributes.xfer);
|
||||
TU_ASSERT(ep);
|
||||
|
||||
_hw_endpoint_init(ep,
|
||||
dev_addr,
|
||||
ep_desc->bEndpointAddress,
|
||||
tu_edpt_packet_size(ep_desc),
|
||||
ep_desc->bmAttributes.xfer,
|
||||
ep_desc->bInterval);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool hcd_edpt_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr, uint8_t * buffer, uint16_t buflen)
|
||||
{
|
||||
(void) rhport;
|
||||
|
||||
pico_trace("hcd_edpt_xfer dev_addr %d, ep_addr 0x%x, len %d\n", dev_addr, ep_addr, buflen);
|
||||
|
||||
uint8_t const ep_num = tu_edpt_number(ep_addr);
|
||||
tusb_dir_t const ep_dir = tu_edpt_dir(ep_addr);
|
||||
|
||||
// Get appropriate ep. Either EPX or interrupt endpoint
|
||||
struct hw_endpoint *ep = get_dev_ep(dev_addr, ep_addr);
|
||||
|
||||
TU_ASSERT(ep);
|
||||
|
||||
// EP should be inactive
|
||||
assert(!ep->active);
|
||||
|
||||
// Control endpoint can change direction 0x00 <-> 0x80
|
||||
if ( ep_addr != ep->ep_addr )
|
||||
{
|
||||
assert(ep_num == 0);
|
||||
|
||||
// Direction has flipped on endpoint control so re init it but with same properties
|
||||
_hw_endpoint_init(ep, dev_addr, ep_addr, ep->wMaxPacketSize, ep->transfer_type, 0);
|
||||
}
|
||||
|
||||
// If a normal transfer (non-interrupt) then initiate using
|
||||
// sie ctrl registers. Otherwise interrupt ep registers should
|
||||
// already be configured
|
||||
if ( ep == &epx )
|
||||
{
|
||||
hw_endpoint_xfer_start(ep, buffer, buflen);
|
||||
|
||||
// That has set up buffer control, endpoint control etc
|
||||
// for host we have to initiate the transfer
|
||||
usb_hw->dev_addr_ctrl = (uint32_t) (dev_addr | (ep_num << USB_ADDR_ENDP_ENDPOINT_LSB));
|
||||
|
||||
uint32_t flags = USB_SIE_CTRL_START_TRANS_BITS | SIE_CTRL_BASE |
|
||||
(ep_dir ? USB_SIE_CTRL_RECEIVE_DATA_BITS : USB_SIE_CTRL_SEND_DATA_BITS) |
|
||||
(need_pre(dev_addr) ? USB_SIE_CTRL_PREAMBLE_EN_BITS : 0);
|
||||
// START_TRANS bit on SIE_CTRL seems to exhibit the same behavior as the AVAILABLE bit
|
||||
// described in RP2040 Datasheet, release 2.1, section "4.1.2.5.1. Concurrent access".
|
||||
// We write everything except the START_TRANS bit first, then wait some cycles.
|
||||
usb_hw->sie_ctrl = flags & ~USB_SIE_CTRL_START_TRANS_BITS;
|
||||
busy_wait_at_least_cycles(12);
|
||||
usb_hw->sie_ctrl = flags;
|
||||
}else
|
||||
{
|
||||
hw_endpoint_xfer_start(ep, buffer, buflen);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool hcd_edpt_abort_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr) {
|
||||
(void) rhport;
|
||||
(void) dev_addr;
|
||||
(void) ep_addr;
|
||||
// TODO not implemented yet
|
||||
return false;
|
||||
}
|
||||
|
||||
bool hcd_setup_send(uint8_t rhport, uint8_t dev_addr, uint8_t const setup_packet[8])
|
||||
{
|
||||
(void) rhport;
|
||||
|
||||
// Copy data into setup packet buffer
|
||||
for ( uint8_t i = 0; i < 8; i++ )
|
||||
{
|
||||
usbh_dpram->setup_packet[i] = setup_packet[i];
|
||||
}
|
||||
|
||||
// Configure EP0 struct with setup info for the trans complete
|
||||
struct hw_endpoint * ep = _hw_endpoint_allocate(0);
|
||||
TU_ASSERT(ep);
|
||||
|
||||
// EPX should be inactive
|
||||
assert(!ep->active);
|
||||
|
||||
// EP0 out
|
||||
_hw_endpoint_init(ep, dev_addr, 0x00, ep->wMaxPacketSize, 0, 0);
|
||||
assert(ep->configured);
|
||||
|
||||
ep->remaining_len = 8;
|
||||
ep->active = true;
|
||||
|
||||
// Set device address
|
||||
usb_hw->dev_addr_ctrl = dev_addr;
|
||||
|
||||
// Set pre if we are a low speed device on full speed hub
|
||||
uint32_t const flags = SIE_CTRL_BASE | USB_SIE_CTRL_SEND_SETUP_BITS | USB_SIE_CTRL_START_TRANS_BITS |
|
||||
(need_pre(dev_addr) ? USB_SIE_CTRL_PREAMBLE_EN_BITS : 0);
|
||||
|
||||
// START_TRANS bit on SIE_CTRL seems to exhibit the same behavior as the AVAILABLE bit
|
||||
// described in RP2040 Datasheet, release 2.1, section "4.1.2.5.1. Concurrent access".
|
||||
// We write everything except the START_TRANS bit first, then wait some cycles.
|
||||
usb_hw->sie_ctrl = flags & ~USB_SIE_CTRL_START_TRANS_BITS;
|
||||
busy_wait_at_least_cycles(12);
|
||||
usb_hw->sie_ctrl = flags;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool hcd_edpt_clear_stall(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr) {
|
||||
(void) rhport;
|
||||
(void) dev_addr;
|
||||
(void) ep_addr;
|
||||
|
||||
panic("hcd_clear_stall");
|
||||
// return true;
|
||||
}
|
||||
|
||||
#endif
|
|
@ -0,0 +1,429 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
|
||||
* Copyright (c) 2021 Ha Thach (tinyusb.org) for Double Buffered
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#include "tusb_option.h"
|
||||
|
||||
#if CFG_TUSB_MCU == OPT_MCU_RP2040
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "rp2040_usb.h"
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// MACRO CONSTANT TYPEDEF PROTOTYPE
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Direction strings for debug
|
||||
const char *ep_dir_string[] = {
|
||||
"out",
|
||||
"in",
|
||||
};
|
||||
|
||||
static void _hw_endpoint_xfer_sync(struct hw_endpoint *ep);
|
||||
|
||||
#if TUD_OPT_RP2040_USB_DEVICE_UFRAME_FIX
|
||||
static bool e15_is_bulkin_ep(struct hw_endpoint *ep);
|
||||
static bool e15_is_critical_frame_period(struct hw_endpoint *ep);
|
||||
#else
|
||||
#define e15_is_bulkin_ep(x) (false)
|
||||
#define e15_is_critical_frame_period(x) (false)
|
||||
#endif
|
||||
|
||||
// if usb hardware is in host mode
|
||||
TU_ATTR_ALWAYS_INLINE static inline bool is_host_mode(void)
|
||||
{
|
||||
return (usb_hw->main_ctrl & USB_MAIN_CTRL_HOST_NDEVICE_BITS) ? true : false;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Implementation
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
void rp2040_usb_init(void)
|
||||
{
|
||||
// Reset usb controller
|
||||
reset_block(RESETS_RESET_USBCTRL_BITS);
|
||||
unreset_block_wait(RESETS_RESET_USBCTRL_BITS);
|
||||
|
||||
#ifdef __GNUC__
|
||||
// Clear any previous state just in case
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Warray-bounds"
|
||||
#if __GNUC__ > 6
|
||||
#pragma GCC diagnostic ignored "-Wstringop-overflow"
|
||||
#endif
|
||||
#endif
|
||||
memset(usb_hw, 0, sizeof(*usb_hw));
|
||||
memset(usb_dpram, 0, sizeof(*usb_dpram));
|
||||
#ifdef __GNUC__
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
|
||||
// Mux the controller to the onboard usb phy
|
||||
usb_hw->muxing = USB_USB_MUXING_TO_PHY_BITS | USB_USB_MUXING_SOFTCON_BITS;
|
||||
|
||||
TU_LOG2_INT(sizeof(hw_endpoint_t));
|
||||
}
|
||||
|
||||
void __tusb_irq_path_func(hw_endpoint_reset_transfer)(struct hw_endpoint *ep)
|
||||
{
|
||||
ep->active = false;
|
||||
ep->remaining_len = 0;
|
||||
ep->xferred_len = 0;
|
||||
ep->user_buf = 0;
|
||||
}
|
||||
|
||||
void __tusb_irq_path_func(_hw_endpoint_buffer_control_update32)(struct hw_endpoint *ep, uint32_t and_mask, uint32_t or_mask)
|
||||
{
|
||||
uint32_t value = 0;
|
||||
|
||||
if ( and_mask )
|
||||
{
|
||||
value = *ep->buffer_control & and_mask;
|
||||
}
|
||||
|
||||
if ( or_mask )
|
||||
{
|
||||
value |= or_mask;
|
||||
if ( or_mask & USB_BUF_CTRL_AVAIL )
|
||||
{
|
||||
if ( *ep->buffer_control & USB_BUF_CTRL_AVAIL )
|
||||
{
|
||||
panic("ep %d %s was already available", tu_edpt_number(ep->ep_addr), ep_dir_string[tu_edpt_dir(ep->ep_addr)]);
|
||||
}
|
||||
*ep->buffer_control = value & ~USB_BUF_CTRL_AVAIL;
|
||||
// 12 cycle delay.. (should be good for 48*12Mhz = 576Mhz)
|
||||
// Don't need delay in host mode as host is in charge
|
||||
#if !CFG_TUH_ENABLED
|
||||
__asm volatile (
|
||||
"b 1f\n"
|
||||
"1: b 1f\n"
|
||||
"1: b 1f\n"
|
||||
"1: b 1f\n"
|
||||
"1: b 1f\n"
|
||||
"1: b 1f\n"
|
||||
"1:\n"
|
||||
: : : "memory");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
*ep->buffer_control = value;
|
||||
}
|
||||
|
||||
// prepare buffer, return buffer control
|
||||
static uint32_t __tusb_irq_path_func(prepare_ep_buffer)(struct hw_endpoint *ep, uint8_t buf_id)
|
||||
{
|
||||
uint16_t const buflen = tu_min16(ep->remaining_len, ep->wMaxPacketSize);
|
||||
ep->remaining_len = (uint16_t)(ep->remaining_len - buflen);
|
||||
|
||||
uint32_t buf_ctrl = buflen | USB_BUF_CTRL_AVAIL;
|
||||
|
||||
// PID
|
||||
buf_ctrl |= ep->next_pid ? USB_BUF_CTRL_DATA1_PID : USB_BUF_CTRL_DATA0_PID;
|
||||
ep->next_pid ^= 1u;
|
||||
|
||||
if ( !ep->rx )
|
||||
{
|
||||
// Copy data from user buffer to hw buffer
|
||||
memcpy(ep->hw_data_buf + buf_id*64, ep->user_buf, buflen);
|
||||
ep->user_buf += buflen;
|
||||
|
||||
// Mark as full
|
||||
buf_ctrl |= USB_BUF_CTRL_FULL;
|
||||
}
|
||||
|
||||
// Is this the last buffer? Only really matters for host mode. Will trigger
|
||||
// the trans complete irq but also stop it polling. We only really care about
|
||||
// trans complete for setup packets being sent
|
||||
if (ep->remaining_len == 0)
|
||||
{
|
||||
buf_ctrl |= USB_BUF_CTRL_LAST;
|
||||
}
|
||||
|
||||
if (buf_id) buf_ctrl = buf_ctrl << 16;
|
||||
|
||||
return buf_ctrl;
|
||||
}
|
||||
|
||||
// Prepare buffer control register value
|
||||
void __tusb_irq_path_func(hw_endpoint_start_next_buffer)(struct hw_endpoint *ep)
|
||||
{
|
||||
uint32_t ep_ctrl = *ep->endpoint_control;
|
||||
|
||||
// always compute and start with buffer 0
|
||||
uint32_t buf_ctrl = prepare_ep_buffer(ep, 0) | USB_BUF_CTRL_SEL;
|
||||
|
||||
// For now: skip double buffered for OUT endpoint in Device mode, since
|
||||
// host could send < 64 bytes and cause short packet on buffer0
|
||||
// NOTE: this could happen to Host mode IN endpoint
|
||||
// Also, Host mode "interrupt" endpoint hardware is only single buffered,
|
||||
// NOTE2: Currently Host bulk is implemented using "interrupt" endpoint
|
||||
bool const is_host = is_host_mode();
|
||||
bool const force_single = (!is_host && !tu_edpt_dir(ep->ep_addr)) ||
|
||||
(is_host && tu_edpt_number(ep->ep_addr) != 0);
|
||||
|
||||
if(ep->remaining_len && !force_single)
|
||||
{
|
||||
// Use buffer 1 (double buffered) if there is still data
|
||||
// TODO: Isochronous for buffer1 bit-field is different than CBI (control bulk, interrupt)
|
||||
|
||||
buf_ctrl |= prepare_ep_buffer(ep, 1);
|
||||
|
||||
// Set endpoint control double buffered bit if needed
|
||||
ep_ctrl &= ~EP_CTRL_INTERRUPT_PER_BUFFER;
|
||||
ep_ctrl |= EP_CTRL_DOUBLE_BUFFERED_BITS | EP_CTRL_INTERRUPT_PER_DOUBLE_BUFFER;
|
||||
}else
|
||||
{
|
||||
// Single buffered since 1 is enough
|
||||
ep_ctrl &= ~(EP_CTRL_DOUBLE_BUFFERED_BITS | EP_CTRL_INTERRUPT_PER_DOUBLE_BUFFER);
|
||||
ep_ctrl |= EP_CTRL_INTERRUPT_PER_BUFFER;
|
||||
}
|
||||
|
||||
*ep->endpoint_control = ep_ctrl;
|
||||
|
||||
TU_LOG(3, " Prepare BufCtrl: [0] = 0x%04x [1] = 0x%04x\r\n", tu_u32_low16(buf_ctrl), tu_u32_high16(buf_ctrl));
|
||||
|
||||
// Finally, write to buffer_control which will trigger the transfer
|
||||
// the next time the controller polls this dpram address
|
||||
_hw_endpoint_buffer_control_set_value32(ep, buf_ctrl);
|
||||
}
|
||||
|
||||
void hw_endpoint_xfer_start(struct hw_endpoint *ep, uint8_t *buffer, uint16_t total_len)
|
||||
{
|
||||
hw_endpoint_lock_update(ep, 1);
|
||||
|
||||
if ( ep->active )
|
||||
{
|
||||
// TODO: Is this acceptable for interrupt packets?
|
||||
TU_LOG(1, "WARN: starting new transfer on already active ep %d %s\r\n", tu_edpt_number(ep->ep_addr),
|
||||
ep_dir_string[tu_edpt_dir(ep->ep_addr)]);
|
||||
|
||||
hw_endpoint_reset_transfer(ep);
|
||||
}
|
||||
|
||||
// Fill in info now that we're kicking off the hw
|
||||
ep->remaining_len = total_len;
|
||||
ep->xferred_len = 0;
|
||||
ep->active = true;
|
||||
ep->user_buf = buffer;
|
||||
|
||||
if ( e15_is_bulkin_ep(ep) )
|
||||
{
|
||||
usb_hw_set->inte = USB_INTS_DEV_SOF_BITS;
|
||||
}
|
||||
|
||||
if ( e15_is_critical_frame_period(ep) )
|
||||
{
|
||||
ep->pending = 1;
|
||||
} else
|
||||
{
|
||||
hw_endpoint_start_next_buffer(ep);
|
||||
}
|
||||
|
||||
hw_endpoint_lock_update(ep, -1);
|
||||
}
|
||||
|
||||
// sync endpoint buffer and return transferred bytes
|
||||
static uint16_t __tusb_irq_path_func(sync_ep_buffer)(struct hw_endpoint *ep, uint8_t buf_id)
|
||||
{
|
||||
uint32_t buf_ctrl = _hw_endpoint_buffer_control_get_value32(ep);
|
||||
if (buf_id) buf_ctrl = buf_ctrl >> 16;
|
||||
|
||||
uint16_t xferred_bytes = buf_ctrl & USB_BUF_CTRL_LEN_MASK;
|
||||
|
||||
if ( !ep->rx )
|
||||
{
|
||||
// We are continuing a transfer here. If we are TX, we have successfully
|
||||
// sent some data can increase the length we have sent
|
||||
assert(!(buf_ctrl & USB_BUF_CTRL_FULL));
|
||||
|
||||
ep->xferred_len = (uint16_t)(ep->xferred_len + xferred_bytes);
|
||||
}else
|
||||
{
|
||||
// If we have received some data, so can increase the length
|
||||
// we have received AFTER we have copied it to the user buffer at the appropriate offset
|
||||
assert(buf_ctrl & USB_BUF_CTRL_FULL);
|
||||
|
||||
memcpy(ep->user_buf, ep->hw_data_buf + buf_id*64, xferred_bytes);
|
||||
ep->xferred_len = (uint16_t)(ep->xferred_len + xferred_bytes);
|
||||
ep->user_buf += xferred_bytes;
|
||||
}
|
||||
|
||||
// Short packet
|
||||
if (xferred_bytes < ep->wMaxPacketSize)
|
||||
{
|
||||
pico_trace(" Short packet on buffer %d with %u bytes\r\n", buf_id, xferred_bytes);
|
||||
// Reduce total length as this is last packet
|
||||
ep->remaining_len = 0;
|
||||
}
|
||||
|
||||
return xferred_bytes;
|
||||
}
|
||||
|
||||
static void __tusb_irq_path_func(_hw_endpoint_xfer_sync) (struct hw_endpoint *ep)
|
||||
{
|
||||
// Update hw endpoint struct with info from hardware
|
||||
// after a buff status interrupt
|
||||
|
||||
uint32_t __unused buf_ctrl = _hw_endpoint_buffer_control_get_value32(ep);
|
||||
TU_LOG(3, " Sync BufCtrl: [0] = 0x%04x [1] = 0x%04x\r\n", tu_u32_low16(buf_ctrl), tu_u32_high16(buf_ctrl));
|
||||
|
||||
// always sync buffer 0
|
||||
uint16_t buf0_bytes = sync_ep_buffer(ep, 0);
|
||||
|
||||
// sync buffer 1 if double buffered
|
||||
if ( (*ep->endpoint_control) & EP_CTRL_DOUBLE_BUFFERED_BITS )
|
||||
{
|
||||
if (buf0_bytes == ep->wMaxPacketSize)
|
||||
{
|
||||
// sync buffer 1 if not short packet
|
||||
sync_ep_buffer(ep, 1);
|
||||
}else
|
||||
{
|
||||
// short packet on buffer 0
|
||||
// TODO couldn't figure out how to handle this case which happen with net_lwip_webserver example
|
||||
// At this time (currently trigger per 2 buffer), the buffer1 is probably filled with data from
|
||||
// the next transfer (not current one). For now we disable double buffered for device OUT
|
||||
// NOTE this could happen to Host IN
|
||||
#if 0
|
||||
uint8_t const ep_num = tu_edpt_number(ep->ep_addr);
|
||||
uint8_t const dir = (uint8_t) tu_edpt_dir(ep->ep_addr);
|
||||
uint8_t const ep_id = 2*ep_num + (dir ? 0 : 1);
|
||||
|
||||
// abort queued transfer on buffer 1
|
||||
usb_hw->abort |= TU_BIT(ep_id);
|
||||
|
||||
while ( !(usb_hw->abort_done & TU_BIT(ep_id)) ) {}
|
||||
|
||||
uint32_t ep_ctrl = *ep->endpoint_control;
|
||||
ep_ctrl &= ~(EP_CTRL_DOUBLE_BUFFERED_BITS | EP_CTRL_INTERRUPT_PER_DOUBLE_BUFFER);
|
||||
ep_ctrl |= EP_CTRL_INTERRUPT_PER_BUFFER;
|
||||
|
||||
_hw_endpoint_buffer_control_set_value32(ep, 0);
|
||||
|
||||
usb_hw->abort &= ~TU_BIT(ep_id);
|
||||
|
||||
TU_LOG(3, "----SHORT PACKET buffer0 on EP %02X:\r\n", ep->ep_addr);
|
||||
TU_LOG(3, " BufCtrl: [0] = 0x%04x [1] = 0x%04x\r\n", tu_u32_low16(buf_ctrl), tu_u32_high16(buf_ctrl));
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Returns true if transfer is complete
|
||||
bool __tusb_irq_path_func(hw_endpoint_xfer_continue)(struct hw_endpoint *ep)
|
||||
{
|
||||
hw_endpoint_lock_update(ep, 1);
|
||||
|
||||
// Part way through a transfer
|
||||
if (!ep->active)
|
||||
{
|
||||
panic("Can't continue xfer on inactive ep %d %s", tu_edpt_number(ep->ep_addr), ep_dir_string[tu_edpt_dir(ep->ep_addr)]);
|
||||
}
|
||||
|
||||
// Update EP struct from hardware state
|
||||
_hw_endpoint_xfer_sync(ep);
|
||||
|
||||
// Now we have synced our state with the hardware. Is there more data to transfer?
|
||||
// If we are done then notify tinyusb
|
||||
if (ep->remaining_len == 0)
|
||||
{
|
||||
pico_trace("Completed transfer of %d bytes on ep %d %s\r\n",
|
||||
ep->xferred_len, tu_edpt_number(ep->ep_addr), ep_dir_string[tu_edpt_dir(ep->ep_addr)]);
|
||||
// Notify caller we are done so it can notify the tinyusb stack
|
||||
hw_endpoint_lock_update(ep, -1);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( e15_is_critical_frame_period(ep) )
|
||||
{
|
||||
ep->pending = 1;
|
||||
} else
|
||||
{
|
||||
hw_endpoint_start_next_buffer(ep);
|
||||
}
|
||||
}
|
||||
|
||||
hw_endpoint_lock_update(ep, -1);
|
||||
// More work to do
|
||||
return false;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Errata 15
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
#if TUD_OPT_RP2040_USB_DEVICE_UFRAME_FIX
|
||||
|
||||
/* Don't mark IN buffers as available during the last 200us of a full-speed
|
||||
frame. This avoids a situation seen with the USB2.0 hub on a Raspberry
|
||||
Pi 4 where a late IN token before the next full-speed SOF can cause port
|
||||
babble and a corrupt ACK packet. The nature of the data corruption has a
|
||||
chance to cause device lockup.
|
||||
|
||||
Use the next SOF to mark delayed buffers as available. This reduces
|
||||
available Bulk IN bandwidth by approximately 20%, and requires that the
|
||||
SOF interrupt is enabled while these transfers are ongoing.
|
||||
|
||||
Inherit the top-level enable from the corresponding Pico-SDK flag.
|
||||
Applications that will not use the device in a situation where it could
|
||||
be plugged into a Pi 4 or Pi 400 (for example, when directly connected
|
||||
to a commodity hub or other host) can turn off the flag in the SDK.
|
||||
*/
|
||||
|
||||
volatile uint32_t e15_last_sof = 0;
|
||||
|
||||
// check if Errata 15 is needed for this endpoint i.e device bulk-in
|
||||
static bool __tusb_irq_path_func(e15_is_bulkin_ep) (struct hw_endpoint *ep)
|
||||
{
|
||||
return (!is_host_mode() && tu_edpt_dir(ep->ep_addr) == TUSB_DIR_IN &&
|
||||
ep->transfer_type == TUSB_XFER_BULK);
|
||||
}
|
||||
|
||||
// check if we need to apply Errata 15 workaround : i.e
|
||||
// Endpoint is BULK IN and is currently in critical frame period i.e 20% of last usb frame
|
||||
static bool __tusb_irq_path_func(e15_is_critical_frame_period) (struct hw_endpoint *ep)
|
||||
{
|
||||
TU_VERIFY(e15_is_bulkin_ep(ep));
|
||||
|
||||
/* Avoid the last 200us (uframe 6.5-7) of a frame, up to the EOF2 point.
|
||||
* The device state machine cannot recover from receiving an incorrect PID
|
||||
* when it is expecting an ACK.
|
||||
*/
|
||||
uint32_t delta = time_us_32() - e15_last_sof;
|
||||
if (delta < 800 || delta > 998) {
|
||||
return false;
|
||||
}
|
||||
TU_LOG(3, "Avoiding sof %lu now %lu last %lu\r\n", (usb_hw->sof_rd + 1) & USB_SOF_RD_BITS, time_us_32(), e15_last_sof);
|
||||
return true;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
|
@ -0,0 +1,143 @@
|
|||
#ifndef RP2040_COMMON_H_
|
||||
#define RP2040_COMMON_H_
|
||||
|
||||
#if defined(RP2040_USB_HOST_MODE) && defined(RP2040_USB_DEVICE_MODE)
|
||||
#error TinyUSB device and host mode not supported at the same time
|
||||
#endif
|
||||
|
||||
#include "common/tusb_common.h"
|
||||
|
||||
#include "pico.h"
|
||||
#include "hardware/structs/usb.h"
|
||||
#include "hardware/irq.h"
|
||||
#include "hardware/resets.h"
|
||||
#include "hardware/timer.h"
|
||||
|
||||
#if defined(PICO_RP2040_USB_DEVICE_ENUMERATION_FIX) && !defined(TUD_OPT_RP2040_USB_DEVICE_ENUMERATION_FIX)
|
||||
#define TUD_OPT_RP2040_USB_DEVICE_ENUMERATION_FIX PICO_RP2040_USB_DEVICE_ENUMERATION_FIX
|
||||
#endif
|
||||
|
||||
#if defined(PICO_RP2040_USB_DEVICE_UFRAME_FIX) && !defined(TUD_OPT_RP2040_USB_DEVICE_UFRAME_FIX)
|
||||
#define TUD_OPT_RP2040_USB_DEVICE_UFRAME_FIX PICO_RP2040_USB_DEVICE_UFRAME_FIX
|
||||
#endif
|
||||
|
||||
#if TUD_OPT_RP2040_USB_DEVICE_UFRAME_FIX
|
||||
#undef PICO_RP2040_USB_FAST_IRQ
|
||||
#define PICO_RP2040_USB_FAST_IRQ 1
|
||||
#endif
|
||||
|
||||
#ifndef PICO_RP2040_USB_FAST_IRQ
|
||||
#define PICO_RP2040_USB_FAST_IRQ 0
|
||||
#endif
|
||||
|
||||
#if PICO_RP2040_USB_FAST_IRQ
|
||||
#define __tusb_irq_path_func(x) __no_inline_not_in_flash_func(x)
|
||||
#else
|
||||
#define __tusb_irq_path_func(x) x
|
||||
#endif
|
||||
|
||||
#define usb_hw_set ((usb_hw_t *) hw_set_alias_untyped(usb_hw))
|
||||
#define usb_hw_clear ((usb_hw_t *) hw_clear_alias_untyped(usb_hw))
|
||||
|
||||
#define pico_info(...) TU_LOG(2, __VA_ARGS__)
|
||||
#define pico_trace(...) TU_LOG(3, __VA_ARGS__)
|
||||
|
||||
// Hardware information per endpoint
|
||||
typedef struct hw_endpoint
|
||||
{
|
||||
// Is this a valid struct
|
||||
bool configured;
|
||||
|
||||
// Transfer direction (i.e. IN is rx for host but tx for device)
|
||||
// allows us to common up transfer functions
|
||||
bool rx;
|
||||
|
||||
uint8_t ep_addr;
|
||||
uint8_t next_pid;
|
||||
|
||||
// Endpoint control register
|
||||
io_rw_32 *endpoint_control;
|
||||
|
||||
// Buffer control register
|
||||
io_rw_32 *buffer_control;
|
||||
|
||||
// Buffer pointer in usb dpram
|
||||
uint8_t *hw_data_buf;
|
||||
|
||||
// User buffer in main memory
|
||||
uint8_t *user_buf;
|
||||
|
||||
// Current transfer information
|
||||
uint16_t remaining_len;
|
||||
uint16_t xferred_len;
|
||||
|
||||
// Data needed from EP descriptor
|
||||
uint16_t wMaxPacketSize;
|
||||
|
||||
// Endpoint is in use
|
||||
bool active;
|
||||
|
||||
// Interrupt, bulk, etc
|
||||
uint8_t transfer_type;
|
||||
|
||||
// Transfer scheduled but not active
|
||||
uint8_t pending;
|
||||
|
||||
#if CFG_TUH_ENABLED
|
||||
// Only needed for host
|
||||
uint8_t dev_addr;
|
||||
|
||||
// If interrupt endpoint
|
||||
uint8_t interrupt_num;
|
||||
#endif
|
||||
|
||||
} hw_endpoint_t;
|
||||
|
||||
#if TUD_OPT_RP2040_USB_DEVICE_UFRAME_FIX
|
||||
extern volatile uint32_t e15_last_sof;
|
||||
#endif
|
||||
|
||||
void rp2040_usb_init(void);
|
||||
|
||||
void hw_endpoint_xfer_start(struct hw_endpoint *ep, uint8_t *buffer, uint16_t total_len);
|
||||
bool hw_endpoint_xfer_continue(struct hw_endpoint *ep);
|
||||
void hw_endpoint_reset_transfer(struct hw_endpoint *ep);
|
||||
void hw_endpoint_start_next_buffer(struct hw_endpoint *ep);
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline void hw_endpoint_lock_update(__unused struct hw_endpoint * ep, __unused int delta) {
|
||||
// todo add critsec as necessary to prevent issues between worker and IRQ...
|
||||
// note that this is perhaps as simple as disabling IRQs because it would make
|
||||
// sense to have worker and IRQ on same core, however I think using critsec is about equivalent.
|
||||
}
|
||||
|
||||
void _hw_endpoint_buffer_control_update32(struct hw_endpoint *ep, uint32_t and_mask, uint32_t or_mask);
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline uint32_t _hw_endpoint_buffer_control_get_value32 (struct hw_endpoint *ep)
|
||||
{
|
||||
return *ep->buffer_control;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline void _hw_endpoint_buffer_control_set_value32 (struct hw_endpoint *ep, uint32_t value)
|
||||
{
|
||||
_hw_endpoint_buffer_control_update32(ep, 0, value);
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline void _hw_endpoint_buffer_control_set_mask32 (struct hw_endpoint *ep, uint32_t value)
|
||||
{
|
||||
_hw_endpoint_buffer_control_update32(ep, ~value, value);
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline void _hw_endpoint_buffer_control_clear_mask32 (struct hw_endpoint *ep, uint32_t value)
|
||||
{
|
||||
_hw_endpoint_buffer_control_update32(ep, ~value, 0);
|
||||
}
|
||||
|
||||
static inline uintptr_t hw_data_offset (uint8_t *buf)
|
||||
{
|
||||
// Remove usb base from buffer pointer
|
||||
return (uintptr_t) buf ^ (uintptr_t) usb_dpram;
|
||||
}
|
||||
|
||||
extern const char *ep_dir_string[];
|
||||
|
||||
#endif
|
|
@ -0,0 +1,14 @@
|
|||
# C source files
|
||||
TINYUSB_SRC_C += \
|
||||
src/tusb.c \
|
||||
src/common/tusb_fifo.c \
|
||||
src/device/usbd.c \
|
||||
src/device/usbd_control.c \
|
||||
src/typec/usbc.c \
|
||||
src/class/cdc/cdc_device.c \
|
||||
src/class/hid/hid_device.c \
|
||||
src/host/usbh.c \
|
||||
src/host/hub.c \
|
||||
src/class/cdc/cdc_host.c \
|
||||
src/class/hid/hid_host.c \
|
||||
src/typec/usbc.c \
|
|
@ -0,0 +1,527 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#include "tusb_option.h"
|
||||
|
||||
#if CFG_TUH_ENABLED || CFG_TUD_ENABLED
|
||||
|
||||
#include "tusb.h"
|
||||
#include "common/tusb_private.h"
|
||||
|
||||
#if CFG_TUD_ENABLED
|
||||
#include "device/usbd_pvt.h"
|
||||
#endif
|
||||
|
||||
#if CFG_TUH_ENABLED
|
||||
#include "host/usbh_pvt.h"
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Public API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
bool tusb_init(void)
|
||||
{
|
||||
#if CFG_TUD_ENABLED && defined(TUD_OPT_RHPORT)
|
||||
// init device stack CFG_TUSB_RHPORTx_MODE must be defined
|
||||
TU_ASSERT ( tud_init(TUD_OPT_RHPORT) );
|
||||
#endif
|
||||
|
||||
#if CFG_TUH_ENABLED && defined(TUH_OPT_RHPORT)
|
||||
// init host stack CFG_TUSB_RHPORTx_MODE must be defined
|
||||
TU_ASSERT( tuh_init(TUH_OPT_RHPORT) );
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool tusb_inited(void)
|
||||
{
|
||||
bool ret = false;
|
||||
|
||||
#if CFG_TUD_ENABLED
|
||||
ret = ret || tud_inited();
|
||||
#endif
|
||||
|
||||
#if CFG_TUH_ENABLED
|
||||
ret = ret || tuh_inited();
|
||||
#endif
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Descriptor helper
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
uint8_t const * tu_desc_find(uint8_t const* desc, uint8_t const* end, uint8_t byte1)
|
||||
{
|
||||
while(desc+1 < end)
|
||||
{
|
||||
if ( desc[1] == byte1 ) return desc;
|
||||
desc += desc[DESC_OFFSET_LEN];
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
uint8_t const * tu_desc_find2(uint8_t const* desc, uint8_t const* end, uint8_t byte1, uint8_t byte2)
|
||||
{
|
||||
while(desc+2 < end)
|
||||
{
|
||||
if ( desc[1] == byte1 && desc[2] == byte2) return desc;
|
||||
desc += desc[DESC_OFFSET_LEN];
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
uint8_t const * tu_desc_find3(uint8_t const* desc, uint8_t const* end, uint8_t byte1, uint8_t byte2, uint8_t byte3)
|
||||
{
|
||||
while(desc+3 < end)
|
||||
{
|
||||
if (desc[1] == byte1 && desc[2] == byte2 && desc[3] == byte3) return desc;
|
||||
desc += desc[DESC_OFFSET_LEN];
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Endpoint Helper for both Host and Device stack
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
bool tu_edpt_claim(tu_edpt_state_t* ep_state, osal_mutex_t mutex)
|
||||
{
|
||||
(void) mutex;
|
||||
|
||||
// pre-check to help reducing mutex lock
|
||||
TU_VERIFY((ep_state->busy == 0) && (ep_state->claimed == 0));
|
||||
(void) osal_mutex_lock(mutex, OSAL_TIMEOUT_WAIT_FOREVER);
|
||||
|
||||
// can only claim the endpoint if it is not busy and not claimed yet.
|
||||
bool const available = (ep_state->busy == 0) && (ep_state->claimed == 0);
|
||||
if (available)
|
||||
{
|
||||
ep_state->claimed = 1;
|
||||
}
|
||||
|
||||
(void) osal_mutex_unlock(mutex);
|
||||
|
||||
return available;
|
||||
}
|
||||
|
||||
bool tu_edpt_release(tu_edpt_state_t* ep_state, osal_mutex_t mutex)
|
||||
{
|
||||
(void) mutex;
|
||||
|
||||
(void) osal_mutex_lock(mutex, OSAL_TIMEOUT_WAIT_FOREVER);
|
||||
|
||||
// can only release the endpoint if it is claimed and not busy
|
||||
bool const ret = (ep_state->claimed == 1) && (ep_state->busy == 0);
|
||||
if (ret)
|
||||
{
|
||||
ep_state->claimed = 0;
|
||||
}
|
||||
|
||||
(void) osal_mutex_unlock(mutex);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool tu_edpt_validate(tusb_desc_endpoint_t const * desc_ep, tusb_speed_t speed)
|
||||
{
|
||||
uint16_t const max_packet_size = tu_edpt_packet_size(desc_ep);
|
||||
TU_LOG2(" Open EP %02X with Size = %u\r\n", desc_ep->bEndpointAddress, max_packet_size);
|
||||
|
||||
switch (desc_ep->bmAttributes.xfer)
|
||||
{
|
||||
case TUSB_XFER_ISOCHRONOUS:
|
||||
{
|
||||
uint16_t const spec_size = (speed == TUSB_SPEED_HIGH ? 1024 : 1023);
|
||||
TU_ASSERT(max_packet_size <= spec_size);
|
||||
}
|
||||
break;
|
||||
|
||||
case TUSB_XFER_BULK:
|
||||
if (speed == TUSB_SPEED_HIGH)
|
||||
{
|
||||
// Bulk highspeed must be EXACTLY 512
|
||||
TU_ASSERT(max_packet_size == 512);
|
||||
}else
|
||||
{
|
||||
// TODO Bulk fullspeed can only be 8, 16, 32, 64
|
||||
TU_ASSERT(max_packet_size <= 64);
|
||||
}
|
||||
break;
|
||||
|
||||
case TUSB_XFER_INTERRUPT:
|
||||
{
|
||||
uint16_t const spec_size = (speed == TUSB_SPEED_HIGH ? 1024 : 64);
|
||||
TU_ASSERT(max_packet_size <= spec_size);
|
||||
}
|
||||
break;
|
||||
|
||||
default: return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void tu_edpt_bind_driver(uint8_t ep2drv[][2], tusb_desc_interface_t const* desc_itf, uint16_t desc_len, uint8_t driver_id)
|
||||
{
|
||||
uint8_t const* p_desc = (uint8_t const*) desc_itf;
|
||||
uint8_t const* desc_end = p_desc + desc_len;
|
||||
|
||||
while( p_desc < desc_end )
|
||||
{
|
||||
if ( TUSB_DESC_ENDPOINT == tu_desc_type(p_desc) )
|
||||
{
|
||||
uint8_t const ep_addr = ((tusb_desc_endpoint_t const*) p_desc)->bEndpointAddress;
|
||||
|
||||
TU_LOG(2, " Bind EP %02x to driver id %u\r\n", ep_addr, driver_id);
|
||||
ep2drv[tu_edpt_number(ep_addr)][tu_edpt_dir(ep_addr)] = driver_id;
|
||||
}
|
||||
|
||||
p_desc = tu_desc_next(p_desc);
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t tu_desc_get_interface_total_len(tusb_desc_interface_t const* desc_itf, uint8_t itf_count, uint16_t max_len)
|
||||
{
|
||||
uint8_t const* p_desc = (uint8_t const*) desc_itf;
|
||||
uint16_t len = 0;
|
||||
|
||||
while (itf_count--)
|
||||
{
|
||||
// Next on interface desc
|
||||
len += tu_desc_len(desc_itf);
|
||||
p_desc = tu_desc_next(p_desc);
|
||||
|
||||
while (len < max_len)
|
||||
{
|
||||
// return on IAD regardless of itf count
|
||||
if ( tu_desc_type(p_desc) == TUSB_DESC_INTERFACE_ASSOCIATION ) return len;
|
||||
|
||||
if ( (tu_desc_type(p_desc) == TUSB_DESC_INTERFACE) &&
|
||||
((tusb_desc_interface_t const*) p_desc)->bAlternateSetting == 0 )
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
len += tu_desc_len(p_desc);
|
||||
p_desc = tu_desc_next(p_desc);
|
||||
}
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Endpoint Stream Helper for both Host and Device stack
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
bool tu_edpt_stream_init(tu_edpt_stream_t* s, bool is_host, bool is_tx, bool overwritable,
|
||||
void* ff_buf, uint16_t ff_bufsize, uint8_t* ep_buf, uint16_t ep_bufsize)
|
||||
{
|
||||
osal_mutex_t new_mutex = osal_mutex_create(&s->ff_mutex);
|
||||
(void) new_mutex;
|
||||
(void) is_tx;
|
||||
|
||||
s->is_host = is_host;
|
||||
tu_fifo_config(&s->ff, ff_buf, ff_bufsize, 1, overwritable);
|
||||
tu_fifo_config_mutex(&s->ff, is_tx ? new_mutex : NULL, is_tx ? NULL : new_mutex);
|
||||
|
||||
s->ep_buf = ep_buf;
|
||||
s->ep_bufsize = ep_bufsize;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline
|
||||
bool stream_claim(tu_edpt_stream_t* s)
|
||||
{
|
||||
if (s->is_host)
|
||||
{
|
||||
#if CFG_TUH_ENABLED
|
||||
return usbh_edpt_claim(s->daddr, s->ep_addr);
|
||||
#endif
|
||||
}else
|
||||
{
|
||||
#if CFG_TUD_ENABLED
|
||||
return usbd_edpt_claim(s->rhport, s->ep_addr);
|
||||
#endif
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline
|
||||
bool stream_xfer(tu_edpt_stream_t* s, uint16_t count)
|
||||
{
|
||||
if (s->is_host)
|
||||
{
|
||||
#if CFG_TUH_ENABLED
|
||||
return usbh_edpt_xfer(s->daddr, s->ep_addr, count ? s->ep_buf : NULL, count);
|
||||
#endif
|
||||
}else
|
||||
{
|
||||
#if CFG_TUD_ENABLED
|
||||
return usbd_edpt_xfer(s->rhport, s->ep_addr, count ? s->ep_buf : NULL, count);
|
||||
#endif
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline
|
||||
bool stream_release(tu_edpt_stream_t* s)
|
||||
{
|
||||
if (s->is_host)
|
||||
{
|
||||
#if CFG_TUH_ENABLED
|
||||
return usbh_edpt_release(s->daddr, s->ep_addr);
|
||||
#endif
|
||||
}else
|
||||
{
|
||||
#if CFG_TUD_ENABLED
|
||||
return usbd_edpt_release(s->rhport, s->ep_addr);
|
||||
#endif
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Stream Write
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
bool tu_edpt_stream_write_zlp_if_needed(tu_edpt_stream_t* s, uint32_t last_xferred_bytes)
|
||||
{
|
||||
// ZLP condition: no pending data, last transferred bytes is multiple of packet size
|
||||
TU_VERIFY( !tu_fifo_count(&s->ff) && last_xferred_bytes && (0 == (last_xferred_bytes & (s->ep_packetsize-1))) );
|
||||
|
||||
TU_VERIFY( stream_claim(s) );
|
||||
TU_ASSERT( stream_xfer(s, 0) );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
uint32_t tu_edpt_stream_write_xfer(tu_edpt_stream_t* s)
|
||||
{
|
||||
// skip if no data
|
||||
TU_VERIFY( tu_fifo_count(&s->ff), 0 );
|
||||
|
||||
// Claim the endpoint
|
||||
TU_VERIFY( stream_claim(s), 0 );
|
||||
|
||||
// Pull data from FIFO -> EP buf
|
||||
uint16_t const count = tu_fifo_read_n(&s->ff, s->ep_buf, s->ep_bufsize);
|
||||
|
||||
if ( count )
|
||||
{
|
||||
TU_ASSERT( stream_xfer(s, count), 0 );
|
||||
return count;
|
||||
}else
|
||||
{
|
||||
// Release endpoint since we don't make any transfer
|
||||
// Note: data is dropped if terminal is not connected
|
||||
stream_release(s);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t tu_edpt_stream_write(tu_edpt_stream_t* s, void const *buffer, uint32_t bufsize)
|
||||
{
|
||||
TU_VERIFY(bufsize); // TODO support ZLP
|
||||
|
||||
uint16_t ret = tu_fifo_write_n(&s->ff, buffer, (uint16_t) bufsize);
|
||||
|
||||
// flush if fifo has more than packet size or
|
||||
// in rare case: fifo depth is configured too small (which never reach packet size)
|
||||
if ( (tu_fifo_count(&s->ff) >= s->ep_packetsize) || (tu_fifo_depth(&s->ff) < s->ep_packetsize) )
|
||||
{
|
||||
tu_edpt_stream_write_xfer(s);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Stream Read
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
uint32_t tu_edpt_stream_read_xfer(tu_edpt_stream_t* s)
|
||||
{
|
||||
uint16_t available = tu_fifo_remaining(&s->ff);
|
||||
|
||||
// Prepare for incoming data but only allow what we can store in the ring buffer.
|
||||
// TODO Actually we can still carry out the transfer, keeping count of received bytes
|
||||
// and slowly move it to the FIFO when read().
|
||||
// This pre-check reduces endpoint claiming
|
||||
TU_VERIFY(available >= s->ep_packetsize);
|
||||
|
||||
// claim endpoint
|
||||
TU_VERIFY(stream_claim(s), 0);
|
||||
|
||||
// get available again since fifo can be changed before endpoint is claimed
|
||||
available = tu_fifo_remaining(&s->ff);
|
||||
|
||||
if ( available >= s->ep_packetsize )
|
||||
{
|
||||
// multiple of packet size limit by ep bufsize
|
||||
uint16_t count = (uint16_t) (available & ~(s->ep_packetsize -1));
|
||||
count = tu_min16(count, s->ep_bufsize);
|
||||
|
||||
TU_ASSERT( stream_xfer(s, count), 0 );
|
||||
|
||||
return count;
|
||||
}else
|
||||
{
|
||||
// Release endpoint since we don't make any transfer
|
||||
stream_release(s);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t tu_edpt_stream_read(tu_edpt_stream_t* s, void* buffer, uint32_t bufsize)
|
||||
{
|
||||
uint32_t num_read = tu_fifo_read_n(&s->ff, buffer, (uint16_t) bufsize);
|
||||
tu_edpt_stream_read_xfer(s);
|
||||
return num_read;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Debug
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
#if CFG_TUSB_DEBUG
|
||||
#include <ctype.h>
|
||||
|
||||
#if CFG_TUSB_DEBUG >= CFG_TUH_LOG_LEVEL || CFG_TUSB_DEBUG >= CFG_TUD_LOG_LEVEL
|
||||
|
||||
char const* const tu_str_speed[] = { "Full", "Low", "High" };
|
||||
char const* const tu_str_std_request[] =
|
||||
{
|
||||
"Get Status" ,
|
||||
"Clear Feature" ,
|
||||
"Reserved" ,
|
||||
"Set Feature" ,
|
||||
"Reserved" ,
|
||||
"Set Address" ,
|
||||
"Get Descriptor" ,
|
||||
"Set Descriptor" ,
|
||||
"Get Configuration" ,
|
||||
"Set Configuration" ,
|
||||
"Get Interface" ,
|
||||
"Set Interface" ,
|
||||
"Synch Frame"
|
||||
};
|
||||
|
||||
char const* const tu_str_xfer_result[] = {
|
||||
"OK", "FAILED", "STALLED", "TIMEOUT"
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
static void dump_str_line(uint8_t const* buf, uint16_t count)
|
||||
{
|
||||
tu_printf(" |");
|
||||
|
||||
// each line is 16 bytes
|
||||
for(uint16_t i=0; i<count; i++)
|
||||
{
|
||||
const char ch = buf[i];
|
||||
tu_printf("%c", isprint(ch) ? ch : '.');
|
||||
}
|
||||
|
||||
tu_printf("|\r\n");
|
||||
}
|
||||
|
||||
/* Print out memory contents
|
||||
* - buf : buffer
|
||||
* - count : number of item
|
||||
* - indent: prefix spaces on every line
|
||||
*/
|
||||
void tu_print_mem(void const *buf, uint32_t count, uint8_t indent)
|
||||
{
|
||||
uint8_t const size = 1; // fixed 1 byte for now
|
||||
|
||||
if ( !buf || !count )
|
||||
{
|
||||
tu_printf("NULL\r\n");
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t const *buf8 = (uint8_t const *) buf;
|
||||
|
||||
char format[] = "%00X";
|
||||
format[2] += 2*size;
|
||||
|
||||
const uint8_t item_per_line = 16 / size;
|
||||
|
||||
for(unsigned int i=0; i<count; i++)
|
||||
{
|
||||
unsigned int value=0;
|
||||
|
||||
if ( i%item_per_line == 0 )
|
||||
{
|
||||
// Print Ascii
|
||||
if ( i != 0 )
|
||||
{
|
||||
dump_str_line(buf8-16, 16);
|
||||
}
|
||||
|
||||
for(uint8_t s=0; s < indent; s++) tu_printf(" ");
|
||||
|
||||
// print offset or absolute address
|
||||
tu_printf("%04X: ", 16*i/item_per_line);
|
||||
}
|
||||
|
||||
tu_memcpy_s(&value, sizeof(value), buf8, size);
|
||||
buf8 += size;
|
||||
|
||||
tu_printf(" ");
|
||||
tu_printf(format, value);
|
||||
}
|
||||
|
||||
// fill up last row to 16 for printing ascii
|
||||
const uint32_t remain = count%16;
|
||||
uint8_t nback = (uint8_t)(remain ? remain : 16);
|
||||
|
||||
if ( remain )
|
||||
{
|
||||
for(uint32_t i=0; i< 16-remain; i++)
|
||||
{
|
||||
tu_printf(" ");
|
||||
for(int j=0; j<2*size; j++) tu_printf(" ");
|
||||
}
|
||||
}
|
||||
|
||||
dump_str_line(buf8-nback, nback);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#endif // host or device enabled
|
|
@ -0,0 +1,150 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#ifndef _TUSB_H_
|
||||
#define _TUSB_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// INCLUDE
|
||||
//--------------------------------------------------------------------+
|
||||
#include "common/tusb_common.h"
|
||||
#include "osal/osal.h"
|
||||
#include "common/tusb_fifo.h"
|
||||
|
||||
#include "class/hid/hid.h"
|
||||
|
||||
//------------- TypeC -------------//
|
||||
#if CFG_TUC_ENABLED
|
||||
#include "typec/usbc.h"
|
||||
#endif
|
||||
|
||||
//------------- HOST -------------//
|
||||
#if CFG_TUH_ENABLED
|
||||
#include "host/usbh.h"
|
||||
|
||||
#if CFG_TUH_HID
|
||||
#include "class/hid/hid_host.h"
|
||||
#endif
|
||||
|
||||
#if CFG_TUH_MSC
|
||||
#include "class/msc/msc_host.h"
|
||||
#endif
|
||||
|
||||
#if CFG_TUH_CDC
|
||||
#include "class/cdc/cdc_host.h"
|
||||
#endif
|
||||
|
||||
#if CFG_TUH_VENDOR
|
||||
#include "class/vendor/vendor_host.h"
|
||||
#endif
|
||||
#else
|
||||
#ifndef tuh_int_handler
|
||||
#define tuh_int_handler(...)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
//------------- DEVICE -------------//
|
||||
#if CFG_TUD_ENABLED
|
||||
#include "device/usbd.h"
|
||||
|
||||
#if CFG_TUD_HID
|
||||
#include "class/hid/hid_device.h"
|
||||
#endif
|
||||
|
||||
#if CFG_TUD_CDC
|
||||
#include "class/cdc/cdc_device.h"
|
||||
#endif
|
||||
|
||||
#if CFG_TUD_MSC
|
||||
#include "class/msc/msc_device.h"
|
||||
#endif
|
||||
|
||||
#if CFG_TUD_AUDIO
|
||||
#include "class/audio/audio_device.h"
|
||||
#endif
|
||||
|
||||
#if CFG_TUD_VIDEO
|
||||
#include "class/video/video_device.h"
|
||||
#endif
|
||||
|
||||
#if CFG_TUD_MIDI
|
||||
#include "class/midi/midi_device.h"
|
||||
#endif
|
||||
|
||||
#if CFG_TUD_VENDOR
|
||||
#include "class/vendor/vendor_device.h"
|
||||
#endif
|
||||
|
||||
#if CFG_TUD_USBTMC
|
||||
#include "class/usbtmc/usbtmc_device.h"
|
||||
#endif
|
||||
|
||||
#if CFG_TUD_DFU_RUNTIME
|
||||
#include "class/dfu/dfu_rt_device.h"
|
||||
#endif
|
||||
|
||||
#if CFG_TUD_DFU
|
||||
#include "class/dfu/dfu_device.h"
|
||||
#endif
|
||||
|
||||
#if CFG_TUD_ECM_RNDIS || CFG_TUD_NCM
|
||||
#include "class/net/net_device.h"
|
||||
#endif
|
||||
|
||||
#if CFG_TUD_BTH
|
||||
#include "class/bth/bth_device.h"
|
||||
#endif
|
||||
#else
|
||||
#ifndef tud_int_handler
|
||||
#define tud_int_handler(...)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// APPLICATION API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Initialize device/host stack
|
||||
// Note: when using with RTOS, this should be called after scheduler/kernel is started.
|
||||
// Otherwise it could cause kernel issue since USB IRQ handler does use RTOS queue API.
|
||||
bool tusb_init(void);
|
||||
|
||||
// Check if stack is initialized
|
||||
bool tusb_inited(void);
|
||||
|
||||
// TODO
|
||||
// bool tusb_teardown(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _TUSB_H_ */
|
|
@ -0,0 +1,531 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#ifndef _TUSB_OPTION_H_
|
||||
#define _TUSB_OPTION_H_
|
||||
|
||||
#include "common/tusb_compiler.h"
|
||||
|
||||
#define TUSB_VERSION_MAJOR 0
|
||||
#define TUSB_VERSION_MINOR 16
|
||||
#define TUSB_VERSION_REVISION 0
|
||||
#define TUSB_VERSION_STRING TU_STRING(TUSB_VERSION_MAJOR) "." TU_STRING(TUSB_VERSION_MINOR) "." TU_STRING(TUSB_VERSION_REVISION)
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Supported MCUs
|
||||
// CFG_TUSB_MCU must be defined to one of following value
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
#define OPT_MCU_NONE 0
|
||||
|
||||
// LPC
|
||||
#define OPT_MCU_LPC11UXX 1 ///< NXP LPC11Uxx
|
||||
#define OPT_MCU_LPC13XX 2 ///< NXP LPC13xx
|
||||
#define OPT_MCU_LPC15XX 3 ///< NXP LPC15xx
|
||||
#define OPT_MCU_LPC175X_6X 4 ///< NXP LPC175x, LPC176x
|
||||
#define OPT_MCU_LPC177X_8X 5 ///< NXP LPC177x, LPC178x
|
||||
#define OPT_MCU_LPC18XX 6 ///< NXP LPC18xx
|
||||
#define OPT_MCU_LPC40XX 7 ///< NXP LPC40xx
|
||||
#define OPT_MCU_LPC43XX 8 ///< NXP LPC43xx
|
||||
#define OPT_MCU_LPC51UXX 9 ///< NXP LPC51U6x
|
||||
#define OPT_MCU_LPC54 10 ///< NXP LPC54
|
||||
#define OPT_MCU_LPC55 11 ///< NXP LPC55
|
||||
// legacy naming
|
||||
#define OPT_MCU_LPC54XXX OPT_MCU_LPC54
|
||||
#define OPT_MCU_LPC55XX OPT_MCU_LPC55
|
||||
|
||||
// NRF
|
||||
#define OPT_MCU_NRF5X 100 ///< Nordic nRF5x series
|
||||
|
||||
// SAM
|
||||
#define OPT_MCU_SAMD21 200 ///< MicroChip SAMD21
|
||||
#define OPT_MCU_SAMD51 201 ///< MicroChip SAMD51
|
||||
#define OPT_MCU_SAMG 202 ///< MicroChip SAMDG series
|
||||
#define OPT_MCU_SAME5X 203 ///< MicroChip SAM E5x
|
||||
#define OPT_MCU_SAMD11 204 ///< MicroChip SAMD11
|
||||
#define OPT_MCU_SAML22 205 ///< MicroChip SAML22
|
||||
#define OPT_MCU_SAML21 206 ///< MicroChip SAML21
|
||||
#define OPT_MCU_SAMX7X 207 ///< MicroChip SAME70, S70, V70, V71 family
|
||||
|
||||
// STM32
|
||||
#define OPT_MCU_STM32F0 300 ///< ST F0
|
||||
#define OPT_MCU_STM32F1 301 ///< ST F1
|
||||
#define OPT_MCU_STM32F2 302 ///< ST F2
|
||||
#define OPT_MCU_STM32F3 303 ///< ST F3
|
||||
#define OPT_MCU_STM32F4 304 ///< ST F4
|
||||
#define OPT_MCU_STM32F7 305 ///< ST F7
|
||||
#define OPT_MCU_STM32H7 306 ///< ST H7
|
||||
#define OPT_MCU_STM32L1 308 ///< ST L1
|
||||
#define OPT_MCU_STM32L0 307 ///< ST L0
|
||||
#define OPT_MCU_STM32L4 309 ///< ST L4
|
||||
#define OPT_MCU_STM32G0 310 ///< ST G0
|
||||
#define OPT_MCU_STM32G4 311 ///< ST G4
|
||||
#define OPT_MCU_STM32WB 312 ///< ST WB
|
||||
#define OPT_MCU_STM32U5 313 ///< ST U5
|
||||
#define OPT_MCU_STM32L5 314 ///< ST L5
|
||||
#define OPT_MCU_STM32H5 315 ///< ST H5
|
||||
|
||||
// Sony
|
||||
#define OPT_MCU_CXD56 400 ///< SONY CXD56
|
||||
|
||||
// TI
|
||||
#define OPT_MCU_MSP430x5xx 500 ///< TI MSP430x5xx
|
||||
#define OPT_MCU_MSP432E4 510 ///< TI MSP432E4xx
|
||||
#define OPT_MCU_TM4C123 511 ///< TI Tiva-C 123x
|
||||
#define OPT_MCU_TM4C129 512 ///< TI Tiva-C 129x
|
||||
|
||||
// ValentyUSB eptri
|
||||
#define OPT_MCU_VALENTYUSB_EPTRI 600 ///< Fomu eptri config
|
||||
|
||||
// NXP iMX RT
|
||||
#define OPT_MCU_MIMXRT1XXX 700 ///< NXP iMX RT1xxx Series
|
||||
#define OPT_MCU_MIMXRT10XX OPT_MCU_MIMXRT1XXX ///< RT10xx
|
||||
#define OPT_MCU_MIMXRT11XX OPT_MCU_MIMXRT1XXX ///< RT11xx
|
||||
|
||||
// Nuvoton
|
||||
#define OPT_MCU_NUC121 800
|
||||
#define OPT_MCU_NUC126 801
|
||||
#define OPT_MCU_NUC120 802
|
||||
#define OPT_MCU_NUC505 803
|
||||
|
||||
// Espressif
|
||||
#define OPT_MCU_ESP32S2 900 ///< Espressif ESP32-S2
|
||||
#define OPT_MCU_ESP32S3 901 ///< Espressif ESP32-S3
|
||||
|
||||
// Dialog
|
||||
#define OPT_MCU_DA1469X 1000 ///< Dialog Semiconductor DA1469x
|
||||
|
||||
// Raspberry Pi
|
||||
#define OPT_MCU_RP2040 1100 ///< Raspberry Pi RP2040
|
||||
|
||||
// NXP Kinetis
|
||||
#define OPT_MCU_KINETIS_KL 1200 ///< NXP KL series
|
||||
#define OPT_MCU_KINETIS_K32L 1201 ///< NXP K32L series
|
||||
#define OPT_MCU_KINETIS_K32 1201 ///< Alias to K32L
|
||||
|
||||
#define OPT_MCU_MKL25ZXX 1200 ///< Alias to KL (obsolete)
|
||||
#define OPT_MCU_K32L2BXX 1201 ///< Alias to K32 (obsolete)
|
||||
|
||||
// Silabs
|
||||
#define OPT_MCU_EFM32GG 1300 ///< Silabs EFM32GG
|
||||
|
||||
// Renesas RX
|
||||
#define OPT_MCU_RX63X 1400 ///< Renesas RX63N/631
|
||||
#define OPT_MCU_RX65X 1401 ///< Renesas RX65N/RX651
|
||||
#define OPT_MCU_RX72N 1402 ///< Renesas RX72N
|
||||
#define OPT_MCU_RAXXX 1403 ///< Renesas RAxxx families
|
||||
|
||||
|
||||
// Mind Motion
|
||||
#define OPT_MCU_MM32F327X 1500 ///< Mind Motion MM32F327
|
||||
|
||||
// GigaDevice
|
||||
#define OPT_MCU_GD32VF103 1600 ///< GigaDevice GD32VF103
|
||||
|
||||
// Broadcom
|
||||
#define OPT_MCU_BCM2711 1700 ///< Broadcom BCM2711
|
||||
#define OPT_MCU_BCM2835 1701 ///< Broadcom BCM2835
|
||||
#define OPT_MCU_BCM2837 1702 ///< Broadcom BCM2837
|
||||
|
||||
// Infineon
|
||||
#define OPT_MCU_XMC4000 1800 ///< Infineon XMC4000
|
||||
|
||||
// PIC
|
||||
#define OPT_MCU_PIC32MZ 1900 ///< MicroChip PIC32MZ family
|
||||
#define OPT_MCU_PIC32MM 1901 ///< MicroChip PIC32MM family
|
||||
#define OPT_MCU_PIC32MX 1902 ///< MicroChip PIC32MX family
|
||||
#define OPT_MCU_PIC32MK 1903 ///< MicroChip PIC32MK family
|
||||
#define OPT_MCU_PIC24 1910 ///< MicroChip PIC24 family
|
||||
#define OPT_MCU_DSPIC33 1911 ///< MicroChip DSPIC33 family
|
||||
|
||||
// BridgeTek
|
||||
#define OPT_MCU_FT90X 2000 ///< BridgeTek FT90x
|
||||
#define OPT_MCU_FT93X 2001 ///< BridgeTek FT93x
|
||||
|
||||
// Allwinner
|
||||
#define OPT_MCU_F1C100S 2100 ///< Allwinner F1C100s family
|
||||
|
||||
// WCH
|
||||
#define OPT_MCU_CH32V307 2200 ///< WCH CH32V307
|
||||
#define OPT_MCU_CH32F20X 2210 ///< WCH CH32F20x
|
||||
|
||||
|
||||
// NXP LPC MCX
|
||||
#define OPT_MCU_MCXN9 2300 ///< NXP MCX N9 Series
|
||||
|
||||
// Check if configured MCU is one of listed
|
||||
// Apply _TU_CHECK_MCU with || as separator to list of input
|
||||
#define _TU_CHECK_MCU(_m) (CFG_TUSB_MCU == _m)
|
||||
#define TU_CHECK_MCU(...) (TU_ARGS_APPLY(_TU_CHECK_MCU, ||, __VA_ARGS__))
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Supported OS
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
#define OPT_OS_NONE 1 ///< No RTOS
|
||||
#define OPT_OS_FREERTOS 2 ///< FreeRTOS
|
||||
#define OPT_OS_MYNEWT 3 ///< Mynewt OS
|
||||
#define OPT_OS_CUSTOM 4 ///< Custom OS is implemented by application
|
||||
#define OPT_OS_PICO 5 ///< Raspberry Pi Pico SDK
|
||||
#define OPT_OS_RTTHREAD 6 ///< RT-Thread
|
||||
#define OPT_OS_RTX4 7 ///< Keil RTX 4
|
||||
|
||||
// Allow to use command line to change the config name/location
|
||||
#ifdef CFG_TUSB_CONFIG_FILE
|
||||
#include CFG_TUSB_CONFIG_FILE
|
||||
#else
|
||||
#include "tusb_config.h"
|
||||
#endif
|
||||
|
||||
#include "common/tusb_mcu.h"
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
// RootHub Mode Configuration
|
||||
// CFG_TUSB_RHPORTx_MODE contains operation mode and speed for that port
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
// Low byte is operational mode
|
||||
#define OPT_MODE_NONE 0x0000 ///< Disabled
|
||||
#define OPT_MODE_DEVICE 0x0001 ///< Device Mode
|
||||
#define OPT_MODE_HOST 0x0002 ///< Host Mode
|
||||
|
||||
// High byte is max operational speed (corresponding to tusb_speed_t)
|
||||
#define OPT_MODE_DEFAULT_SPEED 0x0000 ///< Default (max) speed supported by MCU
|
||||
#define OPT_MODE_LOW_SPEED 0x0100 ///< Low Speed
|
||||
#define OPT_MODE_FULL_SPEED 0x0200 ///< Full Speed
|
||||
#define OPT_MODE_HIGH_SPEED 0x0400 ///< High Speed
|
||||
#define OPT_MODE_SPEED_MASK 0xff00
|
||||
|
||||
//------------- Roothub as Device -------------//
|
||||
|
||||
#if defined(CFG_TUSB_RHPORT0_MODE) && ((CFG_TUSB_RHPORT0_MODE) & OPT_MODE_DEVICE)
|
||||
#define TUD_RHPORT_MODE (CFG_TUSB_RHPORT0_MODE)
|
||||
#define TUD_OPT_RHPORT 0
|
||||
#elif defined(CFG_TUSB_RHPORT1_MODE) && ((CFG_TUSB_RHPORT1_MODE) & OPT_MODE_DEVICE)
|
||||
#define TUD_RHPORT_MODE (CFG_TUSB_RHPORT1_MODE)
|
||||
#define TUD_OPT_RHPORT 1
|
||||
#else
|
||||
#define TUD_RHPORT_MODE OPT_MODE_NONE
|
||||
#endif
|
||||
|
||||
#ifndef CFG_TUD_ENABLED
|
||||
// fallback to use CFG_TUSB_RHPORTx_MODE
|
||||
#define CFG_TUD_ENABLED (TUD_RHPORT_MODE & OPT_MODE_DEVICE)
|
||||
#endif
|
||||
|
||||
#ifndef CFG_TUD_MAX_SPEED
|
||||
// fallback to use CFG_TUSB_RHPORTx_MODE
|
||||
#define CFG_TUD_MAX_SPEED (TUD_RHPORT_MODE & OPT_MODE_SPEED_MASK)
|
||||
#endif
|
||||
|
||||
// For backward compatible
|
||||
#define TUSB_OPT_DEVICE_ENABLED CFG_TUD_ENABLED
|
||||
|
||||
// highspeed support indicator
|
||||
#define TUD_OPT_HIGH_SPEED (CFG_TUD_MAX_SPEED ? (CFG_TUD_MAX_SPEED & OPT_MODE_HIGH_SPEED) : TUP_RHPORT_HIGHSPEED)
|
||||
|
||||
//------------- Roothub as Host -------------//
|
||||
|
||||
#if defined(CFG_TUSB_RHPORT0_MODE) && ((CFG_TUSB_RHPORT0_MODE) & OPT_MODE_HOST)
|
||||
#define TUH_RHPORT_MODE (CFG_TUSB_RHPORT0_MODE)
|
||||
#define TUH_OPT_RHPORT 0
|
||||
#elif defined(CFG_TUSB_RHPORT1_MODE) && ((CFG_TUSB_RHPORT1_MODE) & OPT_MODE_HOST)
|
||||
#define TUH_RHPORT_MODE (CFG_TUSB_RHPORT1_MODE)
|
||||
#define TUH_OPT_RHPORT 1
|
||||
#else
|
||||
#define TUH_RHPORT_MODE OPT_MODE_NONE
|
||||
#endif
|
||||
|
||||
#ifndef CFG_TUH_ENABLED
|
||||
// fallback to use CFG_TUSB_RHPORTx_MODE
|
||||
#define CFG_TUH_ENABLED (TUH_RHPORT_MODE & OPT_MODE_HOST)
|
||||
#endif
|
||||
|
||||
#ifndef CFG_TUH_MAX_SPEED
|
||||
// fallback to use CFG_TUSB_RHPORTx_MODE
|
||||
#define CFG_TUH_MAX_SPEED (TUH_RHPORT_MODE & OPT_MODE_SPEED_MASK)
|
||||
#endif
|
||||
|
||||
// For backward compatible
|
||||
#define TUSB_OPT_HOST_ENABLED CFG_TUH_ENABLED
|
||||
|
||||
// highspeed support indicator
|
||||
#define TUH_OPT_HIGH_SPEED (CFG_TUH_MAX_SPEED ? (CFG_TUH_MAX_SPEED & OPT_MODE_HIGH_SPEED) : TUP_RHPORT_HIGHSPEED)
|
||||
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// TODO move later
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// TUP_MCU_STRICT_ALIGN will overwrite TUP_ARCH_STRICT_ALIGN.
|
||||
// In case TUP_MCU_STRICT_ALIGN = 1 and TUP_ARCH_STRICT_ALIGN =0, we will not reply on compiler
|
||||
// to generate unaligned access code.
|
||||
// LPC_IP3511 Highspeed cannot access unaligned memory on USB_RAM
|
||||
#if TUD_OPT_HIGH_SPEED && TU_CHECK_MCU(OPT_MCU_LPC54XXX, OPT_MCU_LPC55XX)
|
||||
#define TUP_MCU_STRICT_ALIGN 1
|
||||
#else
|
||||
#define TUP_MCU_STRICT_ALIGN 0
|
||||
#endif
|
||||
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Common Options (Default)
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Debug enable to print out error message
|
||||
#ifndef CFG_TUSB_DEBUG
|
||||
#define CFG_TUSB_DEBUG 0
|
||||
#endif
|
||||
|
||||
// Level where CFG_TUSB_DEBUG must be at least for USBH is logged
|
||||
#ifndef CFG_TUH_LOG_LEVEL
|
||||
#define CFG_TUH_LOG_LEVEL 2
|
||||
#endif
|
||||
|
||||
// Level where CFG_TUSB_DEBUG must be at least for USBD is logged
|
||||
#ifndef CFG_TUD_LOG_LEVEL
|
||||
#define CFG_TUD_LOG_LEVEL 2
|
||||
#endif
|
||||
|
||||
// Memory section for placing buffer used for usb transferring. If MEM_SECTION is different for
|
||||
// host and device use: CFG_TUD_MEM_SECTION, CFG_TUH_MEM_SECTION instead
|
||||
#ifndef CFG_TUSB_MEM_SECTION
|
||||
#define CFG_TUSB_MEM_SECTION
|
||||
#endif
|
||||
|
||||
// Alignment requirement of buffer used for usb transferring. if MEM_ALIGN is different for
|
||||
// host and device controller use: CFG_TUD_MEM_ALIGN, CFG_TUH_MEM_ALIGN instead
|
||||
#ifndef CFG_TUSB_MEM_ALIGN
|
||||
#define CFG_TUSB_MEM_ALIGN TU_ATTR_ALIGNED(4)
|
||||
#endif
|
||||
|
||||
// OS selection
|
||||
#ifndef CFG_TUSB_OS
|
||||
#define CFG_TUSB_OS OPT_OS_NONE
|
||||
#endif
|
||||
|
||||
#ifndef CFG_TUSB_OS_INC_PATH
|
||||
#define CFG_TUSB_OS_INC_PATH
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
// Device Options (Default)
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
// Attribute to place data in accessible RAM for device controller (default: CFG_TUSB_MEM_SECTION)
|
||||
#ifndef CFG_TUD_MEM_SECTION
|
||||
#define CFG_TUD_MEM_SECTION CFG_TUSB_MEM_SECTION
|
||||
#endif
|
||||
|
||||
// Attribute to align memory for device controller (default: CFG_TUSB_MEM_ALIGN)
|
||||
#ifndef CFG_TUD_MEM_ALIGN
|
||||
#define CFG_TUD_MEM_ALIGN CFG_TUSB_MEM_ALIGN
|
||||
#endif
|
||||
|
||||
#ifndef CFG_TUD_ENDPOINT0_SIZE
|
||||
#define CFG_TUD_ENDPOINT0_SIZE 64
|
||||
#endif
|
||||
|
||||
#ifndef CFG_TUD_INTERFACE_MAX
|
||||
#define CFG_TUD_INTERFACE_MAX 16
|
||||
#endif
|
||||
|
||||
//------------- Device Class Driver -------------//
|
||||
#ifndef CFG_TUD_BTH
|
||||
#define CFG_TUD_BTH 0
|
||||
#endif
|
||||
|
||||
#if CFG_TUD_BTH && !defined(CFG_TUD_BTH_ISO_ALT_COUNT)
|
||||
#error CFG_TUD_BTH_ISO_ALT_COUNT must be defined to tell Bluetooth driver the number of ISO endpoints to use
|
||||
#endif
|
||||
|
||||
#ifndef CFG_TUD_CDC
|
||||
#define CFG_TUD_CDC 0
|
||||
#endif
|
||||
|
||||
#ifndef CFG_TUD_MSC
|
||||
#define CFG_TUD_MSC 0
|
||||
#endif
|
||||
|
||||
#ifndef CFG_TUD_HID
|
||||
#define CFG_TUD_HID 0
|
||||
#endif
|
||||
|
||||
#ifndef CFG_TUD_AUDIO
|
||||
#define CFG_TUD_AUDIO 0
|
||||
#endif
|
||||
|
||||
#ifndef CFG_TUD_VIDEO
|
||||
#define CFG_TUD_VIDEO 0
|
||||
#endif
|
||||
|
||||
#ifndef CFG_TUD_MIDI
|
||||
#define CFG_TUD_MIDI 0
|
||||
#endif
|
||||
|
||||
#ifndef CFG_TUD_VENDOR
|
||||
#define CFG_TUD_VENDOR 0
|
||||
#endif
|
||||
|
||||
#ifndef CFG_TUD_USBTMC
|
||||
#define CFG_TUD_USBTMC 0
|
||||
#endif
|
||||
|
||||
#ifndef CFG_TUD_DFU_RUNTIME
|
||||
#define CFG_TUD_DFU_RUNTIME 0
|
||||
#endif
|
||||
|
||||
#ifndef CFG_TUD_DFU
|
||||
#define CFG_TUD_DFU 0
|
||||
#endif
|
||||
|
||||
#ifndef CFG_TUD_ECM_RNDIS
|
||||
#ifdef CFG_TUD_NET
|
||||
#warning "CFG_TUD_NET is renamed to CFG_TUD_ECM_RNDIS"
|
||||
#define CFG_TUD_ECM_RNDIS CFG_TUD_NET
|
||||
#else
|
||||
#define CFG_TUD_ECM_RNDIS 0
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef CFG_TUD_NCM
|
||||
#define CFG_TUD_NCM 0
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
// Host Options (Default)
|
||||
//--------------------------------------------------------------------
|
||||
#if CFG_TUH_ENABLED
|
||||
#ifndef CFG_TUH_DEVICE_MAX
|
||||
#define CFG_TUH_DEVICE_MAX 1
|
||||
#endif
|
||||
|
||||
#ifndef CFG_TUH_ENUMERATION_BUFSIZE
|
||||
#define CFG_TUH_ENUMERATION_BUFSIZE 256
|
||||
#endif
|
||||
#endif // CFG_TUH_ENABLED
|
||||
|
||||
// Attribute to place data in accessible RAM for host controller (default: CFG_TUSB_MEM_SECTION)
|
||||
#ifndef CFG_TUH_MEM_SECTION
|
||||
#define CFG_TUH_MEM_SECTION CFG_TUSB_MEM_SECTION
|
||||
#endif
|
||||
|
||||
// Attribute to align memory for host controller
|
||||
#ifndef CFG_TUH_MEM_ALIGN
|
||||
#define CFG_TUH_MEM_ALIGN CFG_TUSB_MEM_ALIGN
|
||||
#endif
|
||||
|
||||
//------------- CLASS -------------//
|
||||
|
||||
#ifndef CFG_TUH_HUB
|
||||
#define CFG_TUH_HUB 0
|
||||
#endif
|
||||
|
||||
#ifndef CFG_TUH_CDC
|
||||
#define CFG_TUH_CDC 0
|
||||
#endif
|
||||
|
||||
#ifndef CFG_TUH_CDC_FTDI
|
||||
// FTDI is not part of CDC class, only to re-use CDC driver API
|
||||
#define CFG_TUH_CDC_FTDI 0
|
||||
#endif
|
||||
|
||||
#ifndef CFG_TUH_CDC_FTDI_VID_PID_LIST
|
||||
// List of product IDs that can use the FTDI CDC driver. 0x0403 is FTDI's VID
|
||||
#define CFG_TUH_CDC_FTDI_VID_PID_LIST \
|
||||
{0x0403, 0x6001}, {0x0403, 0x6006}, {0x0403, 0x6010}, {0x0403, 0x6011}, \
|
||||
{0x0403, 0x6014}, {0x0403, 0x6015}, {0x0403, 0x8372}, {0x0403, 0xFBFA}, \
|
||||
{0x0403, 0xCD18}
|
||||
#endif
|
||||
|
||||
#ifndef CFG_TUH_CDC_CP210X
|
||||
// CP210X is not part of CDC class, only to re-use CDC driver API
|
||||
#define CFG_TUH_CDC_CP210X 0
|
||||
#endif
|
||||
|
||||
#ifndef CFG_TUH_CDC_CP210X_VID_PID_LIST
|
||||
// List of product IDs that can use the CP210X CDC driver. 0x10C4 is Silicon Labs' VID
|
||||
#define CFG_TUH_CDC_CP210X_VID_PID_LIST \
|
||||
{0x10C4, 0xEA60}, {0x10C4, 0xEA70}
|
||||
#endif
|
||||
|
||||
#ifndef CFG_TUH_HID
|
||||
#define CFG_TUH_HID 0
|
||||
#endif
|
||||
|
||||
#ifndef CFG_TUH_MIDI
|
||||
#define CFG_TUH_MIDI 0
|
||||
#endif
|
||||
|
||||
#ifndef CFG_TUH_MSC
|
||||
#define CFG_TUH_MSC 0
|
||||
#endif
|
||||
|
||||
#ifndef CFG_TUH_VENDOR
|
||||
#define CFG_TUH_VENDOR 0
|
||||
#endif
|
||||
|
||||
#ifndef CFG_TUH_API_EDPT_XFER
|
||||
#define CFG_TUH_API_EDPT_XFER 0
|
||||
#endif
|
||||
|
||||
// Enable PIO-USB software host controller
|
||||
#ifndef CFG_TUH_RPI_PIO_USB
|
||||
#define CFG_TUH_RPI_PIO_USB 0
|
||||
#endif
|
||||
|
||||
#ifndef CFG_TUD_RPI_PIO_USB
|
||||
#define CFG_TUD_RPI_PIO_USB 0
|
||||
#endif
|
||||
|
||||
// MAX3421 Host controller option
|
||||
#ifndef CFG_TUH_MAX3421
|
||||
#define CFG_TUH_MAX3421 0
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// TypeC Options (Default)
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
#ifndef CFG_TUC_ENABLED
|
||||
#define CFG_TUC_ENABLED 0
|
||||
|
||||
#define tuc_int_handler(_p)
|
||||
#endif
|
||||
|
||||
//------------------------------------------------------------------
|
||||
// Configuration Validation
|
||||
//------------------------------------------------------------------
|
||||
#if CFG_TUD_ENDPOINT0_SIZE > 64
|
||||
#error Control Endpoint Max Packet Size cannot be larger than 64
|
||||
#endif
|
||||
|
||||
// To avoid GCC compiler warnings when -pedantic option is used (strict ISO C)
|
||||
typedef int make_iso_compilers_happy;
|
||||
|
||||
#endif /* _TUSB_OPTION_H_ */
|
||||
|
||||
/** @} */
|
|
@ -0,0 +1,237 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2023 Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#ifndef _TUSB_PD_TYPES_H_
|
||||
#define _TUSB_PD_TYPES_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include "common/tusb_compiler.h"
|
||||
|
||||
// Start of all packed definitions for compiler without per-type packed
|
||||
TU_ATTR_PACKED_BEGIN
|
||||
TU_ATTR_BIT_FIELD_ORDER_BEGIN
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// TYPE-C
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
typedef enum {
|
||||
TUSB_TYPEC_PORT_SRC,
|
||||
TUSB_TYPEC_PORT_SNK,
|
||||
TUSB_TYPEC_PORT_DRP
|
||||
} tusb_typec_port_type_t;
|
||||
|
||||
enum {
|
||||
PD_CTRL_RESERVED = 0, // 0b00000: 0
|
||||
PD_CTRL_GOOD_CRC, // 0b00001: 1
|
||||
PD_CTRL_GO_TO_MIN, // 0b00010: 2
|
||||
PD_CTRL_ACCEPT, // 0b00011: 3
|
||||
PD_CTRL_REJECT, // 0b00100: 4
|
||||
PD_CTRL_PING, // 0b00101: 5
|
||||
PD_CTRL_PS_READY, // 0b00110: 6
|
||||
PD_CTRL_GET_SOURCE_CAP, // 0b00111: 7
|
||||
PD_CTRL_GET_SINK_CAP, // 0b01000: 8
|
||||
PD_CTRL_DR_SWAP, // 0b01001: 9
|
||||
PD_CTRL_PR_SWAP, // 0b01010: 10
|
||||
PD_CTRL_VCONN_SWAP, // 0b01011: 11
|
||||
PD_CTRL_WAIT, // 0b01100: 12
|
||||
PD_CTRL_SOFT_RESET, // 0b01101: 13
|
||||
PD_CTRL_DATA_RESET, // 0b01110: 14
|
||||
PD_CTRL_DATA_RESET_COMPLETE, // 0b01111: 15
|
||||
PD_CTRL_NOT_SUPPORTED, // 0b10000: 16
|
||||
PD_CTRL_GET_SOURCE_CAP_EXTENDED, // 0b10001: 17
|
||||
PD_CTRL_GET_STATUS, // 0b10010: 18
|
||||
PD_CTRL_FR_SWAP, // 0b10011: 19
|
||||
PD_CTRL_GET_PPS_STATUS, // 0b10100: 20
|
||||
PD_CTRL_GET_COUNTRY_CODES, // 0b10101: 21
|
||||
PD_CTRL_GET_SINK_CAP_EXTENDED, // 0b10110: 22
|
||||
PD_CTRL_GET_SOURCE_INFO, // 0b10111: 23
|
||||
PD_CTRL_REVISION, // 0b11000: 24
|
||||
};
|
||||
|
||||
enum {
|
||||
PD_DATA_RESERVED = 0, // 0b00000: 0
|
||||
PD_DATA_SOURCE_CAP, // 0b00001: 1
|
||||
PD_DATA_REQUEST, // 0b00010: 2
|
||||
PD_DATA_BIST, // 0b00011: 3
|
||||
PD_DATA_SINK_CAP, // 0b00100: 4
|
||||
PD_DATA_BATTERY_STATUS, // 0b00101: 5
|
||||
PD_DATA_ALERT, // 0b00110: 6
|
||||
PD_DATA_GET_COUNTRY_INFO, // 0b00111: 7
|
||||
PD_DATA_ENTER_USB, // 0b01000: 8
|
||||
PD_DATA_EPR_REQUEST, // 0b01001: 9
|
||||
PD_DATA_EPR_MODE, // 0b01010: 10
|
||||
PD_DATA_SRC_INFO, // 0b01011: 11
|
||||
PD_DATA_REVISION, // 0b01100: 12
|
||||
PD_DATA_RESERVED_13, // 0b01101: 13
|
||||
PD_DATA_RESERVED_14, // 0b01110: 14
|
||||
PD_DATA_VENDOR_DEFINED, // 0b01111: 15
|
||||
};
|
||||
|
||||
enum {
|
||||
PD_REV_10 = 0x0,
|
||||
PD_REV_20 = 0x1,
|
||||
PD_REV_30 = 0x2,
|
||||
};
|
||||
|
||||
enum {
|
||||
PD_DATA_ROLE_UFP = 0x0,
|
||||
PD_DATA_ROLE_DFP = 0x1,
|
||||
};
|
||||
|
||||
enum {
|
||||
PD_POWER_ROLE_SINK = 0x0,
|
||||
PD_POWER_ROLE_SOURCE = 0x1,
|
||||
};
|
||||
|
||||
typedef struct TU_ATTR_PACKED {
|
||||
uint16_t msg_type : 5; // [0:4]
|
||||
uint16_t data_role : 1; // [5] SOP only: 0 UFP, 1 DFP
|
||||
uint16_t specs_rev : 2; // [6:7]
|
||||
uint16_t power_role : 1; // [8] SOP only: 0 Sink, 1 Source
|
||||
uint16_t msg_id : 3; // [9:11]
|
||||
uint16_t n_data_obj : 3; // [12:14]
|
||||
uint16_t extended : 1; // [15]
|
||||
} pd_header_t;
|
||||
TU_VERIFY_STATIC(sizeof(pd_header_t) == 2, "size is not correct");
|
||||
|
||||
typedef struct TU_ATTR_PACKED {
|
||||
uint16_t data_size : 9; // [0:8]
|
||||
uint16_t reserved : 1; // [9]
|
||||
uint16_t request_chunk : 1; // [10]
|
||||
uint16_t chunk_number : 4; // [11:14]
|
||||
uint16_t chunked : 1; // [15]
|
||||
} pd_header_extended_t;
|
||||
TU_VERIFY_STATIC(sizeof(pd_header_extended_t) == 2, "size is not correct");
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Source Capability
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// All table references are from USBPD Specification rev3.1 version 1.8
|
||||
enum {
|
||||
PD_PDO_TYPE_FIXED = 0, // Vmin = Vmax
|
||||
PD_PDO_TYPE_BATTERY,
|
||||
PD_PDO_TYPE_VARIABLE, // non-battery
|
||||
PD_PDO_TYPE_APDO, // Augmented Power Data Object
|
||||
};
|
||||
|
||||
// Fixed Power Data Object (PDO) table 6-9
|
||||
typedef struct TU_ATTR_PACKED {
|
||||
uint32_t current_max_10ma : 10; // [9..0] Max current in 10mA unit
|
||||
uint32_t voltage_50mv : 10; // [19..10] Voltage in 50mV unit
|
||||
uint32_t current_peak : 2; // [21..20] Peak current
|
||||
uint32_t reserved : 1; // [22] Reserved
|
||||
uint32_t epr_mode_capable : 1; // [23] epr_mode_capable
|
||||
uint32_t unchunked_ext_msg_support : 1; // [24] UnChunked Extended Message Supported
|
||||
uint32_t dual_role_data : 1; // [25] Dual Role Data
|
||||
uint32_t usb_comm_capable : 1; // [26] USB Communications Capable
|
||||
uint32_t unconstrained_power : 1; // [27] Unconstrained Power
|
||||
uint32_t usb_suspend_supported : 1; // [28] USB Suspend Supported
|
||||
uint32_t dual_role_power : 1; // [29] Dual Role Power
|
||||
uint32_t type : 2; // [30] Fixed Supply type = PD_PDO_TYPE_FIXED
|
||||
} pd_pdo_fixed_t;
|
||||
TU_VERIFY_STATIC(sizeof(pd_pdo_fixed_t) == 4, "Invalid size");
|
||||
|
||||
// Battery Power Data Object (PDO) table 6-12
|
||||
typedef struct TU_ATTR_PACKED {
|
||||
uint32_t power_max_250mw : 10; // [9..0] Max allowable power in 250mW unit
|
||||
uint32_t voltage_min_50mv : 10; // [19..10] Minimum voltage in 50mV unit
|
||||
uint32_t voltage_max_50mv : 10; // [29..20] Maximum voltage in 50mV unit
|
||||
uint32_t type : 2; // [31..30] Battery type = PD_PDO_TYPE_BATTERY
|
||||
} pd_pdo_battery_t;
|
||||
TU_VERIFY_STATIC(sizeof(pd_pdo_battery_t) == 4, "Invalid size");
|
||||
|
||||
// Variable Power Data Object (PDO) table 6-11
|
||||
typedef struct TU_ATTR_PACKED {
|
||||
uint32_t current_max_10ma : 10; // [9..0] Max current in 10mA unit
|
||||
uint32_t voltage_min_50mv : 10; // [19..10] Minimum voltage in 50mV unit
|
||||
uint32_t voltage_max_50mv : 10; // [29..20] Maximum voltage in 50mV unit
|
||||
uint32_t type : 2; // [31..30] Variable Supply type = PD_PDO_TYPE_VARIABLE
|
||||
} pd_pdo_variable_t;
|
||||
TU_VERIFY_STATIC(sizeof(pd_pdo_variable_t) == 4, "Invalid size");
|
||||
|
||||
// Augmented Power Data Object (PDO) table 6-13
|
||||
typedef struct TU_ATTR_PACKED {
|
||||
uint32_t current_max_50ma : 7; // [6..0] Max current in 50mA unit
|
||||
uint32_t reserved1 : 1; // [7] Reserved
|
||||
uint32_t voltage_min_100mv : 8; // [15..8] Minimum Voltage in 100mV unit
|
||||
uint32_t reserved2 : 1; // [16] Reserved
|
||||
uint32_t voltage_max_100mv : 8; // [24..17] Maximum Voltage in 100mV unit
|
||||
uint32_t reserved3 : 2; // [26..25] Reserved
|
||||
uint32_t pps_power_limited : 1; // [27] PPS Power Limited
|
||||
uint32_t spr_programmable : 2; // [29..28] SPR Programmable Power Supply
|
||||
uint32_t type : 2; // [31..30] Augmented Power Data Object = PD_PDO_TYPE_APDO
|
||||
} pd_pdo_apdo_t;
|
||||
TU_VERIFY_STATIC(sizeof(pd_pdo_apdo_t) == 4, "Invalid size");
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Request
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
typedef struct TU_ATTR_PACKED {
|
||||
uint32_t current_extremum_10ma : 10; // [9..0] Max (give back = 0) or Min (give back = 1) current in 10mA unit
|
||||
uint32_t current_operate_10ma : 10; // [19..10] Operating current in 10mA unit
|
||||
uint32_t reserved : 2; // [21..20] Reserved
|
||||
uint32_t epr_mode_capable : 1; // [22] EPR mode capable
|
||||
uint32_t unchunked_ext_msg_support : 1; // [23] UnChunked Extended Message Supported
|
||||
uint32_t no_usb_suspend : 1; // [24] No USB Suspend
|
||||
uint32_t usb_comm_capable : 1; // [25] USB Communications Capable
|
||||
uint32_t capability_mismatch : 1; // [26] Capability Mismatch
|
||||
uint32_t give_back_flag : 1; // [27] GiveBack Flag: 0 = Max, 1 = Min
|
||||
uint32_t object_position : 4; // [31..28] Object Position
|
||||
} pd_rdo_fixed_variable_t;
|
||||
TU_VERIFY_STATIC(sizeof(pd_rdo_fixed_variable_t) == 4, "Invalid size");
|
||||
|
||||
typedef struct TU_ATTR_PACKED {
|
||||
uint32_t power_extremum_250mw : 10; // [9..0] Max (give back = 0) or Min (give back = 1) operating power in 250mW unit
|
||||
uint32_t power_operate_250mw : 10; // [19..10] Operating power in 250mW unit
|
||||
uint32_t reserved : 2; // [21..20] Reserved
|
||||
uint32_t epr_mode_capable : 1; // [22] EPR mode capable
|
||||
uint32_t unchunked_ext_msg_support : 1; // [23] UnChunked Extended Message Supported
|
||||
uint32_t no_usb_suspend : 1; // [24] No USB Suspend
|
||||
uint32_t usb_comm_capable : 1; // [25] USB Communications Capable
|
||||
uint32_t capability_mismatch : 1; // [26] Capability Mismatch
|
||||
uint32_t give_back_flag : 1; // [27] GiveBack Flag: 0 = Max, 1 = Min
|
||||
uint32_t object_position : 4; // [31..28] Object Position
|
||||
} pd_rdo_battery_t;
|
||||
TU_VERIFY_STATIC(sizeof(pd_rdo_battery_t) == 4, "Invalid size");
|
||||
|
||||
|
||||
TU_ATTR_PACKED_END // End of all packed definitions
|
||||
TU_ATTR_BIT_FIELD_ORDER_END
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -0,0 +1,143 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2023 Ha Thach (thach@tinyusb.org) for Adafruit Industries
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#ifndef _TUSB_TCD_H_
|
||||
#define _TUSB_TCD_H_
|
||||
|
||||
#include "common/tusb_common.h"
|
||||
#include "pd_types.h"
|
||||
|
||||
#include "osal/osal.h"
|
||||
#include "common/tusb_fifo.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
//
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
enum {
|
||||
TCD_EVENT_INVALID = 0,
|
||||
TCD_EVENT_CC_CHANGED,
|
||||
TCD_EVENT_RX_COMPLETE,
|
||||
TCD_EVENT_TX_COMPLETE,
|
||||
};
|
||||
|
||||
typedef struct TU_ATTR_PACKED {
|
||||
uint8_t rhport;
|
||||
uint8_t event_id;
|
||||
|
||||
union {
|
||||
struct {
|
||||
uint8_t cc_state[2];
|
||||
} cc_changed;
|
||||
|
||||
struct TU_ATTR_PACKED {
|
||||
uint16_t result : 2;
|
||||
uint16_t xferred_bytes : 14;
|
||||
} xfer_complete;
|
||||
};
|
||||
|
||||
} tcd_event_t;;
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
//
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Initialize controller
|
||||
bool tcd_init(uint8_t rhport, uint32_t port_type);
|
||||
|
||||
// Enable interrupt
|
||||
void tcd_int_enable (uint8_t rhport);
|
||||
|
||||
// Disable interrupt
|
||||
void tcd_int_disable(uint8_t rhport);
|
||||
|
||||
// Interrupt Handler
|
||||
void tcd_int_handler(uint8_t rhport);
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
//
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
bool tcd_msg_receive(uint8_t rhport, uint8_t* buffer, uint16_t total_bytes);
|
||||
bool tcd_msg_send(uint8_t rhport, uint8_t const* buffer, uint16_t total_bytes);
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Event API (implemented by stack)
|
||||
// Called by TCD to notify stack
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
extern void tcd_event_handler(tcd_event_t const * event, bool in_isr);
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline
|
||||
void tcd_event_cc_changed(uint8_t rhport, uint8_t cc1, uint8_t cc2, bool in_isr) {
|
||||
tcd_event_t event = {
|
||||
.rhport = rhport,
|
||||
.event_id = TCD_EVENT_CC_CHANGED,
|
||||
.cc_changed = {
|
||||
.cc_state = {cc1, cc2 }
|
||||
}
|
||||
};
|
||||
|
||||
tcd_event_handler(&event, in_isr);
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline
|
||||
void tcd_event_rx_complete(uint8_t rhport, uint16_t xferred_bytes, uint8_t result, bool in_isr) {
|
||||
tcd_event_t event = {
|
||||
.rhport = rhport,
|
||||
.event_id = TCD_EVENT_RX_COMPLETE,
|
||||
.xfer_complete = {
|
||||
.xferred_bytes = xferred_bytes,
|
||||
.result = result
|
||||
}
|
||||
};
|
||||
|
||||
tcd_event_handler(&event, in_isr);
|
||||
}
|
||||
|
||||
TU_ATTR_ALWAYS_INLINE static inline
|
||||
void tcd_event_tx_complete(uint8_t rhport, uint16_t xferred_bytes, uint8_t result, bool in_isr) {
|
||||
tcd_event_t event = {
|
||||
.rhport = rhport,
|
||||
.event_id = TCD_EVENT_TX_COMPLETE,
|
||||
.xfer_complete = {
|
||||
.xferred_bytes = xferred_bytes,
|
||||
.result = result
|
||||
}
|
||||
};
|
||||
|
||||
tcd_event_handler(&event, in_isr);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -0,0 +1,219 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2023 Ha Thach (thach@tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#include "tusb_option.h"
|
||||
|
||||
#if CFG_TUC_ENABLED
|
||||
|
||||
#include "tcd.h"
|
||||
#include "usbc.h"
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
//
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Debug level of USBD
|
||||
#define USBC_DEBUG 2
|
||||
#define TU_LOG_USBC(...) TU_LOG(USBC_DEBUG, __VA_ARGS__)
|
||||
|
||||
// Event queue
|
||||
// usbc_int_set() is used as mutex in OS NONE config
|
||||
void usbc_int_set(bool enabled);
|
||||
OSAL_QUEUE_DEF(usbc_int_set, _usbc_qdef, CFG_TUC_TASK_QUEUE_SZ, tcd_event_t);
|
||||
tu_static osal_queue_t _usbc_q;
|
||||
|
||||
// if stack is initialized
|
||||
static bool _usbc_inited = false;
|
||||
|
||||
// if port is initialized
|
||||
static bool _port_inited[TUP_TYPEC_RHPORTS_NUM];
|
||||
|
||||
// Max possible PD size is 262 bytes
|
||||
static uint8_t _rx_buf[64] TU_ATTR_ALIGNED(4);
|
||||
static uint8_t _tx_buf[64] TU_ATTR_ALIGNED(4);
|
||||
|
||||
bool usbc_msg_send(uint8_t rhport, pd_header_t const* header, void const* data);
|
||||
bool parse_msg_data(uint8_t rhport, pd_header_t const* header, uint8_t const* dobj, uint8_t const* p_end);
|
||||
bool parse_msg_control(uint8_t rhport, pd_header_t const* header);
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
//
|
||||
//--------------------------------------------------------------------+
|
||||
bool tuc_inited(uint8_t rhport) {
|
||||
return _usbc_inited && _port_inited[rhport];
|
||||
}
|
||||
|
||||
bool tuc_init(uint8_t rhport, uint32_t port_type) {
|
||||
// Initialize stack
|
||||
if (!_usbc_inited) {
|
||||
tu_memclr(_port_inited, sizeof(_port_inited));
|
||||
|
||||
_usbc_q = osal_queue_create(&_usbc_qdef);
|
||||
TU_ASSERT(_usbc_q != NULL);
|
||||
|
||||
_usbc_inited = true;
|
||||
}
|
||||
|
||||
// skip if port already initialized
|
||||
if ( _port_inited[rhport] ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
TU_LOG_USBC("USBC init on port %u\r\n", rhport);
|
||||
TU_LOG_INT(USBC_DEBUG, sizeof(tcd_event_t));
|
||||
|
||||
TU_ASSERT(tcd_init(rhport, port_type));
|
||||
tcd_int_enable(rhport);
|
||||
|
||||
_port_inited[rhport] = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void tuc_task_ext(uint32_t timeout_ms, bool in_isr) {
|
||||
(void) in_isr; // not implemented yet
|
||||
|
||||
// Skip if stack is not initialized
|
||||
if (!_usbc_inited) return;
|
||||
|
||||
// Loop until there is no more events in the queue
|
||||
while (1) {
|
||||
tcd_event_t event;
|
||||
if (!osal_queue_receive(_usbc_q, &event, timeout_ms)) return;
|
||||
|
||||
switch (event.event_id) {
|
||||
case TCD_EVENT_CC_CHANGED:
|
||||
break;
|
||||
|
||||
case TCD_EVENT_RX_COMPLETE:
|
||||
// TODO process message here in ISR, move to thread later
|
||||
if (event.xfer_complete.result == XFER_RESULT_SUCCESS) {
|
||||
pd_header_t const* header = (pd_header_t const*) _rx_buf;
|
||||
|
||||
if (header->n_data_obj == 0) {
|
||||
parse_msg_control(event.rhport, header);
|
||||
|
||||
}else {
|
||||
uint8_t const* p_end = _rx_buf + event.xfer_complete.xferred_bytes;
|
||||
uint8_t const * dobj = _rx_buf + sizeof(pd_header_t);
|
||||
|
||||
parse_msg_data(event.rhport, header, dobj, p_end);
|
||||
}
|
||||
}
|
||||
|
||||
// prepare for next message
|
||||
tcd_msg_receive(event.rhport, _rx_buf, sizeof(_rx_buf));
|
||||
break;
|
||||
|
||||
case TCD_EVENT_TX_COMPLETE:
|
||||
break;
|
||||
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool parse_msg_data(uint8_t rhport, pd_header_t const* header, uint8_t const* dobj, uint8_t const* p_end) {
|
||||
if (tuc_pd_data_received_cb) {
|
||||
tuc_pd_data_received_cb(rhport, header, dobj, p_end);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool parse_msg_control(uint8_t rhport, pd_header_t const* header) {
|
||||
if (tuc_pd_control_received_cb) {
|
||||
tuc_pd_control_received_cb(rhport, header);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
//
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
bool usbc_msg_send(uint8_t rhport, pd_header_t const* header, void const* data) {
|
||||
// copy header
|
||||
memcpy(_tx_buf, header, sizeof(pd_header_t));
|
||||
|
||||
// copy data objcet if available
|
||||
uint16_t const n_data_obj = header->n_data_obj;
|
||||
if (n_data_obj > 0) {
|
||||
memcpy(_tx_buf + sizeof(pd_header_t), data, n_data_obj * 4);
|
||||
}
|
||||
|
||||
return tcd_msg_send(rhport, _tx_buf, sizeof(pd_header_t) + n_data_obj * 4);
|
||||
}
|
||||
|
||||
bool tuc_msg_request(uint8_t rhport, void const* rdo) {
|
||||
pd_header_t const header = {
|
||||
.msg_type = PD_DATA_REQUEST,
|
||||
.data_role = PD_DATA_ROLE_UFP,
|
||||
.specs_rev = PD_REV_30,
|
||||
.power_role = PD_POWER_ROLE_SINK,
|
||||
.msg_id = 0,
|
||||
.n_data_obj = 1,
|
||||
.extended = 0,
|
||||
};
|
||||
|
||||
return usbc_msg_send(rhport, &header, rdo);
|
||||
}
|
||||
|
||||
void tcd_event_handler(tcd_event_t const * event, bool in_isr) {
|
||||
(void) in_isr;
|
||||
switch(event->event_id) {
|
||||
case TCD_EVENT_CC_CHANGED:
|
||||
if (event->cc_changed.cc_state[0] || event->cc_changed.cc_state[1]) {
|
||||
// Attach, start receiving
|
||||
tcd_msg_receive(event->rhport, _rx_buf, sizeof(_rx_buf));
|
||||
}else {
|
||||
// Detach
|
||||
}
|
||||
break;
|
||||
|
||||
default: break;
|
||||
}
|
||||
|
||||
osal_queue_send(_usbc_q, event, in_isr);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
//
|
||||
//--------------------------------------------------------------------+
|
||||
void usbc_int_set(bool enabled) {
|
||||
// Disable all controllers since they shared the same event queue
|
||||
for (uint8_t p = 0; p < TUP_TYPEC_RHPORTS_NUM; p++) {
|
||||
if ( _port_inited[p] ) {
|
||||
if (enabled) {
|
||||
tcd_int_enable(p);
|
||||
}else {
|
||||
tcd_int_disable(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
|
@ -0,0 +1,91 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2023 Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* This file is part of the TinyUSB stack.
|
||||
*/
|
||||
|
||||
#ifndef _TUSB_UTCD_H_
|
||||
#define _TUSB_UTCD_H_
|
||||
|
||||
#include "common/tusb_common.h"
|
||||
#include "pd_types.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// TypeC Configuration
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
#ifndef CFG_TUC_TASK_QUEUE_SZ
|
||||
#define CFG_TUC_TASK_QUEUE_SZ 8
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Application API
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Init typec stack on a port
|
||||
bool tuc_init(uint8_t rhport, uint32_t port_type);
|
||||
|
||||
// Check if typec port is initialized
|
||||
bool tuc_inited(uint8_t rhport);
|
||||
|
||||
// Task function should be called in main/rtos loop, extended version of tud_task()
|
||||
// - timeout_ms: millisecond to wait, zero = no wait, 0xFFFFFFFF = wait forever
|
||||
// - in_isr: if function is called in ISR
|
||||
void tuc_task_ext(uint32_t timeout_ms, bool in_isr);
|
||||
|
||||
// Task function should be called in main/rtos loop
|
||||
TU_ATTR_ALWAYS_INLINE static inline
|
||||
void tuc_task (void) {
|
||||
tuc_task_ext(UINT32_MAX, false);
|
||||
}
|
||||
|
||||
#ifndef _TUSB_TCD_H_
|
||||
extern void tcd_int_handler(uint8_t rhport);
|
||||
#endif
|
||||
|
||||
// Interrupt handler, name alias to TCD
|
||||
#define tuc_int_handler tcd_int_handler
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Callbacks
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
TU_ATTR_WEAK bool tuc_pd_data_received_cb(uint8_t rhport, pd_header_t const* header, uint8_t const* dobj, uint8_t const* p_end);
|
||||
TU_ATTR_WEAK bool tuc_pd_control_received_cb(uint8_t rhport, pd_header_t const* header);
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
//
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
bool tuc_msg_request(uint8_t rhport, void const* rdo);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -0,0 +1,93 @@
|
|||
# Pre-initialize the Raspberry Pi Pico SDK, setting up the platform and toolchain and some CMake utility functions
|
||||
# This file must be included prior to the project() call
|
||||
|
||||
# Note: this file is perhaps named badly, as it provides a method pico_sdk_init which
|
||||
# the enclosing project calls LATER to actually "initialize" the SDK (by including the CMakeLists.txt from this
|
||||
# same directory)
|
||||
|
||||
if (NOT TARGET _pico_sdk_pre_init_marker)
|
||||
add_library(_pico_sdk_pre_init_marker INTERFACE)
|
||||
|
||||
function(pico_is_top_level_project VAR)
|
||||
string(TOLOWER ${CMAKE_CURRENT_LIST_DIR} __list_dir)
|
||||
string(TOLOWER ${CMAKE_SOURCE_DIR} __source_dir)
|
||||
if (__source_dir STREQUAL __list_dir)
|
||||
set(${VAR} 1 PARENT_SCOPE)
|
||||
else()
|
||||
set(${VAR} 0 PARENT_SCOPE)
|
||||
endif()
|
||||
endfunction()
|
||||
|
||||
function(pico_message_debug MESSAGE)
|
||||
# The log-level system was added in CMake 3.15.
|
||||
if(${CMAKE_VERSION} VERSION_LESS "3.15.0")
|
||||
message(${MESSAGE})
|
||||
else()
|
||||
message(DEBUG ${MESSAGE})
|
||||
endif()
|
||||
endfunction()
|
||||
|
||||
if (NOT PICO_SDK_PATH)
|
||||
set(PICO_SDK_PATH ${CMAKE_CURRENT_LIST_DIR})
|
||||
endif ()
|
||||
|
||||
get_filename_component(PICO_SDK_PATH "${PICO_SDK_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}")
|
||||
|
||||
set(PICO_SDK_PATH ${CMAKE_CURRENT_LIST_DIR} CACHE PATH "Path to the Raspberry Pi Pico SDK" FORCE)
|
||||
|
||||
list(APPEND CMAKE_MODULE_PATH ${PICO_SDK_PATH}/cmake)
|
||||
|
||||
include(${CMAKE_CURRENT_LIST_DIR}/pico_sdk_version.cmake)
|
||||
include(pico_utils)
|
||||
|
||||
message("PICO_SDK_PATH is ${CMAKE_CURRENT_LIST_DIR}")
|
||||
|
||||
include(pico_pre_load_platform)
|
||||
|
||||
# We want to configure correct toolchain prior to project load
|
||||
# todo perhaps this should be included by the platform instead?
|
||||
include(pico_pre_load_toolchain)
|
||||
|
||||
macro(pico_sdk_init)
|
||||
if (NOT CMAKE_PROJECT_NAME)
|
||||
message(WARNING "pico_sdk_init() should be called after the project is created (and languages added)")
|
||||
endif()
|
||||
add_subdirectory(${PICO_SDK_PATH} pico-sdk)
|
||||
endmacro()
|
||||
|
||||
macro(add_sub_list_dirs var)
|
||||
foreach(LIST_DIR IN LISTS ${var})
|
||||
get_filename_component(SHORT_NAME "${LIST_DIR}" NAME)
|
||||
pico_message_debug("Including custom CMakeLists.txt ${SHORT_NAME}")
|
||||
add_subdirectory(${LIST_DIR} ${SHORT_NAME})
|
||||
endforeach()
|
||||
endmacro()
|
||||
|
||||
macro(add_sub_list_files var)
|
||||
foreach(LIST_FILE IN LISTS ${var})
|
||||
pico_message_debug("Including custom CMake file ${LIST_FILE}")
|
||||
include(${LIST_FILE})
|
||||
endforeach()
|
||||
endmacro()
|
||||
|
||||
macro(pico_register_common_scope_var NAME)
|
||||
if (NOT ${NAME} IN_LIST PICO_PROMOTE_COMMON_SCOPE_VARS)
|
||||
list(APPEND PICO_PROMOTE_COMMON_SCOPE_VARS ${NAME})
|
||||
endif()
|
||||
endmacro()
|
||||
|
||||
set(PICO_PROMOTE_COMMON_SCOPE_VARS
|
||||
PICO_INCLUDE_DIRS
|
||||
PICO_SDK_POST_LIST_DIRS
|
||||
PICO_SDK_POST_LIST_FILES
|
||||
PICO_CONFIG_HEADER_FILES
|
||||
PICO_RP2040_CONFIG_HEADER_FILES
|
||||
)
|
||||
|
||||
macro(pico_promote_common_scope_vars)
|
||||
set(PICO_PROMOTE_COMMON_SCOPE_VARS ${PICO_PROMOTE_COMMON_SCOPE_VARS} PARENT_SCOPE)
|
||||
foreach(VAR IN LISTS PICO_PROMOTE_COMMON_SCOPE_VARS)
|
||||
SET(${VAR} ${${VAR}} PARENT_SCOPE)
|
||||
endforeach()
|
||||
endmacro()
|
||||
endif()
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue