add tusb_time_millis_api() and default/weak tusb_time_delay_ms_api(). Which is required for host and some device without RTOS.

This commit is contained in:
hathach
2024-11-04 17:20:58 +07:00
parent 5d6d905cb0
commit 80ad7c4e87
6 changed files with 43 additions and 15 deletions

View File

@@ -24,9 +24,8 @@
* This file is part of the TinyUSB stack. * This file is part of the TinyUSB stack.
*/ */
#ifndef TUSB_PRIVATE_H_
#ifndef _TUSB_PRIVATE_H_ #define TUSB_PRIVATE_H_
#define _TUSB_PRIVATE_H_
// Internal Helper used by Host and Device Stack // Internal Helper used by Host and Device Stack
@@ -34,6 +33,13 @@
extern "C" { extern "C" {
#endif #endif
#define TUP_USBIP_CONTROLLER_NUM 2
extern tusb_role_t _tusb_rhport_role[TUP_USBIP_CONTROLLER_NUM];
//--------------------------------------------------------------------+
// Endpoint
//--------------------------------------------------------------------+
typedef struct TU_ATTR_PACKED { typedef struct TU_ATTR_PACKED {
volatile uint8_t busy : 1; volatile uint8_t busy : 1;
volatile uint8_t stalled : 1; volatile uint8_t stalled : 1;
@@ -163,4 +169,4 @@ bool tu_edpt_stream_peek(tu_edpt_stream_t* s, uint8_t* ch) {
} }
#endif #endif
#endif /* _TUSB_PRIVATE_H_ */ #endif

View File

@@ -264,7 +264,7 @@ typedef enum {
} microsoft_os_20_type_t; } microsoft_os_20_type_t;
enum { enum {
CONTROL_STAGE_IDLE, CONTROL_STAGE_IDLE = 0,
CONTROL_STAGE_SETUP, CONTROL_STAGE_SETUP,
CONTROL_STAGE_DATA, CONTROL_STAGE_DATA,
CONTROL_STAGE_ACK CONTROL_STAGE_ACK

View File

@@ -39,14 +39,25 @@
#include "host/usbh_pvt.h" #include "host/usbh_pvt.h"
#endif #endif
#define TUP_USBIP_CONTROLLER_NUM 2 tusb_role_t _tusb_rhport_role[TUP_USBIP_CONTROLLER_NUM] = { 0 };
static tusb_role_t _rhport_role[TUP_USBIP_CONTROLLER_NUM] = { 0 }; //--------------------------------------------------------------------
// Weak/Default API, can be overwritten by Application
//--------------------------------------------------------------------
TU_ATTR_WEAK void tusb_time_delay_ms_api(uint32_t ms) {
#if CFG_TUSB_OS != OPT_OS_NONE
osal_task_delay(ms);
#else
// delay using millis() (if implemented) and/or frame number if possible
const uint32_t time_ms = tusb_time_millis_api();
while ((tusb_time_millis_api() - time_ms) < ms) {}
#endif
}
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// Public API // Public API
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
bool tusb_rhport_init(uint8_t rhport, const tusb_rhport_init_t* rh_init) { bool tusb_rhport_init(uint8_t rhport, const tusb_rhport_init_t* rh_init) {
// backward compatible called with tusb_init(void) // backward compatible called with tusb_init(void)
#if defined(TUD_OPT_RHPORT) || defined(TUH_OPT_RHPORT) #if defined(TUD_OPT_RHPORT) || defined(TUH_OPT_RHPORT)
@@ -58,7 +69,7 @@ bool tusb_rhport_init(uint8_t rhport, const tusb_rhport_init_t* rh_init) {
.speed = TUD_OPT_HIGH_SPEED ? TUSB_SPEED_HIGH : TUSB_SPEED_FULL .speed = TUD_OPT_HIGH_SPEED ? TUSB_SPEED_HIGH : TUSB_SPEED_FULL
}; };
TU_ASSERT ( tud_rhport_init(rhport, &dev_init) ); TU_ASSERT ( tud_rhport_init(rhport, &dev_init) );
_rhport_role[TUD_OPT_RHPORT] = TUSB_ROLE_DEVICE; _tusb_rhport_role[TUD_OPT_RHPORT] = TUSB_ROLE_DEVICE;
#endif #endif
#if CFG_TUH_ENABLED && defined(TUH_OPT_RHPORT) #if CFG_TUH_ENABLED && defined(TUH_OPT_RHPORT)
@@ -68,7 +79,7 @@ bool tusb_rhport_init(uint8_t rhport, const tusb_rhport_init_t* rh_init) {
.speed = TUH_OPT_HIGH_SPEED ? TUSB_SPEED_HIGH : TUSB_SPEED_FULL .speed = TUH_OPT_HIGH_SPEED ? TUSB_SPEED_HIGH : TUSB_SPEED_FULL
}; };
TU_ASSERT( tuh_rhport_init(TUH_OPT_RHPORT, &host_init) ); TU_ASSERT( tuh_rhport_init(TUH_OPT_RHPORT, &host_init) );
_rhport_role[TUH_OPT_RHPORT] = TUSB_ROLE_HOST; _tusb_rhport_role[TUH_OPT_RHPORT] = TUSB_ROLE_HOST;
#endif #endif
return true; return true;
@@ -77,7 +88,7 @@ bool tusb_rhport_init(uint8_t rhport, const tusb_rhport_init_t* rh_init) {
// new API with explicit rhport and role // new API with explicit rhport and role
TU_ASSERT(rhport < TUP_USBIP_CONTROLLER_NUM && rh_init->role != TUSB_ROLE_INVALID); TU_ASSERT(rhport < TUP_USBIP_CONTROLLER_NUM && rh_init->role != TUSB_ROLE_INVALID);
_rhport_role[rhport] = rh_init->role; _tusb_rhport_role[rhport] = rh_init->role;
#if CFG_TUD_ENABLED #if CFG_TUD_ENABLED
if (rh_init->role == TUSB_ROLE_DEVICE) { if (rh_init->role == TUSB_ROLE_DEVICE) {
@@ -112,14 +123,14 @@ void tusb_int_handler(uint8_t rhport, bool in_isr) {
TU_VERIFY(rhport < TUP_USBIP_CONTROLLER_NUM,); TU_VERIFY(rhport < TUP_USBIP_CONTROLLER_NUM,);
#if CFG_TUD_ENABLED #if CFG_TUD_ENABLED
if (_rhport_role[rhport] == TUSB_ROLE_DEVICE) { if (_tusb_rhport_role[rhport] == TUSB_ROLE_DEVICE) {
(void) in_isr; (void) in_isr;
dcd_int_handler(rhport); dcd_int_handler(rhport);
} }
#endif #endif
#if CFG_TUH_ENABLED #if CFG_TUH_ENABLED
if (_rhport_role[rhport] == TUSB_ROLE_HOST) { if (_tusb_rhport_role[rhport] == TUSB_ROLE_HOST) {
hcd_int_handler(rhport, in_isr); hcd_int_handler(rhport, in_isr);
} }
#endif #endif

View File

@@ -169,8 +169,11 @@ void tusb_int_handler(uint8_t rhport, bool in_isr);
// API Implemented by user // API Implemented by user
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// Get current milliseconds, maybe required by some port with no RTOS // Get current milliseconds, required by some port/configuration without RTOS
uint32_t tusb_time_millis(void); uint32_t tusb_time_millis_api(void);
// Delay in milliseconds, use tusb_time_millis_api() by default. required by some port/configuration with no RTOS
void tusb_time_delay_ms_api(uint32_t ms);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@@ -42,6 +42,10 @@ TEST_FILE("msc_device.c")
// MACRO TYPEDEF CONSTANT ENUM DECLARATION // MACRO TYPEDEF CONSTANT ENUM DECLARATION
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
uint32_t tusb_time_millis_api(void) {
return 0;
}
enum enum
{ {
EDPT_CTRL_OUT = 0x00, EDPT_CTRL_OUT = 0x00,

View File

@@ -39,6 +39,10 @@ TEST_FILE("usbd_control.c")
// MACRO TYPEDEF CONSTANT ENUM DECLARATION // MACRO TYPEDEF CONSTANT ENUM DECLARATION
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
uint32_t tusb_time_millis_api(void) {
return 0;
}
enum enum
{ {
EDPT_CTRL_OUT = 0x00, EDPT_CTRL_OUT = 0x00,