key.c

来自「君正早期ucos系统(只有早期的才不没有打包成库),MPLAYER,文件系统,图」· C语言 代码 · 共 336 行

C
336
字号
#include <bsp.h>#include <jz4740.h>#include <ucos_ii.h>#include <key.h>#include <threadprio.h>#ifdef USE_MIDWARE#include "midware.h"#endif#define KEY_TASK_STK_SIZE	1024#define KEY_TASK_PRIO	KEY_THREAD_PRIO //1static OS_EVENT *key_sem;static OS_STK KeyTaskStack[KEY_TASK_STK_SIZE];extern void KeydownInfo();extern u8 LcdState;PFN_KEYHANDLE UpKey = NULL;PFN_KEYHANDLE DownKey = NULL;//PFN_KEYHANDLE UpKeyHandle = NULL;//PFN_KEYHANDLE DownKeyHandle = NULL;#define POWER_OFF_PIN    (125)	//GPD29 29 + 3 * 32#define KEY_INTERRUP_PIN (126)//(32 * 1 + 17) 32 * 3 + 26#define KEY_OK_PIN		(82)//32 * 2 + 18#define KEY_PLAY_PIN	(85)//32 * 2 + 21#define USB_PIN		(86)//32 * 2 + 22#define POFF_TASK_STK_SIZE       128#define POFF_EACH_TIME    100#define POFF_TRY_TIME     20#define KEY_DETECT_REPEAT_TIME 100#define KEY_REPEAT_TIME 50#define KEY_DELAY_TIME 1//#undef printf//#define printf(x,args...)#ifdef USE_MIDWAREvolatile static MIDSRC keysrc;volatile static MIDSRCDTA res;volatile static u32 keyid;volatile static MIDSRC poffsrc;volatile static MIDSRCDTA res;//volatile static u8 cardstate ;static u32 poffid;static int DownKeyHandle();static void UpKeyHandle();bool usbin=0;static void info_power_off(){	keysrc.Src = SRC_POWER_OFF;	keysrc.Event = EVENT_POWER_OFF;	OSQPost(keysrc.CurEvent1 , (void *)&keyid);	OSSemPost(keysrc.CurEvent);}static void GetRequest(MIDSRCDTA *dat){//	printf("Up layer get :%d \n",res.Val);}static void Response(MIDSRCDTA *dat){	if ( dat->Val == 1 )	{		printf("Check can power off or not ! \n");		ShutLcd(0);		cli();		while(1)		{			while( !(__gpio_get_pin(POWER_OFF_PIN)) );                //wait until key up			mdelay(200);			if ( __gpio_get_pin(POWER_OFF_PIN) == 1 )  //really power off			{								jz_pm_hibernate();			}		}	}	else		printf("Can not power off ! \n");}void check_poweroff(){	int i;	for ( i = 0; i < POFF_TRY_TIME ; i ++ )	{		OSTimeDlyHMSM(0,0,0,POFF_EACH_TIME);		if ( __gpio_get_pin(POWER_OFF_PIN) ==1 )			break;		printf("Power key keep low! \n");	}	if ( i >= POFF_TRY_TIME )  //really power off!	{		info_power_off();	}	else	{		DownKeyHandle( 0x40 );		UpKeyHandle( 0x40 );	}}#endifstatic void UpKeyHandle(int key) {#ifdef USE_MIDWARE	OSQPost(keysrc.CurEvent1 , (void *)&keyid);	keysrc.Src = SRC_KEY;	OSSemPost(keysrc.CurEvent);	printf("Key Up %d ! \n",LcdState);#endif		if(UpKey) UpKey(key);}static int DownKeyHandle(int key) {	u8 err;#ifdef USE_MIDWARE	OSQPost(keysrc.CurEvent1 , (void *)&keyid);	keysrc.Src = SRC_KEY;	OSSemPost(keysrc.CurEvent);	printf("Key Down %d ! \n",LcdState);#endif	if(DownKey) DownKey(key);}static void key_interrupt_handler(u32 arg){	__gpio_mask_irq(POWER_OFF_PIN);		OSTaskChangePrio( KEY_TASK_PRIO + 150, KEY_TASK_PRIO );	OSSemPost(key_sem);}static void KeyTaskEntry(void *arg){	u8 err;	u16 i,run,count = 0,count1=0;	u32 key,oldkey,key1,oldkey1;	u32 upkey = 0;	u32 keyrepeat = KEY_DETECT_REPEAT_TIME / KEY_DELAY_TIME;	printf("Key Install \r\n");	while(1)	{		//while(1);		//OSTaskChangePrio( KEY_TASK_PRIO, KEY_TASK_PRIO + 150 );		//OSSemPend(key_sem, 0/*20*/, &err);		OSSemPend(key_sem, 20, &err);			//printf("key_sem 20 \r\n");#ifdef USE_MIDWARE		if ( __gpio_get_pin(POWER_OFF_PIN) == 0 )		{			check_poweroff();			goto out;		}#endif	if(__gpio_get_pin(USB_PIN)&&usbin==0){usbin=1;}	if(usbin==1&&!__gpio_get_pin(USB_PIN)){usbin=0;		printf("usbin \r\n");					UpKeyHandle(0x40);					DownKeyHandle(0x40);					continue;						}				oldkey = Read_Adkey();#if SLCDTYPE == 102		if(!__gpio_get_pin(KEY_OK_PIN))			oldkey |= 0x40; 		if(!__gpio_get_pin(KEY_PLAY_PIN))				oldkey |= 0x01; #elif SLCDTYPE ==101		if(!__gpio_get_pin(KEY_OK_PIN))			oldkey |= 0x01; 		if(!__gpio_get_pin(KEY_PLAY_PIN))			oldkey |= 0x40; 			#endif					run = 1;		count = 0;		keyrepeat = KEY_DETECT_REPEAT_TIME / KEY_DELAY_TIME;		while(run)		{						OSTimeDly(KEY_DELAY_TIME);			key=Read_Adkey();#if SLCDTYPE == 102 						if(!__gpio_get_pin(KEY_OK_PIN))				key |= 0x40; 			if(!__gpio_get_pin(KEY_PLAY_PIN))					key |= 0x01; #elif SLCDTYPE == 101			if(!__gpio_get_pin(KEY_OK_PIN))				key |= 0x01; 			if(!__gpio_get_pin(KEY_PLAY_PIN))				key |= 0x40; 				#endif						//printf("%s.%s key =0x%x", __FILE__, __func__, key);					             if(key==0 && oldkey==0)					run=0;			if(key ^ oldkey)			{								oldkey = key;				continue;			}						else			{				if(key)				{										if(key & (~upkey) )					{												DownKeyHandle(key & (~upkey));						printf("read adkey = %x\n",key);					}					  					else					{						if((key ^ upkey) & upkey)							UpKeyHandle((key ^ upkey) & upkey);						      printf("UpKeyHandle()\n");					}					if(key == upkey)					{						count++;											if(count > keyrepeat)						{							count = 0;						    upkey = 0;							keyrepeat = KEY_REPEAT_TIME / KEY_DELAY_TIME;						}					}else					{						count = 0;						//UpKeyHandle(key);					    upkey = key;					}									}else				{					if(upkey && key==0)						UpKeyHandle(upkey);					run = 0;					upkey =0;									}											}					}	out:				__gpio_ack_irq(POWER_OFF_PIN);		ack_irq(48 + POWER_OFF_PIN);		__gpio_unmask_irq(POWER_OFF_PIN);		ack_irq(48  + KEY_INTERRUP_PIN);		__gpio_unmask_irq(KEY_INTERRUP_PIN);	}	}static void key_interrupt_handler2(u32 arg){	__gpio_mask_irq(KEY_INTERRUP_PIN);	__gpio_ack_irq(KEY_INTERRUP_PIN);	//OSTaskChangePrio( KEY_TASK_PRIO + 150, KEY_TASK_PRIO );	OSSemPost(key_sem);	printf("get keyboard interrupter\n");	//__gpio_unmask_irq(KEY_INTERRUP_PIN);}void KeyInit() {	int i;	key_sem = OSSemCreate(0);#ifdef USE_MIDWARE	keysrc.GetRequest = GetRequest;	keysrc.Response = Response;	keysrc.Name = "KEY";	RegisterMidSrc((PMIDSRC)&keysrc);	keyid = keysrc.ID;	__gpio_mask_irq(POWER_OFF_PIN);	__gpio_as_func0(POWER_OFF_PIN);	__gpio_as_input(POWER_OFF_PIN);	__gpio_disable_pull(POWER_OFF_PIN);#endif	request_irq(48 + POWER_OFF_PIN, key_interrupt_handler, 0);   	OSTaskCreate(KeyTaskEntry, (void *)0,		     (void *)&KeyTaskStack[KEY_TASK_STK_SIZE - 1],		     KEY_TASK_PRIO);	__gpio_as_irq_fall_edge(POWER_OFF_PIN);		__gpio_unmask_irq(POWER_OFF_PIN);#if SLCDTYPE == 102		__gpio_as_input(KEY_OK_PIN);	//__gpio_enable_pull(KEY_OK_PIN);	__gpio_as_input(KEY_PLAY_PIN);	//__gpio_enable_pull(KEY_PLAY_PIN);#elif SLCDTYPE == 101	__gpio_as_input(KEY_OK_PIN);	__gpio_as_input(KEY_PLAY_PIN);	#endif	//__gpio_as_output(KEY_INTERRUP_PIN);        //__gpio_set_pin(KEY_INTERRUP_PIN);#if 0	//request_irq(/*48 + KEY_INTERRUP_PIN*/143, key_interrupt_handler2, 0);	request_irq(48 + KEY_INTERRUP_PIN, key_interrupt_handler2, 0);	__gpio_disable_pull(KEY_INTERRUP_PIN);  __gpio_as_irq_high_level(KEY_INTERRUP_PIN);	//__gpio_as_irq_rise_edge(KEY_INTERRUP_PIN);	__gpio_unmask_irq(KEY_INTERRUP_PIN);#endif}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?