⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 keyboard.c

📁 Cypress公司Cy7c6xxxx芯片的USB键盘及USB鼠标的firmware代码以及一些example代码。
💻 C
📖 第 1 页 / 共 4 页
字号:
{
    if(    ( ENDPOINT_A0_FIFO[USB_wLengthHi] == 0 )
        && ( ENDPOINT_A0_FIFO[USB_wLength] != 0 )
        && ( ENDPOINT_A0_FIFO[USB_wLength] < data_count) )
                data_count = ENDPOINT_A0_FIFO[USB_wLength];
    USB_send_buffer();
    if( premature_setup )
        return;
    EP_A0_COUNTER; /* this does a read of EP_A0_COUNTER */
    temp = DATATOGGLE | byte_count;
    do
    {
        for( EP_A0_COUNTER = temp; EP_A0_COUNTER == temp ;EP_A0_COUNTER = temp)
        {
            USB_set_ep0_mode(USB_MODE_ACK_IN_STATUS_OUT);
            if( premature_setup )
                return;

            do
            {
                while( (EP_A0_MODE & ~USB_MODE_MASK) == 0);
                if( EP_A0_MODE.OUT_RECEIVED )
                {
                    USB_set_ep0_mode(USB_MODE_STATUS_OUT);
                    return;
                }
                if( EP_A0_MODE.SETUP_RECEIVED )
                {
                    premature_setup=1;
                    return;
                }
            }
            while( !EP_A0_MODE.ACKNOWLEDGE );

            USB_send_buffer();
            if( premature_setup != 0)
                return;
            temp = ((EP_A0_COUNTER & DATATOGGLE) ^ DATATOGGLE) | byte_count;
        }
    }
    while( !EP_A0_MODE.SETUP_RECEIVED );
    premature_setup=1;
}

/* USB library main routines */
void ClearRemoteWakeup(void)
{
    USB_no_data_control();
    if ( premature_setup == 0)
    {
        if( ENDPOINT_A0_FIFO[USB_wValue] != USB_DEVICE_REMOTE_WAKEUP )
            USB_send_stall();
        else
            remote_wakeup_status = DISABLE_REMOTE_WAKEUP; /* disable remote wakeup */
    }
}

void SetRemoteWakeup(void)
{
    USB_no_data_control();
    if (premature_setup == 0)
    {
    if (ENDPOINT_A0_FIFO[USB_wValue] != USB_DEVICE_REMOTE_WAKEUP)
        USB_send_stall();
    else
        remote_wakeup_status = ENABLE_REMOTE_WAKEUP; /* enable remote wakeup */
    }
}

void SetConfiguration(void)
{
    AC=ENDPOINT_A0_FIFO[USB_wValue];
    if( (ENDPOINT_A0_FIFO[USB_wValue] != UNCONFIGURED) && (ENDPOINT_A0_FIFO[USB_wValue] != CONFIGURED) )
    {
        USB_send_stall();
    }
    else
    {
        configuration_status = ENDPOINT_A0_FIFO[USB_wValue]; /* load ENDPOINT_A0_FIFO[USB_wValue] lsb */
        endpoint_stall_status = 0; /* not stalled */
        if(configuration_status != UNCONFIGURED)
        {
            EP_A1_MODE = USB_MODE_NAK_IN; /* NAK IN packets until data is */
            ENDPOINT_INTERRUPT |= EPA1_ENABLE;          
            PORT_WRITE(PORT3,PORT3_S | P3_LED_MASK);
        }
        else
        {
            EP_A1_MODE = USB_MODE_DISABLE; /* Disable endpoint 1 */
            ENDPOINT_INTERRUPT &= ~EPA1_ENABLE; /* disable endpoint one interrupts */
            PORT_WRITE(PORT3,PORT3_S & ~P3_LED_MASK);
        }
        USB_no_data_control();
    }
}

void SetAddress(void)
{
    USB_no_data_control(); /* handshake with host */
    if (premature_setup == 0)
        USB_DEVICE_A = ENDPOINT_A0_FIFO[USB_wValue] | (1<<DEVICE_ADDRESS_ENABLE); /* write new USB device address */
}

void USB_send_stall(void)
{
    USB_set_ep0_mode(USB_MODE_STALL_IN_OUT);
}

void ClearEndpointStall(void)
{
    if (ENDPOINT_A0_FIFO[USB_wValue] != USB_ENDPOINT_STALL) /* load ENDPOINT_A0_FIFO[USB_wValue] (which feature) */
        USB_send_stall();
    else
    {
        USB_no_data_control(); /* handshake with host */
        if (premature_setup == 0)
        {
            endpoint_stall_status = 0; /* not stalled */
            EP_A1_COUNTER &= ~DATATOGGLE; /* clear data 0/1 bit */
            EP_A1_MODE = USB_MODE_NAK_IN; /* NAK IN packets until data until data is ready on endpoint one */
        }
    }
}

void SetEndpointStall(void)
{ 
    if (ENDPOINT_A0_FIFO[USB_wValue] != USB_ENDPOINT_STALL) /* load ENDPOINT_A0_FIFO[USB_wValue] */
        USB_send_stall();
    else
    {
        USB_no_data_control(); /* handshake with host */
        if (premature_setup == 0)
        {
            endpoint_stall_status = 1; /* stalled */
            EP_A1_MODE = USB_MODE_STALL_IN_OUT; /* stall endpoint one */
        }
    }
}

void GetDeviceStatus(void)
{
    data_count = 2; /* send two bytes */
    USB_send_buffer_ptr=get_dev_status_table+remote_wakeup_status;
    USB_control_read();
}

void GetDescriptor(void)
{
    switch (ENDPOINT_A0_FIFO[USB_wValueHi]) 
    {
        case USB_DEVICE :
            data_count = device_desc_table[0];
            USB_send_buffer_ptr=device_desc_table;
            break;
        case USB_CONFIGURATION :
            data_count = sizeof(config_desc_table) + sizeof(Interface_Descriptor) + sizeof(Class_Descriptor) + sizeof(Endpoint_Descriptor);
            USB_send_buffer_ptr=config_desc_table;
            break;
        case USB_STRING :
            switch(ENDPOINT_A0_FIFO[USB_wValue])
            {
            case 0x00 :
                data_count = sizeof(USBStringLanguageDescription);
                USB_send_buffer_ptr=USBStringLanguageDescription;
                break;
            case 0x01 :
                data_count = sizeof(USBStringDescription1);
                USB_send_buffer_ptr=USBStringDescription1;
                break;
            case 0x02 :
                data_count = sizeof(USBStringDescription2);
                USB_send_buffer_ptr=USBStringDescription2;
                break;
            case 0x03 :
                data_count = sizeof(USBStringDescription3);
                USB_send_buffer_ptr=USBStringDescription3;
                break;
            case 0x04 :
                data_count = sizeof(USBStringDescription4);
                USB_send_buffer_ptr=USBStringDescription4;
                break;
            case 0x05 :
                data_count = sizeof(USBStringDescription5);
                USB_send_buffer_ptr=USBStringDescription5;
                break;
            default       :
                USB_send_stall();
                return;
            }
            break;
        case USB_HID :
            data_count = sizeof(Class_Descriptor);
            USB_send_buffer_ptr=Class_Descriptor;
            break;
        case USB_REPORT :
            data_count = sizeof(hid_report_desc_table); 
            USB_send_buffer_ptr=hid_report_desc_table;
            break;
        default :
            USB_send_stall();
            return;
    }
    USB_control_read();
}

void GetConfiguration(void)
{
    data_count = sizeof(configuration_status);
    USB_send_buffer_ptr=&configuration_status;
    USB_control_read();
}

void GetInterfaceStatus(void)
{
    data_count = sizeof(get_interface_status_table);
    USB_send_buffer_ptr=get_interface_status_table;
    USB_control_read();
}

void GetEndpointStatus(void)
{
    data_count = 2; /* send two bytes */
    USB_send_buffer_ptr = get_endpoint_status_table + (endpoint_stall_status*2);
    USB_control_read();
}

void SetReport(void)
{
    USB_control_write();
    if( (premature_setup == 0) && (data_ok != 0) )
    {
        data_ok = 0;
        AC = PORT3_S | P3_LED_MASK; 
        PUSHA();
        if( (ENDPOINT_A0_FIFO[0] & 1) != 0 )
        {
            POPA();
            AC &= ~NUM_LOCK_LED;
            PUSHA();
        }
        if( (ENDPOINT_A0_FIFO[0] & 2) != 0)
        {
            POPA();
            AC &= ~CAPS_LOCK_LED;
            PUSHA();
        }
        if( (ENDPOINT_A0_FIFO[0] & 4) != 0)
        {
            POPA();
            AC &= ~SCROLL_LOCK_LED;
            PUSHA();
        }
        POPA();
        PORT_WRITE(PORT3,AC);
        USB_no_data_control();
    }
}       

void SetIdle(void)
{
    USB_no_data_control();
    if (premature_setup == 0)
    {
        new_idle_period = ENDPOINT_A0_FIFO[USB_wValueHi];
        if( (idle_period == 0) && (idle_period >= (idle_period_counter+2)) )
            idle_period = new_idle_period;
        else
            new_idle_flag=1;
    }
}

void SetProtocol(void)
{
    AC=ENDPOINT_A0_FIFO[USB_wValue];
    if( (ENDPOINT_A0_FIFO[USB_wValue] != BOOT_PROTOCOL) && (ENDPOINT_A0_FIFO[USB_wValue] != REPORT_PROTOCOL) )
        USB_send_stall();
    else
    {
        protocol_status = ENDPOINT_A0_FIFO[USB_wValue];
        USB_no_data_control();
    }
}

void GetReport(void)
{
    data_count = 8;
    if (kb_error_sent == 0)
        USB_send_buffer_ptr = error_code_buffer;
    else
        USB_send_buffer_ptr = report_buffer;
    USB_control_read();
}

void GetIdle(void)
{
    data_count = sizeof(new_idle_period);
    USB_send_buffer_ptr=&new_idle_period;
    USB_control_read();
}

void GetProtocol(void)
{
    data_count = sizeof(protocol_status); /* send one byte */
    USB_send_buffer_ptr=&protocol_status;
    USB_control_read();
}

enum request_function_index 
{
  USB_send_stall_index,
  ClearRemoteWakeup_index, SetRemoteWakeup_index, SetAddress_index, SetConfiguration_index,
  ClearEndpointStall_index, SetEndpointStall_index,
  GetDeviceStatus_index, GetDescriptor_index, GetConfiguration_index,
  GetInterfaceStatus_index,
  GetEndpointStatus_index,
  SetReport_index, SetIdle_index, SetProtocol_index,
  GetReport_index, GetIdle_index, GetProtocol_index
};

enum request_function_index function_index;

char far * table_ptr;

const char newtable[] = 
{
/*----------bmRequestType--------------*/  /*------bRequest------*/
/*DIRECTION        TYPE       RECIPIENT*/
/*#####################################*/
  HOST_TO_DEVICE | STANDARD | DEVICE       , USB_CLEAR_FEATURE    , ClearRemoteWakeup_index , /* 00 01 */
  HOST_TO_DEVICE | STANDARD | DEVICE       , USB_SET_FEATURE      , SetRemoteWakeup_index   , /* 00 03 */
  HOST_TO_DEVICE | STANDARD | DEVICE       , USB_SET_ADDRESS      , SetAddress_index        , /* 00 05 */
  HOST_TO_DEVICE | STANDARD | DEVICE       , USB_SET_CONFIGURATION, SetConfiguration_index  , /* 00 09 */
  HOST_TO_DEVICE | STANDARD | ENDPOINT     , USB_CLEAR_FEATURE    , ClearEndpointStall_index, /* 02 01 */
  HOST_TO_DEVICE | STANDARD | ENDPOINT     , USB_SET_FEATURE      , SetEndpointStall_index  , /* 02 03 */
  DEVICE_TO_HOST | STANDARD | DEVICE       , USB_GET_STATUS       , GetDeviceStatus_index   , /* 80 00 */
  DEVICE_TO_HOST | STANDARD | DEVICE       , USB_GET_DESCRIPTOR   , GetDescriptor_index     , /* 80 06 */
  DEVICE_TO_HOST | STANDARD | DEVICE       , USB_GET_CONFIGURATION, GetConfiguration_index  , /* 80 08 */
  DEVICE_TO_HOST | STANDARD | INTERFACE    , USB_GET_STATUS       , GetInterfaceStatus_index, /* 81 00 */
  DEVICE_TO_HOST | STANDARD | INTERFACE    , USB_GET_DESCRIPTOR   , GetDescriptor_index     , /* 81 06 */
  DEVICE_TO_HOST | STANDARD | ENDPOINT     , USB_GET_STATUS       , GetEndpointStatus_index , /* 82 00 */
  DEVICE_TO_HOST | STANDARD | ENDPOINT     , USB_GET_DESCRIPTOR   , GetDescriptor_index     , /* 82 06 */
  HOST_TO_DEVICE | CLASS    | INTERFACE    , USB_SET_REPORT       , SetReport_index         , /* 21 09 */
  HOST_TO_DEVICE | CLASS    | INTERFACE    , USB_SET_IDLE         , SetIdle_index           , /* 21 0A */
  HOST_TO_DEVICE | CLASS    | INTERFACE    , USB_SET_PROTOCOL     , SetProtocol_index       , /* 21 0B */
  HOST_TO_DEVICE | CLASS    | ENDPOINT     , USB_SET_REPORT       , SetReport_index         , /* 22 09 */
  HOST_TO_DEVICE | CLASS    | ENDPOINT     , USB_SET_IDLE         , SetIdle_index           , /* 22 0A */
  HOST_TO_DEVICE | CLASS    | ENDPOINT     , USB_SET_PROTOCOL     , SetProtocol_index       , /* 22 0B */
  DEVICE_TO_HOST | CLASS    | INTERFACE    , USB_GET_REPORT       , GetReport_index         , /* A1 01 */
  DEVICE_TO_HOST | CLASS    | INTERFACE    , USB_GET_IDLE         , GetIdle_index           , /* A1 02 */
  DEVICE_TO_HOST | CLASS    | INTERFACE    , USB_GET_PROTOCOL     , GetProtocol_index         /* A1 03 */
};


const void (* request_func_table[])(void)= 
{
   USB_send_stall,
   ClearRemoteWakeup, SetRemoteWakeup, SetAddress, SetConfiguration,
   ClearEndpointStall, SetEndpointStall,
   GetDeviceStatus, GetDescriptor, GetConfiguration,
   GetInterfaceStatus,
   GetEndpointStatus,
   SetReport, SetIdle, SetProtocol,
   GetReport, GetIdle, GetProtocol
};

void USB_stage_one(void)
{
    table_ptr = newtable;
    function_index = USB_send_stall_index;
    do
    {
        if( *table_ptr == ENDPOINT_A0_FIFO[USB_bmRequestType] )
        {
            if( *(table_ptr+1) == ENDPOINT_A0_FIFO[USB_bRequest] )
                function_index = *(table_ptr+2);
        }
        table_ptr += 3;
    }
    while( table_ptr < (newtable + sizeof(newtable)) ); 
    (*request_func_table[function_index])();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -