1。把驱动编译成模块
考备TX2440-beep.c放到drivers/char目录下,修改Kconfig文件,加入
config TX2440_BEEP
tristate "TX2440 BEEP Driver"
depends on ARCH_S3C2440
help
this is BEEP Driver for TX2440 development boards
修改Makefile文件加入
obj-$(CONFIG_TX2440_BEEP) +=TX2440_beep.o
配置内核,支持驱动模块
Device Drivers -->
Character devices -->
[M] TX2440 BEEP Driver
执行#make M=driver/char/ modules
编译完后,会在目录drivers/char下生成TX2440_Beep.ko文件,将其自制到根文件系统下lib/modules/2/6/31目录下
2,向内核中添加驱动,
#insmod lib/modules/2.6.31/TX2440_beep.ko
#rmmod TX2440_beep
TX2440_beep.c
/*************************************
PWM的驱动,在TX2440A上做测试
linux内核:2.6.31
*************************************/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define DEVICE_NAME "TX2440-Beep"
static int BEEP_MAJOR=0; /* 主设备号 */
static int TX2440_beep_ioctl(
struct inode *inode,
struct file *file,
unsigned int cmd,
unsigned long arg)
{
unsigned long temp;
if(cmd <= 0)
{
temp = __raw_readl(S3C2410_GPBCON); //GPBCON
temp &= ~3;
temp |= 1;
__raw_writel(temp, S3C2410_GPBCON);
temp = __raw_readl(S3C2410_GPBDAT); //GPBDAT
temp &= ~1;
__raw_writel(temp, S3C2410_GPBDAT);
}
else
{
temp = __raw_readl(S3C2410_GPBCON); //GPBCON
temp &= ~3;
temp |= 2;
__raw_writel(temp, S3C2410_GPBCON);
temp = __raw_readl(S3C2410_TCFG0); //TCFG0
temp &= ~0xff;
temp |= 15;
__raw_writel(temp, S3C2410_TCFG0);
temp = __raw_readl(S3C2410_TCFG1); //TCFG1
temp &= ~0xf;
temp |= 2;
__raw_writel(temp, S3C2410_TCFG1);
temp = (50000000/128)/cmd;
__raw_writel(temp, S3C2410_TCNTB(0));
temp >>= 1;
__raw_writel(temp, S3C2410_TCMPB(0));
temp = __raw_readl(S3C2410_TCON); //TCON
temp &= ~0x1f;
temp |= 0xb;
__raw_writel(temp, S3C2410_TCON);
temp &= ~2;
__raw_writel(temp, S3C2410_TCON);
}
return 0;
}
static struct file_operations TX2440_beep_fops = {
.owner = THIS_MODULE,
.ioctl = TX2440_beep_ioctl,
};
static struct class *beep_class;
static int __init TX2440_beep_init(void)
{
printk("TX2440 BEEP DRIVER MODULE INIT\n");
BEEP_MAJOR = register_chrdev(0, DEVICE_NAME, &TX2440_beep_fops);
if (BEEP_MAJOR < 0) {
printk(DEVICE_NAME " can't register major number\n");
return BEEP_MAJOR;
}
printk("register TX2440-Beep Driver OK! Major = %d\n", BEEP_MAJOR);
beep_class = class_create(THIS_MODULE, DEVICE_NAME);
if(IS_ERR(beep_class))
{
printk("Err: failed in TX2440-Beep class. \n");
return -1;
}
device_create(beep_class, NULL, MKDEV(BEEP_MAJOR, 0), NULL, DEVICE_NAME);
printk(DEVICE_NAME " initialized\n");
return 0;
}
static void __exit TX2440_beep_exit(void)
{
unregister_chrdev(BEEP_MAJOR, DEVICE_NAME);
device_destroy(beep_class, MKDEV(BEEP_MAJOR, 0));
class_destroy(beep_class);
}
module_init(TX2440_beep_init);
module_exit(TX2440_beep_exit);
MODULE_AUTHOR("
laohan");
MODULE_DESCRIPTION("TX2440 Beep Driver");
MODULE_LICENSE("GPL");
编写测试程序beep.c
/*************************************
PWM驱动测试程序,在TX2440A开发板做测试
驱动用法:
设备名称:TX2440-Beep
测试程序名称:beep
参数为蜂鸣器的频率
*************************************/
#include
#include
#include
#include
#include
#include
int main(int argc, char **argv)
{
int fd;
unsigned long temp =0;
int i;
fd = open("/dev/TX2440-Beep", O_RDWR);
if (fd < 0)
{
perror("open device TX2440-BEEP");
exit(1);
}
printf("Please enter the times number (0 is stop) :\n");
while(1)
{
scanf("%d",&temp);
printf("times = %d \n",temp);
ioctl(fd, temp, 4);
if(temp == 0)
{
printf("Stop Control Beep!\n");
break;
}
}
close(fd);
return 0;
}
arm-linux-gcc beep.c -o beep
把程序下载到开发板上,加上权限chmod 777 beep
./beep
阅读(1544) | 评论(1) | 转发(0) |