My blink1-tool command-line program is very cross-platform. It currently supports Mac OS X 10.6.8-10.10.6, Windows XP-10, FreeBSD 10+, most Linux distros including Ubuntu, RedHat, OpenWrt, Raspberry Pi, BeagleBone, and Linino Arduino Yun. And I want it to work for C.H.I.P.
The NextThingCo C.H.I.P. runs a tiny Linux similar to OpenWrt or Arduino Yun. For these systems, you don’t compile on the device itself but instead “cross-compile”: run a compiler on a computer that generates binaries for the target system. If you’ve ever played with Arduino, you’re invoking the avr-gcc cross-compiler for the AVR chip on the Arduino board.
Setting up cross-compile with “buildroot” setups
In these small Linuxes, you use a system of Makefiles and cross-compiler suite called buildroot to build your kernel, select kernel features, and choose libraries and programs to build into your disk image. With C.H.I.P., NextThingCo has made things easy by creating a pre-setup CHIP-buildroot and good instructions on how to get a tailored VM to run it.
Generally to compile your own program, you want to create a package for use by buildroot. I find this a bit weighty, so I just create a section of my existing Makefile for new systems like CHIP.
The two things you need to know when doing it this way:
- Path of cross-compiler (CC) and linker (LD)
- Path of any cross-compiled libraries your program needs (CFLAGS & LIBS)
Once I get the cross-compile system built with the libraries I need, I set up a Makefile sort of like this:
CCBASEDIR=/home/vagrant/CHIP-buildroot/output/host TARGET_DIR=$(CCBASEDIR)/usr/arm-buildroot-linux-gnueabihf/sysroot CC = $(CCBASEDIR)/usr/bin/arm-linux-gnueabihf-gcc LD = $(CCBASEDIR)/usr/bin/arm-linux-gnueabihf-ld CFLAGS += -I$(TARGET_DIR)/usr/include/libusb-1.0 LIBS += -L$(TARGET_DIR)/usr/lib -lusb-1.0
The above is showing what I discovered for CHIP-buildroot running inside the vagrant VM they’ve defined. For a simple hello world program that talks libusb, the above’s all you need. Here’s some additional discussion on simple cross-compiling for CHIP.
CHIP buildroot configuration
Unfortunately, CHIP’s firmware is currently highly pared down, so to get libusb I went to the buildroot config and added “libusb” as a package to build.
% cd CHIP-SDK % vagrant up && vagrant ssh $ cd CHIP-buildroot $ make chip_defconfig $ make menuconfig
In menuconfig (pictured below), make the changes:
- Build options -> libraries (both static and shared) -> both static & shared
- Target packages -> Libraries -> Hardware handling -> libusb
$ make toolchain $ make libusb $ make # optional,builds new firmware & selected packages, ~60 mins
The results, sigh, *this close!*
At this point I could hack up the blink1-tool Makefile and start compiling. And… It worked! It compiled! I excitedly ‘scp’d the resulting binary to my CHIP, and blink1-tool --help
and… It printed!
But when I tried scanning for USB devices, it came up empty. I’ve had this problem before where a Linux libusb-based program compiles and runs but libusb can’t see any devices. Generally recompiling with the “right” libusb solves the problem, but I’m not sure what that means here.
Time to go back to a simple libusb “hello world” with maximum error checking turned on and see what’s what.