码迷,mamicode.com
首页 > 其他好文 > 详细

混杂设备驱动--输出两路PWM

时间:2015-05-18 22:59:12      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:

尝试用2440的TOUT0和TOUT1输出PWM驱动两个电机,电机的硬件驱动电路是使用L298N。
先单独测试TOUT0的PWM输出:
(1)驱动程序:使用misc混杂设备驱动模型,当然也可以使用基本的字符设备模型。
使用misc设备驱动模型步骤:
①初始化一个struct miscdevice结构体:主要是file_operation结构体成员和name
②使用misc_register和misc_deregister注册和注销这个结构体
代码示例:

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/gpio.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <asm/uaccess.h>
#include <mach/regs-gpio.h>
#include <mach/hardware.h>
#include <plat/regs-timer.h>
#include <mach/regs-irq.h>
#include <linux/clk.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/miscdevice.h>

#define DEVICE_NAME "pwm" //设备名

#define IOCTL_SET_PWM_DUTY 	1 
#define IOCTL_PWM_STOP 		2

/* 定义信号量 lock用于互斥,因此,改驱动程序只能同时有一个进程使用 */
static struct semaphore lock;

static void pwm_set_duty(unsigned long duty)
{
    unsigned long tcon;
    unsigned long tcnt0,tcmp0;
    unsigned long tcfg1;
    unsigned long tcfg0;
    
	tcfg0 = __raw_readl(S3C2410_TCFG0);
    tcfg0 &= ~S3C2410_TCFG_PRESCALER0_MASK;
    tcfg0 |= (50 - 1); 
    
    tcfg1 = __raw_readl(S3C2410_TCFG1);
    tcfg1 &= ~S3C2410_TCFG1_MUX0_MASK; 
    tcfg1 |= S3C2410_TCFG1_MUX0_DIV16;
    
    __raw_writel(tcfg0, S3C2410_TCFG0);
    __raw_writel(tcfg1, S3C2410_TCFG1); 
    
    tcnt0 = 1000;//设置PWM频率
    tcmp0 = duty;//设置PWM占空比
    __raw_writel(tcnt0, S3C2410_TCNTB(0));
    __raw_writel(tcmp0, S3C2410_TCMPB(0));
     
	tcon = __raw_readl(S3C2410_TCON);
    tcon |= 0xf; //设置输出电平反转,start pwm output
    __raw_writel(tcon, S3C2410_TCON); 
    tcon &= ~2;   /* clear manual update bit */
    __raw_writel(tcon, S3C2410_TCON);
}

static void pwm_stop(void)
{
	unsigned long tcon;
	tcon = __raw_readl(S3C2410_TCON);
    tcon &= ~(1<<3);//Turn off the auto reload bit
    __raw_writel(tcon, S3C2410_TCON); // 
}

static int my_pwm_open(struct inode *inode, struct file *file)
{
    if (!down_trylock(&lock))//不会导致进程休眠
    {	
    	s3c2410_gpio_cfgpin(S3C2410_GPB0, S3C2410_GPB0_TOUT0);
        return 0;
    }
    else
        return -EBUSY; 
}

static int my_pwm_close(struct inode *inode, struct file *file)
{
    pwm_stop();
    s3c2410_gpio_cfgpin(S3C2410_GPB0, S3C2410_GPIO_INPUT);
    up(&lock);
    return 0;
}

static int my_pwm_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
    switch (cmd) {
        case IOCTL_SET_PWM_DUTY: 
            if (arg == 0) 
                return -EINVAL;
            pwm_set_duty(arg);
            break;
        case IOCTL_PWM_STOP:
            pwm_stop();
            break;
        default:
        	printk(KERN_INFO"cmd error!\n");
        	break;
    }
    return 0;
}

static struct file_operations dev_fops = {
    .owner = THIS_MODULE,
    .open = my_pwm_open,
    .release = my_pwm_close,
    .ioctl = my_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"\tinitialized\n");
	return ret;
}

static void __exit dev_exit(void)
{
    misc_deregister(&misc); 
}

module_init(dev_init);
module_exit(dev_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("CLBIAO");
MODULE_DESCRIPTION("S3C2440 Pwm Driver");
(2)测试app程序:
#include <stdlib.h>
#include <stdio.h>  
#include <sys/types.h>  
#include <sys/stat.h>  
#include <fcntl.h>  
#include <unistd.h>
#include <string.h> 

#define IOCTL_SET_PWM_DUTY 	1 
#define IOCTL_PWM_STOP 		2

int main(int argc, char **argv)
{
	int fd,i;
	unsigned long duty;
	fd = open("/dev/pwm",O_RDWR);
	
	for(i=0;i<4;i++)
	{
    	ioctl(fd,IOCTL_SET_PWM_DUTY,duty);//duty=80%
    	duty += 100; 
    	sleep(3);
	}
	close(fd);
	exit(0);
}
实验现象是:
用外用表电压档测gpb0引脚会出现从大到小的变化,说明pwm实现了占空比可调,万用表测到的是引脚的平均电压。
对tout1的单独测试也是一样。
当我把两个单独可以工作的驱动代码合并到一起的时候就出现了奇怪的问题:只要我先设置其中一个timer输出之后另外一个就输出高电平,之后对他进行的配置工作不起效,相当于不受程序控制。
分析问题:在后面才配置的timer在进行操作寄存器之前是否对他有另外的操作?有,将gpb1配置为定时器1输出。
测试方案①:只设置了timer0寄存器,没有配置timer1的,同时gpb0和gpb1设置为定时器输出,使用printk函数打印寄存器的值
测试记录:
tout0实际电平输出:
0.27,0.82,1.67,->0.06
tout1实际电平输出:
3.31,3.31,3.31,->0.47
串口打印:
before set:tcfg0=512,tcfg1=0,tcon=5242894 - 10100000000000000001110 -> timer0/1未开始输出
 after set:tcfg0=561,tcfg1=3,tcon=5242893 - 10100000000000000001101 -> timer0开始输出,timer1未开始输出
before set:tcfg0=561,tcfg1=3,tcon=5242893
 after set:tcfg0=561,tcfg1=3,tcon=5242893
before set:tcfg0=561,tcfg1=3,tcon=5242893
 after set:tcfg0=561,tcfg1=3,tcon=5242893
数据分析:
(512)->1000000000 => bit8-bit0:00000000 -> 00
(561)->1000110001 => bit8-bit0:00110001 -> 49
初步结论:
timer0处于正常工作状态,从读取的寄存器来看timer1并没有开始工作,之所以会在timer0工作时输出高电平猜测是在调用open函数时:执行完s3c2410_gpio_cfgpin(S3C2410_GPB1, S3C2410_GPB1_TOUT1);之后timer1的输出引脚本身就是高电平,之所以会这样的原因进一步猜测是程序中只对timer0进行配置:关键是Timer 0 output inverter on,而此时从读取的timer1来看 Timer 1 output inverter off 的,芯片手册上说关闭反转功能时引脚的初始状态是表现为高电平的。之所以timer0最后会输出0,而不是1,是因为在设置占空比过程中打开了位反转功能和最后引脚配置为INPUT,timer1最后也输出0同样是因为引脚配置为INPUT。
进一步验证:
测试方案②:在配置gpb0/1为timer输出时就进行设置位反转功能
测试数据:
tout0实际电平输出:
0.27,0.82,1.67,->0.06
tout1实际电平输出:
0.06,0.06,0.06,->0.62
串口打印:
before set:tcfg0=512,tcfg1=0,tcon=5243918 - 101000000000100 00001110
 after set:tcfg0=561,tcfg1=3,tcon=5243917 - 101000000000100 00001101
before set:tcfg0=561,tcfg1=3,tcon=5243917
 after set:tcfg0=561,tcfg1=3,tcon=5243917
before set:tcfg0=561,tcfg1=3,tcon=5243917
 after set:tcfg0=561,tcfg1=3,tcon=5243917
和上面的猜测完全吻合。
附上自己对s3c2440英文pdf手册第319页的翻译--即关于输出电平反转功能和输出电平状态的那部分:
技术分享
The following procedure describes how to maintain TOUT as high or low (assume the inverter is off):
1. Turn off the auto reload bit. And then, TOUTn goes to high level and the timer is stopped after the TCNTn reaches
0 (recommended).
2. Stop the timer by clearing the timer start/stop bit to 0. 
If TCNTn<=TCMPn, the output level is high. 
If TCNTn>TCMPn, the output level is low.
3. The TOUTn can be inverted by the inverter on/off bit in TCON. The inverter removes the additional circuit to
adjust the output level.

下面的步骤是描述如何维持TOUT为高电平或低电平(假设电平反转功能关闭):
1.关掉自动重新加载位,然后TOUTn引脚变为高电平,当TCNTn自减到0时,定时器停止工作
2.通过清除定时器的start/stop位来停止定时器
①如果清除停止位时:TCNTn<=TCMPn,那么输出维持为高电平
②如果清除停止位时:TCNTn>TCMPn,那么输出维持为低电平
3.TOUTn引脚的电平反转可以通过TCON寄存器的“inverter on/off”位来进行设置,反转器移除附加的回路来调整输出电平
小结:
也就是说,如果我需要在停止pwm之后引脚的电平保持为确定的电平,就只能考虑第一点,通过关掉自动重新加载位来
停止pwm,然后再来清除start/stop位
---------------------------------------------------------------------------------------------------------------------------------------------------------
之后引脚连接到电机驱动电路之后又遇到类似调不了速的奇葩问题,得到一个结论就是tcon寄存器里的控制各种功能的位不要一次性写进去,有些位写完之后还要稍加延时才能实现对两路PWM占空比的独立控制。
最终可以使用的驱动代码:2440_pwm.c
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/poll.h>
#include <linux/interrupt.h>
#include <linux/gpio.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <asm/uaccess.h>
#include <mach/regs-gpio.h>
#include <mach/hardware.h>
#include <plat/regs-timer.h>
#include <mach/regs-irq.h>
#include <asm/mach/time.h>
#include <linux/clk.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/miscdevice.h>

#define DEVICE_NAME "pwm"

#define IOCTL_CONFIG_PWM0 	1
#define IOCTL_CONFIG_PWM1 	2
#define IOCTL_PWM0_STOP		3
#define IOCTL_PWM1_STOP 	4
	
#define TIMER0				5
#define TIMER1				6
#define AUTO_RELOAD_ON 		7
#define AUTO_RELOAD_OFF 	8
#define INVER_ON			9
#define INVER_OFF		   (10)

static struct semaphore lock;

static void set_input_clock(void)
{
	unsigned int tcfg0,tcfg1;
	tcfg0 = __raw_readl(S3C2410_TCFG0);
	tcfg1 = __raw_readl(S3C2410_TCFG1);
	tcfg0 &= ~S3C2410_TCFG_PRESCALER0_MASK;
    tcfg0 |= (50- 1); 
    tcfg1 &= ~S3C2410_TCFG1_MUX0_MASK; 
    tcfg1 |= S3C2410_TCFG1_MUX0_DIV16;
    __raw_writel(tcfg0, S3C2410_TCFG0);
    __raw_writel(tcfg1, S3C2410_TCFG1);
}

static void set_pwm_duty(int select,unsigned int duty)
{
	unsigned int tcon;
    tcon = __raw_readl(S3C2410_TCON);
	switch(select)
	{
		case TIMER0:
		    __raw_writel(1000, S3C2410_TCNTB(0));
		    __raw_writel(duty, S3C2410_TCMPB(0));
	    	break;
	    
    	case TIMER1:
		    __raw_writel(1000, S3C2410_TCNTB(1));
		    __raw_writel(duty, S3C2410_TCMPB(1));
	    	break;
	}
}
static void auto_reload_on_off(int select,int status)
{
	unsigned int tcon;
	tcon = __raw_readl(S3C2410_TCON);
	switch(select)
	{
		case TIMER0:
			if(status==7)
			{
			    tcon |= (1<<3);
		    	__raw_writel(tcon,S3C2410_TCON);
		    	mdelay(1);
		    }
		    else if(status==8)
		    {
		    	tcon &= ~(1<<3);
		    	__raw_writel(tcon,S3C2410_TCON);
		    	mdelay(1);
		    }
		    break;
		    
		case TIMER1:
		    if(status==7)
			{
			    tcon |= (1<<11);
		    	__raw_writel(tcon,S3C2410_TCON);
		    	mdelay(1);
		    }
		    else if(status==8)
		    {
		    	tcon &= ~(1<<11);
		    	__raw_writel(tcon,S3C2410_TCON);
		    	mdelay(1);
		    }
	    	break;
    }
}
static void start_pwm_out(int select)
{
	unsigned int tcon;
	tcon = __raw_readl(S3C2410_TCON);
	switch(select)
	{
		case TIMER0:
			
		    tcon |= 1;
		    __raw_writel(tcon, S3C2410_TCON);
		    break;
		    
		case TIMER1:
			
		    tcon |= (1<<8);
		    __raw_writel(tcon, S3C2410_TCON);
	    	break;
	}
}
static void stop_pwm_out(int select)
{
	unsigned int tcon;
	tcon = __raw_readl(S3C2410_TCON);
	switch(select)
	{
		case TIMER0:
			auto_reload_on_off(TIMER0,AUTO_RELOAD_OFF);
			mdelay(5);
		    tcon &= ~(1<<0);
	    	__raw_writel(tcon,S3C2410_TCON);
		    break;
		    
		case TIMER1:
			auto_reload_on_off(TIMER1,AUTO_RELOAD_OFF);
		    mdelay(1);
		    tcon &= ~(1<<8);
		    __raw_writel(tcon,S3C2410_TCON);
	    	break;
    }
}

static void manual_update_tcont_tcmp(int select)
{
	unsigned int tcon;
    tcon = __raw_readl(S3C2410_TCON);
	switch(select)
	{
		case TIMER0:
		    tcon |= (1<<1);//Update TCNTB0 & TCMPB0
		    __raw_writel(tcon, S3C2410_TCON);
	    	break;
	    
    	case TIMER1:
		    tcon |= (1<<9);//Update TCNTB1 & TCMPB1
		    __raw_writel(tcon, S3C2410_TCON);
	    	break;
	}
}
static void clear_manual_bit(int select)
{
	unsigned int tcon;
	tcon = __raw_readl(S3C2410_TCON);
	switch(select)
	{
		case TIMER0:
		    tcon &= ~(1<<1);
		    __raw_writel(tcon, S3C2410_TCON);
		    break;
		    
		case TIMER1:
		    tcon &= ~(1<<9);
		    __raw_writel(tcon, S3C2410_TCON);
	    break;
	}
}
static void output_inverte_on_off(int status)
{
	unsigned long tcon;
	switch(status)
	{
		case INVER_ON:
			tcon = __raw_readl(S3C2410_TCON);
		    tcon |= ((1<<2)|(1<<10));
		    __raw_writel(tcon, S3C2410_TCON);
		    break;
		case INVER_OFF:
			tcon = __raw_readl(S3C2410_TCON);
		    tcon &= ~((1<<2)|(1<<10));
		    __raw_writel(tcon, S3C2410_TCON);
		    break;
	}
}
static void print_tcon(int status)
{
	unsigned int tcon;
	tcon = __raw_readl(S3C2410_TCON);
	switch(status)
	{
		case 0:
			printk(KERN_INFO"beford set tcon:tcon=%d\n",tcon);
			break;
		case 1:
			printk(KERN_INFO" after set tcon:tcon=%d\n",tcon);
			break;
	}
}

static int s3c2440_pwm_open(struct inode *inode, struct file *file)
{
    if (!down_trylock(&lock))
    {	
    	s3c2410_gpio_cfgpin(S3C2410_GPB0, S3C2410_GPB0_TOUT0);//靠外边的一个TOUT0
    	s3c2410_gpio_cfgpin(S3C2410_GPB1, S3C2410_GPB1_TOUT1);//
    	s3c2410_gpio_setpin(S3C2410_GPB6,1);

		output_inverte_on_off(INVER_ON);
		stop_pwm_out(TIMER0);
		stop_pwm_out(TIMER1);
		set_input_clock();
		
        return 0;
    }
    else
    {
    	printk(KERN_INFO"/dev/pwm opened!\n");
        return -EBUSY; 
    }
}

static int s3c2440_pwm_close(struct inode *inode, struct file *file)
{
	auto_reload_on_off(TIMER0,AUTO_RELOAD_OFF);
    auto_reload_on_off(TIMER1,AUTO_RELOAD_OFF);
	stop_pwm_out(TIMER0);
	stop_pwm_out(TIMER1);
	output_inverte_on_off(INVER_OFF);
    s3c2410_gpio_cfgpin(S3C2410_GPB0, S3C2410_GPIO_INPUT);
    s3c2410_gpio_cfgpin(S3C2410_GPB1, S3C2410_GPIO_INPUT);
    s3c2410_gpio_setpin(S3C2410_GPB6,0);
    up(&lock);
    return 0;
}
static void config_pwm0(unsigned long duty)
{
	stop_pwm_out(TIMER0);
	set_pwm_duty(TIMER0,duty);
	manual_update_tcont_tcmp(TIMER0);
	auto_reload_on_off(TIMER0,AUTO_RELOAD_ON);
	start_pwm_out(TIMER0);
	clear_manual_bit(TIMER0);
}

static void config_pwm1(unsigned long duty)
{
	stop_pwm_out(TIMER1);
	set_pwm_duty(TIMER1,duty);
	manual_update_tcont_tcmp(TIMER1);
	auto_reload_on_off(TIMER1,AUTO_RELOAD_ON);
	start_pwm_out(TIMER1);
	clear_manual_bit(TIMER1);
}
static int s3c2440_pwm_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
    switch (cmd) {
        case IOCTL_CONFIG_PWM0:
        	printk(KERN_INFO"config timer0:\n");
        	print_tcon(0);
            config_pwm0(arg);
            print_tcon(1);
            break;
        case IOCTL_CONFIG_PWM1:
        	printk(KERN_INFO"config timer1:\n");
        	print_tcon(0);
            config_pwm1(arg);
            print_tcon(1);
            break;
        case IOCTL_PWM0_STOP:
        	printk(KERN_INFO"stop timer0:\n");
        	print_tcon(0);
            stop_pwm_out(TIMER0);
            print_tcon(1);
            break;
        case IOCTL_PWM1_STOP:
        	printk(KERN_INFO"stop timer1:\n");
        	print_tcon(0);
            stop_pwm_out(TIMER1);
            print_tcon(1);
            break;
        default:
        	printk(KERN_INFO"ioctl cmd error!\n");
        	break;
    }
    return 0;
}

static struct file_operations dev_fops = {
    .owner   = THIS_MODULE,
    .open    = s3c2440_pwm_open,
    .release = s3c2440_pwm_close,
    .ioctl   = s3c2440_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"\tinitialized\n");
	return ret;
}

static void __exit dev_exit(void)
{
    misc_deregister(&misc); 
}

module_init(dev_init);
module_exit(dev_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("CLBIAO");
MODULE_DESCRIPTION("S3C2440 Pwm Driver");
测试APP:
#include <stdlib.h>
#include <stdio.h>  
#include <sys/types.h>  
#include <sys/stat.h>  
#include <fcntl.h>  
#include <unistd.h>
#include <string.h> 

#define IOCTL_CONFIG_PWM0 	1
#define IOCTL_CONFIG_PWM1 	2
#define IOCTL_PWM0_STOP		3
#define IOCTL_PWM1_STOP 	4

int main(int argc, char **argv)
{
	int fd;

	fd = open("/dev/pwm",O_RDWR);
	sleep(12);
	ioctl(fd,IOCTL_CONFIG_PWM0,900);
	ioctl(fd,IOCTL_CONFIG_PWM1,100);
	sleep(6);
	ioctl(fd,IOCTL_CONFIG_PWM0,700);
	ioctl(fd,IOCTL_CONFIG_PWM1,300);
	sleep(6);
	ioctl(fd,IOCTL_PWM0_STOP);
	ioctl(fd,IOCTL_PWM1_STOP);
	sleep(6);
	ioctl(fd,IOCTL_CONFIG_PWM0,500);
	ioctl(fd,IOCTL_CONFIG_PWM1,500);
	sleep(6);
	close(fd);
	exit(0);
}
感想:s3c2440芯片手册上并没有说到两路timer同时输出时应该注意哪些方面,我想这就是这次调试过程中遇到最蛋疼的问题了,只能靠自己通过实验现象一点一点去摸索,醉了醉了.............




混杂设备驱动--输出两路PWM

标签:

原文地址:http://blog.csdn.net/clb1609158506/article/details/45829389

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!