本篇目的:移植蜂鸣器驱动,并测试。
本篇参考:http://singleboy.blog.163.com/blog/static/54900194201152921847149/
14.1 蜂鸣器驱动源码添加
(1)添加源码drivers/misc/mini2440_pwm.c
root@ubuntu:~/linux-4.9.2# vim drivers/misc/mini2440_pwm.c
添加如下源码,本源码已经修改并适配于linux-4.9.2内核。
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/poll.h>
#include <linux/interrupt.h>
#include <linux/gpio.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <asm/uaccess.h>
#include <mach/regs-gpio.h>
#include <mach/hardware.h>
#include <mach/regs-irq.h>
#include <asm/mach/time.h>
#include <linux/clk.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/miscdevice.h>
#include <mach/gpio-samsung.h>
#include <mach/regs-gpio.h>
#include <plat/gpio-cfg.h>
#define DEVICE_NAME "pwm" /*设备名*/
#define PWM_IOCTL_SET_FREQ 1 /*定义宏变量,用于后面的ioctl 中的switchcase*/
#define PWM_IOCTL_STOP 0 /*定义信号量 lock*/
#define S3C_TIMERREG(x) (S3C_VA_TIMER + (x))
#define S3C_TIMERREG2(tmr,reg) S3C_TIMERREG((reg)+0x0c+((tmr)*0x0c))
#define S3C2410_TCFG0 S3C_TIMERREG(0x00)
#define S3C2410_TCFG1 S3C_TIMERREG(0x04)
#define S3C2410_TCON S3C_TIMERREG(0x08)
#define S3C2410_TCFG_PRESCALER0_MASK (255<<0)
#define S3C2410_TCFG_PRESCALER1_MASK (255<<8)
#define S3C2410_TCFG_PRESCALER1_SHIFT (8)
#define S3C2410_TCFG_DEADZONE_MASK (255<<16)
#define S3C2410_TCFG_DEADZONE_SHIFT (16)
#define S3C2410_TCFG1_MUX0_DIV2 (0<<0)
#define S3C2410_TCFG1_MUX0_DIV4 (1<<0)
#define S3C2410_TCFG1_MUX0_DIV8 (2<<0)
#define S3C2410_TCFG1_MUX0_DIV16 (3<<0)
#define S3C2410_TCFG1_MUX0_TCLK0 (4<<0)
#define S3C2410_TCFG1_MUX0_MASK (15<<0)
#define S3C2410_TCNTB(tmr) S3C_TIMERREG2(tmr, 0x00)
#define S3C2410_TCMPB(tmr) S3C_TIMERREG2(tmr, 0x04)
#define S3C2410_TCNTO(tmr) S3C_TIMERREG2(tmr, (((tmr) == 4) ? 0x04 : 0x08))
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 ) /*设置pwm 的频率,配置各个寄存器*/
{
unsigned long tcon;
unsigned long tcnt;
unsigned long tcfg1;
unsigned long tcfg0;
struct clk *clk_p;
unsigned long pclk;
/*set GPB0 as tout0,pwm output 设置GPB0 为tout0,pwm 输出*/
s3c_gpio_cfgpin(S3C2410_GPB(0),S3C2410_GPB0_TOUT0);
tcon =__raw_readl(S3C2410_TCON); /*读取寄存器TCON 到tcon*/
tcfg1 =__raw_readl(S3C2410_TCFG1); /*读取寄存器TCFG1 到tcfg1*/
tcfg0 =__raw_readl(S3C2410_TCFG0); /*读取寄存器TCFG0 到tcfg0*/
/*S3C2410_TCFG_PRESCALER0_MASK定时器0 和1 的预分频值的掩码,TCFG[0~8]*/
tcfg0 &=~S3C2410_TCFG_PRESCALER0_MASK;
tcfg0 |= (50 - 1); /* 预分频为50*/
tcfg1 &=~S3C2410_TCFG1_MUX0_MASK; /*S3C2410_TCFG1_MUX0_MASK 定时器0 分割值的掩码TCFG1[0~3]*/
tcfg1 |=S3C2410_TCFG1_MUX0_DIV16; /*定时器0 进行16 分割*/
__raw_writel(tcfg1,S3C2410_TCFG1); /*把tcfg1 的值写到分割寄存器S3C2410_TCFG1 中*/
__raw_writel(tcfg0,S3C2410_TCFG0); /*把tcfg0 的值写到预分频寄存器S3C2410_TCFG0 中*/
clk_p = clk_get(NULL,"timers"); /*得到pclk*/
pclk =clk_get_rate(clk_p);
tcnt =(pclk/50/16)/freq; /*得到定时器的输入时钟,进而设置PWM 的调制频率*/
__raw_writel(tcnt,S3C2410_TCNTB(0)); /*PWM 脉宽调制的频率等于定时器的输入时钟*/
__raw_writel(tcnt/2,S3C2410_TCMPB(0)); /*占空比是50%*/
tcon &= ~0x1f;
tcon |= 0xb; /*disabledeadzone, auto-reload, inv-off, update TCNTB0&TCMPB0, start timer 0*/
__raw_writel(tcon, S3C2410_TCON);/*把tcon 写到计数器控制寄存器S3C2410_TCON 中*/
tcon &= ~2;/*clear manual update bit*/
__raw_writel(tcon,S3C2410_TCON);
}
static void PWM_Stop(void)
{
s3c_gpio_cfgpin(S3C2410_GPB(0),S3C2410_GPIO_OUTPUT); /*设置GPB0 为输出*/
gpio_set_value(S3C2410_GPB(0),0); /*设置GPB0 为低电平,使蜂鸣器停止*/
}
static int s3c24xx_pwm_open(struct inode *inode, struct file *file)
{
if(!down_trylock(&lock)) /*是否获得信号量,是down_trylock(&lock)=0,否则非0*/
return 0;
else
return-EBUSY; /*返回错误信息:请求的资源不可用*/
}
static int s3c24xx_pwm_close(struct inode *inode, struct file *file)
{
PWM_Stop();
up(&lock); /*释放信号量lock*/
return 0;
}
/*cmd 是1,表示设置频率;cmd 是2 ,表示停止pwm*/
static long s3c24xx_pwm_ioctl(struct file *file, unsigned int cmd,unsigned long arg)
{
switch (cmd)
{
casePWM_IOCTL_SET_FREQ: /*if cmd=1 即进入case PWM_IOCTL_SET_FREQ*/
if (arg ==0) /*如果设置的频率参数是0*/
return-EINVAL; /*返回错误信息,表示向参数传递了无效的参数*/
PWM_Set_Freq(arg);/*否则设置频率*/
break;
case PWM_IOCTL_STOP:/* if cmd=2 即进入case PWM_IOCTL_STOP*/
PWM_Stop();/*停止蜂鸣器*/
break;
}
return 0; /*成功返回*/
}
/*初始化设备的文件操作的结构体*/
static struct file_operations dev_fops = {
.owner = THIS_MODULE,
.open =s3c24xx_pwm_open,
.release =s3c24xx_pwm_close,
.unlocked_ioctl =s3c24xx_pwm_ioctl,
};
static struct miscdevice misc = {
.minor =MISC_DYNAMIC_MINOR,
.name = DEVICE_NAME,
.fops = &dev_fops,
};
static int __init dev_init(void)
{
int ret;
sema_init(&lock,1);/*初始化一个互斥锁*/
ret =misc_register(&misc); /*注册一个misc 设备*/
if(ret < 0)
{
printk(DEVICE_NAME"register falid!\n");
return ret;
}
printk (DEVICE_NAME"\tinitialized!\n");
return 0;
}
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("S3C2410/S3C2440 PWM Driver");
保存并退出
(2)添加Kconfig选项
root@ubuntu:~/linux-4.9.2# vim drivers/misc/Kconfig
在110行,接着上回的button下面添加,红色部分
config MINI2440_BUTTONS
tristate "Buttonsdriver for FriendlyARM Mini2440 development boards"
depends on MACH_MINI2440
default y if MACH_MINI2440
help
this is buttons driver forFriendlyARM Mini2440 development boards
config MINI2440_BUZZER
tristate"Buzzer driver for FriendlyARM Mini2440 development boards"
depends onMACH_MINI2440
default yif MACH_MINI2440
help
this isbuzzer driver for FriendlyARM Mini2440 development boards
config DUMMY_IRQ
tristate "DummyIRQ handler"
(3)添加Makefile支持
root@ubuntu:~/linux-4.9.2# vim drivers/misc/Makefile
在38行添加红色部分
obj-$(CONFIG_MINI2440_ADC) += mini2440_adc.o
obj-$(CONFIG_LEDS_MINI2440) += mini2440_leds.o
obj-$(CONFIG_MINI2440_BUTTONS)+= mini2440_buttons.o
obj-$(CONFIG_MINI2440_BUZZER) += mini2440_pwm.o
obj-$(CONFIG_HMC6352) += hmc6352.o
obj-y += eeprom/
obj-y += cb710/
完成后,使用如下指令
root@ubuntu:~/linux-4.9.2# make menuconfig
查看一下
Device Drivers --->
[*] Misc devices --->
buzzer的配置,并保存退出
14.2 编译、测试
(1)编译
root@ubuntu:~/linux-4.9.2# make -j8
root@ubuntu:~/linux-4.9.2# ./mkuImage.sh
(2)测试
重启开发板,等LCD显示QT界面之后,在QT界面的“友善之臂”选项卡,打开“蜂鸣器”应用,按下“start”按钮,可以听到蜂鸣器发声,按+、-号,调节发声频率。