科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道基础软件龙芯软件开发(29)-- 键盘初始化

龙芯软件开发(29)-- 键盘初始化

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

也许看到前面有一些键盘初始化了,但那里的初始化是设置南桥接收键盘数据输入和输出而已,只是一个接口,或者是一个代理。

作者:caimouse 来源:CSDN 2008年1月22日

关键字: 初始化 键盘 Linux

  • 评论
  • 分享微博
  • 分享邮件
如果你的记性好,也许看到前面有一些键盘初始化了,但那里的初始化是设置南桥接收键盘数据输入和输出而已,只是一个接口,或者是一个代理。在这里才是开始真正初始化键盘,让键盘工作在合适的方式。在电脑里,显示卡输出信息给人看,而键盘扮演着另一个角色,就是让人们可以输入东西,控制电脑做各种各样的事情。虽然电脑的历史也有60年了,并且技术也在飞速地发展,但是键盘还是作为重要的输入方式。希望我们大家能发明一种更好的输入方式,语音输入?还是脑电波输入?也许很快就到来。下面来仔细地分析键盘初始化的实现,代码如下:
int kbd_initialize(void)
{
    int status;
    int count;
   
    /* Flush the buffer */
    kbd_clear_input();
上面清除键盘输入缓冲里的数据。由于键盘采用环形缓冲作为输入缓冲区,如果不清除输入的内容,就不是当时按键的内容了。
 
    /*
     * Test the keyboard interface.
     * This seems to be the only way to get it going.
     * If the test is successful a x55 is placed in the input buffer.
     */
    kbd_write_command_w(KBD_CCMD_SELF_TEST);
    if (kbd_wait_for_input() != 0x55) {
       printf("Self test cmd failed,ignored!\n");
       //return 1;
    }
上面进行键盘测试。
 
 
    /*
     * Perform a keyboard interface test. This causes the controller
     * to test the keyboard clock and data lines. The results of the
     * test are placed in the input buffer.
     */
    kbd_write_command_w(KBD_CCMD_KBD_TEST);
    if (kbd_wait_for_input() != 0x00) {
       printf("KBD_TEST cmd failed,ignored!\n");
       //return 1;
    }
上面进行键盘接口测试。
 
    /*
     * Enable the keyboard by allowing the keyboard clock to run.
     */
    kbd_write_command_w(KBD_CCMD_KBD_ENABLE);
上面设置键盘时钟开始运行。
 
    /*
     * Reset keyboard. If the read times out
     * then the assumption is that no keyboard is
     * plugged into the machine.
     * This defaults the keyboard to scan-code set 2.
     *
     * Set up to try again if the keyboard asks for RESEND.
     */
    count = 0;
    do {
       kbd_write_output_w(KBD_CMD_RESET);
       status = kbd_wait_for_input();
       if (status == KBD_REPLY_ACK)
           break;
       if (status != KBD_REPLY_RESEND) {
           printf("reset failed\n");
           if (++count > 1) break;
           //return 2;
       }
    } while (1);
 
    if (kbd_wait_for_input() != KBD_REPLY_POR) {
       printf("NO POR, ignored!\n");
       //return 3;
    }
上面进行键盘复位设置。
 
 
    /*
     * Set keyboard controller mode. During this, the keyboard should be
     * in the disabled state.
     *
     * Set up to try again if the keyboard asks for RESEND.
     */
    count = 0;
    do {
       kbd_write_output_w(KBD_CMD_DISABLE);
       status = kbd_wait_for_input();
       if (status == KBD_REPLY_ACK)
           break;
       if (status != KBD_REPLY_RESEND){
           printf("disable failed\n");
           if (++count > 1) break;
           //return 4;
       }
    } while (1);
 
    kbd_write_command_w(KBD_CCMD_WRITE_MODE);
    kbd_write_output_w(KBD_MODE_KBD_INT
                 | KBD_MODE_SYS
                 | KBD_MODE_DISABLE_MOUSE
                 | KBD_MODE_KCC);
上面设置键盘的控制模式。
 
#if 1
    /* ibm powerpc portables need this to use scan-code set 1 -- Cort */
    if (!(kbd_write_command_w_and_wait(KBD_CCMD_READ_MODE) & KBD_MODE_KCC))
    {
       /*
        * If the controller does not support conversion,
        * Set the keyboard to scan-code set 1.
        */
       kbd_write_output_w(0xF0);
       kbd_wait_for_input();
       kbd_write_output_w(0x01);
       kbd_wait_for_input();
    }
#endif
    if (kbd_write_output_w_and_wait(KBD_CMD_ENABLE) != KBD_REPLY_ACK) {
       return 5;
    }
上面设置键盘输入的编码方式。
 
 
    /*
     * Finally, set the typematic rate to maximum.
     */
    if (kbd_write_output_w_and_wait(KBD_CMD_SET_RATE) != KBD_REPLY_ACK) {
       return 6;
    }
    if (kbd_write_output_w_and_wait(0x00) != KBD_REPLY_ACK) {
       return 7;
    }
上面设置键盘的按键速度。
 
    return 0;
}
 
通过上面的初始化设置,就可以接收键盘输入了。
 

查看本文来源
    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章