嵌入式linux的pwm例程,谁来救救我的PWM程序啊!!

#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");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值