📄 keypadtest.c
字号:
/******************************************************************************
**
** COPYRIGHT (C) 2002 Intel Corporation.
**
** This software as well as the software described in it is furnished under
** license and may only be used or copied in accordance with the terms of the
** license. The information in this file is furnished for informational use
** only, is subject to change without notice, and should not be construed as
** a commitment by Intel Corporation. Intel Corporation assumes no
** responsibility or liability for any errors or inaccuracies that may appear
** in this document or any software that may be provided in association with
** this document.
** Except as permitted by such license, no part of this document may be
** reproduced, stored in a retrieval system, or transmitted in any form or by
** any means without the express written consent of Intel Corporation.
**
** FILENAME: KeypadTest.c
**
** PURPOSE: This file contains the Bulverde Keypad POST code for
** the Intel(r) XScale(tm) Microarchitecture.
**
** LAST MODIFIED: $Modtime: 1/19/04 9:54a $
******************************************************************************/
/*
*******************************************************************************
* HEADER FILES
*******************************************************************************
*/
#include "string.h"
#include "stdio.h"
#include <stdlib.h>
#include <ctype.h>
#include "systypes.h"
//#include "timedelays.h"
//#include "dm_errors.h"
//#include "xsuart.h"
//#include "xsffuart.h"
//#include "xsbtuart.h"
//#include "xsstuart.h"
//#include "uarts.h"
//#include "DM_SerialInOut.h"
//#include "dm_debug.h"
//#include "platform.h"
//#include "cotulla.h"
#include "KeypadTest.h"
//#include "XsMemCtrl.h"
//#include "boardcontrol.h"
#include "xsClkMgr.h"
#include "utils.h"
/*
*******************************************************************************
* GLOBAL DEFINITIONS
*******************************************************************************
*/
/*
*******************************************************************************
* LOCAL DEFINITIONS
*******************************************************************************
*/
/*
*******************************************************************************
* Bulverde MSHC Registers
*******************************************************************************
*/
#define KPC_REG (*(volatile UINT32 *) 0x41500000)
#define KPDK_REG (*(volatile UINT32 *) 0x41500008)
#define KPREC_REG (*(volatile UINT32 *) 0x41500010)
#define KPMK_REG (*(volatile UINT32 *) 0x41500018)
#define KPAS_REG (*(volatile UINT32 *) 0x41500020)
#define KPASMKP0_REG (*(volatile UINT32 *) 0x41500028)
#define KPASMKP1_REG (*(volatile UINT32 *) 0x41500030)
#define KPASMKP2_REG (*(volatile UINT32 *) 0x41500038)
#define KPASMKP3_REG (*(volatile UINT32 *) 0x41500040)
#define KPKDI_REG (*(volatile UINT32 *) 0x41500048)
/*
*******************************************************************************
* Bulverde Keypad Register Field Macros
*******************************************************************************
*/
#define KEYP_KPC_ASACT (0x1 << 29) // Automatic Scan on Activity
#define KEYP_KPC_MKRN (0x3 << 26) // Number of Keypad Rows, hzh
#define KEYP_KPC_MKCN (0x2 << 23) // Number of Keypad Columns, hzh
#define KEYP_KPC_MI (0x1 << 22) // Matrix interrupt bit
#define KEYP_KPC_IMKP (0x1 << 21) // Ignore Multiple Key Press
#define KEYP_KPC_MS7 (0x1 << 20) // Matrix scan line 7
#define KEYP_KPC_MS6 (0x1 << 19) // Matrix scan line 6
#define KEYP_KPC_MS5 (0x1 << 18) // Matrix scan line 5
#define KEYP_KPC_MS4 (0x1 << 17) // Matrix scan line 4
#define KEYP_KPC_MS3 (0x1 << 16) // Matrix scan line 3
#define KEYP_KPC_MS2 (0x1 << 15) // Matrix scan line 2
#define KEYP_KPC_MS1 (0x1 << 14) // Matrix scan line 1
#define KEYP_KPC_MS0 (0x1 << 13) // Matrix scan line 0
#define KEYP_KPC_ME (0x1 << 12) // Matrix Keypad Enable
#define KEYP_KPC_MIE (0x1 << 11) // Matrix Keypad Int Enable
#define KEYP_KPC_DKIN (0x2 << 6) // Direct Key Number -1
#define KEYP_KPC_DI (0x1 << 5) // Direct key interrupt bit
#define KEYP_KPC_REZB (0x1 << 4) // Rotary Encoder Zero Debounce
#define KEYP_KPC_DEE0 (0x1 << 2) // Rotary Encoder 0 Enable
#define KEYP_KPC_DE (0x1 << 1) // Direct Keypad Enable
#define KEYP_KPC_DIE (0x1 << 0) // Direct Keypad Interrupt Enable
/*
*******************************************************************************
* Keypad Register Macros
*******************************************************************************
*/
/*
*******************************************************************************
* Bulverde GPIO Registers used by the Keypad
*******************************************************************************
*/
#define GPDR2_REG (*(volatile UINT32 *) 0x40e00014)
#define GPDR3_REG (*(volatile UINT32 *) 0x40e0010c)
#define GAFR2_U_REG (*(volatile UINT32 *) 0x40e00068)
#define GAFR3_L_REG (*(volatile UINT32 *) 0x40e0006c)
#define LED_MASK (*(volatile UINT32 *) 0x08000040)
#define HEX_LEDS (*(volatile UINT32 *) 0x08000010)
#define GPLR0 *(volatile UINT *)0x40E00000
#define GPLR2 *(volatile UINT *)0x40E00008
#define GPSR0 *(volatile UINT *)0x40E00018
#define GPCR0 *(volatile UINT *)0x40E00024
/*
*******************************************************************************
* Mainstone Platform Registers for the Keypad
*******************************************************************************
*/
/*
*******************************************************************************
* Error sub-location codes for ERR_L_KEYPAD location code
*******************************************************************************
*/
#define ERR_S_KEYPAD_INIT 0x01 // HW Initialization
#define ERR_S_KEYPAD_PASS 0x08 // Test Result = PASS
#define ERR_S_KEYPAD_FAIL 0x09 // Test Result = FAIL
/*
*******************************************************************************
* Return Codes
*******************************************************************************
*/
#define KP_TEST_SUCCESS 0
#define KP_TEST_FAILURE 1
/*
*******************************************************************************
*
* FUNCTION:
* PostKeyopadTest
*
* DESCRIPTION:
* Test the Mainstone Keypad.
*
* INPUT PARAMETERS:
* None.
*
* RETURNS:
* 1 if error, 0 if no error.
*
* GLOBAL EFFECTS:
* None.
*
* ASSUMPTIONS:
* None.
*
* CALLS:
* None.
*
* CALLED BY:
* Post main function.
*
* PROTOTYPE:
* int PostKeypadTest(void);
*
*******************************************************************************
*/
int PostKeypadTest(void) //modified by hzh
{
/* masked by zzm
#if 0
//ErrorT retVal;
int KeypadTestResult = KP_TEST_SUCCESS; // Overall Keypad Test Result
int KeysPressed = 0; // Record of keys pressed
int time; // timer
int scroll_min; // min scroll wheel value
int scroll_max; // max scroll wheel value
int mask; // Work register for hex LED masking
char KeyData; // Data read from Keypad
printf(" Testing Keypad\r\n");
// Initialize the Keypad
PostDisplayProgress(ERR_L_KEYPAD, ERR_S_KEYPAD_INIT, __LINE__);
KeypadHWSetup();
// Print Instructions
printf(" User! Press each key on the keypad in sequence:\r\n");
printf(" User! 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, E and B\r\n");
printf(" User! Observe data in hex LED. * displays E, and # displays F.\r\n");
printf(" User! Begin pressing after you continue.\r\n");
GetUserResponse(YES);
mask = LED_MASK; // Obtain the present state of the LED mask
mask = mask | 0xFF00; // Blank out the hex LEDs
LED_MASK = mask;
while ((KeysPressed != 0xFFF) & (KeyData != 0x23) & (KeyData != 0x54))
{
KeyData = KeypadGetData();
LED_MASK = mask; // Shut off all hex LEDs
if((KeyData != 0x54) & (KeyData != 0x0A))
{
LED_MASK = mask & ~0x100; // Turn on least significant hex digit
HEX_LEDS = KeyData; // Display data
}
if(KeyData == '1') KeysPressed = KeysPressed | 0x001 ; // ASCII 1
if(KeyData == '2') KeysPressed = KeysPressed | 0x002 ; // ASCII 2
if(KeyData == '3') KeysPressed = KeysPressed | 0x004 ; // ASCII 3
if(KeyData == '4') KeysPressed = KeysPressed | 0x008 ; // ASCII 4
if(KeyData == '5') KeysPressed = KeysPressed | 0x010 ; // ASCII 5
if(KeyData == '6') KeysPressed = KeysPressed | 0x020 ; // ASCII 6
if(KeyData == '7') KeysPressed = KeysPressed | 0x040 ; // ASCII 7
if(KeyData == '8') KeysPressed = KeysPressed | 0x080 ; // ASCII 8
if(KeyData == '9') KeysPressed = KeysPressed | 0x100 ; // ASCII 9
if(KeyData == '0') KeysPressed = KeysPressed | 0x200 ; // ASCII *
if(KeyData == 'E') KeysPressed = KeysPressed | 0x400 ; // ASCII 0
if(KeyData == 'B') KeysPressed = KeysPressed | 0x800 ; // ASCII #
}
if (KeysPressed != 0xFFF) KeypadTestResult = KP_TEST_FAILURE;
if (KeyData == 0x54)
{
retVal = ERRORCODEX(ERR_L_KEYPAD, 0x80, 1, ERR_T_TIMEOUT);
XllpUtilityOutputError(retVal);
return (1);
}
if (KeypadTestResult == KP_TEST_SUCCESS)
{
PostDisplayProgress(ERR_L_KEYPAD, ERR_S_KEYPAD_PASS, __LINE__);
printf(" Success!\r\n");
}
else
{
retVal = ERRORCODEX(ERR_L_KEYPAD, 0x80, 1, ERR_T_WRONG_VALUE);
XllpUtilityOutputError(retVal);
}
LED_MASK = mask & ~0xFF00; // Turn on hex LEDs
printf(" Testing Scroll Wheel Press\r\n");
printf(" User! Press and release the scroll wheel.\r\n");
printf(" User! Start press after you continue.\r\n");
GetUserResponse(YES);
time = 200; // This will provide about a 10 second timeout
while (((KPC_REG & KEYP_KPC_DI) != KEYP_KPC_DI) & (time != 0))
{
DM_WaitMs(50); // Dealy 50 mS as part of timeout loop
time = time -1; // Decrement timer
}
if (time == 0)
{
retVal = ERRORCODEX(ERR_L_KEYPAD, 0x80, 1, ERR_T_TIMEOUT);
XllpUtilityOutputError(retVal);
return (1);
}
// Code picks up here when direct key interrupt comes in
KeyData = KPDK_REG; // Fetch direct key data
if(KeyData & 0x4)
{
PostDisplayProgress(ERR_L_KEYPAD, ERR_S_KEYPAD_PASS, __LINE__);
printf(" Success!\r\n");
}
else
{
printf("Press not detected.\r\n");
retVal = ERRORCODEX(ERR_L_KEYPAD, 0x80, 1, ERR_T_TIMEOUT);
XllpUtilityOutputError(retVal);
return (1);
}
// Scroll wheel press test complete - test for scroll operation
printf(" Testing Scroll Wheel Rotation\r\n");
printf(" User! Rotate the scroll wheel.\r\n");
printf(" User! Start press after you continue.\r\n");
GetUserResponse(YES);
scroll_min = 0x7f;
scroll_max = 0x7f;
while((scroll_min > 0x5F) & (scroll_max < 0x9F)) // Stay in test loop until min/max values have been reached
{
KeyData = KPREC_REG;
KeyData = KeyData & 0xFF; // Clear out any extraneous bits
if(KeyData < scroll_min) scroll_min = KeyData;
if(KeyData > scroll_max) scroll_max = KeyData;
}
PostDisplayProgress(ERR_L_KEYPAD, ERR_S_KEYPAD_PASS, __LINE__);
printf(" Success!\r\n");
return (KeypadTestResult);
#else
*/
char KeyData = 0;
printf("Now test keypad, press any key to view result, 'E' to exit.\r\n");
//printf("The 'D' key is not add to the keypad ,don't press it!\r\n");
KeypadHWSetup();
do {
KeyData = (KeypadGetData());
if(KeyData!=0x0A)
printf("You enter %c\r\n", KeyData);
} while(KeyData!='E');
printf("Now Exit KeyPad Test\n");
return KP_TEST_SUCCESS;
//#endif //masked by zzm
}
// end of PostKeypadTest()
/*
*******************************************************************************
*
* FUNCTION:
* KeypadGetData
*
* DESCRIPTION:
* translate a keypress into a key
*
* INPUT PARAMETERS:
* None.
*
* RETURNS:
* None.
*
* GLOBAL EFFECTS:
* None.
*
* ASSUMPTIONS:
* None.
*
* CALLS:
* None.
*
* CALLED BY:
* Keypad test main function.
*
* PROTOTYPE:
* char KeypadGetData(void);
*
*******************************************************************************
*/
char KeypadGetData(void) //modified by hzh
{
char KeyData; // Character from Keypad
unsigned int KPData0; // Register data
unsigned int KPData1; // Register data
unsigned int KPPress; // Keypad press count
int timer; // Loop coutner/timer (for test timeout)
unsigned int kpc;
KeyData = 0x30; // Inital setting = ASCII ?
timer = 200;
// This will provide about a 10 second timeout
kpc = KPC_REG;
while (!(kpc & (KEYP_KPC_MI | KEYP_KPC_DI)) && (timer != 0))
{
DM_WaitMs(50); // Dealy 50 mS as part of timeout loop
timer = timer -1; // Decrement timer
kpc = KPC_REG;
if(GPLR0&0x0001) //查看按键'D',其为GPIO的第0脚
{
DM_WaitMs(10);
if(GPLR0&0x0001)
{
while(GPLR0&0x0001); //等待按健放开
return 'D';
}
}
}
if (timer == 0)
{
KeyData = 0x54; // ASCII "T" for Time out
//return (KeyData);
}
// Code picks up here if a key is pressed
DM_WaitMs(50); // Dealy 50 mS for data to settle
if(kpc & KEYP_KPC_DI)
{
UINT32 val = KPDK_REG;
if(val & (1UL<<31))
{
if(val & (1<<0))
return 'E';
if(val & (1<<1))
return 'F';
if(val & (1<<2))
return '0';
}
}
KPData0 = KPAS_REG;
KPData1 = (KPData0 & 0xF0) >> 4; // Extract Row data and right justify
KPPress = (KPData0 & 0x7C000000) >> 26; // Extract key press count and right justify
KPData0 = KPData0 & 0xF; // Isolate Column Data
//printf("KPData0 = %08x\r\n", KPData0);
//printf("Key Count = %08x\r\n", KPPress);
KeyData = 0x0A;
if (KPPress == 1) // Decode only if 1 keypress has been detected
{
switch (KPData0)
{
case 0:
if(KPData1 == 0) KeyData = 'A';
if(KPData1 == 1) KeyData = '3';
if(KPData1 == 2) KeyData = '2';
if(KPData1 == 3) KeyData = '1';
break;
case 1:
if(KPData1 == 0) KeyData = 'B';
if(KPData1 == 1) KeyData = '6';
if(KPData1 == 2) KeyData = '5';
if(KPData1 == 3) KeyData = '4';
break;
case 2:
if(KPData1 == 0) KeyData = 'C';
if(KPData1 == 1) KeyData = '9';
if(KPData1 == 2) KeyData = '8';
if(KPData1 == 3) KeyData = '7';
break;
}
}
return (KeyData);
}// End of KeypadGetPress()
char PostGetKeypad(void) //hzh
{
char KeyData; // Character from Keypad
unsigned int KPData0; // Register data
unsigned int KPData1; // Register data
unsigned int KPPress; // Keypad press count
unsigned int kpc;
//unsigned int kreg;
// kreg=GPLR0;
if(GPLR0&0x0001)
{
DM_WaitMs(10);
if(GPLR0&0x0001)
{
while(GPLR0&0x0001);
return 'D';
}
}
kpc = KPC_REG;
if (!(kpc & (KEYP_KPC_MI | KEYP_KPC_DI)))
return 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -