上面是我的微信和QQ群,欢迎新朋友的加入。
目录
1.安装QT CREATOR
sudo apt-get install qt5-default qtcreator
2.硬件连接
照片看不太清,对照丝印和原理图,LED连接在GPIO3 PIN26上
在内核源码里面引脚定义是PINID_SAIF1_SDATA0
3.驱动源码
驱动是官方例程,直接改个IO口就行
/*
GPIO Driver driver for EasyARM-iMX283
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/ioctl.h>
#include <linux/delay.h>
#include <linux/bcd.h>
#include <linux/capability.h>
#include <linux/rtc.h>
#include <linux/cdev.h>
#include <linux/miscdevice.h>
#include <linux/gpio.h>
#include <../arch/arm/mach-mx28/mx28_pins.h>
#define DEVICE_NAME "imx28x_oled"
#define LED_GPIO MXS_PIN_TO_GPIO(PINID_SAIF1_SDATA0) //for 283 287A/B
/*--------------------------------------------------------------------------------------------------------
*/
static int gpio_open(struct inode *inode, struct file *filp);
static int gpio_release(struct inode *inode, struct file *filp);
ssize_t gpio_write(struct file *filp, const char __user *buf, size_t count,
loff_t *f_pos);
static int gpio_ioctl(struct inode *inode,struct file *flip,unsigned int command,unsigned long arg);
static int gpio_init(void);
static void gpio_exit(void);
/*--------------------------------------------------------------------------------------------------------
*/
static int gpio_open(struct inode *inode, struct file *filp)
{
gpio_request(LED_GPIO, "led");
return 0;
}
static int gpio_release(struct inode *inode, struct file *filp)
{
gpio_free(LED_GPIO);
return 0;
}
ssize_t gpio_write(struct file *filp, const char __user *buf, size_t count,
loff_t *f_pos)
{
char data[2];
copy_from_user(data, buf, count);
gpio_direction_output(LED_GPIO, data[0]);
return count;
}
static int gpio_ioctl(struct inode *inode,struct file *flip,unsigned int command,unsigned long arg)
{
int data;
switch (command) {
case 0:
gpio_direction_output(LED_GPIO, 0);
break;
case 1:
gpio_direction_output(LED_GPIO, 1);
break;
}
return 0;
}
static struct file_operations gpio_fops={
.owner = THIS_MODULE,
.open = gpio_open,
.write = gpio_write,
.release = gpio_release,
.ioctl = gpio_ioctl,
};
static struct miscdevice gpio_miscdev = {
.minor = MISC_DYNAMIC_MINOR,
.name = DEVICE_NAME,
.fops = &gpio_fops,
};
static int __init gpio_init(void)
{
misc_register(&gpio_miscdev);
printk(DEVICE_NAME" up. \n");
return 0;
}
static void __exit gpio_exit(void)
{
misc_deregister(&gpio_miscdev);
printk(DEVICE_NAME " down.\n");
}
module_init(gpio_init);
module_exit(gpio_exit);
MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("jun");
MODULE_DESCRIPTION("GPIO DRIVER FOR EasyARM-i.MX28xx");
4.编译驱动/测试驱动
测试代码
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <limits.h>
#include <asm/ioctls.h>
#include <time.h>
#include <pthread.h>
int main(void)
{
int fd;
char buf[1] = {0};
fd = open("/dev/imx28x_led", O_RDWR);
if (fd < 0) {
perror("open /dev/imx283_led");
}
printf("test write....\n");
buf[0] = 0;
write(fd, buf, 1);
sleep(2);
buf[0] = 1;
write(fd, buf, 1);
sleep(1);
printf("test ioctl..... \n");
ioctl(fd, 0);
sleep(2);
ioctl(fd, 1);
}
编译测试代码
将代码拷贝到设备
可以看到设备上LED在翻转
5.创建QT界面
三个按键,每个按键实现一个功能
void MainWindow::on_pushButton_clicked()
{
QProcess *process = new QProcess();
process->start("sh led_insmod.sh");
}
void MainWindow::on_pushButton_2_clicked()
{
QProcess *process = new QProcess();
process->start("sh led_rmmod.sh");
}
void MainWindow::on_pushButton_3_clicked()
{
QProcess *process = new QProcess();
process->start("./led_test");
}
这里我创建了两个shell,其实没这么麻烦,直接输入命令就行,这里是为了测试qt调用shell
6.编译QT
下载到设备
7.运行
点击按键,实现驱动加载,运行,卸载