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

📄 mouse.c

📁 Cypress公司Cy7c6xxxx芯片的USB键盘及USB鼠标的firmware代码以及一些example代码。
💻 C
📖 第 1 页 / 共 3 页
字号:
void USB_send_stall(void);
void USB_get_descriptor_length(void);

int MICROSECONDx128_ISR(void)
{
        return(interrupt_mask);
}

int MILLISECOND_ISR(void)
{
        RESET_COP();
        if(--four_ms_counter == 0)
        {
                if(idle_period_counter != 0xff)
                        idle_period_counter++;
                four_ms_counter=4;
        }

        if(button1_deb != 0)
        {
                if(++button1_deb < 30)
                        button1_deb=0;
        }

        if(wakeup_flag == 1)
        {
                wakeup_counter++;
                return(MILLISECOND_ENABLE);
        }
        
        if( USB_STATUS.BUS_ACTIVITY )
        {
                USB_STATUS.BUS_ACTIVITY = 0;
                suspend_counter = 3;
        }
        else
        {
                if(--suspend_counter == 0)
                {
                        port_temp=PORT0;
                        suspend_counter=3;
                        if( remote_wakeup_status == ENABLE_REMOTE_WAKEUP)
                        {
                                CEXT_CLEAR=0;
                                disch_counter=0xff;
                                do
                                {
                                        AC=0xff;
                                        while(--AC);
                                        RESET_COP();
                                }
                                while(--disch_counter);
                                PORT1=0xff;
                                CEXT_CLEAR = 1;
                                not_awake = 1;
                                GLOBAL_INTERRUPT = WAKEUP_ENABLE;                                
                                while(not_awake); /* jiffy, this is used instead of SUSPEND */
                                //PROCESSOR_STATUS.SUSPEND=1;
                                //NOP();
                                PORT1=0xfc;
                        }
                }
        }
        return(interrupt_mask);
}

int USB_EP0_ISR(void)
{
        if( USB_EP0_RX_STATUS.USB_SETUP )
        {
                interrupt_mask &= 0xF7; /* disable endpoint zero interrupt */
                GLOBAL_INTERRUPT = interrupt_mask;
                USB_stage_one();
                interrupt_mask |= 0x08; /* enable endpoint zero interrupt */
        }
        return(interrupt_mask);
}

int USB_EP1_ISR(void)
{
        USB_EP1_TX_CONFIG = (USB_EP1_TX_CONFIG&0x7F)^DATATOGGLE;
        ENDPOINT_A1_FIFO[HORIZ_POSITION] = 0;
        ENDPOINT_A1_FIFO[VERT_POSITION] = 0;
        ENDPOINT_A1_FIFO[BUTTON_POSITION] = 0;
        return(interrupt_mask);
}

int GPIO_ISR(void)
{
        return(interrupt_mask);
}

int CEXT_ISR(void)
{
        CEXT_CLEAR=0;
        PORT1=0xfc;
        disch_counter=0xff;
        do
        {
                AC=0xff;
                while(--AC);
                RESET_COP();
        }
        while(--disch_counter);
        if( PORT0 != port_temp)
        {
                if( USB_STATUS.BUS_ACTIVITY )
                {
                        not_awake = 0;
                        return(WAKEUP_ENABLE);
                }
                USB_STATUS.FORCE_J = 1; /* send wake up signal to host */
                USB_STATUS.FORCE_J = 0;

                USB_STATUS |= (1<<FORCE_RESUME)|(1<<BUS_ACTIVITY);

                wakeup_flag = 1;
                wakeup_counter = 0;
                GLOBAL_INTERRUPT = MILLISECOND_ENABLE;

                while( wakeup_counter < 10 );

                GLOBAL_INTERRUPT = DISABLE_INTERRUPTS;
                USB_STATUS &= ~((1<<FORCE_RESUME)|(1<<BUS_ACTIVITY));
                wakeup_flag = 0;
                port_temp = PORT0;
        }
        PORT1=0xff;
        CEXT_CLEAR=1;
        return(WAKEUP_ENABLE);
}

void __STARTUP(void)
{
        PORT0 = 0xff;

        PORT1_ISINK0 = PORT1_ISINK1 = PORT1_ISINK2 = PORT1_ISINK3 = LED_CURRENT;
        PORT1_PULLUP = 0;
        PORT1 = 0xfc;
        PORT0IE = 0;
        PORT1IE = 0;
        CEXT_CLEAR = 0;

        ENDPOINT_A1_FIFO[HORIZ_POSITION] = 0;
        ENDPOINT_A1_FIFO[VERT_POSITION] = 0;
        ENDPOINT_A1_FIFO[BUTTON_POSITION] = 0;

        RESET_COP();
}

/*
;========================================================================
; This is a state transition table (16 bytes) that has four input bits:
; 	bit [3:2]	previous state
;	bit [1:0]	current state
;
; The state sequences are:
;	00 => 01 => 11 => 10	increment
;	00 => 10 => 11 => 01	decrement
;
;	00 00 =>  0	00 01 =>  1	00 10 => -1	00 11 =>  0
;	01 00 => -1	01 01 =>  0	01 10 =>  0	01 11 =>  1
;	10 00 =>  1	10 01 =>  0	10 10 =>  0	10 11 => -1
;	11 00 =>  0	11 01 => -1	11 10 =>  1	11 11 =>  0
; 
; The count stays the same if either the states are the same or the
; state changed by two transitions (jump).   
*/
const char StateTable[] = 
  { 0,  1, -1,  0,
   -1,  0,  0,  1,
    1,  0,  0, -1,
    0, -1,  1,  0 };

/*
;========================================================================
; Check for vertical movement of the mouse.  The first time I tried this,
; the horizontal movement worked and the vertical movement was backward.
; To correct the problem, the current and next states are switched in the
; check vertical routine when compared with the horizontal states.
;
*/
void CheckVertical(void)
{
        report_buffer.vertical += StateTable[vert_state | (port_temp&0xc)];
        vert_state = (port_temp&0xc)>>2;
}

/*
;========================================================================
; Read the mouse buttons.  If the buttons have changed from the last
; value read, then enable data transmission from endpoint one.
;
; Hardware has buttons in MRL order.  We need to translate them
; to data bits [2:0].
;
;       Port 0 bit[6]   Middle  => bit 2
;       Port 0 bit[5]   Right   => bit 1
;       Port 0 bit[4]   Left    => bit 0
;
*/
void ReadButtons(void)
{
        if( button1_deb == 0 )
        {
                if( report_buffer.buttons != ((~port_temp>>4)&0x7) )
                {
                        report_buffer.buttons = AC;
                        button_flag = 1;
                        button1_deb =1;
                }
        }
}

/*
; If the wheels are configured backwards the StateTable matrix
; 1's and -1's should be exchanged to flip the x and y directions.
; If only one direction has its wheels configured backwards then
; two seperate StateTables should be used and one of them will
; need the 1 to -1 and vice versa exchange.
; A wheel is configured backwards if the two phototransistors 
; associated with it are connected in opposite order to this
; reference design.

;========================================================================
; Check for horizontal movement of the mouse.
;
*/
void CheckHorizontal(void)
{
        report_buffer.horizontal += -StateTable[(port_temp&0x3)|horiz_state];
        horiz_state = (port_temp&0x03)<<2;
}

void main(void)
{
        if( PROCESSOR_STATUS.WATCHDOG_RESET)
        {
                GLOBAL_INTERRUPT = interrupt_mask = MILLISECOND_ENABLE;
                while(1);
        }

        if( !PROCESSOR_STATUS.USB_RESET)
        {
                while(1)
                {
                        port_temp=PORT0;
#ifndef DEBUG
                        PROCESSOR_STATUS.SUSPEND = 1;
#endif /* DEBUG */
                        NOP();
                }
        }

        PROCESSOR_STATUS.RUN = 1;
        PROCESSOR_STATUS &= 0x8F;
        GLOBAL_INTERRUPT = interrupt_mask = EP0_ENABLE | MILLISECOND_ENABLE;
        while(USB_EP1_TX_CONFIG == 0);

        while(1)
        {
                port_temp=PORT0;
                ReadButtons();
                CheckHorizontal();
                CheckVertical();

                if( (report_buffer.horizontal != 0) || (report_buffer.vertical != 0) || (button_flag != 0) || ((idle_period != 0) && (idle_period <= idle_period_counter)) )
                {
                        if( !USB_EP1_TX_CONFIG.7 )
                        {
                                ENDPOINT_A1_FIFO[HORIZ_POSITION] = report_buffer.horizontal;
                                ENDPOINT_A1_FIFO[VERT_POSITION] = report_buffer.vertical;
                                ENDPOINT_A1_FIFO[BUTTON_POSITION] = report_buffer.buttons;
                                USB_EP1_TX_CONFIG = (USB_EP1_TX_CONFIG & DATATOGGLE) | 0x93;
                                if( new_idle_flag == 0 )
                                        idle_period = prev_idle_period;
                                else
                                {
                                        idle_period = new_idle_period;
                                        new_idle_flag = 0;
                                }
                                idle_period_counter = 0;
                                report_buffer.horizontal = 0;
                                report_buffer.vertical = 0;
                                button_flag = 0;
                        }
                }
        }
}

/* USB library main routines */
void ClearRemoteWakeup(void)
{
    USB_no_data_control();
    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 (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) )
    {

⌨️ 快捷键说明

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