Tuesday, September 25, 2018

Shutdown Button with Device Tree Overlays

I want to create shutdown button for raspberry pi.
I found gpio-shutdown parameter for enable Device Tree Overlays on new firmware.

Name:   gpio-shutdown
Info:   Initiates a shutdown when GPIO pin changes. The given GPIO pin
        is configured as an input key that generates KEY_POWER events.
        This event is handled by systemd-logind by initiating a
        shutdown. Systemd versions older than 225 need an udev rule
        enable listening to the input device:

                ACTION!="REMOVE", SUBSYSTEM=="input", KERNEL=="event*", \
                        SUBSYSTEMS=="platform", DRIVERS=="gpio-keys", \
                        ATTRS{keys}=="116", TAG+="power-switch"

        This overlay only handles shutdown. After shutdown, the system
        can be powered up again by driving GPIO3 low. The default
        configuration uses GPIO3 with a pullup, so if you connect a
        button between GPIO3 and GND (pin 5 and 6 on the 40-pin header),
        you get a shutdown and power-up button.
Load:   dtoverlay=gpio-shutdown,<param>=<val>
Params: gpio_pin                GPIO pin to trigger on (default 3)

        active_low              When this is 1 (active low), a falling
                                edge generates a key down event and a
                                rising edge generates a key up event.
                                When this is 0 (active high), this is
                                reversed. The default is 1 (active low).

        gpio_pull               Desired pull-up/down state (off, down, up)
                                Default is "up".

                                Note that the default pin (GPIO3) has an
                                external pullup.
Reference: https://github.com/raspberrypi/firmware/tree/master/boot/overlays


Step to create shutdown switch

  • Connect switch to GPIO3.





  • Add config device tree overlays on /boot/config.txt.


dtoverlay=gpio-shutdown,gpio_pin=3,active_low=1,gpio_pull=up


  •  Create file /etc/udev/rules.d/99-gpio-power.rules


ACTION!="REMOVE", SUBSYSTEM=="input", KERNEL=="event*", \
                        SUBSYSTEMS=="platform", DRIVERS=="gpio-keys", \
                        ATTRS{keys}=="116", TAG+="power-switch"


  • And restart raspberry pi.


After reboot success. I try to push switch.
It works ;). My raspberry pi is shutdown.

Hope to help you.

*** I2C must be deactivated if you use pin 3. (Thank you @Irmoreno007)

Wednesday, July 25, 2018

Mantistek WA150 (WIFI+Bluetooth) RTL8723BU

I buy Mantistek WA150 to use on Raspberry PI. It has both WIFI and Bluetooth.




The raspbian kernel version 4.14.52+ #1123. It hasn't driver to use it.

I use command.

dmesg

I found. Raspbian can detect bluetooth device but it isn't have firmware. (rtl8723b_fw.bin)

[   14.665558] bluetooth hci0: Direct firmware load for rtl_bt/rtl8723b_config.bin failed with error -2
[   14.665824] bluetooth hci0: Direct firmware load for rtl_bt/rtl8723b_fw.bin failed with error -2
[   14.665842] Bluetooth: hci0: Failed to load rtl_bt/rtl8723b_fw.bin

I use command for check usb device id.

lsusb
I found.
Bus 001 Device 004: ID 0bda:b720 Realtek Semiconductor Corp.

The USB Device ID is 0bda:b720.

I search device id for identify device. I found RTL8723BU (not RTL8723AU).

I found driver and firmware.

 WIFI Driver (Source code)


Bluetooth Firmware



Install Step
First step. Install linux kernel header package and other package for raspbian.
$ sudo apt-get install raspberrypi-kernel-headers git build-essential

Create directory for build source and download source code

$ mkdir drivers
$ cd drivers

Install RTL8723BU WIFI Driver
$ git clone https://github.com/lwfinger/rtl8723bu.git
$ cd rtl8723bu

Disable Concurrent Mode
$ sed -i "s/^EXTRA_CFLAGS += -DCONFIG_CONCURRENT_MODE/#EXTRA_CFLAGS += -DCONFIG_CONCURRENT_MODE/g" Makefile

Build and Install
$ make
$ sudo make install

Disable power save
$ echo "options 8723bu rtw_power_mgnt=0 rtw_enusbss=0" | sudo tee /etc/modprobe.d/8723bu.conf

Install RTL8723B Bluetooth Driver

$ git clone https://github.com/lwfinger/rtl8723au_bt.git
$ cd rtl8723au_bt/Linux_BT_USB_2.11.20140423_8723BE/8723B
sudo cp rtl8723b_config /lib/firmware/rtl_bt/rtl8723b_config.bin
sudo cp rtl8723b_fw /lib/firmware/rtl_bt/rtl8723b_fw.bin

And restart raspberry pi

Test bluetooth scan with command.

$ hcitool scan
Scanning ...
          XX:XX:XX:XX:XX:XX       xxxxxx
          XX:XX:XX:XX:XX:XX       xxxxxx

It work.

And check WIFI interface with command.

$ ifconfig

It work. I found wlan0 interface.

wlan0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        ether xx:xx:xx:xx:xx:xx  txqueuelen 1000  (Ethernet)
        RX packets 17  bytes 0 (0.0 B)
        RX errors 0  dropped 80  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

And try to config wpa_supplicant-wlan0.conf for wlan0 interface

$ echo -e "ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev\nupdate_config=1\ncountry=TH\n\n" | sudo tee /etc/wpa_supplicant/wpa_supplicant-wlan0.conf
 Add SSID and Password of Accesspoint to wpa_supplicant config file
wpa_passphrase test 12345678 | sudo tee -a /etc/wpa_supplicant/wpa_supplicant-wlan0.conf

And restart raspbian. Finish step.

Have a good day everyone. 😎



Reference:
WIFI Driver (Source code)
Bluetooth Firmware
Realtek RTL8723BU