• linux gpio喂狗驱动


    0. linux版本

    Linux version 5.4.31
    
    • 1

    1.设备树

    usr_wtd {
                    compatible = "user-watchdog";
                    status = "okay";
                    gpio = <&gpiof 3 GPIO_ACTIVE_HIGH>;
                    pluse-width = <100>; /* 1ms */
                    pluse-inverse = <1>;
                    pluse-count = <3>;
                    timeout-sec = <5>;
            };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    2. 驱动 drivers/watchdog/usr_wtd.c

    #include 
    #include  
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    #include 
    
    
    /*
    #include   
    
    第一类延时函数原型是:(忙等)
    void ndelay(unsigned long nsecs);
    void udelay(unsigned long usecs);
    void mdelay(unsigned long msecs); 
    说明:内核函数 ndelay, udelay, 以及 mdelay 对于短延时好用, 分别延后执行指定的纳秒数, 微秒数或者毫秒数. 它们涉及到的延时常常是最多几个毫秒。
    
    第二类延时函数原型是:(使进程进入休眠)
    void msleep(unsigned int millisecs);
    unsigned long msleep_interruptible(unsigned int millisecs);
    void ssleep(unsigned int seconds) 
    */
    #define WATCHDOG_TAG "[wtd_tag]"
    #define WATCHDOG_MAJOR 0
    #define WATCHDOG_NAME "usr_wdt" //设备节点
    
    struct wtd_device_t {
    	int gpio;
    	int pluse_width;
    	int pluse_count;
    	int timeout_sec;
    	int pluse_inverse;
    	int status;
    	
    	struct timer_list timer;
    	struct class *wtd_class;
    	int major;
    };
    
    static struct wtd_device_t watchdog_dev;
    
    static void feed_dog(struct wtd_device_t *dev)
    {
    	int i = 0;
    	//printk(WATCHDOG_TAG"feed dog,count:%d,gpio:%d,width:%d\n",dev->pluse_count,dev->gpio,dev->pluse_width);
    	for(i=0;i<dev->pluse_count;i++){
    		gpio_direction_output(dev->gpio,dev->pluse_inverse);
    		mdelay(dev->pluse_width);
    		gpio_direction_output(dev->gpio,!dev->pluse_inverse);
    		mdelay(dev->pluse_width);
    	}
    	gpio_direction_output(dev->gpio,dev->pluse_inverse);
    }
    
    static void timer_function(struct timer_list *t)
    {
    	struct wtd_device_t *wtd = from_timer(wtd, t, timer);
    
    	feed_dog(wtd);
    	mod_timer(&wtd->timer, jiffies + wtd->timeout_sec * HZ); 
    } 
    
    static int watchdog_pin_config(int gpio)
    {
    	int ret = 0;
    
    	gpio_free(gpio);
    	ret = gpio_request(gpio,"watchdog_pin");
    
    	if(ret){
    		printk(WATCHDOG_TAG"%s::request gpio[%d] failed\n",__FUNCTION__,gpio);
    		return -1;
    	}else{
    		printk(WATCHDOG_TAG"%s::request gpio[%d] successfully\n",__FUNCTION__,gpio);	
    	}
    	return 0;
    }
    
    static int init_wtd_timer(struct wtd_device_t *dev)
    {
    	timer_setup(&dev->timer,timer_function,0);
    	mod_timer(&dev->timer, jiffies + dev->timeout_sec * HZ);
    
    	return 0 ;
    }
    
    static int watchdog_open(struct inode* inode,struct file* file)
    {
    	return 0;
    }
    
    static int watchdog_release(struct inode* inode ,struct file* file)
    {
    	return 0;
    }
    
    #if 0
    static int watchdog_ioctl(struct inode* inode, struct file* file,unsigned int cmd, unsigned long arg)
    {
    	return 0;
    }
    #endif
    
    static int watchdog_read(struct file *file, char __user *buf, size_t count, loff_t *offset) 
    {
    	return 0;
    }
    
    static int watchdog_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
    {
    	return 0;
    }
    
    static struct  file_operations watchdog_fops = 
    {
    	.owner = THIS_MODULE,
    	.open  = watchdog_open,
    	.release = watchdog_release,
    	.write = watchdog_write,
    	.read  = watchdog_read,
    };
    
    		
    static int user_wtd_probe(struct platform_device * pdev)
    {
    	struct device_node * node = pdev->dev.of_node;
    	int ret;
    	int num = 0;
    	int gpio = 0;
    	enum of_gpio_flags flags;
    	struct wtd_device_t *dev = &watchdog_dev;
    
    	if(!node)
    		return -ENODEV;
    	
    	dev->major = register_chrdev(WATCHDOG_MAJOR,WATCHDOG_NAME,&watchdog_fops);
    	if(dev->major <0){
    		printk(WATCHDOG_TAG" register failed \n");
    		return dev->major;
    	}
    	
    	dev->wtd_class = class_create(THIS_MODULE,WATCHDOG_NAME);
    	if(IS_ERR(dev->wtd_class)){
    		printk(WATCHDOG_TAG " register class failed \n");
    		return -1;
    	}
    	device_create(dev->wtd_class,NULL,MKDEV(dev->major,0),NULL,WATCHDOG_NAME);
    	
    	/* 
    	usr_wtd {
    		compatible = "user-watchdog";
    		status = "okay";
    		gpio = <&gpioh 15 GPIO_ACTIVE_HIGH>;
    		pluse-inverse = <1>;
    		pluse-width = <1>; 
    		pluse-count = <2>;
    		timeout-sec = <32>;
    	};
    	*/
    	ret = of_property_read_u32(node, "pluse-inverse", &num);
            if (ret) {
                    pr_err(WATCHDOG_TAG"pluse-inverse no found !!!\n");
                    goto INIT_ERR_FREE;
            }
            printk("pluse-inverse:%d\n",num);
            dev->pluse_inverse = num;
    
    	ret = of_property_read_u32(node, "pluse-width", &num);
    	if (ret || !num) {
    		pr_err(WATCHDOG_TAG"pluse-width no found !!!\n");
    		goto INIT_ERR_FREE;
    	}
    	printk("pluse-width:%d\n",num);
    	dev->pluse_width = num;
    	
    	ret = of_property_read_u32(node, "pluse-count", &num);
    	if (ret || !num) {
    		pr_err(WATCHDOG_TAG"pluse-count no found !!!\n");
    		goto INIT_ERR_FREE;
    	}
    	printk("pluse-count:%d\n",num);
    	dev->pluse_count = num;
    	
    	ret = of_property_read_u32(node, "timeout-sec", &num);
    	if (ret || !num) {
    		pr_err(WATCHDOG_TAG"timeout-sec no found !!!\n");
    		goto INIT_ERR_FREE;
    	}
    	printk("timeout-sec:%d\n",num);
    	dev->timeout_sec = num;
    	
    	gpio = of_get_named_gpio_flags(node, "gpio", 0,&flags);
    	dev->gpio = gpio;
    	ret = watchdog_pin_config(gpio);
    	if (ret) {
    		goto INIT_ERR_FREE;
    	}
    
    	init_wtd_timer(dev);
    	
    	dev_set_drvdata(&pdev->dev, dev);
    	pr_info("init watchdog dev finish\n");
    	return 0;
    
    INIT_ERR_FREE:
    	pr_err("init watchdog dev err\n");
    	return -1;
    }
    
    static int user_wtd_remove(struct platform_device *pdev)
    {
    	struct wtd_device_t * dev = dev_get_drvdata(&pdev->dev);
    
    	if(dev != NULL){
    		del_timer(&dev->timer);
    		unregister_chrdev(dev->major,WATCHDOG_NAME);
    		device_destroy(dev->wtd_class,MKDEV(dev->major ,0));
    		class_destroy(dev->wtd_class);
    	}
    	pr_info("exit user watchdog \n");
    	return 0;
    }
    
    #ifdef CONFIG_PM
    static int user_wtd_suspend(struct device *dev)
    {
    	return 0;
    }
    
    static int user_wtd_resume(struct device *dev)
    {
    	return 0;
    }
    #else
    #define user_wtd_suspend NULL
    #define user_wtd_resume NULL
    #endif
    
    static const struct dev_pm_ops user_wtd_pm_ops = {
    	.suspend = user_wtd_suspend,
    	.resume = user_wtd_resume,
    };
    
    static struct of_device_id user_wtd_of_match[] = {
    	{ .compatible = "user-watchdog" },
    	{},
    };
    MODULE_DEVICE_TABLE(of, user_wtd_of_match);
    
    static struct platform_driver user_wtd_driver = {
    	.driver		= {
    		.name	= "user-wtd",
    		.owner	= THIS_MODULE,
    		.pm	= &user_wtd_pm_ops,
    		.of_match_table	= of_match_ptr(user_wtd_of_match),
    	},
    	.probe		= user_wtd_probe,
    	.remove		= user_wtd_remove,
    };
    module_platform_driver(user_wtd_driver);
    
    MODULE_DESCRIPTION("user watchdog driver");
    MODULE_AUTHOR("zhengweiqing, 1548889230@qq.com");
    MODULE_LICENSE("GPL");
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280

    3. drivers/watchdog/Makefile

    末尾增加:

    obj-m += usr_wtd.o
    
    • 1

    4. 编译

    5. 使用

    insmod usr_wtd.ko
    
    • 1

    6. 现象

    usr_wtd {
                    compatible = "user-watchdog";
                    status = "okay";
                    gpio = <&gpiof 3 GPIO_ACTIVE_HIGH>;
                    pluse-width = <100>;
                    pluse-inverse = <1>;
                    pluse-count = <3>;
                    timeout-sec = <5>;
            };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    timeout-sec:周期,单位秒
    pluse-inverse:脉冲是否反向
    pluse-count:脉冲个数
    pluse-width:脉冲宽度,单位毫秒

    这里举例的是:每隔5秒有3个宽度为100毫秒的脉冲。

  • 相关阅读:
    阿里云产品试用系列-函数计算 FC
    12. 机器学习 - 拟合
    【数据结构】—— Trie/并查集/堆
    最护眼的灯是白炽灯吗?专业的护眼台灯推荐
    JAVA多线程基础篇-关键字synchronized
    java版工程管理系统Spring Cloud+Spring Boot+Mybatis实现工程管理系统源码
    定时任务报警通知解决方案详解
    适配器模式【结构型模式C++】
    用HTML5实现动画
    Converting HTML to PDF in .NET 7 Crack
  • 原文地址:https://blog.csdn.net/weixin_43479963/article/details/126329382