大家好,在学习Qt4.7的移植时按照TQ6410_QT4.7移植手册的Led测试例程开发,目标板是TQ2440,使用内核EmbedSky-leds为设备文件,改变LED1 ——LED4的状态时开发板上的LED不受控制。


界面程序如下:

  1 #include "ledtest.h"
2 #include "ui_ledtest.h"
3
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include<string.h>
8 #include <sys/ioctl.h>
9 #include<stdio.h>
10
11 #include<QDirectPainter>
12
13 static int fb;
14 static int LED1=0, LED2=0,LED3=0,LED4=0;
15
16
17 ledtest::ledtest(QWidget *parent) :
18 QMainWindow(parent),
19 ui(new Ui::ledtest)
20 {
21 ui->setupUi(this);
22 int screenWidth=QDirectPainter::screenWidth();
23 int screenHeight=QDirectPainter::screenHeight();
24 this->resize(screenWidth,screenHeight);
25 connect(ui->checkBox_LED1,SIGNAL(toggled(bool)),this,SLOT(LED1_Toggle()));
26 connect(ui->checkBox_LED2,SIGNAL(toggled(bool)),this,SLOT(LED2_Toggle()));
27 connect(ui->checkBox_LED3,SIGNAL(toggled(bool)),this,SLOT(LED3_Toggle()));
28 connect(ui->checkBox_LED4,SIGNAL(toggled(bool)),this,SLOT(LED4_Toggle()));
29
30 system("/etc/rc.d/init.d/leds stop");
31 fb=open("/dev/EmbedSky-leds",O_RDWR);
32 if(fb<0)
33 {
34 perror("open device leds fail");
35 exit(1);
36 }
37 for (int i = 0 ; i < 4; i ++)
38 {
39 ioctl(fb, 0, i);
40 }
41
42
43 }
44
45 ledtest::~ledtest()
46 {
47 delete ui;
48 }
49
50 void ledtest::changeEvent(QEvent *e)
51 {
52 QMainWindow::changeEvent(e);
53 switch (e->type()) {
54 case QEvent:: LanguageChange:
55 ui->retranslateUi(this);
56 break;
57 default:
58 break;
59 }
60 }
61
62 void ledtest:: LED1_Toggle()
63 {
64 LED1=~LED1;
65 ui->textBrowser->setText("LED1_Toggle\n");
66 if(LED1==1)
67 {
68 ioctl(fb,1,0);
69 }
70 else
71 {
72 ioctl(fb,0,0);
73 }
74 }
75 void ledtest:: LED2_Toggle()
76 {
77 LED2=~LED2;
78 ui->textBrowser->setText("LED2_Toggle\n");
79 if(LED2==1)
80 {
81 ioctl(fb,1,1);
82 }
83 else
84 {
85 ioctl(fb,0,1);
86 }
87 }
88 void ledtest:: LED3_Toggle()
89 {
90 LED3=~LED3;
91 ui->textBrowser->setText("LED3_Toggle\n");
92 if(LED3==1)
93 {
94 ioctl(fb,1,2);
95 }
96 else
97 {
98 ioctl(fb,0,2);
99 }
100 }
101 void ledtest:: LED4_Toggle()
102 {
103 LED4=~LED4;
104 ui->textBrowser->setText("LED4_Toggle\n");
105 if(LED4==1)
106 {
107 ioctl(fb,1,3);
108 }
109 else
110 {
111 ioctl(fb,0,3);
112 }
113 }

LED驱动EmbedSky_leds.ko是通过TQ的linux-2.6.30.4中 drivers/char/EmbedSky_gpio.c修改为EmbedSky_leds.c编译而成(具体操作按天嵌科技出品-Linux移植之 Step By Step_V4.5_20100605的5.2节来完成的)源码为:

  1 /**************************************
2 NAME:EmbedSky_leds.c
3 COPYRIGHT:www.embedsky.net
4 *************************************/
5 #include <linux/miscdevice.h>
6 #include <linux/delay.h>
7 #include <asm/irq.h>
8 #include <mach/regs-gpio.h>
9 #include <mach/hardware.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/mm.h>
14 #include <linux/fs.h>
15 #include <linux/types.h>
16 #include <linux/delay.h>
17 #include <linux/moduleparam.h>
18 #include <linux/slab.h>
19 #include <linux/errno.h>
20 #include <linux/ioctl.h>
21 #include <linux/cdev.h>
22 #include <linux/string.h>
23 #include <linux/list.h>
24 #include <linux/pci.h>
25 #include <asm/uaccess.h>
26 #include <asm/atomic.h>
27 #include <asm/unistd.h>
28 #include <linux/device.h>
29
30
31 #define DEVICE_NAME "EmbedSky-leds" /* 加载模式后,执行”cat /proc/devices”命令看到的设备名称 */
32
33
34 #define LED_MAJOR 231 /* 主设备号 */
35
36 /* 应用程序执行 ioctl(fd, cmd, arg)时的第 2 个参数 */
37
38 #define IOCTL_LED_ON 1
39 #define IOCTL_LED_OFF 0
40 /* 用来指定 LED 所用的 GPIO 引脚 */
41 static unsigned long led_table [] =
42 {
43 S3C2410_GPB5,
44 S3C2410_GPB6,
45 S3C2410_GPB7,
46 S3C2410_GPB8,
47
48 };
49 /* 用来指定 GPIO 引脚的功能:输出 */
50 static unsigned int led_cfg_table [] =
51 {
52 S3C2410_GPB5_OUTP,
53 S3C2410_GPB6_OUTP,
54 S3C2410_GPB7_OUTP,
55 S3C2410_GPB8_OUTP,
56 };
57 /* 应用程序对设备文件/dev/EmbedSky-leds 执行 open(...)时,
58 * 就会调用 EmbedSky_leds_open 函数*/
59
60 static int EmbedSky_leds_open(struct inode *inode, struct file *file)
61 {
62
63 return 0;
64 }
65
66 /* 应用程序对设备文件/dev/EmbedSky-leds 执行 ioclt(...)时,
67 * 就会调用 EmbedSky_leds_ioctl 函数
68 */
69 static int EmbedSky_leds_ioctl( struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
70 {
71 if (arg > 4)
72 {
73 return -EINVAL;
74 }
75 switch(cmd)
76 {
77 case IOCTL_LED_ON:
78 // 设置指定引脚的输出电平为 0
79 s3c2410_gpio_setpin(led_table[arg], 0);
80 return 0;
81 case IOCTL_LED_OFF:
82 // 设置指定引脚的输出电平为 1
83 s3c2410_gpio_setpin(led_table[arg], 1);
84 return 0;
85 default:
86 return -EINVAL;
87 }
88 }
89 /* 这个结构是字符设备驱动程序的核心
90 * 当应用程序操作设备文件时所调用的 open、read、write 等函数,
91 * 最终会调用这个结构中指定的对应函数
92 */
93 static struct file_operations EmbedSky_leds_fops =
94 {
95 /* 这是一个宏,推向编译模块时自动创建的__this_module 变量 */
96 .owner = THIS_MODULE,
97 .open = EmbedSky_leds_open,
98 .ioctl = EmbedSky_leds_ioctl,
99 };
100 static char __initdata banner[] = "TQ2440/SKY2440 LEDS, (c) 2008,2009 www.embedsky.net\n";
101 static struct miscdevice misc = {
102 .minor = MISC_DYNAMIC_MINOR,
103 .name = DEVICE_NAME,
104 .fops = &EmbedSky_leds_fops,
105 };
106
107
108 /*
109 * 执行“insmod EmbedSky_leds.ko”命令时就会调用这个函数
110 */
111 static int __init EmbedSky_leds_init(void)
112 {
113 int ret;
114 int i;
115 for (i = 0; i < 4; i++)
116 {
117 s3c2410_gpio_cfgpin(led_table, led_cfg_table);
118 s3c2410_gpio_setpin(led_table, 0);
119 }
120 printk(banner);
121 ret = misc_register(&misc);
122 printk(DEVICE_NAME " initialized\n");
123 return 0;
124 }
125 /*
126 * 执行”rmmod EmbedSky_leds.ko”命令时就会调用这个函数
127 */
128 static void __exit EmbedSky_leds_exit(void)
129 {
130 misc_deregister(&misc); //注销类
131 }
132 /* 这两行指定驱动程序的初始化函数和卸载函数 */
133 module_init(EmbedSky_leds_init);
134 module_exit(EmbedSky_leds_exit);
135
136 MODULE_LICENSE("GPL");
137 MODULE_AUTHOR("EmbedSky");
138 MODULE_DESCRIPTION("LED control for EmbedSky SKY2440/TQ2440 Board");



生成EmbedSky_leds.ko后,复制到目标板,安装后在目标板的/dev下生成了EmbedSky-leds,在运行Qt LED测试程序LedTest,开发板上LED全灭,界面上改变LED1--LED4的状态,开发板上的LED灯没反应。
请高手指点!

-------------------------------

注释掉system("/etc/rc.d/init.d/leds stop");后,在板上LED灯量的时候,有控制效果。

---------------------------------

是的。另外要加载驱动

------------------------------------

您好版主,谢谢你的解答。我已经添加了EmbedSky_leds.ko文件并安装成功了,难道还要添加其它的驱动文件?注释掉system("/etc/rc.d/init.d/leds stop");后, 控制效果也不对。

----------------------------------------

问题解决了,应该是程序中的一些小bug,我重新整理了一遍程序就可以了。

------------------------------------------------

版主我也遇到这个问题,界面没问题!但是控制每反应啊!我和上面仁兄的代码一样!那还能是怎么回事呢?

-------------------------------------------------

问题解决了!就是驱动的控制灯亮灭的顺序要和QT的顺序一样才好用!就是程序的顺序问题!

posted on 2012-03-16 05:47  风行雪舞  阅读(1300)  评论(0编辑  收藏  举报
无觅相关文章插件,快速提升流量