资源描述:
《ARM蜂鸣器驱动》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、在linux-2.6.32.2/drivers/char/目录下,增加一个驱动程序文件mini2440_pwm.c,内容如下:#include#include#include#include#include#include#include#include#inc
2、lude#include#include#include#include#include#include#include#include#include#include
3、#include#defineDEVICE_NAME"pwm"//设备名#definePWM_IOCTL_SET_FREQ1//定义宏变量,用于后面的ioctl中的switchcase#definePWM_IOCTL_STOP0//定义信号量lockstaticstructsemaphorelock;/*freq:pclk/50/16/65536~pclk/50/16*ifpclk=50MHz,freqis1Hzto62500Hz*humanear:20Hz
4、~20000Hz*/staticvoidPWM_Set_Freq(unsignedlongfreq)//设置pwm的频率,配置各个寄存器{unsignedlongtcon;unsignedlongtcnt;unsignedlongtcfg1;unsignedlongtcfg0;structclk*clk_p;unsignedlongpclk;//setGPB0astout0,pwmoutput设置GPB0为tout0,pwm输出s3c2410_gpio_cfgpin(S3C2410_GPB(0),
5、S3C2410_GPB0_TOUT0);tcon=__raw_readl(S3C2410_TCON);//读取寄存器TCON到tcontcfg1=__raw_readl(S3C2410_TCFG1);//读取寄存器TCFG1到tcfg1tcfg0=__raw_readl(S3C2410_TCFG0);//读取寄存器TCFG0到tcfg0//prescaler=50tcfg0&=~S3C2410_TCFG_PRESCALER0_MASK;//S3C2410_TCFG_PRESCALER0_MASK定
6、时器0和1的预分频值的掩码,TCFG[0~8]tcfg0
7、=(50-1);//预分频为50//mux=1/16tcfg1&=~S3C2410_TCFG1_MUX0_MASK;//S3C2410_TCFG1_MUX0_MASK定时器0分割值的掩码:TCFG1[0~3]tcfg1
8、=S3C2410_TCFG1_MUX0_DIV16;//定时器0进行16分割__raw_writel(tcfg1,S3C2410_TCFG1);//把tcfg1的值写到分割寄存器S3C2410_TCFG1中__raw_wri
9、tel(tcfg0,S3C2410_TCFG0);//把tcfg0的值写到预分频寄存器S3C2410_TCFG0中clk_p=clk_get(NULL,"pclk");//得到pclkpclk=clk_get_rate(clk_p);tcnt=(pclk/50/16)/freq;//得到定时器的输入时钟,进而设置PWM的调制频率__raw_writel(tcnt,S3C2410_TCNTB(0));//PWM脉宽调制的频率等于定时器的输入时钟__raw_writel(tcnt/2,S3C2410_
10、TCMPB(0));//占空比是50%tcon&=~0x1f;tcon
11、=0xb;//disabledeadzone,auto-reload,inv-off,updateTCNTB0&TCMPB0,starttimer0__raw_writel(tcon,S3C2410_TCON);//把tcon写到计数器控制寄存器S3C2410_TCON中tcon&=~2;//clearmanualupdatebit__raw_writel(tcon,S3C2410_TCON);}staticv