主题 : andoid驱动问题已解决 复制链接 | 浏览器收藏 | 打印
级别: 侠客
UID: 54019
精华: 0
发帖: 76
金钱: 380 两
威望: 76 点
综合积分: 152 分
注册时间: 2011-08-22
最后登录: 2012-03-17
楼主  发表于: 2011-09-17 17:37

 andoid驱动问题已解决

总结如下:
1.自己新添加的设备驱动,默认没有读写权限。
需修改设备权限才能正常访问。
2.android下的应用程序需要使用静态编译(实际上也可以使用动态库
)
关于android下c程序使用动态库的方法请参考 http://www.linuxidc.com/Linux/2011-04/34404.htm



我拷贝了driver/char下面的mini6410_pwm.c 存为mini6410_timer.c
然后改DEVICE_NAME为 timer 并改了一些函数名称
代码如下
然后我在jni下 调用 open("/dev/timer"); 返回错误
另 :在adb shell 查看dev文件夹 中是可以看到/dev/timer的
sys/class/misc/下timer也有 说明设备注册成功 ,但为何无法打开?
我表示粉郁闷


复制代码
  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/fs.h>
  4. #include <linux/init.h>
  5. #include <linux/delay.h>
  6. #include <linux/poll.h>
  7. #include <asm/irq.h>
  8. #include <asm/io.h>
  9. #include <linux/interrupt.h>
  10. #include <asm/uaccess.h>
  11. #include <mach/hardware.h>
  12. #include <plat/regs-timer.h>
  13. #include <mach/regs-irq.h>
  14. #include <asm/mach/time.h>
  15. #include <linux/clk.h>
  16. #include <linux/cdev.h>
  17. #include <linux/device.h>
  18. #include <linux/miscdevice.h>
  19. #include <mach/map.h>
  20. #include <mach/regs-clock.h>
  21. #include <mach/regs-gpio.h>
  22. #include <plat/gpio-cfg.h>
  23. #include <mach/gpio-bank-e.h>
  24. #include <mach/gpio-bank-f.h>
  25. #include <mach/gpio-bank-k.h>
  26. #define DEVICE_NAME     "timer"
  27. #define TIMER_IOCTL_SET_FREQ        1
  28. #define TIMER_IOCTL_STOP            0
  29. static struct semaphore lock;
  30. /* freq:  pclk/50/16/65536 ~ pclk/50/16
  31.   * if pclk = 50MHz, freq is 1Hz to 62500Hz
  32.   * human ear : 20Hz~ 20000Hz
  33.   */
  34. static void timer_Set_Freq( unsigned long freq )
  35. {
  36.     unsigned long tcon;
  37.     unsigned long tcnt;
  38.     unsigned long tcfg1;
  39.     unsigned long tcfg0;
  40.     struct clk *clk_p;
  41.     unsigned long pclk;
  42.     unsigned tmp;
  43.     tmp = readl(S3C64XX_GPFCON);
  44.     tmp &= ~(0x3U << 28);
  45.     tmp |=  (0x2U << 28);
  46.     writel(tmp, S3C64XX_GPFCON);
  47.     tcon = __raw_readl(S3C_TCON);
  48.     tcfg1 = __raw_readl(S3C_TCFG1);
  49.     tcfg0 = __raw_readl(S3C_TCFG0);
  50.     //prescaler = 50
  51.     tcfg0 &= ~S3C_TCFG_PRESCALER0_MASK;
  52.     tcfg0 |= (50 - 1);
  53.     //mux = 1/16
  54.     tcfg1 &= ~S3C_TCFG1_MUX0_MASK;
  55.     tcfg1 |= S3C_TCFG1_MUX0_DIV16;
  56.     __raw_writel(tcfg1, S3C_TCFG1);
  57.     __raw_writel(tcfg0, S3C_TCFG0);
  58.     clk_p = clk_get(NULL, "pclk");
  59.     pclk  = clk_get_rate(clk_p);
  60.     tcnt  = (pclk/50/16)/freq;
  61.     
  62.     __raw_writel(tcnt, S3C_TCNTB(0));
  63.     __raw_writel(tcnt/2, S3C_TCMPB(0));
  64.                 
  65.     tcon &= ~0x1f;
  66.     tcon |= 0xb;        //disable deadzone, auto-reload, inv-off, update TCNTB0&TCMPB0, start timer 0
  67.     __raw_writel(tcon, S3C_TCON);
  68.     
  69.     tcon &= ~2;            //clear manual update bit
  70.     __raw_writel(tcon, S3C_TCON);
  71. }
  72. void timer_Stop( void )
  73. {
  74.     unsigned tmp;
  75.     tmp = readl(S3C64XX_GPFCON);
  76.     tmp &= ~(0x3U << 28);
  77.     writel(tmp, S3C64XX_GPFCON);
  78. }
  79. static int s3c64xx_timer_open(struct inode *inode, struct file *file)
  80. {
  81.     if (!down_trylock(&lock))
  82.         return 0;
  83.     else
  84.         return -EBUSY;
  85. }
  86. static int s3c64xx_timer_close(struct inode *inode, struct file *file)
  87. {
  88.     up(&lock);
  89.     return 0;
  90. }
  91. static long s3c64xx_timer_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
  92. {
  93.     switch (cmd) {
  94.         case TIMER_IOCTL_SET_FREQ:
  95.             if (arg == 0)
  96.                 return -EINVAL;
  97.             timer_Set_Freq(arg);
  98.             break;
  99.         case TIMER_IOCTL_STOP:
  100.         default:
  101.             timer_Stop();
  102.             break;
  103.     }
  104.     return 0;
  105. }
  106. static struct file_operations dev_fops = {
  107.     .owner            = THIS_MODULE,
  108.     .open            = s3c64xx_timer_open,
  109.     .release        = s3c64xx_timer_close,
  110.     .unlocked_ioctl    = s3c64xx_timer_ioctl,
  111. };
  112. static struct miscdevice misc = {
  113.     .minor = MISC_DYNAMIC_MINOR,
  114.     .name = DEVICE_NAME,
  115.     .fops = &dev_fops,
  116. };
  117. static int __init dev_init(void)
  118. {
  119.     int ret;
  120.     init_MUTEX(&lock);
  121.     ret = misc_register(&misc);
  122.     printk (DEVICE_NAME"\tinitialized\n");
  123.         return ret;
  124. }
  125. static void __exit dev_exit(void)
  126. {
  127.     misc_deregister(&misc);
  128. }
  129. module_init(dev_init);
  130. module_exit(dev_exit);
  131. MODULE_LICENSE("GPL");
  132. MODULE_AUTHOR("FriendlyARM Inc.");
  133. MODULE_DESCRIPTION("S3C6410 Timer Driver liyongliu");

makefile与Kconfig都已修改
[ 此帖被odanobunaga在2011-09-19 12:38重新编辑 ]
级别: 侠客
UID: 54019
精华: 0
发帖: 76
金钱: 380 两
威望: 76 点
综合积分: 152 分
注册时间: 2011-08-22
最后登录: 2012-03-17
1楼  发表于: 2011-09-17 18:15
奥知道了 可能是因为互斥锁的原因
我发现设备是打开一次之后不能再次打开了
级别: 侠客
UID: 54019
精华: 0
发帖: 76
金钱: 380 两
威望: 76 点
综合积分: 152 分
注册时间: 2011-08-22
最后登录: 2012-03-17
2楼  发表于: 2011-09-17 18:36
啊 啊 啊啊还不对
我自己的timer驱动第一次就打不开啊 为啥啊为啥
级别: 侠客
UID: 54019
精华: 0
发帖: 76
金钱: 380 两
威望: 76 点
综合积分: 152 分
注册时间: 2011-08-22
最后登录: 2012-03-17
3楼  发表于: 2011-09-17 18:55
另外在编译linux内核时 遇到如下警告


WARNING: drivers/built-in.o(.data+0x1f0): Section mismatch in reference from the variable s3cfb_driver to the function .init.text:s3cfb_probe()
The variable s3cfb_driver references
the function __init s3cfb_probe()
If the reference is valid then annotate the
variable with __init* or __refdata (see linux/init.h) or name the variable:
*_template, *_timer, *_sht, *_ops, *_probe, *_probe_one, *_console,

WARNING: drivers/built-in.o(.data+0x168c8): Section mismatch in reference from the variable s3c_ts_driver to the function .init.text:s3c_ts_probe()
The variable s3c_ts_driver references
the function __init s3c_ts_probe()
If the reference is valid then annotate the
variable with __init* or __refdata (see linux/init.h) or name the variable:
*_template, *_timer, *_sht, *_ops, *_probe, *_probe_one, *_console,

级别: 侠客
UID: 54019
精华: 0
发帖: 76
金钱: 380 两
威望: 76 点
综合积分: 152 分
注册时间: 2011-08-22
最后登录: 2012-03-17
4楼  发表于: 2011-09-19 11:56
今天又用一个纯C的程序测试了一下
即友善的examples里面的test_pwm
改为静态编译 即flag增加-static即可运行
但是open自己定义的设备 /dev/mypwm时 程序无任何反应即退出了?
这是为何?