Keyboard IRQ handler getting fired twice

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
amittomar
Posts: 4
Joined: Mon Oct 28, 2013 1:44 pm

Keyboard IRQ handler getting fired twice

Post by amittomar »

I followed the tutorial from http://www.osdever.net/bkerndev/index.php and wrote some code to handle IRQs.

I am having a peculiar issue. Whenever I press a key, the key I pressed gets printed correctly, but again the IRQ fires and a junk value gets printed as well. Can someone guide what direction should I be looking into ?

Can it be 'spurious interrupt', but then When I run the code taken from the same tutorial as it is, it works fine with no multiple prints.

P.S. I have implemented a Timer as well, and it works fine (fires once).
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: Keyboard IRQ handler getting fired twice

Post by xenos »

You know that you get interrupts both on key press and release?

http://www.win.tue.nl/~aeb/linux/kbd/sc ... html#ss1.1
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
amittomar
Posts: 4
Joined: Mon Oct 28, 2013 1:44 pm

Re: Keyboard IRQ handler getting fired twice

Post by amittomar »

This is how I am handling the key press :

Code: Select all

// Handles the keyboard interrupt
void handleKeyPress(registerStructure *registerInformation)
{
   unsigned char keyPressed ;   
   unsigned short port = 0x60;
   
   // Read the value from data register
   asm volatile("inb %1, %0" : "=a" (keyPressed) : "dN" (port));

    // When a key is released, highest byte is set to 1
    if ( 1 == (keyPressed & 0x80))
    {
        // Handle shift/ctrl keys
    }
    
    // Key pressed
    else
    {        
	    putCharacterOnScreen(keyboardMapping[keyPressed]);	
    }
}
I think it takes appropriate care of both press and release ?

Now that you pointed towards it I noticed something in the output :

1. Valid key is printed as soon as I press the key
2. Invalid key is printed only when I release the key
3. If I keep the key pressed, it keep printing the valid key till I release, which is when an invalid key is printed once.
jnc100
Member
Member
Posts: 775
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Re: Keyboard IRQ handler getting fired twice

Post by jnc100 »

If I was you compiler, I would have optimized
amittomar wrote:

Code: Select all

    if ( 1 == (keyPressed & 0x80))
    {
        // Handle shift/ctrl keys
    }
    
    // Key pressed
    else
    {        
	    putCharacterOnScreen(keyboardMapping[keyPressed]);	
    }
to

Code: Select all

putCharacterOnScreen(keyboardMapping[keyPressed]);
Regards,
John.
amittomar
Posts: 4
Joined: Mon Oct 28, 2013 1:44 pm

Re: Keyboard IRQ handler getting fired twice

Post by amittomar »

@XenOS
if ( 1 == (keyPressed & 0x80))
Here is the bug. It should just have been
if ( (keyPressed & 0x80))
Thanks a lot. You made me think in the right direction. Am new to the forums, anything I can do to give you 'reputation' in any sense ? Some way to 'vote you up' or anything ?
amittomar
Posts: 4
Joined: Mon Oct 28, 2013 1:44 pm

Re: Keyboard IRQ handler getting fired twice

Post by amittomar »

Hehe seems I was just a minute late to realize and reply..
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: Keyboard IRQ handler getting fired twice

Post by xenos »

amittomar wrote:Thanks a lot. You made me think in the right direction. Am new to the forums, anything I can do to give you 'reputation' in any sense ? Some way to 'vote you up' or anything ?
I don't think that the forums have something like this, but reading that my advice could help you already makes my day :D
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
Post Reply