Skip to content

Commit

Permalink
Merge pull request #553 from hathach/host-async-control
Browse files Browse the repository at this point in the history
Host async control
  • Loading branch information
hathach committed Nov 7, 2020
2 parents 075334a + 2907b1e commit a708ab6
Show file tree
Hide file tree
Showing 27 changed files with 1,636 additions and 926 deletions.
3 changes: 2 additions & 1 deletion examples/host/cdc_msc_hid/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ SRC_C += \
src/class/cdc/cdc_host.c \
src/class/hid/hid_host.c \
src/class/msc/msc_host.c \
src/host/usbh.c \
src/host/hub.c \
src/host/usbh.c \
src/host/usbh_control.c \
src/host/ehci/ehci.c \
src/host/ohci/ohci.c \
src/portable/nxp/lpc18_43/hcd_lpc18_43.c \
Expand Down
1 change: 1 addition & 0 deletions examples/host/cdc_msc_hid/ses/lpc18xx/lpc18xx.emProject
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
debug_target_connection="J-Link"
gcc_entry_point="Reset_Handler"
linker_memory_map_file="$(ProjectDir)/LPC1857_MemoryMap.xml"
linker_printf_width_precision_supported="Yes"
linker_section_placement_file="$(ProjectDir)/flash_placement.xml"
macros="DeviceFamily=LPC1800;DeviceSubFamily=LPC185x;Target=LPC1857;Placement=Flash;rootDir=../../../../..;lpcDir=../../../../../hw/mcu/nxp/lpcopen/lpc18xx/lpc_chip_18xx"
package_dependencies="LPC1800"
Expand Down
97 changes: 82 additions & 15 deletions examples/host/cdc_msc_hid/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ void cdc_task(void)
//--------------------------------------------------------------------+
#if CFG_TUH_HID_KEYBOARD

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

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

CFG_TUSB_MEM_SECTION static hid_keyboard_report_t usb_keyboard_report;

void hid_task(void)
{
uint8_t const addr = 1;
if ( tuh_hid_keyboard_is_mounted(addr) )
{
if ( !tuh_hid_keyboard_is_busy(addr) )
{
process_kbd_report(&usb_keyboard_report);
tuh_hid_keyboard_get_report(addr, &usb_keyboard_report);
}
}
}

void tuh_hid_keyboard_mounted_cb(uint8_t dev_addr)
{
// application set-up
Expand All @@ -192,6 +178,58 @@ void tuh_hid_keyboard_isr(uint8_t dev_addr, xfer_result_t event)
#endif

#if CFG_TUH_HID_MOUSE

CFG_TUSB_MEM_SECTION static hid_mouse_report_t usb_mouse_report;

void cursor_movement(int8_t x, int8_t y, int8_t wheel)
{
//------------- X -------------//
if ( x < 0)
{
printf(ANSI_CURSOR_BACKWARD(%d), (-x)); // move left
}else if ( x > 0)
{
printf(ANSI_CURSOR_FORWARD(%d), x); // move right
}else { }

//------------- Y -------------//
if ( y < 0)
{
printf(ANSI_CURSOR_UP(%d), (-y)); // move up
}else if ( y > 0)
{
printf(ANSI_CURSOR_DOWN(%d), y); // move down
}else { }

//------------- wheel -------------//
if (wheel < 0)
{
printf(ANSI_SCROLL_UP(%d), (-wheel)); // scroll up
}else if (wheel > 0)
{
printf(ANSI_SCROLL_DOWN(%d), wheel); // scroll down
}else { }
}

static inline void process_mouse_report(hid_mouse_report_t const * p_report)
{
static hid_mouse_report_t prev_report = { 0 };

//------------- button state -------------//
uint8_t button_changed_mask = p_report->buttons ^ prev_report.buttons;
if ( button_changed_mask & p_report->buttons)
{
printf(" %c%c%c ",
p_report->buttons & MOUSE_BUTTON_LEFT ? 'L' : '-',
p_report->buttons & MOUSE_BUTTON_MIDDLE ? 'M' : '-',
p_report->buttons & MOUSE_BUTTON_RIGHT ? 'R' : '-');
}

//------------- cursor movement -------------//
cursor_movement(p_report->x, p_report->y, p_report->wheel);
}


void tuh_hid_mouse_mounted_cb(uint8_t dev_addr)
{
// application set-up
Expand All @@ -212,6 +250,35 @@ void tuh_hid_mouse_isr(uint8_t dev_addr, xfer_result_t event)
}
#endif



void hid_task(void)
{
uint8_t const addr = 1;

#if CFG_TUH_HID_KEYBOARD
if ( tuh_hid_keyboard_is_mounted(addr) )
{
if ( !tuh_hid_keyboard_is_busy(addr) )
{
process_kbd_report(&usb_keyboard_report);
tuh_hid_keyboard_get_report(addr, &usb_mouse_report);
}
}
#endif

#if CFG_TUH_HID_MOUSE
if ( tuh_hid_mouse_is_mounted(addr) )
{
if ( !tuh_hid_mouse_is_busy(addr) )
{
process_mouse_report(&usb_mouse_report);
tuh_hid_mouse_get_report(addr, &usb_mouse_report);
}
}
#endif
}

//--------------------------------------------------------------------+
// tinyusb callbacks
//--------------------------------------------------------------------+
Expand Down
73 changes: 51 additions & 22 deletions examples/host/cdc_msc_hid/src/msc_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,59 @@
//--------------------------------------------------------------------+
// MACRO TYPEDEF CONSTANT ENUM DECLARATION
//--------------------------------------------------------------------+
static scsi_inquiry_resp_t inquiry_resp;
static scsi_read_capacity10_resp_t capacity_resp;

uint32_t block_size;
uint32_t block_count;

bool capacity_complete_cb(uint8_t dev_addr, msc_cbw_t const* cbw, msc_csw_t const* csw)
{
(void) dev_addr;
(void) cbw;

if (csw->status != 0)
{
printf("Read Capacity (10) failed\r\n");
return false;
}

// Capacity response field: Block size and Last LBA are both Big-Endian
block_count = tu_ntohl(capacity_resp.last_lba) + 1;
block_size = tu_ntohl(capacity_resp.block_size);

printf("Disk Size: %lu MB\r\n", block_count / ((1024*1024)/block_size));
printf("Block Count = %lu, Block Size: %lu\r\n", block_count, block_size);

return true;
}

bool inquiry_complete_cb(uint8_t dev_addr, msc_cbw_t const* cbw, msc_csw_t const* csw)
{
if (csw->status != 0)
{
printf("Inquiry failed\r\n");
return false;
}

// Print out Vendor ID, Product ID and Rev
printf("%.8s %.16s rev %.4s\r\n", inquiry_resp.vendor_id, inquiry_resp.product_id, inquiry_resp.product_rev);

// Read capacity of device
tuh_msc_read_capacity(dev_addr, cbw->lun, &capacity_resp, capacity_complete_cb);

return true;
}

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

//------------- Disk Information -------------//
// SCSI VendorID[8] & ProductID[16] from Inquiry Command
uint8_t const* p_vendor = tuh_msc_get_vendor_name(dev_addr);
uint8_t const* p_product = tuh_msc_get_product_name(dev_addr);

for(uint8_t i=0; i<8; i++) putchar(p_vendor[i]);
block_size = block_count = 0;

putchar(' ');
for(uint8_t i=0; i<16; i++) putchar(p_product[i]);
putchar('\n');

uint32_t last_lba = 0;
uint32_t block_size = 0;
tuh_msc_get_capacity(dev_addr, &last_lba, &block_size);
printf("Disk Size: %ld MB\r\n", (last_lba+1)/ ((1024*1024)/block_size) );
printf("LBA 0-0x%lX Block Size: %ld\r\n", last_lba, block_size);
uint8_t const lun = 0;
tuh_msc_scsi_inquiry(dev_addr, lun, &inquiry_resp, inquiry_complete_cb);
//
// //------------- file system (only 1 LUN support) -------------//
// uint8_t phy_disk = dev_addr-1;
Expand Down Expand Up @@ -103,12 +133,11 @@ void tuh_msc_unmounted_cb(uint8_t dev_addr)
// }
}

// invoked ISR context
void tuh_msc_isr(uint8_t dev_addr, xfer_result_t event, uint32_t xferred_bytes)
{
(void) dev_addr;
(void) event;
(void) xferred_bytes;
}
//void tuh_msc_scsi_complete_cb(uint8_t dev_addr, msc_cbw_t const* cbw, msc_csw_t const* csw)
//{
// (void) dev_addr;
// (void) cbw;
// (void) csw;
//}

#endif
2 changes: 1 addition & 1 deletion examples/host/cdc_msc_hid/src/tusb_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
// CONFIGURATION
//--------------------------------------------------------------------

#define CFG_TUH_HUB 0
#define CFG_TUH_HUB 1
#define CFG_TUH_CDC 1
#define CFG_TUH_HID_KEYBOARD 1
#define CFG_TUH_HID_MOUSE 1
Expand Down
2 changes: 1 addition & 1 deletion examples/obsolete/host/src/msc_host_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void tuh_msc_mounted_cb(uint8_t dev_addr)
putchar('\n');

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

Expand Down
15 changes: 11 additions & 4 deletions hw/bsp/ea4088qs/ea4088qs.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ void SystemInit(void)

Chip_IOCON_Init(LPC_IOCON);
Chip_IOCON_SetPinMuxing(LPC_IOCON, pinmuxing, sizeof(pinmuxing) / sizeof(PINMUX_GRP_T));

/* CPU clock source starts with IRC */
/* Enable PBOOST for CPU clock over 100MHz */
Chip_SYSCTL_EnableBoost();

Chip_SetupXtalClocking();
}

Expand Down Expand Up @@ -130,13 +135,15 @@ void board_init(void)
Chip_USB_Init();

enum {
USBCLK = 0x1B // Host + Device + OTG + AHB
USBCLK_DEVCIE = 0x12, // AHB + Device
USBCLK_HOST = 0x19 , // AHB + OTG + Host
USBCLK_ALL = 0x1B // Host + Device + OTG + AHB
};

LPC_USB->OTGClkCtrl = USBCLK;
while ( (LPC_USB->OTGClkSt & USBCLK) != USBCLK ) {}
LPC_USB->OTGClkCtrl = USBCLK_ALL;
while ( (LPC_USB->OTGClkSt & USBCLK_ALL) != USBCLK_ALL ) {}

// USB1 = host, USB2 = device
// set portfunc: USB1 = host, USB2 = device
LPC_USB->StCtrl = 0x3;
}

Expand Down
53 changes: 37 additions & 16 deletions src/class/cdc/cdc_host.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,22 @@ typedef struct {
//--------------------------------------------------------------------+
static cdch_data_t cdch_data[CFG_TUSB_HOST_DEVICE_MAX];

static inline cdch_data_t* get_itf(uint8_t dev_addr)
{
return &cdch_data[dev_addr-1];
}

bool tuh_cdc_mounted(uint8_t dev_addr)
{
cdch_data_t* cdc = &cdch_data[dev_addr-1];
cdch_data_t* cdc = get_itf(dev_addr);
return cdc->ep_in && cdc->ep_out;
}

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

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

switch (pipeid)
{
Expand Down Expand Up @@ -111,6 +116,27 @@ bool tuh_cdc_receive(uint8_t dev_addr, void * p_buffer, uint32_t length, bool is
return hcd_pipe_xfer(dev_addr, ep_in, p_buffer, length, is_notify);
}

bool tuh_cdc_set_control_line_state(uint8_t dev_addr, bool dtr, bool rts, tuh_control_complete_cb_t complete_cb)
{
cdch_data_t const * p_cdc = get_itf(dev_addr);
tusb_control_request_t const request =
{
.bmRequestType_bit =
{
.recipient = TUSB_REQ_RCPT_INTERFACE,
.type = TUSB_REQ_TYPE_CLASS,
.direction = TUSB_DIR_OUT
},
.bRequest = CDC_REQUEST_SET_CONTROL_LINE_STATE,
.wValue = (rts ? 2 : 0) | (dtr ? 1 : 0),
.wIndex = p_cdc->itf_num,
.wLength = 0
};

TU_ASSERT( tuh_control_xfer(dev_addr, &request, NULL, complete_cb) );
return true;
}

//--------------------------------------------------------------------+
// USBH-CLASS DRIVER API
//--------------------------------------------------------------------+
Expand All @@ -132,7 +158,7 @@ bool cdch_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *it
cdch_data_t * p_cdc;

p_desc = tu_desc_next(itf_desc);
p_cdc = &cdch_data[dev_addr-1];
p_cdc = get_itf(dev_addr);

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

// FIXME move to seperate API : connect
tusb_control_request_t request =
{
.bmRequestType_bit = { .recipient = TUSB_REQ_RCPT_INTERFACE, .type = TUSB_REQ_TYPE_CLASS, .direction = TUSB_DIR_OUT },
.bRequest = CDC_REQUEST_SET_CONTROL_LINE_STATE,
.wValue = 0x03, // dtr on, cst on
.wIndex = p_cdc->itf_num,
.wLength = 0
};

TU_ASSERT( usbh_control_xfer(dev_addr, &request, NULL) );
return true;
}

bool cdch_set_config(uint8_t dev_addr, uint8_t itf_num)
{
(void) dev_addr; (void) itf_num;
return true;
}

void cdch_isr(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes)
bool cdch_xfer_cb(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes)
{
(void) ep_addr;
tuh_cdc_xfer_isr( dev_addr, event, 0, xferred_bytes );
return true;
}

void cdch_close(uint8_t dev_addr)
{
cdch_data_t * p_cdc = &cdch_data[dev_addr-1];
cdch_data_t * p_cdc = get_itf(dev_addr);
tu_memclr(p_cdc, sizeof(cdch_data_t));
}

Expand Down

0 comments on commit a708ab6

Please sign in to comment.