使用混杂设备,移植网上别人的代码,修改头文件,修改编译错误,测试ok
pwm 驱动代码#includepwm 测试代码#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 "pwm" #define PWM_IOCTL_START 2 //设置pwm的频率 #define PWM_IOCTL_SET_FREQ 1 //设置pwm的频率 #define PWM_IOCTL_STOP 0 //停止pwm //定义信号量,此处的信号量是一个互斥信号量,用于PWM设备之多只能被一个进程打开 static struct semaphore lock; static void PWM_Set_Freq( unsigned long freq ) { unsigned long tcon; unsigned long tcnt; unsigned long tcfg1; unsigned long tcfg0; struct clk *clk_p; unsigned long pclk; //set GPB0 as tout0, pwm output s3c2410_gpio_cfgpin(S3C2410_GPB0, S3C2410_GPB0_TOUT0);//设置GPB0为tout0,pwm输出 tcon = __raw_readl(S3C2410_TCON); //读取寄存器TCON到tcon tcfg1 = __raw_readl(S3C2410_TCFG1); //读取寄存器TCFG1到tcfg1 tcfg0 = __raw_readl(S3C2410_TCFG0); //读取寄存器TCFG0到tcfg0 //prescaler = 50 //清除prescaler 0 [7:0]位 tcfg0 &= ~S3C2410_TCFG_PRESCALER0_MASK; // S3C2410_TCFG_PRESCALER0_MASK定时器0和1的预分频值的掩码,TCFG[0~8] //设置prescaler 0 [7:0]位 tcfg0 |= (50 - 1); // 预分频为50HZ //mux = 1/16 详细请看数据手册 tcfg1 &= ~S3C2410_TCFG1_MUX0_MASK; //S3C2410_TCFG1_MUX0_MASK定时器0分割值的掩码:TCFG1[0~3] tcfg1 |= S3C2410_TCFG1_MUX0_DIV16; //定时器0进行16分割 __raw_writel(tcfg1, S3C2410_TCFG1); //把新配置tcfg1的值写到分割寄存器S3C2410_TCFG1中 __raw_writel(tcfg0, S3C2410_TCFG0); //把tcfg0的值写到预分频寄存器S3C2410_TCFG0中 clk_p = clk_get(NULL, "pclk"); //得到pclk pclk = clk_get_rate(clk_p); tcnt = (pclk/50/16)/freq; //得到定时器的输入时钟,进而设置PWM的调制频率 __raw_writel(tcnt, S3C2410_TCNTB(0)); //PWM脉宽调制的频率等于定时器的输入时钟 __raw_writel(tcnt/2, S3C2410_TCMPB(0)); //占空比是50% ,自己认为是高电平时间 //配置定时器 tcon &= ~0x1f;//清空前4位 tcon |= 0xb; //disable deadzone, auto-reload, inv-off, update TCNTB0&TCMPB0, start timer 0 __raw_writel(tcon, S3C2410_TCON); tcon &= ~2; //clear manual update bit __raw_writel(tcon, S3C2410_TCON); //把tcon写到计数器控制寄存器S3C2410_TCON中 } static void PWM_Stop(void) { s3c2410_gpio_cfgpin(S3C2410_GPB0, S3C2410_GPIO_OUTPUT); //设置GPB0为输出 s3c2410_gpio_setpin(S3C2410_GPB0, 0); //设置GPB0为低电平,使蜂鸣器停止 } static void PWM_Start(void) { s3c2410_gpio_cfgpin(S3C2410_GPB0, S3C2410_GPIO_OUTPUT); //设置GPB0为输出 s3c2410_gpio_setpin(S3C2410_GPB0, 1); //设置GPB0为低电平,使蜂鸣器停止 } static int s3c24xx_pwm_open(struct inode *inode, struct file *file) { printk("open pwm beep n"); if (!down_trylock(&lock))//判断是否设备已经打开,已打开返回EBUSY return 0; //是否获得信号量,是down_trylock(&lock)=0,否则非0 else return -EBUSY; } static int s3c24xx_pwm_close(struct inode *inode, struct file *file) { printk("close pwm beep n"); PWM_Stop();//停止PWM up(&lock);//释放信号量 return 0; } static int s3c24xx_pwm_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg) { //printk("ioctl pwm: %x %lxn", cmd, arg); switch (cmd) { case PWM_IOCTL_SET_FREQ: if (arg == 0){ return -EINVAL; } PWM_Set_Freq(arg);//设置频率 printk("driver set pwm freq :%d n", arg); break; case PWM_IOCTL_STOP: PWM_Stop(); printk("driver set pwm stop n"); break; case PWM_IOCTL_START: PWM_Start(); printk("driver set pwm start n"); break; default : printk("default cmd n"); break; } return 0; } static struct file_operations dev_fops = { .owner = THIS_MODULE, .open = s3c24xx_pwm_open, .release = s3c24xx_pwm_close, .ioctl = s3c24xx_pwm_ioctl, }; static struct miscdevice misc = { .minor = MISC_DYNAMIC_MINOR, .name = DEVICE_NAME, .fops = &dev_fops, }; static int __init dev_init(void) { int ret; init_MUTEX(&lock); //初始化一个互斥锁 ret = misc_register(&misc); printk (DEVICE_NAME"tinitializedn"); return ret; } static void __exit dev_exit(void) { misc_deregister(&misc); } module_init(dev_init); module_exit(dev_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("FriendlyARM Inc."); MODULE_DEscriptION("S3C2410/S3C2440 PWM Driver");
#include#include #include #include #include #include int main(int argc, char **argv) { int fd; int value = 50; fd = open("/dev/pwm", O_RDWR); if (fd < 0) { printf("can't open /dev/devn"); return -1; } else { printf("open /dev/pwm success n"); } if (0 == memcmp(argv[1], "pwm_freq", 8)) { value = atoi(argv[2]); printf("freq:%sn", argv[2]); if (ioctl(fd, 1, value) == 0) { printf("set freq success n"); } else { printf("set freq fail n"); } } else if (0 == memcmp(argv[1], "pwm_stop", 8)) { if (ioctl(fd, 0, value) == 0) { printf("set stop success n"); } else { printf("set stop fail n"); } } else if (0 == memcmp(argv[1], "pwm_start", 9)) { if (ioctl(fd, 2, value) == 0) { printf("set start success n"); } else { printf("set start fail n"); } } else { printf("wrong cmd n"); } sleep(5); // while(1); close(fd); return 0; }



