開發板:TQ2440
核心:2.6.30.4-EmbedSky
實現目標:控制led開關,併當前讀取狀態
-------驅動實現 mini2440_leds.c-------
#include <linux/miscdevice.h>#include <linux/delay.h>#include <asm/irq.h>#include <mach/regs-gpio.h>#include <mach/hardware.h>#include <linux/kernel.h>#include <linux/module.h>#include <linux/init.h>#include <linux/mm.h>#include <linux/fs.h>#include <linux/types.h>#include <linux/delay.h>#include <linux/moduleparam.h>#include <linux/slab.h>#include <linux/errno.h>#include <linux/ioctl.h>#include <linux/cdev.h>#include <linux/string.h>#include <linux/list.h>#include <linux/pci.h>#include <asm/uaccess.h>#include <asm/atomic.h>#include <asm/unistd.h>#define DEVICE_NAME "leds" //要操作的裝置名稱,載入成功後在/dev/下出現//操作的4個ledstatic 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 unsigned int led_cfg_table1 [] = { S3C2410_GPB5_INP, S3C2410_GPB6_INP, S3C2410_GPB7_INP, S3C2410_GPB8_INP,};*/static int sbc2440_leds_ioctl( struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg){ unsigned char i,tmp; switch(cmd) { case 0: case 1: if (arg > 4) { return -EINVAL; } s3c2410_gpio_cfgpin(led_table[arg], led_cfg_table[arg]); s3c2410_gpio_setpin(led_table[arg], !cmd); //關或開 return 0; case 2: tmp = s3c2410_gpio_getpin(led_table[arg]); //讀取狀態 tmp = tmp >>(arg+5); return (unsigned int)tmp; default: return -EINVAL; }}static struct file_operations dev_fops = { .owner = THIS_MODULE, .ioctl = sbc2440_leds_ioctl,};//混雜結構static struct miscdevice misc = { .minor = MISC_DYNAMIC_MINOR, .name = DEVICE_NAME, .fops = &dev_fops,};static int __init dev_init(void){ int ret; int i; //初始化四個led,並開啟 for (i = 0; i < 4; i++) { s3c2410_gpio_cfgpin(led_table[i], led_cfg_table[i]); s3c2410_gpio_setpin(led_table[i], 0); } 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("FriendlyARM Inc.");
======應用程式app-led.c======
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/ioctl.h>int main(int argc, char **argv){ int on; int led_no; int fd; if (argc != 3 || sscanf(argv[1], "%d", &led_no) != 1 || sscanf(argv[2],"%d", &on) != 1 || on < 0 || on > 2 || led_no < 0 || led_no > 3) { fprintf(stderr, "Usage: leds led_no 0|1|2\n"); exit(1); } fd = open("/dev/leds", 0); if (fd < 0) { perror("open device leds"); exit(1); } if(on==2) //讀取led狀態 { printf("state:=%d\n",ioctl(fd, on, led_no)); } else //控制led開或關,即0,1 { ioctl(fd, on, led_no); } close(fd); return 0;}
=========Makefile==========
ifneq ($(KERNELRELEASE),)obj-m := mini2440_leds.oelse KDIR := /opt/guoqian/opt/EmbedSky/linux-2.6.30.4all: make -C $(KDIR) M=$(PWD) modules ARCH=arm CROSS_COMPILE=arm-linux-clean: rm -f *.ko *.o *.mod.o *.mod.c *.symvers modul*endif
=======操作結果======
[root@EmbedSky /tmp]# insmod mini2440_leds.ko
[root@EmbedSky /tmp]# ./app-led 1 2
state:=0
[root@EmbedSky /tmp]# ./app-led 1 0
[root@EmbedSky /tmp]# ./app-led 1 2
state:=1
[root@EmbedSky /tmp]#