ADC 驅動移植

(1)Linux-2.6.32.2 內核並沒有提供支持S3C2440 的ADC 驅動程序,所以需要在drivers/char 目錄下,創建一個驅動程序的文件名爲:
mini2440_adc.c(囯嵌提供)

在 S3C2440 芯片中,AD 輸入和觸摸屏接口使用共同的A/D 轉換器

ADC 驅動和觸摸屏驅動若想共存,就必須解決共享“A/D 轉換器”資源這個問

題,因此在ADC 驅動程序中聲明瞭一個全局的“ADC_LOCK”信號量

添加mini2440_adc.c內容如下:

#incl#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/input.h>
#include <linux/init.h>
#include <linux/serio.h>
#include <linux/delay.h>
#include <linux/clk.h>
#include <linux/wait.h>
#include <linux/sched.h>
#include <asm/io.h>
#include <asm/irq.h>
#include <asm/uaccess.h>
#include <mach/regs-clock.h>
#include <plat/regs-timer.h>
#include <plat/regs-adc.h>
#include <mach/regs-gpio.h>
#include <linux/cdev.h>
#include <linux/miscdevice.h>
;自己定義的頭文件,因原生內核並沒有包含
#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
;定義ADC 轉換設備名稱,將出現在/dev/adc
#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 讀函數,一般對應於用戶層/應用層的設備讀函數(read)
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, %d\n", adcdev.channel, adc_data, ADCCON & 0x80 ? 1:0);
;把轉換結果賦予value,以便傳遞到用戶層/應用層
value = adc_data;
;釋放“A/D 轉換器”資源
OwnADC = 0;
up(&ADC_LOCK);
} else {
;沒有“A/D 轉換器”資源,賦值爲“-1”
value = -1;
}
len = sprintf(str, "%d\n", value);
if (count >= len) {
;把轉換結果傳遞到用戶層/應用層
int r = copy_to_user(buffer, str, len);
return r ? r : len;
} else {
return -EINVAL;
}
}
;打開ADC 設備的函數,一般對應於用戶態程序的open
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 opened\n");
return 0;
}
static int s3c2410_adc_release(struct inode *inode, struct file *filp)
{
DPRINTK( "adc closed\n");
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 block\n");
return -ENOMEM;
}
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;
;註冊中斷
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"\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”,以便觸摸屏驅動使用
EXPORT_SYMBOL(ADC_LOCK);
module_init(dev_init);
module_exit(dev_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("FriendlyARM Inc.");


(2)上面的驅動程序中還包含了一個簡單的頭文件“s3c24xx-adc.h”,它也在drivers/char目錄下,內容爲:

#ifndef _S3C2410_ADC_H_
#define _S3C2410_ADC_H_
#define ADC_WRITE(ch, prescale) ((ch)<<16|(prescale))
#define ADC_WRITE_GETCH(data) (((data)>>16)&0x7)
#define ADC_WRITE_GETPRE(data) ((data)&0xff)
#endif /* _S3C2410_ADC_H_ */

(3)然後打開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

(4)再打開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

(5)在內核源代碼目錄的命令行執行:make
menuconfig,依次選擇如下子菜單項,找到剛剛添加的ADC 驅動配置選項:

Device Drivers --->
Character devices --->


然後退出保存所選配置, 在命令行執行: make zImage , 將會生成
arch/arm/boot/zImage,使用supervivi 的“k”命令把它燒寫到開發


(6)編寫一個測試程序 adc-test .c 並在虛擬機終端用命令arm-linux-gcc -o adc-test adc-test編譯再把生成文件下至開發板,它的源代碼如
下:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/fs.h>
#include <errno.h>
#include <string.h>
int main(void)
{
fprintf(stderr, "press Ctrl-C to stop\n");
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] = '\0';
int value = -1;
sscanf(buffer, "%d", &value);
printf("ADC Value: %d\n", value);
} else {
perror("read ADC device:");
return 1;
}
usleep(500* 1000);
}
close(fd);
}

(7)執行adc-test旋轉開發板上的W1 可調電阻,可以看到ADC 轉換的結果也在變動,如圖



按Ctrl + C 退出。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章