高手帮忙看看我这将原先的Buzzer程序该写了,增加了T1,T2,T3的PWM,程序编译没有错误。并且我把重要的一些信息都用printk()给打印出来了,寄存器显示结果分析没什么问题的,但是结果就是GPB0 有PWM ,GPB1 GPB2 GPB3都没有输出。程序源代码如下:
#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 <asm/irq.h>
#include <asm/io.h>
#include <linux/interrupt.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 PWM_IOCTL_SET_FREQ 1
#define PWM_IOCTL_STOP 2
static struct semaphore lock;
/*---------------------------------------------------------------------------------------
Timer 0 Freq=20KHz Duty=0~100%
* human ear : 20Hz~ 20KHz
------------------------------------------------------------------------------------------ */
static void PWM_Set(unsigned int duty)
{
unsigned long tcon;
unsigned long tcnt;
unsigned long tcfg1;
unsigned long tcfg0;
unsigned long tmp; //debug
struct clk *clk_p;
unsigned long pclk;
//get cpu clock
clk_p = clk_get(NULL, "pclk");
pclk = clk_get_rate(clk_p);
/*---------------------------------------------------------
printk() //Debug only
----------------------------------------------------------*/
/*---------------------------------------------------------
Timer0 pwm output
--------------------------------------------------------*/
tcon = __raw_readl(S3C2410_TCON);
printk("Tcon=%x\n",tcon); //Only Timer4 is running
tcfg1 = __raw_readl(S3C2410_TCFG1);
printk("Tcfg1=%x\n",tcfg1); //All mux=1/2
tcfg0 = __raw_readl(S3C2410_TCFG0);
printk("Tcfg0=%x\n",tcfg0); //prescaler0=None,prescaler1=2
//set GPB0 as tout0, pwm output mode
s3c2410_gpio_cfgpin(S3C2410_GPB0, S3C2410_GPB0_TOUT0);
//prescaler0= 50
tcfg0 &= ~S3C2410_TCFG_PRESCALER0_MASK;
tcfg0 |= (50 - 1);
//mux0 = 1/2
tcfg1 &= ~S3C2410_TCFG1_MUX0_MASK;
tcfg1 |= S3C2410_TCFG1_MUX0_DIV2;
__raw_writel(tcfg0, S3C2410_TCFG0);
__raw_writel(tcfg1, S3C2410_TCFG1);
tcnt = (pclk/50)/20000; //Freq=20KHz
__raw_writel(tcnt, S3C2410_TCNTB(0)); //write to Timer0 TCNTB
tmp= __raw_readl(S3C2410_TCNTB(0));
printk("S3C2410_TCNTB(0)=%x\n",tmp);
__raw_writel(duty*tcnt/100, S3C2410_TCMPB(0)); //write to Timer0 TCMPB
tmp= __raw_readl(S3C2410_TCMPB(0));
printk("S3C2410_TCMPB(0)=%x\n",tmp);
tcon &= ~0x1f;
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);
/*---------------------------------------------------------
Timer1 pwm output
--------------------------------------------------------*/
tcon = __raw_readl(S3C2410_TCON);
printk("Tcon=%x\n",tcon); //Only Timer4 is running
tcfg1 = __raw_readl(S3C2410_TCFG1);
printk("Tcfg1=%x\n",tcfg1); //All mux=1/2
tcfg0 = __raw_readl(S3C2410_TCFG0);
printk("Tcfg0=%x\n",tcfg0); //prescaler0=None,prescaler1=2
//set GPB1 as tout1, pwm output mode
s3c2410_gpio_cfgpin(S3C2410_GPB1, S3C2410_GPB1_TOUT1);
//prescaler0= 50
tcfg0 &= ~S3C2410_TCFG_PRESCALER0_MASK;
tcfg0 |= (50 - 1);
//mux0 = 1/2
tcfg1 &= ~S3C2410_TCFG1_MUX0_MASK;
tcfg1 |= S3C2410_TCFG1_MUX0_DIV2;
__raw_writel(tcfg0, S3C2410_TCFG0);
__raw_writel(tcfg1, S3C2410_TCFG1);
tcnt = pclk/50/20000; //Freq=20KHz
__raw_writel(tcnt, S3C2410_TCNTB(1)); //write to Timer0 TCNTB
tmp= __raw_readl(S3C2410_TCNTB(1));
printk("S3C2410_TCNTB(1)=%x\n",tmp);
__raw_writel(duty*tcnt/100, S3C2410_TCMPB(1)); //write to Timer0 TCMPB
tmp= __raw_readl(S3C2410_TCMPB(1));
printk("S3C2410_TCMPB(1)=%x\n",tmp);
tcon &= ~0x1f;
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);
/*---------------------------------------------------------
Timer2 pwm output
--------------------------------------------------------*/
tcon = __raw_readl(S3C2410_TCON);
printk("Tcon=%x\n",tcon); //500009
tcfg1 = __raw_readl(S3C2410_TCFG1);
printk("Tcfg1=%x\n",tcfg1);
tcfg0 = __raw_readl(S3C2410_TCFG0);
printk("Tcfg0=%x\n",tcfg0);
//Set GPB2 as TOUT2 ,pwm output mode
s3c2410_gpio_cfgpin(S3C2410_GPB2, S3C2410_GPB2_TOUT2);
//prescaler1= 50
// tcfg0 &= ~S3C2410_TCFG_PRESCALER1_MASK;
// tcfg0 |= (50 - 1)<<8;
//mux2 = 1/2
tcfg1 &= ~S3C2410_TCFG1_MUX2_MASK;
tcfg1 |= S3C2410_TCFG1_MUX2_DIV2;
// __raw_writel(tcfg0, S3C2410_TCFG0);
__raw_writel(tcfg1, S3C2410_TCFG1);
//Freq & duty Set
tcnt = (pclk/50)/2000; //Freq=20KHz
__raw_writel(tcnt, S3C2410_TCNTB(2)); //write to Timer0 TCNTB
tmp= __raw_readl(S3C2410_TCNTB(2));
printk("S3C2410_TCNTB(2)=%x\n",tmp);
__raw_writel(duty*tcnt/100, S3C2410_TCMPB(2)); //write to Timer0 TCMPB
tmp= __raw_readl(S3C2410_TCMPB(2));
printk("S3C2410_TCMPB(2)=%x\n",tmp);
//auto-reload ,update TCNTB2&TCMPB2,start timer2
tcon &= 0xFFBFFF;
tcon |= 0x9<<12;
__raw_writel(tcon, S3C2410_TCON);
tcon = __raw_readl(S3C2410_TCON);
printk("Tcon=%x\n",tcon);
/*---------------------------------------------------------
Timer3 pwm output
--------------------------------------------------------*/
tcon = __raw_readl(S3C2410_TCON);
printk("Tcon=%x\n",tcon); //500009
tcfg1 = __raw_readl(S3C2410_TCFG1);
printk("Tcfg1=%x\n",tcfg1);
tcfg0 = __raw_readl(S3C2410_TCFG0);
printk("Tcfg0=%x\n",tcfg0);
//Set GPB3 as TOUT3 ,pwm output mode
s3c2410_gpio_cfgpin(S3C2410_GPB3, S3C2410_GPB3_TOUT3);
//prescaler1= 50
// tcfg0 &= ~S3C2410_TCFG_PRESCALER1_MASK;
// tcfg0 |= (50 - 1)<<8;
//mux2 = 1/16
tcfg1 &= ~S3C2410_TCFG1_MUX3_MASK;
tcfg1 |= S3C2410_TCFG1_MUX3_DIV16;
// __raw_writel(tcfg0, S3C2410_TCFG0);
__raw_writel(tcfg1, S3C2410_TCFG1);
//Freq & duty Set
tcnt = (pclk/50)/2000; //Freq=20KHz
__raw_writel(tcnt, S3C2410_TCNTB(3)); //write to Timer0 TCNTB
tmp= __raw_readl(S3C2410_TCNTB(3));
printk("S3C2410_TCNTB(3)=%x\n",tmp);
__raw_writel(duty*tcnt/100, S3C2410_TCMPB(3)); //write to Timer0 TCMPB
tmp= __raw_readl(S3C2410_TCMPB(3));
printk("S3C2410_TCMPB(3)=%x\n",tmp);
//auto-reload ,update TCNTB3&TCMPB3,start timer3
tcon &= 0xFFBFFF;
tcon |= 0x9<<12;
__raw_writel(tcon, S3C2410_TCON);
tcon = __raw_readl(S3C2410_TCON);
printk("Tcon=%x\n",tcon);
}
/*
void PWM_init(void)
{
//TOUT0 OUTPUT
s3c2410_gpio_cfgpin(S3C2410_GPB0, S3C2410_GPB0_OUTP);
s3c2410_gpio_setpin(S3C2410_GPB0, 1);
//TOUT1 OUTPUT
s3c2410_gpio_cfgpin(S3C2410_GPB1, S3C2410_GPB1_OUTP);
s3c2410_gpio_setpin(S3C2410_GPB1, 1);
//TOUT2 OUTPUT
s3c2410_gpio_cfgpin(S3C2410_GPB2, S3C2410_GPB2_OUTP);
s3c2410_gpio_setpin(S3C2410_GPB2, 1);
//TOUT3 OUTPUT
s3c2410_gpio_cfgpin(S3C2410_GPB3, S3C2410_GPB3_OUTP);
s3c2410_gpio_setpin(S3C2410_GPB3, 1);
}
*/
void PWM_Stop( void )
{
//TOUT0 OUTPUT
s3c2410_gpio_cfgpin(S3C2410_GPB0, S3C2410_GPB0_OUTP);
s3c2410_gpio_setpin(S3C2410_GPB0, 0);
//TOUT1 OUTPUT
s3c2410_gpio_cfgpin(S3C2410_GPB1, S3C2410_GPB1_OUTP);
s3c2410_gpio_setpin(S3C2410_GPB1, 0);
//TOUT2 OUTPUT
s3c2410_gpio_cfgpin(S3C2410_GPB2, S3C2410_GPB2_OUTP);
s3c2410_gpio_setpin(S3C2410_GPB2, 0);
//TOUT3 OUTPUT
s3c2410_gpio_cfgpin(S3C2410_GPB3, S3C2410_GPB3_OUTP);
s3c2410_gpio_setpin(S3C2410_GPB3, 0);
}
static int s3c24xx_pwm_open(struct inode *inode, struct file *file)
{
if (!down_trylock(&lock))
return 0;
else
return -EBUSY;
}
static int s3c24xx_pwm_close(struct inode *inode, struct file *file)
{
up(&lock);
return 0;
}
static int s3c24xx_pwm_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned int duty_tmp)
{
switch (cmd) {
case PWM_IOCTL_SET_FREQ:
PWM_Set(duty_tmp);
break;
case PWM_IOCTL_STOP:
PWM_Stop();
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"\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("FriendlyARM Inc.");
MODULE_DESCRIPTION("S3C2410/S3C2440 PWM Driver");