diff options
| author | hathach <[email protected]> | 2013-06-27 16:19:22 +0700 |
|---|---|---|
| committer | hathach <[email protected]> | 2013-06-27 16:19:22 +0700 |
| commit | 3bca56665cb802e6e599fb4a1a2be5dd6e64b70b (patch) | |
| tree | ee67c6bac3050face4e8747b38317dfa2aba4927 /tinyusb | |
| parent | c81c4bb817925f385b45c46857b3f0630edb99ed (diff) | |
add mutex support for osal
add test for mutex in test_osal_none.c
implement usbh_control_xfer using mutex to get access to queue xfer on control pipe
(while semaphore is used to sync with hcd DMA)
failed to issue control xfer: set idle & get report descriptor in hidh_open_subtask (more to work on)
Diffstat (limited to 'tinyusb')
| -rw-r--r-- | tinyusb/class/hid_host.c | 19 | ||||
| -rw-r--r-- | tinyusb/common/errors.h | 1 | ||||
| -rw-r--r-- | tinyusb/host/usbh.c | 32 | ||||
| -rw-r--r-- | tinyusb/host/usbh_hcd.h | 12 | ||||
| -rw-r--r-- | tinyusb/osal/osal.h | 24 | ||||
| -rw-r--r-- | tinyusb/osal/osal_none.h | 49 |
6 files changed, 113 insertions, 24 deletions
diff --git a/tinyusb/class/hid_host.c b/tinyusb/class/hid_host.c index bdadc5666..5d780a819 100644 --- a/tinyusb/class/hid_host.c +++ b/tinyusb/class/hid_host.c @@ -197,6 +197,7 @@ void hidh_init(void) #endif } +//uint8_t report_descriptor[256] TUSB_CFG_ATTR_USBRAM; tusb_error_t hidh_open_subtask(uint8_t dev_addr, tusb_descriptor_interface_t const *p_interface_desc, uint16_t *p_length) { tusb_error_t error; @@ -215,6 +216,8 @@ tusb_error_t hidh_open_subtask(uint8_t dev_addr, tusb_descriptor_interface_t con OSAL_SUBTASK_BEGIN //------------- SET IDLE request -------------// + // TODO this request can be stalled by device (indicate not supported), + // until we have clear stall handler, temporarily disable it. // OSAL_SUBTASK_INVOKED_AND_WAIT( // usbh_control_xfer_subtask( // dev_addr, @@ -230,6 +233,22 @@ tusb_error_t hidh_open_subtask(uint8_t dev_addr, tusb_descriptor_interface_t con // ); //------------- TODO skip Get Report Descriptor -------------// +// memclr_(report_descriptor, 256); + +// OSAL_SUBTASK_INVOKED_AND_WAIT( +// usbh_control_xfer_subtask( +// dev_addr, +// &(tusb_std_request_t) +// { +// .bmRequestType = { .direction = TUSB_DIR_DEV_TO_HOST, .type = TUSB_REQUEST_TYPE_STANDARD, .recipient = TUSB_REQUEST_RECIPIENT_INTERFACE }, +// .bRequest = TUSB_REQUEST_GET_DESCRIPTOR, +// .wValue = HID_DESC_TYPE_REPORT, +// .wIndex = p_interface_desc->bInterfaceNumber, +// .wLength = p_desc_hid->wReportLength, +// }, +// report_descriptor ), +// error +// ); // uint8_t *p_report_desc = NULL; // report descriptor has to be global & in USB RAM if ( HID_SUBCLASS_BOOT == p_interface_desc->bInterfaceSubClass ) diff --git a/tinyusb/common/errors.h b/tinyusb/common/errors.h index 9017d99f4..dc9908bfd 100644 --- a/tinyusb/common/errors.h +++ b/tinyusb/common/errors.h @@ -74,6 +74,7 @@ ENTRY(TUSB_ERROR_OSAL_TASK_CREATE_FAILED )\ ENTRY(TUSB_ERROR_OSAL_QUEUE_FAILED )\ ENTRY(TUSB_ERROR_OSAL_SEMAPHORE_FAILED )\ + ENTRY(TUSB_ERROR_OSAL_MUTEX_FAILED )\ ENTRY(TUSB_ERROR_EHCI_NOT_ENOUGH_QTD )\ ENTRY(TUSB_ERROR_USBD_DESCRIPTOR_STRING )\ ENTRY(TUSB_ERROR_HIDD_DESCRIPTOR_INTERFACE )\ diff --git a/tinyusb/host/usbh.c b/tinyusb/host/usbh.c index 419b1a534..90ee096cd 100644 --- a/tinyusb/host/usbh.c +++ b/tinyusb/host/usbh.c @@ -144,23 +144,30 @@ tusb_error_t usbh_init(void) ASSERT_STATUS( hcd_init() ); - //------------- Semaphore for Control Pipe -------------// - for(uint8_t i=0; i<TUSB_CFG_HOST_DEVICE_MAX+1; i++) // including address zero - { - usbh_devices[i].control.sem_hdl = osal_semaphore_create( OSAL_SEM_REF(usbh_devices[i].control.semaphore) ); - ASSERT_PTR(usbh_devices[i].control.sem_hdl, TUSB_ERROR_OSAL_SEMAPHORE_FAILED); - } - //------------- Enumeration & Reporter Task init -------------// ASSERT_STATUS( osal_task_create(&enum_task_def) ); enum_queue_hdl = osal_queue_create(&enum_queue_def); ASSERT_PTR(enum_queue_hdl, TUSB_ERROR_OSAL_QUEUE_FAILED); + //------------- Semaphore, Mutex for Control Pipe -------------// + for(uint8_t i=0; i<TUSB_CFG_HOST_DEVICE_MAX+1; i++) // including address zero + { + usbh_device_info_t * const p_device = &usbh_devices[i]; + + p_device->control.sem_hdl = osal_semaphore_create( OSAL_SEM_REF(p_device->control.semaphore) ); + ASSERT_PTR(p_device->control.sem_hdl, TUSB_ERROR_OSAL_SEMAPHORE_FAILED); + + p_device->control.mutex_hdl = osal_mutex_create ( OSAL_SEM_REF(p_device->control.mutex) ); + ASSERT_PTR(p_device->control.mutex_hdl, TUSB_ERROR_OSAL_MUTEX_FAILED); + } + //------------- class init -------------// for (uint8_t class_index = 1; class_index < TUSB_CLASS_MAPPED_INDEX_END; class_index++) { if (usbh_class_drivers[class_index].init) + { usbh_class_drivers[class_index].init(); + } } return TUSB_ERROR_NONE; @@ -174,11 +181,17 @@ tusb_error_t usbh_control_xfer_subtask(uint8_t dev_addr, tusb_std_request_t cons OSAL_SUBTASK_BEGIN - usbh_devices[dev_addr].control.pipe_status = TUSB_INTERFACE_STATUS_BUSY; + osal_mutex_wait(usbh_devices[dev_addr].control.mutex_hdl, OSAL_TIMEOUT_NORMAL, &error); + SUBTASK_ASSERT_STATUS_WITH_HANDLER(error, osal_mutex_release(usbh_devices[dev_addr].control.mutex_hdl)); + usbh_devices[dev_addr].control.request = *p_request; - SUBTASK_ASSERT_STATUS( hcd_pipe_control_xfer(dev_addr, &usbh_devices[dev_addr].control.request, data) ); + /*SUBTASK_ASSERT_STATUS*/ (void) ( hcd_pipe_control_xfer(dev_addr, &usbh_devices[dev_addr].control.request, data) ); + usbh_devices[dev_addr].control.pipe_status = TUSB_INTERFACE_STATUS_BUSY; osal_semaphore_wait(usbh_devices[dev_addr].control.sem_hdl, OSAL_TIMEOUT_NORMAL, &error); // careful of local variable without static + + osal_mutex_release(usbh_devices[dev_addr].control.mutex_hdl); + // TODO make handler for this function general purpose SUBTASK_ASSERT_WITH_HANDLER(TUSB_ERROR_NONE == error && usbh_devices[dev_addr].control.pipe_status != TUSB_INTERFACE_STATUS_ERROR, tusbh_device_mount_failed_cb(TUSB_ERROR_USBH_MOUNT_DEVICE_NOT_RESPOND, NULL) ); @@ -190,6 +203,7 @@ tusb_error_t usbh_pipe_control_open(uint8_t dev_addr, uint8_t max_packet_size) A tusb_error_t usbh_pipe_control_open(uint8_t dev_addr, uint8_t max_packet_size) { osal_semaphore_reset( usbh_devices[dev_addr].control.sem_hdl ); + osal_mutex_reset( usbh_devices[dev_addr].control.mutex_hdl ); ASSERT_STATUS( hcd_pipe_control_open(dev_addr, max_packet_size) ); diff --git a/tinyusb/host/usbh_hcd.h b/tinyusb/host/usbh_hcd.h index fa676dfc9..fbb7379a7 100644 --- a/tinyusb/host/usbh_hcd.h +++ b/tinyusb/host/usbh_hcd.h @@ -92,15 +92,19 @@ typedef struct { // TODO internal structure, re-order members uint8_t interface_count; // bNumInterfaces alias //------------- device -------------// - volatile uint8_t state; // device state, value from enum tusbh_device_state_t - uint32_t flag_supported_class; // a bitmap of supported class + volatile uint8_t state; // device state, value from enum tusbh_device_state_t + uint32_t flag_supported_class; // a bitmap of supported class //------------- control pipe -------------// struct { volatile uint8_t pipe_status; tusb_std_request_t request; - OSAL_SEM_DEF(semaphore); // TODO move to semaphore pool - osal_semaphore_handle_t sem_hdl; + + OSAL_SEM_DEF(semaphore); // TODO move to semaphore pool ? + osal_semaphore_handle_t sem_hdl; // used to synchronize with HCD when control xfer complete + + OSAL_MUTEX_DEF(mutex); // TODO move to mutex pool ? + osal_mutex_handle_t mutex_hdl; // used to exclusively occupy control pipe } control; } usbh_device_info_t; diff --git a/tinyusb/osal/osal.h b/tinyusb/osal/osal.h index c28bc4790..f40900919 100644 --- a/tinyusb/osal/osal.h +++ b/tinyusb/osal/osal.h @@ -133,12 +133,28 @@ typedef osal_semaphore_t * osal_semaphore_handle_t; #define OSAL_SEM_REF(name)\ &name -osal_semaphore_handle_t osal_semaphore_create(osal_semaphore_t * const sem); -void osal_semaphore_wait(osal_semaphore_handle_t const sem_hdl, uint32_t msec, tusb_error_t *p_error); -tusb_error_t osal_semaphore_post(osal_semaphore_handle_t const sem_hdl); -void osal_semaphore_reset(osal_semaphore_handle_t const sem_hdl); +osal_semaphore_handle_t osal_semaphore_create(osal_semaphore_t * p_sem); +void osal_semaphore_wait(osal_semaphore_handle_t sem_hdl, uint32_t msec, tusb_error_t *p_error); +tusb_error_t osal_semaphore_post(osal_semaphore_handle_t sem_hdl); +void osal_semaphore_reset(osal_semaphore_handle_t sem_hdl); //--------------------------------------------------------------------+ +// MUTEX API (priority inheritance) +//--------------------------------------------------------------------+ +#define OSAL_MUTEX_DEF(name)\ + osal_mutex_t name + +#define OSAL_MUTEX_REF(name)\ + &name + +typedef osal_semaphore_t osal_mutex_t; +typedef osal_semaphore_handle_t osal_mutex_handle_t; + +osal_mutex_handle_t osal_mutex_create(osal_mutex_t * p_mutex); +void osal_mutex_wait(osal_mutex_handle_t mutex_hdl, uint32_t msec, tusb_error_t *p_error); +tusb_error_t osal_mutex_release(osal_mutex_handle_t mutex_hdl); +void osal_mutex_reset(osal_mutex_handle_t mutex_hdl); +//--------------------------------------------------------------------+ // QUEUE API //--------------------------------------------------------------------+ typedef struct{ diff --git a/tinyusb/osal/osal_none.h b/tinyusb/osal/osal_none.h index 936513d1b..5090d14f5 100644 --- a/tinyusb/osal/osal_none.h +++ b/tinyusb/osal/osal_none.h @@ -166,23 +166,23 @@ typedef osal_semaphore_t * osal_semaphore_handle_t; #define OSAL_SEM_REF(name)\ &name -static inline osal_semaphore_handle_t osal_semaphore_create(osal_semaphore_t * const p_sem) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE; -static inline osal_semaphore_handle_t osal_semaphore_create(osal_semaphore_t * const p_sem) +static inline osal_semaphore_handle_t osal_semaphore_create(osal_semaphore_t * p_sem) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE; +static inline osal_semaphore_handle_t osal_semaphore_create(osal_semaphore_t * p_sem) { - (*p_sem) = 0; + (*p_sem) = 0; // TODO consider to have initial count parameter return (osal_semaphore_handle_t) p_sem; } -static inline tusb_error_t osal_semaphore_post(osal_semaphore_handle_t const sem_hdl) ATTR_ALWAYS_INLINE; -static inline tusb_error_t osal_semaphore_post(osal_semaphore_handle_t const sem_hdl) +static inline tusb_error_t osal_semaphore_post(osal_semaphore_handle_t sem_hdl) ATTR_ALWAYS_INLINE; +static inline tusb_error_t osal_semaphore_post(osal_semaphore_handle_t sem_hdl) { (*sem_hdl)++; return TUSB_ERROR_NONE; } -static inline void osal_semaphore_reset(osal_semaphore_handle_t const sem_hdl) ATTR_ALWAYS_INLINE; -static inline void osal_semaphore_reset(osal_semaphore_handle_t const sem_hdl) +static inline void osal_semaphore_reset(osal_semaphore_handle_t sem_hdl) ATTR_ALWAYS_INLINE; +static inline void osal_semaphore_reset(osal_semaphore_handle_t sem_hdl) { (*sem_hdl) = 0; } @@ -203,6 +203,41 @@ static inline void osal_semaphore_reset(osal_semaphore_handle_t const sem_hdl) }while(0) //--------------------------------------------------------------------+ +// MUTEX API (priority inheritance) +//--------------------------------------------------------------------+ +typedef osal_semaphore_t osal_mutex_t; +typedef osal_semaphore_handle_t osal_mutex_handle_t; + +#define OSAL_MUTEX_DEF(name)\ + osal_mutex_t name + +#define OSAL_MUTEX_REF(name)\ + &name + +static inline osal_mutex_handle_t osal_mutex_create(osal_mutex_t * p_mutex) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE; +static inline osal_mutex_handle_t osal_mutex_create(osal_mutex_t * p_mutex) +{ + (*p_mutex) = 1; + return (osal_mutex_handle_t) p_mutex; +} + +static inline tusb_error_t osal_mutex_release(osal_mutex_handle_t mutex_hdl) ATTR_ALWAYS_INLINE; +static inline tusb_error_t osal_mutex_release(osal_mutex_handle_t mutex_hdl) +{ + (*mutex_hdl) = 1; // mutex is a binary semaphore + + return TUSB_ERROR_NONE; +} + +static inline void osal_mutex_reset(osal_mutex_handle_t mutex_hdl) ATTR_ALWAYS_INLINE; +static inline void osal_mutex_reset(osal_mutex_handle_t mutex_hdl) +{ + (*mutex_hdl) = 1; +} + +#define osal_mutex_wait osal_semaphore_wait + +//--------------------------------------------------------------------+ // QUEUE API //--------------------------------------------------------------------+ typedef struct{ |
