linux 如何通过读取GPIO状态来实现失电检测

    之前都是在linux上开发应用和后台服务,这次有一个任务需要检测linux开发板的失电情况,并做一定的保护,防止文件读写过程中掉电导致数据丢失。开发板是公司自己画的,失电检测的硬件原理比较简单,就是通过一个输入模式的gpio口来实现,失电时输入高电平,正常时输入低电平。

    本文主要是用来记录linux下读取gpio状态的两种方式。

    第一种,驱动模式,在自己写的驱动捕获gpio的中断,然后在中断里发送信号通知应用程序失电发生。关键点就是要找到GPIO_NUM,也就是失电信号输入的gpio号,查看手册一般都能找到引脚定义,本例中就是将一个空闲的引脚

X_GPIO_1_30用来作为失电信号的输入。由于核心板采用的是AM355X,GPIO的计算公式为(32 * (bank) + (gpio)),所以num=62。

驱动代码

#include <linux/module.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/cdev.h>
#include <linux/errno.h>
#include <linux/sched.h>
#include <linux/device.h>
#include <asm/io.h>
#include <asm/switch_to.h>
#include <asm/uaccess.h>

#include <asm/gpio.h>    
#include <linux/interrupt.h>
#include <linux/irq.h>

#define simple_MAJOR 200
 
static struct class *my_class;
int major;
static struct fasync_struct *fasync_queue; //异步通知队列

/*关键点!!!*/
#define GPIO_NUM 62  //假设中断引脚为:GPIO1_30
static unsigned int irq_num;
										  


/* 打开 */
int simple_open(struct inode *inode,struct file *filp){
	return 0;

}

/* 关闭 */
int simple_release(struct inode *inode,struct file *filp){
	return 0;
}

/*
简介:系统调用,应用层的read()将会调用该函数 
参数:buf: 数据传输目的地址
      count:传输数据长度
      f_pos:偏移量
*/
ssize_t simple_read(struct file *filp,char __user *buf,size_t count,loff_t *f_pos){

	return count;
}
 
/*
简介:系统调用,应用层的write()将会调用该函数 
参数:buf: 数据传输源地址
      count:传输数据长度
      f_pos:偏移量
*/ 
ssize_t simple_write(struct file *file,const char __user *buf,size_t count,loff_t *f_pos){

	return count;
}

/*fasync方法的实现*/
static int my_fasync(int fd, struct file * filp, int on)
{
    int retval;
    retval=fasync_helper(fd,filp,on,&fasync_queue);
    /*将该设备登记到fasync_queue队列中去*/
    if(retval<0)
      return retval;
    return 0;
}

/* 设备驱动操作结构体,该结构体的每一个成员的名字都对应着一个系统调用 */  
static const struct file_operations simple_fops={
	.owner=THIS_MODULE,
	.open=simple_open,
	.release=simple_release,
    .read=simple_read,
	.write=simple_write,
    .fasync=my_fasync,
};

/* 在中断服务函数中向应用层发送消息-异步通知! */ 
irqreturn_t irq_callback (int irqno, void *dev_id){
	printk("driver irq work !!!\n");
	if (fasync_queue) {
        kill_fasync(&fasync_queue, SIGIO, POLL_IN);
    }
	return IRQ_HANDLED;
}

/* 加载 */
int simple_init_module(void){
	int rtn;
	
	/* 注册相应的设备驱动 */ 
	major = register_chrdev(0,"my_device",&simple_fops);
	if(major<0){
		printk("Unable to register character device %d!/n",major);
		return major;
	}

	/* 自动创建设备节点 */ 
	my_class = class_create(THIS_MODULE, "my_class");
	device_create(my_class, NULL, MKDEV(major, 0), NULL,"my_device");
	
	/*gpio申请*/
	rtn = gpio_request(GPIO_NUM, "my_irq");
	if(rtn!=0){
		printk("my_irq irq pin request io failed.\n");
	}
	rtn = gpio_direction_input(GPIO_NUM); 
	if(rtn<0){
		printk("gpio_direction_input() failed !\n");  
	}
	/*获取gpio中断号*/
	irq_num = gpio_to_irq(GPIO_NUM);
	
	/*GPIO中断服务函数注册,*/                    /*上升沿触发*/               
	rtn = request_irq(irq_num, irq_callback,IRQF_TRIGGER_RISING,"my_irq", NULL);
	if (rtn<0) {
		printk("my_irq request irq false\n");
	} else {
		printk("my_irq request irq success: %d\n",irq_num);
	}
	
    printk("module_init sucessful!!!\n");
	return 0;
}

/* 卸载 */ 
void simple_cleanup_module(void){
	/* 卸载相应的设备驱动 */ 
	unregister_chrdev(major,"my_device");     
	device_destroy(my_class,MKDEV(major, 0));
	class_destroy(my_class);
	
	/*释放GPIO*/
	gpio_free(GPIO_NUM);
	

    printk("module_exit sucessful!!!\n");
}

/* 宏实现 */
module_init(simple_init_module);
module_exit(simple_cleanup_module);

/* 开源许可声明 */  
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Zhang"); 
 



应用代码

#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>

static int fd;

/* 内核产生异步通知,调用该应用层函数处理 */
void sigterm_handler(int signo)
{
	printf("app irq work !!!\n");
}

int main(void)
 {
    int oflags;

    fd=open("/dev/my_device",O_RDWR);  //打开设备文件
	
    /* 启动异步通知信号驱动机制 */
    signal(SIGIO, sigterm_handler);
    fcntl(fd, F_SETOWN, getpid());
    oflags = fcntl(fd, F_GETFL);
    fcntl(fd, F_SETFL, oflags | FASYNC);
	
    /*建立一个死循环,防止程序结束 */
    while(1)
    {
        printf("sleep\n");
        usleep(200000);  //2ms
    }
	
    close(fd);
    return 0;
 }


第二种,通过sysfs读取GPIO状态。此方法需要在内核中使能/sys/class/gpio。

本例中读取GPIO3的状态,首先是配置gpio3,输入模式,上升沿触发

echo 3 >/sys/class/gpio/export

echo in > /sys/class/gpio/gpio3/direction

echo rising  > /sys/class/gpio/gpio3/edge

然后通过poll的方式获取到失电状态

#define GPIO_FILENAME "/sys/class/gpio/gpio3/value"

void* check_gpio_of_poweroff(void *args)
{
    int fd = open(GPIO_FILENAME, O_RDONLY);
    if (fd < 0)
    {
        perror("open failed!\n");
        return NULL;
    }
    struct pollfd fds[1];
    fds[0].fd = fd;
    fds[0].events = POLLPRI;
    while (1)
    {
        if (poll(fds, 1, 0) == -1)
        {
            perror("poll failed!\n");
            return NULL;
        }
        if (fds[0].revents & POLLPRI)
        {
            if (lseek(fd, 0, SEEK_SET) == -1)
            {
                perror("lseek failed!\n");
                return NULL;
            }
            char buffer[16];
            int len;
            if ((len = read(fd, buffer, sizeof (buffer))) == -1)
            {
                perror("read failed!\n");
                return NULL;
            }
            buffer[len] = 0;
            //            printf("警告!!断电检测引脚状态=%d\n", atoi(buffer));
            if (atoi(buffer) == 1)
            {
                JG_LOG("警告!!断电引脚触发\n");
                pthread_mutex_lock(&filelock);
                break;
            }
        }
    }
}

参考自以下链接:

https://blog.csdn.net/weixin_39917818/article/details/102613690

https://www.cnblogs.com/lxyd/p/9671673.html

  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值