//超声波测距模块共有4个引脚: VCC, GND, Trigger, Echo
//工作过程: 要开始测量时,Trigger引脚给10us以上的高电平.
// Echo引脚会从开始测量到测量结束持续高电平(从上升沿到下降沿)
// 测量的距离: (Echo持续的高电平时间 * 340M/s)/2
PA12作超声波模块的trigger引脚, PA11作超声波的echo引脚.
//注意电源需要接5V.
设备树里的描述:
ultrasonic {
compatible = "myultrasonic";
trigger-gpios = <&pio 0 12 GPIO_ACTIVE_HIGH>;
echo-gpios = <&pio 0 11 GPIO_ACTIVE_HIGH>;
};
设备驱动代码:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/property.h>
#include <linux/gpio/consumer.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/ktime.h>
typedef struct {
struct gpio_desc *trigger_desc; //存放trigger所接的io口信息
struct gpio_desc *echo_desc; //存放echo所接的io口信息
s64 prev_time; //记录测量时的开始时间
}mypdata;
irqreturn_t irq_func(int irqno, void *arg) // arg就是在请求中断时的最后一个参数
{
struct platform_device *pdev = (struct platform_device *)arg;
mypdata *pdata = platform_get_drvdata(pdev);
s64 now = ktime_to_us(ktime_get());
if (gpiod_get_value(pdata->echo_desc))
pdata->prev_time = now;
else
{
printk("%lldus\n", (now - pdata->prev_time)>>1);
}
return IRQ_HANDLED;
}
int myprobe(struct platform_device *pdev)
{
mypdata *pdata = devm_kzalloc(&pdev->dev, sizeof(mypdata), GFP_KERNEL);
int ret = -ENODEV;
//获取设备树里trigger-gpios的信息时,设置io口作输出,并且输出低电平.
pdata->trigger_desc = devm_gpiod_get(&pdev->dev, "trigger", GPIOD_OUT_LOW);
if (IS_ERR(pdata->trigger_desc))
{
printk("trigger io failed\n");
goto err0;
}
//获取设备树里echo-gpios的信息时,设置io口输入.
pdata->echo_desc = devm_gpiod_get(&pdev->dev, "echo", GPIOD_IN);
if (IS_ERR(pdata->echo_desc))
{
printk("echo io failed\n");
goto err1;
}
//请求使用echo引脚的中断
ret = devm_request_any_context_irq(&pdev->dev, gpiod_to_irq(pdata->echo_desc), irq_func, IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING, pdev->name, (void *)pdev);
if (ret < 0)
{
printk("request irq failed\n");
goto err2;
}
platform_set_drvdata(pdev, pdata);
printk("in myprobe\n");
//控制trigger引脚的电平,让模块开始工作
gpiod_set_value(pdata->trigger_desc, 1);
udelay(20);
gpiod_set_value(pdata->trigger_desc, 0);
return 0;
err2:
devm_gpiod_put(&pdev->dev, pdata->echo_desc);
err1:
devm_gpiod_put(&pdev->dev, pdata->trigger_desc);
err0:
devm_kfree(&pdev->dev, pdata);
return ret;
}
int myremove(struct platform_device *pdev)
{
mypdata *pdata = platform_get_drvdata(pdev);
devm_free_irq(&pdev->dev, gpiod_to_irq(pdata->echo_desc), pdev); //中断号是独占使用的, 不用了需要释放. 最后一个参数与request_irq时的最后一个参数要相同.
devm_gpiod_put(&pdev->dev, pdata->trigger_desc);
devm_gpiod_put(&pdev->dev, pdata->echo_desc);
devm_kfree(&pdev->dev, pdata);
printk("in myremove ...\n");
return 0;
}
struct of_device_id ids[] = {
{.compatible = "myultrasonic"},
{},
};
struct platform_driver mydrv = {
.probe = myprobe,
.remove = myremove,
.driver = {
.owner = THIS_MODULE,
.name = "mydrv" ,
.of_match_table = ids,
},
};
module_platform_driver(mydrv);
MODULE_LICENSE("GPL");
在上面的基础上再加入用户进程的访问接口.
#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/property.h>
#include <linux/gpio/consumer.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/ktime.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/mutex.h>
#include <linux/wait.h>
#define MYMA 3344
#define COUNT 1
typedef struct {
struct gpio_desc *trigger_desc; //存放trigger所接的io口信息
struct gpio_desc *echo_desc; //存放echo所接的io口信息
struct cdev cdev;
dev_t devid;
struct class *cls;
struct mutex mutex_irq; //用于在read函数里等待中断完成
struct mutex mutex_read; //用于确保一个用户进程在调用驱动
s64 prev_time; //记录测量时的开始时间
int time_us; //测量出来的时间结果
}mypdata;
ssize_t myread(struct file *fl, char *__user buf, size_t len, loff_t *off)
{
struct inode *ind = fl->f_path.dentry->d_inode;
mypdata *pdata = container_of(ind->i_cdev, mypdata, cdev);
int ret;
mutex_lock(&pdata->mutex_read); //确保只有一个进程在调用read函数
//控制trigger引脚的电平,让模块开始工作
msleep(1);
gpiod_set_value(pdata->trigger_desc, 0);
msleep(1);
gpiod_set_value(pdata->trigger_desc, 1);
msleep(1);
gpiod_set_value(pdata->trigger_desc, 0);
ret = mutex_lock_interruptible(&pdata->mutex_irq);
if (ret < 0)
goto out;
sprintf(buf, "%d mm\n", pdata->time_us * 340 / 1000);
// printk("in myread ...%d us\n", pdata->time_us);
ret = strlen(buf);
out:
mutex_unlock(&pdata->mutex_read);
return ret;
}
struct file_operations fops = {
.owner = THIS_MODULE,
.read = myread,
};
irqreturn_t irq_func(int irqno, void *arg) // arg就是在请求中断时的最后一个参数
{
struct platform_device *pdev = (struct platform_device *)arg;
mypdata *pdata = platform_get_drvdata(pdev);
s64 now = ktime_to_us(ktime_get());
if (gpiod_get_value(pdata->echo_desc))
pdata->prev_time = now;
else
{
pdata->time_us = (now - pdata->prev_time)>>1;
mutex_unlock(&pdata->mutex_irq);
}
return IRQ_HANDLED;
}
int myprobe(struct platform_device *pdev)
{
mypdata *pdata = devm_kzalloc(&pdev->dev, sizeof(mypdata), GFP_KERNEL);
int ret = -ENODEV;
static int mi = 0;
//获取设备树里trigger-gpios的信息时,设置io口作输出,并且输出低电平.
pdata->trigger_desc = devm_gpiod_get(&pdev->dev, "trigger", GPIOD_OUT_LOW);
if (IS_ERR(pdata->trigger_desc))
{
printk("trigger io failed\n");
goto err0;
}
//获取设备树里echo-gpios的信息时,设置io口输入.
pdata->echo_desc = devm_gpiod_get(&pdev->dev, "echo", GPIOD_IN);
if (IS_ERR(pdata->echo_desc))
{
printk("echo io failed\n");
goto err1;
}
///////////////////// cdev 部分/////////
pdata->devid = MKDEV(MYMA, mi);
ret = register_chrdev_region(pdata->devid, COUNT, pdev->name);
if (ret < 0)
goto err2;
cdev_init(&pdata->cdev, &fops);
pdata->cdev.owner = THIS_MODULE;
ret = cdev_add(&pdata->cdev, pdata->devid, COUNT);
if (ret < 0)
goto err3;
pdata->cls = class_create(THIS_MODULE, pdev->name);
device_create(pdata->cls, NULL, pdata->devid, NULL, pdev->name);
////////////
mutex_init(&pdata->mutex_irq);
mutex_init(&pdata->mutex_read);
mutex_lock(&pdata->mutex_irq); //锁第一次是可锁上的
platform_set_drvdata(pdev, pdata);
//请求使用echo引脚的中断
ret = devm_request_any_context_irq(&pdev->dev, gpiod_to_irq(pdata->echo_desc), irq_func, IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING, pdev->name, (void *)pdev);
if (ret < 0)
{
printk("request irq failed\n");
goto err4;
}
printk("in myprobe\n");
mi++;
return 0;
err4:
device_destroy(pdata->cls, pdata->devid);
class_destroy(pdata->cls);
err3:
unregister_chrdev_region(pdata->devid, COUNT);
err2:
devm_gpiod_put(&pdev->dev, pdata->echo_desc);
err1:
devm_gpiod_put(&pdev->dev, pdata->trigger_desc);
err0:
devm_kfree(&pdev->dev, pdata);
return ret;
}
int myremove(struct platform_device *pdev)
{
mypdata *pdata = platform_get_drvdata(pdev);
unregister_chrdev_region(pdata->devid, COUNT);
cdev_del(&pdata->cdev);
device_destroy(pdata->cls, pdata->devid);
class_destroy(pdata->cls);
devm_free_irq(&pdev->dev, gpiod_to_irq(pdata->echo_desc), pdev); //中断号是独占使用的, 不用了需要释放. 最后一个参数与request_irq时的最后一个参数要相同.
devm_gpiod_put(&pdev->dev, pdata->trigger_desc);
devm_gpiod_put(&pdev->dev, pdata->echo_desc);
devm_kfree(&pdev->dev, pdata);
printk("in myremove ...\n");
return 0;
}
struct of_device_id ids[] = {
{.compatible = "myultrasonic"},
{},
};
MODULE_DEVICE_TABLE(of, ids);
struct platform_driver mydrv = {
.probe = myprobe,
.remove = myremove,
.driver = {
.owner = THIS_MODULE,
.name = "mydrv" ,
.of_match_table = ids,
},
};
module_platform_driver(mydrv);
MODULE_LICENSE("GPL");
当再接入一硬件设备时,只需要修改设备树文件,加入相应的描述即可. 如再接入一个超音波模块,trigger引脚接PA19, echo引脚接PA18:
ultrasonic {
compatible = "myultrasonic";
trigger-gpios = <&pio 0 12 GPIO_ACTIVE_HIGH>;
echo-gpios = <&pio 0 11 GPIO_ACTIVE_HIGH>;
};
ultrasonic2 {
compatible = "myultrasonic";
trigger-gpios = <&pio 0 19 GPIO_ACTIVE_HIGH>;
echo-gpios = <&pio 0 18 GPIO_ACTIVE_HIGH>;
};