Tiny 6410 Led 驱动模块加载(五)

为安卓驱动做准备,先熟悉linux下的驱动。

1.重新编译内核 
去掉内核对LED驱动的支持,
#make menuconfig ARCH=arm CROSS_COMPILE=arm-linux- (配置)
Device drivers->Characterdevices->LED Support for Mini6410 GPIO LEDs(取消)
make uImage ARCH=arm CROSS_COMPILE=arm-linux-(编译好后烧入开发板)


2.编写模块
#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>

#include <plat/gpio-cfg.h>
#include<plat/regs-clock.h>
#include<plat/regs-gpio.h>

#include<plat/gpio-bank-e.h>
#include<plat/gpio-bank-k.h>

#define DEVICE_NAME "led123"

static int sbc6410_leds_ioctl(
struct inode *inode, 
struct file *file, 
unsigned int cmd, 
unsigned long arg)
{
printk (DEVICE_NAME": %d,%d\r\n", cmd, arg);
switch(cmd) {
unsigned tmp;
case 0:
case 1:
if (arg > 4) {
return -EINVAL;
}
tmp = readl(S3C64XX_GPKDAT);
tmp &= ~(1 << (4+ arg));
tmp |= ( (!cmd) << (4 + arg));
writel(tmp, S3C64XX_GPKDAT);
// printk (DEVICE_NAME": %d %d\r\n", arg, cmd);
return 0;
default:
return -EINVAL;
}
}

static struct file_operations dev_fops = {
.owner = THIS_MODULE,
.ioctl = sbc6410_leds_ioctl,
};

static struct miscdevice misc = {
.minor = MISC_DYNAMIC_MINOR,
.name = DEVICE_NAME,
.fops = &dev_fops,
};

static int __init dev_init(void)
{
int ret;

{
unsigned tmp;
tmp = readl(S3C64XX_GPKCON);
tmp = (tmp &~(0xffffU<<16))|(0x1111U<<16);
writel(tmp, S3C64XX_GPKCON);
tmp = readl(S3C64XX_GPKDAT);
tmp |= (0xF << 4);
writel(tmp, S3C64XX_GPKDAT);
}

ret = misc_register(&misc);

printk (DEVICE_NAME"\tinitialized\r\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("QiuGong");

使用Android-linux,编译内核模块时出现了好多错误。最后是参照友善原来有的内核模块程序,修改最终完成。


3.编写Makefile
ifneq ($(KERNELRELEASE),)

obj-m := Tiny6410_leds_misc.o

else
KDIR := /home/qiugong/linux-2.6.28.6_use
all:
make -C $(KDIR) M=$(PWD) modules ARCH=armCROSS_COMPILE=arm-linux-
clean:
rm -f *.ko *.o *.mod.o *.mod.c *.symvers  modul*
endif

模块,Makefile编写好后,直接运行make。会产生 .ko文件。
拷贝到开发板,运行 insmod Tiny6410_leds_misc.ko(lsmod:查看当前模块 rmmod Tiny6410_leds_misc:移除模块)


4.编写应用程序
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
//#include "memdev.h" 
int main(int argc, char **argv)
{
int num,sta;
int fd;
if (argc != 3
|| sscanf(argv[1],"%d", &sta) != 1 || sta< 0 || sta >1 
|| sscanf(argv[2],"%d", &num) != 1 || num< 0 || num > 3) {
fprintf(stderr, "Usage:%snum:0~4,sta:0~1.\r\n",argv[0]);
exit(1);
}
fd = open("/dev/led123", 0);
if (fd < 0) {
perror("open device led123");
exit(1);
}
if(sta){
printf("turn on all led123!\n");
ioctl(fd, 1, num);
}
else {
printf("turn off all led123!\n");
ioctl(fd,   0, num);
}
close(fd);
return 0;
}

编译:
arm-linux-gcc -static app-led.c -oapp-led

以上是led模块驱动,有点粗糙,不太具体。但都是最重要的!

你可能感兴趣的:(Tiny 6410 Led 驱动模块加载(五))