在mini2440开发板上(CPU是s3c2440)开发字符设备驱动完成一个led灯的亮灭。
1. 环境
硬件环境:
Host:X86PC
Target:友善之臂mini2440开发板
软件环境:
Host:VMware虚拟机下的redhat linux 9.0(完全安装)
Target:arm-linux kernel:linux-2.6.13
Crosstool: arm-linux-gcc-3.3.2
2. 完成过程
⑴驱动程序文件--mini2440_one_led.c,其具体内容如下:
#define DEVICE_NAME "lsq_led"
#define LED_MAJOR 250
#define GPB5_ON 1
#define GPB5_OFF 2
static int mini2440_led_ioctl(
struct inode *inode,
struct file *file,
unsigned int cmd,
unsigned long arg)
{
if (cmd==GPB5_ON) {
//printk("ioctl LEDON successfully\n");
s3c2410_gpio_setpin(S3C2410_GPB5, 0);
return 0;
}
if (cmd==GPB5_OFF) {
//printk("ioctl LEDOFF successfully\n");
s3c2410_gpio_setpin(S3C2410_GPB5, 1);
return 0;
}
printk("ioctl error \n");
return -EFAULT;
}
static struct file_operations mini2440_led_fops = {
.owner = THIS_MODULE,
.ioctl = mini2440_led_ioctl,
};
static int __init mini2440_led_init(void)
{
int ret;
ret = register_chrdev(LED_MAJOR, DEVICE_NAME, &mini2440_led_fops);
if (ret < 0) {
printk(DEVICE_NAME " can''''''''''''''''''''''''''''''''t register major number\n");
return ret;
}
devfs_mk_cdev(MKDEV(LED_MAJOR, 0), S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP, DEVICE_NAME);
s3c2410_gpio_cfgpin(S3C2410_GPB5, S3C2410_GPB5_OUTP);
s3c2410_gpio_setpin(S3C2410_GPB5, 1);
printk(DEVICE_NAME " initialized\n");
return 0;
}
static void __exit mini2440_led_exit(void){
devfs_remove(DEVICE_NAME);
unregister_chrdev(LED_MAJOR, DEVICE_NAME);
}
module_init(mini2440_led_init);
module_exit(mini2440_led_exit);
⑵ 应用程序文件—文件名led.c,其内容如下:
#define GPB5_ON 1
#define GPB5_OFF 2
int main(void)
{
//int k;
int dev_fd;
int retval;
dev_fd = open("/dev/lsq_led",O_RDWR | O_NONBLOCK);
if ( dev_fd == -1) {
printf("Can''t open file /dev/lsq_led\n");
exit(-1);
}
printf ("\n/dev/lsq_led opened successfully, fd=%d\n",dev_fd);
wlile(1)
{
retval=ioctl(dev_fd,GPB5_ON,0);
if(retval==-1){
perror("ioctl LEDON error\n");
exit(-1);
}
sleep(1);
retval=ioctl(dev_fd,GPB5_OFF,0);
if(retval==-1){
perror("ioctl LEDOFF error\n");
exit(-1);
}
sleep(1);
}
close(dev_fd);
}
⑶ 编译应用程序led.c的Makefile,其内容如下:
all: led
led:
arm-linux-gcc -o led led.c
clean:
@rm -vf mini2440led *.o *~
⑷ 使内核撑持和编译驱动程序mini2440_one_led.c,需在相应的目录下增加如下内容(自己的习气,当然这一步是可选的):
kernel...../drivers/char/
Makefile
obj-$(CONFIG_MINI2440_ONE_LED) += mini2440_one_led.o
Kconfig
config MINI2440_ONE_LED
tristate "mini2440 one LED Driver"
depends on ARCH_S3C2440
help
mini2440 User led, use GPB5.