⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gpio.c

📁 此压缩包为杰得开发得z228的BSP的源代码,可以实现很多功能,尤其是视频解码有很好的效果.
💻 C
字号:
#include <windows.h>
#include "gpio.h"
#include "Socksv2.h"
#include "CEDDK.h"

const unsigned long gpio_addr[] = 
{
	0x20024000,
	0x20025000,
	0x20026000,
	0x20027000,
           
	0x2002b000,
	0x2002c000,
	0x2002d000,
	0x2002e000,
	0x2002f000,
	
};

typedef struct GPIO_MAP_T
{
	unsigned long *addr;
	unsigned char isMapped;
}GPIO_MAP_T;



static GPIO_MAP_T map_addr[] =
{
	0, 0,
	0, 0,
	0, 0,
	0, 0,

	0, 0,
	0, 0,
	0, 0,
	0, 0,
	0, 0
};

static unsigned long *sys_control_reg_base = NULL;

void gpio_init(int id, int pin, int direciton)
{
	unsigned long baseaddr;
	PHYSICAL_ADDRESS PhysicalIoBase;

	if(id > 8 || pin > 7) return ;


	if(map_addr[id].isMapped == 0)
	{

		PhysicalIoBase.HighPart = 0;
		PhysicalIoBase.LowPart = gpio_addr[id];
		map_addr[id].addr = (unsigned long *)MmMapIoSpace( PhysicalIoBase, 0x400 ,0 );		
		map_addr[id].isMapped = 1;
				
	}
	
	if(!sys_control_reg_base)
	{
		// for system control register
		PhysicalIoBase.HighPart = 0;
		PhysicalIoBase.LowPart = 0x20020000;
		sys_control_reg_base = (unsigned long *)MmMapIoSpace( PhysicalIoBase, 0x200 ,0 );		
	}

		
	baseaddr = (unsigned int)sys_control_reg_base;
	if(id == 3)// set spi line for gpio 3
	{
		*(volatile unsigned int*)(baseaddr + 0x10C) |= (1 << 8);// set as gpio
	}
	else if(id == 7)
	{
		*(volatile unsigned int*)(baseaddr + 0x10C) |= (1 << 1);// set as gpio		
	}
	else if(id == 0)
	{
		*(volatile unsigned int*)(baseaddr + 0x10C) |= (1 << 13);// set as gpio	
	}
		
	baseaddr = (unsigned long)map_addr[id].addr;
	
	if(direciton)
		*(volatile unsigned int*)(baseaddr + 0x400) =  ( (*(volatile unsigned int*)(baseaddr + 0x400)) | (1 << pin) );
	else
		*(volatile unsigned int*)(baseaddr + 0x400) =  ( (*(volatile unsigned int*)(baseaddr + 0x400)) & (~(1 << pin)) );
	
	return;	
	
}

void gpio_write(int id, int pin, int value)
{
	unsigned long baseaddr;
	if(id > 8 || pin > 7) return ;
	
	if(map_addr[id].isMapped == 0) return ; // 没有映射的。
		
	baseaddr = (unsigned long)map_addr[id].addr;

	if(value)
		*(volatile unsigned int*)(baseaddr +  ((1 << pin) << 2) ) =  (1 << pin);
	else
		*(volatile unsigned int*)(baseaddr + ((1 << pin) << 2) ) =  0;

	return;
}


int gpio_read(int id, int pin)
{
	unsigned long baseaddr;
	if(id > 8 || pin > 7) return 0xff;
		
	if(map_addr[id].isMapped == 0) return 0xff; // 没有映射的。
	
	baseaddr = (unsigned long)map_addr[id].addr;

	return	(*(volatile unsigned int*)(baseaddr +  ((1 << pin) << 2) ));
	
}

⌨️ 快捷键说明

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