al177 / esp8089

Linux kernel module driver for the ESP8089 WiFi chip
GNU General Public License v2.0
180 stars 114 forks source link

Cross-compiling? #8

Closed tomkcook closed 7 years ago

tomkcook commented 7 years ago

Could you provide instructions to cross-compile this driver?

What I've done so far:

$ mkdir RasPi
$ git clone https://github.com/raspberrypi/tools
$ git clone --branch rpi-4.4.y --single-branch https://github.com/raspberrypi/linux.git
$ git clone https://github.com/al177/esp8089.git
... copy config.gz from pi zero to linux/.config ...
$ cd linux
$ git checkout raspberrypi-kernel_1.20170215-1
$ make KBUILD=~/RasPi/linux ARCH=arm CROSS_COMPILE=~/RasPi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf- oldconfig
$ make KBUILD=~/RasPi/linux ARCH=arm CROSS_COMPILE=~/RasPi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf- modules_prepare
$ cd ../esp8089
$ make KBUILD=~/RasPi/linux ARCH=arm CROSS_COMPILE=~/RasPi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-

This builds successfully. But when I try to insert the module on the Pi Zero:

$ sudo modprobe esp8089
modprobe: ERROR: could not insert 'esp8089': Exec format error

The Pi is running Raspbian 2017-03-02, kernel 4.4.50+.

Have you done this successfully? Any pointers to offer?

tomkcook commented 7 years ago

Never mind, sorry, I'm building it against the wrong kernel source version. I'll report back.

tomkcook commented 7 years ago

Okay, instructions for anyone who comes after me. Here's how I got this to work.

Get the source and tools

$ mkdir RasPi
$ git clone https://github.com/raspberrypi/tools
$ git clone --branch rpi-4.4.y --single-branch https://github.com/raspberrypi/linux.git
$ git clone https://github.com/al177/esp8089.git

Get the right version of the kernel

This is a bit tricky, because it seems the versions that get deployed in Raspbian images are not tagged, or at least not in any useful way. To get the right version, first you need to find the firmware hash of the installed system. So, on a running Pi:

zgrep "* firmware as of" /usr/share/doc/raspberrypi-bootloader/changelog.Debian.gz

The first line will say something like * firmware as of b51046a2b2bb69771579a549d157205d9982f858. That hash is a commit of the repository at https://github.com/raspberrypi/firmware. That repository contains a file, extra/git_hash, which contains the commit hash of the kernel that works with the given firmware. So if we look in the relevant version of that file, we can see which kernel commit to checkout. You can get the raw version of that file at https://raw.githubusercontent.com/raspberrypi/firmware/b51046a2b2bb69771579a549d157205d9982f858/extra/git_hash (make sure you substitute in the firmware hash for the version your raspberry pi is running).

That hash is e223d71ef728c559aa865d0c5a4cedbdf8789cfd. So now checkout that version of the kernel source:

$ cd linux
$ git checkout e223d71ef728c559aa865d0c5a4cedbdf8789cfd

Now we need to configure the kernel source. To do this, on your Pi:

$ sudo modprobe configs
$ zcat /proc/config.gz > .config

Now copy the file .config into ~/RasPi/linux on your build system. Then, on your build system:

$ make KBUILD=~/RasPi/linux ARCH=arm CROSS_COMPILE=~/RasPi/tools/arm-bcm2708/arm-bcm2708hardfp-linux-gnueabi/bin/arm-bcm2708hardfp-linux-gnueabi- oldconfig
$ make KBUILD=~/RasPi/linux ARCH=arm CROSS_COMPILE=~/RasPi/tools/arm-bcm2708/arm-bcm2708hardfp-linux-gnueabi/bin/arm-bcm2708hardfp-linux-gnueabi-

Wait while the kernel builds. You won't use the kernel that's built here, but you need one file it produces, Module.symvers. This file contains a CRC hash of each symbol available to kernel modules. It's used by the kernel build system to check that a module you're building has access to all the kernel symbols it needs.

Now you can build the ESP8089 driver:

$ cd ../esp8089
$ make KBUILD=~/RasPi/linux ARCH=arm CROSS_COMPILE=~/RasPi/tools/arm-bcm2708/arm-bcm2708hardfp-linux-gnueabi/bin/arm-bcm2708hardfp-linux-gnueabi-

Copy the resulting esp8089.ko to somewhere sensible on the Pi (eg /lib/modules/$(uname -r)/kernel/drivers/net/wireless) and then run depmod so that the kernel module loading system knows about it. You should now be able to load the module. Don't forget to tell the module which GPIO you've used for the reset, though:

$ sudo echo "options esp8089 esp_reset_gpio=1" > /etc/modprobe.d/esp8089.conf
$ modprobe esp8089
modprobe: ERROR: could not insert 'esp8089': No such device

And here's a bonus lesson for free: Don't forget to do something useful with the RESET pin of the ESP-12. Leaving it floating doesn't work.

tomkcook commented 7 years ago

Or doesn't always work.

EDIT: My bonus lesson was fudge. The ESP-12F module was entirely dead.