Saturday, April 27, 2013

The mystery of OpenCv and BeagleBoard-XM

This guide is for anyone who is thinking of using BeagleBoard-xM DM3730's DSP for OpenCV to get an extra boost in algorithm execution. This guide is also for everyone who has just started to search for leads , hints, pointers... anything regarding OpenCV and BB-xM

I'll start by referring to a Texas Instruments White paper on OpenCV on TI’s DSP+ARM platforms. The white paper suggests that OpenCV can and is easily ported on TI's heterogeneous SoC (ARM+DSP).

This white paper lead me to believe that we have a shot at porting our current OpenCV anaytics app to an embedded platform assuming it to be a simple "Cross-compile" with the hurdles of "any other" cross-compilation exercise, but I was wrong.

Where was I wrong , you ask ?

1 - Beagleboard-xM has (DM3730) ARM Cortex A8 + DSP (C64x). ARM Cortex A8 has neon fpu but DSP (C64x) is only a fixed point DSP. So, thinking that porting OpenCV to DM3730 will yield the results I wanted to see was wrong.
OpenCV heavily relies on floating point arithmetic, and absence of a hardware fpu may not give the boost I needed.

2 - TI's claim that OpenCV has been ported and optimized for TI's multicore systems. The OpenCV port is compiled with C6EZaccel library which has been rendered obsolete by TI but is still downloadable. And a guide is available for reference on how to Get started with C6EZaccel. But C6EZaccel v2.xxx is the one with "so called" optimized port of OpenCV and that is only available for DM8148 and DM8168.

3 - TI's support will almost always point to Building OpenCV for ARM Cortex A-8 .

4 - C6EZaccel is obsolete, and hence proper support is no longer there. TI is promoting its VLIB I guess. Which leads me to believe that OpenCV project didn't go very well.

Conclusion:
BB-xM is a very good embedded hardware to start a project on. It can make use of OpenCV easily but only on ARM following the guide mentioned in pt.3 .But BB-xM is not a good choice if you are expecting a boost in performance by porting OpenCV to its DSP side.

Useful pointers:
OpenCV is a floating point depended library.
C64x is a fixed point DSP.
C674x is a floating point DSP
If you want performance in OpenCV, use IPP.


I hope this post will save a lot of days of internet surfing for many.

Sunday, February 10, 2013

LG Optimus SU640: Comfirmed working guide to Flashing Custom Rom

After three long weeks of scrounging the web for a working guide to flashing custom Roms on my LG Optimus SU640 and not finding any clear method which would simply work, I have finally come up with a method that would work just like that.

I would like to thank XDA Developers forum, a great Android Development forum which has taught me a lot.

Declaimer: None of the tools I used are my own, but the combination of what to use is entirely cooked up by me by experimentation. So if you want to copy this, please give credit and link back. And  I am not responsible if you brick your phone.

Without further ado, here is what you'll need to do .

Files you need.

SU640 Drivers : Download LG update Tool from LG website, it will package all the required drivers
Clockwork Mod recovery : Download ClockworkMod version 5.8.2.0
AllRoot tool : Download AllRoot
A custom ROM for SU640 : XDA developers
SU640 v10i_kdz firmware image. Download
LG_KDZ_FW-Update_OfflineFix  : Download  Download Originally downloaded from XDA but don't have exact link to link back

What you need to do:

Step 1 : Downgrading

Add the following line to the end of C:\Windows\System32\drivers\etc\hosts

127.0.0.1 csmg.lgmobile.com

Once you have downloaded all the files, copy them to "C:\SU640_Flashing".

Extract KDZ update tool "LG_KDZ_FW-Update_OfflineFix"

In the extracted folder, run "Automatic Offline Procedure".
Press "OK" on first two messages.
A word document will open up (ignore it), and a dialog box , press OK on dialog box. If you are on windows 7, it will show you another dialog box, press "Yes".
You will be presented with "R&D Test Tool" by LG. So far so good.


Make sure your R&D Test Tool window has the following options selected. If not, then select these, the ones marked in Red.

  1. Type : CDMA
  2. Phone Mode: Emergency
  3. KDZ file : browse to v10i_00 kdz file you downloaded
  4. Select model.dll : browse to "model.dll" in  LG_KDZ_FW-Update_OfflineFix folder.
Take out the SIM card, memory card and put your phone in "Download mode"

How to put LG SU640 in Download mode


Turn off your phone. Disconnect your cable.

Keep pressing the Volume Up button and insert your USB cable. wait a few seconds and you will enter Download mode.

Once your phone is in Download mode and connected to computer, press "CSE web upgrade test" on the bottom right corner.


A new dialog box will appear, just press "Start Upgrade"
Another dialog box will appear, select "Different country" and "English" language and Press OK.

A new old-style window will appear, and downgrading process will start. Let it finish, and don't worry if it gets stuck at 4%. Just look at the log, and it will be incrementing to 5% , 6% and so on. You'll also notice a "device model not found" message, you can just ignore it.

Your phone will reboot thrice before you see "(2001,0)" string on the Log. which means the process is complete.

Close the window using "task manager" because it will not reach 100% in progress bar and will not close.

Step 2 : Rooting

Once you have booted into the Korean GingerBread firmware, press the "Left" button and select "English".
You can skip other settings because we are not going to use this ROM very long.

In the settings, "Applications" , select "Unknown sources" , "USB debugging", and connect your phone in "LG Software mode".

Extract the "AllRoot.rar" file. and run "RunMe_En" and follow instructions on the screen.
Once RunMe_En finishes successfully, you are rooted.

Step 3 : Clockwork Mod Recovery

Turn your phone off. Insert the Memory Card and Turn on the phone.
Extract  clockworkmod version 5.8.2 you downloaded. 
Put your phone in USB storage mode and copy recovery.img from clockworkmod folder into your memory card, in "wiredsoup" folder.

Safely disconnect your phone.
Download "Terminal emulator" from Playstore.

Run the downloaded "Terminal Emulator", and type the following commands.

su
cd /sdcard/wiredsoup
dd if=recovery.img of=/dev/block/mmcblk0p13

once the final command finishes telling you how many bytes copied in and out etc, type the following command

reboot recovery
Your phone will reboot, and present you with "Clockwork Mod recovery" :) congratulations.

Now you can reboot into the korean ROM again, and copy any downloaded custom Rom to your sdcard, and flash using the recovery mode.

How to enter clockworkmod recovery on LG SU640 and Nitro HD:

One method is via terminal emulator or adb , the "reboot recovery" command.

Second method is to power off the phone. Keep pressing volume down button and power up button and wait for white text about "factory reset" and stuff. Release both buttons, and press Power button twice.


IMPORTANT:

Note 1:
Some custom ROMs will fail with "some symlinks failed "error and "status 7" . Make sure your downloaded file and original file have the same MD5 hashes.


Note 2:
Some ROMs will give an error: "assert failed getprop(ro.product.device==SU640)" or something like that.

Open that ROM using Winrar, goto "META-INF\com\google\android " and open the file named "updater-script" and remove the first line and save.

On closing the text editor, winrar will ask you if you want to replace the edited file, click "Yes" or "OK" . and flash the ROM again. This time, it will work.

Always remember to wipe before installing new ROMs.

Sunday, February 3, 2013

Rooting LG Optimus SU640 on official ICS

I recently got my hands on my first LG Android experience. LG Optimus SU640, also known as Nitro HD in US which goes with AT&T carrier.

Phone Specs:
LG Optimus LTE SU640 on GSM Arena.




Rooting Official ICS v20D firmware will allow you to debloat your device and eventually make it MUCH more responsive and make your battery last longer.

WARNING: I am not responsible if your device gets bricked , fails to boot, you lose your data etc. Although there is very low possibility of such happening if you follow my guide.

There are two methods i've found to be working on official ICS v20D for SU640.

Method 1 :
Here is what you need to do.

  1. Download Root20D.rar
  2. Extract Root20D.rar to "C:\Root20D"
  3. Connect your LG SU640 to your computer.
  4. Goto "Settings >> Security "
  5. Check "Unknown Sources"
  6. Goto "Settings >> Developer Options
  7. Check "USB Debugging"
  8. Make sure you are connected in "LG Software Mode"
  9. Execute "En_Step1.bat"
  10. Follow the on screen instructions and be patient :) 
  11. if it doesn't succeed the first time, do not give up. Just restart from Step 2.

Method 2:

  1. Download Root_.rar
  2. Extract Root_.rar to "C:\Root_ICS"
  3. Connect your LG SU640 to your computer.
  4. Goto "Settings >> Security "
  5. Check "Unknown Sources"
  6. Goto "Settings >> Developer Options
  7. Check "USB Debugging"
  8. Make sure you are connected in "LG Software Mode"
  9. Execute RunMe.bat
  10. Select "Normal Mode"
  11. Follow the on screen instructions and be patient :) 
When you see text
"Waiting for device to show up again [ali]...."

Wait for your device to boot-up . then Press "Enter"
If the process does not move forward, Press "Ctrl + C"
Press "n" for "No" and the process will proceed.


Do not install CWM on your phone yet, ICS does not allow CWM flashing on mmcblk0p13 Block. You will end up bricking it and will be presented with a pretty message.

"Security breech"
"Long time no see"

Tuesday, January 22, 2013

How to determine Endianness of a system programmatically

What is Endianness ?
Endianness is *in which order the system stores data bytes*.

For example, a Little Endian system will store lower byte of data on lower addresses of memory.

Little Endian example
Suppose an integer of 4 bytes.
Byte3 Byte2 Byte1 Byte0

Base_Address + 0 = Byte0
Base_Address + 1 = Byte1
Base_Address + 2 = Byte2
Base_Address + 3 = Byte3

Big Endian system stores upper data bytes in lower memory addresses.

Big Endian example
Suppose an integer of 4 bytes.
Byte3 Byte2 Byte1 Byte0

Base_Address + 0 = Byte3
Base_Address + 1 = Byte2
Base_Address + 2 = Byte1
Base_Address + 3 = Byte0

Interpret it like this,

int x = 1;
char * ptr = (char*)&x;

For a little endian system
ptr[0] will store 1

For a big endian system
ptr[0] will store 0

Note: ptr[0] is equivalent to dereferencing a pointer reference like this : *(ptr + 0)

Complete code

int x = 1;
char * ptr = (char*)&x;

if (*ptr == 1)
// little endian
else
// big endian

Thursday, December 6, 2012

Reset your Ubuntu password in 3 easy steps

Have you forgotten your Ubuntu password ?
Don't worry you are not alone.

Here is how you can easily reset your Ubuntu password.

Step 1 : Booting into recovery mode

As soon as you see GRUB loading screen with "Press ESC to enter menu". Make sure to press "ESC key".


You will be presented with options to boot into "ubuntu xx.xx kernel xx.xx generic" and " "ubuntu xx.xx kernel xx.xx generic (recovery mode)" . Choose the recovery mode.

Step 2:  Entering Shell

You will be shown "Recovery menu" with multiple options including "root   Drop to shell prompt". Select this option to enter the shell.with root access.


Step 3 : Resetting the password


Now that you are on root shell, use the following commands to reset your password.

passwd [user_name]
for example,
passwd wiredsoup 

 Follow the instructions, and use the following commands once you are done confirming your new password.

sync
reboot -f


That's it :) enjoy logging into you Ubuntu again.

Thursday, November 15, 2012

I2C (Inter Integrated Circuit Bus) : Serial Communication

I2C quick tutorial

I2C (sometimes 2 is in superscript) and read as "eye-two-see" or "eye-square-see" is a serial communication  protocol introduced by Philips semiconductors in early 80s.

I2C is a 2-wire Interface (TWI) with mostly a single master and multiple slaves , there are multiple master configurations possible too.

I2C signals

  1. Data :  SDA  ===========> Always bi-directional
  2. Clock:  SCL ===========> Bi-directional only in Multi-Master Mode

Ground : GND ==========> Common Ground between the two Devices
5V supply

By convention , I2C operates on 5V signals. So make sure you put a level translation buffer in between  if your device is 3.3V tolerant.

Maximum allowable capacitance on the lines is 400 pF and typical device capacitance is 10 pF. So how many devices are supported on single I2C Bus ?

The communication is always Master initiated, Master (typically a Microcontroller) places the address of Slave device with which Master intends to communicate on the Bus. All Slave devices monitor the SDA pin to determine if Master is sending their address. Only the Slave with Matched address can communicate with Master.

The advantages of I2C other than it being a very low cost to implement are
  • Supports multiple devices on only 3 wires
  • Is implemented in Hardware as well as Software (Bit-Banged I2C)
  • Supports Multi Master configuration
  • Many examples available on the Internet
If I had to belittle I2C I would say , The disadvantages of I2C are
  • Short Distances only
  • Slow Clock speeds (Typically 100Khz)
  • Faster Clocked devices cannot coexist with Slower devices on the same bus

I2C Typical Connections
Both SCL and SDA are Open-Drain drivers, which means that the device can drive it low, but cannot drive it High. To drive the line High, you must provide a pull up through a resistor as seen in the above figure. You only need one pull up for SDA and one for SCL irrespective of how many devices are connected to the bus.

Data Transfer Protocol over I2C

When I2C master wishes to communicate with slave devices, it stars by generating a "Start Condition" on the SDA line. "Start Condition" is defined as SDA line going low while SCL is high. Similarly when Master devices wishes to end the transfer, it generates a "Stop Condition" on the line. Stop Condition is defined as SDA going High when SCL is High.


The receiving device ACKnowledges the transfer after every 8 bits (MSB first), by driving SDA Low for Positive ACK and driving SDA High. This means that there are 9 clock cycles per 8-bits of data. 8-bit data and 1-bit for ACK. If the receiver is not ready to receive any more data, it should keep SDA high for 9th bit so the transmitter aborts the transfer.



Data can change while the SCL is low, but it should not change with SCL is high.

Device Addressing

Device Addressing is another section of I2C that confuses new comers. Addresses on I2C bus are 7-bit, but as explained before, the transfers are all 8-bit + 1-bit for ACK, then where did this 7-bit address come from ? Here is how.




In the images above, it is a little bit more clear as in how the addressing is done in I2C. 7-Bit slave address (128 slave devices addressable ) , 8th bit for is it going to be a Read (1) or a Write (0) command, and 9th bit for ACK from the receiver. Always remember, Even address (8th bit zero)  for WRITE and Odd address (8th bit one) for READ.

Example 1
Here is an example where Master device is writing to a Slave. It starts by addressing a Slave device by its 7-bit Address and putting 8th-bit Low indicating a Write Command.



The "Green" bounded parts are Bits transfered by Master, and everything else is transfered by Slave.

Example 2
Master wants to read from Slave



But it is rarely this simple. Devices have multiple internal registers to which you want to write to or read from, so how do you do that ?

Writing to an internal register of an I2C slave

  1. Start Condition (Put automatically if Hardware I2C is used)

  2. Send I2C address of the slave with R/W bit low (even address) for write.
  3. Send the address of the internal register
  4. Send data byte
  5. Send more data bytes if needed
  6. Send Stop Condition (Automatic if Hardware I2C is being used) 

Reading from an Internal Register of an I2C Slave device

Reading from an I2C slave device having internal registers is a wee bit tricky but not tricky enough to get you boggled up. Read from the slave device starts with a write operation in which we write the internal register address to the slave device which we want to read from.


  1. Start condition
  2. Send I2C address of the slave with R/W bit Low (even Address) for write.
  3. Send the address of the internal register
  4. Send "Start Condition" again, referred to as (Restart) in this scenario
  5. Send the I2C address of the slave device with R/W bit High (Odd address) for read operation.
  6. Read data byte
  7. Read more data byes if needed
  8. Send a stop condition.

I hope this helped :) leave your questions in comments.

Monday, November 12, 2012

How to install Google Analytics in Wordpress


How to Install Google Analytics in WordPress ?

This was my question a few minutes ago, and here is the most simple answer :)
Assuming you have Google Analytics set up. If you haven't then start by signing up for Google Analytics account http://www.google.com/analytics/ , its free and simple.

1 - Copy the code that you received in from Google Analytics, and paste it in your theme’s header.php right above the < / head > tag . [Preferred way] 
OR
2 - Copy the code that you received in from Google Analytics, and paste it in your theme’s footer.php right above the < / body > tag 
Theme's header.php and footer.php can be found by going into your Wordpress dashboard > Appearance > Editor
Now on your right you will see a sidebar listing all your pages. Locate the header.php or footer.php whichever you want.


If its still not working, leave your questions in the comments :)