Skip to content

Commit 8e0b8c1

Browse files
authored
Merge pull request hathach#1369 from tannewt/host_string_desc
Add host string descriptor functions
2 parents 5453c7e + 56c2d4b commit 8e0b8c1

File tree

18 files changed

+483
-144
lines changed

18 files changed

+483
-144
lines changed

examples/host/bare_api/CMakeLists.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
3+
include(${CMAKE_CURRENT_SOURCE_DIR}/../../../hw/bsp/family_support.cmake)
4+
5+
# gets PROJECT name for the example
6+
family_get_project_name(PROJECT ${CMAKE_CURRENT_LIST_DIR})
7+
8+
project(${PROJECT})
9+
10+
# Checks this example is valid for the family and initializes the project
11+
family_initialize_project(${PROJECT} ${CMAKE_CURRENT_LIST_DIR})
12+
13+
add_executable(${PROJECT})
14+
15+
# Example source
16+
target_sources(${PROJECT} PUBLIC
17+
${CMAKE_CURRENT_SOURCE_DIR}/src/main.c
18+
)
19+
20+
# Example include
21+
target_include_directories(${PROJECT} PUBLIC
22+
${CMAKE_CURRENT_SOURCE_DIR}/src
23+
)
24+
25+
# Configure compilation flags and libraries for the example... see the corresponding function
26+
# in hw/bsp/FAMILY/family.cmake for details.
27+
family_configure_host_example(${PROJECT})

examples/host/bare_api/Makefile

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
include ../../../tools/top.mk
2+
include ../../make.mk
3+
4+
INC += \
5+
src \
6+
$(TOP)/hw \
7+
8+
# Example source
9+
EXAMPLE_SOURCE += \
10+
src/main.c
11+
12+
SRC_C += $(addprefix $(CURRENT_PATH)/, $(EXAMPLE_SOURCE))
13+
14+
# TODO: suppress warning caused by host stack
15+
CFLAGS += -Wno-error=cast-align -Wno-error=null-dereference
16+
17+
# TinyUSB Host Stack source
18+
SRC_C += \
19+
src/class/cdc/cdc_host.c \
20+
src/class/hid/hid_host.c \
21+
src/class/msc/msc_host.c \
22+
src/host/hub.c \
23+
src/host/usbh.c \
24+
src/host/usbh_control.c \
25+
src/portable/ohci/ohci.c \
26+
src/portable/nxp/lpc17_40/hcd_lpc17_40.c
27+
28+
include ../../rules.mk

examples/host/bare_api/only.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
mcu:LPC175X_6X
2+
mcu:LPC177X_8X
3+
mcu:LPC18XX
4+
mcu:LPC40XX
5+
mcu:LPC43XX
6+
mcu:MIMXRT10XX
7+
mcu:RP2040
8+
mcu:MSP432E4
9+
mcu:RX65X

examples/host/bare_api/src/main.c

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2019 Ha Thach (tinyusb.org)
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*
24+
*/
25+
26+
/* This example current worked and tested with following controller
27+
* - Sony DualShock 4 [CUH-ZCT2x] VID = 0x054c, PID = 0x09cc
28+
*/
29+
30+
31+
#include <stdlib.h>
32+
#include <stdio.h>
33+
#include <string.h>
34+
35+
#include "bsp/board.h"
36+
#include "tusb.h"
37+
38+
//--------------------------------------------------------------------+
39+
// MACRO CONSTANT TYPEDEF PROTYPES
40+
//--------------------------------------------------------------------+
41+
void led_blinking_task(void);
42+
43+
/*------------- MAIN -------------*/
44+
int main(void)
45+
{
46+
board_init();
47+
48+
printf("TinyUSB Host HID Controller Example\r\n");
49+
printf("Note: Events only displayed for explictly supported controllers\r\n");
50+
51+
tusb_init();
52+
53+
while (1)
54+
{
55+
// tinyusb host task
56+
tuh_task();
57+
led_blinking_task();
58+
}
59+
60+
return 0;
61+
}
62+
63+
//--------------------------------------------------------------------+
64+
// TinyUSB Callbacks
65+
//--------------------------------------------------------------------+
66+
67+
void print_device_descriptor(uint8_t dev_addr)
68+
{
69+
(void) dev_addr;
70+
printf("Device Descriptor:\r\n");
71+
}
72+
73+
// Invoked when device is mounted (configured)
74+
void tuh_mount_cb (uint8_t dev_addr)
75+
{
76+
printf("Device attached, address = %d\r\n", dev_addr);
77+
print_device_descriptor(dev_addr);
78+
}
79+
80+
/// Invoked when device is unmounted (bus reset/unplugged)
81+
void tuh_umount_cb(uint8_t dev_addr)
82+
{
83+
printf("Device removed, address = %d\r\n", dev_addr);
84+
}
85+
86+
//--------------------------------------------------------------------+
87+
// Blinking Task
88+
//--------------------------------------------------------------------+
89+
void led_blinking_task(void)
90+
{
91+
const uint32_t interval_ms = 1000;
92+
static uint32_t start_ms = 0;
93+
94+
static bool led_state = false;
95+
96+
// Blink every interval ms
97+
if ( board_millis() - start_ms < interval_ms) return; // not enough time
98+
start_ms += interval_ms;
99+
100+
board_led_write(led_state);
101+
led_state = 1 - led_state; // toggle
102+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2019 Ha Thach (tinyusb.org)
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*
24+
*/
25+
26+
#ifndef _TUSB_CONFIG_H_
27+
#define _TUSB_CONFIG_H_
28+
29+
#ifdef __cplusplus
30+
extern "C" {
31+
#endif
32+
33+
//--------------------------------------------------------------------
34+
// COMMON CONFIGURATION
35+
//--------------------------------------------------------------------
36+
37+
// defined by compiler flags for flexibility
38+
#ifndef CFG_TUSB_MCU
39+
#error CFG_TUSB_MCU must be defined
40+
#endif
41+
42+
#if CFG_TUSB_MCU == OPT_MCU_LPC43XX || CFG_TUSB_MCU == OPT_MCU_LPC18XX || CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX
43+
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_HOST | OPT_MODE_HIGH_SPEED)
44+
#else
45+
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_HOST
46+
#endif
47+
48+
#ifndef CFG_TUSB_OS
49+
#define CFG_TUSB_OS OPT_OS_NONE
50+
#endif
51+
52+
// CFG_TUSB_DEBUG is defined by compiler in DEBUG build
53+
// #define CFG_TUSB_DEBUG 0
54+
55+
/* USB DMA on some MCUs can only access a specific SRAM region with restriction on alignment.
56+
* Tinyusb use follows macros to declare transferring memory so that they can be put
57+
* into those specific section.
58+
* e.g
59+
* - CFG_TUSB_MEM SECTION : __attribute__ (( section(".usb_ram") ))
60+
* - CFG_TUSB_MEM_ALIGN : __attribute__ ((aligned(4)))
61+
*/
62+
#ifndef CFG_TUSB_MEM_SECTION
63+
#define CFG_TUSB_MEM_SECTION
64+
#endif
65+
66+
#ifndef CFG_TUSB_MEM_ALIGN
67+
#define CFG_TUSB_MEM_ALIGN __attribute__ ((aligned(4)))
68+
#endif
69+
70+
//--------------------------------------------------------------------
71+
// CONFIGURATION
72+
//--------------------------------------------------------------------
73+
74+
// Size of buffer to hold descriptors and other data used for enumeration
75+
#define CFG_TUH_ENUMERATION_BUFSIZE 256
76+
77+
// only hub class is enabled
78+
#define CFG_TUH_HUB 1
79+
80+
// max device support (excluding hub device)
81+
// 1 hub typically has 4 ports
82+
#define CFG_TUH_DEVICE_MAX (CFG_TUH_HUB ? 4 : 1)
83+
84+
#define CFG_TUH_ENDPOINT_MAX 8
85+
86+
//------------- HID -------------//
87+
88+
#define CFG_TUH_HID_EP_BUFSIZE 64
89+
90+
#ifdef __cplusplus
91+
}
92+
#endif
93+
94+
#endif /* _TUSB_CONFIG_H_ */

src/common/tusb_common.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#define TU_MIN(_x, _y) ( ( (_x) < (_y) ) ? (_x) : (_y) )
3939
#define TU_MAX(_x, _y) ( ( (_x) > (_y) ) ? (_x) : (_y) )
4040

41+
#define TU_U16(_high, _low) ((uint16_t) (((_high) << 8) | (_low)))
4142
#define TU_U16_HIGH(_u16) ((uint8_t) (((_u16) >> 8) & 0x00ff))
4243
#define TU_U16_LOW(_u16) ((uint8_t) ((_u16) & 0x00ff))
4344
#define U16_TO_U8S_BE(_u16) TU_U16_HIGH(_u16), TU_U16_LOW(_u16)
@@ -349,7 +350,7 @@ static inline const char* tu_lookup_find(tu_lookup_table_t const* p_table, uint3
349350
}
350351

351352
// not found return the key value in hex
352-
sprintf(not_found, "0x%08lX", (unsigned long) key);
353+
snprintf(not_found, sizeof(not_found), "0x%08lX", (unsigned long) key);
353354

354355
return not_found;
355356
}

src/device/usbd.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ bool tud_init (uint8_t rhport)
407407
if ( tud_inited() ) return true;
408408

409409
TU_LOG2("USBD init\r\n");
410+
TU_LOG2_INT(sizeof(usbd_device_t));
410411

411412
tu_varclr(&_usbd_dev);
412413

src/host/hcd.h

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@
3535
extern "C" {
3636
#endif
3737

38+
//--------------------------------------------------------------------+
39+
// Configuration
40+
//--------------------------------------------------------------------+
41+
42+
#ifndef CFG_TUH_ENDPOINT_MAX
43+
#define CFG_TUH_ENDPOINT_MAX (CFG_TUH_HUB + CFG_TUH_HID*2 + CFG_TUH_MSC*2 + CFG_TUH_CDC*3)
44+
// #ifdef TUP_HCD_ENDPOINT_MAX
45+
// #define CFG_TUH_ENDPPOINT_MAX TUP_HCD_ENDPOINT_MAX
46+
// #else
47+
// #define
48+
// #endif
49+
#endif
50+
3851
//--------------------------------------------------------------------+
3952
// MACRO CONSTANT TYPEDEF
4053
//--------------------------------------------------------------------+
@@ -81,26 +94,13 @@ typedef struct
8194

8295
} hcd_event_t;
8396

84-
#if CFG_TUH_ENABLED
85-
// Max number of endpoints per device
86-
enum {
87-
// TODO better computation
88-
HCD_MAX_ENDPOINT = CFG_TUH_DEVICE_MAX*(CFG_TUH_HUB + CFG_TUH_HID*2 + CFG_TUH_MSC*2 + CFG_TUH_CDC*3),
89-
HCD_MAX_XFER = HCD_MAX_ENDPOINT*2,
90-
};
91-
92-
//#define HCD_MAX_ENDPOINT 16
93-
//#define HCD_MAX_XFER 16
94-
9597
typedef struct {
9698
uint8_t rhport;
9799
uint8_t hub_addr;
98100
uint8_t hub_port;
99101
uint8_t speed;
100102
} hcd_devtree_info_t;
101103

102-
#endif
103-
104104
//--------------------------------------------------------------------+
105105
// Controller API
106106
//--------------------------------------------------------------------+

src/host/hub.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ static inline hub_interface_t* get_itf(uint8_t dev_addr)
5454
return &hub_data[dev_addr-1-CFG_TUH_DEVICE_MAX];
5555
}
5656

57-
#if CFG_TUSB_DEBUG
57+
#if CFG_TUSB_DEBUG >= 2
5858
static char const* const _hub_feature_str[] =
5959
{
6060
[HUB_FEATURE_PORT_CONNECTION ] = "PORT_CONNECTION",

0 commit comments

Comments
 (0)