❤️imx6ull按键的唤醒与休眠升级版,通过socket与外界通信❤️

运行效果

通过按键控制灯的亮灭,并通过网络编程把按键的状态传到外界,驱动代码运用到了中断,还有唤醒与休眠机制,当没有数据产生时,app会进入到休眠状态,直到按键产生了数据,这个时候就会唤醒app。可减少cpu的工作效率

驱动代码

#include <linux/module.h>

#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/mutex.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/stat.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/kmod.h>
#include <linux/gfp.h>
#include <linux/gpio/consumer.h>
#include <linux/platform_device.h>
#include <linux/of_gpio.h>
#include <linux/of_irq.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/slab.h>

struct gpio_key
{
	int gpio;
	struct gpio_desc *gpiod;//gpio_desc下面有各种参数,可以配置引脚的输入输出功能等等
	int flag;//引脚的flag
	int irq;//引脚的中断
};
static struct gpio_key *ask_button;

/* 主设备号                                                                 */
static int major = 0;
//创建结点
static struct class *gpio_key_class;
static int g_key = 0;
static DECLARE_WAIT_QUEUE_HEAD(gpio_key_wait);//声明一个工作队列的work头为gpio_key_wait

/* 实现对应的open/read/write等函数,填入file_operations结构体                   */
static ssize_t gpio_key_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset)
{
	//printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	int err;
	
	wait_event_interruptible(gpio_key_wait, g_key);//如果g_key为真则睡眠
	err = copy_to_user(buf, &g_key, 4);
	g_key = 0;
	
	return 4;
}


/* 定义自己的file_operations结构体                                              */
static struct file_operations gpio_key_drv = {
	.owner	 = THIS_MODULE,
	.read    = gpio_key_drv_read,
};

static irqreturn_t button_intr(int irq, void *dev_id)//个人认为 void*dev_id这个参数其实就是request_irq的第五个参数
{
	struct gpio_key *gpio_key = dev_id;
	int val;
	static int state=0;
		val= gpiod_get_value(gpio_key->gpiod);//获得gpio对应引脚的逻辑值
	printk("key:%d %d",gpio_key->gpio,val);//一个是gpio引脚号,应该是系统分配的,一个是逻辑值0||1;
	g_key = (gpio_key->gpio << 8) | val;//判断g_key的真假
	wake_up_interruptible(&gpio_key_wait);
//第一种灯亮的方式
/*	if(gpiod_get_value(gpio_key->gpiod)==1)
		{
	gpiod_set_value(ask_button[2].gpiod, 0);
		}
	else
		gpiod_set_value(ask_button[2].gpiod, 1);
	*/
//第二种灯亮的方式
	
	if (gpiod_get_value(gpio_key->gpiod) == 1)
	{
		if (state % 2 == 0)
		{
			gpiod_set_value(ask_button[2].gpiod, 0);
		}
		else
		{
			gpiod_set_value(ask_button[2].gpiod, 1);
		}
		state++;	
		 	}
    	   
	return IRQ_HANDLED;
}


int gpio_key_probe(struct platform_device *pdev)
{
	//获得节点
	struct device_node	*device_tree=pdev->dev.of_node;
	int count;
	int i;
	int err;
	unsigned flags = GPIOF_IN;
	enum of_gpio_flags flag;//保留疑问
	//unsigned flags = GPIOF_IN;
	count=of_gpio_count(device_tree);//获得该结点下有多少个gpio
	ask_button=kzalloc(sizeof(struct gpio_key)*3, GFP_KERNEL);//给这个结构体分配内存
	for(i=0;i<count-1;i++)
		{
		
		ask_button[i].gpio = of_get_gpio_flags(device_tree,i, &flag);//获得引脚的编号
		ask_button[i].gpiod = gpio_to_desc(ask_button[i].gpio);
		ask_button[i].flag= flag & OF_GPIO_ACTIVE_LOW;
		printk("flag:%d %d %d\n",flag,ask_button[i].flag,flags);
          
		if(flag & OF_GPIO_ACTIVE_LOW)
			flags |= GPIOF_ACTIVE_LOW;	
		printk("flags: %d  count:%d\n",flags,count);
		err = devm_gpio_request_one(&pdev->dev, ask_button[i].gpio, flags, NULL);
		ask_button[i].irq = gpio_to_irq(ask_button[i].gpio);//获得中断号
		}
	ask_button[2].gpio = of_get_gpio_flags(device_tree,i, &flag);//获得引脚的编号
	ask_button[2].gpiod = gpio_to_desc(ask_button[2].gpio);
	gpiod_direction_output( ask_button[2].gpiod, 0);//设置gpio3的方向   ,第二个参数为0应该是输出的意思

	for (i = 0; i < count-1; i++)
	{
		err = request_irq(ask_button[i].irq, button_intr, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, "100ask_gpio_key", &ask_button[i]);
	}
   /* 注册file_operations 	*/
	major = register_chrdev(0, "100ask_gpio_key", &gpio_key_drv);  /* /dev/100ask_gpio_key */

	gpio_key_class = class_create(THIS_MODULE, "100ask_gpio_key_class");
	if (IS_ERR(gpio_key_class)) {
		printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
		unregister_chrdev(major, "100ask_gpio_key");
		return PTR_ERR(gpio_key_class);
	}

	device_create(gpio_key_class, NULL, MKDEV(major, 0), NULL, "100ask_gpio_key"); /* /dev/100ask_gpio_key */    
	return 0;
}


int gpio_key_remove(struct platform_device *pdev)
{
	
	int count;
	int i;
	struct device_node	*device_tree=pdev->dev.of_node;
	device_destroy(gpio_key_class, MKDEV(major, 0));//销毁设备结点
	class_destroy(gpio_key_class);
	unregister_chrdev(major, "100ask_gpio_key");
	count=of_gpio_count(device_tree);
	for(i=0;i<count-1;i++)
		{
		free_irq(ask_button[i].irq,&ask_button[i]);
		}
		kfree(ask_button);
    return 0;	
}


//比较of_match_table中的compaile属性是否和设备树中的相等

static const struct of_device_id ask100_keys[] = {
    { .compatible = "100ask,gpio_key" },
    { },
};


struct platform_driver gpio_keys_driver={
	.probe      = gpio_key_probe,
    .remove     = gpio_key_remove,
	.driver={
		.name="100ask_gpio_key",
		.of_match_table=ask100_keys,
		}
};

static int __init gpio_key_init(void)
{
    int err;
    
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	
    err = platform_driver_register(&gpio_keys_driver); 
	
	return err;
}
static void gpio_key_exit(void)
{
	platform_driver_unregister(&gpio_keys_driver);
}

module_init(gpio_key_init);
module_exit(gpio_key_exit);

MODULE_LICENSE("GPL");




应用层代码


#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
/*
 * ./button_test /dev/100ask_button0
 *
 */
#define SERVER_PORT 8888
struct sockaddr_in tSocketServerAddr;
 int iSocketClient;
int Tcp_client_init(int argc,char **argv)
{	
	int iRet;
	unsigned char ucSendBuf[1000];
	int iSendLen;
	iSocketClient = socket(AF_INET, SOCK_STREAM, 0);

	tSocketServerAddr.sin_family      = AF_INET;
	tSocketServerAddr.sin_port        = htons(SERVER_PORT);  /* host to net, short */
 	//tSocketServerAddr.sin_addr.s_addr = INADDR_ANY;
 	if (0 == inet_aton(argv[1], &tSocketServerAddr.sin_addr))
 	{
		printf("invalid server_ip\n");
		return -1;
	}
	memset(tSocketServerAddr.sin_zero, 0, 8);


	iRet = connect(iSocketClient, (const struct sockaddr *)&tSocketServerAddr, sizeof(struct sockaddr));	
	if (-1 == iRet)
	{
		printf("connect error!\n");
		return -1;
	}
}
int main(int argc, char **argv)
{
	int fd;
	int val;

    Tcp_client_init(argc, argv);
    /* 1. 判断参数 */
	if (argc != 3) 
	{
		printf("Usage: %s <dev>\n", argv[0]);
		return -1;
	}

	/* 2. 打开文件 */
	fd = open(argv[2], O_RDWR);
	if (fd == -1)
	{
		printf("can not open file %s\n", argv[1]);
		return -1;
	}

	while (1)
	{
        int siSendLen;
        char *buf=(char *)malloc(10);
        /* 3. 读文件 */
		read(fd, &val, 4);
        sprintf(buf, "0x%x", val);
        siSendLen = send(iSocketClient, buf, strlen(buf), 0);
		printf("%d,get button : 0x%x\n", siSendLen,val);
	}
	
	close(fd);
	
	return 0;
}



Makefile代码


# 1. 使用不同的开发板内核时, 一定要修改KERN_DIR
# 2. KERN_DIR中的内核要事先配置、编译, 为了能编译内核, 要先设置下列环境变量:
# 2.1 ARCH,          比如: export ARCH=arm64
# 2.2 CROSS_COMPILE, 比如: export CROSS_COMPILE=aarch64-linux-gnu-
# 2.3 PATH,          比如: export PATH=$PATH:/home/book/100ask_roc-rk3399-pc/ToolChain-6.3.1/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin 
# 注意: 不同的开发板不同的编译器上述3个环境变量不一定相同,
#       请参考各开发板的高级用户使用手册

KERN_DIR =/home/book/100ask_imx6ull-sdk/Linux-4.9.88/

all:
	make -C $(KERN_DIR) M=`pwd` modules 
	$(CROSS_COMPILE)gcc -o button button_test.c
clean:
	make -C $(KERN_DIR) M=`pwd` modules clean
	rm -rf modules.order

# 参考内核源码drivers/char/ipmi/Makefile
# 要想把a.c, b.c编译成ab.ko, 可以这样指定:
# ab-y := a.o b.o
# obj-m += ab.o



obj-m +=mygpio_key_drv.o


服务器代码

#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>


/* socket
 * bind
 * listen
 * accept
 * send/recv
 */

#define SERVER_PORT 8888
#define BACKLOG     10

int main(int argc, char **argv)
{
	int iSocketServer;
	int iSocketClient;
	struct sockaddr_in tSocketServerAddr;
	struct sockaddr_in tSocketClientAddr;
	int iRet;
	int iAddrLen;
    
	int iRecvLen;
	unsigned char ucRecvBuf[1000];

	int iClientNum = -1;
	//信号
	signal(SIGCHLD,SIG_IGN);
	//获得一个套接字
	iSocketServer = socket(AF_INET, SOCK_STREAM, 0);
	if (-1 == iSocketServer)
	{
		printf("socket error!\n");
		return -1;
	}

	tSocketServerAddr.sin_family      = AF_INET;
	tSocketServerAddr.sin_port        = htons(SERVER_PORT);  /* host to net, short */
 	tSocketServerAddr.sin_addr.s_addr = INADDR_ANY;
	memset(tSocketServerAddr.sin_zero, 0, 8);
	
	iRet = bind(iSocketServer, (const struct sockaddr *)&tSocketServerAddr, sizeof(struct sockaddr));
	if (-1 == iRet)
	{
		printf("bind error!\n");
		return -1;
	}

	iRet = listen(iSocketServer, BACKLOG);
	if (-1 == iRet)
	{
		printf("listen error!\n");
		return -1;
	}

	while (1)
	{
		iAddrLen = sizeof(struct sockaddr);
		iSocketClient = accept(iSocketServer, (struct sockaddr *)&tSocketClientAddr, &iAddrLen);
		if (-1 != iSocketClient)
		{
			iClientNum++;
			printf("Get connect from client %d : %s\n",  iClientNum, inet_ntoa(tSocketClientAddr.sin_addr));
			if (!fork())
			{
				/* 子进程的源码 */
				while (1)
				{
					/* 接收客户端发来的数据并显示出来 */
					iRecvLen = recv(iSocketClient, ucRecvBuf, 999, 0);
					if (iRecvLen <= 0)
					{
						close(iSocketClient);
						return -1;
					}
					else
					{
						ucRecvBuf[iRecvLen] = '\0';
						printf("Get Msg From Client %d: %s\n", iClientNum, ucRecvBuf);
					}
				}				
			}
		}
	}
	
	close(iSocketServer);
	return 0;
}



开发步骤

  1. 先执行make程序生成.ko文件和button应用层文件
  2. 拷贝到挂载目录下
  3. insmod mygpio_key_drv.ko
  4. ./button 10.36.145.122 /dev/100ask_gpio_key
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

魔动山霸

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值