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

📄 dev_gpio.c

📁 利用开发板上的/dev/mem设备,进行内存映射,驱动cpu的GPIO口,点亮和熄灭led灯.附makefile文件
💻 C
字号:
/* src/dev_gpio.c
*
* This file provide IO reading and writing from user space.
* Pls create some device file in diretory /dev/gpiotest whose major is 220.
* This driver support 0-255 devices. In user space user can read and write
* IO through open and ioctl function provided by glibc.
*
*/
#include "dev_gpio.h"

static ioport_device_t gpio_devices[256];

int __init gpio_init(void)
{
	int i;
	register_chrdev(IOPORT_MAJOR, "gpiotest", &gpio_ctl_fops);
	return 0;
}
__initcall(gpio_init);
/*
* Open/close code for raw IO.
*/
int gpio_open(struct inode *inode, struct file *filp)
{
	int minor;
	minor = MINOR(inode->i_rdev);

	/* if (ioport_devices[minor].io_lock) {
	printk("Device is busy\n");
	return -1;
	}*/
	*(volatile unsigned short*)(0xfff00000+0x962)&=~0x0080;
	*(volatile unsigned short*)(0xfff00000+0x960)|=0x0080;
	gpio_devices[minor]++;
	return 0;
}
//__ioremap
int gpio_release(struct inode *inode, struct file *filp)
{
	int minor;
	minor = MINOR(inode->i_rdev);
	if (gpio_devices[minor])
	gpio_devices[minor]--;
	*(volatile unsigned short*)(0xfff00000+0x960)&=~0x0080;
	*(volatile unsigned short*)(0xfff00000+0x962)|=0x0080;
	return 0;
}
/*
* Deal with ioctls against the raw-device control interface, to bind
* and unbind other raw devices.
*/
int gpio_ctl_ioctl(struct inode *inode,struct file *flip,unsigned int command,unsigned long arg)
{
	int err = 0;
	int minor = MINOR(inode->i_rdev);
	switch (command)
	{
	case IOWRITE:
		*(volatile unsigned short*)(0xfff00000+0x966)&=~0x0080;
		return 0;
	case IOCLEAR:
		*(volatile unsigned short*)(0xfff00000+0x966)|=0x0080;
		return 0;
	default:
		err = -EINVAL;
	}
	return err;
}

⌨️ 快捷键说明

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