树莓派 - 驱动hello

本文介绍如何在树莓派上编写并运行简单的Linux驱动程序。通过一个helloworld示例,展示了驱动程序的基本结构、编译过程及加载方法。读者可以跟随步骤实践,了解树莓派环境下驱动开发的基本流程。

树莓派上Linux驱动,从hello world 开始 ... 

hello.c 

#include <linux/init.h>  
#include <linux/module.h>  
#include <linux/moduleparam.h>  
  
MODULE_LICENSE("Dual BSD/GPL");  
  
static int hello_init(void)  
{  
    printk(KERN_ALERT"Hello, world\n");  
    return 0;  
}  
  
static void hello_exit(void)  
{  
    printk(KERN_ALERT"Goodbye, cruel world\n");  
}  
  
module_init(hello_init);  
module_exit(hello_exit);

Makefile

ifneq ($(KERNELRELEASE),)  
  
obj-m := hello.o  
  
else  
      
KDIR := /home/xxx/Raspberry/linux 
all:  
	make -C $(KDIR) M=$(PWD) modules ARCH=arm CROSS_COMPILE=/home/xxx/Raspberry/tools-master/arm-bcm2708/arm-rpi-4.9.3-linux-gnueabihf/bin/arm-linux-gnueabihf-

clean:  
	rm -f *.ko *.o *.mod.o *.mod.c *.symvers  modul*  
  
endif 

make生成hello.ko

将hello.ko 通过sftp 传给树莓派板


在树莓派班上

$ sudo insmod hello.ko

查看:

$ dmesg | grep Hello
[ 7454.663309] Hello, world
$ lsmod |grep hello
hello                  16384  0


pi@raspberrypi:/sys/module/hello $ tree -a
.
├── coresize
├── holders
├── initsize
├── initstate
├── notes
│   └── .note.gnu.build-id
├── refcnt
├── sections
│   ├── .ARM.exidx
│   ├── .ARM.extab
│   ├── .gnu.linkonce.this_module
│   ├── __mcount_loc
│   ├── .note.gnu.build-id
│   ├── .rodata.str1.4
│   ├── .strtab
│   ├── .symtab
│   └── .text
├── srcversion
├── taint
└── uevent

3 directories, 17 files


pi@raspberrypi:~ $ sudo rmmod hello
pi@raspberrypi:~ $ dmesg | grep Goodbye
[ 8385.391257] Goodbye, cruel world

参考

http://www.cnblogs.com/xianrou/p/6327897.html


树莓派LCD驱动/2.8/3.2/3.5/3.97/4.3/5/7等! You can focus on the following GitHub web site, for the latest lcd drivers: https://github.com/goodtft/LCD-show =============================================================================================================== "LCD-show-170315.tar.gz" just for Raspbian (Release date:2017-03-02) or later, DO NOT use it for the Raspbian version before 2017-03-02. ================================================================================================================ "LCD-show-160701.tar.gz" support Raspbian version before Raspbian-2017-02-16,such as(2017-02-16,2017-01-11, 2016-11-25,2016-09-23,2016-05-27,2016-05-10,2016-03-18,or earlier). And This Driver also tesed on "kali-2.1.2" ,"ubuntu-mate-16.04-beta2" ================================================================================================================ How to Install: 1.)Step1, Install Raspbian official mirror a)Download Raspbian official mirror: https://www.raspberrypi.org/downloads/ b)Use“SDFormatter.exe”to Format your TF Card c)Use“Win32DiskImager.exe” Burning mirror to TF Card 2.) Step2, Clone my repo onto your pi git clone https://github.com/goodtft/LCD-show.git chmod -R 755 LCD-show cd LCD-show/ 3.)Step3, According to your LCD's type, excute: In case of 2.8" LCD sudo ./LCD28-show In case of 3.2" LCD sudo ./LCD32-show In case of 3.5" LCD sudo ./LCD35-show In case of 3.97" LCD sudo ./LCD397-show In case of 4.3" LCD sudo ./LCD43-show In case of 5" LCD sudo ./LCD5-show In case of 7inch(B)-800X480 RPI LCD sudo ./LCD7B-show In case of 7inch(C)-1024X600 RPI LCD sudo ./LCD7C-show If you need to switch back to the traditional HDMI display sudo ./LCD-hdmi Wait a few minutes,the system will restart automaticall , enjoy with your LCD.
### 树莓派 Linux 下设备树驱动教程及配置方法 #### 什么是设备树? 设备树是一种数据结构,用于描述硬件的特性而不依赖于特定的操作系统。它允许操作系统识别和管理硬件资源而无需硬编码这些信息。对于树莓派而言,在 Linux 环境下使用设备树可以简化硬件初始化过程。 --- #### 设备树文件的作用 设备树文件(`.dts` 和 `.dtb` 文件)主要用于描述硬件平台上的外设连接情况以及它们的功能参数。在树莓派中,设备树文件通常位于 `/boot/overlays/` 目录下[^1]。通过修改或扩展这些文件,开发者能够轻松支持新的硬件功能。 --- #### 如何编写设备树片段 (Device Tree Overlay) 为了适配不同的硬件需求,可以通过创建 Device Tree Overlays 来动态加载额外的硬件配置。以下是具体步骤: 1. **编辑 DTS 文件** 创建一个新的 `.dts` 文件来定义所需的硬件设置。例如: ```c /dts-v1/; /plugin/; / { compatible = "brcm,bcm2708"; fragment@0 { target = <&gpio>; __overlay__ { status = "okay"; my_gpio_pins: pinmux_my_gpio { brcm,pins = <17>; /* GPIO Pin Number */ brcm,function = <1>; /* Output Mode */ brcm,pull = <2>; /* Pull Up */ }; }; }; fragment@1 { target = <&i2c_arm>; __overlay__ { status = "okay"; my_i2c_device: device@5a { compatible = "my,i2c-device"; reg = <0x5a>; }; }; }; }; ``` 2. **编译 DTB 文件** 将上述 `.dts` 文件转换为二进制格式 `.dtbo` 文件以便加载到内核中。 ```bash dtc -I dts -O dtb -o my_overlay.dtbo my_overlay.dts ``` 编译完成后,将生成的 `my_overlay.dtbo` 放置到 `/boot/overlays/` 中[^3]。 3. **启用 Overlay** 修改 `/boot/config.txt` 文件以激活新创建的 overlay: ```ini dtoverlay=my-overlay ``` 4. **验证加载状态** 可以通过以下命令确认设备树是否成功应用: ```bash lsmod | grep my_overlay dmesg | tail ``` --- #### 配合驱动程序开发 当完成设备树配置后,还需要编写相应的驱动代码并与之配合工作。假设目标是一个简单的字符设备,则按照以下流程操作: 1. **进入源码目录** 转移到已编译好的 Linux 内核源码路径中的字符设备驱动子目录: ```bash cd ~/linux-rpi-4.14.y/drivers/char/ ``` 2. **编写驱动模块** 制作一个基础的字符设备驱动模板,例如: ```c #include <linux/module.h> #include <linux/fs.h> static int major_number; static char message[256]; static ssize_t dev_read(struct file *filp, char __user *buffer, size_t length, loff_t *offset) { copy_to_user(buffer, message, sizeof(message)); return sizeof(message); } static struct file_operations fops = { .owner = THIS_MODULE, .read = dev_read, }; static int __init hello_init(void) { major_number = register_chrdev(0, "hello", &fops); sprintf(message, "Hello from the kernel!"); printk(KERN_INFO "Registered character device with major %d\n", major_number); return 0; } static void __exit hello_exit(void) { unregister_chrdev(major_number, "hello"); printk(KERN_INFO "Unregistered character device.\n"); } module_init(hello_init); module_exit(hello_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Your Name"); MODULE_DESCRIPTION("A Simple Character Driver Example."); ``` 3. **构建并安装模块** 执行标准 Makefile 构建流程,并将结果复制到树莓派上运行: ```makefile obj-m += hello.o KDIR := /lib/modules/$(shell uname -r)/build PWD := $(shell pwd) all: make -C $(KDIR) M=$(PWD) modules clean: make -C $(KDIR) M=$(PWD) clean ``` 4. **手动创建节点** 如果未自动注册设备节点,可通过命令手工建立: ```bash sudo mknod /dev/hello c MAJOR MINOR ``` --- #### 测试与调试 最后一步是对整个系统的功能性进行全面检验。利用用户空间应用程序访问该虚拟设备,观察其行为表现是否符合预期设计逻辑。 ```python with open('/dev/hello', 'rb') as fd: data = fd.read() print(data.decode()) ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值