设为首页 加入收藏

TOP

mini6410 PWM驱动程序
2014-11-24 12:51:46 来源: 作者: 【 】 浏览:0
Tags:mini6410 PWM 驱动程序

mini6410 PWM驱动程序:


#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_SET_FREQ 1
#define PWM_IOCTL_STOP 0


static struct semaphore lock;


/* freq: pclk/50/16/65536 ~ pclk/50/16
* if pclk = 50MHz, freq is 1Hz to 62500Hz
* human ear : 20Hz~ 20000Hz
*/
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;


unsigned tmp;


tmp = readl(S3C64XX_GPFCON);
tmp &= ~(0x3U << 28);
tmp |= (0x2U << 28);
writel(tmp, S3C64XX_GPFCON);


tcon = __raw_readl(S3C_TCON);
tcfg1 = __raw_readl(S3C_TCFG1);
tcfg0 = __raw_readl(S3C_TCFG0);


//prescaler = 50
tcfg0 &= ~S3C_TCFG_PRESCALER0_MASK;
tcfg0 |= (50 - 1);


//mux = 1/16
tcfg1 &= ~S3C_TCFG1_MUX0_MASK;
tcfg1 |= S3C_TCFG1_MUX0_DIV16;


__raw_writel(tcfg1, S3C_TCFG1);
__raw_writel(tcfg0, S3C_TCFG0);


clk_p = clk_get(NULL, "pclk");
pclk = clk_get_rate(clk_p);
tcnt = (pclk/50/16)/freq;

__raw_writel(tcnt, S3C_TCNTB(0));
__raw_writel(tcnt/2, S3C_TCMPB(0));

tcon &= ~0x1f;
tcon |= 0xb; //disable deadzone, auto-reload, inv-off, update TCNTB0&TCMPB0, start timer 0
__raw_writel(tcon, S3C_TCON);

tcon &= ~2; //clear manual update bit
__raw_writel(tcon, S3C_TCON);
}


void PWM_Stop( void )
{
unsigned tmp;
tmp = readl(S3C64XX_GPFCON);
tmp &= ~(0x3U << 28);
writel(tmp, S3C64XX_GPFCON);
}


static int s3c64xx_pwm_open(struct inode *inode, struct file *file)
{
if (!down_trylock(&lock))
return 0;
else
return -EBUSY;
}



static int s3c64xx_pwm_close(struct inode *inode, struct file *file)
{
up(&lock);
return 0;
}



static int s3c64xx_pwm_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
switch (cmd) {
case PWM_IOCTL_SET_FREQ:
if (arg == 0)
return -EINVAL;
PWM_Set_Freq(arg);
break;


case PWM_IOCTL_STOP:
default:
PWM_Stop();
break;
}


return 0;
}



static struct file_operations dev_fops = {
.owner = THIS_MODULE,
.open = s3c64xx_pwm_open,
.release = s3c64xx_pwm_close,
.ioctl = s3c64xx_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("S3C6410 PWM Driver");


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇解决Ubuntu编译内核uImage出现问.. 下一篇Android教程:开机自启动C程序

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·C语言中如何将结构体 (2025-12-24 22:20:09)
·纯C语言结构体成员变 (2025-12-24 22:20:06)
·C语言中,指针函数和 (2025-12-24 22:20:03)
·哈希表 - 菜鸟教程 (2025-12-24 20:18:55)
·MySQL存储引擎InnoDB (2025-12-24 20:18:53)