# cd serial # modprobe usbserial # cd ../class # modprobe cdc-acm.ko #
Cross compile for MIPS architecture: How to building USBSerial kernel’s module for FritxBox 7270 to comunicate with Arduino.
I have a FritxBox 7270 v2 (Firmware 54.05.23), a great piece of hardware, it is an ADSL modem, router, voip…and have a USB port where you can connect an USB PenDrive or a GSM Modem to create a fault tollerant internet connection.
I am passionate about electronic, and Arduino is another great platform to play with.
Arduino has an USB connector for serial communication (with onboard USB/Serial converter) and power supply.
The idea: connecting Arduino with FritxBox via SerialPort with the advantage of: One power supplier for two device, expose Arduino serial port over internet/intranet avoiding to buy ethernet shild for Arduino and a busy port of FritxBox switch.
But we can’t limit to Arduino, I think all USB Serial device can comunicate with FritzBox only installing the correct kernel modules.
But you need a USB Hub, because we need two USB port from FritxBox, one for Arduino and another for a USB PenDrive where we store the files needed (kernel modules) for let reconize the Arduino’s USB Serial Port by FritxBox.
Another happy point is that you don’t need to flash you Fritz.
Download and unzip usb_serial_kernel_modules_fritxbox.zip. Copy the folder usbserial and class in a USB PenDrive.
Start FritxBox telnet by call *78*8 from a phone attached to FritxBox.
Attach USB Hub to FritxBox and the PenDrive to Hub.
Login telnet on FritxBox and go to /var/media/ftp/PENDRIVE/usbserial (where PENDRIVE is the label of your PenDrive)
Digit:
# cd serial # modprobe usbserial # cd ../class # modprobe cdc-acm.ko #
Attach the Arduino to the USB Hub, now you can see the /dev/ttyACM0, the serial port of Arduino.
Do it all yourself is a long way, but learn more things, and you have the possibility to expand the functionality of your great FritzBox by develop C program that can run in your modem. I known that isn’t easy, but isn’t impossible!
What you need: A Linux box (real or virtual), a littel experience with C programming languare, experience with Linux and kernel compilation, few hours of time, a PenDrive, an USB Hub and an Arduino of course!
Connect the PenDrive to Hub and the Hub to FritxBox. Configure with the Web interface the FritxBox to show the PenDrive with FTP Nas functionality. In all example below the label of my PenDrive is Kingston-DataTraveler2-0-01.
Let’s go!
From a phone attached to FritxBox compose #96*7* to enable the telnet service, this is my console:
[dometec@precision2 ~]$ telnet 192.168.1.1 Trying 192.168.1.1... Connected to 192.168.1.1. Escape character is '^]'. Fritz!Box web password: BusyBox v1.18.5 (2012-03-27 14:03:03 CEST) built-in shell (ash) Enter 'help' for a list of built-in commands. ermittle die aktuelle TTY tty is "/dev/pts/0" Console Ausgaben auf dieses Terminal umgelenkt #
Do a cat of /proc/cpuinfo to see the detail of CPU:
# cat /proc/cpuinfo system type : TI UR8 (7270) processor : 0 cpu model : MIPS 4KEc V6.8 BogoMIPS : 359.62 wait instruction : yes microsecond timers : yes tlb_entries : 16 extra interrupt vector : yes hardware watchpoint : no ASEs implemented : shadow register sets : 1 core : 0 VCED exceptions : not available VCEI exceptions : not available #
The CPU is a MIPS 4KEc V6.8 at 360MHz, which is a MIPS32 enhanced (Release 2) architecture.
Optionally, for another check, you can copy an executable file from FritxBox to PenDrive and back to Linux box and do a file command to see more info on ELF Format:
# cp /usr/bin/dsl_info /var/media/ftp/Kingston-DataTraveler2-0-01/
And from PC:
[dometec@precision2 ~]$ cd /tmp/ [dometec@precision2 tmp]$ wget --password='xxxx' ftp://[email protected]/Kingston-DataTraveler2-0-01/dsl_info [dometec@precision2 tmp]$ file dsl_info dsl_info: ELF 32-bit LSB executable, MIPS, MIPS32 rel2 version 1 (SYSV), dynamically linked (uses shared libs), with unknown capability 0xf41 = 0x756e6700, with unknown capability 0x70100 = 0x3040000, stripped [dometec@precision2 tmp]$
Now we are sure that it’s a MIPS architecture rel2, 32 bit.
We need other three info: endianness of CPU, kernel version and version of C library
used (the FritxBox use the uCLibC instead of standard glibc, uClibc is used when you don’t have enougth space as in this device).
This model of CPU is little endian see here.
The version of kernel is 2.6.32.41:
# uname -a Linux fritz.fonwlan.box 2.6.32.41 #1 Mon Jun 4 16:38:22 CEST 2012 mips GNU/Linux #
The version of uCLibC is 0.9.32 as you can see from:
# ls -l /lib/ | grep uCl -rwxrwxrwx 1 root root 22660 Jul 26 2012 ld-uClibc-0.9.32.so lrwxrwxrwx 1 root root 19 Jul 26 2012 ld-uClibc.so.0 -> ld-uClibc-0.9.32.so lrwxrwxrwx 1 root root 19 Jul 26 2012 libc.so.0 -> libuClibc-0.9.32.so -rwxrwxrwx 1 root root 659996 Jul 26 2012 libuClibc-0.9.32.so
Recap the info collected that we need for next step:
Architecture: MIPS 32bit
Version: Mips32r2 littel endian
uClibc version: 0.9.32
Kernel version: 2.6.32.41
The next operation are execute in a fresh installation of Fedora 16 64bit.
[dometec@localhost ~]$ mkdir Downloads/ [dometec@localhost ~]$ cd Downloads/ [dometec@localhost Downloads]$ wget http://www.kernel.org/pub/linux/kernel/v2.6/longterm/v2.6.32/linux-2.6.32.41.tar.bz2 [dometec@localhost Downloads]$ tar xjf linux-2.6.32.41.tar.bz2
Install the gcc compiler:
[dometec@localhost Downloads]$ su - Password: [root@localhost ~]# yum install gcc [root@localhost ~]# yum -y install gcc gcc-c++ bison flex gperf texinfo patch libtool ncurses-devel expat expat-devel . . . [root@localhost Downloads]# exit
Download the latest release (1.18.0 at this time) and extract it on a folder:
[dometec@localhost Downloads]$ wget http://www.crosstool-ng.org/download/crosstool-ng/crosstool-ng-1.18.0.tar.bz2 [dometec@localhost Downloads]$ tar xjf crosstool-ng-1.18.0.tar.bz2 [dometec@localhost Downloads]$ cd crosstool-ng-1.18.0/ [dometec@localhost crosstool-ng-1.18.0]$ mkdir $HOME/bin/ [dometec@localhost crosstool-ng-1.18.0]$ mkdir $HOME/bin/crosstool-ng [dometec@localhost crosstool-ng-1.18.0]$ ./configure --prefix=$HOME/bin/crosstool-ng [dometec@localhost crosstool-ng-1.18.0]$ make [dometec@localhost crosstool-ng-1.18.0]$ make install [dometec@localhost crosstool-ng-1.18.0]$ export PATH="${PATH}:$HOME/bin/crosstool-ng/bin" [dometec@localhost crosstool-ng-1.18.0]$
Now we have to create the tool chain for our architecture, we start with mipsel-unknown-linux-gnu default configuration:
[dometec@localhost crosstool-ng-1.18.0]$ mkdir $HOME/toolchain [dometec@localhost crosstool-ng-1.18.0]$ cd $HOME/toolchain [dometec@localhost toolchain]$ ct-ng mipsel-unknown-linux-gnu
now we personalize the version of Target Options and Libraries of the toolchain:
[dometec@localhost toolchain]$ ct-ng menuconfig
And go to Target options and sure that:
Target Architecture (mips)
Endianness: (Little endian)
Bitness: (32-bit)
ABI (o32)
Go to Operating System:
Select Linux kernel version (custom tarball or directory)
and set the dir of just extracted kernel ($HOME/Downloads/linux-2.6.32.41) Path to custom source, tarball or directory
Go to C-library and select:
C library (uClibc)
uClibc version (0.9.32)
Go to Debug facilities:
enable only strace
ESC → ESC → and SAVE
Now we have to build the tool chain:
[dometec@localhost toolchain]$ ct-ng build . . .
Go forward until the buil end with the error:
[ERROR] You did not provide a uClibc config file!
Yes, we have to configure uClibc now that is downloaded and extracted:
[dometec@localhost toolchain]$ cd .build/src/uClibc-0.9.32/ [dometec@localhost uClibc-0.9.32]$ make menuconfig
Select:
Target Architecture (mips)
In Target Architecture Features and Options select:
Target Processor Architecture (MIPS II)
Target Processor Endianness (Little Endian)
ESC → ESC → and SAVE
run againg menuconfig of ct-ng:
[dometec@localhost uClibc-0.9.32]$ cd .. [dometec@localhost src]$ cd .. [dometec@localhost .build]$ cd .. [dometec@localhost toolchain]$ ct-ng menuconfig
And set in C-library → configuration file with: $HOME/toolchain/.build/src/uClibc-0.9.32/.config
ESC → ESC → and SAVE
and rebuil toolchain:
[dometec@localhost toolchain]$ ct-ng build . . .
Now we have all tool for cross compile installed in $HOME/x-tools/. Update the PATH environment:
[dometec@localhost toolchain]$ cd .. [dometec@localhost ~]$ export PATH="${PATH}:$HOME/x-tools/mipsel-unknown-linux-uclibc/bin" [dometec@localhost ~]$
For a very simple test we can compile a HelloWord C programm and compile it for MIPS Architecture.
[dometec@localhost ~]$ echo -e "#include<stdio.h>\n\nmain() {\n\tprintf(\"Hello World on MIPS.\\\n\");\n}" > hello.c [dometec@localhost ~]$ mipsel-unknown-linux-uclibc-gcc hello.c -o hello [dometec@localhost ~]$ copy the file hello to PenDrive and execute it on FritxBox: # ./hello Hello World on MIPS. This is the md5 of hello compiled file, if you have any problem compare this md5 with md5 of your compiled hello programm. # md5sum hello cc226dfd058ba585d056e43283a8699d hello # |
[dometec@localhost ~]$ cd Downloads/ [dometec@localhost Downloads]$ cd linux-2.6.32.41/ [dometec@localhost Downloads]$ export ARCH=mips [dometec@localhost linux-2.6.32.41]$ export CROSS_COMPILE=$HOME/x-tools/mipsel-unknown-linux-uclibc/bin/mipsel-unknown-linux-uclibc- [dometec@localhost linux-2.6.32.41]$ make menuconfig
Under Machine selection:
Select System type (MIPS Malta board)
Under Linux Kernel Configuration:
Select Endianess selection (Little endian)
Under CPU selection:
Select CPU type (MIPS32 Release 2)
Under Device Driver → USB support:
Select <M> USB Serial Converter support and ensure that all driver are as modules.
ESC → ESC → and SAVE
Now compile the kernel:
[dometec@localhost linux-2.6.32.41]$ make . .
If you get this error:
HOSTCC Documentation/video4linux/v4lgrab Documentation/video4linux/v4lgrab.c:34:28: fatal error: linux/videodev.h: File o directory non esistente compilation terminated. make[2]: *** [Documentation/video4linux/v4lgrab] Errore 1 make[1]: *** [Documentation/video4linux] Errore 2 make: *** [vmlinux] Errore 2 [dometec@localhost linux-2.6.32.41]$ Remove video4linux/ from file Documentation/Makefile.orig (in the last line): [dometec@localhost linux-2.6.32.41]$ cat Documentation/Makefile.orig obj-m := DocBook/ accounting/ auxdisplay/ connector/ \ filesystems/configfs/ ia64/ networking/ \ pcmcia/ spi/ video4linux/ vm/ watchdog/src/ See https://patchwork.kernel.org/patch/1009812/ for more info. |
drivers/usb/class
drivers/usb/serial
on PenDrive and from FritxBox telnet console do:
# cd serial # modprobe usbserial # cd ../class # modprobe cdc-acm.ko #
We have DONE!
Now load into Arduino a sketch that write something on serial, I use the ASCIITable example from Arduino IDE, and attach Arduino to USB Hub. You can see a new device, ttyACM0:
# ls /dev/tty* /dev/tty /dev/ttyACM0 /dev/ttyS1 /dev/ttyp1 /dev/ttyp3 /dev/ttyp5 /dev/ttyp7 /dev/ttyp9 /dev/ttypb /dev/ttypd /dev/ttypf /dev/tty0 /dev/ttyS0 /dev/ttyp0 /dev/ttyp2 /dev/ttyp4 /dev/ttyp6 /dev/ttyp8 /dev/ttypa /dev/ttypc /dev/ttype #
cat from /dev/ttyACM0 to see the output:
# cat /dev/ttyACM0 AASCII Table ~ Character Map !, dec: 33, hex: 21, oct: 41, bin: 100001 ", dec: 34, hex: 22, oct: 42, bin: 100010 #, dec: 35, hex: 23, oct: 43, bin: 100011 . . .
Now you can comunicate with Arduino from internet or intranet without use the Ehetnet shild!
I look forward to your feedback!
If something go wrong, you can find some log on /dev/debug:
# cat /dev/debug . . |
[dometec@localhost ~]$ mkdir crosstmp/ [dometec@localhost crosstmp]$ [dometec@localhost crosstmp]$ wget http://www.crosstool-ng.org/download/crosstool-ng/crosstool-ng-1.18.0.tar.bz2 --2013-02-12 15:36:30-- http://www.crosstool-ng.org/download/crosstool-ng/crosstool-ng-1.18.0.tar.bz2 Risoluzione di www.crosstool-ng.org... 140.211.15.107 Connessione a www.crosstool-ng.org|140.211.15.107|:80... connesso. HTTP richiesta inviata, in attesa di risposta... 200 OK Lunghezza: 1884219 (1,8M) [application/x-bzip] Salvataggio in: "crosstool-ng-1.18.0.tar.bz2" 100%[=============================================================================================>] 1.884.219 371K/s in 5,6s 2013-02-12 15:36:36 (327 KB/s) - "crosstool-ng-1.18.0.tar.bz2" salvato [1884219/1884219] [dometec@localhost crosstmp]$ tar xjf crosstool-ng-1.18.0.tar.bz2 [dometec@localhost crosstmp]$ cd crosstool-ng-1.18.0/ [dometec@localhost crosstool-ng-1.18.0]$ mkdir ~/bin/ [dometec@localhost crosstool-ng-1.18.0]$ mkdir ~/bin/crosstool-ng [dometec@localhost crosstool-ng-1.18.0]$ ./configure --prefix=$HOME/bin/crosstool-ng checking build system type... x86_64-unknown-linux-gnu checking host system type... x86_64-unknown-linux-gnu checking for a BSD-compatible install... /usr/bin/install -c checking for grep that handles long lines and -e... /bin/grep checking for egrep... /bin/grep -E checking for a sed that does not truncate output... /bin/sed checking whether sed understands -r -i -e... yes checking whether ln -s works... yes checking for gcc... gcc checking whether the C compiler works... yes checking for C compiler default output file name... a.out checking for suffix of executables... checking whether we are cross compiling... no checking for suffix of object files... o checking whether we are using the GNU C compiler... yes checking whether gcc accepts -g... yes checking for gcc option to accept ISO C89... none needed checking how to run the C preprocessor... gcc -E checking for ranlib... ranlib checking for objcopy... objcopy checking for absolute path to objcopy... /usr/bin/objcopy checking for objdump... objdump checking for absolute path to objdump... /usr/bin/objdump checking for readelf... readelf checking for absolute path to readelf... /usr/bin/readelf checking for bison... bison checking for flex... flex checking for gperf... gperf checking for makeinfo... makeinfo checking for cut... cut checking for stat... stat checking for readlink... readlink checking for wget... wget checking for tar... tar checking for gzip... gzip checking for bzip2... bzip2 checking for patch... /usr/bin/patch checking for bash >= 3.1... /bin/bash checking for GNU awk... /bin/awk checking for GNU make >= 3.80... /usr/bin/make checking whether /usr/bin/make sets $(MAKE)... yes checking for GNU libtool >= 1.5.26... /usr/bin/libtool checking for GNU libtoolize >= 1.5.26... /usr/bin/libtoolize checking for GNU automake >= 1.10... /usr/bin/automake checking for xz... xz checking for cvs... no checking for svn... no checking for inline... inline checking for ANSI C header files... yes checking for sys/types.h... yes checking for sys/stat.h... yes checking for stdlib.h... yes checking for string.h... yes checking for memory.h... yes checking for strings.h... yes checking for inttypes.h... yes checking for stdint.h... yes checking for unistd.h... yes checking for stdlib.h... (cached) yes checking for GNU libc compatible malloc... yes checking for stdlib.h... (cached) yes checking for GNU libc compatible realloc... yes checking for working alloca.h... yes checking for alloca... yes checking libintl.h usability... yes checking libintl.h presence... yes checking for libintl.h... yes checking whether gettext is declared... yes checking ncurses/ncurses.h usability... yes checking ncurses/ncurses.h presence... yes checking for ncurses/ncurses.h... yes checking for library containing initscr... -lncursesw configure: creating ./config.status config.status: creating Makefile [dometec@localhost crosstool-ng-1.18.0]$ make SED 'ct-ng' SED 'scripts/crosstool-NG.sh' SED 'scripts/saveSample.sh' SED 'scripts/showTuple.sh' GEN 'config/configure.in' GEN 'paths.mk' GEN 'paths.sh' DEP 'nconf.gui.dep' DEP 'nconf.dep' DEP 'lxdialog/menubox.dep' DEP 'lxdialog/textbox.dep' DEP 'lxdialog/yesno.dep' DEP 'lxdialog/util.dep' DEP 'lxdialog/inputbox.dep' DEP 'lxdialog/checklist.dep' DEP 'mconf.dep' DEP 'conf.dep' BISON 'zconf.tab.c' GPERF 'zconf.hash.c' LEX 'lex.zconf.c' DEP 'zconf.tab.dep' CC 'zconf.tab.o' CC 'conf.o' LD 'conf' CC 'lxdialog/checklist.o' CC 'lxdialog/inputbox.o' CC 'lxdialog/util.o' CC 'lxdialog/yesno.o' CC 'lxdialog/textbox.o' CC 'lxdialog/menubox.o' CC 'mconf.o' LD 'mconf' CC 'nconf.o' CC 'nconf.gui.o' LD 'nconf' SED 'docs/ct-ng.1' GZIP 'docs/ct-ng.1.gz' [dometec@localhost crosstool-ng-1.18.0]$ make install GEN 'config/configure.in' GEN 'paths.mk' GEN 'paths.sh' MKDIR '/home/dometec/bin/crosstool-ng/bin/' INST 'ct-ng' RMDIR '/home/dometec/bin/crosstool-ng/lib/ct-ng.1.18.0/' MKDIR '/home/dometec/bin/crosstool-ng/lib/ct-ng.1.18.0/' INSTDIR 'config/' INSTDIR 'contrib/' INSTDIR 'patches/' INSTDIR 'scripts/' INST 'steps.mk' INST 'paths' INSTDIR 'samples/' INST 'kconfig/' MKDIR '/home/dometec/bin/crosstool-ng/share/doc/crosstool-ng/ct-ng.1.18.0/' INST 'docs/*.txt' MKDIR '/home/dometec/bin/crosstool-ng/share/man/man1/' INST 'ct-ng.1.gz' For auto-completion, do not forget to install 'ct-ng.comp' into your bash completion directory (usually /etc/bash_completion.d) [dometec@localhost crosstool-ng-1.18.0]$ [dometec@localhost crosstool-ng-1.18.0]$ export PATH="${PATH}:$HOME/bin/crosstool-ng/bin" [dometec@localhost crosstool-ng-1.18.0]$ mkdir $HOME/toolchain [dometec@localhost crosstool-ng-1.18.0]$ cd $HOME/toolchain [dometec@localhost toolchain]$ [dometec@localhost toolchain]$ [dometec@localhost toolchain]$ [dometec@localhost toolchain]$ ct-ng build [INFO ] Performing some trivial sanity checks [INFO ] Build started 20130214.082010 [INFO ] Building environment variables [WARN ] Directory '/home/dometec/src' does not exist. [WARN ] Will not save downloaded tarballs to local storage. [EXTRA] Preparing working directories [EXTRA] Installing user-supplied crosstool-NG configuration [EXTRA] ================================================================= [EXTRA] Dumping internal crosstool-NG configuration [EXTRA] Building a toolchain for: [EXTRA] build = x86_64-unknown-linux-gnu [EXTRA] host = x86_64-unknown-linux-gnu [EXTRA] target = mipsel-unknown-linux-uclibc [EXTRA] Dumping internal crosstool-NG configuration: done in 0.37s (at 00:11) [INFO ] ================================================================= [INFO ] Retrieving needed toolchain components' tarballs [EXTRA] Using 'linux-custom' from custom location [INFO ] Retrieving needed toolchain components' tarballs: done in 0.43s (at 00:12) [INFO ] ================================================================= [INFO ] Extracting and patching toolchain components [INFO ] Extracting and patching toolchain components: done in 0.32s (at 00:12) [INFO ] ================================================================= [INFO ] Checking C library configuration [EXTRA] Munging uClibc configuration [INFO ] Checking C library configuration: done in 0.33s (at 00:13) [INFO ] ================================================================= [INFO ] Installing GMP for host [EXTRA] Configuring GMP [EXTRA] Building GMP [EXTRA] Installing GMP [INFO ] Installing GMP for host: done in 565.68s (at 09:39) [INFO ] ================================================================= [INFO ] Installing MPFR for host [EXTRA] Configuring MPFR [EXTRA] Building MPFR [EXTRA] Installing MPFR [INFO ] Installing MPFR for host: done in 111.05s (at 11:30) [INFO ] ================================================================= [INFO ] Installing binutils for host [EXTRA] Configuring binutils [EXTRA] Building binutils [EXTRA] Installing binutils [INFO ] Installing binutils for host: done in 447.97s (at 18:58) [INFO ] ================================================================= [INFO ] Installing pass-1 core C compiler [EXTRA] Configuring gcc [EXTRA] Building gcc [EXTRA] Installing gcc [INFO ] Installing pass-1 core C compiler: done in 786.95s (at 32:05) [INFO ] ================================================================= [INFO ] Installing kernel headers [EXTRA] Installing kernel headers [EXTRA] Checking installed headers [INFO ] Installing kernel headers: done in 19.26s (at 32:24) [INFO ] ================================================================= [INFO ] Installing C library headers [EXTRA] Copying sources to build dir [EXTRA] Applying configuration [EXTRA] Building headers [EXTRA] Installing headers [EXTRA] Building start files [EXTRA] Building dummy shared libs [EXTRA] Installing start files [EXTRA] Installing dummy shared libs [INFO ] Installing C library headers: done in 39.66s (at 33:04) [INFO ] ================================================================= [INFO ] Installing pass-2 core C compiler [EXTRA] Configuring gcc [EXTRA] Building gcc [EXTRA] Installing gcc [INFO ] Installing pass-2 core C compiler: done in 1362.81s (at 55:47) [INFO ] ================================================================= [INFO ] Installing C library [EXTRA] Copying sources to build dir [EXTRA] Applying configuration [EXTRA] Building C library [EXTRA] Installing C library [INFO ] Installing C library: done in 239.99s (at 59:47) [INFO ] ================================================================= [INFO ] Installing final compiler [EXTRA] Configuring gcc [EXTRA] Building gcc [EXTRA] Installing gcc [INFO ] Installing final compiler: done in 1724.58s (at 88:32) [INFO ] ================================================================= [INFO ] Installing binutils for target [EXTRA] Configuring binutils for target [EXTRA] Building binutils' libraries (libiberty bfd) for target [EXTRA] Installing binutils' libraries (libiberty bfd) for target [INFO ] Installing binutils for target: done in 358.41s (at 94:30) [INFO ] ================================================================= [INFO ] Installing strace [EXTRA] Configuring strace [EXTRA] Building strace [EXTRA] Installing strace [INFO ] Installing strace: done in 56.86s (at 95:27) [INFO ] ================================================================= [INFO ] Cleaning-up the toolchain's directory [INFO ] Stripping all toolchain executables [EXTRA] Installing the populate helper [EXTRA] Installing a cross-ldd helper [EXTRA] Creating toolchain aliases [EXTRA] Removing access to the build system tools [EXTRA] Removing installed documentation [INFO ] Cleaning-up the toolchain's directory: done in 9.19s (at 95:37) [INFO ] Build completed at 20130214.095546 [INFO ] (elapsed: 95:35.55) [INFO ] Finishing installation (may take a few seconds)... [95:37] / [dometec@localhost toolchain]$ [dometec@localhost toolchain]$