diff --git a/tests/test/host/test_usbd_host.c b/tests/test/host/test_usbd_host.c index 944dc40f1..fc59769f0 100644 --- a/tests/test/host/test_usbd_host.c +++ b/tests/test/host/test_usbd_host.c @@ -46,7 +46,7 @@ tusb_handle_device_t dev_hdl; void setUp(void) { dev_hdl = 0; - device_info_pool[dev_hdl].status = TUSB_DEVICE_STATUS_READY; + memset(device_info_pool, 0, TUSB_CFG_HOST_DEVICE_MAX*sizeof(usbh_device_info_t)); } void tearDown(void) @@ -133,10 +133,60 @@ void queue_recv_stub (osal_queue_handle_t const queue_hdl, uint32_t *p_data, uin (*p_error) = TUSB_ERROR_NONE; } +tusb_error_t pipe_control_stub(pipe_handle_t pipe_hdl, const tusb_std_request_t * const p_request, uint8_t data[], int num_call) +{ + tusb_descriptor_device_t dev_desc = + { + .bLength = sizeof(tusb_descriptor_device_t), + .bDescriptorType = TUSB_DESC_DEVICE, + .bcdUSB = 0x0200, + .bDeviceClass = 0x00, + .bDeviceSubClass = 0x00, + .bDeviceProtocol = 0x00, + + .bMaxPacketSize0 = 64, + + .idVendor = 0x1FC9, + .idProduct = 0x4000, + .bcdDevice = 0x0100, + + .iManufacturer = 0x01, + .iProduct = 0x02, + .iSerialNumber = 0x03, + + .bNumConfigurations = 0x02 + }; + + if (p_request->bRequest == TUSB_REQUEST_GET_DESCRIPTOR) + { + switch (p_request->wValue >> 8) + { + case TUSB_DESC_DEVICE: + memcpy(data, &dev_desc, p_request->wLength); + break; + + default: + TEST_FAIL(); + break; + } + } + + return TUSB_ERROR_NONE; +} + void test_enum_task_connect(void) { + pipe_handle_t pipe_addr0 = 12; + + + osal_queue_receive_StubWithCallback(queue_recv_stub); hcd_port_connect_status_ExpectAndReturn(enum_connect.core_id, true); + hcd_port_speed_ExpectAndReturn(enum_connect.core_id, TUSB_SPEED_FULL); + hcd_pipe_addr0_open_ExpectAndReturn(enum_connect.core_id, TUSB_SPEED_FULL, 0, 0, pipe_addr0); + + hcd_pipe_control_xfer_StubWithCallback(pipe_control_stub); +// hcd_pipe_control_open_ExpectAnd(1, ); usbh_enumeration_task(); } diff --git a/tests/test/test_osal_none.c b/tests/test/test_osal_none.c index 3a5d5b812..b49d9eae2 100644 --- a/tests/test/test_osal_none.c +++ b/tests/test/test_osal_none.c @@ -108,27 +108,25 @@ void test_queue_send(void) void sample_task_semaphore(void) { tusb_error_t error = TUSB_ERROR_NONE; - OSAL_TASK_LOOP - { - OSAL_TASK_LOOP_BEGIN - statements[0]++; + OSAL_TASK_LOOP_BEGIN - osal_semaphore_wait(sem_hdl, OSAL_TIMEOUT_WAIT_FOREVER, &error); - statements[1]++; + statements[0]++; - osal_semaphore_wait(sem_hdl, OSAL_TIMEOUT_WAIT_FOREVER, &error); - statements[2]++; + osal_semaphore_wait(sem_hdl, OSAL_TIMEOUT_WAIT_FOREVER, &error); + statements[1]++; - osal_semaphore_wait(sem_hdl, OSAL_TIMEOUT_WAIT_FOREVER, &error); - statements[3]++; + osal_semaphore_wait(sem_hdl, OSAL_TIMEOUT_WAIT_FOREVER, &error); + statements[2]++; - osal_semaphore_wait(sem_hdl, OSAL_TIMEOUT_NORMAL, &error); - statements[4]++; - TEST_ASSERT_EQUAL(TUSB_ERROR_OSAL_TIMEOUT, error); + osal_semaphore_wait(sem_hdl, OSAL_TIMEOUT_WAIT_FOREVER, &error); + statements[3]++; - OSAL_TASK_LOOP_END - } + osal_semaphore_wait(sem_hdl, OSAL_TIMEOUT_NORMAL, &error); + statements[4]++; + TEST_ASSERT_EQUAL(TUSB_ERROR_OSAL_TIMEOUT, error); + + OSAL_TASK_LOOP_END } void test_task_with_semaphore(void) @@ -168,30 +166,27 @@ void sample_task_with_queue(void) uint32_t data; tusb_error_t error; - OSAL_TASK_LOOP - { - OSAL_TASK_LOOP_BEGIN + OSAL_TASK_LOOP_BEGIN - statements[0]++; + statements[0]++; - osal_queue_receive(queue_hdl, &data, OSAL_TIMEOUT_WAIT_FOREVER, &error); - TEST_ASSERT_EQUAL(0x1111, data); - statements[1]++; + osal_queue_receive(queue_hdl, &data, OSAL_TIMEOUT_WAIT_FOREVER, &error); + TEST_ASSERT_EQUAL(0x1111, data); + statements[1]++; - osal_queue_receive(queue_hdl, &data, OSAL_TIMEOUT_WAIT_FOREVER, &error); - TEST_ASSERT_EQUAL(0x2222, data); - statements[2]++; + osal_queue_receive(queue_hdl, &data, OSAL_TIMEOUT_WAIT_FOREVER, &error); + TEST_ASSERT_EQUAL(0x2222, data); + statements[2]++; - osal_queue_receive(queue_hdl, &data, OSAL_TIMEOUT_WAIT_FOREVER, &error); - TEST_ASSERT_EQUAL(0x3333, data); - statements[3]++; + osal_queue_receive(queue_hdl, &data, OSAL_TIMEOUT_WAIT_FOREVER, &error); + TEST_ASSERT_EQUAL(0x3333, data); + statements[3]++; - osal_queue_receive(queue_hdl, &data, OSAL_TIMEOUT_NORMAL, &error); - statements[4]++; - TEST_ASSERT_EQUAL(TUSB_ERROR_OSAL_TIMEOUT, error); + osal_queue_receive(queue_hdl, &data, OSAL_TIMEOUT_NORMAL, &error); + statements[4]++; + TEST_ASSERT_EQUAL(TUSB_ERROR_OSAL_TIMEOUT, error); - OSAL_TASK_LOOP_END - } + OSAL_TASK_LOOP_END } void test_task_with_queue(void) diff --git a/tinyusb/common/assertion.h b/tinyusb/common/assertion.h index 4f3678a50..c507d3a2a 100644 --- a/tinyusb/common/assertion.h +++ b/tinyusb/common/assertion.h @@ -70,7 +70,7 @@ extern "C" //--------------------------------------------------------------------+ #define ASSERT_FILENAME __FILE__ #define ASSERT_FUNCTION __PRETTY_FUNCTION__ -#define ASSERT_STATEMENT(format, ...)\ +#define ASSERT_MESSAGE(format, ...)\ _PRINTF("Assert at %s: %s:%d: " format "\n", ASSERT_FILENAME, ASSERT_FUNCTION, __LINE__, __VA_ARGS__) #ifndef _TEST_ASSERT_ @@ -83,7 +83,7 @@ extern "C" do{\ setup_statement;\ if (!(condition)) {\ - ASSERT_STATEMENT(format, __VA_ARGS__);\ + ASSERT_MESSAGE(format, __VA_ARGS__);\ ASSERT_ERROR_HANDLE(error);\ }\ }while(0) diff --git a/tinyusb/common/common.h b/tinyusb/common/common.h index e6550e586..09a82a217 100644 --- a/tinyusb/common/common.h +++ b/tinyusb/common/common.h @@ -70,6 +70,7 @@ #include "hal/hal.h" #include "core/tusb_types.h" #include "core/std_descriptors.h" +#include "core/std_request.h" // TODO try to manipulate gcc cmd option instead #ifndef _TEST_ diff --git a/tinyusb/core/std_descriptors.h b/tinyusb/core/std_descriptors.h index db7ba4110..dc43bce76 100644 --- a/tinyusb/core/std_descriptors.h +++ b/tinyusb/core/std_descriptors.h @@ -54,6 +54,9 @@ #include "common/common.h" +//--------------------------------------------------------------------+ +// STANDARD DESCRIPTORS +//--------------------------------------------------------------------+ /// USB Standard Device Descriptor (section 9.6.1, table 9-8) typedef ATTR_PREPACKED struct ATTR_PACKED { uint8_t bLength ; ///< Size of this descriptor in bytes. diff --git a/tinyusb/core/std_request.h b/tinyusb/core/std_request.h new file mode 100644 index 000000000..7b95fed8d --- /dev/null +++ b/tinyusb/core/std_request.h @@ -0,0 +1,79 @@ +/* + * std_request.h + * + * Created on: Feb 1, 2013 + * Author: hathach + */ + +/* + * Software License Agreement (BSD License) + * Copyright (c) 2012, hathach (tinyusb.net) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT + * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT + * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * This file is part of the tiny usb stack. + */ + +/** \file + * \brief TBD + * + * \note TBD + */ + +/** \ingroup TBD + * \defgroup TBD + * \brief TBD + * + * @{ + */ + +#ifndef _TUSB_STD_REQUEST_H_ +#define _TUSB_STD_REQUEST_H_ + +#ifdef __cplusplus + extern "C" { +#endif + +#include "common/common.h" + +typedef ATTR_PREPACKED struct ATTR_PACKED { + struct { + uint8_t recipient : 5; /**< Recipient type. */ + uint8_t type : 2; /**< Request type. */ + uint8_t direction : 1; /**< Direction type. */ + } bmRequestType; + + uint8_t bRequest; + uint16_t wValue; + uint16_t wIndex; + uint16_t wLength; +} tusb_std_request_t; + +#ifdef __cplusplus + } +#endif + +#endif /* _TUSB_STD_REQUEST_H_ */ + +/** @} */ diff --git a/tinyusb/core/tusb_types.h b/tinyusb/core/tusb_types.h index 9550565d0..b683b35c9 100644 --- a/tinyusb/core/tusb_types.h +++ b/tinyusb/core/tusb_types.h @@ -68,6 +68,11 @@ typedef enum { TUSB_XFER_INTERRUPT }tusb_transfer_type_t; +typedef enum { + TUSB_DIR_HOST_TO_DEV = 0, + TUSB_DIR_DEV_TO_HOST = 1 +}tusb_direction_t; + /// TBD typedef enum { TUSB_PID_SETUP, @@ -106,6 +111,19 @@ typedef enum { TUSB_REQUEST_SYNCH_FRAME ///< 12 }tusb_std_request_code_t; +typedef enum { + TUSB_REQUEST_TYPE_STANDARD = 0, + TUSB_REQUEST_TYPE_CLASS, + TUSB_REQUEST_TYPE_VENDOR +} tusb_std_request_type_t; + +typedef enum { + TUSB_REQUEST_RECIPIENT_DEVICE =0, + TUSB_REQUEST_RECIPIENT_INTERFACE, + TUSB_REQUEST_RECIPIENT_ENDPOINT, + TUSB_REQUEST_RECIPIENT_OTHER +} tusb_std_request_recipient_t; + typedef enum { TUSB_CLASS_UNSPECIFIED = 0 , ///< 0 TUSB_CLASS_AUDIO = 1 , ///< 1 diff --git a/tinyusb/host/hcd.h b/tinyusb/host/hcd.h index 415bb184a..d62c9290c 100644 --- a/tinyusb/host/hcd.h +++ b/tinyusb/host/hcd.h @@ -56,7 +56,6 @@ #endif #include "common/common.h" -#include "core/tusb_types.h" typedef uint32_t pipe_handle_t; @@ -66,7 +65,11 @@ tusb_error_t hcd_init(uint8_t hostid) ATTR_WARN_UNUSED_RESULT; //--------------------------------------------------------------------+ // PIPE API //--------------------------------------------------------------------+ -//pipe_handle_t hcd_pipe_control_open(core_id, speed, hub_addr, hub_port, dev_addr, max_packet_size); +pipe_handle_t hcd_pipe_addr0_open(uint8_t core_id, tusb_speed_t speed, uint8_t hub_addr, uint8_t hub_port); + +pipe_handle_t hcd_pipe_control_open(uint8_t dev_addr, uint8_t max_packet_size); +tusb_error_t hcd_pipe_control_xfer(pipe_handle_t pipe_hdl, const tusb_std_request_t * const p_request, uint8_t data[]); +pipe_handle_t hcd_pipe_open(uint8_t dev_addr, tusb_descriptor_endpoint_t* endpoint_desc); #if 0 //tusb_error_t hcd_pipe_open( @@ -83,7 +86,7 @@ tusb_error_t hcd_pipe_cancel()ATTR_WARN_UNUSED_RESULT; //--------------------------------------------------------------------+ /// return the current connect status of roothub port bool hcd_port_connect_status(uint8_t core_id) ATTR_WARN_UNUSED_RESULT; -tusb_speed_t hcd_port_speed_get(uint8_t core_id) ATTR_WARN_UNUSED_RESULT; +tusb_speed_t hcd_port_speed(uint8_t core_id) ATTR_WARN_UNUSED_RESULT; #ifdef __cplusplus } diff --git a/tinyusb/host/usbd_host.c b/tinyusb/host/usbd_host.c index 657bb28be..b8445d2b8 100644 --- a/tinyusb/host/usbd_host.c +++ b/tinyusb/host/usbd_host.c @@ -56,6 +56,12 @@ //--------------------------------------------------------------------+ // INTERNAL OBJECT & FUNCTION DECLARATION //--------------------------------------------------------------------+ +struct { + tusb_speed_t speed; + uint8_t hub_addr; + uint8_t hub_port; +} usbh_device_addr0[TUSB_CFG_HOST_CONTROLLER_NUM]; + STATIC_ usbh_device_info_t device_info_pool[TUSB_CFG_HOST_DEVICE_MAX]; //--------------------------------------------------------------------+ @@ -81,31 +87,39 @@ void usbh_enumeration_task(void) usbh_enumerate_t enum_item; tusb_error_t error; - OSAL_TASK_LOOP + OSAL_TASK_LOOP_BEGIN + + osal_queue_receive(enum_queue_hdl, (uint32_t*)&enum_item, OSAL_TIMEOUT_NORMAL, &error); + TASK_ASSERT_STATUS(error); + + if (enum_item.hub_address == 0) // direct connection { - OSAL_TASK_LOOP_BEGIN - - osal_queue_receive(enum_queue_hdl, (uint32_t*)&enum_item, OSAL_TIMEOUT_NORMAL, &error); - if (error != TUSB_ERROR_NONE) + if ( enum_item.connect_status == hcd_port_connect_status(enum_item.core_id) ) // there chances the event is out-dated { - ASSERT_STATEMENT("%s", TUSB_ErrorStr[error]); - } - else - { - if (enum_item.hub_address == 0) // direct connection + tusb_std_request_t request_dev_desc = { - if ( enum_item.connect_status == hcd_port_connect_status(enum_item.core_id) ) // there chances the event is out-dated - { + .bmRequestType = + { + .direction = TUSB_DIR_DEV_TO_HOST, + .type = TUSB_REQUEST_TYPE_STANDARD, + .recipient = TUSB_REQUEST_RECIPIENT_DEVICE + }, - } - }else // device connect via a hub - { - ASSERT_STATEMENT("%s", "Hub is not supported yet"); - } + .bRequest = TUSB_REQUEST_GET_DESCRIPTOR, + .wValue = (TUSB_DESC_DEVICE << 8), + .wLength = 8 + }; + tusb_speed_t speed = hcd_port_speed(enum_item.core_id); + pipe_handle_t pipe_addr0 = hcd_pipe_addr0_open(enum_item.core_id, speed, enum_item.hub_address, enum_item.hub_port); + + // hcd_pipe_control_xfer(pipe_addr0, &request_dev_desc) } - - OSAL_TASK_LOOP_END + }else // device connect via a hub + { + ASSERT_MESSAGE("%s", "Hub is not supported yet"); } + + OSAL_TASK_LOOP_END } //--------------------------------------------------------------------+ diff --git a/tinyusb/host/usbd_host.h b/tinyusb/host/usbd_host.h index 73a40f16b..9c02378dd 100644 --- a/tinyusb/host/usbd_host.h +++ b/tinyusb/host/usbd_host.h @@ -110,16 +110,21 @@ enum { typedef uint8_t tusbh_device_status_t; typedef uint32_t tusbh_flag_class_t; -typedef struct { // TODO internal structure +typedef struct { // TODO internal structure, re-order members uint8_t core_id; - tusbh_device_status_t status; - pipe_handle_t pipe_control; + tusb_speed_t speed; + uint8_t hub_addr; + uint8_t hub_port; -#if 0 // TODO allow configure for vendor/product uint16_t vendor_id; uint16_t product_id; - uint8_t configure_count; + + tusbh_device_status_t status; + pipe_handle_t pipe_control; + tusb_std_request_t request_control; + +#if 0 // TODO allow configure for vendor/product struct { uint8_t interface_count; uint8_t attributes; diff --git a/tinyusb/osal/osal.h b/tinyusb/osal/osal.h index 298a54f3e..707a74bff 100644 --- a/tinyusb/osal/osal.h +++ b/tinyusb/osal/osal.h @@ -82,9 +82,12 @@ typedef uint32_t osal_task_t; #define OSAL_TASK_DEF(name, code, stack_depth, prio) \ osal_task_t name -#define OSAL_TASK_LOOP #define OSAL_TASK_LOOP_BEGIN #define OSAL_TASK_LOOP_END +#define TASK_ASSERT_STATUS(sts) \ + ASSERT_DEFINE(tusb_error_t status = (tusb_error_t)(sts),\ + TUSB_ERROR_NONE == status, (void) 0, "%s", TUSB_ErrorStr[status]) + tusb_error_t osal_task_create(osal_task_t *task); diff --git a/tinyusb/osal/osal_none.h b/tinyusb/osal/osal_none.h index 56277d90c..682f60efa 100644 --- a/tinyusb/osal/osal_none.h +++ b/tinyusb/osal/osal_none.h @@ -79,18 +79,31 @@ uint32_t osal_tick_get(void); //--------------------------------------------------------------------+ //#define osal_task_create(code, name, stack_depth, parameters, prio) -#define OSAL_TASK_LOOP \ +#define OSAL_TASK_LOOP_BEGIN \ static uint32_t timeout = 0;\ static uint16_t state = 0;\ - switch(state)\ - -#define OSAL_TASK_LOOP_BEGIN \ - case 0: + switch(state) {\ + case 0:\ #define OSAL_TASK_LOOP_END \ default:\ - state = 0; + state = 0;\ + } +#define TASK_ASSERT_DEFINE(setup_statement, condition, error, format, ...) \ + do{\ + setup_statement;\ + if (!(condition)) {\ + ASSERT_MESSAGE(format, __VA_ARGS__);\ + state = 0; /* reset task loop */\ + break;\ + }\ + }while(0) + +#define TASK_ASSERT(condition, error) TASK_ASSERT_DEFINE( , (condition), error, "%s", "evaluated to false") +#define TASK_ASSERT_STATUS(sts) \ + TASK_ASSERT_DEFINE(tusb_error_t status = (tusb_error_t)(sts),\ + TUSB_ERROR_NONE == status, status, "%s", TUSB_ErrorStr[status]) //--------------------------------------------------------------------+ // Semaphore API //--------------------------------------------------------------------+