底层驱动day8作业

LinuxGPIO驱动程序示例与字符设备实现
本文介绍了如何在Linux中编写一个使用GPIO和定时器的驱动程序,创建字符设备,以及一个简单的用户应用程序与之交互。内容包括设备节点的查找、GPIO操作和定时器触发事件。

代码:

//驱动程序
#include<linux/init.h>
#include<linux/module.h>
#include<linux/of.h>
#include<linux/of_gpio.h>
#include<linux/gpio.h>
#include<linux/timer.h>

struct device_node *dnode;
//unsigned int gpiono;
char kbuf[128] = {0};
//定义定时器对象
struct timer_list mytimer;
struct gpio_desc *gpiono;


int major;
struct class *cls;
struct device *dev;
int mycdev_open(struct inode *inode, struct file *file)
{

    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}

ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    int ret=copy_from_user(kbuf,ubuf,size);

    if(ret)
    {
        printk("copy_from_user filed\n");
        return -EIO;
    }
    printk("%d\n",kbuf[0] - 48);
    gpiod_set_value(gpiono,kbuf[0] - 48);
    return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{

    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}

// 定义操作方法结构体变量并赋值
struct file_operations fops = {

    .open = mycdev_open,
    .write = mycdev_write,
    .release = mycdev_close,
};

void handler(struct timer_list *timer)
{
    printk("hello world\n");
    mod_timer(timer,jiffies + 5 * HZ);
}


static int __init mycdev_init(void)
{
    dnode = of_find_node_by_path("/myled");

    if(NULL == dnode)
    {
        printk("解析设备节点信息失败\n");
        return  -ENXIO;
    }
    //获取LED1 GPIO编号
    gpiono = gpiod_get_from_of_node(dnode,"led1-gpio",0,GPIOD_OUT_LOW,NULL);
    if(IS_ERR(gpiono))
    {
        printk("申请gpio信息失败\n");
        return -PTR_ERR(gpiono);
    }
    //初始化定时器对象
    timer_setup(&mytimer,handler,0);
    mytimer.expires = jiffies + HZ;
    //注册定时器
    add_timer(&mytimer);
    
     major = register_chrdev(0, "mychrdev", &fops);
    if (major < 0)
    {
        printk("字符设备驱动注册失败\n");
        return major;
    }
    printk("字符设备驱动注册成功:major=%d\n", major);
    // 向上提交目录
    cls = class_create(THIS_MODULE, "mychrdev");
    if (IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        return -PTR_ERR(cls);
    }
    printk("向上提交目录成功\n");
    // 向上提交设备节点信息
    int i; // 向上提交三次设备节点信息
    for (i = 0; i < 3; i++)
    {
        dev = device_create(cls, NULL, MKDEV(major, i), NULL, "myled%d", i);
        if (IS_ERR(dev))
        {
            printk("向上提交设备节点失败\n");
            return -PTR_ERR(dev);
        }
    }
    printk("向上提交设备节点成功\n");



    return 0;
}

static void __exit mycdev_exit(void)
{
    del_timer(&mytimer);
    
    //gpio_free(gpiono);
    
    gpiod_put(gpiono);

    // 销毁设备节点信息
    int i;
    for (i = 0; i < 3; i++)
    {
        device_destroy(cls, MKDEV(major, i));
    }

    // 销毁目录
    class_destroy(cls);
    // 注销字符设备驱动
    unregister_chrdev(major, "mychrdev");
}


module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
//应用程序
#include<stdio.h>
#include<stdlib.h>
#include<sys/ioctl.h>
#include<string.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>
#include<fcntl.h>
int main(int argc, char const *argv[])
{
    char buf[128] = {0};
    int fd = open("/dev/myled0",O_RDWR);
    while(1)
    {
        printf("请输入>>>");
        scanf("%s",buf);
        write(fd,buf,1);
    }
    
    return 0;
}

 实验现象:

 

【无线传感器】使用 MATLAB和 XBee连续监控温度传感器无线网络研究(Matlab代码实现)内容概要:本文档围绕“使用 MATLAB 和 XBee 连续监控温度传感器无线网络”的研究主题,介绍了基于 MATLAB 编程与 XBee 无线通信模块相结合的无线传感网络构建与实时温度监控系统实现方法。文中详细阐述了系统架构设计、XBee 模块的组网与配置、MATLAB 数据采集与可视化流程,并通过实际代码展示了如何实现传感器数据的无线传输、接收解析及动态图表显示,从而完成对温度变化的连续监测。该研究突出了 MATLAB 在数据处理与图形展示方面的优势,以及 XBee 在低功耗、可靠通信中的应用价值。; 适合人群:具备一定 MATLAB 编程基础和通信原理知识的本科生、研究生及从事物联网系统开发的初级工程师;适用于电子信息、自动化、计算机等相关专业的科研与课程设计实践。; 使用场景及目标:①实现无线传感器网络的数据采集与远程监控;②学习 MATLAB 与硬件模块(如 XBee)的串口通信编程;③掌握无线传感系统的设计与调试流程,为环境监测、工业测温等应用场景提供技术参考。; 阅读建议:建议读者结合文中提供的 MATLAB 代码与硬件连接示意图进行仿真与实物验证,重点关注串口通信参数设置、数据帧解析逻辑及实时绘图实现方式,以加深对系统整体工作流程的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值