OSDev.org

The Place to Start for Operating System Developers
It is currently Fri Apr 19, 2024 2:32 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 14 posts ] 
Author Message
 Post subject: Keyboard driver
PostPosted: Thu Jan 26, 2017 11:33 am 
Offline

Joined: Wed Jan 25, 2017 9:54 am
Posts: 5
I am developing my OS and I am trying to create a driver for a keyboard.

Please tell me,why is it not working and how can I use my functions right way.
===================keyboard.h===================
Code:
typedef struct {
    uint8_t scancode;
    char chr; //Character it corresponds to.
} kbd_scancode;

static kbd_scancode regular_scancodes[] = {
  /* Numerical keys */
  {0x02, '1'}, {0x03, '2'}, {0x04, '3'}, {0x05, '4'}, {0x06, '5'}, {0x07, '6'}, {0x08, '7'}, {0x09, '8'}, {0x0A, '9'}, {0x0B, '0'},
  /* Some characters after numerical keys */
  {0x0C, '-'}, {0x0D, '='}, {0x0E, '\b'}, {0x0F, '\t'},
  /* Alphabet! */
  {0x10, 'q'}, {0x11, 'w'}, {0x12, 'e'}, {0x13, 'r'}, {0x14, 't'}, {0x15, 'y'}, {0x16, 'u'}, {0x17, 'i'}, {0x18, 'o'}, {0x19, 'p'}, {0x1A, '['}, {0x1B, ']'}, {0x1C, '\n'},
  {0x1E, 'a'}, {0x1F, 's'}, {0x20, 'd'}, {0x21, 'f'}, {0x22, 'g'}, {0x23, 'h'}, {0x24, 'j'}, {0x25, 'k'}, {0x26, 'l'}, {0x27, ';'}, {0x28, '\''}, {0x29, '`'},
  {0x2B, '\\'}, {0x2C, 'z'}, {0x2D, 'x'}, {0x2E, 'c'}, {0x2F, 'v'}, {0x30, 'b'}, {0x31, 'n'}, {0x32, 'm'}, {0x33, ','}, {0x34, '.'}, {0x35, '/'}, {0x37, '*'},
  {0x39, ' '}, {0x47, '7'}, {0x48, '8'}, {0x49, '9'}, {0x4A, '-'},
               {0x4B, '4'}, {0x4C, '5'}, {0x4D, '6'}, {0x4E, '+'},
               {0x4F, '1'}, {0x50, '2'}, {0x51, '3'},
               {0x52, '0'}, {0x53, '.'}, {0x00, '\0'}
};
static char kbd_buffer[256]={};
static uint8_t kbd_buffer_current_pos=0;
static void kbd_buffer_push(char value)
{
   if(kbd_buffer_current_pos!=255)
      kbd_buffer[kbd_buffer_current_pos++]=value;
   else
   {
      return;//Buffer  overflow
   }
}
static char kbd_buffer_pop()
{
   if(kbd_buffer_current_pos>0)
      return kbd_buffer[kbd_buffer_current_pos--];
   else
      return 0;//Nothing  in  the  buffer
}
char convert(uint8_t c)
{
   for(int i=0;regular_scancodes[i].scancode!=0x00;i++)
   {
      if(regular_scancodes[i].scancode==c)
         return regular_scancodes[i].chr;
      return 0;
   }
}
void handler()
{
   uint8_t scancode=inb(0x60);
   kbd_buffer_push(convert(scancode));
}
char kbd_getchar()
{
   char ret=0;
   while(!ret)
   {
      ret=kbd_buffer_pop();
   }
   return ret;
}


=============kernel.c=============

Code:
#include "tty.h"
#include "io.h"
#include "keyboard.h"
#include "GDT.h"
void kmain(void)
{
   terminal_init(VGA_WHITE,VGA_BLACK);
   terminal_writestring("Creating  GDT  descriptors....");
   createDescriptors();
   int i=0;
   while(i<5000)
   {
   i++;
   handler();
   }
   terminal_writestring("Cought  key..");
   terminal_writestring(kbd_getchar());
}


P.S. sorry for mess

P.P.S. Write me if you want to work with me.


Top
 Profile  
 
 Post subject: Re: Keyboard driver
PostPosted: Thu Jan 26, 2017 11:48 am 
Offline
Member
Member

Joined: Wed Oct 26, 2011 12:00 pm
Posts: 202
I just finished my ps2 driver with drivers for both mouse and keyboard, with interrupts, not polling, but maybe it can help you to see how I did it

https://github.com/Fadekraft/MollenOS/t ... ch/x86/ps2

_________________
mollenos | gracht (protocol library) | vioarr (window-manager) | bake (package manager)


Top
 Profile  
 
 Post subject: Re: Keyboard driver
PostPosted: Fri Jan 27, 2017 8:34 am 
Offline

Joined: Wed Jan 25, 2017 9:54 am
Posts: 5
I don't know.Your code is written on C++ and mine is on C.
I think,I can't understand it well.
So,Can you write me a basic example on C?Just a simple and easy.


Top
 Profile  
 
 Post subject: Re: Keyboard driver
PostPosted: Fri Jan 27, 2017 9:04 am 
Offline

Joined: Wed Jan 25, 2017 9:54 am
Posts: 5
Oh,I forgot.When I print keyboard scancodes on the terminal,it just prints S.
Just 'S'.Nothing else
Please,help


Top
 Profile  
 
 Post subject: Re: Keyboard driver
PostPosted: Fri Jan 27, 2017 9:59 am 
Offline
Member
Member
User avatar

Joined: Thu Jul 12, 2012 7:29 am
Posts: 723
Location: Tallinn, Estonia
Ruslan wrote:
I don't know.Your code is written on C++ and mine is on C.


Terribly sorry to interrupt, but where did you find C++ there?

_________________
Learn to read.


Top
 Profile  
 
 Post subject: Re: Keyboard driver
PostPosted: Sat Jan 28, 2017 6:23 am 
Offline

Joined: Wed Jan 25, 2017 9:54 am
Posts: 5
Sorry,If I mistaken.I thought is was C++.

So,can you give me some examples?


Top
 Profile  
 
 Post subject: Re: Keyboard driver
PostPosted: Sat Jan 28, 2017 9:08 am 
Offline
Member
Member
User avatar

Joined: Fri Apr 03, 2015 9:41 am
Posts: 492
Dude, that's my code from kbd.c! :D

And... you used it COMPLETELY wrong. It should be called in your interrupt handler, not in loop.

_________________
Developing U365.
Source:
only testing: http://gitlab.com/bps-projs/U365/tree/testing

OSDev newbies can copy any code from my repositories, just leave a notice that this code was written by U365 development team, not by you.


Top
 Profile  
 
 Post subject: Re: Keyboard driver
PostPosted: Sat Jan 28, 2017 9:43 am 
Offline
Member
Member

Joined: Sat Nov 21, 2009 5:11 pm
Posts: 852
I thought this looked familiar. You copied your code from a 12 year old, no wonder it doesn't work.

http://forum.osdev.org/viewtopic.php?f=1&t=31069

If you honestly can't figure out what's wrong by yourself, I strongly suggest finding another pastime.

Hint: Suppose the one byte has been pushed onto the input stack, then kbd_buffer_pop gets called. Where does it read its result from? Also, what would be the correct data structure to use if you wanted typed characters to appear to the caller in the order they were typed?


Top
 Profile  
 
 Post subject: Re: Keyboard driver
PostPosted: Sat Jan 28, 2017 10:40 am 
Offline
Member
Member
User avatar

Joined: Fri Apr 03, 2015 9:41 am
Posts: 492
Gigasoft wrote:
I thought this looked familiar. You copied your code from a 12 year old, no wonder it doesn't work.

http://forum.osdev.org/viewtopic.php?f=1&t=31069

If you honestly can't figure out what's wrong by yourself, I strongly suggest finding another pastime.

Hint: Suppose the one byte has been pushed onto the input stack, then kbd_buffer_pop gets called. Where does it read its result from? Also, what would be the correct data structure to use if you wanted typed characters to appear to the caller in the order they were typed?

Wait, he copied the code from that thread? It's the buggy version, I fixed it long time ago. It works in current version of my OS, he just uses it wrong. He should make an interrupt handler for keyboard interrupt and call it only there, otherwise he won't get some adequate values at all.

To Ruslan:
Sorry if I'm being a bit rude, but you won't be able to use my code if you won't implement interrupts. Here's some tips:
1. You should implement interrupts as soon as possible, you will need them.
2. After that you can just call handler(); in your IRQ #1 handler. Also I strongly recommend you to rename handler() to something like "keyboard_irq_handler" as you will need more than one IRQ handler in your OS, you will need at least PIT and (maybe) mouse.
3. You just copy-pasted the code as I can see and even removed some features like modifier keys. Sorry, but you need to understand it first. At this point it looks that you didn't. This definitely won't be waste of time to read the code. Sorry if this was been too rude :)

_________________
Developing U365.
Source:
only testing: http://gitlab.com/bps-projs/U365/tree/testing

OSDev newbies can copy any code from my repositories, just leave a notice that this code was written by U365 development team, not by you.


Top
 Profile  
 
 Post subject: Re: Keyboard driver
PostPosted: Sat Jan 28, 2017 10:43 am 
Offline
Member
Member
User avatar

Joined: Fri Apr 03, 2015 9:41 am
Posts: 492
And as I seen in your post you put it to .h (header) file. Separate it to .h and .c files. My code was in .c file only because these structures are only for driver internal use and shouldn't be declared somewhere outside. You can do the same.

_________________
Developing U365.
Source:
only testing: http://gitlab.com/bps-projs/U365/tree/testing

OSDev newbies can copy any code from my repositories, just leave a notice that this code was written by U365 development team, not by you.


Top
 Profile  
 
 Post subject: Re: Keyboard driver
PostPosted: Sat Jan 28, 2017 12:39 pm 
Offline
Member
Member

Joined: Wed Oct 26, 2011 12:00 pm
Posts: 202
OP can't tell the difference from C/C++ properly, I really don't believe OS development is a task he/she is suited for.

_________________
mollenos | gracht (protocol library) | vioarr (window-manager) | bake (package manager)


Top
 Profile  
 
 Post subject: Re: Keyboard driver
PostPosted: Sat Jan 28, 2017 1:38 pm 
Offline
Member
Member
User avatar

Joined: Fri Apr 03, 2015 9:41 am
Posts: 492
MollenOS wrote:
OP can't tell the difference from C/C++ properly, I really don't believe OS development is a task he/she is suited for.

He. Ruslan is Russian male name.

_________________
Developing U365.
Source:
only testing: http://gitlab.com/bps-projs/U365/tree/testing

OSDev newbies can copy any code from my repositories, just leave a notice that this code was written by U365 development team, not by you.


Top
 Profile  
 
 Post subject: Re: Keyboard driver
PostPosted: Sun Jan 29, 2017 7:35 am 
Offline

Joined: Wed Jan 25, 2017 9:54 am
Posts: 5
Quote:
And... you used it COMPLETELY wrong. It should be called in your interrupt handler, not in loop.


Then,can you give me an example how to use it,please? :D


Top
 Profile  
 
 Post subject: Re: Keyboard driver
PostPosted: Sun Jan 29, 2017 7:45 am 
Offline
Member
Member
User avatar

Joined: Fri Apr 03, 2015 9:41 am
Posts: 492
Ruslan wrote:
Quote:
And... you used it COMPLETELY wrong. It should be called in your interrupt handler, not in loop.


Then,can you give me an example how to use it,please? :D

I can't give you an example as it differs per OS. Call handler() in IRQ #1 handler.

_________________
Developing U365.
Source:
only testing: http://gitlab.com/bps-projs/U365/tree/testing

OSDev newbies can copy any code from my repositories, just leave a notice that this code was written by U365 development team, not by you.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Majestic-12 [Bot], MichaelPetch and 161 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group