附件:程序
附件.rar 说明: 目标内核 2.6.33
开发板 :TQ2440
tq移植手册中的 内核是2.6.30.4 ,与2.6.33 不一样,在按照tq 移植手册
中的 led.c 移植时候,总出现 下面的错误- error: 'S3C2410_GPB5_OUTP' undeclared here (not in a function)
所以参考了网上强大 的资源
Linux2.6.33 自己动手写驱动实现了自己 led 功能。
硬件说明:
tq 开发板 led GPB5 6 7 8 是led 控制管脚,当低电平时,led亮。
1. 编写 led 驱动程序- root@yuweixian:/opt/me_linux/linux-2.6.33# cd drivers/ywx-driver/
-
root@yuweixian:/opt/me_linux/linux-2.6.33/drivers/ywx-driver# gedit led.c
led.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>
-
-
-
-
#define DEVICE_NAME "led-control"
-
-
-
#define S3C2410_GPBCON S3C2410_GPIOREG(0x10)
-
-
#define S3C2410_GPBDAT S3C2410_GPIOREG(0x14)
-
-
#define S3C2410_GPBUP S3C2410_GPIOREG(0x18)
-
-
-
#define S3C2410_GPB5 S3C2410_GPIONO(S3C2410_GPIO_BANKB, 5)
-
-
#define S3C2410_GPB6 S3C2410_GPIONO(S3C2410_GPIO_BANKB, 6)
-
-
#define S3C2410_GPB7 S3C2410_GPIONO(S3C2410_GPIO_BANKB, 7)
-
-
#define S3C2410_GPB8 S3C2410_GPIONO(S3C2410_GPIO_BANKB, 8)
-
-
-
-
#define S3C2410_GPB5_OUTP (0x01 << 10)
-
-
#define S3C2410_GPB6_OUTP (0x01 << 12)
-
-
#define S3C2410_GPB7_OUTP (0x01 << 14)
-
-
#define S3C2410_GPB8_OUTP (0x01 << 16)
-
-
/* 应用程序执行ioctl(fd, cmd, arg)时的第2个参数 */
-
-
#define IOCTL_GPIO_ON 1
-
-
#define IOCTL_GPIO_OFF 0
-
-
-
/* 用来指定LED所用的GPIO引脚 */
-
-
static unsigned long gpio_table [] =
-
-
{
-
S3C2410_GPB(5),
-
-
S3C2410_GPB(6),
-
-
S3C2410_GPB(7),
-
-
S3C2410_GPB(8),
-
-
};
-
-
/* 用来指定GPIO引脚的功能:输出 */
-
-
static unsigned int gpio_cfg_table [] =
-
-
{
-
-
//由于2.6.33内核中没有定义GPIO的动作,所以我在上面自己定义了一下。
-
-
S3C2410_GPB5_OUTP,
-
-
S3C2410_GPB6_OUTP,
-
-
S3C2410_GPB7_OUTP,
-
-
S3C2410_GPB8_OUTP,
-
-
};
-
-
-
-
static int te2440_gpio_ioctl(
-
-
struct inode *inode,
-
-
struct file *file,
-
-
unsigned int cmd,
-
-
unsigned long arg)
-
-
{
-
-
if (arg > 4)
-
-
{
-
-
return -EINVAL;
-
-
}
-
-
switch(cmd)
-
-
{
-
-
case IOCTL_GPIO_ON:
-
-
// 设置指定引脚的输出电平为0
-
-
s3c2410_gpio_setpin(gpio_table[arg], 0);
-
-
return 0;
-
-
-
-
case IOCTL_GPIO_OFF:
-
-
// 设置指定引脚的输出电平为1
-
-
s3c2410_gpio_setpin(gpio_table[arg], 1);
-
-
return 0;
-
-
-
-
default:
-
-
return -EINVAL;
-
-
}
-
-
}
-
-
-
-
static struct file_operations dev_fops = {
-
-
.owner = THIS_MODULE,
-
-
.ioctl = te2440_gpio_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;
- for (i = 0; i < 4; i++)
-
{
-
s3c2410_gpio_cfgpin(gpio_table[i], gpio_cfg_table[i]);
-
-
s3c2410_gpio_setpin(gpio_table[i], 0);
-
-
}
-
ret = misc_register(&misc);
- printk (DEVICE_NAME" initialized\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("");
-
-
MODULE_DESCRIPTION("GPIO control for Topelec TE2440DEV_I Board");
2. 修改 Kconfig Makefile
修改 drivers/ywx-driver/Kconfig
- config LED
-
tristate "led test "
-
help
-
this is led test
修改 drivers/ywx-driver/Makefile
- obj-$(CONFIG_HELLO) += hello.o
-
obj-$(CONFIG_LED) += led.o
3.编译 make menuconfig,
make zImage
下载到开发板中
4.生成 .ko文件 make SUBDIR=drivers/ywx-driver/ modules
在 dirvers/ywx-driver/ 生成 led.ko 文件,复制到开发板 /lib/mouldes/2.6.33-yuweixian/
5. 编写 应用程序,测试驱动 可用否 应用程序 是 tq 提供的代码 leds.c
arm-linux-gcc -o leds leds.c
cp leds /mnt/hgfs/share-linux/down/leds-application
-
-
#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 > 1 || led_no < 1 || led_no > 4) {
-
fprintf(stderr, "Usage: leds led_no 0|1\n");
-
exit(1);
-
}
-
fd = open("/dev/led-control", 0);
-
if (fd < 0) {
-
perror("open device leds");
-
exit(1);
-
}
-
ioctl(fd, on, (led_no-1));
-
close(fd);
-
return 0;
-
}
6.测试 - [root@yuweixian 2.6.33-yuweixian]# ls
-
hello.ko led.ko leds-applicaiton modules.dep.bb
-
[root@yuweixian 2.6.33-yuweixian]# insmod led.ko
-
led-control initialized
-
[root@yuweixian 2.6.33-yuweixian]# lsmod
-
led 881 0 - Live 0xbf00c000
-
[root@yuweixian 2.6.33-yuweixian]# ./leds-applicaiton 2 0
-
[root@yuweixian 2.6.33-yuweixian]# ./leds-applicaiton 3 0
-
[root@yuweixian 2.6.33-yuweixian]# ./leds-applicaiton 3 1
-
[root@yuweixian 2.6.33-yuweixian]# rmmod led
-
[root@yuweixian 2.6.33-yuweixian]# lsmod
-
[root@yuweixian 2.6.33-yuweixian]#
阅读(1588) | 评论(0) | 转发(0) |