2440 WinCE操作GPIO

Windows CE系统将GPIO的实地址(例如2440的GPIO的基地址为0x56000000)映射到虚拟地址空间(GPIO对应为0xB1600000),这 样,通过对这段虚拟地址空间的操作,就能够完成对GPIO或者其他片内资源的控制、输入输出工作。要操作一个平台的GPIO,在其对应BSP中按照基地址,找到虚拟地址,并且找到方便操作这个地址的数据结构就可以了,关键函数就是 VirtualAlloc和VirtualCopy。下面以操作Samsung S3C2440的GPIO为例,讲述这个步骤:
1.首先在BSP中的s2440.h文件,找到虚拟地址映射以及操作GPIO的寄存器结构体(这个在自己制作一些特殊设备的BSP时,会依据需要而发生更改)


#define IOP_BASE      0xB1600000 // 0x56000000
typedef struct  {
	unsigned int  rGPACON;			// 00
	unsigned int  rGPADAT;
	unsigned int  rPAD1[2];

	unsigned int  rGPBCON;			// 10
	unsigned int  rGPBDAT;
	unsigned int  rGPBUP;
	unsigned int  rPAD2;

	unsigned int  rGPCCON;			// 20
	unsigned int  rGPCDAT;
	unsigned int  rGPCUP;
	unsigned int  rPAD3;

	unsigned int  rGPDCON;			// 30
	unsigned int  rGPDDAT;
	unsigned int  rGPDUP; 
	unsigned int  rPAD4;

	unsigned int  rGPECON;			// 40
	unsigned int  rGPEDAT;
	unsigned int  rGPEUP;
	unsigned int  rPAD5;

	unsigned int  rGPFCON;			// 50
	unsigned int  rGPFDAT;
	unsigned int  rGPFUP; 
	unsigned int  rPAD6;

	unsigned int  rGPGCON;			// 60
	unsigned int  rGPGDAT;
	unsigned int  rGPGUP; 
	unsigned int  rPAD7;

	unsigned int  rGPHCON;			// 70
	unsigned int  rGPHDAT;
	unsigned int  rGPHUP; 
	unsigned int  rPAD8;

	unsigned int  rMISCCR;			// 80
	unsigned int  rDCKCON;		
	unsigned int  rEXTINT0;
	unsigned int  rEXTINT1;		
	unsigned int  rEXTINT2;			// 90
	unsigned int  rEINTFLT0;
	unsigned int  rEINTFLT1;
	unsigned int  rEINTFLT2;
	unsigned int  rEINTFLT3;		// A0
	unsigned int  rEINTMASK;
	unsigned int  rEINTPEND;
	unsigned int  rGSTATUS0;		// AC
	unsigned int  rGSTATUS1;		// B0
	unsigned int  rGSTATUS2;		// B4 ;;; SHL
	unsigned int  rGSTATUS3;		// B8
	unsigned int  rGSTATUS4;		// BC

	unsigned int  rFLTOUT;			// C0
	unsigned int  rDSC0;
	unsigned int  rDSC1;
	unsigned int  rMSLCON;

	unsigned int  rGPJCON;			// D0
	unsigned int  rGPJDAT;
	unsigned int  rGPJUP;
	unsigned int  rPAD9;

}IOPreg;  

二、因为我用的mini2440开发板的GPB0接在蜂鸣器上,所以我以操作GPB0为例编写代码。这样给GPB0一个高电平,那么蜂鸣器就会响,反之蜂鸣器就会停止发声

#ifdef __cplusplus
extern "C"
{
#endif
	BOOL VirtualCopy( PVOID, PVOID, DWORD, DWORD );
#ifdef __cplusplus
}
#endif


void GPIO()        //通过GPIO控制蜂鸣器
{
	volatile IOPreg *v_pIORegs;
	v_pIORegs = (volatile IOPreg *)VirtualAlloc(0, sizeof(IOPreg), MEM_RESERVE, PAGE_NOACCESS);
	if(v_pIORegs)
	{
		if(VirtualCopy((PVOID)v_pIORegs, (PVOID)IOP_BASE, sizeof(IOPreg), PAGE_READWRITE | PAGE_NOCACHE))
		{
			v_pIORegs->rGPBCON = 1;                    //将GPB0设置为OUTPUT
			v_pIORegs->rGPBDAT = 0x3ff;               //设置为高电平,蜂鸣器发声
			Sleep(3000);
			v_pIORegs->rGPBDAT = 0;                     //恢复为低电平,蜂鸣器停止发声
		}

		VirtualFree((LPVOID)v_pIORegs, 0, MEM_RELEASE);
		v_pIORegs = NULL;
	}
}

@written by .cpp

百度空间:http://hi.baidu.com/anglecloudy

博客园:http://www.cnblogs.com/dotcpp

posted @ 2010-07-29 17:06  李玉宝  阅读(1114)  评论(0编辑  收藏  举报