视频:https://www.bilibili.com/video/BV18SE26cEjn?vd_source=92b7efa4daa5bb4fdcdc9f2db97c307b&p=14
lesson13后面的视频是说设备树和misc 设备,感觉设备树讲的不太好,没有基础的估计完全看不懂,而且这个系列没有说中断,DMA,SPI,UART,IIC等,也没说网络设备和块设备,值得称道的是源码例子非常浅显。
设备树:
lesson14 说了下设备树基本概念。linux 官方解释了设备树的三大目的:
Linux and the Devicetree — The Linux Kernel documentation
Linux uses DT data for three major purposes:
platform identification,
runtime configuration, and
device population.
也就是定义识别设备/总线,读取设备树配置设备,生成设备信息。
我的理解是设备树是描述硬件状态的一个文件,它的语法类似json,需要更多了解下设备树语法。
一般官方会给出一个设备树模板,自己根据情况修改。比如之前给zynq的linux 写个system_user.dtsi,要写个mcp2518fd的设备树,xilinx 官方 内核提供了设备树模板文件microchip,mcp251xfd.yaml;
examples: - | #include <dt-bindings/gpio/gpio.h> #include <dt-bindings/interrupt-controller/irq.h> spi { #address-cells = <1>; #size-cells = <0>; can@0 { compatible = "microchip,mcp251xfd"; reg = <0>; clocks = <&can0_osc>; pinctrl-names = "default"; pinctrl-0 = <&can0_pins>; spi-max-frequency = <20000000>; interrupts-extended = <&gpio 13 IRQ_TYPE_LEVEL_LOW>; microchip,rx-int-gpios = <&gpio 27 GPIO_ACTIVE_LOW>; vdd-supply = <®5v0>; xceiver-supply = <®5v0>; }; };这个设备树有头文件 <dt-bindings/gpio/gpio.h>,<dt-bindings/interrupt-controller/irq.h>,所以自己写的时候要加进去;zynq 里面基本可以不考虑pin_ctrl,因为vivado里面配置zynq的时候已经给spi总线设置好了IO口各种属性了。
然后写zynq system_user.dtsi 是overlay的(lesson15解释了overlay),所以这个SPI调用的时候SPI总线节点需要给它加上&号,根据硬件原理图就是&SPI1,然后例子里面的can0_osc 这个需要自己写个对应的根节点。
所以,我就先参考官方的例子:Device Tree Tips - AMD Adaptive Computing Wiki - Confluence
给晶振clock 说明写好一个clocks对应的晶振osc:
osc: oscillator { //节点:osc,别名 oscillator
compatible = "fixed-clock"; //这个看厂家,我找的zynq的选择是 fixed-clock
#clock-cells = <0>;
clock-frequency = <40000000>; //晶振频率40MHZ
clock-output-names = "osc";
};
然后spi总线SPI1里面调用这个osc, clocks =<&osc>;
最后设备MCP2518FD的设备树自己写了如下:
&spi1{
#address-cells = <1>;
#size-cells = <0>;
status = "okay";
canfd@0{
compatible = "microchip,mcp2518fd";
reg = <0>;
clocks =<&osc>;
spi-max-frequency = <20000000>;
interrupt-parent = <&gpio0>;
interrupts-extended = <&gpio0 62 IRQ_TYPE_LEVEL_LOW>;
microchip,rx-int = <&gpio0 61 GPIO_ACTIVE_LOW>;
};
};
同时内核进行相应的裁减操作,这样驱动读取设备树文件dtsi就知道SPI总线用的是40Mhz的时钟晶振,spi 的sclk 20MHZ,中断用的是哪个IO口。
总而言之,PCB 软件用画原理图来表明这个设备的情况,设备树是通过语言的方式描叙硬件的状态。具体设备树怎么写一般下载内核后在DOCUMENT下面查找各个内核的设备树,照说明写。
要自己写驱动,写好设备树之后让自己的驱动读取它来工作,可以看看视频lesson17,lesson18了解。
视频里面的代码那些都不放了。感觉大同小异。看视频和git,了解即可。
搜索看到Johannes还有个UP传送了另一个系列的他的linux 内核视频系列: ://www.bilibil
https://www.bilibili.com/video/BV1CNJc64Esv/?vd_source=92b7efa4daa5bb4fdcdc9f2db97c307b
内容挺丰富的
1. Simple Kernel Module
2. Device Numbers and Device Files
3. Create device file in driver and callbacks
4. GPIO Driver
5. Text LCD Driver
6. PWM Module
7. Temperature Sensor (I2C)
8. Timer in Linux Kernel Modules
9. High Resolution Timer in Linux Kernel Modules
10. Accessing SPI with a Linux Kernel Module (BMP280 sensor again)
11. Using a GPIO Interrupt in a Linux Kernel Module
12. Using Parameters in a Linux Kernel Module
13. IOCTL in a Linux Kernel Module
14. Threads in a Linux Kernel Module
15. Sending a signal from a Linux Kernel Module to an userspace application
16. The poll callback
17. Waitqueues in a Linux Kernel Module
18. Create procfs entries from a Linux Kernel Module
19. Create sysfs entries from a Linux Kernel Module
20. Parse the device tree from a Linux Kernel Module to get the deivce properties of a specific device
21. Device Tree GPIO Driver
22. Device Tree Driver for I2C Device
23. Dynamical memory management in a Linux Kernel module
24. Serial (UART) Driver
25. Industrial IO compatible driver for an ATMEGA I2C ADC
26. Device Tree SPI Driver (IIO compatible driver for Atmega SPI ADC)
27. Misc Device
28. Mutex for exclusive access to shared resource
29. Completions for synchronisation
30. Direct Memory Access (DMA) memcopy example
31. Accessing files form a Linux Driver
32. The mmap callback
33. Linked Lists
34. Registering device numbers, read and write callback in character devices Take 2
35. Private Data in struct file
36. I2C Device Driver without Device Tree
37. Sysfs Class
38. Kernel Log levels
git 上面有程序资源,B站评论区有。