当前位置: 技术问答>linux和unix
s3c2410 ad驱动采集数值的诡异问题
来源: 互联网 发布时间:2016-04-30
本文导语: 最近在开发yc2410 arm的ad驱动,经过一段时间的调试,总算有时候可以得到正确 的数值了(注:是有时候)。结果发现,在某个地方加上一句printk语句后,就可以得到正确的电压采集值,如果不加上这一句,或者多加...
最近在开发yc2410 arm的ad驱动,经过一段时间的调试,总算有时候可以得到正确 的数值了(注:是有时候)。结果发现,在某个地方加上一句printk语句后,就可以得到正确的电压采集值,如果不加上这一句,或者多加上一句,或者换成别的语句就是死活不对(采集的电压值总是很小,大概0.5左右)。实在是很无语了 ,望各路大侠能相助。源代码我贴在下面了,那句printk我已经在程序中注明了。
首先是.h文件:
-------------------------------------------------------------
struct adc_dev {
struct semaphore sem; /* mutual exclusion semaphore */
struct cdev cdev; /* Char device structure */
};
#define adc_MAJOR 0
#define adc_MINOR 0
-------------------------------------------------------------
然后是程序主文件:
--------------------------------------------------------------------------------------------------------------------------
#ifndef __KERNEL__
#define __KERNEL__
#endif
#ifndef MODULE
#define MODULE
#endif
#define Debug_Ram4
#define vPRSCVL 49
#define ADCRAY_IOC_MAGIC 't'
#define ADCRAY_SEL _IOW(ADCRAY_IOC_MAXNR, 0, int)//set which adc will read
#define ADCRAY_IOC_MAXNR 2
#define adc_number 8
#include
#include
#include
#include /* printk() */
#include /* kmalloc() */
#include /* everything... */
#include /* error codes */
#include /* size_t */
#include
#include /* O_ACCMODE */
#include
#include
#include /* cli(), *_flags */
#include /* copy_*_user */
#include
#include
#include
#include "adcray.h" /* local definitions */
#include /* needed for the _IOW etc stuff used later */
#include
#include
MODULE_LICENSE("Dual BSD/GPL");
static int adc_major = adc_MAJOR;
static int adc_minor = adc_MINOR;
static dev_t dev;
static struct adc_dev *adc_devices;
typedef struct _adc_trbl{
int Adc_number;
int can_be_used;//tto excluded adc port that is used for other purpose
int To_Be_Used;// 0 : not used 1 : used
}adc_trbl;
static adc_trbl adcarray [adc_number] = {
{0, 0, 0},
{1, 0, 0},
{2, 0, 0},
{3, 0, 0},
{4, 0, 0},
{5, 1, 0},
{6, 0, 0},
{7, 1, 0}
};
/* adin 0 1 2 3 4 6 are free to use while adin 5 and 7 are connected to lcd module*/
int __init adc_init(void);
void __exit adc_cleanup_module(void);
int adc_devnum(void);
void adc_cleanup_module(void);
void adc_setup_cdev(struct adc_dev *dev);
int adc_open (struct inode *inode, struct file *filp);
ssize_t adc_read(struct file *filp, char __user *buff, size_t count, loff_t* f_pos);
int adc_release(struct inode *inode, struct file *filp);
int adc_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg);
ssize_t adc_read(struct file *filp, char __user *buff, size_t count, loff_t* f_pos)
{
int result=0, i=0;
unsigned int ADCCON_Get_Val = 0, ADCCON_Set_Val=0;
int each_adc_get_val=0, ret_adc_value[adc_number];
ssize_t retval=0;
#ifdef Debug_Ram3
printk(KERN_ALERT "call read funcn");
#endif
memset(ret_adc_value, 0, adc_number* sizeof(int));
for(i=0; i ADCRAY_IOC_MAXNR) return -ENOTTY;
switch (cmd)
{
case ADCRAY_SEL:/* arg -> which adc to read */ /* use bit 0 to bit 7 to tell which will used */
for(i =0 ;icdev, devno, 1);
}
int __init adc_init(void)
{
int result;
if ( (result = adc_devnum())
首先是.h文件:
-------------------------------------------------------------
struct adc_dev {
struct semaphore sem; /* mutual exclusion semaphore */
struct cdev cdev; /* Char device structure */
};
#define adc_MAJOR 0
#define adc_MINOR 0
-------------------------------------------------------------
然后是程序主文件:
--------------------------------------------------------------------------------------------------------------------------
#ifndef __KERNEL__
#define __KERNEL__
#endif
#ifndef MODULE
#define MODULE
#endif
#define Debug_Ram4
#define vPRSCVL 49
#define ADCRAY_IOC_MAGIC 't'
#define ADCRAY_SEL _IOW(ADCRAY_IOC_MAXNR, 0, int)//set which adc will read
#define ADCRAY_IOC_MAXNR 2
#define adc_number 8
#include
#include
#include
#include /* printk() */
#include /* kmalloc() */
#include /* everything... */
#include /* error codes */
#include /* size_t */
#include
#include /* O_ACCMODE */
#include
#include
#include /* cli(), *_flags */
#include /* copy_*_user */
#include
#include
#include
#include "adcray.h" /* local definitions */
#include /* needed for the _IOW etc stuff used later */
#include
#include
MODULE_LICENSE("Dual BSD/GPL");
static int adc_major = adc_MAJOR;
static int adc_minor = adc_MINOR;
static dev_t dev;
static struct adc_dev *adc_devices;
typedef struct _adc_trbl{
int Adc_number;
int can_be_used;//tto excluded adc port that is used for other purpose
int To_Be_Used;// 0 : not used 1 : used
}adc_trbl;
static adc_trbl adcarray [adc_number] = {
{0, 0, 0},
{1, 0, 0},
{2, 0, 0},
{3, 0, 0},
{4, 0, 0},
{5, 1, 0},
{6, 0, 0},
{7, 1, 0}
};
/* adin 0 1 2 3 4 6 are free to use while adin 5 and 7 are connected to lcd module*/
int __init adc_init(void);
void __exit adc_cleanup_module(void);
int adc_devnum(void);
void adc_cleanup_module(void);
void adc_setup_cdev(struct adc_dev *dev);
int adc_open (struct inode *inode, struct file *filp);
ssize_t adc_read(struct file *filp, char __user *buff, size_t count, loff_t* f_pos);
int adc_release(struct inode *inode, struct file *filp);
int adc_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg);
ssize_t adc_read(struct file *filp, char __user *buff, size_t count, loff_t* f_pos)
{
int result=0, i=0;
unsigned int ADCCON_Get_Val = 0, ADCCON_Set_Val=0;
int each_adc_get_val=0, ret_adc_value[adc_number];
ssize_t retval=0;
#ifdef Debug_Ram3
printk(KERN_ALERT "call read funcn");
#endif
memset(ret_adc_value, 0, adc_number* sizeof(int));
for(i=0; i ADCRAY_IOC_MAXNR) return -ENOTTY;
switch (cmd)
{
case ADCRAY_SEL:/* arg -> which adc to read */ /* use bit 0 to bit 7 to tell which will used */
for(i =0 ;icdev, devno, 1);
}
int __init adc_init(void)
{
int result;
if ( (result = adc_devnum())