Skip to content

Commit a708ab6

Browse files
authored
Merge pull request #553 from hathach/host-async-control
Host async control
2 parents 075334a + 2907b1e commit a708ab6

File tree

27 files changed

+1636
-926
lines changed

27 files changed

+1636
-926
lines changed

examples/host/cdc_msc_hid/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ SRC_C += \
1616
src/class/cdc/cdc_host.c \
1717
src/class/hid/hid_host.c \
1818
src/class/msc/msc_host.c \
19-
src/host/usbh.c \
2019
src/host/hub.c \
20+
src/host/usbh.c \
21+
src/host/usbh_control.c \
2122
src/host/ehci/ehci.c \
2223
src/host/ohci/ohci.c \
2324
src/portable/nxp/lpc18_43/hcd_lpc18_43.c \

examples/host/cdc_msc_hid/ses/lpc18xx/lpc18xx.emProject

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
debug_target_connection="J-Link"
2424
gcc_entry_point="Reset_Handler"
2525
linker_memory_map_file="$(ProjectDir)/LPC1857_MemoryMap.xml"
26+
linker_printf_width_precision_supported="Yes"
2627
linker_section_placement_file="$(ProjectDir)/flash_placement.xml"
2728
macros="DeviceFamily=LPC1800;DeviceSubFamily=LPC185x;Target=LPC1857;Placement=Flash;rootDir=../../../../..;lpcDir=../../../../../hw/mcu/nxp/lpcopen/lpc18xx/lpc_chip_18xx"
2829
package_dependencies="LPC1800"

examples/host/cdc_msc_hid/src/main.c

Lines changed: 82 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ void cdc_task(void)
111111
//--------------------------------------------------------------------+
112112
#if CFG_TUH_HID_KEYBOARD
113113

114+
CFG_TUSB_MEM_SECTION static hid_keyboard_report_t usb_keyboard_report;
114115
uint8_t const keycode2ascii[128][2] = { HID_KEYCODE_TO_ASCII };
115116

116117
// look up new key in previous keys
@@ -153,21 +154,6 @@ static inline void process_kbd_report(hid_keyboard_report_t const *p_new_report)
153154
prev_report = *p_new_report;
154155
}
155156

156-
CFG_TUSB_MEM_SECTION static hid_keyboard_report_t usb_keyboard_report;
157-
158-
void hid_task(void)
159-
{
160-
uint8_t const addr = 1;
161-
if ( tuh_hid_keyboard_is_mounted(addr) )
162-
{
163-
if ( !tuh_hid_keyboard_is_busy(addr) )
164-
{
165-
process_kbd_report(&usb_keyboard_report);
166-
tuh_hid_keyboard_get_report(addr, &usb_keyboard_report);
167-
}
168-
}
169-
}
170-
171157
void tuh_hid_keyboard_mounted_cb(uint8_t dev_addr)
172158
{
173159
// application set-up
@@ -192,6 +178,58 @@ void tuh_hid_keyboard_isr(uint8_t dev_addr, xfer_result_t event)
192178
#endif
193179

194180
#if CFG_TUH_HID_MOUSE
181+
182+
CFG_TUSB_MEM_SECTION static hid_mouse_report_t usb_mouse_report;
183+
184+
void cursor_movement(int8_t x, int8_t y, int8_t wheel)
185+
{
186+
//------------- X -------------//
187+
if ( x < 0)
188+
{
189+
printf(ANSI_CURSOR_BACKWARD(%d), (-x)); // move left
190+
}else if ( x > 0)
191+
{
192+
printf(ANSI_CURSOR_FORWARD(%d), x); // move right
193+
}else { }
194+
195+
//------------- Y -------------//
196+
if ( y < 0)
197+
{
198+
printf(ANSI_CURSOR_UP(%d), (-y)); // move up
199+
}else if ( y > 0)
200+
{
201+
printf(ANSI_CURSOR_DOWN(%d), y); // move down
202+
}else { }
203+
204+
//------------- wheel -------------//
205+
if (wheel < 0)
206+
{
207+
printf(ANSI_SCROLL_UP(%d), (-wheel)); // scroll up
208+
}else if (wheel > 0)
209+
{
210+
printf(ANSI_SCROLL_DOWN(%d), wheel); // scroll down
211+
}else { }
212+
}
213+
214+
static inline void process_mouse_report(hid_mouse_report_t const * p_report)
215+
{
216+
static hid_mouse_report_t prev_report = { 0 };
217+
218+
//------------- button state -------------//
219+
uint8_t button_changed_mask = p_report->buttons ^ prev_report.buttons;
220+
if ( button_changed_mask & p_report->buttons)
221+
{
222+
printf(" %c%c%c ",
223+
p_report->buttons & MOUSE_BUTTON_LEFT ? 'L' : '-',
224+
p_report->buttons & MOUSE_BUTTON_MIDDLE ? 'M' : '-',
225+
p_report->buttons & MOUSE_BUTTON_RIGHT ? 'R' : '-');
226+
}
227+
228+
//------------- cursor movement -------------//
229+
cursor_movement(p_report->x, p_report->y, p_report->wheel);
230+
}
231+
232+
195233
void tuh_hid_mouse_mounted_cb(uint8_t dev_addr)
196234
{
197235
// application set-up
@@ -212,6 +250,35 @@ void tuh_hid_mouse_isr(uint8_t dev_addr, xfer_result_t event)
212250
}
213251
#endif
214252

253+
254+
255+
void hid_task(void)
256+
{
257+
uint8_t const addr = 1;
258+
259+
#if CFG_TUH_HID_KEYBOARD
260+
if ( tuh_hid_keyboard_is_mounted(addr) )
261+
{
262+
if ( !tuh_hid_keyboard_is_busy(addr) )
263+
{
264+
process_kbd_report(&usb_keyboard_report);
265+
tuh_hid_keyboard_get_report(addr, &usb_mouse_report);
266+
}
267+
}
268+
#endif
269+
270+
#if CFG_TUH_HID_MOUSE
271+
if ( tuh_hid_mouse_is_mounted(addr) )
272+
{
273+
if ( !tuh_hid_mouse_is_busy(addr) )
274+
{
275+
process_mouse_report(&usb_mouse_report);
276+
tuh_hid_mouse_get_report(addr, &usb_mouse_report);
277+
}
278+
}
279+
#endif
280+
}
281+
215282
//--------------------------------------------------------------------+
216283
// tinyusb callbacks
217284
//--------------------------------------------------------------------+

examples/host/cdc_msc_hid/src/msc_app.c

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,59 @@
3030
//--------------------------------------------------------------------+
3131
// MACRO TYPEDEF CONSTANT ENUM DECLARATION
3232
//--------------------------------------------------------------------+
33+
static scsi_inquiry_resp_t inquiry_resp;
34+
static scsi_read_capacity10_resp_t capacity_resp;
3335

36+
uint32_t block_size;
37+
uint32_t block_count;
38+
39+
bool capacity_complete_cb(uint8_t dev_addr, msc_cbw_t const* cbw, msc_csw_t const* csw)
40+
{
41+
(void) dev_addr;
42+
(void) cbw;
43+
44+
if (csw->status != 0)
45+
{
46+
printf("Read Capacity (10) failed\r\n");
47+
return false;
48+
}
49+
50+
// Capacity response field: Block size and Last LBA are both Big-Endian
51+
block_count = tu_ntohl(capacity_resp.last_lba) + 1;
52+
block_size = tu_ntohl(capacity_resp.block_size);
53+
54+
printf("Disk Size: %lu MB\r\n", block_count / ((1024*1024)/block_size));
55+
printf("Block Count = %lu, Block Size: %lu\r\n", block_count, block_size);
56+
57+
return true;
58+
}
59+
60+
bool inquiry_complete_cb(uint8_t dev_addr, msc_cbw_t const* cbw, msc_csw_t const* csw)
61+
{
62+
if (csw->status != 0)
63+
{
64+
printf("Inquiry failed\r\n");
65+
return false;
66+
}
67+
68+
// Print out Vendor ID, Product ID and Rev
69+
printf("%.8s %.16s rev %.4s\r\n", inquiry_resp.vendor_id, inquiry_resp.product_id, inquiry_resp.product_rev);
70+
71+
// Read capacity of device
72+
tuh_msc_read_capacity(dev_addr, cbw->lun, &capacity_resp, capacity_complete_cb);
73+
74+
return true;
75+
}
3476

3577
//------------- IMPLEMENTATION -------------//
3678
void tuh_msc_mounted_cb(uint8_t dev_addr)
3779
{
3880
printf("A MassStorage device is mounted\r\n");
3981

40-
//------------- Disk Information -------------//
41-
// SCSI VendorID[8] & ProductID[16] from Inquiry Command
42-
uint8_t const* p_vendor = tuh_msc_get_vendor_name(dev_addr);
43-
uint8_t const* p_product = tuh_msc_get_product_name(dev_addr);
44-
45-
for(uint8_t i=0; i<8; i++) putchar(p_vendor[i]);
82+
block_size = block_count = 0;
4683

47-
putchar(' ');
48-
for(uint8_t i=0; i<16; i++) putchar(p_product[i]);
49-
putchar('\n');
50-
51-
uint32_t last_lba = 0;
52-
uint32_t block_size = 0;
53-
tuh_msc_get_capacity(dev_addr, &last_lba, &block_size);
54-
printf("Disk Size: %ld MB\r\n", (last_lba+1)/ ((1024*1024)/block_size) );
55-
printf("LBA 0-0x%lX Block Size: %ld\r\n", last_lba, block_size);
84+
uint8_t const lun = 0;
85+
tuh_msc_scsi_inquiry(dev_addr, lun, &inquiry_resp, inquiry_complete_cb);
5686
//
5787
// //------------- file system (only 1 LUN support) -------------//
5888
// uint8_t phy_disk = dev_addr-1;
@@ -103,12 +133,11 @@ void tuh_msc_unmounted_cb(uint8_t dev_addr)
103133
// }
104134
}
105135

106-
// invoked ISR context
107-
void tuh_msc_isr(uint8_t dev_addr, xfer_result_t event, uint32_t xferred_bytes)
108-
{
109-
(void) dev_addr;
110-
(void) event;
111-
(void) xferred_bytes;
112-
}
136+
//void tuh_msc_scsi_complete_cb(uint8_t dev_addr, msc_cbw_t const* cbw, msc_csw_t const* csw)
137+
//{
138+
// (void) dev_addr;
139+
// (void) cbw;
140+
// (void) csw;
141+
//}
113142

114143
#endif

examples/host/cdc_msc_hid/src/tusb_config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
// CONFIGURATION
7070
//--------------------------------------------------------------------
7171

72-
#define CFG_TUH_HUB 0
72+
#define CFG_TUH_HUB 1
7373
#define CFG_TUH_CDC 1
7474
#define CFG_TUH_HID_KEYBOARD 1
7575
#define CFG_TUH_HID_MOUSE 1

examples/obsolete/host/src/msc_host_app.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ void tuh_msc_mounted_cb(uint8_t dev_addr)
6464
putchar('\n');
6565

6666
uint32_t last_lba, block_size;
67-
tuh_msc_get_capacity(dev_addr, &last_lba, &block_size);
67+
tuh_msc_read_capacity(dev_addr, &last_lba, &block_size);
6868
printf("Disk Size: %d MB\n", (last_lba+1)/ ((1024*1024)/block_size) );
6969
printf("LBA 0-0x%X Block Size: %d\n", last_lba, block_size);
7070

hw/bsp/ea4088qs/ea4088qs.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ void SystemInit(void)
9898

9999
Chip_IOCON_Init(LPC_IOCON);
100100
Chip_IOCON_SetPinMuxing(LPC_IOCON, pinmuxing, sizeof(pinmuxing) / sizeof(PINMUX_GRP_T));
101+
102+
/* CPU clock source starts with IRC */
103+
/* Enable PBOOST for CPU clock over 100MHz */
104+
Chip_SYSCTL_EnableBoost();
105+
101106
Chip_SetupXtalClocking();
102107
}
103108

@@ -130,13 +135,15 @@ void board_init(void)
130135
Chip_USB_Init();
131136

132137
enum {
133-
USBCLK = 0x1B // Host + Device + OTG + AHB
138+
USBCLK_DEVCIE = 0x12, // AHB + Device
139+
USBCLK_HOST = 0x19 , // AHB + OTG + Host
140+
USBCLK_ALL = 0x1B // Host + Device + OTG + AHB
134141
};
135142

136-
LPC_USB->OTGClkCtrl = USBCLK;
137-
while ( (LPC_USB->OTGClkSt & USBCLK) != USBCLK ) {}
143+
LPC_USB->OTGClkCtrl = USBCLK_ALL;
144+
while ( (LPC_USB->OTGClkSt & USBCLK_ALL) != USBCLK_ALL ) {}
138145

139-
// USB1 = host, USB2 = device
146+
// set portfunc: USB1 = host, USB2 = device
140147
LPC_USB->StCtrl = 0x3;
141148
}
142149

src/class/cdc/cdc_host.c

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,22 @@ typedef struct {
5151
//--------------------------------------------------------------------+
5252
static cdch_data_t cdch_data[CFG_TUSB_HOST_DEVICE_MAX];
5353

54+
static inline cdch_data_t* get_itf(uint8_t dev_addr)
55+
{
56+
return &cdch_data[dev_addr-1];
57+
}
58+
5459
bool tuh_cdc_mounted(uint8_t dev_addr)
5560
{
56-
cdch_data_t* cdc = &cdch_data[dev_addr-1];
61+
cdch_data_t* cdc = get_itf(dev_addr);
5762
return cdc->ep_in && cdc->ep_out;
5863
}
5964

6065
bool tuh_cdc_is_busy(uint8_t dev_addr, cdc_pipeid_t pipeid)
6166
{
6267
if ( !tuh_cdc_mounted(dev_addr) ) return false;
6368

64-
cdch_data_t const * p_cdc = &cdch_data[dev_addr-1];
69+
cdch_data_t const * p_cdc = get_itf(dev_addr);
6570

6671
switch (pipeid)
6772
{
@@ -111,6 +116,27 @@ bool tuh_cdc_receive(uint8_t dev_addr, void * p_buffer, uint32_t length, bool is
111116
return hcd_pipe_xfer(dev_addr, ep_in, p_buffer, length, is_notify);
112117
}
113118

119+
bool tuh_cdc_set_control_line_state(uint8_t dev_addr, bool dtr, bool rts, tuh_control_complete_cb_t complete_cb)
120+
{
121+
cdch_data_t const * p_cdc = get_itf(dev_addr);
122+
tusb_control_request_t const request =
123+
{
124+
.bmRequestType_bit =
125+
{
126+
.recipient = TUSB_REQ_RCPT_INTERFACE,
127+
.type = TUSB_REQ_TYPE_CLASS,
128+
.direction = TUSB_DIR_OUT
129+
},
130+
.bRequest = CDC_REQUEST_SET_CONTROL_LINE_STATE,
131+
.wValue = (rts ? 2 : 0) | (dtr ? 1 : 0),
132+
.wIndex = p_cdc->itf_num,
133+
.wLength = 0
134+
};
135+
136+
TU_ASSERT( tuh_control_xfer(dev_addr, &request, NULL, complete_cb) );
137+
return true;
138+
}
139+
114140
//--------------------------------------------------------------------+
115141
// USBH-CLASS DRIVER API
116142
//--------------------------------------------------------------------+
@@ -132,7 +158,7 @@ bool cdch_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *it
132158
cdch_data_t * p_cdc;
133159

134160
p_desc = tu_desc_next(itf_desc);
135-
p_cdc = &cdch_data[dev_addr-1];
161+
p_cdc = get_itf(dev_addr);
136162

137163
p_cdc->itf_num = itf_desc->bInterfaceNumber;
138164
p_cdc->itf_protocol = itf_desc->bInterfaceProtocol; // TODO 0xff is consider as rndis candidate, other is virtual Com
@@ -194,30 +220,25 @@ bool cdch_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *it
194220
}
195221
}
196222

197-
// FIXME move to seperate API : connect
198-
tusb_control_request_t request =
199-
{
200-
.bmRequestType_bit = { .recipient = TUSB_REQ_RCPT_INTERFACE, .type = TUSB_REQ_TYPE_CLASS, .direction = TUSB_DIR_OUT },
201-
.bRequest = CDC_REQUEST_SET_CONTROL_LINE_STATE,
202-
.wValue = 0x03, // dtr on, cst on
203-
.wIndex = p_cdc->itf_num,
204-
.wLength = 0
205-
};
206-
207-
TU_ASSERT( usbh_control_xfer(dev_addr, &request, NULL) );
223+
return true;
224+
}
208225

226+
bool cdch_set_config(uint8_t dev_addr, uint8_t itf_num)
227+
{
228+
(void) dev_addr; (void) itf_num;
209229
return true;
210230
}
211231

212-
void cdch_isr(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes)
232+
bool cdch_xfer_cb(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes)
213233
{
214234
(void) ep_addr;
215235
tuh_cdc_xfer_isr( dev_addr, event, 0, xferred_bytes );
236+
return true;
216237
}
217238

218239
void cdch_close(uint8_t dev_addr)
219240
{
220-
cdch_data_t * p_cdc = &cdch_data[dev_addr-1];
241+
cdch_data_t * p_cdc = get_itf(dev_addr);
221242
tu_memclr(p_cdc, sizeof(cdch_data_t));
222243
}
223244

0 commit comments

Comments
 (0)