/*********************************************************实验源代码 ********************************************************************/
一、源代码:
/***********************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>
#include <linux/gpio.h> //2.6.32.2内核版本要求
#define DEVICE_NAME "leds"
//定义GPIO管脚
static unsigned long led_table [] = {
S3C2410_GPB(5), //不能是S3C2410_GPB5; 因为没有这样定义,可以通过#define S3C2410_GPB5 S3C2410_GPB(5)
S3C2410_GPB(6),
S3C2410_GPB(7),
S3C2410_GPB(8),
};
//设置管脚模式
static unsigned int led_cfg_table [] = {
S3C2410_GPIO_OUTPUT, //随内核版本中定义类型的变化,
S3C2410_GPIO_OUTPUT,
S3C2410_GPIO_OUTPUT,
S3C2410_GPIO_OUTPUT,
};
//ioctl函数实现
static int sbc2440_leds_ioctl(
struct inode *inode,
struct file *file,
unsigned int cmd,
unsigned long arg)
{
switch(cmd) {
case 0:
case 1:
if (arg > 4) {
return -EINVAL;
}
s3c2410_gpio_setpin(led_table[arg], !cmd);
return 0;
default:
return -EINVAL;
}
}
//dev_fops操作指令集
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,
};
//第二步:gpio模式选择,设定管脚
// 注册混杂设备
static int __init dev_init(void)
{
int ret;
int i;
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_init(dev_init);
module_exit(dev_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("FriendlyARM Inc.");
/**************************Makefile文件************************
*
*****************************************************************/
ifneq ($(KERNELRELEASE),)
obj-m := mini2440_leds.o
else
KDIR:=/home/kernel/linux-2.6.32.2
all:
make -C $(KDIR) M=$(PWD) modules ARCH=arm CROSS_COMPILE=arm-linux-
clean:
rm -f *.ko *.o *.mod.o *.mod.c *.symvers modul*
endif
二、测试LED驱动的APP程序
/*******************************app_leds.c***************************************
*AUTHOR DREAM
*
**********************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include "memdev.h" /* 包含命令定义 */
int main(int argc, char **argv)
{
int on;
int fd;
if (argc != 2 || sscanf(argv[1],"%d", &on) != 1 ||on < 0 || on > 1 ) {
fprintf(stderr, "Usage:%s 0|1\n",argv[0]);
exit(1);
}
fd = open("/dev/leds", 0);
if (fd < 0) {
perror("open device leds");
exit(1);
}
/*通过ioctl来控制流水灯*/
if(on){
printf("start liushuideng !\n");
ioctl(fd, MEMDEV_IOCON, 0);
}
else {
printf("turn off all leds!\n");
ioctl(fd, MEMDEV_IOCOFF, 0);
}
close(fd);
return 0;
}
三、遇到的问题
1、
[root@localhost LED]# make
make -C /home/kernel/linux-2.6.32.2 M=/home/nfsshare/root_qtopia/Drive/LED modules ARCH=arm CROSS_COMPILE=arm-linux-
make[1]: Entering directory `/home/kernel/linux-2.6.32.2'
CC [M] /home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.o
/home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.c:29: error: 'S3C2410_GPB5' undeclared here (not in a function)
/home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.c:30: error: 'S3C2410_GPB6' undeclared here (not in a function)
/home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.c:31: error: 'S3C2410_GPB7' undeclared here (not in a function)
/home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.c:32: error: 'S3C2410_GPB8' undeclared here (not in a function)
/home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.c:36: error: 'S3C2410_GPB5_OUTP' undeclared here (not in a function)
/home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.c:36: error: initializer element is not constant
/home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.c:36: error: (near initialization for 'led_cfg_table[0]')
/home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.c:37: error: 'S3C2410_GPB6_OUTP' undeclared here (not in a function)
/home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.c:37: error: initializer element is not constant
/home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.c:37: error: (near initialization for 'led_cfg_table[1]')
/home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.c:38: error: 'S3C2410_GPB7_OUTP' undeclared here (not in a function)
/home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.c:38: error: initializer element is not constant
/home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.c:38: error: (near initialization for 'led_cfg_table[2]')
/home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.c:39: error: 'S3C2410_GPB8_OUTP' undeclared here (not in a function)
/home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.c:39: error: initializer element is not constant
/home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.c:39: error: (near initialization for 'led_cfg_table[3]')
/home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.c: In function 'sbc2440_leds_ioctl':
/home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.c:54: error: implicit declaration of function 's3c2410_gpio_setpin'
/home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.c: In function 'dev_init':
/home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.c:80: error: implicit declaration of function 's3c2410_gpio_cfgpin'
make[2]: *** [/home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.o] 错误 1
make[1]: *** [_module_/home/nfsshare/root_qtopia/Drive/LED] 错误 2
make[1]: Leaving directory `/home/kernel/linux-2.6.32.2'
make: *** [all] 错误 2
错误原因:内核版本不对,没有S3C2410_GPB5的类型定义 需要修改为S3C2410_GPB(5)
内核版本定义如下:
/* arch/arm/mach-s3c2410/include/mach/gpio-nrs.h
*
* Copyright (c) 2008 Simtec Electronics
* http://armlinux.simtec.co.uk/
* Ben Dooks <ben@simtec.co.uk>
*
* S3C2410 - GPIO bank numbering
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#ifndef __MACH_GPIONRS_H
#define __MACH_GPIONRS_H
#define S3C2410_GPIONO(bank,offset) ((bank) + (offset))
#define S3C2410_GPIO_BANKA (32*0)
#define S3C2410_GPIO_BANKB (32*1)
#define S3C2410_GPIO_BANKC (32*2)
#define S3C2410_GPIO_BANKD (32*3)
#define S3C2410_GPIO_BANKE (32*4)
#define S3C2410_GPIO_BANKF (32*5)
#define S3C2410_GPIO_BANKG (32*6)
#define S3C2410_GPIO_BANKH (32*7)
/* GPIO bank sizes */A
#define S3C2410_GPIO_A_NR (32)
#define S3C2410_GPIO_B_NR (32)
#define S3C2410_GPIO_C_NR (32)
#define S3C2410_GPIO_D_NR (32)
#define S3C2410_GPIO_E_NR (32)
#define S3C2410_GPIO_F_NR (32)
#define S3C2410_GPIO_G_NR (32)
#define S3C2410_GPIO_H_NR (32)
#if CONFIG_S3C_GPIO_SPACE != 0
#error CONFIG_S3C_GPIO_SPACE cannot be zero at the moment
#endif
#define S3C2410_GPIO_NEXT(__gpio) \
((__gpio##_START) + (__gpio##_NR) + CONFIG_S3C_GPIO_SPACE + 0)
#ifndef __ASSEMBLY__
enum s3c_gpio_number {
S3C2410_GPIO_A_START = 0,
S3C2410_GPIO_B_START = S3C2410_GPIO_NEXT(S3C2410_GPIO_A),
S3C2410_GPIO_C_START = S3C2410_GPIO_NEXT(S3C2410_GPIO_B),
S3C2410_GPIO_D_START = S3C2410_GPIO_NEXT(S3C2410_GPIO_C),
S3C2410_GPIO_E_START = S3C2410_GPIO_NEXT(S3C2410_GPIO_D),
S3C2410_GPIO_F_START = S3C2410_GPIO_NEXT(S3C2410_GPIO_E),
S3C2410_GPIO_G_START = S3C2410_GPIO_NEXT(S3C2410_GPIO_F),
S3C2410_GPIO_H_START = S3C2410_GPIO_NEXT(S3C2410_GPIO_G),
};
#endif /* __ASSEMBLY__ */
/* S3C2410 GPIO number definitions. */
#define S3C2410_GPA(_nr) (S3C2410_GPIO_A_START + (_nr))
#define S3C2410_GPB(_nr) (S3C2410_GPIO_B_START + (_nr))
#define S3C2410_GPC(_nr) (S3C2410_GPIO_C_START + (_nr))
#define S3C2410_GPD(_nr) (S3C2410_GPIO_D_START + (_nr))
#define S3C2410_GPE(_nr) (S3C2410_GPIO_E_START + (_nr))
#define S3C2410_GPF(_nr) (S3C2410_GPIO_F_START + (_nr))
#define S3C2410_GPG(_nr) (S3C2410_GPIO_G_START + (_nr))
#define S3C2410_GPH(_nr) (S3C2410_GPIO_H_START + (_nr))
/* compatibility until drivers can be modified */
//宏定义了GPA和GPE
#define S3C2410_GPA0 S3C2410_GPA(0)
#define S3C2410_GPA1 S3C2410_GPA(1)
#define S3C2410_GPA3 S3C2410_GPA(3)
#define S3C2410_GPA7 S3C2410_GPA(7)
#define S3C2410_GPE0 S3C2410_GPE(0)
#define S3C2410_GPE1 S3C2410_GPE(1)
#define S3C2410_GPE2 S3C2410_GPE(2)
#define S3C2410_GPE3 S3C2410_GPE(3)
#define S3C2410_GPE4 S3C2410_GPE(4)
#define S3C2410_GPE5 S3C2410_GPE(5)
#define S3C2410_GPE6 S3C2410_GPE(6)
#define S3C2410_GPE7 S3C2410_GPE(7)
#define S3C2410_GPE8 S3C2410_GPE(8)
#define S3C2410_GPE9 S3C2410_GPE(9)
#define S3C2410_GPE10 S3C2410_GPE(10)
#define S3C2410_GPH10 S3C2410_GPH(10)
#endif /* __MACH_GPIONRS_H */
2、问题二
[root@localhost LED]# make
make -C /home/kernel/linux-2.6.32.2 M=/home/nfsshare/root_qtopia/Drive/LED modules ARCH=arm CROSS_COMPILE=arm-linux-
make[1]: Entering directory `/home/kernel/linux-2.6.32.2'
CC [M] /home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.o
/home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.c: In function 'sbc2440_leds_ioctl':
/home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.c:54: error: implicit declaration of function 's3c2410_gpio_setpin'
/home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.c: In function 'dev_init':
/home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.c:80: error: implicit declaration of function 's3c2410_gpio_cfgpin'
make[2]: *** [/home/nfsshare/root_qtopia/Drive/LED/mini2440_leds.o] 错误 1
make[1]: *** [_module_/home/nfsshare/root_qtopia/Drive/LED] 错误 2
make[1]: Leaving directory `/home/kernel/linux-2.6.32.2'
原因:没有声明s3c2410_gpio_setpin和s3c2410_gpio_cfpin
j解决方法:调价头文件#include<linux/gpio.h>
3、问题三
在securecat中加载mini2440.ko文件,出现File existed
原因:使用的内核已经编译了led驱动模块,因此报错
解决:重新编译内核模块,在make menuconfig中不选择led选项
三、测设驱动程序
驱动设备的名字为:leds
看驱动设备:cat /proc/devices ls /dev/leds
测试结果:insmod mini2440_leds.ko
rmmod mini2440_leds
lsmod 显示动态加载的
/********************************************************************************* 自己写的led驱动程序**********************************************************/
功能:
实现LED驱动测试案例:
led_test on //对应四个LED全亮
led_test off // 对应四个LED全灭
led_test run // 运行跑马灯实验
led_test shine //4个LED灯全灭、全亮交替闪烁
led_test 1 on //对应LED1点亮
led_test 1 off // 对应LED1熄灭
...
led_test 4 on //对应LED4点亮
led_test 4 off // 对应LED4熄灭
1、led_function.c
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/input.h>
#include <linux/init.h>
#include <linux/serio.h>
#include <linux/delay.h>
#include <linux/clk.h>
#include <linux/miscdevice.h>
#include <linux/gpio.h>
#include <linux/ioctl.h>
#include <asm/io.h>
#include <asm/irq.h>
#include <asm/uaccess.h>
#include <mach/regs-clock.h>
#include <plat/regs-timer.h>
#include <mach/regs-gpio.h>
#include <linux/cdev.h>
static int major_dev=0;
//#define DEV_NAME "led"
static char DEV_NAME[]="led"; //设备名字全局变量
static struct cdev Leddev;
static unsigned int data;
#define MAGIC 'k'
#define LED_ALL_ON _IOW (MAGIC,1,int)
#define LED_ALL_OFF _IOW (MAGIC,2,int)
#define LED_RUN _IOW (MAGIC,3,int)
#define LED_SHINE _IOW (MAGIC,4,int)
#define LED_ON _IOW (MAGIC,5,int)
#define LED_OFF _IOW (MAGIC,6,int)
static unsigned long led_table [] = {
S3C2410_GPB(5),
S3C2410_GPB(6),
S3C2410_GPB(7),
S3C2410_GPB(8),
};
static unsigned int led_cfg_table[]={
S3C2410_GPIO_OUTPUT,
S3C2410_GPIO_OUTPUT,
S3C2410_GPIO_OUTPUT,
S3C2410_GPIO_OUTPUT,
};
static int led_all_on(void) //错误9
{
int i;
for(i=0;i<4;i++)
{
s3c2410_gpio_setpin(led_table[i],0);
}
printk(KERN_NOTICE"led_all_on successed\n");
return 0;
}
static int led_all_off(void) //错误10
{
int i;
for(i=0;i<4;i++)
{
s3c2410_gpio_setpin(led_table[i],1);
}
printk(KERN_NOTICE"led_all_off successed\n");
return 0;
}
/*static int led_run(int data)
{
int i,j;
int count;
count=data;
for(j=0;j<count;j++)
{
for(i=0;i<4;i++)
{
s3c2410_gpio_setpin(led_table[i],0);
mdelay(1000);
s3c2410_gpio_setpin(led_table[i],1);
mdelay(1000);
}
}
return 0;
}*/
/*static int led_shine(int data)
{
int i,j;
int count;
count=data;
for(j=0;j<count;j++)
{
for(i=0;i<4;i++)
{
s3c2410_gpio_setpin(led_table[i],0);
}
mdelay(1000);
for(i=0;i<4;i++)
{
s3c2410_gpio_setpin(led_table[i],1);
}
mdelay(1000);
}
return 0;
}*/
static int led_open(struct inode *inode,struct file *filp)
{
int i;
for(i=0;i<4;i++)
{
s3c2410_gpio_cfgpin(led_table[i], led_cfg_table[i]);
s3c2410_gpio_setpin(led_table[i],1);
}
printk(KERN_NOTICE "led config successed\n");
return 0;
}
static int led_release(struct inode *inode,struct file *filp)
{
return 0;
}
//static int led_read(struct file *filp,char _ _user *buf, ssize_t count,loff_t *ppos)
//{
// return 0;
//}
//static int led_write(struct file *filp,char _ _user *buf, ssize_t count,loff_t *ppos)
//{
// return 0;
//}
//static ssize_t led_llseek(struct file *filp,loff_t offset,int orig)
//{
// return 0;
//}
static int led_ioctl(struct inode *inode,
struct file *filp,
unsigned int cmd,
unsigned long arg)
{
/*类型检测*/
if(_IOC_TYPE(cmd)!=MAGIC)
printk("type eror define\n");
/*传递argment*/
// unsigned int data;
// if (copy_from_user(&data, (unsigned int __user *)arg, sizeof(int)))
// return -EFAULT;
// data=(unsigned int)arg;
if (__get_user(data, (unsigned int __user *)arg)) //方法二:指针参数传递
return -EFAULT;
switch (cmd)
{
case LED_ALL_ON:
led_all_on();
return 0;
case LED_ALL_OFF:
led_all_off();
return 0;
case LED_RUN:
{
int i,j;
led_all_off();
for(j=0;j<data;j++)
{
for(i=0;i<4;i++)
{
s3c2410_gpio_setpin(led_table[i],0);
mdelay(1000);
s3c2410_gpio_setpin(led_table[i],1);
mdelay(1000);
}
}
printk(KERN_NOTICE "led_run successed\n");
return 0;
}
case LED_SHINE:
{
int i,j;
led_all_off();
for(j=0;j<data;j++)
{
for(i=0;i<4;i++)
{
s3c2410_gpio_setpin(led_table[i],0);
}
mdelay(1000);
for(i=0;i<4;i++)
{
s3c2410_gpio_setpin(led_table[i],1);
}
mdelay(1000);
}
printk(KERN_NOTICE "led_shine successed\n");
return 0;
}
case LED_ON:
s3c2410_gpio_setpin(led_table[data],0);
return 0;
case LED_OFF:
s3c2410_gpio_setpin(led_table[data],1);
return 0;
default:
return -EINVAL;
}
}
static struct file_operations fops={ //错误1
.owner=THIS_MODULE,
.open=led_open,
.release=led_release, //错误3
// .write=led_write,
// .read=led_read,
// .llseek=led_llseek,
.ioctl=led_ioctl,
};
static int __init leds_init(void)
{
/*申请设备号*/
int ret,err;
dev_t dev,new_dev;
dev=MKDEV(major_dev,0); //获取设备号
if(major_dev) //错误6
ret=register_chrdev_region(dev,1,DEV_NAME);
else
{
ret=alloc_chrdev_region(&dev,0,1,DEV_NAME);
major_dev=MAJOR(dev);
}
/*调试信息*/
if(ret<0)
{
printk(KERN_WARNING "leds: unable to get major %d\n", major_dev);
// return ret;
}
printk("device major ID :%d\n",major_dev);
printk("device name : %s\n",DEV_NAME);
/*初始化设备*/
cdev_init(&Leddev,&fops);
Leddev.owner=THIS_MODULE; //所属模块 struct module *owner
Leddev.ops=&fops;
/*添加设备*/
new_dev=MKDEV(major_dev,0); //再次获取设备号
err=cdev_add(&Leddev,new_dev,1);
if(err)
{
printk (KERN_NOTICE "Error %d adding led\n", err);
return err;
}
}
static void __exit leds_exit(void) //错误7
{
cdev_del(&Leddev);//注销设备
unregister_chrdev_region(MKDEV(major_dev,0),1); //释放设备号 //错误2
printk("leds uninstalled\n");
}
module_init(leds_init);
module_exit(leds_exit);
MODULE_LICENSE("GPL"); //错误5
MODULE_AUTHOR("DREAM");
2、Makefile
ifneq ($(KERNELRELEASE),)
obj-m := led_funtion.o
else
KDIR:=/home/kernel/linux-2.6.32.2
all:
make -C $(KDIR) M=$(PWD) modules ARCH=arm CROSS_COMPILE=arm-linux-
clean:
rm -f *.ko *.o *.mod.o *.mod.c *.symvers modul* *bak
endif
3、应用测试程序
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#define MAGIC 'k'
#define LED_ALL_ON _IOW (MAGIC,1,int)
#define LED_ALL_OFF _IOW (MAGIC,2,int)
#define LED_RUN _IOW (MAGIC,3,int)
#define LED_SHINE _IOW (MAGIC,4,int)
#define LED_ON _IOW (MAGIC,5,int)
#define LED_OFF _IOW (MAGIC,6,int)
void usage(char *exename)
{
printf("Usage:\n");
printf(" %s <led_no> <on/off>\n", exename);
printf(" led_no = 1, 2, 3 or 4\n");
}
int main(int argc, char **argv)
{
unsigned int led;
int fd = -1;
unsigned int count=3;
//参数只能为2 和 3个
if (argc > 3 || argc == 1)
goto err;
fd = open("/dev/led", 0); // 打开设备
if (fd < 0) {
printf("Can't open /dev/leds\n");
return -1;
}
if (argc == 2) {
if (!strcmp(argv[1], "all_on")) {
ioctl(fd, LED_ALL_ON, &count); // 点亮它
} else if (!strcmp(argv[1], "all_off")) {
ioctl(fd, LED_ALL_OFF, &count); // 熄灭它
} else if (!strcmp(argv[1], "run")) {
ioctl(fd, LED_RUN, &count); //运行跑马灯
} else if (!strcmp(argv[1], "shine")) {
ioctl(fd, LED_SHINE, &count); //闪烁
} else {
goto err;
}
}
if (argc == 3) {
led = strtoul(argv[1], 0, 10)-1; // 操作哪个LED?
if (led > 3)
goto err;
if (!strcmp(argv[2], "on")) {
ioctl(fd, LED_ON, &led); // 点亮
} else if (!strcmp(argv[2], "off")) {
ioctl(fd, LED_OFF, &led); // 熄灭
} else {
goto err;
}
}
close(fd);
return 0;
err:
if (fd > 0)
close(fd);
usage(argv[0]);
return -1;
}
实验结果:创建设备节点mknod /dev/led c 253 0