其他分享
首页 > 其他分享> > Hi3559AV100获取和设置GPIO管脚的状态Demo

Hi3559AV100获取和设置GPIO管脚的状态Demo

作者:互联网

         由于针对海思GPIO管脚调试一下,一般都可以用himm来设置寄存器值,当时还要查找对应管脚寄存器,设置方向和数值,有点麻烦。可是针对hi3559av100中《HiMPP V4.0 媒体处理软件开发参考.pdf》删除了HI_MPI_SYS_SetReg和HI_MPI_SYS_GetReg,所以操作寄存器比较麻烦。通过研究himm源码,自己仿写一个demo,可以设置和获取gpio的状态。

          

1、定义hi3559av100各个GPIO的基地址
static int gsHisiGpioBaseAddrTab[19]={
					0x12140000,0x12141000,0x12142000,0x12143000,0x12144000,/**GPIO0-4*/
					0x12145000,0x12146000,0x12147000,0x12148000,0x12149000,/**GPIO5-9*/
					0x1214A000,0x1214B000,0x1214C000,0x1214D000,0x1214E000,/**GPIO10-14*/
					0x1214F000, 0x12150000, 0x12151000, 0x12152000/**GPIO15-18*/
					};
2、提供两个获取和设置寄存器函数
HI_S32 SetReg(HI_U32 u32Addr, HI_U32 u32Value)
{
	unsigned long ulOld;
	
	void* pMem  = NULL;

	pMem = memmap(u32Addr, DEFAULT_MD_LEN);
	if (pMem == NULL) {
		printf("memmap failed!\n");
		return -1;
	}
	
	ulOld = *(unsigned int*)pMem;
	*(unsigned int*)pMem = u32Value;
    printf("[SetReg]0x%08lX --> 0x%08lX \n",  ulOld, u32Value);
	memunmap(pMem);
	return 0;
			
	
}
HI_S32 GetReg(HI_U32 u32Addr, HI_U32 *pu32Value)
{
	void* pMem  = NULL;

	pMem = memmap(u32Addr, DEFAULT_MD_LEN);
	if (pMem == NULL) {
		printf("memmap failed!\n");
		return -1;
	}
	
	*pu32Value = *(unsigned int*)pMem;
    printf("[GetReg]0x%08lX =[0x%08lX] \n",  u32Addr, *pu32Value);
	memunmap(pMem);
	return 0;
}
3、封装设置GPIO方向、设置和获取函数
void HisiGpioDirCfg(int group,int pin,bool isOutput)
{
    unsigned int val = 0;
    unsigned int dirAddr = 0;
    dirAddr = GPIO_GROUP_DIR_ADDR(gsHisiGpioBaseAddrTab[group]);

    if( 0 == GetReg(dirAddr,&val) )
    {
		if(isOutput)
		{
			val |= 1 << pin;
		}
		else
		{
			val &= ~(1 << pin);
		}
		if( 0 != SetReg(dirAddr,val) )
		{
			printf("[HisiGpioDirCfg] SetReg Error! val: %d\n",val);
		}
		printf("[HisiGpioDirCfg]addr: 0x%08lX =[0x%08lX] \n", dirAddr, val);
    }
	else
	{
		printf("[HisiGpioDirCfg] GetReg Error! val: %d\n",val);
	}
}

void HisiGpioSetValue(int group,int pin,bool isHeight)
{
    unsigned int val = 0;
    unsigned int dataAddr = 0;
    dataAddr = GPIO_DATA_ADDR(gsHisiGpioBaseAddrTab[group], pin);
    if(isHeight)
    {
    	val = 1 << pin;
    }
    else
    {
    	val = 0;
    }
    if( 0 != SetReg(dataAddr,val) )
    {
        printf("HisiGpioSetValue Error! val: %d\n",val);
    }
}

int HisiGpioGetValue(int group,int pin)
{
    unsigned int val = 0;
    unsigned int dataAddr = 0;
    dataAddr = GPIO_DATA_ADDR(gsHisiGpioBaseAddrTab[group], pin);
    if( 0 == GetReg(dataAddr,&val) )
    {
        if(val & (1 << pin))
        {
            val = 1;
        }
        else
        {
            val = 0;
        }
        return val;
    }
    else
    {
        printf("HisiGpioGetValue Error! val: %d\n",val);
        return -1;
    }
}
4.main函数实现
int main(int argc, char **argv)
{
	if(argc <4)
	{
		printf("usage: gpio_rw <flag 0:read,1:write> <groupnum> <gpio_bit> <dir 0:input,1:output> <value>\n");
		printf("sample: gpio_rw 0 0 3 [获取 GPIO0_3]\n");
		printf("sample: gpio_rw 1 0 3 0 1 [设置 GPIO0_3)]\n");
		return 0;
	}
	int flag = 0;
	unsigned int gpio_groupnum = 0;
	unsigned int gpio_bitnum = 0;
	unsigned int gpio_dir = 0;
	unsigned int bitvalue = 0;
	unsigned int status  = 0;
	flag = atoi(argv[1]);
	gpio_groupnum = atoi(argv[2]);
	gpio_bitnum = atoi(argv[3]);
	
	if (0 == flag)
	{
		if(argc == 4)
		{
			status = HisiGpioGetValue(gpio_groupnum, gpio_bitnum);
			
			printf("get gpio(%d,%d) value=%d\n",gpio_groupnum,gpio_bitnum,status);
		}
	}
	else if (1 == flag)
	{
		if(argc == 6)
		{
			
			if(gpio_dir >= 0)
			{
				gpio_dir = atoi(argv[4]);
				HisiGpioDirCfg(gpio_groupnum, gpio_bitnum, gpio_dir);
				printf("set gpio(%d,%d) dir=%d\n",gpio_groupnum,gpio_bitnum,gpio_dir);
			}
			if(bitvalue >= 0)
			{
				bitvalue = atoi(argv[5]);
				HisiGpioSetValue(gpio_groupnum, gpio_bitnum, bitvalue);
				printf("set gpio(%d,%d) value=%d\n",gpio_groupnum,gpio_bitnum,bitvalue);
			}
		}
	}
	
	return (int)status;
	
}

 

5、主要借鉴了osdrv\tools\board\reg-tools-1.0.0中的memmap.c和strfunc.c函数,在sdk中自行参考。

 

标签:bitnum,int,Demo,Hi3559AV100,unsigned,printf,gpio,管脚,pMem
来源: https://blog.csdn.net/xclshwd/article/details/87944526