Ejemplo n.º 1
0
static void _usb_otg_task(void* otg_handle)
{
    usb_otg_state_struct_t * usb_otg_struct_ptr = (usb_otg_state_struct_t *) otg_handle;
#if ((OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_SDK)&& (USE_RTOS))  
    OS_Event_wait(usb_otg_struct_ptr->otg_isr_event, USB_OTG_KHCI_ISR_EVENT | USB_OTG_MAX3353_ISR_EVENT, FALSE, 10);
    _usb_otg_callback_get_status((usb_otg_handle) otg_handle);
#else
    if(OS_Event_check_bit(usb_otg_struct_ptr->otg_isr_event, USB_OTG_KHCI_ISR_EVENT|USB_OTG_MAX3353_ISR_EVENT))
    {
        _usb_otg_callback_get_status((usb_otg_handle)otg_handle);
    }
#endif
    _usb_otg_sm(usb_otg_struct_ptr);

    /* Application events */
    if (OS_Event_check_bit(usb_otg_struct_ptr->otg_app_event, OTG_STATE_EVENT_MASK))
    {
        /* App indications */
        if (usb_otg_struct_ptr->init_struct->app_otg_callback)
        {
            usb_otg_struct_ptr->init_struct->app_otg_callback((usb_otg_handle) usb_otg_struct_ptr, usb_otg_struct_ptr->otg_app_event);
        }
        OS_Event_clear((usb_otg_struct_ptr->otg_app_event), OTG_STATE_EVENT_MASK);
    }
}
Ejemplo n.º 2
0
/* 
    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;
    }
}
Ejemplo n.º 3
0
/*FUNCTION*----------------------------------------------------------------
*
* Function Name  : usb_kbd_host_hid_recv_callback
* Returned Value : None
* Comments       :
*     Called when a interrupt pipe transfer is completed.
*
*END*--------------------------------------------------------------------*/
void usb_host_hid_keyboard_recv_callback(
/* [IN] pointer to pipe */
usb_pipe_handle pipe_handle,
/* [IN] user-defined parameter */
void *user_parm,
/* [IN] buffer address */
unsigned char *buffer,
/* [IN] length of data transferred */
uint32_t buflen,
/* [IN] status, hopefully USB_OK or USB_DONE */
uint32_t status)
{
    /* notify application that data are available */
    if(!OS_Event_check_bit(usb_keyboard_event, USB_Keyboard_Event_DATA))
    {
        OS_Event_set(usb_keyboard_event, USB_Keyboard_Event_DATA);
    }

}
Ejemplo n.º 4
0
/*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 */
}
Ejemplo n.º 5
0
usb_status usb_class_cdc_set_acm_ctrl_state
   (
      /* [IN] The communication device data instance structure */
      cdc_class_call_struct_t *            ccs_ptr,
      /* [IN] DTR state to set */
      uint8_t                           dtr,
      /* [IN] RTS state to set */
      uint8_t                           rts
   )
{ /* Body */
    usb_data_class_intf_struct_t * if_data_ptr;
    usb_acm_class_intf_struct_t *  if_acm_ptr;
    cdc_class_call_struct_t *          acm_instance;
    usb_status                     status = USBERR_NO_INTERFACE;
    os_event_handle                event = NULL;
    cdc_command_t                    cmd;

    #ifdef _HOST_DEBUG_
        DEBUG_LOG_TRACE("usb_class_cdc_set_acm_ctrl_state");
    #endif

    /* Validity checking: for data interface */
    if (usb_class_cdc_intf_validate(ccs_ptr)) {
        if_data_ptr = (usb_data_class_intf_struct_t *)ccs_ptr->class_intf_handle;
        acm_instance = if_data_ptr->BOUND_CONTROL_INTERFACE;
  
        if (usb_class_cdc_intf_validate(acm_instance)) {
            if_acm_ptr = (usb_acm_class_intf_struct_t *) if_data_ptr->BOUND_CONTROL_INTERFACE->class_intf_handle;
		    USB_CDC_ACM_lock();
            if (if_acm_ptr->acm_desc->bmCapabilities & USB_ACM_CAP_LINE_CODING) {
                event = if_acm_ptr->acm_event;
   
                if (event != NULL) {
                    usb_class_cdc_os_event_wait(event, USB_ACM_CTRL_PIPE_FREE | USB_ACM_DETACH, FALSE, 0);
					if (OS_Event_check_bit(event, USB_ACM_CTRL_PIPE_FREE))
						OS_Event_clear(event, USB_ACM_CTRL_PIPE_FREE);
					if (OS_Event_check_bit(event, USB_ACM_DETACH))
						OS_Event_clear(event, USB_ACM_DETACH);
                }
   
                if (usb_class_cdc_intf_validate(acm_instance)) {
                    cmd.CLASS_PTR = ccs_ptr;
                    cmd.CALLBACK_FN = (tr_callback) usb_class_cdc_ctrl_acm_callback;
                    cmd.CALLBACK_PARAM = acm_instance;
                    
                    if_acm_ptr->ctrl_state.state  = dtr ? USB_ACM_LINE_STATE_DTR : 0;
                    if_acm_ptr->ctrl_state.state |= rts ? USB_ACM_LINE_STATE_RTS : 0;
                    status = usb_class_cdc_cntrl_common(&cmd, if_acm_ptr,
                        REQ_TYPE_OUT | REQ_TYPE_CLASS | REQ_TYPE_INTERFACE,
                        USB_CDC_SET_CTRL_LINE_STATE, if_acm_ptr->ctrl_state.state, 0, NULL);
                }
            }
            else{
               status = USBERR_INVALID_BMREQ_TYPE;
			}
			USB_CDC_ACM_unlock();
        } /* Endif */
    } /* Endif */
    
    /* This piece of code cannot be executed if it is running in USB interrupt context */
    if (!status && event != NULL) {
        /* wait for command completion */
        usb_class_cdc_os_event_wait(event, USB_ACM_CTRL_PIPE_FREE | USB_ACM_DETACH, FALSE, 0);
		if (OS_Event_check_bit(event, USB_ACM_CTRL_PIPE_FREE))
			OS_Event_clear(event, USB_ACM_CTRL_PIPE_FREE);
		if (OS_Event_check_bit(event, USB_ACM_DETACH))
			OS_Event_clear(event, USB_ACM_DETACH);
        /* and unlock control pipe as it is automatic event */
        OS_Event_set(event, USB_ACM_CTRL_PIPE_FREE);
    }

    #ifdef _HOST_DEBUG_
    if (!status) {
        DEBUG_LOG_TRACE("usb_class_cdc_set_acm_ctrl_state, SUCCESSFUL");
    }
    else {
        DEBUG_LOG_TRACE("usb_class_cdc_set_acm_ctrl_state, FAILED");
    }
    #endif
 
    return status;
} /* Endbody */
Ejemplo n.º 6
0
/*FUNCTION*----------------------------------------------------------------
*
* Function Name  : main (Main_Task if using MQX)
* Returned Value : none
* Comments       :
*     Execution starts here
*
*END*--------------------------------------------------------------------*/
void keyboard_task(uint32_t param)
{
    usb_status status = USB_OK;
    static unsigned char *buffer;

    if(keyboard_inited == 0) 
    {
        g_hid_com = (hid_command_t*) OS_Mem_alloc_zero(sizeof(hid_command_t));
        buffer = (unsigned char *)OS_Mem_alloc_uncached(HID_KEYBOARD_BUFFER_SIZE);
        if (buffer == NULL) {
            printf("\nMemory allocation failed. STATUS: %x", status);
            //fflush(stdout);
            return;
        }
        keyboard_inited = 1;
    }

    if (OS_Event_check_bit(usb_keyboard_event, USB_Keyboard_Event_CTRL))
    {
        OS_Event_clear(usb_keyboard_event, USB_Keyboard_Event_CTRL);
        switch (g_kbd_hid_device.dev_state) {
            case USB_DEVICE_CONFIGURED:
                status = usb_host_open_dev_interface(host_handle, g_kbd_hid_device.dev_handle, g_kbd_hid_device.intf_handle, (void *) & g_kbd_hid_device.class_handle);
                if(status != USB_OK) {
                printf("\nError in _usb_hostdev_select_interface: %x", status);
                //fflush(stdout);
                return;
                }
                break;
            case USB_DEVICE_IDLE:
                break;
            case USB_DEVICE_INTERFACED:
                g_hid_com->class_ptr = g_kbd_hid_device.class_handle;
                g_hid_com->callback_fn = usb_host_hid_keyboard_recv_callback;
                g_hid_com->callback_param = 0;
                OS_Event_clear(usb_keyboard_event, USB_Keyboard_Event_DATA);
                status = usb_class_hid_recv_data(g_hid_com, (unsigned char *) buffer, HID_KEYBOARD_BUFFER_SIZE);
                if(status != USB_OK) {
                    printf("\nError in _usb_host_recv_data: %x", status);
                //fflush(stdout);
                }
                break;
        }
    }


    if (OS_Event_check_bit(usb_keyboard_event, USB_Keyboard_Event_DATA))
    {
        if (g_kbd_hid_device.dev_state == USB_DEVICE_INTERFACED) 
        {
            process_kbd_buffer((unsigned char *)buffer);
            OS_Event_clear(usb_keyboard_event, USB_Keyboard_Event_DATA);
            g_hid_com->class_ptr = g_kbd_hid_device.class_handle;
            g_hid_com->callback_fn = usb_host_hid_keyboard_recv_callback;
            g_hid_com->callback_param = 0;
            //printf("\nClass handle %x",  g_kbd_hid_device.class_handle);
            OS_Event_clear(usb_keyboard_event, USB_Keyboard_Event_DATA);
            status = usb_class_hid_recv_data(g_hid_com, (unsigned char *) buffer, HID_KEYBOARD_BUFFER_SIZE);
            if(status != USB_OK) {
                printf("\nError in _usb_host_recv_data: %x", status);
            }
        }
    }
#if (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_MQX)
    OS_Time_delay(1);
#endif
}
Ejemplo n.º 7
0
/* 
    video_camera_stream_task
*/
void video_camera_stream_task(void)
{
    usb_status              status = USB_OK;

    // Wait for insertion or removal event
    OS_Event_wait(g_video_camera.video_camera_stream_event, USB_EVENT_CTRL, FALSE, 0);
    if (OS_Event_check_bit(g_video_camera.video_camera_stream_event, USB_EVENT_CTRL))
        OS_Event_clear(g_video_camera.video_camera_stream_event, USB_EVENT_CTRL);
    
    switch ( g_video_camera.stream_state)
    {
        case USB_DEVICE_IDLE:
            break;

        case USB_DEVICE_ATTACHED:
            USB_PRINTF("Video device attached\r\n");
            g_video_camera.stream_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.stream_intf_handle, (class_handle*)&g_video_camera.video_stream_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_stream_handle = g_video_camera.video_stream_handle;
            //g_video_camera.stream_interface_status = (uint8_t)STREAM_INTERFACE_IDLE;
            break;

        case USB_DEVICE_INTERFACE_OPENED:
            if (g_video_camera.stream_interface_status == (uint8_t)STREAM_INTERFACE_IDLE)
            {
                g_video_camera.stream_interface_status = (uint8_t)STREAM_INTERFACE_SET_DEFAULT_INTRFACE;
            }
            else if (g_video_camera.stream_interface_status == (uint8_t)STREAM_INTERFACE_INTERFACE_OPENING)
            {
                g_video_camera.stream_interface_status = (uint8_t)STREAM_INTERFACE_FIND_OPTIMAL_SETTING;
            }
            else if (g_video_camera.stream_interface_status == (uint8_t)STREAM_INTERFACE_SET_INTERFACE)
            {
                g_video_camera.stream_interface_status = (uint8_t)STREAM_INTERFACE_STREAMING;
                g_video_camera.stream_state = USB_DEVICE_VIDEO_START;
                g_video_camera.stream_pipe_opened = 1;
                g_video_camera.stream_transfer.is_1ms = 0;
                g_video_camera.video_command_ptr->callback_param = &g_video_camera;
                g_video_camera.video_command_ptr->callback_fn = usb_host_video_stream_callback;
            }
            else
            {
                
            }
            break;
        case USB_DEVICE_INTERFACE_OPENING:
            break;
        case USB_DEVICE_VIDEO_START:
//            if(g_video_camera.stream_transfer.stream_transfer == 0)
//            {
//                g_video_camera.video_command_ptr->callback_param = &g_video_camera;
//                g_video_camera.video_command_ptr->callback_fn = usb_host_audio_stream_callback;
//                g_video_camera.stream_transfer.stream_transfer = 1;
//            }
//            else
//            {
//                
//            }
            break;
        case USB_DEVICE_DETACHED:
            status = usb_host_close_dev_interface(g_video_camera.host_handle, g_video_camera.dev_handle, g_video_camera.stream_intf_handle, g_video_camera.video_stream_handle);
            if (status != USB_OK)
            {
                USB_PRINTF("error in _usb_hostdev_close_interface %x\n", status);
            }
            g_video_camera.stream_pipe_opened = 0;
            g_video_camera.stream_interface_status = STREAM_INTERFACE_IDLE;
            g_video_camera.stream_intf_handle = NULL;
            g_video_camera.video_stream_handle = NULL;
            g_video_camera.stream_transfer.stream_transfer = 0;
            USB_PRINTF("Going to idle state\r\n");
            g_video_camera.stream_state = USB_DEVICE_IDLE;
            break;
        case USB_DEVICE_OTHER:
            break;
        default:
            break;
        } /* Endswitch */
}