gpt4 book ai didi

linux - LED 通过 Rpi Linux 设备驱动程序闪烁

转载 作者:太空宇宙 更新时间:2023-11-04 05:59:19 27 4
gpt4 key购买 nike

在设备驱动程序编程中,我们使用 1. 设备驱动程序代码、2. 设备文件、3. 用户空间应用程序代码和 4. 实际物理硬件。

  • 我能够编写驱动程序代码、用户空间应用程序代码并手动创 build 备文件,但我想尝试连接硬件(至少通过 Raspberry Pi 中的设备驱动程序使 LED 闪烁),这对我来说看起来没什么困难。

  • 如果我能够使 LED 闪烁,则表示上述所有四个列出的项目之间正在发生通信。

我获取了一些用于闪烁 LED 的在线代码并进行了测试,但它不起作用。

最佳答案

Driver code:

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>

#define BUF_MAX_SIZE 1024

int major_num;
char driver_buf[BUF_MAX_SIZE];

int myopen(struct inode *inodep, struct file *filep)
{
printk(KERN_INFO "Bhaskar: Open function called.\n");
return 0;
}

int myclose(struct inode *indoep, struct file *filep)
{
printk(KERN_INFO "Bhaskar: Close function called.\n");
return 0;
}

ssize_t myread(struct file *filep, char *ubuf, size_t nbytes, loff_t *offset)
{
printk(KERN_INFO "Bhaskar: Read function called.\n");
copy_to_user(ubuf, driver_buf + filep->f_pos, nbytes);
filep->f_pos += nbytes;
return 0;
}

ssize_t mywrite(struct file *filep, const char *ubuf, size_t nbytes, loff_t *offset)
{
printk(KERN_INFO "Bhaskar: Write function called.\n");
copy_from_user(driver_buf + filep->f_pos, ubuf, nbytes);
filep->f_pos += nbytes;
return 0;
}

loff_t mylseek(struct file *filep, loff_t offset, int whence)
{
int newpos;
printk(KERN_INFO "Bhaskar: llseek function called.\n");
switch(whence)
{
case SEEK_SET:
newpos = offset;
printk(KERN_INFO "llseek called with SEEK_SET.\n");
break;

case SEEK_CUR:
newpos = filep->f_pos + offset;
printk(KERN_INFO "llseek called with SEEK_CUR.\n");
break;

case SEEK_END:
newpos = BUF_MAX_SIZE + offset;
printk(KERN_INFO "llseek called with SEEK_END.\n");
break;
}
filep->f_pos = newpos;
printk(KERN_INFO "new position of file pointer is:%d\n", newpos);
return newpos;
}

struct file_operations fops = {
.open = myopen,
.release = myclose,
.read = myread,
.write = mywrite,
.llseek = mylseek
};

static int hello_init(void)
{
printk(KERN_INFO "Bhaskar: Module loaded successfully.\n");
major_num = register_chrdev(0, "MyCharDriver", &fops);
printk(KERN_INFO "Char driver registered with major num is:%d\n", major_num);
return 0;
}

static void hello_exit(void)
{
printk(KERN_INFO "Bhaskar: Module unloaded successfully.\n");
unregister_chrdev(major_num, "MyCharDriver");
}

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Bhaskar <bhaskarauvsp@gmail.com>");
MODULE_DESCRIPTION("Its a dummy char driver");

module_init(hello_init);
module_exit(hello_exit);

关于linux - LED 通过 Rpi Linux 设备驱动程序闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58234681/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com