#include
#include
#include
#include
#include
#include
#include
#include
#define
DEVICE_NAME "leds" #define
LED_MAJOR 231
#define
IOCTL_LED_ON 0
#define IOCTL_LED_OFF 1
static unsigned long led_table [] = {
S3C2410_GPB5,
S3C2410_GPB6,
S3C2410_GPB7,
S3C2410_GPB8,
};
static unsigned int led_cfg_table [] = {
S3C2410_GPB5_OUTP,
S3C2410_GPB6_OUTP,
S3C2410_GPB7_OUTP,
S3C2410_GPB8_OUTP,
};
static int s3c24xx_leds_open(struct inode *inode, struct file
*file)
{
int i;
for (i = 0;
i < 4; i++) {
// 设置GPIO引脚的功能:本驱动中LED所涉及的GPIO引脚设为输出功能
s3c2410_gpio_cfgpin(led_table[i], led_cfg_table[i]);
}
return
0;
}
static int s3c24xx_leds_ioctl(
struct inode
*inode,
struct file
*file,
unsigned int
cmd,
unsigned
long arg)
{
if (arg >
4) {
return -EINVAL;
}
switch(cmd)
{
case
IOCTL_LED_ON:
// 设置指定引脚的输出电平为0
s3c2410_gpio_setpin(led_table[arg], 0);
return 0;
case
IOCTL_LED_OFF:
// 设置指定引脚的输出电平为1
s3c2410_gpio_setpin(led_table[arg], 1);
return 0;
default:
return -EINVAL;
}
}
static struct file_operations s3c24xx_leds_fops = {
.owner = THIS_MODULE, .open = s3c24xx_leds_open, .ioctl = s3c24xx_leds_ioctl,
};
static int __init s3c24xx_leds_init(void)
{
int ret;
ret =
register_chrdev(LED_MAJOR, DEVICE_NAME,
&s3c24xx_leds_fops);
if (ret <
0) {
printk(DEVICE_NAME " can't register major number\n");
return ret;
}
printk(DEVICE_NAME " initialized\n");
return
0;
}
static void __exit s3c24xx_leds_exit(void)
{
unregister_chrdev(LED_MAJOR, DEVICE_NAME);
}
module_init(s3c24xx_leds_init);
module_exit(s3c24xx_leds_exit);
MODULE_AUTHOR("http://www.100ask.net"); // 驱动程序的作者
MODULE_DESCRIPTION("S3C2410/S3C2440 LED
Driver"); // 一些描述信息
MODULE_LICENSE("GPL"); // 遵循的协议
之后再drives/char/Kconfig中加入:
config ConFig_led_test
bool "key test"
help
this key test help
在drives/char/Makefile中加入:
obj-m$(CONFIG_led_test)+=s3c24xx_leds.o
回到内核根目录:
执行 make modules
error 1: asm/arch/regs-gpio.h: No such file or directory
解决:头文件在/home/h-a2/vl/home/lzw/linuxlast/arch/arm/mach-s3c2410/include/mach中,修改源代码即可
编译后生成s3c24xx_leds.ko 接着编译ledtest
用u盘将这俩拷贝到开发板的/lib/modules/2.6.32.2-FriendlyARM下
insmod s3c24xx_leds.ko和rmmod s3c24xx_leds.ko加载和卸载驱动,用led_test 1
on来验证驱动。