mtk7688openwrt平台下实现完整GPIO驱动和应用开发
[复制链接]
mtk7688openwrt平台下实现完整GPIO驱动和应用开发
在上一篇写的mtk7688的gpio应用开发中,使用的是gcc交叉编译后好的二进制通过scp传输到开发板上面运行的,没有使用到openwrt平台下特有的opkg安装软件包。这篇主要是讲解GPIO在openwrt平台下完整流程,涉及到驱动应用的开发以及makefile的编写,makefile 有两份一份是编译成二进制文件,另外一份是涉及到软件和openwrt之间的组织关系,尤其细节太多,我也就不细说,感兴趣自己多百度看下。
SEC灯驱动代码,这个可以作为一份不错的模板,只需要修改涉及到具体GPIO操作的地方就行,因为GPIO11就是GPIO功能,没有复用,所以不需要配置功能模式,我把修改的代码红色标注。
/*****************************
*
* 驱动程序模板
* 版本:V1
* 使用方法(末行模式下):
* :%s/mydrv/"你的驱动名称"/g
*
*******************************/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MYLEDS_LED1_ON 0
#define MYLEDS_LED1_OFF 1
volatile unsigned long *GPIOMODE;
volatile unsigned long *GPIO11_DIR;
volatile unsigned long *GPIO11_SETDATA;
volatile unsigned long *GPIO11_RESETDATA;
/**************** 基本定义 **********************/
//内核空间缓冲区定义
#if 0
#define KB_MAX_SIZE 20
#define kbuf[KB_MAX_SIZE];
#endif
//加密函数参数内容: _IOW(IOW_CHAR , IOW_NUMn , IOW_TYPE)
//加密函数用于mydrv_ioctl函数中
//使用举例:ioctl(fd , _IOW('L',0x80,long) , 0x1);
//#define NUMn mydrv , if you need!
#define IOW_CHAR 'L'
#define IOW_TYPE long
#define IOW_NUM1 0x80
//初始化函数必要资源定义
//用于初始化函数当中
//device number;
dev_t dev_num;
//struct dev
struct cdev mydrv_cdev;
//auto "mknode /dev/mydrv c dev_num minor_num"
struct class *mydrv_class = NULL;
struct device *mydrv_device = NULL;
/**************** 结构体 file_operations 成员函数 *****************/
//open
static int mydrv_open(struct inode *inode, struct file *file)
{
printk("mydrv drive open...\n");
*GPIO11_RESETDATA |=(1u<<11);
return 0;
}
//close
static int mydrv_close(struct inode *inode , struct file *file)
{
printk("mydrv drive close...\n");
return 0;
}
//read
static ssize_t mydrv_read(struct file *file, char __user *buffer,
size_t len, loff_t *pos)
{
int ret_v = 0;
printk("mydrv drive read...\n");
return ret_v;
}
//write
static ssize_t mydrv_write( struct file *file , const char __user *buffer,
size_t len , loff_t *offset )
{
int ret_v = 0;
printk("mydrv drive write...\n");
return ret_v;
}
//unlocked_ioctl
static int mydrv_ioctl (struct file *filp , unsigned int cmd , unsigned long arg)
{
int ret_v = 0;
printk("mydrv drive ioctl...\n");
switch(cmd)
{
//常规:
//cmd值自行进行修改
case MYLEDS_LED1_ON:
*GPIO11_RESETDATA |= (1u<<11);
break;
case MYLEDS_LED1_OFF:
*GPIO11_SETDATA |= (1u<<11);
break;
//带密码保护:
//请在"基本定义"进行必要的定义
case _IOW(IOW_CHAR,IOW_NUM1,IOW_TYPE):
{
if(arg == 0x1) //第二条件
{
}
}
break;
default:
break;
}
return ret_v;
}
/***************** 结构体: file_operations ************************/
//struct
static const struct file_operations mydrv_fops = {
.owner = THIS_MODULE,
.open = mydrv_open,
.release = mydrv_close,
.read = mydrv_read,
.write = mydrv_write,
.unlocked_ioctl = mydrv_ioctl,
};
/************* functions: init , exit*******************/
//条件值变量,用于指示资源是否正常使用
unsigned char init_flag = 0;
unsigned char add_code_flag = 0;
//init
static __init int mydrv_init(void)
{
int ret_v = 0;
printk("mydrv drive init...\n");
//函数alloc_chrdev_region主要参数说明:
//参数2: 次设备号
//参数3: 创建多少个设备
if( ( ret_v = alloc_chrdev_region(&dev_num,0,1,"mydrv") ) < 0 )
{
goto dev_reg_error;
}
init_flag = 1; //标示设备创建成功;
printk("The drive info of mydrv:\nmajor: %d\nminor: %d\n",
MAJOR(dev_num),MINOR(dev_num));
cdev_init(&mydrv_cdev,&mydrv_fops);
if( (ret_v = cdev_add(&mydrv_cdev,dev_num,1)) != 0 )
{
goto cdev_add_error;
}
mydrv_class = class_create(THIS_MODULE,"mydrv");
if( IS_ERR(mydrv_class) )
{
goto class_c_error;
}
mydrv_device = device_create(mydrv_class,NULL,dev_num,NULL,"mydrv");
if( IS_ERR(mydrv_device) )
{
goto device_c_error;
}
printk("auto mknod success!\n");
//------------ 请在此添加您的初始化程序 --------------//
//GPIOMODE=(volatile unsigned long *)ioremap(0x10000060,4);//gpio 复用时需要配置
GPIO11_DIR=(volatile unsigned long *)ioremap(0x10000600,4);
GPIO11_SETDATA=(volatile unsigned long *)ioremap(0x10000630,4);
GPIO11_RESETDATA=(volatile unsigned long *)ioremap(0x10000640,4);
//*GPIOMODE |= (1u<<11);
*GPIO11_DIR |= (1u<<11);
//如果需要做错误处理,请:goto mydrv_error;
add_code_flag = 1;
//---------------------- END ---------------------------//
goto init_success;
dev_reg_error:
printk("alloc_chrdev_region failed\n");
return ret_v;
cdev_add_error:
printk("cdev_add failed\n");
unregister_chrdev_region(dev_num, 1);
init_flag = 0;
return ret_v;
class_c_error:
printk("class_create failed\n");
cdev_del(&mydrv_cdev);
unregister_chrdev_region(dev_num, 1);
init_flag = 0;
return PTR_ERR(mydrv_class);
device_c_error:
printk("device_create failed\n");
cdev_del(&mydrv_cdev);
unregister_chrdev_region(dev_num, 1);
class_destroy(mydrv_class);
init_flag = 0;
return PTR_ERR(mydrv_device);
//------------------ 请在此添加您的错误处理内容 ----------------//
mydrv_error:
add_code_flag = 0;
return -1;
//-------------------- END -------------------//
init_success:
printk("mydrv init success!\n");
return 0;
}
//exit
static __exit void mydrv_exit(void)
{
printk("mydrv drive exit...\n");
if(add_code_flag == 1)
{
//---------- 请在这里释放您的程序占有的资源 ---------//
printk("free your resources...\n");
//iounmap(GPIOMODE);
iounmap(GPIO11_DIR);
iounmap(GPIO11_SETDATA);
iounmap(GPIO11_RESETDATA);
printk("free finish\n");
//---------------------- END -------------------//
}
if(init_flag == 1)
{
//释放初始化使用到的资源;
cdev_del(&mydrv_cdev);
unregister_chrdev_region(dev_num, 1);
device_unregister(mydrv_device);
class_destroy(mydrv_class);
}
}
/**************** module operations**********************/
//module loading
module_init(mydrv_init);
module_exit(mydrv_exit);
//some infomation
MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("from Jafy");
MODULE_DESCRIPTION("mydrv drive");
/********************* The End ***************************/
SEC应用开发软件
#include
#include
#include
#include
#include
#include
#include
#define MYLEDS_LED1_ON 0
#define MYLEDS_LED1_OFF 1
void print_usage(char *file)
{
printf("Usage:\n");
printf("%s \n",file);
printf("eg. \n");
printf("%s led1 on\n", file);
printf("%s led1 off\n", file);
printf("%s led2 on\n", file);
printf("%s led2 off\n", file);
}
int main(int argc , char** argv)
{
int fd;
if(argc != 3)
{
print_usage(argv[0]);
return 0;
}
fd = open("/dev/mydrv" , O_RDWR|O_NONBLOCK);
if(fd < 0)
{
printf("can't open /dev/mydrv\n");
return -1;
}
if(!strcmp("led1",argv[1]))
{
if(!strcmp("on", argv[2]))
{
ioctl(fd,MYLEDS_LED1_ON);
}
else if(!strcmp("off", argv[2]))
{
ioctl(fd,MYLEDS_LED1_OFF);
}
else
{
print_usage(argv[0]);
return 0;
}
}
else
{
print_usage(argv[0]);
return 0;
}
return 0;
}
其实我把怎么在openwrt平台下编译出ipk包给省略了,这个百度下很多,基本大同小异,makefile文件可以作为模板用,也不需要自己怎么修改,改下文件名字就行,运行结果如下:
root@Widora:/# opkg install mydrv_app_1_ramips_24kec.ipk
Installing mydrv_app (1) to root...
Configuring mydrv_app.
root@Widora:/# mydrv_app
Usage:
mydrv_app
eg.
mydrv_app led1 on
mydrv_app led1 off
mydrv_app led2 on
mydrv_app led2 off
root@Widora:/# mydrv_app[ 879.260000] WLAN:STA 04:79:70:90:da:dc(dev:ra0 rate:65Mbps RSSI:-13dBm) associated
Usage:
mydrv_app
eg.
mydrv_app led1 on
mydrv_app led1 off
mydrv_app led2 on
mydrv_app led2 off
root@Widora:/# mydrv_app led1 on
can't open /dev/mydrv
root@Widora:/# [ 914.250000] WLAN:STA 04:79:70:90:da:dc(dev:ra0 rate:13Mbps RSSI:-36dBm) associated
[ 914.250000] WLAN:STA 04:79:70:90:da:dc(dev:ra0 rate:13Mbps singnal:-42dBm) disconnect
root@Widora:/# mydrv_app led1 off
can't open /dev/mydrv
root@Widora:/# cd /lib/modules/3.18.29/
root@Widora:/lib/modules/3.18.29# insmod mydrv.ko
[ 975.370000] mydrv drive init...
[ 975.370000] The drive info of mydrv:
[ 975.370000] major: 252
[ 975.370000] minor: 0
[ 975.380000] auto mknod success!
[ 975.390000] mydrv init success!
root@Widora:/lib/modules/3.18.29# mydrv_app led1 on
[ 993.040000] mydrv drive open...
[ 993.040000] mydrv drive ioctl...
[ 993.050000] mydrv drive close...
root@Widora:/lib/modules/3.18.29# mydrv_app led1 off
[ 998.360000] mydrv drive open...
[ 998.370000] mydrv drive ioctl...
[ 998.370000] mydrv drive close...
root@Widora:/lib/modules/3.18.29# [ 1143.420000] WLAN:STA 18:5e:0f:e1:bf:36(dev:ra0 rate:65Mbps RSSI:-15dBm) associated
[ 1143.430000] WLAN:STA 18:5e:0f:e1:bf:36(dev:ra0 rate:65Mbps singnal:-23dBm) disconnect
root@Widora:/lib/modules/3.18.29# mydrv_app led1 on
[ 1208.450000] mydrv drive open...
[ 1208.450000] mydrv drive ioctl...
[ 1208.450000] mydrv drive close...
root@Widora:/lib/modules/3.18.29# mydrv_app led1 off
[ 1210.940000] mydrv drive open...
[ 1210.940000] mydrv drive ioctl...
[ 1210.950000] mydrv drive close...
root@Widora:/lib/modules/3.18.29# mydrv_app led1 on
[ 1606.190000] mydrv drive open...
[ 1606.190000] mydrv drive ioctl...
[ 1606.200000] mydrv drive close...
root@Widora:/lib/modules/3.18.29# mydrv_app led1 off
[ 1608.580000] mydrv drive open...
[ 1608.580000] mydrv drive ioctl...
[ 1608.580000] mydrv drive close...
这样就完整了实现了驱动的加载和调用相关驱动的应用开发了,相关资料见附件,其实一般常见的什么GPIO,串口很多时候在dts文件修改就可以了,sys/class文件下调用就行。这个我是采用了动态加载的方法尝试下,其实也可以连同内核一起编译,这样更方便,其他什么的传感器采集什么驱动都可以按照我上面的模块修改下就OK.不过个人觉得用MTK7688当MCU用,那真的是大材小用。上linux的话,其实我更多是应用到网络资源和各种开源应用软件,仅仅用来做驱动开发,得不偿失,还不如MCU+MTK7688模式,让MCU去做各种外设的操作,MTK7688做大型应用开发和网络处理,之间串口通信,如果外设不多的话,比如采集一个温湿度,那没问题,最后根据实际情况来确定了,我个人属于能用几毛钱解决的永远不会去用1块钱去解决,穷怕了,在几毛的片子上裸奔习惯了,一下用linux+这么高端的处理器不习惯了。
此内容由EEWORLD论坛网友wateras1 原创,如需转载或用于商业用途需征得作者同意并注明出处