13.07.2015 Views

Designing a USB Keyboard and PS/2® Mouse Combination Device ...

Designing a USB Keyboard and PS/2® Mouse Combination Device ...

Designing a USB Keyboard and PS/2® Mouse Combination Device ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Designing</strong> a <strong>USB</strong> <strong>Keyboard</strong> <strong>and</strong> <strong>PS</strong>/2 <strong>Mouse</strong> <strong>Combination</strong> <strong>Device</strong>sends it to the host system if necessary. For more details onits operation please see the <strong>USB</strong> keyboard application note.Similarly, the <strong>Mouse</strong>Task calls mouse_poll subroutine to readdata from the <strong>PS</strong>/2 mouse, copies the mouse report frommemory buffer to endpoint 1 FIFO <strong>and</strong> transmits it to the hostsystem. An arbitrary delay of 20 ms was added before thecode loops back. This is to make sure that the <strong>PS</strong>/2 mousegets ready to respond to the next polling comm<strong>and</strong>.<strong>USB</strong> Suspend/Resume <strong>and</strong> Remote WakeupAccording to the <strong>USB</strong> Specification 1.0, a device has to gointo suspend mode after 3 ms of no bus activity. The 1 ms ISRdetermines when this condition occurs (by reading the BusActivity bit, bit 3, of the <strong>USB</strong> Status <strong>and</strong> Control Register, 1Fh)<strong>and</strong> suspends the microcontroller. This is accomplished bysetting bit 0 (Run bit) <strong>and</strong> bit 3 (Suspend bit) of the ProcessorStatus <strong>and</strong> Control Register (FFh). The microcontroller willresume operation upon one of the following three events:<strong>USB</strong> bus activity, keyboard activity, or <strong>PS</strong>/2 mouse activity.<strong>USB</strong> bus activity resume is carried out automatically by themicrocontroller. On the other h<strong>and</strong>, keyboard <strong>and</strong> <strong>PS</strong>/2mouse activity during suspend are detected through GPIOinterrupts. For this reason, before entering suspend, GPIOinterrupts for the keyboard <strong>and</strong> mouse must be properly enabled.For the keyboard, prior to suspending the microcontroller, the1 ms ISR pulls down all the column port lines <strong>and</strong> enable afalling edge interrupt on all Port 2 lines (Row port). If any keyis pressed while in suspended state, one of the row lines willbe pulled LOW <strong>and</strong> trigger a GPIO interrupt. The GPIO ISRwill then send a resume signal (force a K state where D+ isHIGH <strong>and</strong> D– is LOW for 10 ms) to wake up the host system(remote wake up).The <strong>PS</strong>/2 mouse normally operates in Remote (polling)mode, where the mouse only transmits a data report in responseto a Read Data comm<strong>and</strong>. When the microcontrollerenters suspend state, however, it no longer polls the mousefor data. Thus, it is desirable to allow the mouse to be capableof autonomously waking up the microcontroller if it has datato transmit (mouse movement or button press). This is accomplishedby placing the mouse in Stream mode before enteringsuspend. In the Stream mode, the <strong>PS</strong>/2 mouse willtransmit data at regular intervals any time it has new dataavailable, generating GPIO interrupts <strong>and</strong> waking up the microcontroller.Before entering suspend the main code must call functionmouse_suspend, which initiates a Set Stream Mode comm<strong>and</strong><strong>and</strong> operates mouse_machine until the transaction iscomplete. Then the main code will enable suspend, leavingthe Port 3 GPIO interrupt enabled. During suspend state, anyactivity on the <strong>PS</strong>/2 port will cause an entry to the GPIO ISR,which will immediately inhibit the mouse transmission. Duringthe wakeup sequence, the firmware must also call mouse_initto place the mouse back to Remote mode for normal datapolling operations.GPIO Interrupt Service RoutineIn the keyboard-only firmware, the GPIO ISR was only usedto h<strong>and</strong>le the keyboard resume <strong>and</strong> the sending of the remotewakeup signal after leaving the suspend state. For the keyboard<strong>and</strong> mouse combination device, however, the GPIO interruptis also used to drive the <strong>PS</strong>/2 low-level state machine(mouse_int) at each falling edge of the <strong>PS</strong>/2 clock signal.NoBus StillIdle?YesSend RemoteWakeup SignalGPIO ISRIn SuspendMode?ReturnYesCall <strong>Mouse</strong>Bit/byte LevelState Machinemouse_intFigure 10. GPIO Interrupt Service RoutineUpon entering the GPIO ISR (See Figure 10), a test is doneto determine whether the firmware is running in normal operationmode or has just left suspend mode. If it is in normaloperation, the mouse_int state machine is called; otherwise,the remote wakeup sequence begins.1-ms Interrupt Service RoutineFor the keyboard <strong>and</strong> mouse combination firmware, a fewchanges were added to the keyboard-only 1 ms ISR (Figure11):• The keyboard scan task is no longer scheduled every 4 msas in the keyboard-only firmware.• Before entering suspend state, the <strong>PS</strong>/2 mouse is placedin Stream mode. Also, after coming out of suspend, themouse is set back to Remote mode.• The <strong>PS</strong>/2 mouse time-out counter increment <strong>and</strong> check isincluded. If the <strong>PS</strong>/2 is in the active state for more than 25ms <strong>and</strong> no transfer happens, a time-out condition will occur,resetting the <strong>PS</strong>/2 bus <strong>and</strong> setting the state machines toidle.<strong>PS</strong>/2 Signal Timing IssueFor reliable <strong>PS</strong>/2 bus operation, the GPIO interrupt serviceprovided by the main code must ensure that the latency betweena falling edge on the <strong>PS</strong>/2 clock line <strong>and</strong> resultant entryinto the bit/byte level state machine (mouse_int) is no morethan 17 µs. This requirement precludes the disabling of interruptsfor long periods of time by other code modules, whilemouse_machine is busy. Special attention should be given tothe 1 ms ISR or other ISRs that may disable interrupt for longperiods of time.No9

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!