解決樹莓派新內核無法使用18B20和沒有聲音的問題


現在新版的樹莓派內核由於為了兼容樹莓派2和樹莓派B+等以前的版本,采用了和原來不同的內核運行方式,使用了設備樹的方式,更加靈活。但是由於可能不習慣這樣的方式以及沒太多相關這方面的介紹,導致很多用戶更新了內核后出現比如接18B20無法讀取設備信息,以及樹莓派沒有聲音輸出的問題。
在這里細心的用戶就會發現在新內核boot分區下多了個overlays文件夾,里面有很多dtb文件,這些其實就是樹莓派的設備樹,打開README,就可以知道該怎么使用了。

1

下面是來自README的話:

Introduction
============
This directory contains Device Tree overlays. Device Tree makes it possible
to support many hardware configurations with a single kernel and without the
need to explicitly load or blacklist kernel modules. Note that this isn't a
"pure" Device Tree configuration (c.f. MACH_BCM2835) - some on-board devices
are still configured by the board support code, but the intention is to
eventually reach that goal.
On Raspberry Pi, Device Tree usage is controlled from /boot/config.txt. By
default, the Raspberry Pi kernel boots with device tree enabled. You can
completely disable DT usage (for now) by adding:
    device_tree=
to your config.txt, which should cause your Pi to revert to the old way of
doing things after a reboot.

如果要禁用設備樹就直接在boot/config.txt里寫device_tree=然后保存重啟即可。
那么我們要怎么使用設備樹呢?就拿啟用W1來讀取18B20數據來說,Readme里有這樣一段:

Using Overlays
==============
Overlays are loaded using the "dtoverlay" directive. As an example, consider the
popular lirc-rpi module, the Linux Infrared Remote Control driver. In the
pre-DT world this would be loaded from /etc/modules, with an explicit
"modprobe lirc-rpi" command, or programmatically by lircd. With DT enabled,
this becomes a line in config.txt:
    dtoverlay=lirc-rpi
This causes the file /boot/overlays/lirc-rpi-overlay.dtb to be loaded. By
default it will use GPIOs 17 (out) and 18 (in), but this can be modified using
DT parameters:
    dtoverlay=lirc-rpi,gpio_out_pin=17,gpio_in_pin=13
Parameters always have default values, although in some cases (e.g. "w1-gpio")
it is necessary to provided multiple overlays in order to get the desired
behaviour. See the list of overlays below for a description of the parameters and their defaults.

也就是說,在使用config.txt里添加dtoverlay=【設備樹名】來啟動設備樹,同樣也可以在這里指定加載的設備樹參數,實際上這個操作就和之前的內核在系統里添加模塊類似,只不過這里是在內核啟動的時候加載,之前的是在系統啟動后加載。
那么我們就可以照搬添加w1到config.txt里面然后重啟讀取18B20了,Readme里也有相關參數說明:

Name:   w1-gpio
Info:   Configures the w1-gpio Onewire interface module.
        Use this overlay if you *don't* need a GPIO to drive an external pullup.
Load:   dtoverlay=w1-gpio,<param>=<val>
Params: gpiopin                  GPIO for I/O (default "4")
        pullup                   Non-zero, "on", or "y" to enable the parasitic
                                 power (2-wire, power-on-data) feature
Name:   w1-gpio-pullup
Info:   Configures the w1-gpio Onewire interface module.
        Use this overlay if you *do* need a GPIO to drive an external pullup.
Load:   dtoverlay=w1-gpio-pullup,<param>=<val>,...
Params: gpiopin                  GPIO for I/O (default "4")
        pullup                   Non-zero, "on", or "y" to enable the parasitic
                                 power (2-wire, power-on-data) feature
        extpullup                GPIO for external pullup (default "5")

這里提供了更多的參數進行選擇,如果你打算采用寄生的連接方式這里也提供了參數實現內部上拉,當然如果你想保留原來的連接電路方式不進行電路改動的話那么我們就這樣設置好了:
在config.txt里插入下面的內容:
dtoverlay=w1-gpio-pullup,gpiopin=4
這樣就可以按照原來的電路連接方式連接18B20,保存重啟后一樣的載入w1-gpio和w1-therm后就可以讀取到18B20數據了。
如果你要啟用樹莓派的音頻的話,在里面添加一行
audio=on即可,新版的內核禁用了audio的原因是因為樹莓派PWM和audio使用的是同一個接口,可能介於很多人希望使用樹莓派的硬件PWM的原因,所以才這樣靈活的設計讓大家有更多自由使用的空間。
Readme關於audio和pwm的原文:

he Overlay and Parameter Reference
===================================
N.B. When editing this file, please preserve the indentation levels to make it simple to parse
programmatically. NO HARD TABS.
Name:   <The base DTB>
Info:   Configures the base Raspberry Pi hardware
Load:   <loaded automatically>
Params:
        audio                    Set to "on" to enable the onboard ALSA audio
                                 interface (default "off")

====================

Name:   pwm
Info:   Configures a single PWM channel
        Legal pin,function combinations for each channel:
          PWM0: 12,4(Alt0) 18,2(Alt5) 40,4(Alt0)            52,5(Alt1)
          PWM1: 13,4(Alt0) 19,2(Alt5) 41,4(Alt0) 45,4(Alt0) 53,5(Alt1)
        N.B.:
          1) Pin 18 is the only one available on all platforms, and
             it is the one used by the I2S audio interface.
             Pins 12 and 13 might be better choices on an A+, B+ or Pi2.
          2) The onboard analogue audio output uses both PWM channels.
          3) So be careful mixing audio and PWM.
          4) Currently the clock must have been enabled and configured
             by other means.
Load:   dtoverlay=pwm-2chan,<param>=<val>
Load:   dtoverlay=pwm,<param>=<val>
Params: pin                      Output pin (default 18) - see table
        func                     Pin function (default 2 = Alt5) - see above
        clock                    PWM clock frequency (informational)
Name:   pwm-2chan
Info:   Configures both PWM channels
        Legal pin,function combinations for each channel:
          PWM0: 12,4(Alt0) 18,2(Alt5) 40,4(Alt0)            52,5(Alt1)
          PWM1: 13,4(Alt0) 19,2(Alt5) 41,4(Alt0) 45,4(Alt0) 53,5(Alt1)
        N.B.:
          1) Pin 18 is the only one available on all platforms, and
             it is the one used by the I2S audio interface.
             Pins 12 and 13 might be better choices on an A+, B+ or Pi2.
          2) The onboard analogue audio output uses both PWM channels.
          3) So be careful mixing audio and PWM.
          4) Currently the clock must have been enabled and configured
             by other means.
Load:   dtoverlay=pwm-2chan,<param>=<val>
Params: pin                      Output pin (default 18) - see table
        pin2                     Output pin for other channel (default 19)
        func                     Pin function (default 2 = Alt5) - see above
        func2                    Function for pin2 (default 2 = Alt5)
        clock                    PWM clock frequency (informational)

更多信息大家自己去閱讀/boot/overlays/README


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM