linux4.4 JZ ADC驱动

1.驱动开发
在 module_driver 目录之下 adc.c adc_hal.c

三要素 : 
module_init(jz_adc_init);
module_exit(jz_adc_exit);

static struct file_operations adc_fops= {
    .owner= THIS_MODULE,
    .open= adc_open,
    .read = adc_read,
    .release= adc_release,
    .unlocked_ioctl= adc_ioctl,
};

有四路adc通道   
struct miscdevice adc_mdev[ADC_MAX_CHANNELS] = {
    {
        .minor   = MISC_DYNAMIC_MINOR,
        .name    = "jz_adc_aux_0",
        .fops    = &adc_fops,
    },
    {
        .minor   = MISC_DYNAMIC_MINOR,
        .name    = "jz_adc_aux_1",
        .fops    = &adc_fops,
    },
    {
        .minor   = MISC_DYNAMIC_MINOR,
        .name    = "jz_adc_aux_2",
        .fops    = &adc_fops,
    },
    {
        .minor   = MISC_DYNAMIC_MINOR,
        .name    = "jz_adc_aux_3",
        .fops    = &adc_fops,
    }
};

static struct file_operations adc_fops= {
    .owner= THIS_MODULE,
    .open= adc_open,
    .read = adc_read,
    .release= adc_release,
    .unlocked_ioctl= adc_ioctl,
};

hal.c中
#define SADC_REG_BASE   0xB0070000  //此为芯片adc地址

#define SADC_ADDR(reg) ((volatile unsigned long *)(SADC_REG_BASE + reg))

inline void adc_hal_enable_controller(void)
{
    unsigned int val;
    unsigned int count = 100000;

    do {
        adc_set_bit(ADENA, POWER, 0);
        mdelay(2);
        val = adc_get_bit(ADENA, POWER);
    } while (val && --count);

    usleep_range(2000, 2000);

    if (count == 0)
        printk(KERN_ERR "adc hal enable timeout\n");
}

static inline void adc_set_bit(unsigned long reg, int bit, unsigned int val)
{
    set_bit_field(SADC_ADDR(reg), bit, bit, val);
}

2.应用层调用

#include <stdio.h>
#include <libhardware2/adc.h>             //包含adc相关api 
#include <stdint.h>												
#include <unistd.h>

int main(void)										    //读取adc电压示例 
{

	long handle = adc_enable(0);                 //打开adc 
	if(handle==-1)
        printf("open adc fail......\n ");

	int  fd0 = adc_set_vref( handle, 3300);   //设置基准定压 

	while (1)
	{
	
	if (fd0!=0)
	{
		 printf("adc_set_vref fail......\n ");
	}

     int value0 = adc_get_value(handle);   //原始值
	int value1 = adc_get_voltage(handle); //计算值

	 printf("value0 = %d,voltage = %d \n ",value0,value1);
	 sleep(3);

}


    return 0;
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值