Wednesday, July 29, 2015

Java converting bytes to integers

Saw this Java method on github for converting Little Endian bytes to an integer:


private static int U8TO32_LE(byte[] x, int i) {
    return x[i] | (x[i + 1] << 8) | (x[i + 2] << 16) | (x[i + 3] << 24);
}

The implementation above is incorrect.  In Java, bytes are signed.  So the signed bit will got extended when a byte is casted as integer.  The correct way to do it should be:

private static int U8TO32_LE(byte[] x, int i) {
    return (x[i] & 0xff) | ((x[i + 1] & 0xff) << 8) | ((x[i + 2] & 0xff) << 16) | ((x[i + 3] & 0xff) << 24);
}





ChaCha20 Java implementation

My quick-and-dirty standalone Java implementation of the ChaCha20 stream cipher is available on GitHub

Thursday, July 16, 2015

Netwon's method with Java lambda expression

Here is a quick-and-dirty implementation of the Newton's method using Java Lambda Expression.



And here are some use/test cases:


Friday, June 12, 2015

Dual band wireless router as bridge with DD-WRT

Many people configure old wireless routers as repeaters to extend range of their networks:
(figure from dd-wrt web site)

However, with a dual band (2.4GHz and 5GHz) router, it can be used to boost wifi performance.  The idea is that, at location where wifi is weak, the secondary router can be configured as bridge to connect to primary router using 2.4GHz (as it has better penetration through walls than 5GHz).  Not to mention that with DD-WRT, the TX power can be configured to be much higher than your portable devices.

At the same time, the 5GHz channel of the secondary router can provide a strong and non-interfered signal to local devices.  Internet traffic will be bridged to primary router via the 2.4GHz channel.



I recently moved back to Canada and now living in a house sharing the existing Telus internet connection with others :(.  There are some tricks that can be done on the crappy V1000H router provided by Telus, but that will be another story.

Since the internet router is located in another floor, the wifi connection is not optimal for me.  So I reconfigured my DD-WRT-ed Asus RT-N66U as the diagram shown above.  Steps are clearly stated on DD-WRT web site on how to setup router as bridge.  The only different is that since my router is dual band, the 5GHz channel can be used to serve my devices while 2.4GHz is used as bridge.

Here are some speed tests ran on my Dell Inspiron 13 (Intel AC7265):

2.4GHz directly to the primary router:


5GHz to secondary router, bridged to primary router via 2.4GHz:

As expected:
- latency is higher when there is one more hop
- improvement on both download and upload speed is significant



Wednesday, April 29, 2015

Raspberry Pi to the rescue




With my freebsd machine down, this little white box will be my web machine for a while

Tuesday, March 10, 2015

Cross-compile kernel modules for Raspberry Pi

This is a quick note on how to cross-compile third party kernel modules for Raspberry Pi. The aim is to keep the stock kernel while compiling a module with matching version.

On Raspberry Pi
Update software on the Raspberry Pi

$ sudo apt-get update
$ sudo apt-get upgrade


Also, install rpi-update to upgrade the firmware and kernel. Then reboot.

$ sudo apt-get install rpi-update
$ sudo rpi-update
$ sudo sh -c “sync; sync; shutdown -r now”


Check the version of kernel

$ uname -a
Linux fax1 3.18.9+ #767 PREEMPT Sat Mar 7 21:41:13 GMT 2015 armv6l GNU/Linux


Also, we need to know the configuration used to compile the kernel. Execute the following command to save it to .config. Transfer the file to the cross-compile machine. We will use it later when compiling the kernel.

$ zcat /proc/config.gz > .config



Here the kernel version is 3.18.9. We can now work on a desktop computer for cross-compiling. First we need to compile the kernel source in order to generate the Module.symvers file. We need this file when compiling third-party modules. For the following example, I will be using a Ubuntu x64 machine.

On the cross-compile machine
Create a folder and download the Rapberry Pi kernel source.

$ git clone https://github.com/raspberrypi/linux.git


Check the Makefile and make sure the version is same as the kernel running on your Raspberry Pi.

Create another folder and download the compiler and tools

$ git clone https://github.com/raspberrypi/tools


Set an environment variable KERNEL_SRC to point to the location of the source, e.g.

$ KERNEL_SRC=/home/cho/work/rpi/linux


Set an environment variable CCPREFIX to the prefix of the path of tools, e.g.

$ CCPREFIX=/home/cho/work/rpi-tools/tools/arm-bcm2708/arm-bcm2708-linux-gnueabi/bin/arm-bcm2708-linux-gnueabi-


Change directory to the kernel source and clean the tree

$ cd /home/cho/work/rpi/linux
$ make mrproper


Copy the .config file we created earlier on Raspberry Pi to the kernel source directory, e.g.

$ cp ~/download/.config /home/cho/work/rpi/linux


We can start to compile the kernel now. In theory we only needed to build the modules. However, here we build the whole kernel and modules just for fun

$ cd /home/cho/work/rpi/linux
$ ARCH=arm CROSS_COMPILE=${CCPREFIX} make oldconfig


Once it is done, we can go ahead to build our third party modules, e.g.

$ cd /home/cho/work/tsc2007/raspi/tsc2007
$ ARCH=arm CROSS_COMPILE=${CCPREFIX} make


Note that you may need to modify the module's Makefile, e.g.
obj-m += tsc2007.o
obj-m += tsc_raspi.o

all:
        make -C /home/cho/work/rpi/linux M=$(PWD) modules

clean:
        make -C /home/cho/work/rpi/linux M=$(PWD) clean


Here, the "-C" path is pointing to the root of the kernel source we just built.

Thursday, June 12, 2014

FreeBSD and CPU scaling

Recently built a new FreeBSD machine with AMD Athlon 5350. Found that if powerd is enabled, the machine will randomly reboot itself. Forcing the lowest CPU frequency to max by setting debug.cpufreq.lowest=2050 in /boot/loader.conf can prevent that. So I suspect it is related to the CPU scaling or C-state.

This problem can be avoided if disabling the C6 state in BIOS.

For reference, the motherboard is ASRock AM1H-ITX.

Here are the CPU freq under normal powerd management.

$ sysctl dev.cpu.0.freq_levels
dev.cpu.0.freq_levels: 2050/5021 1850/4590 1650/3675 1443/3215 1400/2937 1225/2569 1200/2266 1050/1982 1000/1880 875/1645 800/1527 700/1336 600/1145 500/954 400/763 300/572 200/381 100/190


Update: 14 Jun

Found a matching bug report and reported my findings too. Basically, either C6 in BIOS or powerd in FreeBSD need to be disabled to get around the issue. Or, with C6 and powerd enable, the issue can be avoided if CPU throttling is disabled. Add this to /boot/loader.conf:

hint.acpi_throttle.0.disabled=1

But this can affect the reported lowest CPU freq.
Throttling disabled:
root@bart:~ # sysctl dev.cpu | grep freq
dev.cpu.0.freq: 800
dev.cpu.0.freq_levels: 2050/5021 1850/4590 1650/3675 1400/2937 1200/2266 1000/1880 800/1527

Saturday, May 10, 2014

When Java 8 meets Project Euler

Here is a Java implementation of a Project Euler problem. Used lambda expression and steam features of Java 8.
package net.clarenceho.euler;

import java.util.stream.IntStream;

public class Multiples3and5 {

    public static void main(String args[]) {
        System.out.println("The answer is " +
            Multiples3and5.dividableStream(1000).sum());
    }
 
    static boolean dividable(int i) {
        boolean result = false;
        if (i % 3 == 0 || i % 5 == 0) {
            result = true;
        }
        return result;
    }
 
    static IntStream dividableStream(int max) {
        return IntStream.range(1, max)
            .filter(i -> dividable(i));
    }
}

Saturday, February 15, 2014

OpenCL on UDOO

For those who interested to try OpenCL on UDOO, here is the device info

Number of platforms: 1
CL_PLATFORM_PROFILE: EMBEDDED_PROFILE
CL_PLATFORM_VERSION: OpenCL 1.1
CL_PLATFORM_VENDOR: Vivante Corporation
CL_PLATFORM_EXTENSIONS:
Number of devices: 1
CL_DEVICE_TYPE: CL_DEVICE_TYPE_GPU
CL_DEVICE_VENDOR_ID: 5654870
CL_DEVICE_MAX_COMPUTE_UNITS: 4
CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS: 3
CL_DEVICE_MAX_WORK_ITEM_SIZES: 1024 1024 1024
CL_DEVICE_MAX_WORK_GROUP_SIZE: 1024
CL_DEVICE_PREFERRED_VECTOR_WIDTH_CHAR: 4
CL_DEVICE_PREFERRED_VECTOR_WIDTH_SHORT: 4
CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT: 4
CL_DEVICE_PREFERRED_VECTOR_WIDTH_LONG: 0
CL_DEVICE_PREFERRED_VECTOR_WIDTH_FLOAT: 4
CL_DEVICE_PREFERRED_VECTOR_WIDTH_DOUBLE: 0
CL_DEVICE_PREFERRED_VECTOR_WIDTH_HALF: 0
CL_DEVICE_NATIVE_VECTOR_WIDTH_CHAR: 4
CL_DEVICE_NATIVE_VECTOR_WIDTH_SHORT: 4
CL_DEVICE_NATIVE_VECTOR_WIDTH_INT: 4
CL_DEVICE_NATIVE_VECTOR_WIDTH_LONG: 0
CL_DEVICE_NATIVE_VECTOR_WIDTH_FLOAT: 4
CL_DEVICE_NATIVE_VECTOR_WIDTH_DOUBLE: 0
CL_DEVICE_NATIVE_VECTOR_WIDTH_HALF: 0
CL_DEVICE_MAX_CLOCK_FREQUENCY: 500
CL_DEVICE_ADDRESS_BITS: 32
CL_DEVICE_MAX_MEM_ALLOC_SIZE: 33554432
CL_DEVICE_IMAGE_SUPPORT: 1
CL_DEVICE_MAX_READ_IMAGE_ARGS: 8
CL_DEVICE_MAX_WRITE_IMAGE_ARGS: 8
CL_DEVICE_IMAGE2D_MAX_WIDTH: 8192
CL_DEVICE_IMAGE2D_MAX_WIDTH: 8192
CL_DEVICE_IMAGE2D_MAX_HEIGHT: 8192
CL_DEVICE_IMAGE3D_MAX_WIDTH: 0
CL_DEVICE_IMAGE3D_MAX_HEIGHT: 0
CL_DEVICE_IMAGE3D_MAX_DEPTH: 0
CL_DEVICE_MAX_SAMPLERS: 8
CL_DEVICE_MAX_PARAMETER_SIZE: 256
CL_DEVICE_MEM_BASE_ADDR_ALIGN: 1024
CL_DEVICE_MIN_DATA_TYPE_ALIGN_SIZE: 128
CL_DEVICE_SINGLE_FP_CONFIG: CL_FP_ROUND_TO_ZERO
CL_DEVICE_SINGLE_FP_CONFIG:
CL_DEVICE_GLOBAL_MEM_CACHE_TYPE: CL_READ_WRITE_CACHE
CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE: 64
CL_DEVICE_GLOBAL_MEM_CACHE_SIZE: 4096
CL_DEVICE_GLOBAL_MEM_SIZE: 67108864
CL_DEVICE_MAX_CONSTANT_BUFFER_SIZE: 4096
CL_DEVICE_MAX_CONSTANT_ARGS: 9
CL_DEVICE_LOCAL_MEM_TYPE: CL_LOCAL | CL_GLOBAL
CL_DEVICE_LOCAL_MEM_SIZE: 1024
CL_DEVICE_ERROR_CORRECTION_SUPPORT: 1
CL_DEVICE_HOST_UNIFIED_MEMORY: 1
CL_DEVICE_PROFILING_TIMER_RESOLUTION: 1000
CL_DEVICE_ENDIAN_LITTLE: 1
CL_DEVICE_AVAILABLE: 1
CL_DEVICE_COMPILER_AVAILABLE: 1
CL_DEVICE_EXECUTION_CAPABILITIES: CL_EXEC_KERNEL
CL_DEVICE_QUEUE_PROPERTIES: CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE | CL_QUEUE_PROFILING_ENABLE
CL_DEVICE_PLATFORM: 0x2ace9530
CL_DEVICE_NAME: Vivante OpenCL Device
CL_DEVICE_VENDOR: Vivante Corporation
CL_DRIVER_VERSION: OpenCL 1.1
CL_DEVICE_PROFILE: EMBEDDED_PROFILE
CL_DEVICE_VERSION: OpenCL 1.1
CL_DEVICE_OPENCL_C_VERSION: OpenCL C 1.1
CL_DEVICE_EXTENSIONS: cl_khr_byte_addressable_store

Sunday, December 15, 2013

DIY DAC for Beaglebone Black

I ordered the PCM5102 DAC chip long time ago, planning to follow the discussion in a forum to build a DAC for my BBB. Finally got a chance a few weeks ago as I was taking sick leave to stay at home :P. Here is the summary:




Hardware

  • The chip I used is PCM5102. There are discussions in the forum that there might be timing issue on the I2S signal with BBB. But audio seems to be not affected.

  • The schematic shown in the discussion forum was with headphone amplifier. Since I already owned too many DIY headphone amplifiers, I only implemented the PCM5102 part. In fact, it is quite similar to the "Typical Application Circuits" shown in the datashet.

  • Only some minor variations on components, e.g. the power capacitors, as I used whatever I could find in my spare-parts box

  • Two LM3940 used to convert 5V to 3.3v for PCM5102, one to analog and one to the rest of the chip. Granted that LM3940 is not very good at ripple and noise handling, but it should be cleaner than the 3.3v on BBB.

  • Pin 11 of PCM5102 set as high so filter runs in low latency mode

  • The BBB connects to my home wifi network with a dongle (Ralink RT8070 chipset)

Software

  • My Beaglebone Black is running Debian with 3.8.13-bone30 kernel

  • To support UPnP such that I can control the playlist etc from my phones, tablets, and other computers etc, gmediarenderer was compiled and installed

  • With normal UPnP setup, the controller needs to be connected to the renderer all the time so that it can send instructions to play one song after another. This is undesirable as I wanted to use mobile devices to setup a playlist and then disconnect. One solution is to add an OpenHome renderer. This way, the playlist and play controls (e.g. random and loop) can be set and stored centrally.

  • As I paid for the BubbleUPnp on my Android devices, I chose to install the BubbleUPnP Server as the OpenHome renderer on BBB. It is a Java application. As mentioned in my previous post, I have the Oracle JDK 8 Early Access ARM VM on my BBB.

  • The gmediarenderer and BubbleUPnP JVM take around 15% CPU while playing music. My NAS (acting as UPnp server to serve music files) transcodes FLAC to WAV

  • I did test FLAC files on the setup using ffmpeg. The performance is OK too.






Issues

  • Of my 2 BBBs, only one works with the DIY DAC. Dont know if it is a hardware defect or something else

  • Do not set the playback volume to 100% on BBB. Seems that the PCM5102 charge pump is not providing rail-to-rail voltage. Setting the volume to 100% will cause clipping

  • Using ffmpeg to play back audio at 44.1kHz will produce occasional distortion. Can be prevented by upsampling the music in ffmpeg to 48k, 88.2k etc. No such issue with gmediarenderer