Page 1 of 1

Help on LED's

Posted: Mon Jun 20, 2005 7:07 pm
by Spoon spider
Hi. :)
Slight problem with some code.
Here goes..
DisableKeyboard:
cli
J1: in al,0x64
test al,2
jnz J1


mov al,0xad
out 0x64,al
;*****************************************
L1: in al,0x64
test al,2
jnz L1

mov al,0xed
out 0x60,al

L3: in al,0x64
test al,2
jnz L3

L4: mov al,0xff
out 0x60,al
;*****************************************
EnableKeyboard:
J3: in al,0x64
test al,2
jnz J3

J4: mov al,0xae
out 0x64,al
sti
jmp $

The LED's come then go off again. This doesn't happen when I boot into windows.
Any takers ;)

Re:Help on LED's

Posted: Tue Jun 21, 2005 2:23 am
by Kemp
You're probably going to need to be a lot more specific than that for people to be able to help you, for example:

1) Does that happen when you call enable or disable? Or both?

2) "This doesn't happen when I boot into windows" isn't a very good basis for saying it shouldn't happen. Windows could do it completely differently.

3) Does your code do anything with the LEDs or are you relying on the keyboard to sort them out?

4) I'm sure you get the idea by this point.

Re:Help on LED's

Posted: Tue Jun 21, 2005 5:22 am
by Spoon spider
Hi all. :)
Ok get the point ;)
When I boot into windows nothing happens with the keyboard LED's, except the Num Lock light is on due to a BIOS setting.
When I set the LED's to off they stay off, but when I set them for on, they come on the go back off.
The reference I'am useing is "The Undocumented PC" and I have been following example code, I can't seem to find anything wrong. :(
This routine is just a basic test, it doesn't keep a track of LED status as it's just a test to get the LED's to switch on or off.

Thanks for any help
Spoon spider :)

Re:Help on LED's

Posted: Tue Jun 21, 2005 5:59 am
by Kemp
Ok, that's a lot more helpful :) I have a copy of The Undocumented PC at home, I'll check up the code when I get back, so if it works on my system. Of course, if anyone can spot anything wrong with the posted code then they can help you a bit quicker, I can't remember ports without a reference next to me.

Re:Help on LED's

Posted: Tue Jun 21, 2005 6:30 am
by Spoon spider
Thanks Kemp :D

Re:Help on LED's

Posted: Tue Jun 21, 2005 6:44 am
by Pype.Clicker
just a guess:
how long does your key actually remain enabled ?
any chance you could be erasing leds bits in some interrupt handling ?
any chance you could be clearing the led bit when the "xxx lock" key is released (thus meaning it will only remain lit up while you keep the key pressed :P)

Re:Help on LED's

Posted: Tue Jun 21, 2005 7:44 am
by Brendan
Hi,

The code shown does the following:

a) Tell the keyboard controller chip to disable the clock for the first PS/2 device (the keyboard). This prevents the keyboard from sending data to the PS/2 controller chip.

b) Send command 0xED to the keyboard, which is the right command to set the LEDs, but the keyboard can't send an ACK (because of the first step) so this might not actually do anything - I assume that if the keyboard can't send the ACK it doesn't move on to the "wait for next byte" part...

c) Send 0xFF to the device, which would be interpretted as "perform internal power-on reset function", otherwise known as the BAT (Basic Assurance Test). This is the same test that occurs when the keyboard first gets power (ie. you turn the computer on), which makes the LEDs flash (just like when you turn the computer on). This 0xFF is not the second byte of the set LEDs command, as the set LEDs command didn't work.

d) Tell the PS/2 controller chip to enable the clock for the first PS/2 device (the keyboard). This allows the keyboard to send data to the PS/2 controller chip. At this point the keyboard might send an ACK or something that it failed to send earlier, or possibly an 0xFE (Resend).

Some advice (all IMHO): Windows is crap. If you plug 2 PS/2 keyboards in or you've got the mouse & keyboard around the wrong way (easy enough to do), Windows will lock up (not sure about XP here but 95/98 does). There's no reason why an OS can't detect this and display an error message, or auto-detect the devices and just plain work in most cases - the old version of my OS does work with non-standard combinations of PS/2 devices, and the new version will also (Linux can work with a pair of PS/2 keyboards, but I don't know what's involved in setting it up).

Some more advice (still IMHO): Unless you're willing to test & initialize the PS/2 controller chip correctly, don't (see http://www.mega-tokyo.com/forum/index.p ... eadid=7794 for how I do it). Most hobby OS's (and I assume Windows too) just cross their fingers and hope everything is going to work without any of this, which is why they fail when everything isn't as they expect.

With this in mind you may be able to use something like:

Code: Select all

InitKeyboard:
   cli

;Force the keyboard's output buffer empty

L1:   in al,0x64
   test al,0x20
   je L2
   in al,0x60
   jmp L1
                        
;Make sure keyboard's input buffer is empty
   
L2:   in al,0x64
   test al,2
   jnz L2

;Send "set LEDs" command

   mov al,0xed
   out 0x60,al

;Wait for "ACK" and ignore it without testing

L3:   in al,0x64
   test al,0x20
   jne L3
   in al,0x60

;Make sure keyboard's input buffer is empty
; Note: possibly not needed as we know it's received the last command because it sent ACK
   
L4:   in al,0x64
   test al,2
   jnz L4

;Send the LED flags

   mov al,0x07
   out 0x60,al

   sti
   ret
Note: After enabling interrupts your keyboard IRQ handler should be ready to receive an ACK left over from the last byte sent to the keyboard.

My code above is barely adequate - if the user has a USB keyboard (with PS/2 emulation disabled) or no keyboard at all (server) then it will lock up waiting for the first ACK to come from a keyboard that doesn't exist.

A much better idea is to have a pile of little helper routines with time-outs, for e.g.:

Code: Select all

;Send byte to PS/2 controller
   
send_byte:
   push ecx
   push eax
   mov ecx,[current_system_timer_tick]
   add ecx,a_value_representing_a_50_mS_timeout_value
L1:   in al,0x64
   test al,2
   jz L2
   cmp [current_system_timer_tick],ecx
   jb L1
   pop eax
   stc
   ret

L2:   pop eax
   pop ecx
   out 0x60,al
   clc
   ret



;Initialize the keyboard
   
InitKeyboard:
   cli
   
   call force_keyboard_output_empty

   mov al,0xED      ;Send "set LEDs" command
   call send_byte
   jc .error
   
   call wait_for_ACK
   jc .error

   mov al,0x07      ;LEDs - all on
   call send_byte
   jc .error

   sti

   ret
   
This makes it much easier to see what the "init" code is doing (and it won't lock up when something isn't as expected).


Cheers,

Brendan

Re:Help on LED's

Posted: Tue Jun 21, 2005 7:50 am
by Kemp
Brendan = God ;D (with the except of the Ancients who set up this forum, much shrouded in mystery they are ;) )

Now that's an answer and a half. Pity this forum doesn't have one of those systems where you can give people stars or points or whatever for being particularily helpful, though I doubt we have an int type large enough to track yours ;)

Re:Help on LED's

Posted: Tue Jun 21, 2005 11:46 am
by Spoon spider
Brendan YOU ARE THE MAN :)
Works like a treat. It seems that I have been missing some code from the examples in "The Undocumented PC"

L3: in al,0x64
test al,0x20
jne L3
in al,0x60

as you had put it.
But anyway thats for the help.

Spoon spider ;)