1.注册字符设备使用 int register_chrdev(unsigned int major, const char *name, struct file_operations *fops); ret = register_chrdev(LED_MAJOR, DEVICE_NAME, &led_ops); @major:主设备号 @name:设备名 @fops:操作函数 移除字符设备使用 int unregister_chrdev(unsigned int major, const char *name); unregister_chrdev(LED_MAJOR, DEVICE_NAME); 2.gpio的使用 1)申请管脚 gpio_request(led_table[i],led_names[i]); 2)设置gpio输入输出高低电平 gpio_direction_output(led_table[i], 1);//低电平亮 ----------------------------------------------- flowing_lights_driver.c /** *s3c6410 linux2.6.35.7 */ #include <linux/init.h> #include <linux/module.h> #include <linux/fs.h> #include <linux/init.h> #include <mach/gpio.h> #include <plat/gpio-cfg.h> #include <linux/device.h> #define LED_MAJOR 234 #define DEVICE_NAME "led_driver" struct class *led_class; static char *led_names[] = {"led0", "led1", "led2", "led3"}; static unsigned long led_table[] = { S3C64XX_GPM(0), S3C64XX_GPM(1), S3C64XX_GPM(2), S3C64XX_GPM(3), }; int led_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg) { switch (cmd) { case 0: gpio_direction_output(led_table[0], 0);//低电平点亮 gpio_direction_output(led_table[1], 1); gpio_direction_output(led_table[2], 1); gpio_direction_output(led_table[3], 1); break; case 1: gpio_direction_output(led_table[0], 1);//低电平点亮 gpio_direction_output(led_table[1], 0); gpio_direction_output(led_table[2], 1); gpio_direction_output(led_table[3], 1); break; case 2: gpio_direction_output(led_table[0], 1);//低电平点亮 gpio_direction_output(led_table[1], 1); gpio_direction_output(led_table[2], 0); gpio_direction_output(led_table[3], 1); break; case 3: gpio_direction_output(led_table[0], 1);//低电平点亮 gpio_direction_output(led_table[1], 1); gpio_direction_output(led_table[2], 1); gpio_direction_output(led_table[3], 0); break; default: return -EINVAL; break; } return 0; } struct file_operations led_ops = { .owner = THIS_MODULE, .ioctl = led_ioctl, }; static int __init init_led(void) { int i, ret; //creat own class under /sysfs ls /sys/class/led_class/led_driver/ led_class = class_create(THIS_MODULE, "led_class"); if(IS_ERR(led_class)) { printk("failed to creat class\n"); return -1; } ret = register_chrdev(LED_MAJOR, DEVICE_NAME, &led_ops); if (ret < 0) { printk(DEVICE_NAME, "can't register major number\n"); } //在dev/下创建设备节点,节点名为DEVICE_NAME ls -l /dev/led_driver device_create(led_class, NULL, MKDEV(LED_MAJOR, 0), NULL, DEVICE_NAME, 0); for (i=0; i<4; i++) { //申请管脚 gpio_request(led_table[i],led_names[i]); //在gpio引脚上写值之后,把这个端口设置为输出方式 gpio_direction_output(led_table[i], 1);//低电平亮 } printk("led init\n"); return 0; } static void __exit exit_led(void) { unregister_chrdev(LED_MAJOR, DEVICE_NAME); } module_init(init_led); module_exit(exit_led); MODULE_LICENSE("GPL"); ---------------------------------------------------- led_test.c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <fcntl.h> #include <unistd.h> #define LED_DEV "/dev/led_driver" //# mknod /dev/led_driver c 234 0 int main(int argc, char **argv) { int fd = 0; fd = open(LED_DEV, O_RDWR); if (fd < 0) { perror("open"); exit(1); } while (1) { ioctl(fd, 0); printf("led0 is on\n"); sleep(1); ioctl(fd, 1); printf("led1 is on\n"); sleep(1); ioctl(fd, 2); printf("led2 is on\n"); sleep(1); ioctl(fd, 3); printf("led3 is on\n"); sleep(1); } close(fd); return 0; }