在http://blog.csdn.net/weixin_40109283/article/details/79227038博客中已经介绍了基于DragonBoard 410c的自动浇花机的硬件设计,接下来介绍一下软件设计。
代码如下所示:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/sysfs.h>
#include <linux/delay.h>
#include <linux/platform_device.h>
#include <linux/err.h>
#include <linux/device.h>
#include <linux/of_gpio.h>
#include <asm/uaccess.h>
#include <linux/kdev_t.h>
#include <linux/slab.h>
#include <linux/timer.h>
#include <linux/jiffies.h>
struct water_pdata {
struct platform_device *pdev;
int machine_gpio;
int humidity_gpio1;
int humidity_gpio2;
int humidity_gpio3;
int irq;
struct timer_list watertime;
};
struct water_pdata* pdata;
static void water_flowers_func(unsigned long data)
{
int a, b, c;
printk(KERN_INFO "water_flowesr_func enter\n");
a = gpio_get_value(pdata->humidity_gpio1);
printk("humidity gpio1 = %d.\n", a);
b = gpio_get_value(pdata->humidity_gpio2);
printk("humidity gpio2 = %d.\n", b);
c = gpio_get_value(pdata->humidity_gpio3);
printk("humidity gpio3 = %d.\n", c);
if (a+b+c >= 2) {
gpio_set_value(pdata->machine_gpio, 1);
printk("watering flowers!\n");
}
else {
gpio_set_value(pdata->machine_gpio, 0);
printk("Not water flowers.\n");
}
mod_timer(&(pdata->watertime), jiffies + HZ);
}
static int water_probe(struct platform_device *pdev)
{
int result;
struct device_node* node = pdev->dev.of_node;
printk("water probe enter\n");
pdata = devm_kzalloc(&pdev->dev, sizeof(pdata), GFP_KERNEL);
if (!pdata) {
pr_err("%s kzalloc error\n", __FUNCTION__);
return -ENOMEM;
}
dev_set_drvdata(&pdev->dev, pdata);
pdata->humidity_gpio1 = of_get_named_gpio(node, "thundersoft,humidity_gpio1", 0);
if (!gpio_is_valid(pdata->humidity_gpio1)) {
pr_err("humidity gpio1 not specified\n");
goto err1;
} else {
result = gpio_request(pdata->humidity_gpio1, "humidity_gpio1");
if (result < 0) {
pr_err("Unable to request humidity gpio1\n");
goto err1;
} else {
gpio_direction_input(pdata->humidity_gpio1);
}
}
pdata->humidity_gpio2 = of_get_named_gpio(node, "thundersoft,humidity_gpio2", 0);
if (!gpio_is_valid(pdata->humidity_gpio2)) {
pr_err("humidity gpio2 not specified\n");
goto err2;
} else {
result = gpio_request(pdata->humidity_gpio2, "humidity_gpio2");
if (result < 0) {
pr_err("Unable to request humidity gpio2\n");
goto err2;
} else {
gpio_direction_input(pdata->humidity_gpio2);
}
}
pdata->humidity_gpio3 = of_get_named_gpio(node, "thundersoft,humidity_gpio3", 0);
if (!gpio_is_valid(pdata->humidity_gpio3)) {
pr_err("humidity gpio3 not specified\n");
goto err3;
} else {
result = gpio_request(pdata->humidity_gpio3, "humidity_gpio3");
if (result < 0) {
pr_err("Unable to request humidity gpio3\n");
goto err3;
} else {
gpio_direction_input(pdata->humidity_gpio3);
}
}
pdata->machine_gpio = of_get_named_gpio(node, "thundersoft,machine_gpio", 0);
if (!gpio_is_valid(pdata->machine_gpio)) {
pr_err("machine gpio not secified\n");
goto err4;
} else {
result = gpio_request(pdata->machine_gpio, "machine_gpio");
if (result < 0) {
pr_err("Unable to request machine gpio\n");
goto err4;
} else {
gpio_direction_output(pdata->machine_gpio, 0);
}
}
setup_timer(&(pdata->watertime), water_flowers_func, (unsigned long)pdata);
pdata->watertime.expires = jiffies + HZ;
add_timer(&(pdata->watertime));
printk(KERN_INFO "water probe success\n");
return 0;
err4:
gpio_free(pdata->humidity_gpio3);
err3:
gpio_free(pdata->humidity_gpio2);
err2:
gpio_free(pdata->humidity_gpio1);
err1:
kfree(pdata);
printk(KERN_ERR "water probe failed\n");
return -EINVAL;
}
static int water_remove(struct platform_device *pdev)
{
gpio_free(pdata->machine_gpio);
gpio_free(pdata->humidity_gpio3);
gpio_free(pdata->humidity_gpio2);
gpio_free(pdata->humidity_gpio1);
del_timer(&(pdata->watertime));
kfree(pdata);
return 0;
}
static struct of_device_id water_match_table[] = {
{ .compatible = "thundersoft,water"},
{ },
};
static struct platform_driver water_driver = {
.probe = water_probe,
.remove = water_remove,
.driver = {
.owner = THIS_MODULE,
.name = "water",
.of_match_table = water_match_table,
},
};
module_platform_driver(water_driver);
MODULE_AUTHOR("heql0703@thundersoft.com");
MODULE_LICENSE("GPL");