update hid,msc to new usbh control API

This commit is contained in:
hathach 2022-03-11 22:13:57 +07:00
parent bcdeb386cc
commit ec28593ce5
4 changed files with 139 additions and 98 deletions

View File

@ -71,20 +71,6 @@ int main(void)
#if CFG_TUH_CDC #if CFG_TUH_CDC
CFG_TUSB_MEM_SECTION static char serial_in_buffer[64] = { 0 }; CFG_TUSB_MEM_SECTION static char serial_in_buffer[64] = { 0 };
void tuh_mount_cb(uint8_t dev_addr)
{
// application set-up
printf("A device with address %d is mounted\r\n", dev_addr);
tuh_cdc_receive(dev_addr, serial_in_buffer, sizeof(serial_in_buffer), true); // schedule first transfer
}
void tuh_umount_cb(uint8_t dev_addr)
{
// application tear-down
printf("A device with address %d is unmounted \r\n", dev_addr);
}
// invoked ISR context // invoked ISR context
void tuh_cdc_xfer_isr(uint8_t dev_addr, xfer_result_t event, cdc_pipeid_t pipe_id, uint32_t xferred_bytes) void tuh_cdc_xfer_isr(uint8_t dev_addr, xfer_result_t event, cdc_pipeid_t pipe_id, uint32_t xferred_bytes)
{ {
@ -109,6 +95,19 @@ void cdc_task(void)
// TinyUSB Callbacks // TinyUSB Callbacks
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
void tuh_mount_cb(uint8_t dev_addr)
{
// application set-up
printf("A device with address %d is mounted\r\n", dev_addr);
}
void tuh_umount_cb(uint8_t dev_addr)
{
// application tear-down
printf("A device with address %d is unmounted \r\n", dev_addr);
}
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// Blinking Task // Blinking Task
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+

View File

@ -123,22 +123,29 @@ bool tuh_cdc_receive(uint8_t dev_addr, void * p_buffer, uint32_t length, bool is
bool tuh_cdc_set_control_line_state(uint8_t dev_addr, bool dtr, bool rts, tuh_control_xfer_cb_t complete_cb) bool tuh_cdc_set_control_line_state(uint8_t dev_addr, bool dtr, bool rts, tuh_control_xfer_cb_t complete_cb)
{ {
cdch_data_t const * p_cdc = get_itf(dev_addr); cdch_data_t const * p_cdc = get_itf(dev_addr);
tusb_control_request_t const request =
tuh_control_xfer_t const xfer =
{ {
.bmRequestType_bit = .request =
{ {
.recipient = TUSB_REQ_RCPT_INTERFACE, .bmRequestType_bit =
.type = TUSB_REQ_TYPE_CLASS, {
.direction = TUSB_DIR_OUT .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
}, },
.bRequest = CDC_REQUEST_SET_CONTROL_LINE_STATE,
.wValue = (rts ? 2 : 0) | (dtr ? 1 : 0), .buffer = NULL,
.wIndex = p_cdc->itf_num, .complete_cb = complete_cb,
.wLength = 0 .user_arg = 0
}; };
TU_ASSERT( tuh_control_xfer(dev_addr, &request, NULL, complete_cb) ); return tuh_control_xfer(dev_addr, &xfer);
return true;
} }
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+

View File

@ -103,13 +103,13 @@ uint8_t tuh_hid_get_protocol(uint8_t dev_addr, uint8_t instance)
return hid_itf->protocol_mode; return hid_itf->protocol_mode;
} }
static bool set_protocol_complete(uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result) static bool set_protocol_complete(uint8_t dev_addr, tuh_control_xfer_t const * xfer, xfer_result_t result)
{ {
uint8_t const itf_num = (uint8_t) request->wIndex; uint8_t const itf_num = (uint8_t) xfer->request.wIndex;
uint8_t const instance = get_instance_id_by_itfnum(dev_addr, itf_num); uint8_t const instance = get_instance_id_by_itfnum(dev_addr, itf_num);
hidh_interface_t* hid_itf = get_instance(dev_addr, instance); hidh_interface_t* hid_itf = get_instance(dev_addr, instance);
if (XFER_RESULT_SUCCESS == result) hid_itf->protocol_mode = (uint8_t) request->wValue; if (XFER_RESULT_SUCCESS == result) hid_itf->protocol_mode = (uint8_t) xfer->request.wValue;
if (tuh_hid_set_protocol_complete_cb) if (tuh_hid_set_protocol_complete_cb)
{ {
@ -126,37 +126,44 @@ bool tuh_hid_set_protocol(uint8_t dev_addr, uint8_t instance, uint8_t protocol)
TU_LOG2("HID Set Protocol = %d\r\n", protocol); TU_LOG2("HID Set Protocol = %d\r\n", protocol);
tusb_control_request_t const request = tuh_control_xfer_t const xfer =
{ {
.bmRequestType_bit = .request =
{ {
.recipient = TUSB_REQ_RCPT_INTERFACE, .bmRequestType_bit =
.type = TUSB_REQ_TYPE_CLASS, {
.direction = TUSB_DIR_OUT .recipient = TUSB_REQ_RCPT_INTERFACE,
.type = TUSB_REQ_TYPE_CLASS,
.direction = TUSB_DIR_OUT
},
.bRequest = HID_REQ_CONTROL_SET_PROTOCOL,
.wValue = protocol,
.wIndex = hid_itf->itf_num,
.wLength = 0
}, },
.bRequest = HID_REQ_CONTROL_SET_PROTOCOL,
.wValue = protocol, .buffer = NULL,
.wIndex = hid_itf->itf_num, .complete_cb = set_protocol_complete,
.wLength = 0 .user_arg = 0
}; };
TU_ASSERT( tuh_control_xfer(dev_addr, &request, NULL, set_protocol_complete) ); TU_ASSERT( tuh_control_xfer(dev_addr, &xfer) );
return true; return true;
} }
static bool set_report_complete(uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result) static bool set_report_complete(uint8_t dev_addr, tuh_control_xfer_t const * xfer, xfer_result_t result)
{ {
TU_LOG2("HID Set Report complete\r\n"); TU_LOG2("HID Set Report complete\r\n");
if (tuh_hid_set_report_complete_cb) if (tuh_hid_set_report_complete_cb)
{ {
uint8_t const itf_num = (uint8_t) request->wIndex; uint8_t const itf_num = (uint8_t) xfer->request.wIndex;
uint8_t const instance = get_instance_id_by_itfnum(dev_addr, itf_num); uint8_t const instance = get_instance_id_by_itfnum(dev_addr, itf_num);
uint8_t const report_type = tu_u16_high(request->wValue); uint8_t const report_type = tu_u16_high(xfer->request.wValue);
uint8_t const report_id = tu_u16_low(request->wValue); uint8_t const report_id = tu_u16_low(xfer->request.wValue);
tuh_hid_set_report_complete_cb(dev_addr, instance, report_id, report_type, (result == XFER_RESULT_SUCCESS) ? request->wLength : 0); tuh_hid_set_report_complete_cb(dev_addr, instance, report_id, report_type, (result == XFER_RESULT_SUCCESS) ? xfer->request.wLength : 0);
} }
return true; return true;
@ -167,21 +174,28 @@ bool tuh_hid_set_report(uint8_t dev_addr, uint8_t instance, uint8_t report_id, u
hidh_interface_t* hid_itf = get_instance(dev_addr, instance); hidh_interface_t* hid_itf = get_instance(dev_addr, instance);
TU_LOG2("HID Set Report: id = %u, type = %u, len = %u\r\n", report_id, report_type, len); TU_LOG2("HID Set Report: id = %u, type = %u, len = %u\r\n", report_id, report_type, len);
tusb_control_request_t const request = tuh_control_xfer_t const xfer =
{ {
.bmRequestType_bit = .request =
{ {
.recipient = TUSB_REQ_RCPT_INTERFACE, .bmRequestType_bit =
.type = TUSB_REQ_TYPE_CLASS, {
.direction = TUSB_DIR_OUT .recipient = TUSB_REQ_RCPT_INTERFACE,
.type = TUSB_REQ_TYPE_CLASS,
.direction = TUSB_DIR_OUT
},
.bRequest = HID_REQ_CONTROL_SET_REPORT,
.wValue = tu_u16(report_type, report_id),
.wIndex = hid_itf->itf_num,
.wLength = len
}, },
.bRequest = HID_REQ_CONTROL_SET_REPORT,
.wValue = tu_u16(report_type, report_id), .buffer = report,
.wIndex = hid_itf->itf_num, .complete_cb = set_report_complete,
.wLength = len .user_arg = 0
}; };
TU_ASSERT( tuh_control_xfer(dev_addr, &request, report, set_report_complete) ); TU_ASSERT( tuh_control_xfer(dev_addr, &xfer) );
return true; return true;
} }
@ -256,9 +270,9 @@ void hidh_close(uint8_t dev_addr)
// Enumeration // Enumeration
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
static bool config_set_protocol (uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result); static bool config_set_protocol (uint8_t dev_addr, tuh_control_xfer_t const * xfer, xfer_result_t result);
static bool config_get_report_desc (uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result); static bool config_get_report_desc (uint8_t dev_addr, tuh_control_xfer_t const * xfer, xfer_result_t result);
static bool config_get_report_desc_complete (uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result); static bool config_get_report_desc_complete (uint8_t dev_addr, tuh_control_xfer_t const * xfer, xfer_result_t result);
static void config_driver_mount_complete(uint8_t dev_addr, uint8_t instance, uint8_t const* desc_report, uint16_t desc_len); static void config_driver_mount_complete(uint8_t dev_addr, uint8_t instance, uint8_t const* desc_report, uint16_t desc_len);
@ -337,65 +351,79 @@ bool hidh_set_config(uint8_t dev_addr, uint8_t itf_num)
// SET IDLE request, device can stall if not support this request // SET IDLE request, device can stall if not support this request
TU_LOG2("HID Set Idle \r\n"); TU_LOG2("HID Set Idle \r\n");
tusb_control_request_t const request = tuh_control_xfer_t const xfer =
{ {
.bmRequestType_bit = .request =
{ {
.recipient = TUSB_REQ_RCPT_INTERFACE, .bmRequestType_bit =
.type = TUSB_REQ_TYPE_CLASS, {
.direction = TUSB_DIR_OUT .recipient = TUSB_REQ_RCPT_INTERFACE,
.type = TUSB_REQ_TYPE_CLASS,
.direction = TUSB_DIR_OUT
},
.bRequest = HID_REQ_CONTROL_SET_IDLE,
.wValue = idle_rate,
.wIndex = itf_num,
.wLength = 0
}, },
.bRequest = HID_REQ_CONTROL_SET_IDLE,
.wValue = idle_rate, .buffer = NULL,
.wIndex = itf_num, .complete_cb = (hid_itf->itf_protocol != HID_ITF_PROTOCOL_NONE) ? config_set_protocol : config_get_report_desc,
.wLength = 0 .user_arg = 0
}; };
TU_ASSERT( tuh_control_xfer(dev_addr, &request, NULL, (hid_itf->itf_protocol != HID_ITF_PROTOCOL_NONE) ? config_set_protocol : config_get_report_desc) ); TU_ASSERT( tuh_control_xfer(dev_addr, &xfer) );
return true; return true;
} }
// Force device to work in BOOT protocol // Force device to work in BOOT protocol
static bool config_set_protocol(uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result) static bool config_set_protocol(uint8_t dev_addr, tuh_control_xfer_t const * xfer, xfer_result_t result)
{ {
// Stall is a valid response for SET_IDLE, therefore we could ignore its result // Stall is a valid response for SET_IDLE, therefore we could ignore its result
(void) result; (void) result;
uint8_t const itf_num = (uint8_t) request->wIndex; uint8_t const itf_num = (uint8_t) xfer->request.wIndex;
uint8_t const instance = get_instance_id_by_itfnum(dev_addr, itf_num); uint8_t const instance = get_instance_id_by_itfnum(dev_addr, itf_num);
hidh_interface_t* hid_itf = get_instance(dev_addr, instance); hidh_interface_t* hid_itf = get_instance(dev_addr, instance);
TU_LOG2("HID Set Protocol to Boot Mode\r\n"); TU_LOG2("HID Set Protocol to Boot Mode\r\n");
hid_itf->protocol_mode = HID_PROTOCOL_BOOT; hid_itf->protocol_mode = HID_PROTOCOL_BOOT;
tusb_control_request_t const new_request = tuh_control_xfer_t const new_xfer =
{ {
.bmRequestType_bit = .request =
{ {
.recipient = TUSB_REQ_RCPT_INTERFACE, .bmRequestType_bit =
.type = TUSB_REQ_TYPE_CLASS, {
.direction = TUSB_DIR_OUT .recipient = TUSB_REQ_RCPT_INTERFACE,
.type = TUSB_REQ_TYPE_CLASS,
.direction = TUSB_DIR_OUT
},
.bRequest = HID_REQ_CONTROL_SET_PROTOCOL,
.wValue = HID_PROTOCOL_BOOT,
.wIndex = hid_itf->itf_num,
.wLength = 0
}, },
.bRequest = HID_REQ_CONTROL_SET_PROTOCOL,
.wValue = HID_PROTOCOL_BOOT, .buffer = NULL,
.wIndex = hid_itf->itf_num, .complete_cb = config_get_report_desc,
.wLength = 0 .user_arg = 0
}; };
TU_ASSERT( tuh_control_xfer(dev_addr, &new_request, NULL, config_get_report_desc) ); TU_ASSERT( tuh_control_xfer(dev_addr, &new_xfer) );
return true; return true;
} }
static bool config_get_report_desc(uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result) static bool config_get_report_desc(uint8_t dev_addr, tuh_control_xfer_t const * xfer, xfer_result_t result)
{ {
// We can be here after SET_IDLE or SET_PROTOCOL (boot device) // We can be here after SET_IDLE or SET_PROTOCOL (boot device)
// Trigger assert if result is not successful with set protocol // Trigger assert if result is not successful with set protocol
if ( request->bRequest != HID_REQ_CONTROL_SET_IDLE ) if ( xfer->request.bRequest != HID_REQ_CONTROL_SET_IDLE )
{ {
TU_ASSERT(result == XFER_RESULT_SUCCESS); TU_ASSERT(result == XFER_RESULT_SUCCESS);
} }
uint8_t const itf_num = (uint8_t) request->wIndex; uint8_t const itf_num = (uint8_t) xfer->request.wIndex;
uint8_t const instance = get_instance_id_by_itfnum(dev_addr, itf_num); uint8_t const instance = get_instance_id_by_itfnum(dev_addr, itf_num);
hidh_interface_t* hid_itf = get_instance(dev_addr, instance); hidh_interface_t* hid_itf = get_instance(dev_addr, instance);
@ -409,21 +437,21 @@ static bool config_get_report_desc(uint8_t dev_addr, tusb_control_request_t cons
config_driver_mount_complete(dev_addr, instance, NULL, 0); config_driver_mount_complete(dev_addr, instance, NULL, 0);
}else }else
{ {
TU_ASSERT(tuh_descriptor_get_hid_report(dev_addr, itf_num, hid_itf->report_desc_type, usbh_get_enum_buf(), hid_itf->report_desc_len, config_get_report_desc_complete)); TU_ASSERT(tuh_descriptor_get_hid_report(dev_addr, itf_num, hid_itf->report_desc_type, usbh_get_enum_buf(), hid_itf->report_desc_len, config_get_report_desc_complete, 0));
} }
return true; return true;
} }
static bool config_get_report_desc_complete(uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result) static bool config_get_report_desc_complete(uint8_t dev_addr, tuh_control_xfer_t const * xfer, xfer_result_t result)
{ {
TU_ASSERT(XFER_RESULT_SUCCESS == result); TU_ASSERT(XFER_RESULT_SUCCESS == result);
uint8_t const itf_num = (uint8_t) request->wIndex; uint8_t const itf_num = (uint8_t) xfer->request.wIndex;
uint8_t const instance = get_instance_id_by_itfnum(dev_addr, itf_num); uint8_t const instance = get_instance_id_by_itfnum(dev_addr, itf_num);
uint8_t const* desc_report = usbh_get_enum_buf(); uint8_t const* desc_report = usbh_get_enum_buf();
uint16_t const desc_len = request->wLength; uint16_t const desc_len = xfer->request.wLength;
config_driver_mount_complete(dev_addr, instance, desc_report, desc_len); config_driver_mount_complete(dev_addr, instance, desc_report, desc_len);

View File

@ -358,7 +358,7 @@ bool msch_xfer_cb(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t event, uint32
// MSC Enumeration // MSC Enumeration
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
static bool config_get_maxlun_complete (uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result); static bool config_get_maxlun_complete (uint8_t dev_addr, tuh_control_xfer_t const * xfer, xfer_result_t result);
static bool config_test_unit_ready_complete(uint8_t dev_addr, msc_cbw_t const* cbw, msc_csw_t const* csw); static bool config_test_unit_ready_complete(uint8_t dev_addr, msc_cbw_t const* cbw, msc_csw_t const* csw);
static bool config_request_sense_complete(uint8_t dev_addr, msc_cbw_t const* cbw, msc_csw_t const* csw); static bool config_request_sense_complete(uint8_t dev_addr, msc_cbw_t const* cbw, msc_csw_t const* csw);
static bool config_read_capacity_complete(uint8_t dev_addr, msc_cbw_t const* cbw, msc_csw_t const* csw); static bool config_read_capacity_complete(uint8_t dev_addr, msc_cbw_t const* cbw, msc_csw_t const* csw);
@ -405,27 +405,34 @@ bool msch_set_config(uint8_t dev_addr, uint8_t itf_num)
//------------- Get Max Lun -------------// //------------- Get Max Lun -------------//
TU_LOG2("MSC Get Max Lun\r\n"); TU_LOG2("MSC Get Max Lun\r\n");
tusb_control_request_t request = tuh_control_xfer_t const xfer =
{ {
.bmRequestType_bit = .request =
{ {
.recipient = TUSB_REQ_RCPT_INTERFACE, .bmRequestType_bit =
.type = TUSB_REQ_TYPE_CLASS, {
.direction = TUSB_DIR_IN .recipient = TUSB_REQ_RCPT_INTERFACE,
.type = TUSB_REQ_TYPE_CLASS,
.direction = TUSB_DIR_IN
},
.bRequest = MSC_REQ_GET_MAX_LUN,
.wValue = 0,
.wIndex = itf_num,
.wLength = 1
}, },
.bRequest = MSC_REQ_GET_MAX_LUN,
.wValue = 0, .buffer = &p_msc->max_lun,
.wIndex = itf_num, .complete_cb = config_get_maxlun_complete,
.wLength = 1 .user_arg = 0
}; };
TU_ASSERT(tuh_control_xfer(dev_addr, &request, &p_msc->max_lun, config_get_maxlun_complete)); TU_ASSERT(tuh_control_xfer(dev_addr, &xfer));
return true; return true;
} }
static bool config_get_maxlun_complete (uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result) static bool config_get_maxlun_complete (uint8_t dev_addr, tuh_control_xfer_t const * xfer, xfer_result_t result)
{ {
(void) request; (void) xfer;
msch_interface_t* p_msc = get_itf(dev_addr); msch_interface_t* p_msc = get_itf(dev_addr);