哪位高手来救救小弟我的PWM程序啊
谁来救救我的PWM程序啊!!
各位****D的大侠们。小弟初学嵌入式。用s3c2410的定时器0产生PWM信号。但是当我结束应用程序后,驱动产生的PWM波还在。这是怎么回事啊?望各位大侠不吝赐教。
我的驱动程序如下:
应用程序如下:
各位****D的大侠们。小弟初学嵌入式。用s3c2410的定时器0产生PWM信号。但是当我结束应用程序后,驱动产生的PWM波还在。这是怎么回事啊?望各位大侠不吝赐教。
我的驱动程序如下:
- C/C++ code
#include<linux/kernel.h> #include<linux/module.h> #include<linux/init.h> #include<linux/fs.h> #include<linux/types.h> #include<linux/poll.h> #include<linux/version.h> #include<linux/errno.h> #include <asm/io.h> #include <linux/clk.h> #include<asm-arm/arch-s3c2410/regs-gpio.h> #include <asm-arm/plat-s3c/regs-timer.h> #define pwm_device_MAJOR 50 #define DEVICE_NAME "pwm_device" #define FREQ 50 #define PWM_CLOSE 0 #define PWM_OPEN 1 static int pwm_device_close(struct inode *inode,struct file *file); static int pwm_device_open(struct inode *inode,struct file *file); static int pwm_device_ioctl(struct inode *inode,struct file *file,unsigned int cmd,unsigned long arg); static int __init pwm_device_init(void); static void __exit pwm_device_cleanup(void); static struct file_operations pwm_device_fops= { owner: THIS_MODULE, open: pwm_device_open, release: pwm_device_close, ioctl: pwm_device_ioctl, }; static void set_pwm_duty(unsigned long duty) { unsigned long tcon; unsigned long tcnt; unsigned long tcmp; unsigned long tcfg0; unsigned long tcfg1; struct clk *clk_p; unsigned long pclk; s3c2410_gpio_cfgpin(S3C2410_GPB0,S3C2410_GPB0_TOUT0); tcfg0 = __raw_readl(S3C2410_TCFG0); tcfg1 = __raw_readl(S3C2410_TCFG1); tcfg0 &= ~S3C2410_TCFG_PRESCALER0_MASK; tcfg0 |= (256 - 1); tcfg1 &= ~S3C2410_TCFG1_MUX0_MASK; tcfg1 |= S3C2410_TCFG1_MUX0_DIV16; __raw_writel(tcfg0, S3C2410_TCFG0); __raw_writel(tcfg1, S3C2410_TCFG1); clk_p = clk_get(NULL, "pclk"); pclk = clk_get_rate(clk_p); tcnt = (pclk/256/16)/FREQ; tcmp = (tcnt*duty)/100; __raw_writel(tcnt, S3C2410_TCNTB(0)); __raw_writel(tcmp, S3C2410_TCMPB(0)); tcon = __raw_readl(S3C2410_TCON); tcon &= ~0x1f; tcon |= 0xb; __raw_writel(tcon, S3C2410_TCON); tcon &= ~2; __raw_writel(tcon, S3C2410_TCON); } static int __init pwm_device_init(void) { int result; result=register_chrdev(pwm_device_MAJOR,DEVICE_NAME,&pwm_device_fops); if (result<0) { printk(KERN_INFO "[FAILED:Cannot register pwm_device Device!]\n"); return -EIO; } else printk("Initializing pwm_device Device\t----->\t"); printk("[OK]\n"); return 0; } static int pwm_device_open(struct inode *inode,struct file *file) { printk(KERN_CRIT "DEMO:pwm_device Device Open!\n"); return 0; } static int pwm_device_ioctl(struct inode *inode,struct file *file,unsigned int cmd,unsigned long arg) { switch(cmd) { case PWM_CLOSE: break; case PWM_OPEN: if (arg == 0) return -EINVAL; set_pwm_duty(arg); break; default: break; } return 0; } static int pwm_device_close(struct inode *inode,struct file *file) { printk(KERN_CRIT "DEMO:pwm_device Device close!\n"); return 0; } static void __exit pwm_device_cleanup(void) { unregister_chrdev(pwm_device_MAJOR,DEVICE_NAME); printk("DEMO:pwm_device Device Is Cleanup!\n"); } module_init(pwm_device_init); modele_exit(pwm_device_cleanup); MODULE_LICENSE("GPL");
应用程序如下:
- C/C++ code
#include<stdlib.h> #include<sys/ioctl.h> #include<sys/stat.h> #include<fcntl.h> #include<unistd.h> #include<stdio.h> #define DEVICE "/dev/pwm_device" int main() { int fd; int on_off; int duty; fd=open(DEVICE,O_RDWR); if(fd<0) { printf("open device error!\n"); exit(-1); } printf("请输入控制信号.0为关闭PWM输出,1为打开PWM输出功能!\n"); scanf("%d",&on_off); printf("您的输入是:%d\n",on_off); printf("请输入小于100的占空比值!\n"); scanf("%d",&duty); printf("您的输入是:%d\n",duty); ioctl(fd,on_off,duty); while(1); close(fd); return 0; }