Saturday, June 22, 2013

Using USB to UART / RS232 modules with Android phones

Installation:

As we compiled the kernel modules needed for USB to UART modules in the previous post, Here I'm going to show how to use them.

You can mount /system partition on the phone with read/write permisions.
su
mount -o rw,remount /system

Then make a directory for kernel modules and copy them to there,
mkdir /system/modules
then copy modules (my compiled modules for 2.6.32.60-Kappa1.6) to /system/modules/

I'm using init.d script to load these modules in the bootup, if not you will have to load them manually each time.
copy my script to /system/etc/init.d/
chmod 755 /system/etc/init.d/usbser

If you like to load modules manually, you can refer this to get an idea,


















Now everything is done and when you plug an USB to UART module, it will be taken as /dev/ttyUSB0. You can use it with any application like on any Linux distribution.

You can do a lsmod to verify whether  the modules are loaded.


















Testing:

These are some dmesg outputs (when I connect the device) for some popular USB to UART devices that I have compiled the drivers for,

PL2303:



















CP2102:

















CH341 (from a USB to RS232 module):

















A loopback test with a Serial terminal app:

















Usage & Applications:

As  I mentioned earlier you can you these devices with any serial communication application for Android. I personally use "android-serialport-api" as a terminal application.


















Reading from a data-logger:


















Tuesday, June 18, 2013

Compiling kernel modules for an Android phone

I recently got a USB OTG cable for my mobile phone and I wanted to use my PL2303 USB to UART convertor with it. It is great for the embedded system projects I'm working on. You can follow the below procedure to do it yourself.


Phone kernel : Linux 2.6.32.60-Kappa1.6
PC O/S : Ubuntu 13.04 32bit

1) Check for your kernel info using "uname -a" on either terminal emulator or ADB (use "adb shell" and run it).
My result:  Linux version 2.6.32.60-Kappa1.6 (Ka@Kappa) (gcc version 4.7.3 20130102 (prerelease) (Linaro GCC 4.7-2013.01) ) #94 PREEMPT Sun Apr 28 23:46:13 CEST 2013

2) Get the appropriate kernel source for your kernel. I got 2.6.32.60-Kappa1.6 kernel source from a GIT host using, (If you don't have git installed, you can install it by sudo apt-get install git)
git clone https://github.com/KaSt/Kappa.git

3) Download the appropriate cross-compiler toolchain, I'm using Linaro GCC 4.7-2013.01 toolchain.

4) Extract toolchain to somewhere, I extracted it to, /home/buddika/linaro

5) Set path variable to the bin directory of the toolchain using,
export PATH=/home/buddika/linaro/bin/:$PATH

6) Copy the kernel configuration file to the main folder where kernel source it. I executed following command at there,
cp /home/buddika/kernel/Kappa/arch/arm/configs/ka_coconut_defconfig .config

7) Check MakeFile and config files and edit them to match the kernel magic version(in my case 2.6.32.60-Kappa1.6). I had to edit following line on config file,
CONFIG_LOCALVERSION="$(KERNEL_LOCAL_VERSION)-Kappa $(KAREL)" to
CONFIG_LOCALVERSION="$(KERNEL_LOCAL_VERSION)-Kappa1.6"

8) Check the toolchain's binary directory and find the prefix for the compiler, if you see file like arm-linux-gnueabihf-gcc , the prefix is "arm-linux-gnueabihf-". Note this down for steps below.

9) Go to the kernel source directory, Let's clean the old binary files which may be there.
ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- make clean

10) Let's run menuconfig to configure the kernel options. This will give you a GUI where you can enable features. Select features you need as modules (in my case usbserial...etc) and press M to build them as modules.
ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- make menuconfig

11) Finally build the selected features as kernel modules. :)
ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- make modules

Now you can find the compiled kernel modules at "drivers/usb/serial/" folder in the kernel source folder. If you enabled other functions please check the appropriate folder.

I will show you how to use these newly compiled modules on your phone to use USB to UART module in the next post :D

Sunday, June 16, 2013

Native C programs for ARM based Android phones

You can always use Android SDK to program and build Java programs for android phone which are running on Dalvik Virtual Machine. Recently I wanted to build some native C applications for my phone. As Android is based on Linux kernal, it's matter of finding an appropriate cross compiler for the PC. As I found out there are many toolchains for this and I used Linaro ARM tool chain for Windows (WIN32). This is based on GCC and  I downloaded 2013.05 version and installed it using the installer. Please make sure to tick "add to the PATH" when you installing this.

I wrote this small code to test and saved as count.c ,

#include <stdio.h>
int main()
{
int x=0;
for(x=0;x<11;x++){
printf("%i\n",x);
}
}


Then I compiled it using following command in command prompt,

D:\>arm-linux-gnueabihf-gcc count.c -o count -static

The I copied the binary file to mobile using ADB (Android Debug Bridge), you can do it using a file manager too. Make sure to put the compile binary to /system because you can execute binaries only at there in android system.

D:\>adb push count /system
3049 KB/s (463524 bytes in 0.148s)

Then I logged into android shell using ADB, You may use a Terminal Emulator software on the phone itself.

D:\>adb shell

I got root access and changed the file attributes of "count" binary to make it executable (same as on any Linux distribution). (You can either use chmod +x or chmod 777)

~ # su
root@android:/ # cd /system
root@android:/system # chmod 777 count

I executed "count" and tested ,


root@android:/system # ./count
./count
0
1
2
3
4
5
6
7
8
9
10
11|root@android:/system #

I also tested the program on phone using a terminal emulator too,





Monday, June 3, 2013

Simple LM386 based audio amplifier

I built this simple LM386-N1 based audio amplifier (Gain=200 example on datasheet) for my radio project. Here I'm testing it using a small signal generated by multimeter. You can find the schematic and more information in the datasheet. This is a quick test and I'll explain more details in the future of the project.


Sunday, June 2, 2013

Using UART & GPIO with AR7240 MIPS board

As I introduced MIPS based router board which has a AR7240 SoC, I further experimented with it to use GPIO and UART in the chip. I'm using my TPLINK WR740N router for these experiments.
I soldered some wires to the board and took the connectors to the exterior of the router so I can play with it easily. You can find the connections to the GPIO and UART pins in this page.
























Then I connected my CP2102 USB to UART convertor to the UART port of the AR7240.
















Then I logged into the router console using PuTTY and tried to send some data from the UART port of the AR7240 to the PC. In Linux we can use the highlighted command to send a file to the UART port.















 The test was a success and I got the data to the PC using my favorite terminal program SSCOM32.