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

📄 mouse.c

📁 Cypress公司Cy7c6xxxx芯片的USB键盘及USB鼠标的firmware代码以及一些example代码。
💻 C
📖 第 1 页 / 共 3 页
字号:
        USB_send_stall();
    }
    else
    {
        configuration_status = ENDPOINT_A0_FIFO[USB_wValue]; /* load ENDPOINT_A0_FIFO[USB_wValue] lsb */
        endpoint_stall_status = 0; /* not stalled */
        USB_EP1_TX_CONFIG &= ~DATATOGGLE;   
        if(configuration_status != UNCONFIGURED)
        { 
            USB_EP1_TX_CONFIG = (USB_EP1_TX_CONFIG & 0x7f) | 0x10;
            interrupt_mask |= 0x10;
            USB_STATUS &= 0xef;
        }
        else
        {
            USB_EP1_TX_CONFIG &= 0xef; /* Disable endpoint 1 */
            interrupt_mask &= 0xef;
        }
        USB_no_data_control();
    }
}

void SetAddress(void)
{
    USB_no_data_control(); /* handshake with host */
    USB_DEVICE_A = ENDPOINT_A0_FIFO[USB_wValue]; /* write new USB device address */
}

/* function:       USB_set_ep0_mode
 * purpose:        Writes the mode stored in the accumulator into the EPO
 *                 mode register. Takes care of unlocking the register
 ***/
void USB_send_stall(void)
{
    USB_EP0_TX_CONFIG=0xA0;
}

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 */
        endpoint_stall_status = 0; /* not stalled */
        USB_EP1_TX_CONFIG &= ~DATATOGGLE; /* clear data 0/1 bit */
        USB_STATUS &= 0xef; /* 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 */
        endpoint_stall_status = 1; /* stalled */
        USB_EP1_TX_CONFIG = 0x30; /* 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 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 SetIdle(void)
{
    USB_no_data_control();
    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)
{
    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 = sizeof(report_buffer);
    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();
}

void GetConfiguration(void)
{
    data_count = sizeof(configuration_status);
    USB_send_buffer_ptr=&configuration_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,
  SetIdle_index, SetProtocol_index,
  GetReport_index, GetIdle_index, GetProtocol_index
};

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

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 | INTERFACE    , USB_GET_INTERFACE    , GetInterfaceStatus_index, /* 81 0A */
  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_IDLE         , SetIdle_index           , /* 21 0A */
  HOST_TO_DEVICE | CLASS    | INTERFACE    , USB_SET_PROTOCOL     , SetProtocol_index       , /* 21 0B */
  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 */
};

void USB_stage_one(void)
{
    USB_EP0_RX_STATUS = 0;
    USB_STATUS = 8;
    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])();
}

void USB_control_read(void)
{       
        NOP();

        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];

        endp0_data_toggle = 0;
top:
        USB_EP0_RX_STATUS=0;
        if( !USB_EP0_RX_STATUS.USB_SETUP )
        {
                USB_STATUS = 0x08;
                for(loop_counter = 0; (data_count>0) && (loop_counter < 8); loop_counter++)
                {
                        ENDPOINT_A0_FIFO[loop_counter] = *USB_send_buffer_ptr;
                        USB_send_buffer_ptr++;
                        data_count--;
                }
                if( !USB_EP0_RX_STATUS.USB_SETUP )
                {
                        endp0_data_toggle ^= 0x40;
                        USB_EP0_TX_CONFIG = endp0_data_toggle | 0x80 | loop_counter;
                        GLOBAL_INTERRUPT = interrupt_mask;

                        do
                        {
                                if( USB_EP0_TX_CONFIG.7 == 0 )
                                        goto top;
                        }
                        while( !USB_EP0_RX_STATUS.USB_OUT );
                }
        }
        GLOBAL_INTERRUPT = interrupt_mask;
}

void USB_no_data_control(void)
{
        USB_EP0_TX_CONFIG = 0xC0;
        GLOBAL_INTERRUPT = interrupt_mask;
        while( USB_EP0_TX_CONFIG.7 );
}

⌨️ 快捷键说明

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