1.2.1 关于 S3C2440 的 ADC 和触摸屏接口
Linux-2.6.32.2 内核并没有提供支持 S3C2440 的 ADC 驱动程序,因此我们自行设计了一个,这个驱动比较简单,属于字符设备,。在 S3C2440 芯片中,AD 输入和触摸屏接口使用共同的 A/D 转换器,见S3C2440芯片手册第 16 章节,如图。
1.2.2 在内核中添加 ADC 驱动
在 S3C2440 芯片中,AD 输入和触摸屏接口使用共同的 A/D 转换器,因此,ADC 驱动和触摸屏驱动若想共存,就必须解决共享"A/D 转换器"资源这个问
题,因此在 ADC 驱动程序中声明了一个全局的"ADC_LOCK"信号量。
(1)将ADC驱动程序(s3c24xx-adc.h文件和mini2440_adc.c)放于drivers/char目录下
//s3c24xx-adc.h 内容如下:
#ifndef _S3C2410_ADC_H_
#define _S3C2410_ADC_H_
#define ADC_WRITE_GETCH(data) (((data)>>16)&0x7)
#define ADC_WRITE_GETPRE(data) ((data)&0xff)
// ADC_WRITE_GETCH(data)得到ch ADC_WRITE_GETPRE(data)得到prescale!!
#define ADC_WRITE(ch, prescale) ((ch)<<16|(prescale))
#endif /* _S3C2410_ADC_H_ */
//mini2440_adc.c 内容如下:
/*ADC驱动*/
#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 "adc" static void __iomem *base_addr; //定义ADC设备就结构 typedef struct { wait_queue_head_t wait; int channel; int prescale; }ADC_DEV; //声明全局变量,以使和触摸屏驱动程序共享A/D转换器 DECLARE_MUTEX(ADC_LOCK); //ADC驱动是否拥有A/D转换器资源的状态变量 static int OwnADC = 0; static ADC_DEV adcdev; static volatile int ev_adc = 0; static int adc_data; 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 #define PRESCALE_DIS (0 << 14) #define PRESCALE_EN (1 << 14) #define PRSCVL(x) ((x) << 6) #define ADC_INPUT(x) ((x) << 3) #define ADC_START (1 << 0) #define ADC_ENDCVT (1 << 15) //开启AD输入 #define START_ADC_AIN(ch, prescale) do{ ADCCON = PRESCALE_EN | PRSCVL(prescale) | ADC_INPUT((ch)) ; ADCCON |= ADC_START; }while(0) //ADC中断处理函数 static irqreturn_t adcdone_int_handler(int irq, void *dev_id) { //如果ADC驱动拥有A/D转换器资源,则从ADC寄存器读取转换状态 if (OwnADC) { adc_data = ADCDAT0 & 0x3ff; ev_adc = 1; wake_up_interruptible(&adcdev.wait); } return IRQ_HANDLED; } //ADC读函数, 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; //判断'A/D转换器'资源是否可用 if (down_trylock(&ADC_LOCK) == 0) { OwnADC = 1;//标记为'A/D转换器'资源是可用 START_ADC_AIN(adcdev.channel, adcdev.prescale);//开始转换 wait_event_interruptible(adcdev.wait, ev_adc);//等待转换结束 ev_adc = 0; DPRINTK("AIN[%d] = 0x%04x, %dn", adcdev.channel, adc_data, ADCCON & 0x80 ? 1:0); //把转换结果赋值,以便传递到用户层 value = adc_data; //释放A/D转换器资源 OwnADC = 0; up(&ADC_LOCK); } else { //没有A/D转换器资源 value = -1; } len = sprintf(str, "%dn", value); if (count >= len) { //将转换结果传到到用户层 int r = copy_to_user(buffer, str, len); return r ? r : len; } else { return -EINVAL; } } //ADC设备的打开函数 static int s3c2410_adc_open(struct inode *inode, struct file *filp) { //初始化中断队列 init_waitqueue_head(&(adcdev.wait)); //缺省通道为'0' adcdev.channel=0; adcdev.prescale=0xff; DPRINTK( "adc openedn"); return 0; } static int s3c2410_adc_release(struct inode *inode, struct file *filp) { DPRINTK( "adc closedn"); return 0; } 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; base_addr=ioremap(S3C2410_PA_ADC,0x20); if (base_addr == NULL) { printk(KERN_ERR "Failed to remap register blockn"); return -ENOMEM; } adc_clock = clk_get(NULL, "adc"); if (!adc_clock) { printk(KERN_ERR "failed to get adc clock sourcen"); return -ENOENT; } clk_enable(adc_clock); /* 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"tinitializedn"); 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"以便触摸屏驱动使用 EXPORT_SYMBOL(ADC_LOCK); module_init(dev_init); module_exit(dev_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("FriendlyARM Inc."); 然后打开 drivers/char/Makefile 文件,在大概 114 行(这里没有必要一定是,看到类似的地方加进去就行了)加入 ADC 驱动程序目标模块: obj-$(CONFIG_JS_RTC) += js-rtc.o js-rtc-y = rtc.o obj-$(CONFIG_MINI2440_ADC) += mini2440_adc.o # Files generated that shall be removed upon make clean clean-files := consolemap_deftbl.c defkeymap.c (2)再打开 drivers/char/Kconfig 文件,加入 ADC 驱动配置选项: config DEVKMEM bool "/dev/kmem virtual device support" default y help Say Y here if you want to support the /dev/kmem device. The /dev/kmem device is rarely used, but can be used for certain kind of kernel debugging operations. When in doubt, say "N". config MINI2440_ADC bool "ADC driver for FriendlyARM Mini2440 development boards" depends on MACH_MINI2440 default y if MACH_MINI2440 help this is ADC driver for FriendlyARM Mini2440 development boards Notes: the touch-screen-driver required this option config BFIN_JTAG_COMM tristate "Blackfin JTAG Communication" depends on BLACKFIN help (3)这样,我们就在内核中添加了 ADC 驱动,现在内核源代码目录的命令行执行:make menuconfig,依次选择如下子菜单项,找到刚刚添加的 ADC 驱动配置选项: Device Drivers ---> Character devices ---> 按空格键选中 ADC 配置选项 (4)然 后 退 出 保 存 所 选 配 置 , 在 命 令 行 执 行 : make zImage , 将 会 生 成 arch/arm/boot/zImage,使用 supervivi 的"k"命令把它烧写到开发板。 1.2.3 ADC 测试程序 我们在这里使用友善之臂自带的文件系统,里面有一个adc-test命令,它的源代码如下: #include #include #include #include #include #include #include #include #include #include int main(void) { fprintf(stderr, "press Ctrl-C to stopn"); int fd = open("/dev/adc", 0); if (fd < 0) { perror("open ADC device:"); return 1; } for(;;) { char buffer[30]; int len = read(fd, buffer, sizeof buffer -1); if (len > 0) { buffer[len] = '