Makefile: EXEC = leds.ko OBJS = magic-leds.o SRC = magic-leds.c
INCLUDE = /zylinux/kernel/include USEINC =. CC = arm-linux-gcc LD = arm-linux-ld MODCFLAGS = -O2 -Wall -D__KERNEL__ -DMODULE -I$(INCLUDE) -I$(USEINC) -march=armv4t -c -o LDFLAGS = -r
all: $(EXEC)
$(EXEC): $(OBJS) $(LD) $(LDFLAGS) -o $@ $(OBJS)
%.o:%.c $(CC) $(MODCFLAGS) -mapcs -c $< -o $@
clean: -rm -f $(EXEC) *.o *~ core
cross_2.95.3.tar.bz2已经安装好:/usr/local/arm/下,环境变量已经设置好,编译其它的程序(不是驱动程序)都是可以用的。linux为redhat9.0,内核为:2.4.20-8
magic-leds.c:
#ifndef __KERNEL__ #define __KERNEL__ #endif
#ifndef MODULE #define MODULE #endif
#include <linux/config.h> #include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> #include <linux/sched.h>
#include <asm/hardware.h>
#define DEVICE_NAME "magic-leds" //#define LED_MAJOR 231 //can be 231~239 or 240~254
static unsigned long leds_table [] = { GPIO_E11, GPIO_E12, GPIO_H4, GPIO_H6, GPIO_H10, };
static int magic_leds_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg) { if (arg > 4) return -EINVAL;
switch(cmd) { case 0: case 1: write_gpio_bit(leds_table[arg], cmd); default: return -EINVAL; } }
static int magic_leds_open(struct inode *inode, struct file *filp) { int i;
for (i = 0; i < 5; i++) { set_gpio_ctrl (leds_table | GPIO_PULLUP_EN | GPIO_MODE_OUT); write_gpio_bit(leds_table, 1); }
MOD_INC_USE_COUNT; printk(KERN_INFO DEVICE_NAME ": opened.
"); return 0; }
static int magic_leds_release(struct inode *inode, struct file *filp) { MOD_DEC_USE_COUNT; printk(KERN_INFO DEVICE_NAME ": released.
"); return 0; }
static struct file_operations magic_leds_fops = { owner: THIS_MODULE, ioctl: magic_leds_ioctl, open: magic_leds_open, release: magic_leds_release, };
static devfs_handle_t devfs_handle; static int __init magic_leds_init(void) { // int result;
// result = register_chrdev(0, DEVICE_NAME, &magic_leds_fops); // if (result < 0) // { // printk(KERN_ERR DEVICE_NAME ": Failed to register major.
"); // return result; // } devfs_handle = devfs_register(NULL, DEVICE_NAME, DEVFS_FL_AUTO_DEVNUM, 0, 0, S_IFCHR | S_IRUSR | S_IWUSR, &magic_leds_fops, NULL);
printk(KERN_INFO DEVICE_NAME ": Initialize OK.
"); return 0; }
static void __exit magic_leds_exit(void) { devfs_unregister(devfs_handle); // unregister_chrdev(0, DEVICE_NAME); }
module_init(magic_leds_init); module_exit(magic_leds_exit); |