Maker.io main logo

Intro to Embedded Linux Part 7 - Enable WiFi Networking with Yocto

1,069

2021-11-01 | By ShawnHymel

License: Attribution Single Board Computers

In the previous tutorial, we showed you how to build a custom application into a Linux image using the Yocto Project. This time, we enable networking (specifically, WiFi) by including certain kernel modules in our image.

See here if you would like to view this tutorial in video format:

 

 

Required Hardware

I will try to explain what is happening at each step in these tutorials so that you can generalize the instructions to almost any single board computer (assuming the board is supported by the build system). However, for the demonstration, I will be using the following platform:

STM32MP157D-DK1

You will also need an SD card. The STM32MP157D-DK1 kit should come with an SD card. In addition, you will need a USB-C power supply capable of supplying 5V, 3A.

You will need a USB WiFi dongle for this tutorial. I am using a TP-Link TL-WN725N. Note that not all adapters will work with Linux--it will likely require some trial and error.

Required Software

You will need Linux for this project, as all of the tools we are using must be run in Linux (on the host computer). I will show steps that work in Ubuntu and Linux Mint (and likely other flavors of Debian), but you can probably get almost any Linux distro to work. LiveCD, dual-booting, Windows Subsystem for Linux (WSL), and pre-made Docker images will also likely work.

I also recommend using a fairly modern computer with at least 4GB of RAM. While you can probably build a Linux image in a Raspberry Pi, expect it to take a very long time.

Enable Kernel Modules

Look up the chipset for your USB WiFi adapter (using a site such as http://linux-wless.passys.nl/). For example, the chipset in my TL-WN725N is an rtl8188eu.

Enable the build environment:

Copy Code
cd ~/Projects/yocto
source poky/oe-init-build-env build-mp1

Load the kernel configuration tool:

Copy Code
bitbake -c menuconfig virtual/kernel 

Here, enable and disable the following settings:

Copy Code
Networking support > Networking options (following should be enabled):
    [*] TCP/IP networking
        [*] IP: kernel level autoconfiguration
            [*] IP: DHCP support
            [*] IP: BOOTP support
            [*] IP: RARP support
        <*> The IPv6 protocol
Networking support > Wireless
    <M> cfg80211 - wireless configuration API
        [*] cfg80211 wireless extensions compatibility
    <M> Generic IEEE 802.11 Networking Stack (mac80211)
    [*] Minstrel (for rate control in 802.11)
Device Drivers> Network device support > Wireless LAN
    [*] Realtek devices
        < > Realtek rtlwifi family of devices
            < > Realtek RTL8192CU/RTL8188CU USB Wireless Network Adapter
            [ ] Debugging output for rtlwifi driver family
        < > RTL8723AU/RTL8188[CR]U/RTL8192[12]CU (mac80211) support
            [ ] Include support for untested Realtek 8xxx USB devices (EXPERIMENTAL)
Device Drivers > USB support, make sure USB Host is enabled:
    <*> Support for Host-side USB
    [*] Enable USB persist by default
Device Drivers > Staging Drivers
    <M> Realtek RTL8188EU Wireless LAN NIC driver
        [ ] Realtek RTL8188EU AP mode (NEW)
[*] Enable loadable module support  --->
    [*]   Module signature verification
        [*]   Require modules to be validly signed
        [*]   Automatically sign all modules
        Which hash algorithm should modules be signed with? (SHA-256)

You can press ‘spacebar’ to enable or disable settings.

Configuring kernel modules

Note that these settings only apply to the rtl8188eu chipset! If you are using a different WiFi adapter, the settings will likely be different. It may take some time (days or weeks) to figure out exactly which modules you need to enable to get your WiFi working on your build. Sometimes, the adapter won’t work at all with Linux, so be patient and try several different adapters.

Make Kernel Changes Permanent

Right now, the kernel changes will only be in effect for the current build. You can save the configuration settings to your custom layer so that they become more permanent whenever you build custom-image. To do that, create a default configuration file (defconfig) with the current settings:

Copy Code
bitbake -c savedefconfig virtual/kernel

Copy the defconfig file to your custom layer:

Copy Code
cd ../meta-custom
mkdir -p recipes-kernel/linux/files
cp ../build_mp1/tmp/work/stm32mp1-poky-linux-gnueabi/linux-stm32mp/5.10.10-r0/build/defconfig recipes-kernel/linux/files/defconfig

Next, we need to edit the kernel recipe to include our new configuration file:

Copy Code
vi recipes-kernel/linux/linux-stm32mp_%.bbappend

This should have a few lines from when we enabled the I2C port. Change the FILESEXTRAPATHS_prepend variable to include the new files directory in the search path. Additionally, add the KERNEL_DEFCONFIG_<machine> variable with the name of the defconfig file. The recipe should look like the following:

Copy Code
FILESEXTRAPATHS_prepend := "${THISDIR}:${THISDIR}/files:"
SRC_URI += "file://0001-add-i2c5-userspace-dts.patch"

# Apply default configuration
KERNEL_DEFCONFIG_stm32mp1 = "defconfig"

Note that the name (“defconfig”) and location of the file (files directory) are very important! Do not change them--bitbake looks for a very particular filename when using a kernel configuration file from a layer. Additionally, the KERNEL_DEFCONFIG variable should have the MACHINE name appended to the end (e.g. “_stmp32mp1”).

Kernel recipe in Yocto Project

Save and exit.

In addition to enabling these modules in the kernel, we also need to include them along with some tools/firmware in our image. To do that, we must update the IMAGE_INSTALL variable. Also, we want to autoload the rtl8188eu driver (known as “r8188eu”) whenever Linux boots. This saves us from having to call modprobe after every reboot.

Edit the custom-image.bb recipe:

Copy Code
vi recipes-core/images/custom-image.bb

Add the following lines to the end:

Copy Code
# Add kernel module for USB WiFi driver
IMAGE_INSTALL += "kernel-module-r8188eu \
                  linux-firmware-rtl8188 \
                  dhcp-client \
                  iw \
                  wpa-supplicant \
                  wireless-regdb-static"

# Autoload WiFi driver on boot
KERNEL_MODULE_AUTOLOAD += “r8188eu”

Your recipe should look like the following:

Image recipe in Yocto Project

Save and exit.

Build and Deploy

Our configuration should be complete, so it’s time to build the image:

Copy Code
cd ../build-mp1
bitbake custom-image

This will take some time (1-2 hours likely), so be patient.

When the build process is finished, flash the bootfs and rootfs onto the SD card as shown at the end of Part 4 (we need to flash bootfs as we made changes to the kernel).

Don’t forget to change bootfs/mmc0_extlinux/extlinux.conf so that root points to /dev/mmcblk05!

Change rootfs partition in extlinux conf file

Test WiFi

Plug the SD card into your STM32MP157D-DK1 and boot it up. Connect to the serial terminal (you may need to change ttyACM0 to some other device file, depending on your particular flavor of Linux and hardware setup):

Copy Code
picocom -b 115200 /dev/ttyACM0

Log in to the board with “root” and “toor” as the password. List the available network interfaces:

Copy Code
ifconfig -a

Bring up your wireless interface:

Copy Code
ifconfig wlan0 up

Scan the available networks (this may or may not work with some chipsets):

Copy Code
iw wlan0 scan

Add your network credentials to the wpa_supplicant.conf file:

Copy Code
wpa_passphrase “<SSID>” “<WiFi Password>” >> /etc/wpa_supplicant.conf

I recommend removing the commended out “#psk” variable in the wpa_supplicant.conf file, as it contains your password in plaintext:

Copy Code
vi /etc/wpa_supplicant.conf

As shown in the screenshot, remove the #psk… line.

wpa_supplicant configuration file

Run the wpa_supplicant tool:

Copy Code
wpa_supplicant -B -i wlan0 -D wext -c /etc/wpa_supplicant.conf

Check the link status to see if your board is connected to your access point (this may or may not work depending on your particular chipset):

Copy Code
iw dev wlan0 link

Get an IP address from your DHCP server (likely your router):

Copy Code
dhclient wlan0

Do network stuff!

Copy Code
ping -c 3 8.8.8.8
wget -qO - http://example.com

If all goes well, you should be able to communicate with other devices/servers on the Internet.

Fetching a web page with wget

Recommended Reading

The following material should help you as you continue your embedded Linux journey:

I found the following guides helpful when creating this tutorial:

ST has a great workshop series on the STM32MP1 where you can learn more about the hardware, drivers, Linux, etc.

The Yocto Project YouTube channel has a number of videos like this one to help you get started.

Robert Nelson at Digi-Key maintains a great series of getting started guides with Yocto for various boards that you can view here.

制造商零件编号 STM32MP157D-DK1
DISCOVERY STM32MP157D EVAL BRD
STMicroelectronics
Add all DigiKey Parts to Cart
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.