1 driver
ADC设备在Linux中可以看做是简单的字符设备,也可以当做是一混杂设备(misc设备),这里我们就看做是misc设备来实现ADC的驱动。注意:这里我们获取AD转换后的数据将采用中断的方式,即当AD转换完成后产生AD中断,在中断服务程序中来读取ADCDAT0的第0-9位的值(即AD转换后的值)。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "s3c24xx-adc.h"
#undef DEBUG
//#define DEBUG
#ifdef DEBUG
#define DPRINTK(x...) {printk(__FUNCTION__"(%d): ",__LINE__);printk(##x);}
#else
#define DPRINTK(x...) (void)(0)
#endif
#define DEVICE_NAME "Myadc"
//经过虚拟地址映射之后的内存地址
static void __iomem *base_addr;
// ADC device
typedef struct
{
wait_queue_head_t wait;//阻塞 等待队列,进程读取设备,如果没有转换数据,就会睡眠在此队列上
int channel;//AD转换通道 s3c2440有八通道的ad,但是只有四个通道AIN[3:0]可以使用,其他四个用于触摸屏
int prescale;//预分频值 计算转换速率的时候使用
}ADC_DEV;
//申请并初始化信号量
DECLARE_MUTEX(ADC_LOCK);
//ADC驱动是否拥有AD转换器资源的状态变量
static int OwnADC = 0;
static ADC_DEV adcdev; //adcdev
static volatile int ev_adc = 0;//标识AD转换后的数据是否可以读取,0表示不可读取
static int adc_data;//保存读取后的AD转换的值,该值在ADC中断中读取
//保存从时钟平台队列中获取ADC的时钟
static struct clk *adc_clock;
//定义ADC的寄存器 虚拟地址
#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
//以下都定义于ADCCON中
#define PRESCALE_DIS (0 << 14)//预分频禁止
#define PRESCALE_EN (1 << 14)//预分频允许
#define PRSCVL(x) ((x) << 6)//预分频值设置
#define ADC_INPUT(x) ((x) << 3)//模拟输入通道选择 SEL_MUX
#define ADC_START (1 << 0)//开始AD转换
#define ADC_ENDCVT (1 << 15)//AD转换结束 ECFLG
//设置ADC控制器ADCCON,开启ADC转换
#define START_ADC_AIN(ch, prescale) \
do{ \
ADCCON = PRESCALE_EN | PRSCVL(prescale) | ADC_INPUT((ch)) ; \
ADCCON |= ADC_START; \
}while(0)
//中断服务程序,从ADC数据寄存器中读取AD转换后的值
static irqreturn_t adcdone_int_handler(int irq, void *dev_id)
{
if (OwnADC)
{ //如果ADC驱动拥有AD转换器资源,则从ADC寄存器读取转换状态
adc_data = ADCDAT0 & 0x3ff;//AD转换后的值保存在[0~9]位?
ev_adc = 1;//将可读标识为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)//尝试获取信号量,判断AD转换器资源是否可用
{
OwnADC = 1;//标记AD转换器资源可用
START_ADC_AIN(adcdev.channel, adcdev.prescale);//设置ADC控制器,开启AD转换
wait_event_interruptible(adcdev.wait, ev_adc);//使等待队列进入唤醒,等待转换结束
ev_adc = 0;//已经有AD转换后的数据,则标识清0,给下一次读作判断
DPRINTK("AIN[%d] = 0x%04x, %d\n", adcdev.channel, adc_data, ADCCON & 0x80 ? 1:0);
value = adc_data;//把转换结果赋值,以便传递给应用层
OwnADC = 0;//释放转换器资源
up(&ADC_LOCK);//解锁
}
else
{
//没有AD转换器资源
value = -1;
}
len = sprintf(str, "%d\n", value);
if(count >= len)
{
int r = copy_to_user(buffer, str, len);//将读取到的ADC转换后的值发往到应用程序
return r ? r : len;
}
else
{
return -EINVAL;
}
}
//打开ADC,并设置频道和预分频值
static int s3c2410_adc_open(struct inode *inode, struct file *filp)
{
init_waitqueue_head(&(adcdev.wait));//初始化中断队列
adcdev.channel=0;//设置ADC频道
adcdev.prescale=0xff;//设置预分频值
DPRINTK( "adc opened\n");
return 0;
}
static int s3c2410_adc_release(struct inode *inode, struct file *filp)
{
DPRINTK( "adc closed\n");
return 0;
}
//非标准C的用法,GNU C的用法
static struct file_operations dev_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 = &dev_fops,
};
//初始化设备
static int __init dev_init(void)
{
int ret;
//将ADC的IO端口占用的这段I/O空间映射到内存的虚拟地址,
//S3C2410_PA_ADC是ADC控制器的基地址,0x20是虚拟地址长度
base_addr=ioremap(S3C2410_PA_ADC,0x20);
if (base_addr == NULL) {
printk(KERN_ERR "Failed to remap register block\n");
return -ENOMEM;
}
//获取ADC时钟
adc_clock = clk_get(NULL, "adc");
if (!adc_clock) {
printk(KERN_ERR "failed to get adc clock source\n");
return -ENOENT;
}
clk_enable(adc_clock);//时钟使能
/* normal ADC */
ADCTSC = 0;
//ADC中断申请,采用共享中断,
ret = request_irq(IRQ_ADC, adcdone_int_handler, IRQF_SHARED, DEVICE_NAME, &adcdev); //IRQF_SHARED 共享中断的
if (ret) {
iounmap(base_addr);//解除内存映射
return ret;
}
ret = misc_register(&misc);
printk (DEVICE_NAME"\tinitialized\n");
return ret;
}
static void __exit dev_exit(void)
{
free_irq(IRQ_ADC, &adcdev);//释放中断
iounmap(base_addr);//解除内存映射
//屏蔽和销毁时钟
if (adc_clock)
{
clk_disable(adc_clock);
clk_put(adc_clock);
adc_clock = NULL;
}
misc_deregister(&misc);
}
//导出信号量ADC_LOCK在触摸屏驱动中使用,因为触摸屏和ADC公用寄存器,会产生竞争
//用信号量保证资源的互斥访问
EXPORT_SYMBOL(ADC_LOCK);
module_init(dev_init);
module_exit(dev_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("FriendlyARM Inc.");
2 test
#include
#include
#include
int main(int argc, char **argv)
{
int fd;
//以阻塞方式打开设备文件,非阻塞时flags=O_NONBLOCK
fd = open("/dev/Myadc", 0);
if(fd < 0)
{
printf("Open ADC Device Faild!\n");
exit(1);
}
while(1)
{
int ret;
int data;
ret = read(fd, &data, sizeof(data)); //读设备
if(ret != sizeof(data))
{
if(errno != EAGAIN)
{
printf("Read ADC Device Faild!\n");
}
continue;
}
else
{
printf("Read ADC value is: %d\n", data);
}
}
close(fd);
return 0;
}