static usb_status USB_Phdc_Free_Handle(phdc_device_struct_t* handle) { int32_t cnt = 0; for (;cnt< MAX_PHDC_DEVICE;cnt++) { if ((&g_phdc_class[cnt]) == handle) { OS_Mem_zero((void*)handle, sizeof(phdc_device_struct_t)); return USB_OK; } } return USBERR_INVALID_PARAM; }
static usb_status USB_Cdc_Free_Handle(cdc_handle_t handle) { int32_t cnt = 0; for (; cnt < MAX_CDC_DEVICE; cnt++) { if (((cdc_handle_t)&cdc_device_array[cnt]) == handle) { OS_Mem_zero((void * )handle, sizeof(cdc_device_struct_t)); return USB_OK; } } return USBERR_INVALID_PARAM; }
/*FUNCTION*------------------------------------------------------------- * * Function Name : _usb_device_get_handle * Returned Value : NULL * Comments : * This function is used to get one unused device object * *END*-----------------------------------------------------------------*/ static usb_dev_state_struct_t* _usb_device_get_handle ( void ) { uint8_t i = 0; for (; i < USBCFG_DEV_NUM; i++) { if (g_usb_dev[i].occupied != USB_DEV_HANDLE_OCCUPIED) { OS_Mem_zero(&g_usb_dev[i], sizeof(usb_dev_state_struct_t)); g_usb_dev[i].occupied = USB_DEV_HANDLE_OCCUPIED; return &g_usb_dev[i]; } } return NULL; }
/***************************************************************************** * * @name TestApp_Init * * @brief This function is the entry for Audio generator * * @param None * * @return None ** *****************************************************************************/ void APP_init(void) { printf("Hid_audio_TestApp_Init\n"); audio_config_callback.composite_application_callback.callback = Audio_USB_App_Callback; audio_config_callback.composite_application_callback.arg = &g_audio_handle; audio_config_callback.class_specific_callback.callback = Audio_App_Param_Callback; audio_config_callback.class_specific_callback.arg = &g_audio_handle; audio_config_callback.desc_callback_ptr = &desc_callback; audio_config_callback.type = USB_CLASS_AUDIO; /* initialize the Global Variable Structure */ OS_Mem_zero(&g_mouse, sizeof(MOUSE_GLOBAL_VARIABLE_STRUCT)); /* Initialize the USB interface */ Hid_config_callback.composite_application_callback.callback = Hid_App_Callback; Hid_config_callback.composite_application_callback.arg = &g_mouse.app_handle; Hid_config_callback.class_specific_callback.callback = Hid_App_Param_Callback; Hid_config_callback.class_specific_callback.arg = &g_mouse.app_handle; Hid_config_callback.desc_callback_ptr = &desc_callback; Hid_config_callback.type = USB_CLASS_HID; usb_composite_callback.count = 2; usb_composite_callback.class_app_callback = composite_cfg; usb_composite_callback.class_app_callback[0] = audio_config_callback; usb_composite_callback.class_app_callback[1] = Hid_config_callback; /* Initialize the USB interface */ USB_Composite_Init(CONTROLLER_ID, &usb_composite_callback, &g_app_handle); USB_Composite_Get_Class_Handle(g_app_handle, USB_CLASS_HID, &g_mouse.app_handle); USB_Composite_Get_Class_Handle(g_app_handle, USB_CLASS_AUDIO, &g_audio_handle); Hid_TestApp_Init(); Audio_TestApp_Init(); // while(1); }
/*FUNCTION*---------------------------------------------------------------- * * Function Name : Keyboard_Task * Returned Value : none * Comments : * Execution starts here * *END*--------------------------------------------------------------------*/ void Keyboard_Task(void* param) { usb_status status = USB_OK; static uint8_t i = 0; usb_host_handle kbd_host_handle = (usb_host_handle) param; uint8_t j = 0; // Wait for insertion or removal event OS_Event_wait(kbd_usb_event, USB_EVENT_CTRL | USB_EVENT_DATA | USB_EVENT_DATA_CORRUPTED | USB_EVEN_OUTPUT | USB_EVENT_STATE_CHANGE, FALSE, 0); if (OS_Event_check_bit(kbd_usb_event, USB_EVENT_STATE_CHANGE)) { OS_Event_clear(kbd_usb_event, USB_EVENT_STATE_CHANGE); update_state(); } if (OS_Event_check_bit(kbd_usb_event, USB_EVEN_OUTPUT)) { OS_Event_clear(kbd_usb_event, USB_EVEN_OUTPUT); if (detach == 1) { /*clear the keyboard flag value*/ for (j = 8 - 1; j >= 2; j--) { flag[j] = 0; } } for (j = 2; j < 8; j++) { if ((buffer_timer[j] == flag[j]) && ((g_current_time - flag_time[j]) > KB_CONTINUE_PRESS)) { if (flag[j] > 1) { usb_host_hid_keyboard_output_keyvalue(flag[j]); } } } if (!OS_Event_check_bit(kbd_usb_event, USB_EVENT_CTRL | USB_EVENT_DATA | USB_EVENT_DATA_CORRUPTED)) { return; } } if (OS_Event_check_bit(kbd_usb_event, USB_EVENT_CTRL)) { OS_Event_clear(kbd_usb_event, USB_EVENT_CTRL); } switch(kbd_hid_device.DEV_STATE) { case USB_DEVICE_IDLE: break; case USB_DEVICE_ATTACHED: USB_PRINTF("\r\nKeyboard device attached\r\n"); kbd_hid_device.DEV_STATE = USB_DEVICE_SET_INTERFACE_STARTED; status = usb_host_open_dev_interface(kbd_host_handle, kbd_hid_device.DEV_HANDLE, kbd_hid_device.INTF_HANDLE, (usb_class_handle*) &kbd_hid_device.CLASS_HANDLE); if (status != USB_OK) { USB_PRINTF("\r\nError in _usb_hostdev_open_interface: %x\r\n", status); return; } break; case USB_DEVICE_INTERFACE_OPENED: kbd_hid_get_report_descriptor(); break; case USB_DEVICE_GET_REPORT_DESCRIPTOR_DONE: kbd_hid_get_buffer(); USB_PRINTF("Keyboard interfaced, setting protocol...\r\n"); /* now we will set the USB Hid standard boot protocol */ kbd_hid_device.DEV_STATE = USB_DEVICE_SETTING_PROTOCOL; kbd_hid_com->class_ptr = kbd_hid_device.CLASS_HANDLE; kbd_hid_com->callback_fn = usb_host_hid_keyboard_ctrl_callback; kbd_hid_com->callback_param = 0; status = usb_class_hid_set_protocol(kbd_hid_com, USB_PROTOCOL_HID_KEYBOARD); if (status != USB_OK) { USB_PRINTF("\r\nError in usb_class_hid_set_protocol: %x\r\n", status); } break; case USB_DEVICE_SET_INTERFACE_STARTED: break; case USB_DEVICE_INUSE: //USB_PRINTF("%d\n", data_received); i++; if (kbd_data_received) { /****************************************************************** Initiate a transfer request on the interrupt pipe ******************************************************************/ kbd_hid_com->class_ptr = kbd_hid_device.CLASS_HANDLE; kbd_hid_com->callback_fn = usb_host_hid_keyboard_recv_callback; kbd_hid_com->callback_param = 0; if (i == 4) { sleep_test = TRUE; } status = usb_class_hid_recv_data(kbd_hid_com, kbd_buffer, kbd_size); if (sleep_test) { sleep_test = FALSE; } if (status != USB_OK) { USB_PRINTF("\r\nError in usb_class_hid_recv_data: %x", status); } else { //USB_PRINTF("try to get recv data\n"); kbd_data_received = 0; } } /* Wait until we get the data from keyboard. */ if (OS_Event_wait(kbd_usb_event, USB_EVENT_CTRL | USB_EVENT_DATA | USB_EVENT_DATA_CORRUPTED, FALSE, 0) == OS_EVENT_OK) { if (OS_Event_check_bit(kbd_usb_event, USB_EVENT_DATA)) { //USB_PRINTF("get data\n"); OS_Event_clear(kbd_usb_event, USB_EVENT_DATA); kbd_data_received = 1; if (kbd_hid_device.DEV_STATE == USB_DEVICE_INUSE) { process_kbd_buffer((uint8_t *) kbd_buffer); } status = usb_class_hid_recv_data(kbd_hid_com, kbd_buffer, kbd_size); if (status != USB_OK) { USB_PRINTF("\r\nError in usb_class_hid_recv_data: %x", status); } else { //USB_PRINTF("try to get recv data\n"); kbd_data_received = 0; } } if (OS_Event_check_bit(kbd_usb_event, USB_EVENT_DATA_CORRUPTED)) { //USB_PRINTF("get corrupted\n"); kbd_data_received = 1; OS_Event_clear(kbd_usb_event, USB_EVENT_DATA_CORRUPTED); status = usb_class_hid_recv_data(kbd_hid_com, kbd_buffer, kbd_size); if (status != USB_OK) { USB_PRINTF("\r\nError in usb_class_hid_recv_data: %x", status); } else { //USB_PRINTF("try to get recv data\n"); kbd_data_received = 0; } } if (OS_Event_check_bit(kbd_usb_event, USB_EVENT_CTRL)) { //USB_PRINTF("get control\n"); //OS_Event_set(USB_Event, USB_EVENT_CTRL); } } break; case USB_DEVICE_DETACHED: if (kbd_data_received == 1) { status = usb_host_close_dev_interface(kbd_host_handle, kbd_hid_device.DEV_HANDLE, kbd_hid_device.INTF_HANDLE, kbd_hid_device.CLASS_HANDLE); if (status != USB_OK) { USB_PRINTF("error in _usb_hostdev_close_interface %x\r\n", status); } kbd_hid_device.INTF_HANDLE = NULL; kbd_hid_device.CLASS_HANDLE = NULL; USB_PRINTF("Going to idle state\r\n"); kbd_hid_device.DEV_STATE = USB_DEVICE_IDLE; if (kbd_reportDescriptor != NULL) { OS_Mem_free(kbd_reportDescriptor); kbd_reportDescriptor = NULL; } if (kbd_buffer != NULL) { OS_Mem_free(kbd_buffer); kbd_buffer = NULL; } } else { if (OS_Event_wait(kbd_usb_event, USB_EVENT_CTRL | USB_EVENT_DATA | USB_EVENT_DATA_CORRUPTED, FALSE, 0) == OS_EVENT_OK) { if (OS_Event_check_bit(kbd_usb_event, USB_EVENT_DATA)) { //USB_PRINTF("get data\n"); OS_Event_clear(kbd_usb_event, USB_EVENT_DATA); kbd_data_received = 1; if (kbd_hid_device.DEV_STATE == USB_DEVICE_INUSE) { process_kbd_buffer((uint8_t *) kbd_buffer); OS_Mem_zero(kbd_buffer, kbd_size); } } if (OS_Event_check_bit(kbd_usb_event, USB_EVENT_DATA_CORRUPTED)) { //USB_PRINTF("get corrupted\n"); kbd_data_received = 1; OS_Event_clear(kbd_usb_event, USB_EVENT_DATA_CORRUPTED); } if (OS_Event_check_bit(kbd_usb_event, USB_EVENT_CTRL)) { //USB_PRINTF("get control\n"); //OS_Event_set(USB_Event, USB_EVENT_CTRL); } } OS_Event_set(kbd_usb_event, USB_EVENT_CTRL); } break; case USB_DEVICE_OTHER: break; default: break; } /* Endswitch */ }
/***************************************************************************** * * @name APP_init * * @brief This function do initialization for APP. * * @param None * * @return None ** *****************************************************************************/ void USB_init() { class_config_struct_t* cdc_vcom_config_callback_handle; class_config_struct_t* msc_disk_config_callback_handle; cdc_vcom_preinit(); msc_disk_preinit(); #if (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_MQX) if (NULL == g_composite_device) { g_composite_device = OS_Mem_alloc_uncached_zero(sizeof(composite_device_struct_t)); } /* cdc vcom device */ cdc_vcom_config_callback_handle = &g_composite_device->composite_device_config_list[CDC_VCOM_INTERFACE_INDEX]; cdc_vcom_config_callback_handle->composite_application_callback.callback = VCom_USB_App_Device_Callback; cdc_vcom_config_callback_handle->composite_application_callback.arg = &g_composite_device->cdc_vcom; cdc_vcom_config_callback_handle->class_specific_callback.callback = (usb_class_specific_handler_func) VCom_USB_App_Class_Callback; cdc_vcom_config_callback_handle->class_specific_callback.arg = &g_composite_device->cdc_vcom; cdc_vcom_config_callback_handle->desc_callback_ptr = &desc_callback; cdc_vcom_config_callback_handle->type = USB_CLASS_CDC; /* msc disk device */ msc_disk_config_callback_handle = &g_composite_device->composite_device_config_list[MSC_DISK_INTERFACE_INDEX]; msc_disk_config_callback_handle->composite_application_callback.callback = Disk_USB_App_Device_Callback; msc_disk_config_callback_handle->composite_application_callback.arg = &g_composite_device->msc_disk; msc_disk_config_callback_handle->class_specific_callback.callback = (usb_class_specific_handler_func) Disk_USB_App_Class_Callback; msc_disk_config_callback_handle->class_specific_callback.arg = &g_composite_device->msc_disk; msc_disk_config_callback_handle->desc_callback_ptr = &desc_callback; msc_disk_config_callback_handle->type = USB_CLASS_MSC; OS_Mem_zero(&g_composite_device->msc_disk, sizeof(disk_struct_t)); g_composite_device->composite_device_config_callback.count = 2; g_composite_device->composite_device_config_callback.class_app_callback = g_composite_device->composite_device_config_list; msc_disk_init(&g_composite_device->msc_disk); /* Initialize the USB interface */ USB_Composite_Init(CONTROLLER_ID, &g_composite_device->composite_device_config_callback, &g_composite_device->composite_device); g_composite_device->cdc_vcom = (cdc_handle_t) g_composite_device->composite_device_config_list[CDC_VCOM_INTERFACE_INDEX].class_handle; g_composite_device->msc_disk.app_handle = (msd_handle_t) g_composite_device->composite_device_config_list[MSC_DISK_INTERFACE_INDEX].class_handle; cdc_vcom_init(&g_composite_device->cdc_vcom); #else /* cdc vcom device */ cdc_vcom_config_callback_handle = &g_composite_device.composite_device_config_list[CDC_VCOM_INTERFACE_INDEX]; cdc_vcom_config_callback_handle->composite_application_callback.callback = VCom_USB_App_Device_Callback; cdc_vcom_config_callback_handle->composite_application_callback.arg = &g_composite_device.cdc_vcom; cdc_vcom_config_callback_handle->class_specific_callback.callback = (usb_class_specific_handler_func) VCom_USB_App_Class_Callback; cdc_vcom_config_callback_handle->class_specific_callback.arg = &g_composite_device.cdc_vcom; cdc_vcom_config_callback_handle->desc_callback_ptr = &desc_callback; cdc_vcom_config_callback_handle->type = USB_CLASS_CDC; /* msc disk device */ msc_disk_config_callback_handle = &g_composite_device.composite_device_config_list[MSC_DISK_INTERFACE_INDEX]; msc_disk_config_callback_handle->composite_application_callback.callback = Disk_USB_App_Device_Callback; msc_disk_config_callback_handle->composite_application_callback.arg = &g_composite_device.msc_disk; msc_disk_config_callback_handle->class_specific_callback.callback = (usb_class_specific_handler_func) Disk_USB_App_Class_Callback; msc_disk_config_callback_handle->class_specific_callback.arg = &g_composite_device.msc_disk; msc_disk_config_callback_handle->desc_callback_ptr = &desc_callback; msc_disk_config_callback_handle->type = USB_CLASS_MSC; OS_Mem_zero(&g_composite_device.msc_disk, sizeof(disk_struct_t)); g_composite_device.composite_device_config_callback.count = 2; g_composite_device.composite_device_config_callback.class_app_callback = g_composite_device.composite_device_config_list; msc_disk_init(&g_composite_device.msc_disk); /* Initialize the USB interface */ USB_Composite_Init(CONTROLLER_ID, &g_composite_device.composite_device_config_callback, &g_composite_device.composite_device); g_composite_device.cdc_vcom = (cdc_handle_t) g_composite_device.composite_device_config_list[CDC_VCOM_INTERFACE_INDEX].class_handle; g_composite_device.msc_disk.app_handle = (msd_handle_t) g_composite_device.composite_device_config_list[MSC_DISK_INTERFACE_INDEX].class_handle; cdc_vcom_init(&g_composite_device.cdc_vcom); #endif }