OK6410 PWM波形输出代码

  1. #include <</SPAN>linux/module.h>  
  2. #include <</SPAN>linux/kernel.h>  
  3. #include <</SPAN>linux/fs.h>  
  4. #include <</SPAN>linux/init.h>  
  5. #include <</SPAN>linux/delay.h>  
  6. #include <</SPAN>linux/poll.h>  
  7. #include <</SPAN>asm/irq.h>  
  8. #include <</SPAN>asm/io.h>  
  9. #include <</SPAN>linux/interrupt.h>  
  10. #include <</SPAN>asm/uaccess.h>  
  11. #include <</SPAN>mach/hardware.h>  
  12. #include <</SPAN>plat/regs-timer.h>  
  13. #include <</SPAN>mach/regs-irq.h>  
  14. #include <</SPAN>asm/mach/time.h>  
  15. #include <</SPAN>linux/clk.h>  
  16. #include <</SPAN>linux/cdev.h>  
  17. #include <</SPAN>linux/device.h>  
  18. #include <</SPAN>linux/miscdevice.h>  
  19.   
  20. #include <</SPAN>mach/map.h>  
  21. #include <</SPAN>mach/regs-clock.h>  
  22. #include <</SPAN>mach/regs-gpio.h>  
  23.   
  24. #include <</SPAN>plat/gpio-cfg.h>  
  25. #include <</SPAN>mach/gpio-bank-e.h>  
  26. #include <</SPAN>mach/gpio-bank-f.h>  
  27. #include <</SPAN>mach/gpio-bank-k.h>  
  28.   
  29. #define DEVICE_NAME     "pwm"  
  30.   
  31. #define PWM_IOCTL_SET_FREQ       
  32. #define PWM_IOCTL_STOP           
  33.   
  34. static struct semaphore lock; //信号量  
  35.   
  36.   
  37. static void PWM_Set_Freq( unsigned long freq  
  38.  
  39.     unsigned long tcon;  
  40.     unsigned long tcnt;  
  41.     unsigned long tcfg1;  
  42.     unsigned long tcfg0;  
  43.   
  44.     struct clk *clk_p;  
  45.     unsigned long pclk;  
  46.   
  47.     unsigned tmp;  
  48.   
  49.     tmp readl(S3C64XX_GPFCON);    
  50.     //tmp &= ~(0x3U <</SPAN><</SPAN> 28);  
  51.     //tmp |=  (0x2U <</SPAN><</SPAN> 28);  
  52.   
  53.          tmp &=~(0x3U <</SPAN><</SPAN> 30);  //GPF15  [31:30]    10 PWM TOUT[1]   
  54.          tmp |=  (0x2U <</SPAN><</SPAN> 30);  
  55.   
  56.     writel(tmp, S3C64XX_GPFCON);  
  57.   
  58.     tcon __raw_readl(S3C_TCON);  //定时器控制寄存器  
  59.     tcfg1 __raw_readl(S3C_TCFG1); //时钟多路复用器和 DMA模式的选择  
  60.     tcfg0 __raw_readl(S3C_TCFG0); //时钟预定标器和死区结构  
  61.   
  62.     //prescaler 50   Prescaler [7:0] R/W Prescaler value for timer   
  63.     tcfg0 &= ~S3C_TCFG_PRESCALER0_MASK;  //{prescaler value} 1~255    
  64.     tcfg0 |= (50 1);  //prescaler value=50  
  65.   
  66.     //mux 1/16  TCFG1  Divider MUX0 [3:0] R/W Select Mux input for PWM Timer   
  67.     tcfg1 &= ~S3C_TCFG1_MUX0_MASK;  
  68.     tcfg1 |= S3C_TCFG1_MUX0_DIV16; // 0100: 1/16    
  69.   
  70.     __raw_writel(tcfg1, S3C_TCFG1);  
  71.     __raw_writel(tcfg0, S3C_TCFG0);  
  72.   
  73.   
  74.   
  75.   
  76.    
  77.     clk_p clk_get(NULL, "pclk");  //获取一个名为id的时针  
  78.     pclk  clk_get_rate(clk_p);  //获取该时钟的频率  
  79.     //定时器输入时钟频率   
  80.     //Timer input clock Frequency PCLK {prescaler value 1} {divider value}   
  81.     tcnt  (pclk/50/16)/freq;    
  82.   
  83.     __raw_writel(tcnt, S3C_TCNTB(0)); //TCNTB0:定时器0计数缓冲器。   
  84.     __raw_writel(tcnt/2, S3C_TCMPB(0));//TCMPB0:定时器0比较缓冲寄存器。   
  85.                   
  86.     tcon &= ~0x1f;  
  87.     tcon |= 0xb;        //disable deadzone, auto-reload, inv-off, update TCNTB0&TCMPB0, start timer  
  88.     __raw_writel(tcon, S3C_TCON);  
  89.       
  90.   
  91.       
  92.     tcon &= ~2;         //clear manual update bit  
  93.     __raw_writel(tcon, S3C_TCON);  
  94.  
  95.   
  96. void PWM_Stop( void  
  97.  
  98.     unsigned tmp;  
  99.     tmp readl(S3C64XX_GPFCON);  
  100.     //tmp &= ~(0x3U <</SPAN><</SPAN> 28);  
  101.         tmp &= ~(0x3U <</SPAN><</SPAN> 30);  
  102.   
  103.     writel(tmp, S3C64XX_GPFCON);  
  104.  
  105.   
  106.   
  107. static int s3c64xx_pwm_open(struct inode *inode, struct file *file)  
  108.  
  109.     if (!down_trylock(&lock))  
  110.         return 0;  
  111.     else  
  112.         return -EBUSY;  
  113.  
  114.   
  115.   
  116. static int s3c64xx_pwm_close(struct inode *inode, struct file *file)  
  117.  
  118.     up(&lock);  //该函数释放信号量sem,唤醒等待者  
  119.     return 0;  
  120.  
  121.   
  122.   
  123. static long s3c64xx_pwm_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)  
  124.  
  125.     switch (cmd)  
  126.         case PWM_IOCTL_SET_FREQ:  
  127.             if (arg == 0)  
  128.                 return -EINVAL;  
  129.             PWM_Set_Freq(arg);  
  130.             break;  
  131.   
  132.         case PWM_IOCTL_STOP:  
  133.         default:  
  134.             PWM_Stop();  
  135.             break;  
  136.      
  137.   
  138.     return 0;  
  139.  
  140.   
  141.   
  142. static struct file_operations dev_fops  
  143.     .owner          THIS_MODULE 
  144.     .open           s3c64xx_pwm_open 
  145.     .release        s3c64xx_pwm_close  
  146.     .unlocked_ioctl s3c64xx_pwm_ioctl 
  147. };  
  148.   
  149. static struct miscdevice misc  
  150.     .minor MISC_DYNAMIC_MINOR 
  151.     .name DEVICE_NAME 
  152.     .fops &dev_fops,  
  153. };  
  154.   
  155. static int __init dev_init(void)  
  156.  
  157.     int ret;  
  158.   
  159.   
  160.     init_MUTEX(&lock);  
  161.     ret misc_register(&misc);  
  162.   
  163.     printk (DEVICE_NAME"\tinitialized\n");  
  164.         return ret;  
  165.  
  166.   
  167. static void __exit dev_exit(void)  
  168.  
  169.     misc_deregister(&misc);  
  170.  
  171.   
  172. module_init(dev_init);  
  173. module_exit(dev_exit);  
  174. MODULE_LICENSE("GPL");  
  175. MODULE_AUTHOR("FORLINX Inc.");  
  176. MODULE_DESCRIPTION("S3C6410 PWM Driver");  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值