首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

基于 2.6 内核的 pwm 蜂鸣器驱动设计(2)

基于 2.6 内核的 pwm 蜂鸣器驱动设计(2)

  2. 开发板上蜂鸣器原理图分析
  由原理图可以得知, 蜂鸣器是通过 GPB0 IO 口使用 PWM 信号驱动工作的, 而 GPB0 口是一个复用的 IO 口,要使用它得先把他设置成 TOUT0 PWM 输出 模式。 3. 编写合适开发板的蜂鸣器驱动程序,文件名:my2440_pwm.c ================================================ Name Author Date Copyright : my2440_pwm.c : Huang Gang : 25/11/09 : GPL
  Description : my2440 pwm driver ================================================ */ #include#include#include#include
  #include#include#include#include #include #include #include#define PWM_MAJOR 0 //主设备号 //设备名称
  #define PWM_NAME "my2440_pwm"
  static int device_major = PWM_MAJOR; //系统动态生成的主设备号 //打开设备 static int pwm_open(struct inode *inode, struct file *file) { //对 GPB0 复用口进行复用功能设置,设置为 TOUT0 PWM 输出 s3c2410_gpio_cfgpin(S3C2410_GPB0, S3C2410_GPB0_TOUT0); return 0; } //关闭设备 static int pwm_close(struct inode *inode, struct file *file) { return 0; } //对设备进行控制 static int pwm_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
  {if(cmd <= 0)//如果输入的参数小于或等于 0 的话,就让蜂鸣器停止工作 { //这里又恢复 GPB0 口为 IO 口输出功能, 由原理图可知直接给低电平 可让蜂鸣器停止工作 s3c2410_gpio_cfgpin(S3C2410_GPB0, S3C2410_GPB0_OUTP); s3c2410_gpio_setpin(S3C2410_GPB0, 0); } else//如果输入的参数大于 0,就让蜂鸣器开始工作,不同的参数,蜂鸣 器的频率也不一样 { //定义一些局部变量 unsigned long tcon; unsigned long tcnt; unsigned long tcfg1; unsigned long tcfg0; struct clk *clk_p; unsigned long pclk; //以下对各寄存器的操作结合上面讲的开始一个 PWM 定时器的步骤 和 2440 手册 PWM 寄存器操作部分来看就比较容易理解 tcfg1 = __raw_readl(S3C2410_TCFG1); 1 的值 tcfg0 = __raw_readl(S3C2410_TCFG0); 0 的值 tcfg0 &= ~S3C2410_TCFG_PRESCALER0_MASK; tcfg0 |= (50 - 1); //设置 tcfg0 的值为 49 //读取定时器配置寄存器 //读取定时器配置寄存器
  tcfg1 &= ~S3C2410_TCFG1_MUX0_MASK;
  tcfg1 |= S3C2410_TCFG1_MUX0_DIV16; 0x0011 即:1/16 __raw_writel(tcfg1, S3C2410_TCFG1); 置寄存器 1 中 __raw_writel(tcfg0, S3C2410_TCFG0); 置寄存器 0 中 clk_p = clk_get(NULL, "pclk"); pclk = clk_get_rate(clk_p); 钟频率,在 include/linux/clk.h 中定义 tcnt = (pclk/50/16)/cmd; (pclk/{prescaler0 + 1}/divider value)
  //设置 tcfg1 的值为
  //将值 tcfg1 写入定时器配
  //将值 tcfg0 写入定时器配
  //从系统平台时钟队列中获取 pclk 的时
  //计算定时器 0 的输出时钟频率
  __raw_writel(tcnt, S3C2410_TCNTB(0)); 寄存器的值
  //设置定时器 0 计数缓存
返回列表