Saturday, February 21, 2015

The Keys to the Kingdom

Apparently, the NSA stole the keys to the GSM kingdom.  It was already clear that GSM is not secure and two heads of state were annoyed by the Yanks listening in to their calls already, but this is ridiculous:

What is clear, is that the US government doesn't care how much damage the NSA causes to states and corporations the world over.  This is a Dutch/French company, NATO members and the NSA did them in to the tune of half a billion Dollars, with hardly a peep from any politician anywhere.  If your computer system is not secure, then you can lose your business thanks to the US military, whether you are an ally, at peace, at war or neutral.  With such friends, who needs enemies?

The CTO of Lenovo has now proven himself to be just as clueless as a previous CTO of Sony:

They all failed to understand that if you subvert thousands of computers, then the system is weakened and can be misused by anyone else too.  Even the much maligned DHS is on the ball on this one

Where did these guys buy their university degree certificates - maybe here

The only way to combat the spying, is to buy all your computer and networking equipment in bits and pieces from random vendors and assemble it yourself, then load it with an obscure version of UNIX such as OpenBSD.

Whatever comes preinstalled on any machine, not only from the special class at Lenovo, cannot be trusted.

Monday, February 9, 2015

Thermionic Valve Amplifier

I stumbled upon a source for some cool old stuff called Magic Eye tubes.  The shop is in Chelyabinsk Russia, called Tubes Store.

Here is a nice VU meter video:

While browsing there, I decided to make a little audio amplifier to hook to my Mac, get a couple of Magic Eye tubes to use for a VU meter (blah) or robot eyes (yes!) and some Nixie tubes for a clock or digital read-out, or goodness knows what, so I ended up buying a handful of brand new old junk.

Now the problem is getting high voltage transformers and capacitors that can handle >200 Volts.  Everything in my junk box is low voltage and will go pop, just like the weasel of yore.

After a little bit of head scratching, I came up with this cool little audio amplifier design:

It is a pretty standard push-puller, using a miniature 6N21 dual triode at the front end and a pair of 6E6 tubes at the back end (It is a rough drawing, but it gives the idea.  I didn't want to create a special transformer part in Eagle.).

Bluetooth Audio

I'd like to have both a wired and wireless interface.  It may be an old fashioned amplifier, but it is the 21st century after all!

Sparkfun sells a Bluetooth Audio interface module


Now where on earth can I get transformers for the 6V heaters and 200 V anodes?  One could use an automotive ignition coil for the audio output transformer and a couple of low voltage transformers back to back for the heater and high voltage power supply, but it turned out that there is a manufacturer of replica guitar amp transformers in Canada, Hammond Manufacturing, eh...

The Hammond classic audio output transformer should sound better than a car coil.  Well, I hope - at least it will look better:

and one can get high voltage electrolytic capacitors from Digikey:

Next time I walk past a Pakistani trinket store down town, I'll pick up an old fashioned little wooden box to put it in. I saw a replica His Master's Voice wind-up record player the other day - that store will have something.

So, it is no problem to build a new old toy today, but it ain't cheap.  Shipping transformers halfway around the world is expensive.  I guess my little amplifier will cost about $300 when it is all done.


I finally ordered the transformers.  It turned out to be easiest to get them from Mouser and instead of getting a multi-winding tube amp power transformer, I got separate transformers for the heaters and anodes - easier that way. Mouser does stock a Hammond Fender replica power transformer, but it is 115 V only - while I live in UAE with 240 V mains supply.

Therefore I got a Hammond 185C230 for the anode voltage and a VPS12-2000 for the heaters.

The audio transformer I settled on is the Hammond 1750H.

Note that you must never run a tube amp without a load - the transformer will arc and destroy itself.  I'll get a 6 inch full range car speaker pair at Lulu or Carrefour and stick it in an old fashioned wooden box from my newfound Pakistani friend downtown.

Tube Sockets

One can still buy new octal sockets.  They are manufactured for relays.  However, all the tubes I got have 9 or more pins, so that is no help.  I'll have to make a plan with some D-sub connector sockets - they look like they will fit.

Terminal Strips and Turret Boards

These old fashioned high voltage solder tag strips are hard to find, but Digikey and Mouser do have them, if you know to search for 'turret boards'.

For hooking the parts up, you need 22 AWG tinned coper wire, which is unhelpfully named 'bus bar wire' by the vendors.  It is available in little 100 foot rolls.  Don't use 'magnet wire' - well, you could if you are a masochist.  Magnet wire is insulated with epoxy or nylon which is very hard to strip.

Once all is working, clean with alcohol and spray the electronics (not the tubes!) with V-66 or similar conformal coating to keep it from corroding and it will keep working and look nice for years.

Friday, February 6, 2015

Windows 10 on Virtualbox

What? Herman uses Windows? Shock, horror...

My first desktop computer - if you could call it that - was a Commodore Vic 20 - ugh.  The second one, was an Apple IIe, which was probably the first desktop computer worthy of the name.  Since then, I have used many, many systems, including Sperry, UNIVAC, Varian, DEC and some that nobody ever heard of.

As far as Microsoft operating systems go - if you can call them that - I have used practically every version of everything O'l Billybob ever made.  I even once received a cheque from Microsoft for one of their infamous lawsuits that they lost.

If it is Free, Take Two!

You can download a Windows 10 ISO file for free here:

Windows 10 Running in a Window on Fedora Linux KDE

You just have to give them your whole pedigree and create an online account, but if you are old enough to do this on your own, then MS already has your whole pedigree and you may even have an account too... 

The Magical Incantation

After downloading the thing (4.9 GB), run Virtualbox and create a "Windows 8.1, 64 bit" disk.  Select the ISO file in the Storage, CDROM widget and boot up.

That trick above is the sole reason I'm writing this, to save you from a Blue Screen of Death Error 5D.


Turn power management off, since your host already does that, install Guest Additions, enable file sharing and once everything is working right, disable the network, since that is the only way to keep a Windows machine secure.

Now please don't blame me for all the usual problems you may get with Windows...

La Voila!


Monday, January 12, 2015

Makefiles Made Easy

The bane of computer programming is the Makefile. As soon as there are more than one source file and a library, a Makefile becomes essential and generating one can be painful.  The Makefile syntax reminds me of a sendmail configuration file, which looks like someone banged his head on a keyboard.

# This rule ensures that all local mail is delivered using the 
# smtp transport, everything else will go via the smart host. 
R$* < @ $* .$m. > $* $#smtp $@ $2.$m. $: $1 < @ $2.$m. > $3 dnl

Obvious, yeah...

Same as with sendmail, you should never edit a Makefile manually.  It is a total waste of time.  There are nice and simple utilities to do it for you!


If you have a very simple project, then gcc can generate the Makefile for you:
$ gcc -MM *.c > Makefile
$ make

Error: cc1plus

If you get the following or similar error regarding cc1plus, then the solution is not at all obvious:
gcc: error trying to exec 'cc1plus': execvp: No such file or directory

The problem is that while gcc is installed, g++ is missing:
# yum install gcc-c++

Also see the previous post for details on installing gcc.


If your project is more complex, then cmake can generate the Makefile for you, but you need to give it a few instructions in the file CMakeLists.txt.  First install cmake and related tools:
# yum install cmake*

Now create the file CMakeLists.txt and tell it which are the output, source and libraries:
add_executable(flasher flasher.c)
find_library(FTDI ftdi)
target_link_libraries(flasher ${FTDI})

Note that gcc assumes that library file names start with lib and end with .so, therefore only specify the base ftdi or whatever, not

The first time you run cmake, you need to specify the compiler:
$ CXX=gcc
$ export CXX

After that, run cmake to generate the makefile complete with all the obscure, head banging, gobbledygook lines:
$ cmake .
-- Configuring done
-- Generating done
-- Build files have been written to: /home/herman/sw/ftdi

Now you should have a very detailed almost 200 line Makefile and can compile your project with:
$ make

La voila!

Serial Port I/O

Until about 20 years ago, most desktop and portable computers had serial and parallel ports built in.  Those ports were great for controlling and testing home brew gadgets.  Modern machines are blessed/cursed with USB ports, which are just getting more and more complex.

The result is that if you want to do anything at all in your Underground Lab or Rooftop Radio Shack, you need an Arduino,  a Raspberry or a Beaglebone embedded computer.

However, every respectable engineer has two or three FTDI USB to RS232 serial adaptors lying in a drawer (to control his Raspberry or Beaglebone).  These adaptors are great, since they effectively insulate your target system from your expensive computer, therefore whatever goes wrong on the far end, is unlikely to fry your machine and they are delightfully hackable.

I prefer the dongles made by SerialComm.  They are cheap and both RS232 and RS422/485 are available.

You can also get ones from Sparkfun that are even more hackable, or you can crack the case and remove the line driver chip from a regular off the shelf one if you are desperate for TTL I/O lines.

The secret Open Sauce is the libftdi project, which is available for Linux and Mac systems. Documentation and examples are here.  The online files are the latest and greatest.  Therefore it may be best to work with the header file on your machine which is /usr/include/ftdi.h since your installed version of the library may be older and some functions may be deprecated or missing.

Simple Bitbanging with libftdi

Here is a tiny little C program for an older version of libftdi, that will toggle the Tx, DTR and CTS lines on a USB RS232 adaptor.  Note that you have to run it as root (or join some or other USB group), otherwise the USB device will not open.

/* File flasher.c */
/* libftdi API Example LED Flasher */

#include <stdio.h>
#include <ftdi.h>

/* RS232 DE9 pins */
#define DCD 0x40 /* 1 in */
#define RX  0x02 /* 2 in */
#define TX  0x01 /* 3 out */
#define DTR 0x10 /* 4 out */
#define DSR 0x20 /* 6 in */
#define RTS 0x04 /* 7 out */
#define CTS 0x08 /* 8 in */
#define RI  0x80 /* 9 in */
/* 5 GND */

int main()
    unsigned char data = 0;
    unsigned char pins = TX | DTR | RTS;
    struct ftdi_context context;

    /* Initialize libftdi */

    /* Open FTDI dongle using FT232RL vendor & product IDs */
    if(ftdi_usb_open(&context, 0x0403, 0x6001) < 0) 

        puts("ERROR: ftdi_usb_open()");
        return 1;

    /* Set bitbang mode on the RS232 output pins */
    ftdi_enable_bitbang(&context, pins);

    /* Forever */

        data ^= pins;
        ftdi_write_data(&context, &data, 1);

Improved Bitbanging with libftdi

Here is a more advanced program with proper error checking.  Note that you have to run it as root (or join some or other USB group), otherwise the USB device will not open.

/* File flasher.c */
/* API Example LED Flasher */
/* Copyright reserved Herman Oosthuysen, 2015 */
/* License: GPL version 2 or later */
/* Use at your own peril */

#include <stdio.h>
#include <stdlib.h>
#include <ftdi.h>

/* RS232 DE9 pins */
#define DCD 0x40 /* 1 in */
#define RX  0x02 /* 2 in */
#define TX  0x01 /* 3 out */
#define DTR 0x10 /* 4 out */
#define DSR 0x20 /* 6 in */
#define RTS 0x04 /* 7 out */
#define CTS 0x08 /* 8 in */
#define RI  0x80 /* 9 in */
/* 5 GND */

int main()
    int i;
    int ret;
    unsigned char data = 0;
    unsigned char outputs = TX | DTR | RTS;
    struct ftdi_context *ftdi;

    /* Initialize libftdi */
    printf("FTDI Initialize\n");
    ftdi = ftdi_new();
    if (ftdi == NULL)
        fprintf(stderr, "ERROR: ftdi_new()\n");
        return EXIT_FAILURE;

    ret = ftdi_init(ftdi);
    if (ret < 0)
        fprintf(stderr, "ERROR: ftdi_init() = %d\n", ret);
        return EXIT_FAILURE;

    /* Open FTDI dongle using FT232RL vendor & product IDs */
    /* Plug the device in and run 'dmesg' to see these codes */
    printf("FTDI USB Open\n");
    ret = ftdi_usb_open(ftdi, 0x0403, 0x6001);
    if (ret < 0)
        fprintf(stderr, "ERROR: ftdi_usb_open() = %d\n", ret);
        return EXIT_FAILURE;

    /* Set bitbang mode on the RS232 output pins */
    /* pins: Output = 1, Input = 0 */
    printf("FTDI set Bitbang Mode\n");
    ret = ftdi_set_bitmode(ftdi, outputs, BITMODE_BITBANG);
    if (ret < 0)
        fprintf(stderr, "ERROR: ftdi_set_bitmode() = %d\n", ret);
        return EXIT_FAILURE;

    /* Flash for a little while */

    /* Note: Use ftdi_read_pins() to read data directly */
    printf("FTDI Flashing...\n");
    for(i = 0; i < 10; i++)
        printf("%d\r", i);
        data ^= outputs;
        ret = ftdi_write_data(ftdi, &data, sizeof(data));
        if (ret < 0)
            fprintf(stderr, "ERROR: ftdi_write_data() = %d\n", ret);
            return EXIT_FAILURE;

    /* Done */
    ret = ftdi_usb_close(ftdi);
    if (ret)
        fprintf(stderr, "ERROR: ftdi_usb_close() = %d\n", ret);
        return EXIT_FAILURE;

    return EXIT_SUCCESS;

CBUS Discretes

In addition to the 8 lines used for RS232, there are 4 more, which are used to control the LEDs and RS485 drivers.  These lines can be controlled in a similar fashion using BITMODE_CBUS.  It is not clear whether one can interleave the two bitbashing modes in order to control all 12 lines at the same time and whether the lines will glitch if one does. See this example.

I also read that one can use the CBUS bitbashing concurrently with the normal serial mode, to provide a UART plus 4 discretes, but I have not tried it.

Compile and Test

Assuming that your Linux machine is configured properly with GCC, compile it thus:
$ gcc -o flasher flasher.c -lftdi
$ chmod 754 flasher

Now stick a LED with a 1k resistor in series onto the RS232 adaptor Tx and Gnd pins to see how it works and run it:
$ ./flasher

Install and Configuration of libftdi

If you don't have a GCC and libftdi configured system yet, then assuming that you have Fedora Linux:
$ su -
# yum update

# yum install kernel-headers

# yum groupinstall "Development Tools" "Development Libraries"
# yum install libftdi libftdi-devel

Now you can control the world!


Also see the next post on Makefiles.

Thursday, January 8, 2015

Ecrasez l'infâme

A moment of silence please, in a world gone mad.

$ make bzImage
$ echo Not war.

Tuesday, January 6, 2015

MGL V6R ATC VHF Radio Control Protocol in Bash

The MGL V6R and V10, ATC VHF radios have a binary protocol (described in the V10 manual), in addition to the ubiquitous Garmin SL40 protocol.

The Emergency/Setup channel is 121.5 MHz.  Don't use it for testing...

In this example, I used the Bash let operator to do the calculations and printf is treated as a built-in function - TIMTOWTDI.

Here is a Bash script that attempts to compute the frequency message:

#! /bin/bash
echo MGL V6R ATC VHF Radio Protocol

STX="02"        # Message header
DLE="05"        # Message header
CC="00"            # Active Frequency message ID = 00H, Standby Frequency message ID = 01H
KHZ="000000"    # Frequency in Kilohertz, decimal string (6 digits)
KHZH="000000"    # Frequency in Kilohertz, hexadecimal string (3 bytes)       
KHZ0="00"        # Frequency in Kilohertz, B0, LSB first
KHZ1="00"        # Frequency in Kilohertz, B1
KHZ2="00"        # Frequency in Kilohertz, B2, MSB last
SUM="00"        # XOR checksum from CC to last data byte, then XOR with 55H to invert some bits

if [ -n "$1" ]; then
echo "Frequency = $KHZ kHz Decimal"

echo "Set serial port USB0 to 9600N81"
stty -F /dev/ttyUSB0 raw
stty -F /dev/ttyUSB0 9600

echo "Compute frequency in hexadecimal"
printf -v KHZH "%06x" "$KHZ"
echo "Frequency MSB first = $KHZH Hex"

KHZ0=$(echo "$KHZH" | cut -c 5-6)
KHZ1=$(echo "$KHZH" | cut -c 3-4)
KHZ2=$(echo "$KHZH" | cut -c 1-2)
echo "Frequency LSB first = $KHZ0$KHZ1$KHZ2 Hex"

# Bash by default handles numbers as decimal ASCII strings.
# A number starting with a zero is considered to be octal.
# A number starting with 0x is considered to be hexadecimal.
# The final modulo 256 reduces the answer to one byte
# and the printf makes it hexadecimal.
echo "Compute byte wide XOR checksum"
let "SUM=$CC ^ $KHZ0 ^ $KHZ1 ^ $KHZ2"
let "SUM ^= 0x55"
let "SUM %= 256"
printf -v SUM "%02x" "$SUM"
echo "SUM = $SUM"

# The format string "\x" tells echo to output each variable as an
# 8 bit binary value to the serial port,
# and not as two ASCII characters.
echo "Frequency message = $STX $DLE $CC $KHZ0 $KHZ1 $KHZ2 $SUM"
echo -en "\x$STX\x$DLE\x$CC\x$KHZ0\x$KHZ1\x$KHZ2\x$SUM" > /dev/ttyUSB0

echo "La Voila!"
exit 1

echo Example: mglfreq 123450
exit 0

Please note that I don't know whether this is completely correct yet.  There is always some uncertainty with interpreting an ICD and I may have something wrong still and MGL Avionics sent me the V6R protocol doc after I wrote the above based on the V10 document.

By implementing this in a Bash script, any problems can be fixed easily and one will probably only need to rearrange some variables in the final echo statements to make it work.

Use at your own peril though...