41 #if MODE_DEVICE_SUPPORTED
43 #define _TINY_USB_SOURCE_FILE_
64 .control_request_subtask = hidd_control_request_subtask,
65 .xfer_cb = hidd_xfer_cb,
70 #if TUSB_CFG_DEVICE_MSC
75 .control_request_subtask = mscd_control_request_subtask,
76 .xfer_cb = mscd_xfer_cb,
81 #if TUSB_CFG_DEVICE_CDC
86 .control_request_subtask = cdcd_control_request_subtask,
87 .xfer_cb = cdcd_xfer_cb,
94 enum { USBD_CLASS_DRIVER_COUNT =
sizeof(usbd_class_drivers) /
sizeof(
usbd_class_driver_t) };
99 static tusb_error_t usbd_set_configure_received(uint8_t coreid, uint8_t config_number);
105 bool tusbd_is_configured(uint8_t coreid)
107 return usbd_devices[coreid].state == TUSB_DEVICE_STATE_CONFIGURED;
115 enum { USBD_TASK_QUEUE_DEPTH = 8 };
118 USBD_EVENTID_SETUP_RECEIVED = 1,
119 USBD_EVENTID_XFER_DONE
126 uint8_t sub_event_id;
134 uint32_t xferred_byte;
139 STATIC_ASSERT(
sizeof(usbd_task_event_t) <= 12,
"size is not correct");
142 OSAL_QUEUE_DEF(usbd_queue_def, USBD_TASK_QUEUE_DEPTH, usbd_task_event_t);
143 OSAL_SEM_DEF(usbd_control_xfer_semaphore_def);
146 osal_semaphore_handle_t usbd_control_xfer_sem_hdl;
156 ASSERT_STATUS ( dcd_init() );
159 usbd_queue_hdl = osal_queue_create( OSAL_QUEUE_REF(usbd_queue_def) );
160 ASSERT_PTR(usbd_queue_hdl, TUSB_ERROR_OSAL_QUEUE_FAILED);
162 usbd_control_xfer_sem_hdl = osal_semaphore_create( OSAL_SEM_REF(usbd_control_xfer_semaphore_def) );
163 ASSERT_PTR(usbd_queue_hdl, TUSB_ERROR_OSAL_SEMAPHORE_FAILED);
165 ASSERT_STATUS( osal_task_create( OSAL_TASK_REF(usbd_task) ));
171 for (uint8_t class_code =
TUSB_CLASS_AUDIO; class_code < USBD_CLASS_DRIVER_COUNT; class_code++)
173 if ( usbd_class_drivers[class_code].init )
175 usbd_class_drivers[class_code].init();
179 return TUSB_ERROR_NONE;
185 OSAL_TASK_FUNCTION(usbd_task, p_task_para)
196 static usbd_task_event_t event;
201 error = TUSB_ERROR_NONE;
203 osal_queue_receive(usbd_queue_hdl, &event, OSAL_TIMEOUT_WAIT_FOREVER, &error);
204 SUBTASK_ASSERT_STATUS(error);
206 if ( USBD_EVENTID_SETUP_RECEIVED == event.event_id )
208 OSAL_SUBTASK_INVOKED_AND_WAIT( usbd_control_request_subtask(event.coreid, &event.setup_received), error );
212 class_index = std_class_code_to_index( event.xfer_done.edpt_hdl.class_code );
214 if (usbd_class_drivers[class_index].xfer_cb)
216 usbd_class_drivers[class_index].xfer_cb( event.xfer_done.edpt_hdl, (tusb_event_t) event.sub_event_id, event.xfer_done.xferred_byte);
219 hal_debugger_breakpoint();
234 error = TUSB_ERROR_NONE;
237 if( TUSB_REQUEST_RECIPIENT_DEVICE == p_request->bmRequestType_bit.recipient &&
238 TUSB_REQUEST_TYPE_STANDARD == p_request->bmRequestType_bit.type )
242 uint8_t
const * p_buffer = NULL;
245 error = get_descriptor(coreid, p_request, &p_buffer, &length);
247 if ( TUSB_ERROR_NONE == error )
249 dcd_pipe_control_xfer(coreid, (tusb_direction_t) p_request->bmRequestType_bit.direction, (uint8_t*) p_buffer, length,
false);
254 dcd_controller_set_address(coreid, (uint8_t) p_request->wValue);
255 usbd_devices[coreid].state = TUSB_DEVICE_STATE_ADDRESSED;
259 usbd_set_configure_received(coreid, (uint8_t) p_request->wValue);
262 error = TUSB_ERROR_DCD_CONTROL_REQUEST_NOT_SUPPORT;
267 else if ( TUSB_REQUEST_RECIPIENT_INTERFACE == p_request->bmRequestType_bit.recipient)
269 static uint8_t class_code;
271 class_code = usbd_devices[coreid].interface2class[ u16_low_u8(p_request->wIndex) ];
274 if ( (class_code > 0) && (class_code < USBD_CLASS_DRIVER_COUNT) &&
275 usbd_class_drivers[class_code].control_request_subtask )
277 OSAL_SUBTASK_INVOKED_AND_WAIT( usbd_class_drivers[class_code].control_request_subtask(coreid, p_request), error );
280 error = TUSB_ERROR_DCD_CONTROL_REQUEST_NOT_SUPPORT;
285 else if ( TUSB_REQUEST_RECIPIENT_ENDPOINT == p_request->bmRequestType_bit.recipient &&
286 TUSB_REQUEST_TYPE_STANDARD == p_request->bmRequestType_bit.type &&
289 dcd_pipe_clear_stall(coreid, u16_low_u8(p_request->wIndex) );
292 error = TUSB_ERROR_DCD_CONTROL_REQUEST_NOT_SUPPORT;
295 if(TUSB_ERROR_NONE != error)
297 dcd_pipe_control_stall(coreid);
299 }
else if (p_request->wLength == 0)
301 dcd_pipe_control_xfer(coreid, (tusb_direction_t) p_request->bmRequestType_bit.direction, NULL, 0,
false);
309 static tusb_error_t usbd_set_configure_received(uint8_t coreid, uint8_t config_number)
311 dcd_controller_set_configuration(coreid);
312 usbd_devices[coreid].state = TUSB_DEVICE_STATE_CONFIGURED;
320 while( p_desc < p_desc_config + config_total_length )
322 if ( TUSB_DESC_TYPE_INTERFACE_ASSOCIATION == p_desc[DESCRIPTOR_OFFSET_TYPE])
324 p_desc += p_desc[DESCRIPTOR_OFFSET_LENGTH];
327 ASSERT( TUSB_DESC_TYPE_INTERFACE == p_desc[DESCRIPTOR_OFFSET_TYPE], TUSB_ERROR_NOT_SUPPORTED_YET );
334 ASSERT( class_index != 0 && class_index < USBD_CLASS_DRIVER_COUNT && usbd_class_drivers[class_index].open != NULL, TUSB_ERROR_NOT_SUPPORTED_YET );
335 ASSERT( 0 == usbd_devices[coreid].interface2class[p_desc_interface->
bInterfaceNumber], TUSB_ERROR_FAILED);
337 usbd_devices[coreid].interface2class[p_desc_interface->
bInterfaceNumber] = class_index;
340 ASSERT_STATUS( usbd_class_drivers[class_index].open( coreid, p_desc_interface, &length ) );
347 return TUSB_ERROR_NONE;
353 uint8_t
const desc_index = u16_low_u8( p_request->wValue );
355 uint8_t
const * p_data = NULL ;
359 case TUSB_DESC_TYPE_DEVICE:
364 case TUSB_DESC_TYPE_CONFIGURATION:
369 case TUSB_DESC_TYPE_STRING:
370 if ( !(desc_index < 100) )
return TUSB_ERROR_DCD_CONTROL_REQUEST_NOT_SUPPORT;
373 ASSERT( p_data != NULL, TUSB_ERROR_FAILED);
375 (*p_length) = p_data[0];
378 default:
return TUSB_ERROR_DCD_CONTROL_REQUEST_NOT_SUPPORT;
381 (*p_length) = min16_of(p_request->wLength, (*p_length) );
382 ASSERT( (*p_length) <= TUSB_CFG_DEVICE_ENUM_BUFFER_SIZE, TUSB_ERROR_NOT_ENOUGH_MEMORY);
384 memcpy(usbd_enum_buffer, p_data, (*p_length));
385 (*pp_buffer) = usbd_enum_buffer;
387 return TUSB_ERROR_NONE;
396 void usbd_dcd_bus_event_isr(uint8_t coreid, usbd_bus_event_type_t bus_event)
400 case USBD_BUS_EVENT_RESET :
401 case USBD_BUS_EVENT_UNPLUGGED :
403 osal_semaphore_reset(usbd_control_xfer_sem_hdl);
404 for (uint8_t class_code =
TUSB_CLASS_AUDIO; class_code < USBD_CLASS_DRIVER_COUNT; class_code++)
406 if ( usbd_class_drivers[class_code].close ) usbd_class_drivers[class_code].close( coreid );
410 case USBD_BUS_EVENT_SUSPENDED:
411 usbd_devices[coreid].state = TUSB_DEVICE_STATE_SUSPENDED;
420 usbd_task_event_t task_event =
423 .event_id = USBD_EVENTID_SETUP_RECEIVED,
426 task_event.setup_received = (*p_request);
427 osal_queue_send(usbd_queue_hdl, &task_event);
430 void usbd_xfer_isr(
endpoint_handle_t edpt_hdl, tusb_event_t event, uint32_t xferred_bytes)
432 if (edpt_hdl.class_code == 0 )
434 osal_semaphore_post( usbd_control_xfer_sem_hdl );
437 usbd_task_event_t task_event =
439 .coreid = edpt_hdl.coreid,
440 .event_id = USBD_EVENTID_XFER_DONE,
441 .sub_event_id =
event
444 task_event.xfer_done.xferred_byte = xferred_bytes;
445 task_event.xfer_done.edpt_hdl = edpt_hdl;
447 osal_queue_send(usbd_queue_hdl, &task_event);
USB Standard Interface Descriptor (section 9.6.1 table 9-12)
tusbd_descriptor_pointer_t tusbd_descriptor_pointers
uint8_t const ** p_string_arr
a array of pointers to string descriptors
#define TUSB_CFG_ATTR_USBRAM
USB Standard Configuration Descriptor (section 9.6.1 table 9-10) */.
uint8_t const * p_configuration
pointer to the whole configuration descriptor, starting by tusb_descriptor_configuration_t ...
uint8_t bInterfaceNumber
Number of this interface. Zero-based value identifying the index in the array of concurrent interface...
#define TUSB_CFG_OS_TASK_PRIO
If TUSB_CFG_OS is configured to use a real RTOS (other than TUSB_OS_NONE). This determines the priori...
tusb_error_t
Error Code returned.
uint8_t const * p_device
pointer to device descritpor tusb_descriptor_device_t
#define ATTR_ALIGNED(Bytes)
This attribute specifies a minimum alignment for the variable or structure field, measured in bytes...
tusb_std_descriptor_type_t
USB Descriptor Types (section 9.4 table 9-5)
uint8_t bInterfaceClass
Class code (assigned by the USB-IF).
USB Standard Device Descriptor (section 9.6.1, table 9-8)