linux驱动arm蜂鸣器响,ARM11 硬件 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 "misc_pwm"

#define command_set_freq 0

#define command_stop 1

static struct semaphore flag;

static void PWM_set_freq(unsigned long freq)

{

unsigned long tcon;

unsigned long tcnt;

unsigned long tcfg0;

unsigned long tcfg1;

struct clk *clk_pointer;

unsigned long pclk;

s3c_gpio_cfgpin(S3C64XX_GPF(14),S3C64XX_GPF14_PWM_TOUT0);

tcon = __raw_readl(S3C_TCON);

tcfg0 = __raw_readl(S3C_TCFG0);

tcfg1 = __raw_readl(S3C_TCFG1);

tcfg0 &= ~(S3C_TCFG_PRESCALER0_MASK);

tcfg0 |= 1;

__raw_writel(tcfg0,S3C_TCFG0);

tcfg1 &= ~(S3C_TCFG1_MUX0_MASK);

tcfg1 |= S3C_TCFG1_MUX0_DIV1;

__raw_writel(tcfg1,S3C_TCFG1);

clk_pointer = clk_get(NULL,"pclk");

pclk = clk_get_rate(clk_pointer);

tcnt = (pclk/1/1)/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)

{

s3c_gpio_cfgpin(S3C64XX_GPF(14),S3C_GPIO_INPUT);

__raw_writel(0x00,S3C_TCON);

}

static int PWM_open(struct inode *inode, struct file *filp)

{

if(!down_trylock(&flag))

{

return 0;

}

else

{

return -EBUSY;

}

}

static int PWM_release(struct inode *inode,struct file *filp)

{

up(&flag);

return 0;

}

static long PWM_unlocked_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)

{

switch(cmd)

{

case command_set_freq:

PWM_set_freq(arg);

break;

case command_stop:

PWM_Stop();

break;

default:

PWM_Stop();

break;

}

return 0;

}

static struct file_operations PWM_fops = {

.owner = THIS_MODULE,

.open = PWM_open,

.release = PWM_release,

.unlocked_ioctl = PWM_unlocked_ioctl,

};

static struct miscdevice miscpwm = {

.minor = MISC_DYNAMIC_MINOR,

.name = device_name,

.fops = &PWM_fops,

};

int pwm_up_init(void)

{

int ret;

sema_init(&flag,1);

ret = misc_register(&miscpwm);

if(ret < 0)

{

printk("the pwm has unmodule \n");

}

printk("the pwm has registered \n");

return 0;

}

void pwm_exit(void)

{

misc_deregister(&miscpwm);

printk("the pwm has exit\n");

}

module_init(pwm_up_init);

module_exit(pwm_exit);

MODULE_LICENSE("GPL");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值