/*FUNCTION*---------------------------------------------------------------- * * Function Name : usb_class_video_control_pre_deinit * Returned Value : None * Comments : * This function is called by common class to initialize the class driver. It * is called in response to a select interface call by application * *END*--------------------------------------------------------------------*/ usb_status usb_class_video_control_pre_deinit ( /* [IN] the class driver handle */ class_handle handle ) { usb_video_control_struct_t* video_control_ptr = (usb_video_control_struct_t*)handle; usb_status status = USB_OK; if (video_control_ptr == NULL) { #ifdef _DEBUG USB_PRINTF("usb_class_video_control_pre_deinit fail\n"); #endif return USBERR_ERROR; } if (video_control_ptr->control_interrupt_in_pipe != NULL) { status = usb_host_cancel(video_control_ptr->host_handle, video_control_ptr->control_interrupt_in_pipe, NULL); if (status != USB_OK) { #ifdef _DEBUG USB_PRINTF("error in usb_class_video_control_pre_deinit to close pipe\n"); #endif } } //USB_PRINTF("Video class driver pre_deinit\n"); return USB_OK; } /* Endbody */
/*FUNCTION*------------------------------------------------------------- * * Function Name : usb_device_process_resume * Returned Value : USB_OK or error code * Comments : * Process Resume event * *END*-----------------------------------------------------------------*/ usb_status usb_device_assert_resume ( /* [IN] the USB_USB_dev_initialize state structure */ usb_device_handle handle ) { usb_dev_state_struct_t* usb_dev_ptr; usb_status error = USB_OK; if (handle == NULL) { #if _DEBUG USB_PRINTF("usb_device_assert_resume: handle is NULL\n"); #endif return USBERR_ERROR; } usb_dev_ptr = (usb_dev_state_struct_t*)handle; if ((usb_dev_ptr->usb_dev_interface)->dev_assert_resume != NULL) { error= (usb_dev_ptr->usb_dev_interface)->dev_assert_resume(usb_dev_ptr->controller_handle); } else { #if _DEBUG USB_PRINTF("usb_device_assert_resume: dev_assert_resume is NULL\n"); #endif error = USBERR_ERROR; } return error; }
/*FUNCTION*---------------------------------------------------------------- * * Function Name : usb_class_hub_deinit * Returned Value : None * Comments : * This function is called by common class to deinitialize the class driver. It * is called in response to a select interface call by application * *END*--------------------------------------------------------------------*/ usb_status usb_class_hub_deinit ( /* [IN] the class driver handle */ class_handle handle ) { usb_hub_class_struct_t* hub_class = (usb_hub_class_struct_t*)handle; usb_status status = USB_OK; if (hub_class == NULL) { USB_PRINTF("usb_class_hid_deinit fail\n"); return USBERR_ERROR; } if (hub_class->interrupt_pipe != NULL) { status = usb_host_close_pipe(hub_class->host_handle, hub_class->interrupt_pipe); if (status != USB_OK) { USB_PRINTF("error in usb_class_hid_deinit to close pipe\n"); } } OS_Mem_free(handle); //USB_PRINTF("HID class driver de-initialized\n"); return USB_OK; } /* Endbody */
/*FUNCTION*---------------------------------------------------------------- * * Function Name : usb_class_hid_pre_deinit * Returned Value : None * Comments : * This function is called by common class to initialize the class driver. It * is called in response to a select interface call by application * *END*--------------------------------------------------------------------*/ usb_status usb_class_hub_pre_deinit ( /* [IN] the class driver handle */ class_handle handle ) { usb_hub_class_struct_t* hub_class = (usb_hub_class_struct_t*)handle; usb_status status = USB_OK; if (hub_class == NULL) { USB_PRINTF("_usb_host_cancel_call_interface fail\n"); return USBERR_ERROR; } if (hub_class->interrupt_pipe != NULL) { status = usb_host_cancel(hub_class->host_handle, hub_class->interrupt_pipe, NULL); if (status != USB_OK) { USB_PRINTF("error in _usb_host_cancel_call_interface to close pipe\n"); } } //USB_PRINTF("HID class driver pre_deinit\n"); return USB_OK; } /* Endbody */
void time_init(void) { extern const hwtimer_devif_t kSystickDevif; extern const hwtimer_devif_t kPitDevif; #define HWTIMER_LL_DEVIF kPitDevif // Use hardware timer PIT #define HWTIMER_LL_SRCCLK kBusClock // Source Clock for PIT #define HWTIMER_LL_ID 1 #define HWTIMER_PERIOD 1000 // 1 ms interval if (kHwtimerSuccess != HWTIMER_SYS_Init(&hwtimer, &HWTIMER_LL_DEVIF, HWTIMER_LL_ID, 5, NULL)) { USB_PRINTF("\r\nError: hwtimer initialization.\r\n"); } if (kHwtimerSuccess != HWTIMER_SYS_SetPeriod(&hwtimer, HWTIMER_LL_SRCCLK, HWTIMER_PERIOD)) { USB_PRINTF("\r\nError: hwtimer set period.\r\n"); } if (kHwtimerSuccess != HWTIMER_SYS_RegisterCallback(&hwtimer, hwtimer_callback, NULL)) { USB_PRINTF("\r\nError: hwtimer callback registration.\r\n"); } if (kHwtimerSuccess != HWTIMER_SYS_Start(&hwtimer)) { USB_PRINTF("\r\nError: hwtimer start.\r\n"); } }
usb_status usb_class_audio_send_data ( /* [IN] audio control class interface pointer */ audio_command_t* audio_ptr, /* [IN] buffer pointer */ uint8_t * buffer, /* [IN] data length */ uint32_t buf_size ) { /* Body */ audio_stream_struct_t* audio_class; usb_status status = USBERR_ERROR; //uint16_t request_value; tr_struct_t* tr_ptr; //usb_audio_command_t* p_endpoint_command; if ((audio_ptr == NULL) || (audio_ptr->class_stream_handle == NULL)) { USB_PRINTF("input parameter error\n"); return USBERR_ERROR; } audio_class = (audio_stream_struct_t*)audio_ptr->class_stream_handle; if ((audio_class == NULL) || (buffer == NULL)) { USB_PRINTF("get audio class parameter error\n"); return USBERR_ERROR; } audio_class->send_callback = audio_ptr->callback_fn; audio_class->send_param = audio_ptr->callback_param; if (audio_class->dev_handle == NULL) { USB_PRINTF("get audio class dev handle error\n"); return USBERR_ERROR; } if (usb_host_get_tr(audio_class->host_handle, usb_class_audio_send_callback, audio_class, &tr_ptr) != USB_OK) { USB_PRINTF("error to get tr\n"); return USBERR_ERROR; } tr_ptr->tx_buffer = buffer; tr_ptr->tx_length = buf_size; status = usb_host_send_data(audio_class->host_handle, audio_class->iso_out_pipe, tr_ptr); if (status != USB_OK) { USB_PRINTF("\nError in _usb_host_send_data: %x", (unsigned int)status); usb_host_release_tr(audio_class->host_handle, tr_ptr); return USBERR_ERROR; } return USB_OK; } /* Endbody */
/*FUNCTION*---------------------------------------------------------------- * * Function Name : usb_class_video_control_recv_data * Returned Value : USB_OK if command has been passed on USB. * Comments : * This function is used to recv interrupt data * *END*--------------------------------------------------------------------*/ usb_status usb_class_video_control_recv_data ( /* [IN] Class Interface structure pointer */ video_command_t* com_ptr, /* [IN] The buffer address */ uint8_t * buffer, /* [IN] The buffer address */ uint16_t length ) { usb_video_control_struct_t* video_control_ptr; tr_struct_t* tr_ptr; usb_status status; if ((com_ptr == NULL) || (com_ptr->class_control_handle == NULL)) { return USBERR_ERROR; } video_control_ptr = (usb_video_control_struct_t*)com_ptr->class_control_handle; if ((video_control_ptr == NULL) || (buffer == NULL)) { #ifdef _DEBUG USB_PRINTF("input parameter error\n"); #endif return USBERR_ERROR; } video_control_ptr->recv_callback = com_ptr->callback_fn; video_control_ptr->recv_param = com_ptr->callback_param; if (video_control_ptr->dev_handle == NULL) { return USBERR_ERROR; } if (usb_host_get_tr(video_control_ptr->host_handle, usb_class_video_control_recv_callback, video_control_ptr, &tr_ptr) != USB_OK) { #ifdef _DEBUG USB_PRINTF("error to get tr\n"); #endif return USBERR_ERROR; } tr_ptr->rx_buffer = buffer; tr_ptr->rx_length = length; status = usb_host_recv_data(video_control_ptr->host_handle, video_control_ptr->control_interrupt_in_pipe, tr_ptr); if (status != USB_OK) { #ifdef _DEBUG USB_PRINTF("\nError in usb_class_video_recv_data: %x", status); #endif usb_host_release_tr(video_control_ptr->host_handle, tr_ptr); return USBERR_ERROR; } return USB_OK; }
/*FUNCTION*---------------------------------------------------------------- * * Function Name : usb_device_get_status * Returned Value : USB_OK or error code * Comments : * Provides API to access the USB internal state. * *END*--------------------------------------------------------------------*/ usb_status usb_device_get_status ( /* [IN] Handle to the USB device */ usb_device_handle handle, /* [IN] What to get the error of */ uint8_t component, /* [OUT] The requested error */ uint16_t* error ) { /* Body */ usb_dev_state_struct_t* usb_dev_ptr; usb_dev_ptr = (usb_dev_state_struct_t*)handle; OS_Mutex_lock(usb_dev_ptr->mutex); if (component & USB_STATUS_ENDPOINT) { if ((usb_dev_ptr->usb_dev_interface)->dev_get_endpoint_status != NULL) { (usb_dev_ptr->usb_dev_interface)->dev_get_endpoint_status(usb_dev_ptr->controller_handle, (uint8_t)(component),error); } else { #if _DEBUG USB_PRINTF("usb_device_get_status: DEV_GET_ENDPOINT_STATUS is NULL\n"); #endif OS_Mutex_unlock(usb_dev_ptr->mutex); return USBERR_ERROR; } } else { if ((usb_dev_ptr->usb_dev_interface)->dev_get_device_status != NULL) { (usb_dev_ptr->usb_dev_interface)->dev_get_device_status(usb_dev_ptr->controller_handle, (uint8_t)(component),error); } else { #if _DEBUG USB_PRINTF("usb_device_get_status: DEV_GET_DEVICE_STATUS is NULL\n"); #endif OS_Mutex_unlock(usb_dev_ptr->mutex); return USBERR_ERROR; } } OS_Mutex_unlock(usb_dev_ptr->mutex); return USB_OK; }
/****************************************************************************** * * @name Virtual_Com_App * * @brief * * @param None * * @return None * *****************************************************************************/ void Virtual_Com_App(void) { /* User Code */ if ((0 != g_recv_size) && (0xFFFFFFFF != g_recv_size)) { int32_t i; /* Copy Buffer to Send Buff */ for (i = 0; i < g_recv_size; i++) { //USB_PRINTF("Copied: %c\n", g_curr_recv_buf[i]); g_curr_send_buf[g_send_size++] = g_curr_recv_buf[i]; } g_recv_size = 0; } if (g_send_size) { uint8_t error; uint32_t size = g_send_size; g_send_size = 0; error = USB_Class_CDC_Send_Data(g_app_handle, DIC_BULK_IN_ENDPOINT, g_curr_send_buf, size); if (error != USB_OK) { /* Failure to send Data Handling code here */ } } #if USBCFG_DEV_KEEP_ALIVE_MODE #if (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_SDK) if( (waitfordatareceive)) { if(comopen == 1) { OS_Time_delay(30); comopen = 0; } USB_PRINTF("Enter lowpower\r\n"); usb_hal_khci_disable_interrupts((uint32_t)USB0, INTR_TOKDNE); POWER_SYS_SetMode(kDemoVlps, kPowerManagerPolicyAgreement); waitfordatareceive = 0; usb_hal_khci_enable_interrupts((uint32_t)USB0,INTR_TOKDNE); USB_PRINTF("Exit lowpower\r\n"); } #endif #endif return; }
/*FUNCTION*------------------------------------------------------------- * * Function Name : usb_device_init_endpoint * Returned Value : USB_OK or error code * Comments : * Initializes the endpoint and the data structures associated with the * endpoint * *END*-----------------------------------------------------------------*/ usb_status usb_device_init_endpoint ( /* [IN] the USB_USB_dev_initialize state structure */ usb_device_handle handle, /* [IN] the endpoint structure, include members such as endpoint number, * endpoint type, endpoint direction and the max packet size */ usb_ep_struct_t* ep_ptr, /* [IN] After all data is transfered, should we terminate the transfer * with a zero length packet if the last packet size == MAX_PACKET_SIZE? */ uint8_t flag ) { usb_status error = 0; usb_dev_state_struct_t* usb_dev_ptr; struct xd_struct xd; if (handle == NULL) { #if _DEBUG USB_PRINTF("_usb_device_shutdowna: handle is NULL\n"); #endif return USBERR_ERROR; } usb_dev_ptr = (usb_dev_state_struct_t*)handle; /* Initialize the transfer descriptor */ xd.ep_num = ep_ptr->ep_num; xd.bdirection = ep_ptr->direction; xd.wmaxpacketsize = (uint16_t)(ep_ptr->size & 0x0000FFFF); xd.ep_type = ep_ptr->type; xd.dont_zero_terminate = flag; xd.wtotallength = 0; xd.wsofar = 0; if ((usb_dev_ptr->usb_dev_interface)->dev_init_endoint != NULL) { error=(usb_dev_ptr->usb_dev_interface)->dev_init_endoint(usb_dev_ptr->controller_handle, &xd); } else { #if _DEBUG USB_PRINTF("usb_device_init_endpoint: DEV_INIT_ENDPOINT is NULL\n"); #endif return USBERR_ERROR; } return error; } /* EndBody */
/*FUNCTION*------------------------------------------------------------- * * Function Name : usb_device_set_address * Returned Value : USB_OK or error code * Comments : * Sets the device address as assigned by the host during enumeration * *END*-----------------------------------------------------------------*/ usb_status usb_device_set_address ( /* [IN] the USB_USB_dev_initialize state structure */ usb_device_handle handle, /* [IN] the USB address to be set in the hardware */ uint8_t address ) { usb_dev_state_struct_t* usb_dev_ptr; usb_status error; usb_dev_ptr = (usb_dev_state_struct_t*)handle; if ((usb_dev_ptr->usb_dev_interface)->dev_set_address != NULL) { error = (usb_dev_ptr->usb_dev_interface)->dev_set_address(usb_dev_ptr->controller_handle, address); return error; } else { #if _DEBUG USB_PRINTF("usb_device_set_address: DEV_SET_ADDRESS is NULL\n"); #endif return USBERR_ERROR; } }
/*FUNCTION*------------------------------------------------------------- * * Function Name : _usb_device_shutdown * Returned Value : USB_OK or error code * Comments : * Shutdown an initialized USB device * *END*-----------------------------------------------------------------*/ static usb_status _usb_device_shutdown ( /* [IN] the USB_USB_dev_initialize state structure */ usb_device_handle handle ) { usb_status error; usb_dev_state_struct_t* usb_dev_ptr; usb_dev_ptr = (usb_dev_state_struct_t*)handle; #if USBCFG_DEV_USE_TASK if (usb_dev_ptr->task_id != (uint32_t)OS_TASK_ERROR) { OS_Task_delete(usb_dev_ptr->task_id); } if (NULL != usb_dev_ptr->usb_dev_service_que) { OS_MsgQ_destroy(usb_dev_ptr->usb_dev_service_que); } #endif if (usb_dev_ptr->usb_dev_interface->dev_shutdown != NULL) { error = (usb_dev_ptr->usb_dev_interface)->dev_shutdown(usb_dev_ptr->controller_handle); return error; } else { #if _DEBUG USB_PRINTF("_usb_device_shutdown: DEV_SHUTDOWN is NULL\n"); #endif return USBERR_ERROR; } } /* EndBody */
/*FUNCTION*------------------------------------------------------------- * * Function Name : usb_device_register_desc_request_notify * Returned Value : USB_OK or error code * Comments : * * *END*-----------------------------------------------------------------*/ usb_status usb_device_register_desc_request_notify ( /* [IN] the USB_USB_dev_initialize state structure */ usb_device_handle handle, usb_desc_request_notify_struct_t* desc_request_notify_callback, void* desc_request_notify_param ) { usb_dev_state_struct_t* usb_dev_ptr; usb_status error = USB_OK; if (handle == NULL) { #if _DEBUG USB_PRINTF("usb_device_register_desc_request_notify\n"); #endif return USBERR_ERROR; } usb_dev_ptr = (usb_dev_state_struct_t*)handle; usb_dev_ptr->usb_framework.desc_notify_callback = desc_request_notify_callback; usb_dev_ptr->usb_framework.desc_notify_param = desc_request_notify_param; return error; }
/*FUNCTION*---------------------------------------------------------------- * * Function Name : usb_class_video_cntrl_callback * Returned Value : USB_OK if command has been passed on USB. * Comments : * This is the callback used when HID information is sent or received * *END*--------------------------------------------------------------------*/ static void usb_class_video_cntrl_callback ( /* [IN] Unused */ void* tr_ptr, /* [IN] Pointer to the class interface instance */ void* param, /* [IN] Data buffer */ uint8_t * buffer, /* [IN] Length of buffer */ uint32_t len, /* [IN] Error code (if any) */ usb_status status ) { /* Body */ usb_video_control_struct_t* video_control = (usb_video_control_struct_t*)param; if (usb_host_release_tr(video_control->host_handle, tr_ptr) != USB_OK) { #ifdef _DEBUG USB_PRINTF("_usb_host_release_tr failed\n"); #endif } video_control->in_setup = FALSE; if (video_control->ctrl_callback) { video_control->ctrl_callback(NULL, video_control->ctrl_param, buffer, len, status); } } /* Endbody */
static void usb_class_audio_send_callback ( /* [IN] Unused */ void* tr_ptr, /* [IN] Pointer to the class interface instance */ void* param, /* [IN] Data buffer */ uint8_t * buffer, /* [IN] Length of buffer */ uint32_t len, /* [IN] Error code (if any) */ usb_status status ) { /* Body */ audio_stream_struct_t* audio_class = (audio_stream_struct_t*)param; usb_status usbstatus; usbstatus = usb_host_release_tr(audio_class->host_handle, (tr_struct_t*)tr_ptr); if (usbstatus != USB_OK) { USB_PRINTF("_usb_host_release_tr failed:%x\n",(unsigned int)usbstatus); } if (audio_class->send_callback) { audio_class->send_callback(NULL, audio_class->send_param, buffer, len, status); } } /* Endbody */
/*FUNCTION*---------------------------------------------------------------- * * Function Name : usb_device_set_status * Returned Value : USB_OK or error code * Comments : * Provides API to set internal state * *END*--------------------------------------------------------------------*/ usb_status usb_device_set_status ( /* [IN] Handle to the usb device */ usb_device_handle handle, /* [IN] What to set the error of */ uint8_t component, /* [IN] What to set the error to */ uint16_t setting ) { usb_dev_state_struct_t* usb_dev_ptr; uint8_t error = USB_OK; usb_dev_ptr = (usb_dev_state_struct_t*)handle; OS_Mutex_lock(usb_dev_ptr->mutex); if ((usb_dev_ptr->usb_dev_interface)->dev_set_device_status != NULL) { (usb_dev_ptr->usb_dev_interface)->dev_set_device_status(usb_dev_ptr->controller_handle, (uint8_t)(component),setting); } else { #if _DEBUG USB_PRINTF("usb_device_set_status: dev_set_device_status is NULL\n"); #endif OS_Mutex_unlock(usb_dev_ptr->mutex); return USBERR_ERROR; } OS_Mutex_unlock(usb_dev_ptr->mutex); return error; } /* EndBody */
int usb_init(void) { int result; void *ctrl; int i, start_index = 0; struct usb_device *dev; gpio_init_usb(1); running=0; dev_index=0; asynch_allowed=1; usb_hub_reset(); /* first make all devices unknown */ for (i = 0; i < USB_MAX_DEVICE; i++) { memset(&usb_dev[i], 0, sizeof(struct usb_device)); usb_dev[i].devnum = -1; } /* init low_level USB */ #define CONFIG_USB_MAX_CONTROLLER_COUNT 1 for (i = 0; i < CONFIG_USB_MAX_CONTROLLER_COUNT; i++) { /* init low_level USB */ printf("USB%d: ", i); if (usb_lowlevel_init(i, USB_INIT_HOST, &ctrl)) { puts("lowlevel init failed\n"); continue; } /* * lowlevel init is OK, now scan the bus for devices * i.e. search HUBs and configure them */ start_index = dev_index; printf("scanning bus %d for devices... ", i); dev = usb_alloc_new_device(ctrl); /* * device 0 is always present * (root hub, so let it analyze) */ if (dev) usb_new_device(dev); if (start_index == dev_index) puts("No USB Device found\n"); else printf("%d USB Device(s) found\n", dev_index - start_index); running = 1; } USB_PRINTF("scan end\n"); if (!running) { puts("USB error: all controllers failed lowlevel init\n"); return -1; } return 0; }
static void usb_class_audio_control_recv_callback ( /* [IN] Unused */ void* tr_ptr, /* [IN] void* to the class interface instance */ void* param, /* [IN] Data buffer */ uint8_t * buffer, /* [IN] Length of buffer */ uint32_t len, /* [IN] Error code (if any) */ usb_status status ) { /* Body */ audio_control_struct_t* audio_class = (audio_control_struct_t*)param; if (usb_host_release_tr(audio_class->host_handle, (tr_struct_t*)tr_ptr) != USB_OK) { USB_PRINTF("_usb_host_release_tr failed\n"); } if (audio_class->interrupt_callback) { audio_class->interrupt_callback(NULL, audio_class->interrupt_callback_param, buffer, len, status); } } /* Endbody */
/* * submits a control message and waits for comletion (at least timeout * 1ms) * If timeout is 0, we don't wait for completion (used as example to set and * clear keyboards LEDs). For data transfers, (storage transfers) we don't * allow control messages with 0 timeout, by previousely resetting the flag * asynch_allowed (usb_disable_asynch(1)). * returns the transfered length if OK or -1 if error. The transfered length * and the current status are stored in the dev->act_len and dev->status. */ int usb_control_msg(struct usb_device *dev, unsigned int pipe, unsigned char request, unsigned char requesttype, unsigned short value, unsigned short index, void *data, unsigned short size, int timeout) { if((timeout==0)&&(!asynch_allowed)) /* request for a asynch control pipe is not allowed */ return -1; /* set setup command */ setup_packet.requesttype = requesttype; setup_packet.request = request; setup_packet.value = swap_16(value); setup_packet.index = swap_16(index); setup_packet.length = swap_16(size); USB_PRINTF("usb_control_msg: request: 0x%X, requesttype: 0x%X\nvalue 0x%X index 0x%X length 0x%X\n", request,requesttype,value,index,size); dev->status=USB_ST_NOT_PROC; /*not yet processed */ submit_control_msg(dev,pipe,data,size,&setup_packet); if(timeout==0) { return (int)size; } while(timeout--) { if(!((volatile unsigned long)dev->status & USB_ST_NOT_PROC)) break; wait_ms(1); } if(dev->status==0) return dev->act_len; else { return -1; } }
/*FUNCTION*---------------------------------------------------------------- * * Function Name : kbd_hid_get_buffer * Returned Value : none * Comments : * used to get the buffer to store the data transferred from device. * *END*--------------------------------------------------------------------*/ uint32_t kbd_hid_get_buffer(void) { uint8_t ep_no; usb_device_ep_struct_t *lpEp; /* first get the max packet size from interface info */ usb_device_interface_struct_t* lpHostIntf = (usb_device_interface_struct_t*) kbd_hid_get_interface(); for (ep_no = 0; ep_no < lpHostIntf->ep_count; ep_no++) { lpEp = &lpHostIntf->ep[ep_no]; if (((lpEp->lpEndpointDesc->bEndpointAddress & IN_ENDPOINT) != 0) && ((lpEp->lpEndpointDesc->bmAttributes & IRRPT_ENDPOINT) != 0)) { kbd_size = (USB_SHORT_UNALIGNED_LE_TO_HOST(lpEp->lpEndpointDesc->wMaxPacketSize) & PACKET_SIZE_MASK); break; } } if ((kbd_size != 0) && (kbd_buffer == NULL)) { kbd_buffer = (uint8_t*) OS_Mem_alloc_uncached_zero(kbd_size); if (kbd_buffer == NULL) { USB_PRINTF("allocate memory failed in hid_get_buffer\r\n"); return (uint32_t)(-1); } } return 0; }
/*FUNCTION*---------------------------------------------------------------- * * Function Name : usb_class_hub_cntrl_callback * Returned Value : USB_OK if command has been passed on USB. * Comments : * This is the callback used when hub information is sent or received * *END*--------------------------------------------------------------------*/ void usb_class_hub_int_callback ( /* [IN] Unused */ void* tr_ptr, /* [IN] Pointer to the class interface instance */ void* param, /* [IN] Data buffer */ uint8_t * buffer, /* [IN] Length of buffer */ uint32_t len, /* [IN] Error code (if any) */ usb_status status ) { usb_hub_class_struct_t* hub_class = (usb_hub_class_struct_t*)param; //USB_PRINTF("hub int\n"); if (usb_host_release_tr(hub_class->host_handle, tr_ptr) != USB_OK) { USB_PRINTF("_usb_host_release_tr failed\n"); } hub_class->in_interrupt = FALSE; if (hub_class->interrupt_callback) { hub_class->interrupt_callback(NULL, hub_class->interrupt_param, buffer, len, status); } }
/****************************************************************************** * * @name USB_App_Callback * * @brief This function handles the callback * * @param handle : handle to Identify the controller * @param event_type : value of the event * @param val : gives the configuration value * * @return None * *****************************************************************************/ void USB_App_Callback(uint8_t event_type, void* val,void* arg) { uint16_t interface_setting; uint8_t interface_alternate; uint8_t interface_num; if ((event_type == USB_DEV_EVENT_BUS_RESET) || (event_type == USB_DEV_EVENT_CONFIG_CHANGED)) { virtual_camera.attached=FALSE; if (USB_OK == USB_Class_Video_Get_Speed(virtual_camera.video_handle, &virtual_camera.app_speed)) { USB_Desc_Set_Speed(virtual_camera.video_handle, virtual_camera.app_speed); } } else if (event_type == USB_DEV_EVENT_ENUM_COMPLETE) { virtual_camera.attached=TRUE; USB_PRINTF("Virtual camera is working ... \r\n"); } else if (event_type == USB_DEV_EVENT_ERROR) { /* add user code for error handling */ } else if (event_type == USB_DEV_EVENT_INTERFACE_CHANGED) { interface_setting = *((uint16_t*)val); interface_alternate = (uint8_t)(interface_setting&0x00FF); interface_num = (uint8_t)((interface_setting>>8)&0x00FF); if (VIDEO_STREAM_IF_INDEX == interface_num) { if (0 != interface_alternate) { virtual_camera.start_send = TRUE; if (((uint8_t)TRUE == virtual_camera.attached) && ((uint8_t)TRUE == virtual_camera.start_send)) { if ((uint8_t)FALSE == virtual_camera.is_sending) { virtual_camera.is_sending = (uint8_t)TRUE; virtual_camera.frame_sent = 0; virtual_camera.frame_progress = 0; USB_Prepare_Data(); (void)USB_Class_Video_Send_Data(virtual_camera.video_handle,VIDEO_ISO_ENDPOINT,virtual_camera.image_buffer,virtual_camera.frame_send_length); } } } else { virtual_camera.transmit_type = 0; virtual_camera.start_send = (uint8_t)FALSE; if ((uint8_t)TRUE == virtual_camera.is_sending) { virtual_camera.is_sending = (uint8_t)FALSE; (void)USB_Class_Video_Cancel(virtual_camera.video_handle,VIDEO_ISO_ENDPOINT,USB_SEND); } } } }
/* video_camera_control_task */ void video_camera_control_task(void) { usb_status status = USB_OK; // Wait for insertion or removal event OS_Event_wait(g_video_camera.video_camera_control_event, USB_EVENT_CTRL, FALSE, 0); if (OS_Event_check_bit(g_video_camera.video_camera_control_event, USB_EVENT_CTRL)) OS_Event_clear(g_video_camera.video_camera_control_event, USB_EVENT_CTRL); switch ( g_video_camera.control_state) { case USB_DEVICE_IDLE: break; case USB_DEVICE_ATTACHED: USB_PRINTF("Video device attached\r\n"); g_video_camera.control_state = USB_DEVICE_SET_INTERFACE_STARTED; status = usb_host_open_dev_interface(g_video_camera.host_handle, g_video_camera.dev_handle, g_video_camera.control_intf_handle, (class_handle*)&g_video_camera.video_control_handle); if (status != USB_OK) { USB_PRINTF("\r\nError in _usb_hostdev_open_interface: %x\r\n", status); return; } g_video_camera.video_command_ptr->class_control_handle = g_video_camera.video_control_handle; break; case USB_DEVICE_INTERFACE_OPENED: break; case USB_DEVICE_DETACHED: status = usb_host_close_dev_interface(g_video_camera.host_handle, g_video_camera.dev_handle, g_video_camera.control_intf_handle, g_video_camera.video_control_handle); if (status != USB_OK) { USB_PRINTF("error in _usb_hostdev_close_interface %x\n", status); } g_video_camera.control_intf_handle = NULL; g_video_camera.video_control_handle = NULL; USB_PRINTF("Going to idle state\r\n"); g_video_camera.control_state = USB_DEVICE_IDLE; break; case USB_DEVICE_OTHER: break; default: break; } }
usb_status usb_host_hid_unsupported_device_event ( /* [IN] pointer to device instance */ usb_device_instance_handle dev_handle, /* [IN] pointer to interface descriptor */ usb_interface_descriptor_handle intf_handle, /* [IN] code number for event causing callback */ uint32_t event_code ) { usb_device_interface_struct_t* pDeviceIntf; interface_descriptor_t* intf_ptr; if (USB_ATTACH_INTF_NOT_SUPPORT == event_code) { pDeviceIntf = (usb_device_interface_struct_t*) intf_handle; intf_ptr = pDeviceIntf->lpinterfaceDesc; USB_PRINTF("----- Unsupported Interface of attached Device -----\r\n"); USB_PRINTF(" Interface Number = %d", intf_ptr->bInterfaceNumber); USB_PRINTF(" Alternate Setting = %d", intf_ptr->bAlternateSetting); USB_PRINTF(" Class = %d", intf_ptr->bInterfaceClass); USB_PRINTF(" SubClass = %d", intf_ptr->bInterfaceSubClass); USB_PRINTF(" Protocol = %d\r\n", intf_ptr->bInterfaceProtocol); } else if (USB_ATTACH_DEVICE_NOT_SUPPORT == event_code) { USB_PRINTF("----- Unsupported Device attached -----\r\n"); } return USB_OK; }
/*FUNCTION*------------------------------------------------------------- * * Function Name : usb_device_cancel_transfer * Returned Value : USB_OK or error code * Comments : * returns the status of the transaction on the specified endpoint. * *END*-----------------------------------------------------------------*/ usb_status usb_device_cancel_transfer ( /* [IN] the USB_USB_dev_initialize state structure */ usb_device_handle handle, /* [IN] the Endpoint number */ uint8_t ep_num, /* [IN] direction */ uint8_t direction ) { uint8_t error = USB_OK; usb_dev_state_struct_t* usb_dev_ptr; if (handle == NULL) { #if _DEBUG USB_PRINTF("_usb_device_shutdowna: handle is NULL\n"); #endif return USBERR_ERROR; } usb_dev_ptr = (usb_dev_state_struct_t*)handle; OS_Mutex_lock(usb_dev_ptr->mutex); /* Cancel transfer on the specified endpoint for the specified ** direction */ if ((usb_dev_ptr->usb_dev_interface)->dev_cancel_transfer != NULL) { error = (usb_dev_ptr->usb_dev_interface)->dev_cancel_transfer(usb_dev_ptr->controller_handle, ep_num, direction); } else { #if _DEBUG USB_PRINTF("usb_device_cancel_transfer: dev_cancel_transfer is NULL\n"); #endif OS_Mutex_unlock(usb_dev_ptr->mutex); return USBERR_ERROR; } OS_Mutex_unlock(usb_dev_ptr->mutex); return error; }
/*FUNCTION*---------------------------------------------------------------- * * Function Name : usb_host_hid_keyboard_ctrl_callback * Returned Value : None * Comments : * Called when a control pipe command is completed. * *END*--------------------------------------------------------------------*/ void usb_host_hid_keyboard_ctrl_callback ( /* [IN] no used */ void* unused, /* [IN] user-defined parameter */ void* user_parm, /* [IN] buffer address */ uint8_t * buffer, /* [IN] length of data transferred */ uint32_t buflen, /* [IN] status, hopefully USB_OK or USB_DONE */ usb_status status ) { if (status == USBERR_ENDPOINT_STALLED) { USB_PRINTF("\r\nHID control Request failed DEV_STATE 0x%x status 0x%x!\r\n", kbd_hid_device.DEV_STATE, status); } else if (status) { USB_PRINTF("\r\nHID control Request failed DEV_STATE 0x%x status 0x%x!\r\n", kbd_hid_device.DEV_STATE, status); } if (kbd_hid_device.DEV_STATE == USB_DEVICE_SETTING_PROTOCOL) { kbd_hid_device.state_change |= USB_STATE_CHANGE_INUSE; OS_Event_set(kbd_usb_event, USB_EVENT_STATE_CHANGE); USB_PRINTF("setting protocol done\r\n"); } else if (kbd_hid_device.DEV_STATE == USB_DEVICE_GET_REPORT_DESCRIPTOR) { kbd_hid_device.state_change |= USB_STATE_CHANGE_DESCRIPTOR_DONE; OS_Event_set(kbd_usb_event, USB_EVENT_STATE_CHANGE); USB_PRINTF("get report descriptor done\r\n"); } /* notify application that status has changed */ OS_Event_set(kbd_usb_event, USB_EVENT_CTRL); }
/**************************************************************************//*! * * @name USB_PHDC_Requests * * @brief The function provides flexibility to add class and vendor specific * requests * * @param handle * @param setup_packet: setup packet received * @param data: data to be send back * @param size: size to be returned * * @return status * USB_OK : When Successfully * Others : Errors ****************************************************************************** * The function provides flexibility to add class and vendor specific requests *****************************************************************************/ usb_status USB_PHDC_Requests ( usb_setup_struct_t *setup_packet,/* [IN] setup packet received */ uint8_t **data, /* [OUT] data to be send back */ uint32_t *size, /* [OUT] size to be returned */ void* arg ) { usb_status error = USBERR_INVALID_REQ_TYPE; phdc_device_struct_t *devicePtr = (phdc_device_struct_t*)arg; if (devicePtr == NULL) { #if _DEBUG USB_PRINTF("USB_PHDC_Requests:phdc_object_ptr is NULL\n"); #endif return error; } if ((setup_packet->request_type & USB_DEV_REQ_STD_REQUEST_TYPE_TYPE_POS) == USB_DEV_REQ_STD_REQUEST_TYPE_TYPE_CLASS) { /*class request so handle it here */ *size = 0; error = USB_OK; switch (setup_packet->request) { case SET_FEATURE_REQUEST: case CLEAR_FEATURE_REQUEST: { #if USB_METADATA_SUPPORTED /* set/clear meta data feature */ devicePtr->phdc_metadata = (bool)((setup_packet->request & USB_SET_REQUEST_MASK) >> 1); /* inform the application that feature has changed */ if (devicePtr->phdc_application_callback.callback != NULL) { devicePtr->phdc_application_callback.callback(USB_DEV_EVENT_FEATURE_CHANGED, (void*)(&devicePtr->phdc_metadata), devicePtr->phdc_application_callback.arg); } #endif break; } case GET_STATUS_REQUEST: { /*implement get status request to get which endpoint has data*/ *data = (uint8_t *)(&devicePtr->phdc_ep_has_data); *size = setup_packet->length; break; } default: { break; } } }
/***************************************************************************//** * @brief * Print USB device strings. * * @param[in] buf * Place holder for device strings, no check on buffer size. ******************************************************************************/ static void PrintDeviceStrings(uint8_t *buf) { /* Get and print device string descriptors. */ if (device.devDesc.iManufacturer) { USBH_GetStringB(&device, buf, 255, device.devDesc.iManufacturer, USB_LANGID_ENUS); USBH_PrintString("\n\niManufacturer = \"", (USB_StringDescriptor_TypeDef*) buf, "\""); } else { USB_PRINTF("\n\niManufacturer = <NONE>"); } if (device.devDesc.iProduct) { USBH_GetStringB(&device, buf, 255, device.devDesc.iProduct, USB_LANGID_ENUS); USBH_PrintString("\niProduct = \"", (USB_StringDescriptor_TypeDef*) buf, "\""); } else { USB_PRINTF("\niProduct = <NONE>"); } if (device.devDesc.iSerialNumber) { USBH_GetStringB(&device, buf, 255, device.devDesc.iSerialNumber, USB_LANGID_ENUS); USBH_PrintString("\niSerialNumber = \"", (USB_StringDescriptor_TypeDef*) buf, "\"\n"); } else { USB_PRINTF("\niSerialNumber = <NONE>\n"); } }
void time_init(void) { if (kHwtimerSuccess != HWTIMER_SYS_Init(&hwtimer, &HWTIMER_LL_DEVIF, HWTIMER_LL_ID, NULL)) { USB_PRINTF("\r\nError: hwtimer initialization.\r\n"); } if (kHwtimerSuccess != HWTIMER_SYS_SetPeriod(&hwtimer, HWTIMER_PERIOD)) { USB_PRINTF("\r\nError: hwtimer set period.\r\n"); } if (kHwtimerSuccess != HWTIMER_SYS_RegisterCallback(&hwtimer, hwtimer_callback, NULL)) { USB_PRINTF("\r\nError: hwtimer callback registration.\r\n"); } if (kHwtimerSuccess != HWTIMER_SYS_Start(&hwtimer)) { USB_PRINTF("\r\nError: hwtimer start.\r\n"); } OS_intr_init(HWTIMER_IRQ_NUM, HWTIMER_IRQ_PRI, 0, TRUE); }
/* usb_init */ void usb_init(void) { usb_status status = USB_OK; status = usb_host_init(CONTROLLER_ID, &g_video_camera.host_handle); if (status != USB_OK) { USB_PRINTF("\r\nUSB Host Initialization failed! STATUS: 0x%x", status); return; } /* ** since we are going to act as the host driver, register the driver ** information for wanted class/subclass/protocols */ status = usb_host_register_driver_info(g_video_camera.host_handle, (void *)DriverInfoTable); if (status != USB_OK) { USB_PRINTF("\r\nUSB Initialization driver info failed! STATUS: 0x%x", status); return; } status = usb_host_register_unsupported_device_notify(g_video_camera.host_handle, usb_host_video_unsupported_device_event); if (status != USB_OK) { USB_PRINTF("\r\nUSB Initialization driver info failed! STATUS: 0x%x", status); return; } g_video_camera.video_camera_control_event = OS_Event_create(0);/* manually clear */ if (g_video_camera.video_camera_control_event == NULL) { USB_PRINTF("\r\nOS_Event_create failed!\r\n"); return; } g_video_camera.video_camera_stream_event = OS_Event_create(0);/* manually clear */ if (g_video_camera.video_camera_stream_event == NULL) { USB_PRINTF("\r\nOS_Event_create failed!\r\n"); return; } g_video_camera.video_command_ptr = (video_command_t*)OS_Mem_alloc_zero(sizeof(video_command_t)); if (g_video_camera.video_command_ptr == NULL) { USB_PRINTF("\r\nOS_Mem_alloc_zero failed!\r\n"); return; } g_video_camera.stream_interface_alternate = 0; time_init(); USB_PRINTF("Video camera starting...\r\n"); }