Waiting for keyboard response interrupt when setting up
Posted: Wed May 06, 2015 3:12 am
I've started work on my Keyboard driver, and I'm having an issue when I'm trying to send commands to the device during initialization.
Here is my function to write a one-byte command (with no required data) to the keyboard.
Here's my keyboard interrupt handler:
What I do is if I'm sending a command during initialization, I send the command and then wait in an indefinite while loop. When I receive my next keyboard interrupt, I then modify the boolean controlling the inescapable while loop such that the loop will end when I return from the interrupt. My problem is, however, that although the interrupt saying that the keyboard has read my command fires, the loop doesn't terminate. I'm assuming that this is because my loop is too fast. If I slow down the code by printing text before entering the loop (And allowing the interrupt to fire before entering the loop at all) or by doing something like printing text within the loop (And slowing it down to let it work) then there is no problem.
Obviously then, the problem is the way I wait for the interrupt to occur. I suppose my question is: What's the best way to wait for the interrupt? Should I just find some way to slow down the loop, or is it better practice to wait for the interrupt in a different way?
Here is my function to write a one-byte command (with no required data) to the keyboard.
Code: Select all
//Sends a command byte to the keyboard.
kbd_response_t send_command_byte(uint8_t cmd){
int i = 0;
kbd_response_t toreturn = {true, RESEND};
do{
bool waserror = send_byte_to_kbd(cmd);
received_interrupt = false;
//No error when sending the byte? Then check for the response
if(!waserror){
//Loop until out keyboard interrupt says we received a response
while(!received_interrupt);
uint8_t response = inb(PS2_DATA_REG);
//If we didn't get asked for a resend, then it was sent successfully
if(response != RESEND){
toreturn.send_error = false;
toreturn.response_code = response;
return toreturn;
}
}
}
while(i < MAX_RESEND_ATTEMPTS);
//Return a keyboard response saying we encountered an error
return toreturn;
}
Code: Select all
void handle_keyboard_interrupt(){
if(!initialized){
received_interrupt = true;
return;
}
//Read the scan code
}
Obviously then, the problem is the way I wait for the interrupt to occur. I suppose my question is: What's the best way to wait for the interrupt? Should I just find some way to slow down the loop, or is it better practice to wait for the interrupt in a different way?