[自制操作系統] 第17回 編寫鍵盤驅動


目錄
一、前景回顧
二、實現鍵盤輸入的中斷函數
三、編寫鍵盤驅動
四、實現環形輸入緩沖區
五、運行測試

 

一、前景回顧

  上一回我們完成了鎖的實現,並且利用鎖優化了終端輸出函數。這一回我們來實現鍵盤的輸入,為后面的用戶交互功能打好基礎。

二、實現鍵盤輸入的中斷函數

  首先我們需要知道鍵盤是屬於外設,所以對應的中斷屬於外部中斷。在講中斷那一章節時,我們知道了外部中斷的處理流程,不過對於鍵盤的輸入中斷,還需要增加一點點東西。

   

  8048是鍵盤上的芯片,其主要任務就是監控哪個鍵被按下,一旦有按鍵信息,8048就將按鍵信息傳遞給鍵盤控制器8042(8042通常是Intel 8042或兼容芯片,集成在主機內部的主板上),再由8042發送中斷信號給8259A。最重要的一點是,鍵盤的中斷號。

   

  我們可以看到鍵盤對應的是IR1口,這個是硬件上決定的,所以我們無法更改。除此之外,在我們的程序中,我們將IR0口的中斷號設置為0x20,后面依次遞增,所以我們可以知道鍵盤的中斷號為0x21。這里我們不管按鍵信息如何,我們只需要知道一旦有按鍵按下,就會有中斷觸發,所以我們嘗試寫一下按鍵的中斷處理函數。

  在project/kernel目錄下新建keyboard.c、keyboard.h文件,除此之外還需要修改interrupt.c文件。

 1 #include "keyboard.h"
 2 #include "print.h"
 3 #include "interrupt.h"
 4 #include "io.h"
 5 #include "global.h"
 6 #include "stdint.h"
 7 
 8 #define KBD_BUF_PORT  0x60
 9 
10 static void intr_keyboard_handler(void) 11 { 12     put_str("k\n"); 13  inb(KBD_BUF_PORT); 14 } 15 
16 /*鍵盤初始化*/
17 void keyboard_init(void) 18 { 19     put_str("keyboard init start\n"); 20     register_handler(0x21, intr_keyboard_handler); 21     put_str("keyboard init done\n"); 22 }
keyboard.c
1 #ifndef __KERNEL_KEYBOARD_H 2 #define  __KERNEL_KEYBOARD_H
3 
4 void keyboard_init(void); 5 static void intr_keyboard_handler(void); 6 #endif
keyboard.h
 1 ...  2 
 3 /* 初始化可編程中斷控制器8259A */
 4 static void pic_init(void) {  5    /* 初始化主片 */
 6    outb (PIC_M_CTRL, 0x11);   // ICW1: 邊沿觸發,級聯8259, 需要ICW4.
 7    outb (PIC_M_DATA, 0x20);   // ICW2: 起始中斷向量號為0x20,也就是IR[0-7] 為 0x20 ~ 0x27.
 8    outb (PIC_M_DATA, 0x04);   // ICW3: IR2接從片. 
 9    outb (PIC_M_DATA, 0x01);   // ICW4: 8086模式, 正常EOI
10 
11    /* 初始化從片 */
12    outb (PIC_S_CTRL, 0x11);    // ICW1: 邊沿觸發,級聯8259, 需要ICW4.
13    outb (PIC_S_DATA, 0x28);    // ICW2: 起始中斷向量號為0x28,也就是IR[8-15] 為 0x28 ~ 0x2F.
14    outb (PIC_S_DATA, 0x02);    // ICW3: 設置從片連接到主片的IR2引腳
15    outb (PIC_S_DATA, 0x01);    // ICW4: 8086模式, 正常EOI
16 
17    /*只打開鍵盤中斷*/
18    outb (PIC_M_DATA, 0xfd); 19    outb (PIC_S_DATA, 0xff); 20 
21    put_str("pic_init done\n"); 22 } 23 
24 ...
interrupt.c

  最后編譯運行,可以看到我們一旦按下按鍵,屏幕便會打印信息,而且釋放按鍵也會打印信息。當然這是后面需要講解的內容,總之到現在,我們已經成功實現了按鍵的中斷處理函數。

   

三、編寫鍵盤驅動

  現在來說說為什么按下按鍵和釋放按鍵都會觸發中斷。其實這是硬件所決定的,一個鍵的狀態要么是按下,要么是彈起,因此一個按鍵有兩個編碼,按鍵被按下時的編碼是通碼,按鍵被釋放時的編碼是斷碼。

  無論是按下鍵或是松開鍵,當鍵的狀態改變后,鍵盤中的8048芯片把按鍵對應的掃描碼(通碼或者斷碼)發送到主板上的8042芯片,由8042處理后保存在自己的寄存器中,然后向8259A發送中斷信號,這樣處理器便去執行鍵盤中斷處理程序,將8042處理過的掃描碼從它的寄存器中讀取出來,隨后將掃描碼轉換成對應的ASCII碼。

  所以我們的函數中需要自己完善這么一個映射關系,也就是掃描碼到ASCII碼的映射。這里就偷懶直接抄書上的,我也沒有去仔細看了,總之能知道整個流程就行了。

  所以進一步完善我們的keyboard.c文件如下。

 1 #include "keyboard.h"
 2 #include "print.h"
 3 #include "interrupt.h"
 4 #include "io.h"
 5 #include "global.h"
 6 #include "stdint.h"
 7 #include "ioqueue.h"
 8 
 9 #define KBD_BUF_PORT  0x60
 10 
 11 /*用轉移字符定義部分控制字符*/
 12 #define esc        '\033'
 13 #define backspace  '\b'
 14 #define tab        '\t'
 15 #define enter      '\r'
 16 #define delete     '\0177'
 17 
 18 /*以下不可見字符一律為0*/
 19 #define char_invisible  0
 20 #define ctrl_l_char     char_invisible
 21 #define ctrl_r_char     char_invisible
 22 #define shift_l_char    char_invisible
 23 #define shift_r_char    char_invisible
 24 #define alt_l_char      char_invisible
 25 #define alt_r_char      char_invisible
 26 #define caps_lock_char  char_invisible
 27 
 28 /*定義控制字符的通碼和斷碼*/
 29 #define shift_l_make     0x2a
 30 #define shift_r_make     0x36
 31 #define alt_l_make       0x38
 32 #define alt_r_make       0xe038
 33 #define alt_r_break      0xe0b8
 34 #define ctrl_l_make      0x1d
 35 #define ctrl_r_make      0xe01d
 36 #define ctrl_r_break     0xe09d
 37 #define caps_lock_make   0x3a
 38  
 39 /*定義以下變量記錄相應鍵是否按下的狀態*/
 40 static bool ctrl_status, shift_status, alt_status, caps_lock_status, ext_scancode;  41 
 42 
 43 /*以通碼make_code為索引的二維數組*/
 44 static char keymap[][2] = {  45 /*掃描碼未與shift組合*/
 46 /* 0x00 */    {0,    0},  47 /* 0x01 */ {esc, esc},  48 /* 0x02 */    {'1',    '!'},  49 /* 0x03 */    {'2',    '@'},  50 /* 0x04 */    {'3',    '#'},  51 /* 0x05 */    {'4',    '$'},  52 /* 0x06 */    {'5',    '%'},  53 /* 0x07 */    {'6',    '^'},  54 /* 0x08 */    {'7',    '&'},  55 /* 0x09 */    {'8',    '*'},  56 /* 0x0A */    {'9',    '('},  57 /* 0x0B */    {'0',    ')'},  58 /* 0x0C */    {'-',    '_'},  59 /* 0x0D */    {'=',    '+'},  60 /* 0x0E */ {backspace, backspace},  61 /* 0x0F */ {tab, tab},  62 /* 0x10 */    {'q',    'Q'},  63 /* 0x11 */    {'w',    'W'},  64 /* 0x12 */    {'e',    'E'},  65 /* 0x13 */    {'r',    'R'},  66 /* 0x14 */    {'t',    'T'},  67 /* 0x15 */    {'y',    'Y'},  68 /* 0x16 */    {'u',    'U'},  69 /* 0x17 */    {'i',    'I'},  70 /* 0x18 */    {'o',    'O'},  71 /* 0x19 */    {'p',    'P'},  72 /* 0x1A */    {'[',    '{'},  73 /* 0x1B */    {']',    '}'},  74 /* 0x1C */ {enter, enter},  75 /* 0x1D */ {ctrl_l_char, ctrl_l_char},  76 /* 0x1E */    {'a',    'A'},  77 /* 0x1F */    {'s',    'S'},  78 /* 0x20 */    {'d',    'D'},  79 /* 0x21 */    {'f',    'F'},  80 /* 0x22 */    {'g',    'G'},  81 /* 0x23 */    {'h',    'H'},  82 /* 0x24 */    {'j',    'J'},  83 /* 0x25 */    {'k',    'K'},  84 /* 0x26 */    {'l',    'L'},  85 /* 0x27 */    {';',    ':'},  86 /* 0x28 */    {'\'',    '"'},  87 /* 0x29 */    {'`',    '~'},  88 /* 0x2A */ {shift_l_char, shift_l_char},  89 /* 0x2B */    {'\\',    '|'},  90 /* 0x2C */    {'z',    'Z'},  91 /* 0x2D */    {'x',    'X'},  92 /* 0x2E */    {'c',    'C'},  93 /* 0x2F */    {'v',    'V'},  94 /* 0x30 */    {'b',    'B'},  95 /* 0x31 */    {'n',    'N'},  96 /* 0x32 */    {'m',    'M'},  97 /* 0x33 */    {',',    '<'},  98 /* 0x34 */    {'.',    '>'},  99 /* 0x35 */    {'/',    '?'}, 100 /* 0x36 */ {shift_r_char, shift_r_char}, 101 /* 0x37 */    {'*',    '*'}, 102 /* 0x38 */ {alt_l_char, alt_l_char}, 103 /* 0x39 */    {' ',    ' '}, 104 /* 0x3A */ {caps_lock_char, caps_lock_char} 105 }; 106 
107 /*鍵盤中斷處理程序*/
108 static void intr_keyboard_handler(void) 109 { 110     bool ctrl_down_last = ctrl_status; 111     bool shift_down_last = shift_status; 112     bool caps_lock_last = caps_lock_status; 113 
114 
115     bool break_code; 116     uint16_t scancode = inb(KBD_BUF_PORT); 117 
118     /*若掃描碼scancode是以e0開頭的,表示此鍵的按下將產生多個掃描碼 119  所以馬上結束此次中斷處理函數,等待下一個掃描碼進入*/
120     if (scancode == 0xe0) { 121         ext_scancode = true; 122         return; 123  } 124 
125     /*如果賞賜是以0xe0開頭的,將掃描碼合並*/
126     if (ext_scancode) { 127         scancode = ((0xe00) | scancode); 128         ext_scancode = false; 129  } 130 
131     break_code = ((scancode & 0x0080) != 0); 132     if (break_code) { 133         uint16_t make_code = (scancode &= 0xff7f); //多字節不處理
134         if(make_code == ctrl_l_make || make_code == ctrl_r_make) { 135             ctrl_status = false; 136  } 137         else if (make_code == shift_l_make || make_code == shift_r_make) { 138             shift_status = false; 139  } 140         else if (make_code == alt_l_make || make_code == alt_r_make) { 141             alt_status = false; 142  } 143         return; 144  } 145     else if((scancode > 0x00 && scancode < 0x3b) || (scancode == alt_r_make) || (scancode == ctrl_r_make)) { 146         bool shift = false; //先默認設置成false
147         if ((scancode < 0x0e) || (scancode == 0x29) || (scancode == 0x1a) || \ 148         (scancode == 0x1b) || (scancode == 0x2b) || (scancode == 0x27) || \ 149         (scancode == 0x28) || (scancode == 0x33) || (scancode == 0x34) || \ 150         (scancode == 0x35)) 151  { 152             if (shift_down_last) { 153                 shift = true; 154  } 155         } else { 156             if (shift_down_last && caps_lock_last) { 157                 shift = false; //效果確實是這樣子的 我試了一下
158  } 159             else if(shift_down_last || caps_lock_last) { 160                 shift = true; //其中任意一個都是大寫的作用
161  } 162             else shift = false; 163  } 164         
165         uint8_t index = (scancode & 0x00ff); 166         char cur_char = keymap[index][shift]; 167 
168  put_char(cur_char); 169  } 170     return; 171 } 172 
173 
174 /*鍵盤初始化*/
175 void keyboard_init(void) 176 { 177     put_str("keyboard init start\n"); 178     register_handler(0x21, intr_keyboard_handler); 179     put_str("keyboard init done\n"); 180 }
keyboard.c

   

  此時可以發現,我們在鍵盤上按下鍵,屏幕上能相應地輸出字符。

四、實現環形輸入緩沖區

  雖然我們已經實現了鍵盤驅動,但是目前能實現的功能僅僅是在屏幕上輸出我們所按下的按鍵,但是並沒有什么實用的地方。我們在鍵盤上操作是為了能和系統進行交互,而交互過程一般都是鍵入各種shell命令,然后shell解析並且執行。

  所以我們需要實現一個緩沖區,在按鍵的中斷處理函數中將輸入的按鍵信息保存在緩沖區中,將來實現的shell進程在該緩沖區中讀取數據並且輸出到屏幕上,等到我們按下了回車后,就將前面讀取到的字符解析去處理。雖然我們還沒有實現shell進程,但是我們可以新建線程來讀取數據,測試緩沖區的功能。

  所以下面的代碼便是實現緩沖區,在project/kernel目錄下新建ioqueue.c和ioqueue.h文件。

 1 #include "ioqueue.h"
 2 #include "interrupt.h"
 3 #include "global.h"
 4 #include "debug.h"
 5 #include "thread.h"
 6 #include "stdbool.h"
 7 #include "stddef.h"
 8 
 9 /*初始化io隊列ioq*/
10 void ioqueue_init(struct ioqueue *ioq) 11 { 12     lock_init(&ioq->lock); 13     ioq->consumer = ioq->producer = NULL; 14     ioq->head = ioq->tail = 0;      /*隊列的首尾指針都指向緩沖區數組的第0個位置*/      
15 } 16 
17 /*返回pos在緩沖區的下一個位置*/
18 static int32_t next_pos(int32_t pos) 19 { 20     return ((pos + 1) % bufsize); 21 } 22 
23 /*判斷隊列是否已滿*/
24 bool ioq_full(struct ioqueue *ioq) 25 { 26     //return ((ioq->head + 1) % bufsize == ioq->tail) ? true : false;
27     ASSERT(intr_get_status() == INTR_OFF); 28     return next_pos(ioq->head) == ioq->tail; 29 } 30 
31 /*判斷隊列是否為空*/
32 bool ioq_empty(struct ioqueue *ioq) 33 { 34     ASSERT(intr_get_status() == INTR_OFF); 35     return ioq->head == ioq->tail; 36 } 37 
38 /*使當前生產者或消費者在此緩沖區上等待*/
39 static void ioq_wait(struct task_struct **waiter) 40 { 41     ASSERT(*waiter == NULL && waiter != NULL); 42     *waiter = running_thread(); 43  thread_block(TASK_BLOCKED); 44 } 45 
46 /*喚醒waiter*/
47 static void wakeup(struct task_struct **waiter) 48 { 49     ASSERT(*waiter != NULL); 50     thread_unblock(*waiter); 51     *waiter = NULL; 52 } 53 
54  
55 /*消費者從ioq隊列中獲取一個字符*/
56 char ioq_getchar(struct ioqueue *ioq) 57 { 58     ASSERT(intr_get_status() == INTR_OFF); 59 
60     while (ioq_empty(ioq)) { 61         lock_acquire(&ioq->lock); 62         ioq_wait(&ioq->consumer); 63         lock_release(&ioq->lock); 64  } 65 
66     char byte = ioq->buf[ioq->tail]; 67     ioq->tail = next_pos(ioq->tail); 68 
69     if (ioq->producer != NULL) { 70         wakeup(&ioq->producer); 71  } 72     return byte; 73 } 74 
75 
76 /*生產者往ioq隊列中寫入一個字符byte*/
77 void ioq_putchar(struct ioqueue *ioq, char byte) 78 { 79     while (ioq_full(ioq)) { 80         lock_acquire(&ioq->lock); 81         ioq_wait(&ioq->producer); 82         lock_release(&ioq->lock); 83  } 84     ioq->buf[ioq->head] = byte; 85     ioq->head = next_pos(ioq->head); 86 
87     if (ioq->consumer != NULL) { 88         wakeup(&ioq->consumer); 89  } 90 }
ioqueue.c
 1 #ifndef __KERNEL_IOQUEUE_H  2 #define  __KERNEL_IOQUEUE_H
 3 #include "sync.h"
 4 #include "stdint.h"
 5 
 6 #define bufsize 64
 7 
 8 /*環形隊列*/
 9 struct ioqueue { 10 /*生產者消費問題*/
11     struct lock lock; 12     struct task_struct *producer; 13     struct task_struct *consumer; 14     char buf[bufsize]; 15  int32_t head; 16  int32_t tail; 17 }; 18 
19 void ioq_putchar(struct ioqueue *ioq, char byte); 20 char ioq_getchar(struct ioqueue *ioq); 21 static void wakeup(struct task_struct **waiter); 22 static void ioq_wait(struct task_struct **waiter); 23 bool ioq_empty(struct ioqueue *ioq); 24 bool ioq_full(struct ioqueue *ioq); 25 static int32_t next_pos(int32_t pos); 26 void ioqueue_init(struct ioqueue *ioq); 27 
28 #endif
ioqueue.h

五、運行測試

  上面我們已經實現了環形輸入緩沖區,接下來我們在main函數中新建兩個線程,這兩個線程不停地從緩沖區中一個字節一個字節地取數據,如果沒有便阻塞,直到緩沖區中又有數據。除此之外還需要修改interrupt.c文件,我們前面只開啟了鍵盤中斷,現在加入線程調度,所以需要開啟時鍾中斷。修改的代碼一並如下:

 1 ...  2 
 3 /* 初始化可編程中斷控制器8259A */
 4 static void pic_init(void) {  5    /* 初始化主片 */
 6    outb (PIC_M_CTRL, 0x11);   // ICW1: 邊沿觸發,級聯8259, 需要ICW4.
 7    outb (PIC_M_DATA, 0x20);   // ICW2: 起始中斷向量號為0x20,也就是IR[0-7] 為 0x20 ~ 0x27.
 8    outb (PIC_M_DATA, 0x04);   // ICW3: IR2接從片. 
 9    outb (PIC_M_DATA, 0x01);   // ICW4: 8086模式, 正常EOI
10 
11    /* 初始化從片 */
12    outb (PIC_S_CTRL, 0x11);    // ICW1: 邊沿觸發,級聯8259, 需要ICW4.
13    outb (PIC_S_DATA, 0x28);    // ICW2: 起始中斷向量號為0x28,也就是IR[8-15] 為 0x28 ~ 0x2F.
14    outb (PIC_S_DATA, 0x02);    // ICW3: 設置從片連接到主片的IR2引腳
15    outb (PIC_S_DATA, 0x01);    // ICW4: 8086模式, 正常EOI
16 
17    /*打開鍵盤和時鍾中斷*/
18    outb (PIC_M_DATA, 0xfc); 19    outb (PIC_S_DATA, 0xff); 20    
21    put_str("pic_init done\n"); 22 } 23 
24 ...
interrupt.c
 1 #include "print.h"
 2 #include "init.h"
 3 #include "memory.h"
 4 #include "thread.h"
 5 #include "list.h"
 6 #include "interrupt.h"
 7 #include "console.h"
 8 #include "ioqueue.h"
 9 #include "keyboard.h"
10 
11 void k_thread_a(void *arg); 12 void k_thread_b(void *arg); 13 
14 int main(void) 15 { 16     put_str("HELLO KERNEL\n"); 17  init_all(); 18     thread_start("k_thread_a", 31, k_thread_a, "ThreadA_"); 19     thread_start("k_thread_b", 8, k_thread_b, "ThreadB_"); 20  intr_enable(); 21     while(1); 22 } 23 
24 /*在線程中運行的函數k_thread_a*/
25 void k_thread_a(void *arg) 26 { 27     char *para = arg; 28     while(1) { 29         enum intr_status old_status = intr_disable(); 30         if (!ioq_empty(&kbd_buf)) { 31  console_put_str(arg); 32             char byte = ioq_getchar(&kbd_buf); 33             console_put_char(byte); 34             console_put_str("\n"); 35  } 36  intr_set_status(old_status); 37  } 38 } 39 
40 /*在線程中運行的函數k_thread_b*/
41 void k_thread_b(void *arg) 42 { 43     char *para = arg; 44     while(1) { 45         enum intr_status old_status = intr_disable(); 46         if (!ioq_empty(&kbd_buf)) { 47  console_put_str(arg); 48             char byte = ioq_getchar(&kbd_buf); 49             console_put_char(byte); 50             console_put_str("\n"); 51  } 52  intr_set_status(old_status); 53  } 54 }
main.c
1 #ifndef __KERNEL_KEYBOARD_H 2 #define  __KERNEL_KEYBOARD_H
3 
4 void keyboard_init(void); 5 static void intr_keyboard_handler(void); 6 extern struct ioqueue kbd_buf; 7 #endif
keyboard.h 
 1 #include "init.h"
 2 #include "print.h"
 3 #include "interrupt.h"
 4 #include "timer.h"
 5 #include "memory.h"
 6 #include "thread.h"
 7 #include "list.h"
 8 #include "console.h"
 9 #include "keyboard.h"
10 
11 void init_all(void) 12 { 13     put_str("init_all\n"); 14  idt_init(); 15  timer_init(); 16  mem_init(); 17  thread_init(); 18  console_init(); 19  keyboard_init(); 20 }
init.c 
 1 #include "keyboard.h"
 2 #include "print.h"
 3 #include "interrupt.h"
 4 #include "io.h"
 5 #include "global.h"
 6 #include "stdint.h"
 7 #include "ioqueue.h"
 8 
 9 #define KBD_BUF_PORT  0x60
 10 
 11 /*用轉移字符定義部分控制字符*/
 12 #define esc        '\033'
 13 #define backspace  '\b'
 14 #define tab        '\t'
 15 #define enter      '\r'
 16 #define delete     '\0177'
 17 
 18 /*以下不可見字符一律為0*/
 19 #define char_invisible  0
 20 #define ctrl_l_char     char_invisible
 21 #define ctrl_r_char     char_invisible
 22 #define shift_l_char    char_invisible
 23 #define shift_r_char    char_invisible
 24 #define alt_l_char      char_invisible
 25 #define alt_r_char      char_invisible
 26 #define caps_lock_char  char_invisible
 27 
 28 /*定義控制字符的通碼和斷碼*/
 29 #define shift_l_make     0x2a
 30 #define shift_r_make     0x36
 31 #define alt_l_make       0x38
 32 #define alt_r_make       0xe038
 33 #define alt_r_break      0xe0b8
 34 #define ctrl_l_make      0x1d
 35 #define ctrl_r_make      0xe01d
 36 #define ctrl_r_break     0xe09d
 37 #define caps_lock_make   0x3a
 38  
 39 /*定義以下變量記錄相應鍵是否按下的狀態*/
 40 static bool ctrl_status, shift_status, alt_status, caps_lock_status, ext_scancode;  41 
 42 struct ioqueue kbd_buf;  43 
 44 /*以通碼make_code為索引的二維數組*/
 45 static char keymap[][2] = {  46 /*掃描碼未與shift組合*/
 47 /* 0x00 */    {0,    0},  48 /* 0x01 */ {esc, esc},  49 /* 0x02 */    {'1',    '!'},  50 /* 0x03 */    {'2',    '@'},  51 /* 0x04 */    {'3',    '#'},  52 /* 0x05 */    {'4',    '$'},  53 /* 0x06 */    {'5',    '%'},  54 /* 0x07 */    {'6',    '^'},  55 /* 0x08 */    {'7',    '&'},  56 /* 0x09 */    {'8',    '*'},  57 /* 0x0A */    {'9',    '('},  58 /* 0x0B */    {'0',    ')'},  59 /* 0x0C */    {'-',    '_'},  60 /* 0x0D */    {'=',    '+'},  61 /* 0x0E */ {backspace, backspace},  62 /* 0x0F */ {tab, tab},  63 /* 0x10 */    {'q',    'Q'},  64 /* 0x11 */    {'w',    'W'},  65 /* 0x12 */    {'e',    'E'},  66 /* 0x13 */    {'r',    'R'},  67 /* 0x14 */    {'t',    'T'},  68 /* 0x15 */    {'y',    'Y'},  69 /* 0x16 */    {'u',    'U'},  70 /* 0x17 */    {'i',    'I'},  71 /* 0x18 */    {'o',    'O'},  72 /* 0x19 */    {'p',    'P'},  73 /* 0x1A */    {'[',    '{'},  74 /* 0x1B */    {']',    '}'},  75 /* 0x1C */ {enter, enter},  76 /* 0x1D */ {ctrl_l_char, ctrl_l_char},  77 /* 0x1E */    {'a',    'A'},  78 /* 0x1F */    {'s',    'S'},  79 /* 0x20 */    {'d',    'D'},  80 /* 0x21 */    {'f',    'F'},  81 /* 0x22 */    {'g',    'G'},  82 /* 0x23 */    {'h',    'H'},  83 /* 0x24 */    {'j',    'J'},  84 /* 0x25 */    {'k',    'K'},  85 /* 0x26 */    {'l',    'L'},  86 /* 0x27 */    {';',    ':'},  87 /* 0x28 */    {'\'',    '"'},  88 /* 0x29 */    {'`',    '~'},  89 /* 0x2A */ {shift_l_char, shift_l_char},  90 /* 0x2B */    {'\\',    '|'},  91 /* 0x2C */    {'z',    'Z'},  92 /* 0x2D */    {'x',    'X'},  93 /* 0x2E */    {'c',    'C'},  94 /* 0x2F */    {'v',    'V'},  95 /* 0x30 */    {'b',    'B'},  96 /* 0x31 */    {'n',    'N'},  97 /* 0x32 */    {'m',    'M'},  98 /* 0x33 */    {',',    '<'},  99 /* 0x34 */    {'.',    '>'}, 100 /* 0x35 */    {'/',    '?'}, 101 /* 0x36 */ {shift_r_char, shift_r_char}, 102 /* 0x37 */    {'*',    '*'}, 103 /* 0x38 */ {alt_l_char, alt_l_char}, 104 /* 0x39 */    {' ',    ' '}, 105 /* 0x3A */ {caps_lock_char, caps_lock_char} 106 }; 107 
108 /*鍵盤中斷處理程序*/
109 static void intr_keyboard_handler(void) 110 { 111     bool ctrl_down_last = ctrl_status; 112     bool shift_down_last = shift_status; 113     bool caps_lock_last = caps_lock_status; 114 
115 
116     bool break_code; 117     uint16_t scancode = inb(KBD_BUF_PORT); 118 
119     /*若掃描碼scancode是以e0開頭的,表示此鍵的按下將產生多個掃描碼 120  所以馬上結束此次中斷處理函數,等待下一個掃描碼進入*/
121     if (scancode == 0xe0) { 122         ext_scancode = true; 123         return; 124  } 125 
126     /*如果賞賜是以0xe0開頭的,將掃描碼合並*/
127     if (ext_scancode) { 128         scancode = ((0xe00) | scancode); 129         ext_scancode = false; 130  } 131 
132     break_code = ((scancode & 0x0080) != 0); 133     if (break_code) { 134         uint16_t make_code = (scancode &= 0xff7f); //多字節不處理
135         if(make_code == ctrl_l_make || make_code == ctrl_r_make) { 136             ctrl_status = false; 137  } 138         else if (make_code == shift_l_make || make_code == shift_r_make) { 139             shift_status = false; 140  } 141         else if (make_code == alt_l_make || make_code == alt_r_make) { 142             alt_status = false; 143  } 144         return; 145  } 146     else if((scancode > 0x00 && scancode < 0x3b) || (scancode == alt_r_make) || (scancode == ctrl_r_make)) { 147         bool shift = false; //先默認設置成false
148         if ((scancode < 0x0e) || (scancode == 0x29) || (scancode == 0x1a) || \ 149         (scancode == 0x1b) || (scancode == 0x2b) || (scancode == 0x27) || \ 150         (scancode == 0x28) || (scancode == 0x33) || (scancode == 0x34) || \ 151         (scancode == 0x35)) 152  { 153             if (shift_down_last) { 154                 shift = true; 155  } 156         } else { 157             if (shift_down_last && caps_lock_last) { 158                 shift = false; //效果確實是這樣子的 我試了一下
159  } 160             else if(shift_down_last || caps_lock_last) { 161                 shift = true; //其中任意一個都是大寫的作用
162  } 163             else shift = false; 164  } 165         
166         uint8_t index = (scancode & 0x00ff); 167         char cur_char = keymap[index][shift]; 168     
169         if (cur_char) { 170             if (!ioq_full(&kbd_buf)) { 171                 ioq_putchar(&kbd_buf, cur_char); 172  } 173             return; 174  } 175     
176     if(scancode == ctrl_l_make || scancode == ctrl_r_make) 177         ctrl_status = true; 178     else if(scancode == shift_l_make || scancode == shift_r_make) 179             shift_status = true; 180     else if(scancode == alt_l_make || scancode == alt_r_make) 181         alt_status = true; 182     else if(scancode == caps_lock_make) 183         caps_lock_status = !caps_lock_status; 184     else put_str("unknown key\n"); 185  } 186     return; 187 } 188 
189 
190 /*鍵盤初始化*/
191 void keyboard_init(void) 192 { 193     put_str("keyboard init start\n"); 194     ioqueue_init(&kbd_buf); 195     register_handler(0x21, intr_keyboard_handler); 196     put_str("keyboard init done\n"); 197 }
keyboard.c

  

   本回到此結束,預知后事如何,請看下回分解。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM