天嵌 ARM开发社区

 找回密码
 注册
查看: 2220|回复: 0

tq2440 adc+中断驱动

[复制链接]
freewing 发表于 2016-3-24 15:20:50 | 显示全部楼层 |阅读模式

 TQ2440是由广州天嵌计算机科技有限公司精心打造的一款开发板。以稳定的性能,过硬的质量,丰富的扩展接口,优质的售后服务和技术支持,赢得众多企业的青睐和支持。

  基于tq2440开发板,内核2.6.30

  功能

  能进中断了,而且通道2读出来的数据也正确。

  点击触摸屏的时候可能会混淆。因为触摸屏用的也是 adc-irq .

  #include

  #include

  #include

  #include

  #include

  #include

  #include

  #include

  #include

  #include

  #include

  #include

  #include

  #include

  #include

  #include

  #include

  #include

  #include

  #include

  #include

  #include

  #include

  #include

  #include

  #include

  #include

  #include

  #define DEVICE_NAME "driver_adc"

  volatile unsigned long *adccon = NULL; //adc控制寄存器

  volatile unsigned long *adctsc = NULL; //adc 触摸屏控制寄存器

  volatile unsigned long *adcdly = NULL; //adc 起始延迟寄存器

  volatile unsigned long *adcdat0 = NULL; //adc 转换数据寄存器

  volatile unsigned long *adcdat1 = NULL; //adc 转换数据寄存器

  volatile unsigned long *intmsk = NULL;

  //*gpbdat &= ~((1<<5) );

  //ain2

  static DECLARE_WAIT_QUEUE_HEAD(adc_waitq);

  static volatile int val=0;

  static volatile int ev_press = 0;

  static irqreturn_t adc_irq(int irq, void *dev_id)

  {

  printk("-------------adc_irq is ok ------------\n");

  ev_press=1;

  //*adccon |= 0X1; //开始转换

  val=(*adcdat0&0x3ff);

  wake_up_interruptible(&adc_waitq);

  return IRQ_RETVAL(IRQ_HANDLED);

  }

  // ------------------- OPEN ------------------------

  ssize_t drive_open (struct inode * inode ,struct file * file)

  {

  printk("-----------------drive open ok----------------\n");

  request_irq(IRQ_ADC,adc_irq, IRQF_SAMPLE_RANDOM|IRQF_SHARED,"adc_irq",1);

  return 0;

  }

  // ------------------- RELEASE/CLOSE ---------------

  ssize_t drive_release (struct inode * inode ,struct file * file)

  {

  printk("-----------------drive close ok----------------\n");

  free_irq(IRQ_ADC, 1);

  return 0;

  }

  // ------------------- READ ------------------------

  ssize_t drive_read (struct file * file ,char * buf, size_t count, loff_t * f_ops)

  {

  printk("-----------------drive read ok----------------\n");

  *adccon |= (1<<14)|(0xff<<6)|(2<<3);//设置分频倍数0xff,设置输入通道为2

  //*intmsk |= (1<<31)| (1<<5)|(1<<8);

  *adccon |= 0X1; //开始转换

  wait_event_interruptible(adc_waitq, ev_press);

  ev_press = 0;

  /*

  while( !(*adccon & 0x8000) ) //等待转换结束

  ;

  val=(*adcdat0&0x3ff);

  */

  *adccon &=~1; //关adc

  printk("----------------drive -----val=%d \n",val);

  copy_to_user( buf,&val,sizeof(val) );

  printk("---------------drive-close-ok-------------\n",val);

  return val;

  }

  // ------------------- WRITE -----------------------

  ssize_t drive_write (struct file * file ,const char * buf, size_t count, loff_t * f_ops)

  {

  printk("-----------------drive write ok----------------\n");

  return 0;

  }

  // ------------------- IOCTL -----------------------

  ssize_t drive_ioctl (struct inode * inode ,struct file * file, unsigned int cmd, unsigned long arg)

  {

  printk("-----------------drive ioctl ok----------------\n");

  return 0;

  }

  // -------------------------------------------------

  static struct file_operations drive_ops ={

  .owner = THIS_MODULE,

  .open = drive_open,

  .read = drive_read,

  .write = drive_write,

  .ioctl = drive_ioctl,

  .release = drive_release,

  };

  static struct miscdevice misc = {

  .minor = MISC_DYNAMIC_MINOR,

  .name = DEVICE_NAME,

  .fops = &drive_ops,

  };

  static int __init init_drive(void)

  {

  int ret;

  ret = misc_register(&misc);

  adccon = (volatile unsigned long *)ioremap(0x58000000, 16);

  adctsc = (volatile unsigned long *)ioremap(0x58000004, 8);

  adcdly = (volatile unsigned long *)ioremap(0x58000008, 16);

  adcdat0= (volatile unsigned long *)ioremap(0x5800000c, 16);

  adcdat1= (volatile unsigned long *)ioremap(0x58000010, 16);

  intmsk = (volatile unsigned long *)ioremap(0x4a000008, 32);

  printk("-----------------drive button init ok----------------\n");

  return 0;

  }

  static void __exit exit_drive(void)

  {

  misc_deregister(&misc);

  printk("-----------------drive button exit ok----------------\n");

  }

  module_init(init_drive);

  module_exit(exit_drive);

  MODULE_LICENSE("GPL");

  //-------------------------------------应用程序------------------------------------------------

  #include

  #include

  #include

  #include // open() close()

  #include // read() write()

  #define DEVICE_NAME "/dev/driver_adc"

  //------------------------------------- main ---------------------------------------------

  int main(int argc, char **argv)

  {

  int fd,ret;

  int val;

  fd = open(DEVICE_NAME, O_RDWR);

  if (fd == -1)

  {

  printf("can't open device mknod %s c zhu ci \n",DEVICE_NAME);

  return 0;

  }

  while(1)

  {

  read(fd,&val,sizeof(val));

  printf("------------------app--------------------\n");

  printf("val=%d\n",val);

  sleep(3);

  }

  // close

  ret = close(fd);

  if (ret == -1)

  {

  printf("app close error!!!!!!!!!!\n");

  return 0;

  }

  return 0;


  }// end main


感谢chen4013874的分享!


  供货情况:

  天嵌科技提供专业嵌入式板卡和行业解决方案。如有需要,可以联系天嵌科技的销售人员。

  销售电话:020-38219416     38373101

  技术支持:020-38219416转807     820

  网址:http://www.embedsky.com


您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

i.MX8系列ARM cortex A53 M4 工控板上一条 /1 下一条

Archiver|手机版|小黑屋|天嵌 嵌入式开发社区 ( 粤ICP备11094220号-2 )

GMT+8, 2024-6-11 02:39 , Processed in 1.031250 second(s), 22 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表