1、驱动源代码
#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include
#define ADC_WRITE(ch, prescale) ((ch) << 16 | (prescale))
#define ADC_WRITE_GETCH(data) (((data) >> 16) & 0x7)
#define ADC_WRITE_GETPRE(data) ((data) & 0xff)
#define DEVICE_NAME "adc"
static void __iomem *base_addr;
typedef struct { //adc转换的设备结构体
wait_queue_head_t wait; //等待队列
int channel; //ADC转换通道
int prescale;
}ADC_DEV;
DECLARE_MUTEX(ADC_LOCK); //定义互斥信号量
static int ADC_OK = 0;
static ADC_DEV adcdev;
static volatile int ev_adc = 0;
static int adc_data;
static struct clk *adc_clock;
#define ADCCON (*(volatile unsigned long *)(base_addr + S3C2410_ADCCON)) //ADC control
#define ADCTSC (*(volatile unsigned long *)(base_addr + S3C2410_ADCTSC)) //ADC touch screen control
#define ADCDLY (*(volatile unsigned long *)(base_addr + S3C2410_ADCDLY)) //ADC start or Interval Delay
#define ADCDAT0 (*(volatile unsigned long *)(base_addr + S3C2410_ADCDAT0)) //ADC conversion data 0
#define ADCDAT1 (*(volatile unsigned long *)(base_addr + S3C2410_ADCDAT1)) //ADC conversion data 1
#define ADCUPDN (*(volatile unsigned long *)(base_addr + 0x14)) //Stylus Up/Down interrupt status
#define PRESCALE_DIS (0 << 14)
#define PRESCALE_EN (1 << 14) //使能预分频
#define PRSCVL(x) ((x) << 6)//设定的预分频值
#define ADC_INPUT(x) ((x) << 3) //选择ADC转换通道
#define ADC_START (1 << 0) // 启动ADC转换
#define ADC_ENDCVT (1 << 15)
//AD转换中断处理函数
static irqreturn_t adcdone_int_handler(int irq, void *dev_id)
{
if (ADC_OK)
{
adc_data = ADCDAT0 & 0x3ff; //提取AD转换后的数据
ev_adc = 1; //转换结束唤醒等待的进程
wake_up_interruptible(&adcdev.wait);
}
return IRQ_HANDLED;
}
static ssize_t s3c2410_adc_read(struct file *filp, char *buffer, size_t count, loff_t *ppos)
{
char str[20];
int value;
size_t len;
if (down_trylock(&ADC_LOCK) == 0) //获得信号量 :获得不到信号量不会阻塞
{
ADC_OK = 1;//启动AD转换
ADCCON = PRESCALE_EN | PRSCVL(adcdev.prescale) | ADC_INPUT((adcdev.channel)) ;
//使能预分频 设置预分频值 选择ADC转换通道
ADCCON |= ADC_START; //启动AD转换
wait_event_interruptible(adcdev.wait, ev_adc); //阻塞等待AD转换
ev_adc = 0;//清除转换标志位
// printk("AIN[%d] = 0x%04x, %d\n", adcdev.channel, adc_data, ADCCON & 0x80 ? 1:0);
value = adc_data; //AD转换的数据
sprintf(str,"%5d", adc_data);//数据转换
//copy_to_user(buffer, (char *)&adc_data, sizeof(adc_data));
ADC_OK = 0;
up(&ADC_LOCK);//释放信号量
}
else
{
value = -1;
}
len = sprintf(str, "%d\n", value);
if (count >= len)
{
int r = copy_to_user(buffer, str, len); //把AD转换数据送回用户空间
return r ? r : len;
}
else
{
return -EINVAL;
}
}
static int s3c2410_adc_open(struct inode *inode, struct file *filp)
{
init_waitqueue_head(&(adcdev.wait)); //初始化等待队列
adcdev.channel=0; //选择ADC转换的通道 //AIN 0 or (0~7)
adcdev.prescale=0xff; //AD转换延时
printk( "ADC opened\n");
return 0;
}
static int s3c2410_adc_release(struct inode *inode, struct file *filp)
{
printk( "EmbedSky-ADC closed\n");
return 0;
}
static struct file_operations Adc_fops = {
owner: THIS_MODULE,
open: s3c2410_adc_open,
read: s3c2410_adc_read,
release: s3c2410_adc_release,
};
static struct miscdevice misc = {
.minor = MISC_DYNAMIC_MINOR,
.name = DEVICE_NAME,
.fops = &Adc_fops,
};
static char __initdata banner[] = "S3C2440 ADC\n";
static int __init Adc_init(void)
{
int ret;
printk(banner);
base_addr=ioremap(S3C2410_PA_ADC,0x20);//映射把物理地址映射为虚拟地址
if (base_addr == NULL) //返回值检测
{
printk(KERN_ERR "Failed to remap register block\n");
return -ENOMEM;
}
adc_clock = clk_get(NULL, "adc"); //获得ADC的时钟
if (!adc_clock)
{
printk(KERN_ERR "failed to get adc clock source\n");
return -ENOENT;
}
clk_enable(adc_clock);//使能ADC的时钟
/* normal ADC */
ADCTSC = 0;//不使用触摸屏
ret = request_irq(IRQ_ADC, adcdone_int_handler, IRQF_SHARED, DEVICE_NAME, &adcdev);//注册中断
if (ret)
{
iounmap(base_addr);
return ret;
}
ret = misc_register(&misc);//注册一个混杂设备
printk(DEVICE_NAME " initialized\n");
return ret;
}
static void __exit Adc_exit(void)
{
free_irq(IRQ_ADC, &adcdev);//注销中断
iounmap(base_addr); //解除映射
if (adc_clock)
{
clk_disable(adc_clock);//关闭adc转换的时钟
clk_put(adc_clock);
adc_clock = NULL;
}
misc_deregister(&misc);//注销混杂设备
}
EXPORT_SYMBOL(ADC_LOCK);
module_init(Adc_init);
module_exit(Adc_exit);
MODULE_LICENSE("GPL");
2、应用程序
#include #include #include #include #include #include #include #include #include #include #include int main(void)
{
int adc_fd;
int count;
char adc[6];
adc_fd = open("/dev/adc", O_RDWR); //以阻塞的方式打开设备文件
if (adc_fd < 0) {
perror("open device adc_fd");
exit(1);
}
for (;;) {
count = read(adc_fd,&adc,sizeof(adc));
adc[count]='\0';
printf(" adc = %s\n", adc);
sleep(2);//2秒钟采样一次
}
close(adc_fd);
return 0;
}