⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 adc.c

📁 lpc2200 ad 转换器的驱动代码。在uclinux下编译使用。
💻 C
字号:
/****************************************Copyright (c)**************************************************
**                               Guangzou ZLG-MCU Development Co.,LTD.
**                                     graduate school
**                                 http://www.zlgmcu.com
**
**--------------File Info-------------------------------------------------------------------------------
** File name:           adc.c
** Last modified Date:  2005-04-21
** Last Version:        1.0
** Descriptions:        This is a Kernel module for uClinux 2.4.x .**                      This module let uClinux 2.4.x can use adc. 
**------------------------------------------------------------------------------------------------------
** Created by:          Chenmingji
** Created date:        2005-04-21
** Version:             1.0
** Descriptions:        The original version
**
**------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
**

********************************************************************************************************/
#define IN_adc#include "config.h"/********************************************************************************************************
              function announce********************************************************************************************************/
#if 0static loff_t adc_llseek(struct file *filp, loff_t off, int whence);static ssize_t adc_write(struct file *filp, const char *buf, size_t count, loff_t *f_pos);#endifstatic ssize_t adc_read(struct file *filp, char *buf, size_t count, loff_t *f_pos);
static int adc_open(struct inode *inode, struct file *filp);static int adc_release(struct inode *inode, struct file *filp); static int adc_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,                     unsigned long param);int  adc_init(void);void adc_cleanup(void);
/********************************************************************************************************
              function announce********************************************************************************************************/
#define MAJOR_NR majormodule_init(adc_init);module_exit(adc_cleanup);MODULE_PARM(major, "i");MODULE_LICENSE("Proprietary");MODULE_DESCRIPTION("Guangzou ZLG-MCU Development Co.,LTD.\ngraduate school\nhttp://www.zlgmcu.com");MODULE_SUPPORTED_DEVICE("uClinux2.4.x LPC2200 adc");MODULE_AUTHOR("chenmingji");/*********************************************************************************************************
**                  "全局和静态变量在这里定义"         
**        global variables and static variables define here
********************************************************************************************************/
static int major = ADC_MAJOR_NR;
static u32 PinSel1Save;
static unsigned int adc_usage[MAX_ADC];     /* device using count */
static u32 AdcrSave;/********************************************************************************************************/
static struct file_operations adc_fops =        /* driver info  */{    owner:      THIS_MODULE,#if 0    llseek:     adc_llseek,    write:      adc_write,#endif    read:       adc_read,    ioctl:      adc_ioctl,    open:       adc_open,    release:    adc_release,};#if 0/*********************************************************************************************************
** Function name: adc_llseek
** Descriptions:  move read and write point
** Input: filp:   pointer of file
**        off:    ofset
**        whence: move mode
**                0: seek set
**                1: seek file' current point
**                2: seek file' end
** Output :      new point
** Created by:   Chenmingji
** Created Date: 2005-4-21
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date: 
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
        static loff_t adc_llseek(struct file *filp, loff_t off, int whence){    return 0;}#endif
/*********************************************************************************************************
** Function name: adc_read
** Descriptions:  read device
** Input: filp:   pointer of file
**        buf:    buf for save data
**        count:  size for read
**        f_pos:  *f_pos = read point
** Output : read size    
** Created by:   Chenmingji
** Created Date: 2005-4-21
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date: 
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
        static ssize_t adc_read(struct file *filp, char *buf, size_t count,                           loff_t *f_pos){
    unsigned long flag;    u32 temp;
    u16 temp1;
    u8  temp2;
    
    if (count > 2)
    {        return -EFBIG;
    }

    if (!access_ok(VERIFY_WRITE, (void *)buf, count))
    {
        return -EFAULT;    }

    temp = (int)filp->private_data;

    local_irq_save(flag);
    outl(AdcrSave | (1u << temp) | (1u << 24), ADCR);
    while (1)
    {
        temp = inl(ADDR);
        if ((temp & (1u << 31)) != 0)
        {
            break;
        }
    }
    local_irq_restore(flag);
    temp1 = temp & 0xffff;
    if (count == 1)
    {
        temp2 = temp1 >> 8;
        put_user(temp2, (u8 *)buf);
    }
    if (count == 2)
    {
        put_user(temp1, (u16 *)buf);
    }
    return count;}
#if 0/*********************************************************************************************************
** Function name: adc_write
** Descriptions:  write device
** Input: filp:   pointer of file
**        buf:    buf to write data
**        count:  size for write
**        f_pos:  *f_pos = write point
** Output : write size    
** Created by:   Chenmingji
** Created Date: 2005-4-21
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date: 
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
        static ssize_t adc_write(struct file *filp, const char *buf, size_t count,                            loff_t *f_pos){    return 0;}#endif                /*********************************************************************************************************
** Function name: adc_open
** Descriptions:  open device
** Input:inode:   information of device
**       filp:    pointer of file
** Output 0:      OK
**        other:  not OK
** Created by:    Chenmingji
** Created Date:  2005-4-21
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date: 
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
        static int adc_open(struct inode *inode, struct file *filp){
    unsigned long flag;    int num, temp;
    num = MINOR(inode->i_rdev);    if (num >= MAX_ADC)    {        return -ENODEV;    }
    
    if (adc_usage[num] == 0)
    {
        local_irq_save(flag);        temp = num + num ;
        outl(inl(PINSEL1) & (~(0x03 << (temp + 22))), PINSEL1);
        outl(inl(PINSEL1) | (0x01 << (temp + 22)), PINSEL1);
        filp->private_data = (void *)(num);
        local_irq_restore(flag);
    }    adc_usage[num]++;
    MOD_INC_USE_COUNT;    return 0;          /* success */} /*********************************************************************************************************
** Function name: adc_release
** Descriptions:  release device
** Input:inode:   information of device
**       filp:    pointer of file
** Output 0:      OK
**        other:  not OK
** Created by:    Chenmingji
** Created Date:  2005-4-21
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date: 
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
        static int adc_release(struct inode *inode, struct file *filp) {    unsigned long flag;    int num;
    u32 temp, temp1;
    num = MINOR(inode->i_rdev);    if (num >= MAX_ADC)    {        return -ENODEV;    }
    MOD_DEC_USE_COUNT;    adc_usage[num]--;
    if (adc_usage[num] == 0)
    {
        local_irq_save(flag);

        temp = temp1 = inl(PINSEL1);
        temp &= (PinSel1Save & (0x03 << (num * 2 + 22))) | 
                (temp1 & ~((0x03 << (num * 2 + 22))));

        outl(temp, PINSEL1);
        
        local_irq_restore(flag);    }
    return(0); } /*********************************************************************************************************
** Function name: adc_ioctl
** Descriptions:  IO control function
** Input:inode:   information of device
**       filp:    pointer of file
**       cmd:     command
**       arg:     additive parameter
** Output 0:      OK
**        other:  not OK
** Created by:    Chenmingji
** Created Date:  2005-4-21
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date: 
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
        static int adc_ioctl(struct inode *inode, struct file *filp,                        unsigned int cmd, unsigned long arg){
    u32 temp;
    if (_IOC_TYPE(cmd) != ADC_IOC_MAGIC)    {        return -ENOTTY;    }    if (_IOC_NR(cmd) >= ADC_MAXNR)    {        return -ENOTTY;    }
    switch(cmd)    {        case ADC_SET_CLKDIV:
            temp = AdcrSave;
            temp &= (~(0xff << 8));
            temp |= ((arg & 0xff) << 8);            AdcrSave = temp;            break;        case ADC_SET_BITS:
            arg &= 0x0f;
            if (arg < 3)
            {
                arg = 3;
            }
            if (arg > 10)
            {
                arg = 10;
            }
            arg++;
            arg = 11 - arg;
            temp = AdcrSave;
            temp &= ~(0x07 << 17);
            temp |= (arg << 17);            AdcrSave = temp;            break;
        default:            return -ENOTTY;            break;    }    return 0;}
/*********************************************************************************************************
** Function name: adc_init
** Descriptions:  init driver
** Input:none
** Output 0:      OK
**        other:  not OK
** Created by:    Chenmingji
** Created Date:  2005-4-21
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date: 
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
        int adc_init(void){    int  result;    
    result = register_chrdev(MAJOR_NR,  DEVICE_NAME,  &adc_fops);     if (result < 0)    {        printk(KERN_ERR DEVICE_NAME ": Unable to get major %d\n", MAJOR_NR );        return(result);     } 
    if (MAJOR_NR == 0)    {        MAJOR_NR = result; /* dynamic */    }

    PinSel1Save = inl(PINSEL1) & (0xff << 22);

    AdcrSave = (0xff << 8) | (0x00 << 17) | (1 << 21);
    outl(0, ADCR);
    
    printk(KERN_INFO DEVICE_NAME ": init OK\n");    return(0); }/*********************************************************************************************************
** Function name: adc_cleanup
** Descriptions:  exit driver
** Input:none
** Output none
** Created by:    Chenmingji
** Created Date:  2005-4-21
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date: 
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
        void adc_cleanup(void){
    outl(0, ADCR);
    outl(inl(PINSEL1) & (~(0xff << 22)), PINSEL1);
    outl(inl(PINSEL1) | PinSel1Save, PINSEL1);
    unregister_chrdev(MAJOR_NR, DEVICE_NAME);}/***********************************************************************************************************                            End Of File********************************************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -