“扩展板风扇驱动移植”的版本间的差异
来自华清远见研发中心
(→实验平台) |
(→实验步骤) |
||
第25行: | 第25行: | ||
==实验步骤== | ==实验步骤== | ||
+ | <ol> | ||
+ | <li>导入交叉编译工具链</li> | ||
+ | linux@ubuntu:$ source /opt/st/stm32mp1/3.1-openstlinux-5.4-dunfell-mp1-20-06-24/environment-setup-cortexa7t2hf-neon-vfpv4-ostl-linux-gnueabi | ||
+ | <li>内核配置</li> | ||
+ | 内核中为pwm风扇提供了标准的驱动,只要在内核中配置对应选项即可,驱动路径为: | ||
+ | drivers/hwmon/pwm-fan.c | ||
+ | 执行make menuconfig配置内核对应选项 | ||
+ | <pre> | ||
+ | linux@ubuntu:$ make menuconfig | ||
+ | Device Drivers ---> | ||
+ | <*> Hardware Monitoring support ---> | ||
+ | <*> PWM fan | ||
+ | </pre> | ||
+ | |||
+ | <li>修改设备树</li> | ||
+ | 参考linux内核参考文档: | ||
+ | <pre> | ||
+ | Documentation/devicetree/bindings/pwm/pwm-stm32.txt | ||
+ | Documentation/devicetree/bindings/hwmon/pwm-fan.txt | ||
+ | </pre> | ||
+ | |||
+ | 修改设备树文件: | ||
+ | arch/arm/boot/dts/stm32mp157a-fsmp1a-extended.dts | ||
+ | |||
+ | 由于timers1在stm32mp151.dtsi中已完成定义,这里需要在原有基础添加与硬件对应的相关信息,在文件stm32mp157a-fsmp1a-extended.dts末尾集成并添加timers1相关内容: | ||
+ | |||
+ | <pre> | ||
+ | &timers1 { | ||
+ | /* spare dmas for other usage */ | ||
+ | /delete-property/dmas; | ||
+ | /delete-property/dma-names; | ||
+ | status = "okay"; | ||
+ | pwm1: pwm { | ||
+ | pinctrl-0 = <&pwm1_pins_b>; | ||
+ | pinctrl-1 = <&pwm1_sleep_pins_b>; | ||
+ | pinctrl-names = "default", "sleep"; | ||
+ | #pwm-cells = <2>; | ||
+ | status = "okay"; | ||
+ | }; | ||
+ | timer@0 { | ||
+ | status = "disabled"; | ||
+ | }; | ||
+ | }; | ||
+ | </pre> | ||
+ | 仿照设备树对于pwm管脚的配置添加timers1 pwm输出管脚配置,在stm32mp157a-fsmp1a-extended.dts文件pinctrl节点中添加pwm1管脚信息,红色字体内容为添加内容: | ||
+ | |||
+ | <div style="background-color:#F8F8F8;border:1px solid #E5E5E5;font-family: monospace,Courier;"> | ||
+ | &pinctrl { | ||
+ | : …… | ||
+ | <font color="#F00"> | ||
+ | : pwm1_pins_b: pwm1-0 { | ||
+ | :: pins { | ||
+ | ::: pinmux = <STM32_PINMUX('E', 9, AF1)>; /* TIM1_CH1 */ | ||
+ | ::: bias-pull-down; | ||
+ | ::: drive-push-pull; | ||
+ | ::: slew-rate = <0>; | ||
+ | :: }; | ||
+ | : }; | ||
+ | : pwm1_sleep_pins_b: pwm1-sleep-0 { | ||
+ | :: pins { | ||
+ | ::: pinmux = <STM32_PINMUX('E', 9, ANALOG)>; /* TIM1_CH1 */ | ||
+ | :: }; | ||
+ | : }; | ||
+ | </font> | ||
+ | }; | ||
+ | </div> | ||
+ | 最后在根节点下添加风扇驱动对应节点: | ||
+ | <pre> | ||
+ | fan { | ||
+ | compatible = "pwm-fan"; | ||
+ | #cooling-cells = <2>; | ||
+ | pwms = <&pwm1 0 10000>; | ||
+ | cooling-levels = <0 102 170 230>; | ||
+ | }; | ||
+ | </pre> | ||
+ | |||
+ | <li>重新编译内核和设备树文件</li> | ||
+ | linux@ubuntu:$ make -j4 uImage dtbs LOADADDR=0xC2000040 | ||
+ | |||
+ | <li>更新系统内核和设备树</li> | ||
+ | |||
+ | <li>测试</li> | ||
+ | 系统启动后可以查看对应目录 | ||
+ | |||
+ | <pre> | ||
+ | root@fsmp1a:~# ls -l /sys/class/hwmon/hwmon1/ | ||
+ | total 0 | ||
+ | lrwxrwxrwx 1 root root 0 Jun 30 12:01 device -> ../../../fan | ||
+ | -r--r--r-- 1 root root 4096 Jun 30 12:01 name | ||
+ | drwxr-xr-x 2 root root 0 Jun 30 12:01 power | ||
+ | -rw-r--r-- 1 root root 4096 Jun 30 12:01 pwm1 | ||
+ | -rw-r--r-- 1 root root 4096 Jun 30 12:00 uevent | ||
+ | …… | ||
+ | |||
+ | </pre> | ||
+ | 可通过下列操作调整风扇转速: | ||
+ | 可以写0~255之间的数字到文件/sys/class/hwmon/hwmon1/pwm1文件中调节转速 | ||
+ | 比如: | ||
+ | echo 200 > /sys/class/hwmon/hwmon1/pwm1 | ||
+ | |||
+ | 通过写入不同的值观察风扇的转速,如有条件可以通过示波器查看当前pwm的波形改变。 | ||
+ | </ol> |
2021年3月23日 (二) 14:46的最新版本
实验原理
打开扩展板原理图对照扩展板可以看到扩展板有1个风扇FAN1,如下图:
由上图课件可通过TIM1_CH1电平改变控制电路的通断从而驱动风扇。
查看原理图可知TIM1_CH1对应PE9,查看芯片手册可知PE9可以作为PWM TIMER1的通道1使用,本例实现如何通过PWM驱动风扇:
原理图网络编号 | 对应管脚 | 管脚功能 | 管脚功能码 |
---|---|---|---|
TIM1_CH1 | PE9 | TIM1_CH1 | AF1 |
实验平台
华清远见开发环境,FS-MP1A平台
实验步骤
- 导入交叉编译工具链
- 内核配置
- 修改设备树
- ……
- pwm1_pins_b: pwm1-0 {
- pins {
- pinmux = <STM32_PINMUX('E', 9, AF1)>; /* TIM1_CH1 */
- bias-pull-down;
- drive-push-pull;
- slew-rate = <0>;
- };
- pins {
- };
- pwm1_sleep_pins_b: pwm1-sleep-0 {
- pins {
- pinmux = <STM32_PINMUX('E', 9, ANALOG)>; /* TIM1_CH1 */
- };
- pins {
- };
- 重新编译内核和设备树文件
- 更新系统内核和设备树
- 测试
linux@ubuntu:$ source /opt/st/stm32mp1/3.1-openstlinux-5.4-dunfell-mp1-20-06-24/environment-setup-cortexa7t2hf-neon-vfpv4-ostl-linux-gnueabi
内核中为pwm风扇提供了标准的驱动,只要在内核中配置对应选项即可,驱动路径为:
drivers/hwmon/pwm-fan.c
执行make menuconfig配置内核对应选项
linux@ubuntu:$ make menuconfig Device Drivers ---> <*> Hardware Monitoring support ---> <*> PWM fan
参考linux内核参考文档:
Documentation/devicetree/bindings/pwm/pwm-stm32.txt Documentation/devicetree/bindings/hwmon/pwm-fan.txt
修改设备树文件:
arch/arm/boot/dts/stm32mp157a-fsmp1a-extended.dts
由于timers1在stm32mp151.dtsi中已完成定义,这里需要在原有基础添加与硬件对应的相关信息,在文件stm32mp157a-fsmp1a-extended.dts末尾集成并添加timers1相关内容:
&timers1 { /* spare dmas for other usage */ /delete-property/dmas; /delete-property/dma-names; status = "okay"; pwm1: pwm { pinctrl-0 = <&pwm1_pins_b>; pinctrl-1 = <&pwm1_sleep_pins_b>; pinctrl-names = "default", "sleep"; #pwm-cells = <2>; status = "okay"; }; timer@0 { status = "disabled"; }; };
仿照设备树对于pwm管脚的配置添加timers1 pwm输出管脚配置,在stm32mp157a-fsmp1a-extended.dts文件pinctrl节点中添加pwm1管脚信息,红色字体内容为添加内容:
&pinctrl {
};
最后在根节点下添加风扇驱动对应节点:
fan { compatible = "pwm-fan"; #cooling-cells = <2>; pwms = <&pwm1 0 10000>; cooling-levels = <0 102 170 230>; };
linux@ubuntu:$ make -j4 uImage dtbs LOADADDR=0xC2000040
系统启动后可以查看对应目录
root@fsmp1a:~# ls -l /sys/class/hwmon/hwmon1/ total 0 lrwxrwxrwx 1 root root 0 Jun 30 12:01 device -> ../../../fan -r--r--r-- 1 root root 4096 Jun 30 12:01 name drwxr-xr-x 2 root root 0 Jun 30 12:01 power -rw-r--r-- 1 root root 4096 Jun 30 12:01 pwm1 -rw-r--r-- 1 root root 4096 Jun 30 12:00 uevent ……
可通过下列操作调整风扇转速: 可以写0~255之间的数字到文件/sys/class/hwmon/hwmon1/pwm1文件中调节转速 比如:
echo 200 > /sys/class/hwmon/hwmon1/pwm1
通过写入不同的值观察风扇的转速,如有条件可以通过示波器查看当前pwm的波形改变。