现在的位置: 首页 > 综合 > 正文

驱动程序的编译及动态加载方法及ADC驱动程序详解

2013年10月22日 ⁄ 综合 ⁄ 共 7631字 ⁄ 字号 评论关闭

#insmod   adc.ko

#mknod /dev/beep c 253 0      //主次设备号的获取方法请见,主次设备号blog主次设备号的介绍

#chmod +x adc_test

#./adc_test          //为交叉编译器编译的,上面的都应该在目标板上执行。

 

Makefile文件: 

1.目标板的驱动makefile方法( 条件是宿主机已经装好了arm-linux-gcc)

ifneq ($(KERNELRELEASE),)
obj-m:=adc.o
else
KERNELDIR       ?=/opt/FriendlyARM/linux-2.6.32.2
PWD     :=$(shell pwd)
modules:
        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules ARCH=arm CROSS_COMPLIE=arm-none-linux-gnueabi
clean:
        rm -rf *.o
.PHONY:modules clean

endif

2. x86机器上跑的makefile

obj-m   :=second.o

KDIR    :=/lib/modules/$(shell uname -r)/build
PWD     :=$(shell pwd)

default:
        $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
clean:
        rm -rf *.ko
        rm -rf *.mod.*
        rm -rf .*.cmd
        rm -rf *.o

~         

adc.c源文件

      1 #include <linux/errno.h>
      2 #include <linux/kernel.h>
      3 #include <linux/module.h>
      4 #include <linux/slab.h>
      5 #include <linux/input.h>
      6 #include <linux/init.h>
      7 #include <linux/serio.h>
      8 #include <linux/delay.h>
      9 #include <linux/clk.h>
     10 #include <linux/wait.h>
     11 #include <linux/sched.h>
     12 #include <asm/io.h>
     13 #include <asm/irq.h>
     14 #include <asm/uaccess.h>
     15 #include <mach/regs-clock.h>
     16 #include <plat/regs-timer.h>
     17
     18 #include <plat/regs-adc.h>
     19 #include <mach/regs-gpio.h>
     20 #include <linux/cdev.h>
     21 #include <linux/miscdevice.h>
     22
     23 #include "s3c24xx-adc.h"

     25#undef    DEBUG

     27 #ifdef     DEBUG
    28 #define DPRINTK(x...) {printk(__FUNCTION__"(%d): ",__LINE__);printk(##x);}
     29 #else
     30 #define DPRINTK(x...) (void)(0)
     31 #endif
     32
     33 #define DEVICE_NAME     "adc"
     34
     35 static void __iomem *base_addr;
     36
     37 typedef struct {
     38         wait_queue_head_t wait;
     39         int channel;
     40         int prescale;
     41 }ADC_DEV;
     42
     43 DECLARE_MUTEX(ADC_LOCK);     
//该宏声明一个信号量ADC_LOCK,并初始化它的值为0             
          
     44 static int OwnADC = 0;
     45
     46 static ADC_DEV adcdev;
     47 static volatile int ev_adc = 0;
     48 static int adc_data;
     49
     50 static struct clk       *adc_clock;
    52 #define ADCCON      (*(volatile unsigned long *)(base_addr + S3C2410_ADCCO        N))   //ADC control
     53 #define ADCTSC      (*(volatile unsigned long *)(base_addr + S3C2410_ADCTS        C))   //ADC touch screen control
     54 #define ADCDLY      (*(volatile unsigned long *)(base_addr + S3C2410_ADCDL        Y))   //ADC start or Interval Delay
     55 #define ADCDAT0     (*(volatile unsigned long *)(base_addr + S3C2410_ADCDA        T0))  //ADC conversion data 0
     56 #define ADCDAT1     (*(volatile unsigned long *)(base_addr + S3C2410_ADCDA        T1))  //ADC conversion data 1
     57 #define ADCUPDN     (*(volatile unsigned long *)(base_addr + 0x14))     //        Stylus Up/Down interrupt status
     58
     59 #define PRESCALE_DIS        (0 << 14)
     60 #define PRESCALE_EN         (1 << 14)
     61 #define PRSCVL(x)           ((x) << 6)
     62 #define ADC_INPUT(x)        ((x) << 3)
     63 #define ADC_START           (1 << 0)
     64 #define ADC_ENDCVT          (1 << 15)
     65
     66 #define START_ADC_AIN(ch, prescale) \
     67         do{ \
      68                 ADCCON = PRESCALE_EN | PRSCVL(prescale) | ADC_INPUT((ch))         ; \
     69                 ADCCON |= ADC_START; \
     70         }while(0)
     71
     72
     73 static irqreturn_t adcdone_int_handler(int irq, void *dev_id)
     74 {
     75         if (OwnADC) {
     76                 adc_data = ADCDAT0 & 0x3ff;
     77
     78                 ev_adc = 1;
     79                 wake_up_interruptible(&adcdev.wait);
     80         }
     81
     82         return IRQ_HANDLED;
     83 }
     84
     85 static ssize_t s3c2410_adc_read(struct file *filp, char *buffer, size_t co        unt, loff_t *ppos)
     86 {
     87         char str[20];
     88         int value;
     89         size_t len;
     90         if (down_trylock(&ADC_LOCK) == 0) {                                      //该函数试着获得信号量sem,如果能够立刻获得,它就获得该信号量并返回0,否则,表示不能获得信号量sem,返回值为非0值。因此,它不会导致调用者睡眠,可以在中断上下文使用。
     91                 OwnADC = 1;
     92                 START_ADC_AIN(adcdev.channel, adcdev.prescale);
     93                 wait_event_interruptible(adcdev.wait, ev_adc);
     94
     95                 ev_adc = 0;
     96
     97                 DPRINTK("AIN[%d] = 0x%04x, %d\n", adcdev.channel, adc_data        , ADCCON & 0x80 ? 1:0);
     98
     99                 value = adc_data;
    100
    101                 OwnADC = 0;
    102                 up(&ADC_LOCK);              //解锁
    103         } else {
    104                 value = -1;
    105         }
    106
    107         len = sprintf(str, "%d\n", value);
    108         if (count >= len) {

     109                 int r = copy_to_user(buffer, str, len);
    110                 return r ? r : len;
    111         } else {
    112                 return -EINVAL;
    113         }
    114 }
    115
    116 static int s3c2410_adc_open(struct inode *inode, struct file *filp)
    117 {
    118         init_waitqueue_head(&(adcdev.wait));
    119
    120         adcdev.channel=0;
    121         adcdev.prescale=0xff;
    122
    123         DPRINTK( "adc opened\n");
    124         return 0;
    125 }
    126
    127 static int s3c2410_adc_release(struct inode *inode, struct file *filp)
    128 {
    129         DPRINTK( "adc closed\n");
    130         return 0;
    131 }
133
    134 static struct file_operations dev_fops = {
    135         owner:  THIS_MODULE,
    136         open:   s3c2410_adc_open,
    137         read:   s3c2410_adc_read,
    138         release:        s3c2410_adc_release,
    139 };
    140
    141 static struct miscdevice misc = {
    142         .minor = MISC_DYNAMIC_MINOR,
    143         .name = DEVICE_NAME,
    144         .fops = &dev_fops,
    145 };
    146
    147 static int __init dev_init(void)
    148 {
    149         int ret;
    150
    151         base_addr=ioremap(S3C2410_PA_ADC,0x20);   //0x20 is the size that         map
    152         if (base_addr == NULL) {
    153                 printk(KERN_ERR "Failed to remap register block\n");
                            return -ENOMEM;
        }

一、虚拟地址的应用,就是物理地址转换为虚拟地址,ADC驱动程序中的寄存器就是都用到了虚拟地址了。其中最主要的转换的语句是

base_addr=ioremap(S3C2410_PA_ADC,0x20);/

ioremap()功能: 将一个IO地址空间映射到内核的虚拟地址空间上去,返回值就是虚拟地址。  物理地址和虚拟地址的转换,就是将S3C2410_PA_ADC物理地址转换为base_addr 的地址。 

S3C2410_PA_ADC 为物理地址0x58000000 ,(包含了AD控制的所有寄存器)

base_addr 虚拟地址,应该是有系统分配的,调用ioremap()既能分配到。

而base_addr 的定义是 static void __iomem *base_addr;

使用完后需要 iounmap(base_addr);释放掉虚拟地址。

 

        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);// shared the int
        if (ret) {
                iounmap(base_addr);
                return ret;
        }

        ret = misc_register(&misc);

        printk (DEVICE_NAME"\tinitialized\n");

}

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);
}

EXPORT_SYMBOL(ADC_LOCK);     // 内核符号导出使用
module_init(dev_init);
module_exit(dev_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("FriendlyARM Inc.");

 测试函数adc_test.c

#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);   //休眠0.5秒
        }

        close(fd);
}

 

 

抱歉!评论已关闭.