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

📄 adc.c

📁 sbc2410上运行的 采集usb设想头视频并通过局域网进行视频传输的程序 还具有一些远程io控制的功能
💻 C
字号:
#include <linux/config.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>

#include <linux/miscdevice.h>
#include <linux/sched.h>
#include <linux/delay.h>
#include <linux/poll.h>
#include <linux/spinlock.h>
#include <linux/irq.h>
#include <linux/delay.h>

#include <asm/hardware.h>

#define DEVICE_NAME	"adc"
#define ADC_MAJOR 239
#define LOOP 100000




static int ready = 0;
//static int adc_value = 0;

//static DECLARE_WAIT_QUEUE_HEAD(buttons_wait);





static int adc_read(struct file * file, char * buffer, size_t count, loff_t *ppos)
{
  int adcdata;
	int i;
	int preScaler;
	static int prevCh=-1;
	int ch=0;
	preScaler=0xFF;
	ADCCON=(1<<14)|(preScaler<<6)|(ch<<3);
	if(prevCh!=ch)
	{
		ADCCON=(1<<14)|(preScaler<<6)|(ch<<3);
		for(i=0;i<LOOP;i++);
		prevCh=ch;
	}
	ADCCON|=0x1;
	while(ADCCON&0x1);
	while(!(ADCCON&0x8000));
	adcdata=(int)ADCDAT0&0x3ff;
	copy_to_user(buffer, (char *)&adcdata, sizeof(adcdata));
	return sizeof adcdata;
	
}
static int adc_open(struct inode *inode, struct file *filp)
{
	MOD_INC_USE_COUNT;
	printk( "adc opened\n");
	return 0;
}


static int adc_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
	switch(cmd) {
	default:
		return -EINVAL;
	}
}
static int adc_release(struct inode *inode, struct file *filp)
{
	MOD_DEC_USE_COUNT;
	printk( "adc closed\n");
	return 0;
}
static struct file_operations adc_fops = {
	owner:	THIS_MODULE,
  open: adc_open,
	ioctl:  adc_ioctl,
	//poll: matrix4_buttons_select,
 	read: adc_read,
  release: adc_release,
};


static devfs_handle_t devfs_handle;
static int __init adc_init(void)
{
	int ret;

	ready = 0;
	ret = register_chrdev(ADC_MAJOR, DEVICE_NAME, &adc_fops);
	if (ret < 0) {
	  printk(DEVICE_NAME " can't register major number\n");
	  return ret;
	}
	
	
	devfs_handle = devfs_register(NULL, DEVICE_NAME, DEVFS_FL_DEFAULT,
				ADC_MAJOR, 0, S_IFCHR | S_IRUSR | S_IWUSR, &adc_fops, NULL);//向VFS注册统一的设备操作函数
///dev处的设备入口在设备初始化时建立,设备移除时删除.
//设备驱动和用户空间程序都可以改变所有性和许可权.
//不必分配主设备号,不必处理次设备号.
	return 0;
}

static void __exit adc_exit(void)
{
	devfs_unregister(devfs_handle);
	
	unregister_chrdev(ADC_MAJOR, DEVICE_NAME);
}

module_init(adc_init);
module_exit(adc_exit);
MODULE_LICENSE("GPL");

⌨️ 快捷键说明

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