📄 kbd.c
字号:
//**********************************************************************
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// Copyright(c) Cirrus Logic Corporation 2001, All Rights Reserved
//
// File: kbd.c Module: keyboard driver
//
// Revision:
//
// Synopsis: Low level keyboard drivers. This file provides the common control
// of the keyboard hardware.
// Most of these methods are exposed for external access.
// These driver methods must be supported at an OS driver level.
//
// EXAMPLE:
// void kbd_main(void)
// {
// byte rr1,rr2,cc1,cc2;
//
// GKBD_SetDebounce(0xfc);
// GKBD_SetPreScale(0xfa);
//
// rr1=GKBD_GetLastR1();
// rr2=GKBD_GetLastR2();
// cc1=GKBD_GetLastC1();
// cc2=GKBD_GetLastC2();
//
// // initialize the keyboard device
// GKBD_Enable(TRUE);
//
// // loop
// while (1)
// {
// byte r1 = GKBD_GetLastR1();
// byte r2 = GKBD_GetLastR2();
// byte c1 = GKBD_GetLastC1();
// byte c2 = GKBD_GetLastC2();
//
// if ((rr1!=r1 || cc1!=c1) &&
// (rr2!=r2 || cc2!=c2))
// {
// rr1=r1;
// cc1=c1;
// rr2=r2;
// cc2=c2;
// }
// }
//
//
// return;
// }
//
//
//**********************************************************************
/*=============================================================================
> INCLUDE FILES <
-----------------------------------------------------------------------------*/
#include <windows.h>
#include <hwdefs.h>
#include "kbd.h" // keyboard defines and structures
/*=============================================================================
> DEFINITIONS LOCAL TO THIS FILE <
-----------------------------------------------------------------------------*/
/*=============================================================================
> FUNCTION PROTOTYPES LOCAL TO THIS FILE <
-----------------------------------------------------------------------------*/
static unsigned char KBD_IsIrqActive(unsigned long result);
static unsigned char KBD_IsOneKey(unsigned long result);
static unsigned char KBD_IsTwoKey(unsigned long result);
static unsigned char KBD_GetFirstRow(unsigned long result);
static unsigned char KBD_GetSecondRow(unsigned long result);
static unsigned char KBD_GetFirstCol(unsigned long result);
static unsigned char KBD_GetSecondCol(unsigned long result);
/*=============================================================================
> STRUCTURES LOCAL TO THIS FILE <
-----------------------------------------------------------------------------*/
/*=============================================================================
> DATA DECLARATIONS LOCAL TO THIS FILE <
-----------------------------------------------------------------------------*/
static unsigned char g_nFirstRow=0,g_nFirstCol=0;
static unsigned char g_nSecondRow=0,g_nSecondCol=0;
static unsigned char g_bNoKey=FALSE;
//****************************************************************************
// GKBD_HandleIrq
//****************************************************************************
// This should be called as the IRQ handler. This function will grab and
// store button press results, check and make sure values are correct
// and reset the IRQ so that other KBD events can be processed.
//
void GKBD_HandleIrq( void )
{
//
// get the keypress results register
//
unsigned long result = *KEYBOARD_KEYREG;
//
// if a IRQ was signaled by this device
//
if (KBD_IsIrqActive(result))
{
// if two keys were pressed at the same time
// we need to grab the second keypress first.
// while this is not a requirement, it is
// just done this why to make the code
// expediant
if (KBD_IsTwoKey(result))
{
// get the second key values
g_nSecondRow=KBD_GetSecondRow(result)+1;
g_nSecondCol=KBD_GetSecondCol(result)+1;
}
// make sure that the first keypress
// is available
else if (!KBD_IsOneKey(result))
{
g_bNoKey=TRUE;
// get the second key values
g_nSecondRow=0;
g_nSecondCol=0;
return;
}
// get the first key press
g_nFirstRow=KBD_GetFirstRow(result)+1;
g_nFirstCol=KBD_GetFirstCol(result)+1;
// set global marker that no keys
// are currently pressed
g_bNoKey=FALSE;
}
return;
}
//****************************************************************************
// GKBD_Enable
//****************************************************************************
// Sets bState to TRUE or FALSE. This function is used to enable the keypad
// IRQ only.
//
void GKBD_Enable
(
unsigned char bState // TRUE = enable IRQ handling, FALSE for not
)
{
// set KBD bit int IRQ mask
/*
if (bState)
*IRQ_MASK_A |= INT_KBD;
else
*IRQ_MASK_A &= ~INT_KBD;
*/
return;
}
//****************************************************************************
// GKBD_SetDebounce
//****************************************************************************
// This function is used to preload the de-bounce counter. The de-bounce
// counter counts the number of consectutive scans that decoded the same keys.
// Terminal count for the de-bounce counter is 0xff. Terminal count indirectly
// generates key scan interrupt. A pre-load value of 0xfc will cause the
// key scan circuitry to count 3 identical consecutive keypad scans.
//
void GKBD_SetDebounce
(
unsigned char nDebounce // debounce value in scan counts
)
{
unsigned long nValue=0;
unsigned long nResult = *KEYBOARD_SCANINIT;
//
// mask off any bits higher then the 8th bit
//
nDebounce &= 0xff;
//
// move "passed" debounce value into internal storage
//
nValue |= nDebounce;
//
// move our new value into the low byte of the high word
//
nValue <<= 16;
//
// clear bits
//
nResult &= ~nValue;
//
// OR our new debounce value into the results value
//
nResult |= nValue;
//
// set the return results
//
*KEYBOARD_SCANINIT = nResult;
return;
}
//****************************************************************************
// GKBD_GetDebounce
//****************************************************************************
// This function is used to get the de-bounce scan counter.
// refer to GKBD_SetDebounce for a full explaination of the
// debounce value.
//
//
unsigned char GKBD_GetDebounce (void)
{
unsigned long result = *KEYBOARD_SCANINIT;
// shift upper 16 bits into lower
result >>= 16;
// mask out any unwanted bits
result &= 0xff;
return ((unsigned char)result);
}
//****************************************************************************
// GKBD_Enable3KeyReset
//****************************************************************************
// Use this method to predefined three-key reset feature. This will
// allow/protect a three-key press used to signal the internal watchdog for
// a complete system reset.
//
void GKBD_Enable3KeyReset
(
unsigned char bState // set to TRUE to enable, and FALSE to disable
)
{
#if 0
unsigned long nResult = KBD_GetRegisterValue(KBD_SCANINIT);
// remove or set the bit
// on the hardware 0 is enabled, and 1 is disable
if (bState)
nResult &= ~KBD_DIS3KY;
else
nResult |= KBD_DIS3KY;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -