
设置好交叉编译工具链,内核是前面移植的2.6.32版本,在原来移植修改后的基础上进入下面操作。
GEC2440开发板将S 3C2440A的TOUT1端口(定时器1的脉冲输出端口,GPB1)与蜂鸣器的脉冲输入端口相连,蜂鸣器的接口电路图如下:使TOUT1输出300-3400Hz的时钟信号来驱动蜂鸣器。由原理图可以得知,蜂鸣器是通过GPB1 IO口使用PWM信号驱动工作的,而GPB1口是一个复用的IO口,要使用它得先把他设置成TOUT1 PWM输出模式。
1、 创建drivers/char/gec2440_pwm.c文件,把编写好的PWM驱动程序源码复制到该文件中,文件内容如下:
#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 <plat/regs-timer.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>
#define DEVICE_NAME "gec2440_pwm"
#define PWM_IOCTL_SET_FREQ 1
#define PWM_IOCTL_STOP 0
static struct semaphore lock;
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;
//set GPB1 as tout1, pwm output
s3c2410_gpio_cfgpin(S3C2410_GPB(1), S3C2410_GPB1_TOUT1);
tcon = __raw_readl(S3C2410_TCON);
tcfg1 = __raw_readl(S3C2410_TCFG1);
tcfg0 = __raw_readl(S3C2410_TCFG0);
//prescaler = 16
tcfg0 &= ~S3C2410_TCFG_PRESCALER0_MASK;
tcfg0 |= 15; //(16 - 1)
//mux = 1/8
tcfg1 &= ~S3C2410_TCFG1_MUX0_MASK;
tcfg1 |= S3C2410_TCFG1_MUX0_DIV8;
__raw_writel(tcfg1, S3C2410_TCFG1);
__raw_writel(tcfg0, S3C2410_TCFG0);
clk_p = clk_get(NULL, "pclk");
pclk = clk_get_rate(clk_p);
tcnt = (pclk>>7)/freq;
__raw_writel(tcnt, S3C2410_TCNTB(1));
__raw_writel(tcnt/2, S3C2410_TCMPB(1));
tcon &= ~(0xf<<8);
tcon |= (0xb<<8); //disable deadzone, auto-reload, inv-off, update TCNTB1&TCMPB1, start timer 1
__raw_writel(tcon, S3C2410_TCON);
tcon &= ~(1<<9); //clear manual update bit
__raw_writel(tcon, S3C2410_TCON);
}
static void PWM_Stop(void)
{ s3c2410_gpio_cfgpin(S3C2410_GPB(1), S3C2410_GPIO_OUTPUT); s3c2410_gpio_setpin(S3C2410_GPB(1), 0); } static int s3c24xx_pwm_open(struct inode *inode, struct file *file) { if (!down_trylock(&lock)) return 0; else return -EBUSY; } static int s3c24xx_pwm_close(struct inode *inode, struct file *file) { PWM_Stop(); up(&lock); return 0; } static int s3c24xx_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: PWM_Stop(); break; } return 0; } static struct file_operations dev_fops = { .owner = THIS_MODULE, .open = s3c24xx_pwm_open, .release = s3c24xx_pwm_close, .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; 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("GCX_GEC"); MODULE_DESCRIPTION("S3C2410/S3C2440 PWM Driver"); 2、把PWM蜂鸣器的驱动代码部署到内核中
修改drivers/char/Kconfig文件,添加如下内容:
config GEC2440_PWM_BEEP
tristate "GEC2440 PWM Beep Device"
depends on ARCH_S3C2440
default y
help
GEC2440 PWM Beep
修改drivers/char/Makefile文件,添加如下内容:
obj-$(CONFIG_GEC2440_PWM_BEEP) += gec2440_pwm.o
3、重新配置内核
make menuconfig
在内核配置菜单中添加如下选项:
Device Drivers --->
Character devices --->
<*> GEC2440 PWM Beep Device (NEW)
4、编译内核并下载到开发板上。这里要注意,现在不需要手动创建设备的节点了,因为使用了mdev进行管理了,在驱动程序中也添加了对类设备接口的支持。现在可以查看到/dev目录下自动创建好的gec2440_pwm设备节点,就直接可以使用它了。
5、编写PWM蜂鸣器驱动测试程序。文件名为pwm_test.c,文件内容如下:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/ioctl.h>
int main(int argc, char **argv)
{
int tmp;
int fd;
int i;
//打开蜂鸣器设备
fd = open("/dev/gec2440_pwm", O_RDWR);
if(fd < 0)
{
printf("Open PWM Device Faild!\n");
exit(1);
}
//提示用户输入一个参数用来对蜂鸣器进行调频,0表示停止工作
printf("please enter the times number(0 is stop):\n");
while(1)
{
//输入参数
scanf("%d", &tmp);
printf("times = %d\n", tmp);
//IO控制
if(tmp <= 0)
{
ioctl(fd,0);
break;
}
else
{
ioctl(fd, 1,tmp);
}
}
//关闭设备
close(fd);
return 0;
}
6、在开发主机上交叉编译测试程序,并把生成的可执行文件下载到板上运行
交叉编译方法:arm-linux-gcc -o pwm_test pwm_test.c
运行时结果如下截图:
可以看到根据输入参数的大小,蜂鸣器会发出不同频率的叫声,输入0蜂鸣器停止鸣叫。

