在mini2440開發(fā)板上(CPU是s3c2440)開發(fā)字符設(shè)備驅(qū)動完成一個led燈的亮滅。
1. 環(huán)境
硬件環(huán)境:
Host:X86PC
Target:友善之臂mini2440開發(fā)板
軟件環(huán)境:
Host:VMware虛擬機下的redhat linux 9.0(完全安裝)
Target:arm-linux kernel:linux-2.6.13
Crosstool: arm-linux-gcc-3.3.2
2. 完成過程
⑴驅(qū)動程序文件--mini2440_one_led.c,其具體內(nèi)容如下:
#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);
⑵ 應(yīng)用程序文件—文件名led.c,其內(nèi)容如下:
#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);
}
⑶ 編譯應(yīng)用程序led.c的Makefile,其內(nèi)容如下:
all: led
led:
arm-linux-gcc -o led led.c
clean:
@rm -vf mini2440led *.o *~
⑷ 使內(nèi)核撐持和編譯驅(qū)動程序mini2440_one_led.c,需在相應(yīng)的目錄下增加如下內(nèi)容(自己的習(xí)氣,當(dāng)然這一步是可選的):
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.