Tuesday, July 31, 2012

Transistor abuse: using the avalanching effect of a BJT transistor to generate short pulses

I recently came across an EEVBlog video on a little circuit made popular by Jim Williams in Linear Technology Application Note 47.  This is a simple pulse generator using the breakdown avalanching effect of a BJT transistor which can be used to calculate the bandwidth response of an oscilloscope among other things. Rise times in the order of 300ps are easily achieved this way. The idea of making transistors do things they were not really intended to do is intriguing and I couldn't resist giving it a shot  myself.

The device used in the app note is a Motorola 2N2369, which breaks down at about Vce = 90V. I don't have a PSU that reaches 90V, so I just ordered some low cost, low Vceo transistors.

The circuit used is almost identical to that in App Note 47. I used a larger capacitor (39pF vs 2pF in AN47).


The results have so far been quite pleasing. The following are traces from pulses generated from 3 different BJT devices (see captions under images).  Time base is 20ns / division. My setup is not optimum: the circuit above was implemented on a bread board and the pulses were not coupled to the oscilloscope via 50Ω transmission line. Also my scope is rated at 100MHz bandwidth.

DUT is NXP BFG424F. Avalanching observed when Vce is 15.8V.

DUT is NXP BFG425W. Avalanching observed when Vce is 16.4V.

DUT is NXP BFT25A. Avalanching observed when Vce is 11.8V.

Saturday, June 30, 2012

OKI 900 mobile phone limited tear down

I found my old OKI 900 analog mobile phone while looking for a ferrite rod in a junk box at my parents home. This unit was manufactured in 1991. I acquired it second-hand around 1993. It cost me about a months wages back then.

Googling this phone, it seems to have been a very hackable phone back in it's day. It's based on the 8051 MCU core and there are various ROMs available on the network to do all sorts of interesting things.  However, right now I don't have any interest/time in making any mods to this. Indeed I couldn't even be bothered powering it up. So I'll take Dave Jones advice: "don't power it up, take it apart"... but only out of lazyiness :-)






Saturday, May 12, 2012

Lidl 'Silvercrest' radio teardown

Last week Lidl (a German budget supermarket chain) had a small "transistor" radio on special. Like a lot of their low end electronics it was "Silvercrest" brand. For various reasons I had need of one (actually I was planning to canabilise it for its ferrite rod antenna, but found an alternative source in the mean time).

Usually Lidl stuff is fine. Not brands you'd normally recognize, but solid stuff none-the-less. So this was rather disappointing.
On powering up, the LCD was faulty. The top half of the display wasn't working.

So I had a look inside:


Apart from the grubby PCB (normal enough for low end electronics these days) there was a botched bodge capacitor (not connect on the left terminal) and a wire to the ferrite antenna nicked by a screw.

So I exchanged it for another. But thankfully decided to checked it out in the car before leaving. Radio #2 didn't even power up. So I tried #3 which did work fine. Two out of three failure rate seems a bit high... or maybe I was just unlucky.

The IC in the photo is a Sony CXA1691 AM/FM radio chip. Everything else looked like it was lifted right out of the 1970s. But hey, it's just a "transistor" radio...

Thursday, April 12, 2012

The STM32W-RFCKIT as a 802.15.4 network analyzer on Linux

Summary: The STM32W-RFCKIT together with ST Microelectronics application note AN3406 is a low cost 802.15.4 (ZigBee, MiWi etc) network analyzer. Unfortunately the solution is Windows only and closed source. In this article I have reverse engineered the firmware protocol used in this app note so that a similar solution can be implemented on Linux, Mac OSX and other platforms. I have also written a first draft of this software for Linux.
STM32W-RFCKIT
USB dongle
The STM32W-RFCKIT is a low cost evaluation kit for the STM32W108 802.15.4 SoC from ST Microelectronics. The kit retails for about €33 ($40?) and comprises a USB dongle and a battery powered remote control device with buttons.

I was drawn to this kit because I had recently purchased a STM32F4Discovery board and I was impressed at how easy it was get a GCC tool chain up an running on Linux. I'm currently on the look out for a reliable, low cost 802.15.4 network analyzer solution and given its low cost,  the hope of Linux friendliness and the 802.15.4 transceiver I figured I had a good shot with this hardware.

I was pleasantly surprised to find the kit already comes with a free 802.15.4 network analyzer.  One one of the associated application notes, AN3406, is a 802.15.4 packet sniffer for the dongle. This works by loading special firmware onto the dongle. This then communicates with a Windows server program which translates the dongle's captured 802.15.4 packets (encoded in their own proprietary frame format) into PCAP. The Windows server program also invokes Wireshark and feeds the packet data to it.

Block diagram of the ST Microelectronics AN3406 packet sniffer solution (taken from application note AN3406).
Unfortunately this is a Windows only solution. As a Linux user it's always a pain to have to boot a Windows virtual machine (consuming a huge chunk of my available RAM) just to run one tool. So I set about writing a tool to facilitate the same functionality in Linux.

Although the AN3406 software is free, it is not open source and there is no documentation on the dongle to Wireshark server protocol. So I had to reverse engineer the dongle's protocol to get the same functionality with Linux.

I used the free (and excellent!) serial port monitor tool at www.serial-port-monitor.com to capture the packets to/from the device while opening the Wireshark server and starting a packet capture. Fortunately everything looked very straightforward and it wasn't difficult to write a Linux based server.

The packet structure is

All frames are prefixed with two bytes 0x15, 0xFF. This is followed by a one byte frame length field which is the number of bytes in the frame (excluding the 0x15, 0xFF prefix, the checksum and 0x0C terminator). All frames have a packet type or command code, therefore the minimum valid value for the frame length is 2.

This is followed by a one byte packet type or command code. I notice that frames sent from host to dongle have the command code most significant bit (MSB) cleared while those from the dongle to the host have the command MSB set. For example command 0x10 (set channel) is responded by command 0x90.

Depending on the command there may be zero, one or more data bytes.

Next a frame check sum which is calculated by summing of all bytes from the length field to the last data byte in a 8 bit register and then doing a bitwise NOT.  Finally all frames are terminated with byte 0x0C.

The known command codes are:

Command CodeDirectionDescriptionParameters / Data
0x01 Host to dongle No idea. However it's required for packet capture to work None
0x81Dongle to hostResponse to 0x01 command.One byte: 0x00.
0x10Host to dongleSet 802.15.4 channelFollowed by one byte of data which will contain the channel number (11 to 26).
0x90Dongle to host Set channel responseSame as 0x10
0x11Host to dongleStart relaying packets.No parameters
0x91Dongle to hostStart relay response.No parameters
0x12Host to dongleStop relaying packets.No parameters
0x92Dongle to hostStop relay response.No parameters
0xF0Dongle to host802.15.4 packet data802.15.4 packet metadata (timestamp, RSSI) followed by actual packet data. See section 'Packet capture format' for details.


Packet capture format:

802.15.4 packets are returned along with some packet metadata in the data portion of frames with command code 0xF0. After observing a few frames it quickly became apparent that the actual 802.15.4 packet data started about 8 bytes in. Thus the first 7 bytes is some sort of metadata.

I'll refer to these bytes by their index values starting at 0. Byte 5 remained constant throughout the entire capture and happened to be the same value as the 802.15.4 channel number. So I can safely say that's the channel number. Bytes 1 to 4 also grew in value consistent with a time stamp. As for byte 0, I wasn't sure about that. It seemed random consistent with the LSB of a time stamp, but I thought 5 bytes for a time stamp was odd. Also I figured that there must be a link quality indicator (RSSI) in there somewhere also. Byte 6 looked like a good candidate for the RSSI. So how to know? Plotting these values as a function of time can often reveal their meaning:


Byte 0 (blue) seems to be completely random. So my guess is that's the the least significant byte of a 5 byte time stamp clock. Byte 6 (red) on the other hand must be the RSSI. There are several ZigBee devices on the network. Each device will have an RSSI value based on it's distance/location/antenna that won't vary quickly with time. So each of those red traces corresponds to one ZigBee devices.

So it seems that byte 0, byte 1 and the lower nybble of byte 2 form a 20 bit fraction of a second (ie if treated as an integer each unit is 1/(2^20) seconds.  The upper nybble of byte 2, byte 3 and byte 4 form a 20 bit seconds field.

Putting it all together:

To try this, you must first load the sniffer firmware to the dongle. Right now the only way I know to achieve this is using the AN3406 Windows software. I've documented the procedure in this blog post. And it's also covered in AN3406. However I have no doubt that this can also be accomplished with some of the Linux tools used to program the STM32-F4Discovery board. If you know how, please let me know.

I wrote a translator from this firmware protocol to a PCAP format which can be piped into Wireshark. It's released under the BSD licence. Here is my first draft (version 0.1).

To run:

wireshark -k -i <( ./stm32w-wireshark /dev/ttyACM0 12 )

The command line tool takes two arguments: the device (usually /dev/ttyACM0) and the 802.15.4 channel number (11 to 26). Use the -h flag to get information on more options.

Note: there is a problem with launching Wireshark this way. If you close the Wireshark window, the stm32w-wireshark utility does not die automatically and remains running in the background.  Starting it again will mean that two stm32w-wireshark processes will be grabbing data from the dongle causing massive packet corruption. If you see packet corruption, check for running processes (ps -auxw | grep stm32w-wireshark) and kill any lingering processes. If you have suggestions for a better way of handling this please let me know.

What next?

There are a few lose ends I would like to tidy up (and I would appreciate any suggestions):
  • What's the best way of launching this tool together with Wireshark? Closing the Wireshark window should cause the tool to die. Should I be using named pipes?
  • It's a shame that the RSSI value cannot be recorded in the PCAP file (there simply is no field for arbitrary metadata). So if one wanted to use RSSI information together with Wireshark how can this be achieved? 
  • I would like to be able to upload this firmware without having to boot windows. It's the only Windows dependency left and it would nice to remove it. I suspect one of the standard tools used with the STM32 kits will do the job, but I don't have time to figure that out right now. 

Monday, April 2, 2012

STM32W-RFCKIT as a low cost 802.15.4 / ZigBee network analyzer

Only a few years ago 802.15.4 / ZigBee network analyzers were an expensive affair. Now, many of the low cost evaluation kits from manufacturers such as Microchip, Texas Instruments and ST Microelectronics can be configured as network analyzers.

This morning I took delivery of a ST Microelectronics STM32W-RFCKIT (€33 from Mouser). I was pleasantly surprised that it too has a network analyzer in the form of a Wireshark bridge. These days Wireshark is the only game in town for packet analysis, so this was a smart move.


The kit comes in two pieces: a remote control device with 6 buttons and a dongle for the PC.

Unfortunately all this is Windows software. So I powered up a Windows XP virtual machine, and installed the software from the ZIP file SW application to interface Wireshark packet capture tool (AN3406). I also needed to install Wireshark for Windows. I plugged in the dongle and assigned the USB device to my Windows VM (this is the same as physically plugging in the device if it was a real computer). Then the usual windows new hardware detection and driver installation. (I can never quite understand what is going on with Windows when new hardware is plugged in. I did a reboot also, but that was more out of habit... it might not have been  necessary).

Next, start the STM32W108 Wireshark Server. In the Settings menu set the location of the Wireshark application (usually under Program Files/Wireshark) if this has not been already set.

Right out of the box, the dongle does not have the right firmware installed. So select the Tools menu and select "Flasher". Click the 'Flash' button and all going well, the packet sniffer firmware will be transferred onto the dongle.


Now back to the main window of the Wireshark Server application. Select the right serial port. The Play button should now become active. Hit the play button and all going well Wireshark should start sniffing packets from the dongle.

Linux is my OS of choice, so it would be great to have this working without having to boot a Windows VM. Unfortunately it seems that despite the wise choice of using the Wireshark tool and extolling the virtues of open source code in the documentation, they've neglected to include the source code for the firmware and server software. However it shouldn't be too difficult to reverse engineer. If I get a chance over the coming days I'll give it a stab.

A screen grab of  Wireshark sniffing 802.15.4 / ZigBee packets from the dongle supplied with the STM32W-RFCKIT.
Update (3 April 2012): I just exchanged emails with STM tech support to see if there were willing to share the sniffer firmware source code. Unfortunately they are not. So it's either reverse engineer what's already there or write new firmware.

Update (7 April 2012): I did a quick comparison with the STM32W dongle vs my Microchip ZENA (first version). Both devices are on my desk, about 50cm apart. I ran a Wireshark session on both devices at the same time on the same channel. After a few minutes the STM32W device captured 424 packets vs 290 packets from the ZENA.  It seems the STM32W-RFCKIT makes for a better packet sniffer!

Update (9 April 2012): I've made good progress reverse engineering the firmware protocol. I've now got a Linux command line tool (written in C) that will dump packet hex to terminal and allows the 802.15.4. channel to be changed. I hope to release the first version of the Linux Wireshark server in the next few days.

Update (12 April 2012): The first version of the Linux Wireshark server has been released.

Saturday, March 31, 2012

Irish landed estates database 'heatmap'


I am involved with a project to catalog Irish landed estates in conjunction with the Moore Institute at the National University of Ireland, Galway. Right now the database covers the provinces of Muster and Connaught.

The web site includes an interactive map which visitors can use to browse for houses and estates of interest. I thought it might be interesting to make a 'heat map' of the areas which people found the most interesting.

Every time someone zooms into an area on the map, the database is queried for records matching that rectangular area. The web server logs record all these queries (anonymously of course).

The image on the right was generated by summing about half a million of these database queries. I had intended to super-impose the heat map on a Google map, but unfortunately I don't have the time to complete that right now. However one can see the distinct outline of the west and south of Ireland coast line with hot spots around the cities of Galway and Limerick. (Why there is no hot-spot around Cork city? I have no idea!)

Wednesday, March 14, 2012

The weight ratio of dry pasta to cooked pasta

It's not often you ask Google a simple question and you can't get a straight answer. My question was this: "what is the weight ratio of dry pasta to cooked pasta?"

There is plenty of discussion on this matter, with units such as ounces, cups, Fahrenheit, buschels and microfortnights bandied around. But not one answer that had the simple dimensionless ratio I was looking for.

So it's off to the lab (... well kitchen ...) to do an experiment:
  • 100g fusilli (Lid's finest)
  • 500ml water
  • Bring to boil and boil for 10 minutes
  • Strain pasta
  • Weigh pasta
So what is this magic ratio? Well, this is not an exact science. Obviously al dente pasta is going to be lighter than over cooked. And it's probable that the topology of the pasta will have some influence on it's ability to retain water. Pasta topologies with lots of surface area and enclosed spaces (eg pasta shells) are going to have more water clinging to the surface. These are experiments for another day.

Anyhow, as for fusilli, the cooked weight of 100g of dry pasta after 10 minutes boiling was 208g.  So the weight ratio of dry pasta to cooked pasta is

1 :  2.1

Thursday, March 8, 2012

Visualizing diurnal temperature variation

I've been logging temperature from a ZigBee temperature sensor (located in my shed in the back garden) since about November last year. I thought it would be interesting to see the diurnal (daily) temperature variation over time. On the x-axis are days (from about 10 November 2011 to 7 March 2012) and on the y axis is time-of-day, with midnight being at the very top and bottom. Temperatures vary from about -3C (deep blue) to +15C (red). Periods where there is no data are gray. Sorry no legend or axis markings.


The chart was constructed by averaging temperatures into 15 minute bins (with about 5 samples per bin) and plotting each bin as a rectangle in SVG. I then used rsvg to convert to a PNG image.

For comparison here is a chart for the same time period of my home office temperature. The color scale is different, deep blue being 12C and red 22C.


Wednesday, February 29, 2012

Raspberry Pi now available from Farnell and RS Components

The Raspberry Pi Foundation just announced that they are partnering with Farnell and RS Components to manufacture and distribute their much anticipated 'Raspberry Pi' computer.

This is a sensible move. Else the charity would have to built up an Amazon style logistics operation from scratch -- a huge distraction from their mission to provide affordable and hackable computers for teaching, experimentation and kids to play with. I just managed to get my order in to Farnell. It wasn't easy. Their system was creaking under the load! (at 06:00 in the morning!)

A quick summary of what a Raspberry Pi has to offer: it's an electronics board about the size of a credit card (no casing is provided right now). At its heart is an ARM based Broadcom BCM2835 with integrated CPU, GPU and RAM clocked at 700MHz (and a pretty beefy GPU by all accounts!),  256MB RAM, HDMI video output, SD card for storage, 2 x USB for peripherals and external storage, 1 x 100Mbps ethernet port, 3.5mm jack for audio.  And for hardware hackers, a general purpose IO port (with hardware support for UART, SPI, I2C etc).

Cost? Well it's supposed to be about $35. I got mine for €31.87 + VAT (ie about €35) from Farnell. But that includes shipping: so it's a fair price. (BTW: here is a direct link to the Raspberry Pi  Model B at Farnell).

I look forward to my Pi in the coming days!

Update (1 Mar 2012): Just got an email from Farnell. Despite pushing in my order 13 minutes after the announcement I still didn't make the first batch of 10,000 units. ETA for my Pi: end April 2012 ☹


Monday, February 13, 2012

Getting started with the PIC24FJ64GB002 and PIC32MX110F016B


Microchip's new MPLABX development environment (free download) came out of beta a few weeks ago and the PICkit3 programmer/debugger was on special offer. So I figured this was as good a time as any to pick up a PICkit3 and to get a 'hello world' program running on the PIC24FJ64GB002 and PIC32MX110F016B samples I had in my parts drawer. Both are available in 28 pin DIP packages. (Can't beat DIPs and breadboards for quick prototyping!)

Programming the 8 bit PIC range such as 12F and 16F series with the PICkit2 programmer is simple: just connect the five ICSP pins MCLR, Vdd, GND, PGD, PGC from the programmer to the corresponding pins on the chip. Nothing else is needed. The chip can then be probed and programmed. I prefer to use the command line tool.  For example to verify that a chip was present:
pk2cmd -P
and then I'd upload myprogram.hex firmware with something like
pk2cmd -P -Fmyprogram.hex  -M -R
and apply power at 3.3V for testing (internal oscillator must be configured if there is no external crystal):
pk2cmd -P -T -A3.3

The PIC24FJ64GB002 and PIC32MX110F016B chips are not as straight forward. My naive attempt at treating them like the 8 bit PICs didn't work. I noticed searching Microchip's forums that others were have difficulty also. Like most problems relating to programming MCUs the answer is in the datasheet. You either need to read it cover-to-cover or fumble around and experiment. I'm still not sure which approach gets you there faster, but the latter is more fun :-)



There are a few important differences when compared to the older 12F and 16F series. These chips use a dual power supply. The CPU core runs at a lower voltage (usually 2.2V) and the on-chip peripherals run off a higher voltage (usually 3.3V). The core power can be configured to be supplied from an on-chip source or externally. The on-chip supply is selected by grounding the DISVREG pin and connecting Vcap to ground via a 10µF capacitor [Reference PIC24FJ64GB004 family datasheet, section 2 "Guidelines for getting started with 16-bit microcontrollers"].

Another important difference: the chip must be powered up, even for probing and programming. This is not necessary for the 8 bit PICs that I've been using. This one threw me for a bit: MPLABX with PICkit3 kept telling me no target was connected. Finally I twigged that I should have power applied:
right-click on the project and select 'Properties', select PICkit3, select the Power category from the pull down menu, select the desired voltage (usually something close to 3.3V) and check "Power target circuit from PICkit3" checkbox.



So to summarize the setup for both chips (electrically identical, just different names/functions on some pins).

PIC24FJ64GB002
  • MCLR (pin 1) to programmer MCLR
  • 10k resistor (approx) between MCLR and Vdd
  • Vdd (pin 13) to programmer Vdd
  • Vdd (pin 28) to programmer Vdd
  • Vss (pin 27) to programmer Gnd
  • Vss (pin 8) to programmer Gnd
  • PGED (choose from port 1,2 or 3) to programmer PGD
  • PGEC (chose from port 1,2 or 3) to programmer PGC. Must be the same port as that used for PGED.
  • Vcore (pin 20) to 10µF capacitor to Gnd
  • DISVREG (pin 19) to Gnd
  • Decoupling capacitor between pins 28,27
  • Decoupling capacitor between pins 13,8
PIC32MX110F016B
  • MCLR (pin 1) to programmer MCLR
  • 10k resistor (approx) between MCLR and Vdd
  • Vdd (pin 13) to programmer Vdd
  • AVdd (pin 28) to programmer Vdd
  • AVss (pin 27) to programmer Gnd
  • Vss (pin 8) to programmer Gnd
  • PGED (choose from port 1,2 or 3) to programmer PGD
  • PGEC (chose from port 1,2 or 3) to programmer PGC. Must be same port as that used for PGED
  • Vcap (pin 20) to 10µF capacitor to Gnd
  • Vss (pin 19) to Gnd
  • Decoupling capacitor between pins 28,27
  • Decoupling capacitor between pins 13,8
You should now be able to program the chip with MPLAB and the PICkit3.

Finally the configuration bits must be set just right if you want the PICkit3 in-circuit debugger to work. The syntax used to defining the configuration bits seem seems to vary from compiler to compiler. For the PIC24FJ64GB002  this is what worked for me using the Microchip C30 C compiler version 3.30c:

_CONFIG1(JTAGEN_OFF
        & GCP_OFF
        & GWRP_OFF
        & ICS_PGx2  // NB: set to correct channel
        & FWDTEN_OFF
        & WINDIS_OFF
        & FWPSA_PR32
        & WDTPS_PS8192);
_CONFIG2(IESO_OFF
        & FNOSC_FRCPLL
        & OSCIOFNC_ON
        & POSCMOD_NONE
        & PLL96MHZ_ON
        & PLLDIV_DIV2
        & FCKSM_CSDCMD
        & IOL1WAY_OFF);
_CONFIG3(WPFP_WPFP0
        & SOSCSEL_IO
        & WUTSEL_FST
        & WPDIS_WPDIS
        & WPCFG_WPCFGDIS
        & WPEND_WPENDMEM);
_CONFIG4(DSWDTPS_DSWDTPS3 
        & DSWDTOSC_LPRC
        & RTCOSC_LPRC
        & DSBOREN_OFF
        & DSWDTEN_OFF);



The configuration bits I found that worked for the PIC32MX110F016B (Microchip C32, v2.02) are:

#pragma config FPLLODIV = DIV_1 
#pragma config FPLLMUL = MUL_20 
#pragma config FPLLIDIV = DIV_2 
#pragma config FWDTEN = OFF 
#pragma config FPBDIV = DIV_1 
#pragma config WDTPS = PS1 
#pragma config FCKSM = CSECME 
#pragma config OSCIOFNC = OFF 
#pragma config POSCMOD = HS 
#pragma config IESO = ON 
#pragma config FSOSCEN = OFF 
#pragma config FNOSC = PRIPLL 
#pragma config CP = OFF
#pragma config BWP = OFF 
#pragma config PWP = OFF 
#pragma config ICESEL = ICS_PGx2 // NB: set to correct channel
#pragma config DEBUG = OFF

Note that both of these chips have several programming ports. Any can be used for programming. However for debugging you must specify which port is being used in the configuration bits.