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