编写驱动代码实现下面逻辑

如下要求
a.应用程序通过阻塞的io模型来读取number变量的值b.number是内核驱动中的一个变量
c.number的值随着按键按下而改变(按键中断)
例如number=O按下按键number=1 ,再次按下按键number=Od .在按下按键的时候需要同时将led1的状态取反
e .驱动中需要编写字符设备驱动
f.驱动中需要自动创建设备节点
g.这个驱动需要的所有设备信息放在设备树的同一个节点中
 

驱动代码

#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/mod_devicetable.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include <linux/interrupt.h>
#include <linux/of_irq.h>
#include <linux/uaccess.h>

int major; // 主设备号
struct class *cls;
struct device *dev;
struct gpio_desc *gpiono;         // 存储gpio相关信息
unsigned int irqno;               // 软中断号
wait_queue_head_t wq_head; // 等待队列头
unsigned int condition = 0;
unsigned int number = 0;
char kbuf[10] = "";

// 中断处理函数
irqreturn_t irq_handler(int irqno, void *arg)
{
    number = gpiod_get_value(gpiono);
    number = !number;
    gpiod_set_value(gpiono, number);
    condition = 1;
    wake_up_interruptible(&wq_head);
    return IRQ_HANDLED;
}

// 封装操作方法
int my_open(struct inode *inode, struct file *file)
{
    return 0;
}

ssize_t my_read(struct file *file, char *ubuf, size_t size, loff_t *lof)
{
    int ret;
    if (file->f_flags & O_NONBLOCK) // 不阻塞
    {
        return -EINVAL;
    }
    else // 阻塞
    {
        wait_event_interruptible(wq_head, condition);
    }
    if (number == 1)
        kbuf[0] = '1';
    else if (number == 0)
        kbuf[0] = '0';
    ret = copy_to_user(ubuf, kbuf, sizeof(kbuf));
    if (ret < 0)
    {
        printk("copy_to_user failed\n");
        return -1;
    }
    condition = 0;
    return 0;
}

int my_close(struct inode *inode, struct file *file)
{
    return 0;
}

// 定义并初始化操作方法结构体
struct file_operations fops = {
    .open = my_open,
    .read = my_read,
    .release = my_close,
};

// probe函数,设备匹配成功后执行
int pdrv_probe(struct platform_device *pdev)
{
    int ret;
    printk("设备与驱动匹配成功\n");
    // 初始化等待队列头
    init_waitqueue_head(&wq_head);
    // 注册字符设备驱动
    major = register_chrdev(0, "myplatform", &fops);
    if (major < 0)
    {
        printk("注册字符设备驱动失败\n");
        ret = major;
        goto out1;
    }
    printk("注册字符设备驱动成功\n");

    // 向上提交设备节点目录
    cls = class_create(THIS_MODULE, "myplatform");
    if (IS_ERR(cls))
    {
        printk("向上提交设备节点目录失败\n");
        ret = -PTR_ERR(cls);
        goto out2;
    }
    printk("向上提交设备节点目录成功\n");

    // 向上提交设备节点信息
    dev = device_create(cls, NULL, MKDEV(major, 0), NULL, "myplatform0");
    if (IS_ERR(dev))
    {
        printk("向上提交设备节点信息失败\n");
        ret = -PTR_ERR(dev);
        goto out3;
    }
    printk("向上提交设备节点信息成功\n");

    // 获取gpio信息
    gpiono = gpiod_get_from_of_node(pdev->dev.of_node, "led1", 0, GPIOD_OUT_LOW, NULL);
    if (IS_ERR(gpiono))
    {
        printk("获取gpio信息失败\n");
        ret = -PTR_ERR(gpiono);
        goto out4;
    }
    printk("获取gpio信息成功\n");

    // 获取软中断号
    irqno = platform_get_irq(pdev, 0);
    if (irqno < 0)
    {
        printk("获取中断资源失败\n");
        ret = irqno;
        goto out5;
    }
    printk("获取中断类型资源成功,irqno = %d\n", irqno);

    // 注册中断
    ret = request_irq(irqno, irq_handler, IRQF_TRIGGER_FALLING, "myplatform", NULL);
    if (ret < 0)
    {
        printk("注册中断失败\n");
        goto out5;
    }
    printk("注册中断成功\n");

    return 0;

out5:
    gpiod_put(gpiono);
out4:
    device_destroy(cls, MKDEV(major, 0));
out3:
    class_destroy(cls);
out2:
    unregister_chrdev(major, "myplatform");
out1:
    return ret;
}

// remove函数,设备与驱动分离时执行
int pdrv_remove(struct platform_device *pdev)
{
    // 灭灯
    gpiod_set_value(gpiono, 0);
    free_irq(irqno, NULL);
    // 释放gpio信息
    gpiod_put(gpiono);
    device_destroy(cls, MKDEV(major, 0));
    class_destroy(cls);
    unregister_chrdev(major, "myplatform");
    return 0;
}

// 设备树匹配的表
struct of_device_id oftable[] =
    {
        {.compatible = "hqyj,myplatform"},
        {.compatible = "hqyj,myplatform1"},
        {},
};

// platform驱动信息对象结构体
struct platform_driver pdrv = {
    .probe = pdrv_probe,
    .remove = pdrv_remove,
    .driver = {
        .name = "myplatform",
        .of_match_table = oftable,
    },
};

// 一键注册宏
module_platform_driver(pdrv);
MODULE_LICENSE("GPL");

应用程序

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int main(int argc, const char *argv[])
{
    char buf[10] = "";
    int ret;
    int fd = open("/dev/myplatform0",O_RDWR);
    if(fd < 0)
    {
        printf("打开设备文件失败\n");
        return -1;
    }
    printf("打开设备文件成功\n");
    while(1)
    {
        ret = read(fd,buf,sizeof(buf));
        if(ret < 0)
        {
            printf("读取失败\n");
            return -1;
        }
        printf("number = %c\n",buf[0]);
    }
    close(fd);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个使用OpenMV进行小车循迹的完整代码示例: ```python import sensor import image import time from pyb import Pin, Timer # 初始化摄像头 sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QQVGA) sensor.skip_frames(time = 2000) # 设置黑线阈值,根据实际情况进行调整 BLACK_THRESHOLD = (0, 100) # 控制小车运动的引脚 LEFT_PIN = Pin('P0', Pin.OUT_PP) RIGHT_PIN = Pin('P1', Pin.OUT_PP) # 定时器用于控制小车的PWM信号 LEFT_TIMER = Timer(2, freq=1000) LEFT_CHANNEL = LEFT_TIMER.channel(1, Timer.PWM, pin=LEFT_PIN) RIGHT_TIMER = Timer(4, freq=1000) RIGHT_CHANNEL = RIGHT_TIMER.channel(2, Timer.PWM, pin=RIGHT_PIN) def move_forward(): LEFT_CHANNEL.pulse_width_percent(50) RIGHT_CHANNEL.pulse_width_percent(50) def turn_left(): LEFT_CHANNEL.pulse_width_percent(30) RIGHT_CHANNEL.pulse_width_percent(70) def turn_right(): LEFT_CHANNEL.pulse_width_percent(70) RIGHT_CHANNEL.pulse_width_percent(30) def stop(): LEFT_CHANNEL.pulse_width_percent(0) RIGHT_CHANNEL.pulse_width_percent(0) while True: img = sensor.snapshot() # 获取图像 # 检测黑线 line = img.get_histogram().get_percentile(0.1, 0.9) img.binary([BLACK_THRESHOLD]) # 根据黑线的位置控制小车运动 if line < img.width() / 2: turn_left() else: turn_right() time.sleep(10) # 控制循迹速度 ``` 请注意,这只是一个示例代码,具体的引脚配置和PWM信号设置需要根据您的硬件和电机驱动器进行调整。此外,您可能还需要根据实际情况修改循迹逻辑和速度控制。希望这个示例能为您提供一些启发,如果您有其他问题,请随时提出。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值