Playing With Flash

I recently picked up one of these $10 CH341A USB flash programmers and a test clip. I didn’t really have any initial plans for that it but thought it might come in useful someday while messing with router bootloaders. This unexpectedly turned out to be a lot of fun so far and not a bad tool for the price.

Rummaging through my parts bin, I found a Winbond 4K SPI flash of unknown provenance, hooked it up, and read out the contents with flashrom. Turns out this was an AMI BIOS that I pulled from some motherboard way back when. Disassembling the BIOS might make a good rainy day exercise.

I tried using chavrprog to program an ATTiny13, which did not work at all. The chip did not appear to respond to any of the SPI commands. Oh well.

On a bricked router I had sitting around, I read the flash directly from the board using the test clip. I happen to remember the confluence of events that lead to me bricking this (writing bootloader to wrong partition), and could finally verify the damage on the flash dump. When I get a free moment, I’ll try bringing it back to life by reflashing a repaired image.

On a different model, unbricked router, reading out the flash on the board didn’t work. Applying power to the flash chip powered up other components on the board, so I’m guessing I’d have to desolder this flash chip in order to read/write it. Maybe worth retrying after spending a few weeks practicing SMT rework.

RFID reader

In the previous installment, I was messing with the serial library on Arduino to get it to work on the older board. The purpose was this:

Pictured is an RFID reader from SparkFun that I picked up years ago and sat in my parts box since then. This RFID reader is intended to read Mifare tags and as such predates the whole NFC thing by a year or three. The programming interface is to send commands over serial to the device such as “Authenticate sector N”, “Read sector N”, etc. I’ve connected D7 to D2 so that I could use D2 as the interrupt pin to suit my Arduino.

It appears to work, in that I can dump sectors from my yubikey and a tap-and-go credit card. The sector trailer parts look valid, and the first few bytes of sector 0 have the device ID (verified by the NFC reader on my phone). I can’t seem to read any NDEF information; whether that is due to limitations in the reader, bugs in my code, or my almost zero knowledge about how all this stuff works, I’m not sure. Sketch code is over here.

As to possible applications, for that I have no real idea either, which is probably why it sat in the parts bin all this time.

Software Serial on the Arduino NG

As mentioned before, I have a really old Arduino, the Arduino NG with an Atmega8 on board. No fancy embedded Linux wifi-enabled SoC on that thing.

Recently, I found myself doing a bit of yak shaving when I was trying to use a code that was meant for Arduinos of a more recent vintage. And here is what I learned:

The Atmega8 does not have the PCICR (which I believe stands for “pin change interrupt control”) register. This feature allows multiplexing of interrupts so that you can have multiple pins signal the same external interrupt, and the pins that trigger interrupts are programmable. On the Atmega8, you can only use Arduino pins D2 and D3 to directly signal interrupts 0 and 1 respectively.

So the device I was trying to use operates over serial, and the supplied code uses the built-in SoftwareSerial bit-banging serial library to talk to it.
The SoftwareSerial library attaches an interrupt to the configured Arduino RX pin (device TX) to trigger when it goes low, indicating the start of a transmission from the device. This library was only written with the more configurable chips in mind, so didn’t support direct interrupts like on the Atmega8. Google didn’t turn up any obvious fixes other than people running into the same compiler error I initially hit:

error: 'PCICR' was not declared in this scope.

In the end a few short changes are needed to make it work: one to disable digitalPinToPCICR() and related macros (as there is no such register on this device); and another change to attach the proper interrupt inside SoftwareSerial. A patch is here.

To debug this, I hooked up the TX side of a USB-serial adapter to the RX pin on the Arduino, ran screen(1) on the computer to control it, and started mashing keys. Until I got the irq triggering working properly, I also monitored the output with a DMM just to make sure the output was going low when I held down a key. A test sketch for that looks like this:

SoftwareSerial port2(2, 3);

void setup()
{
  Serial.begin(9600);
  port2.begin(9600);
  Serial.println("Listening...");
}

void loop()
{
  while (port2.available()) {
    Serial.write(port2.read());
  }
}

It works fine. I’ll write about what I was doing with it in some future installment.

Arduino pumpkin lights

Lacking creativity this year, I kind of phoned in the pumpkin carving. We went with regular old triangle-eyed Jack-o-lanterns. It turned out this suited Alex fine: he has decided lately that a certain level of traditionalism is called for. Watching keenly as I cut away bits of pumpkin, he was quick to point out whenever I was doing it the wrong way.

Anyhow, not wanting Halloween to pass buy without doing something new, I decided I’d write a pumpkin light control sketch for Arduino. I have the original board with the Atmega8, and the last time I touched it, the SDK was arduino-0011, which was released about seven years, three domiciles, and one country ago. Since then, I’m happy to see that arduino-mk landed in Debian, and the SDK hasn’t changed APIs in any noticeable way, so it was easy to get back up and running.

The concept is pretty simple: write PWM patterns to an output pin to control the brightness or on/off state of the pumpkin light. I came up with three patterns: fade up and down; random toggle; and sequential stepping. The sketch code is over here.

Arduino light controllerOn the hardware side, I used 3 LEDs for each pumpkin, snipped from a roll that I had left over from my cabinet lighting project. The LEDs expect a +12V supply. On the spool, they are wired in series with a 150 ohm resistor and each LED has a 2.7V voltage drop. Powering that would stretch the Arduino, so I used a separate 12V power supply for those, and used the Arduino to switch an NPN transistor corresponding to each light. I soldered the LEDs to some speaker wire and put the rest of the components on an Arduino protoshield inside a box. This was essentially a lunch hour’s worth of work to get the basic functionality going, and then a little more time to make it neat.

This year Alex and Ian went as Bumblebee and Iron Man respectively (-ENOTPICTURED). Sadly, with store bought costumes — homemade ones will have to wait until one of us learns to sew something other than kites.

DWM-W034 UART

In response to my previous post about UART access on the Alfa AWUS036NHA, I got a nice email from Jim Ewing about the DWM-W034, an ath9k_htc device that apparently is embedded inside HDTVs and readily available for $10. He found the TX/RX pins on the board, and it looks like it would be a bit easier to put a socket on one of these compared to the Alfa since there are no pesky chips nearby.

With his permission, and so that this information doesn’t get lost to the sands of time, here’s the pic he sent me. Enjoy!

Reserialized

I got a shiny new device today and the first thing I did was crack it open and heat it to 680 degrees. A good feeling.

The device in question is an ath9k_htc USB wifi dongle. A colleague suggested this one has an easy to access UART because they brought out the TX/RX pins out from the SoC to dedicated pads. He and I apparently have different definitions for “easy.” Unlike the TP-Link routers which give you through-holes to work with, these are simply SMT pads, and they are tiny in comparison. My soldering iron tip is at least double the size of the pad. Yes, it is somewhat better than trying to solder directly to the pins on the SoC, but not by a whole lot.

As it happens, this is my first attempt at soldering SMT. What could go wrong?

Because the pads are small I used some wire-wrap wire (30 gauge? All I know is that it’s at least two stops smaller than the smallest hole on my wire stripper: getting insulation off was “fun”). During my comedic attempts at getting the wires stuck in the right places, I managed to completely obliterate one of the two pads. Luckily, the one I screwed up was the RX pin, which I don’t really need or care about. Having got the TX and ground wires (badly) soldered in place, I made a some circumspect passes with the magnifying glass and continuity tester to convince myself there were no shorts.

Even so, I am as surprised as you are that it actually worked when I turned it on the first time. And the wifi still works too.

==>[cUSB_REQ_COMP]: 0x%08x
VendorCmd: DownloadComplete!
5. usb only!!
 A_WDT_INIT()
 ==>warm start<==
ALLOCRAM start 0x50d80c size 106484
Enable Tx Stream mode: 0x367
USB mode: 0xf
[+++Magpie_init]
[+++VBUF_init(100)]
[+++VBUF_init(100)]
: Attaching the driver
: Vendor id 0x168c Dev id 0x24
ath_pci_probe 24
 ath_hal = 0x00510928 

        =>[dnQ] 0x0050f288 
[       =>[upQ] 0x0050f264 
[       =>[hp dnQ] 0x0050f240 
[       =>[mp dnQ] 0x0050f21c 
[Tgt running]

Scoped

G#I have this more-expensive-than-my-car hunk of metal on loan for the next few weeks. It’s a pretty far cry from the old Tektronix analog scopes I trained on back in the day (which, at the time, were also much more expensive than my then-primary mode of transportation: shoes). As a warmup exercise, I hooked it up to the output of my guitar amplifier and played an A, which you can see pictured. You can also see that I’m apparently tuned down a half-step as the frequency measured on the scope is about a semitone shy of 220 Hz.

Making wavesThis was a neat teaching opportunity for my four year-old: I showed Alex visually that sound is just made up of different kinds of waves. Later at bathtime he connected that with the physical waves he was making in the tub: “Just like music!” he said.

Pumpkins all Pumpkiny

It is a bit late for a Halloween post, but insert lame excuse here. Consider it a counter-balance to the force that causes Christmas decorations to show up in stores in September.

IMG_4305This year I chose Toopy and Binoo, a Canadian children’s cartoon pair that you have probably never heard of, as subjects for my pumpkin carving. Unfortunately, the pumpkins shriveled quite quickly, and the designs didn’t lend themselves well to preservation, so by Halloween, they grew a toothpick scaffolding to maintain their facade. Next year, I believe I’ll try not cutting all the way through to avoid such issues. (I’m not sure what that technique is called, but some of the more artistic neighbors employed it to great effect.)

5 minute pumpkinImprovised pumpkin lightInside these gourds I used little battery-powered LED lights made for the purpose instead of actual candles. While LED lights don’t look nearly as nice as real candles, one need not care so much about potentially setting things on fire. On the evening of the 31st, as a few groups of kids had already arrived and absconded with their hard-earned treats, I found myself with a third pumpkin untouched by blade, but no light (or candle) to go in it, should I decide to carve it. Then, I remembered my extra spools of SMT LEDs from the cabinet lighting project. I calculated that a 9 volt battery could power three such LEDs for about 10 hours (that estimate was conservative by a factor of 3, it turned out — I need to go back to EE school). Thanks to various other projects, I already had some speaker wire with alligator clips on each end, so, in the course of 5 minutes, I threw together a functional pumpkin light, carved a few holes in the pumpkin, and called it a day. No need to clean out the guts when open flames are not a factor.

IMG_4423For his first Halloween, Ian went as a dragon. Typical of his recent enthusiasm for all things space-related, Alex went as an astronaut. Instead of saying “trick-or-treat,” he would announce, “Hi, I’m Alex. I’m an astronaut!” Much candy was received, all the same.

Amped

Some progress has been made on my guitar chip amp. Readers may remember that at the time of my last post, I had tinkered with KiCad and created the basic layout for my PCB. Since then, I finalized the design and had it fabbed through the great OSH Park batch prototyping service. This is a great deal: it wound up being about $10 a board (you get three at $5/sq. in) and the boards are of excellent quality. It took about 3 weeks from time of design submission for the finished boards to arrive in Canada from the States.


Practice amp PCB - topPractice amp PCB - bottom

Above are the top and bottom of one the unpopulated boards. The solder mask is the OSH Park signature purple, and the through-holes are gold-plated. The bottom side silkscreen says “This silkscreen intentionally left blank” — a last minute addition because the service’s design checks fail if any layers are missing. I should get more creative with that next time.


Speaker box practice amp

Although I do plan to someday build a wooden speaker cabinet to house everything, for now I just shoved all the electronics in the box that the speaker came in, as it happens to be the right size. Plug the instrument cable in the front, flip on the illuminated switch, and one is ready to rock.

I had all the parts waiting for the boards, so when they arrived I quickly populated one and hooked it up, plugged in my Les Paul and strummed an E chord. The resulting sound was something short of musical: loud, ringing, distorted noises were heard instead of the clear tones of my prototype. I feared that my board design was flawed, and without a scope handy, it would be tough to track down. But on the plus side, I knew that I was about to learn something.

I spent an hour or so following all of the traces on the board and comparing them to the schematic, making sure the caps were all connected the right way around and so forth. I also compared the finished board to my protoboard, which happened to work fine. Everything was the same, except where I had used two 1 ohm resistors in series to invent a 2 ohm resistor (the datasheet called for a 2.2 which I didn’t have at prototyping time).

Then I looked at said resistors a little more closely. On the outside edge, I could just make out a faint yellow band where I previously thought there was none. And that brown band was just the tiniest bit in the purple spectrum. Oops! My 1 ohm resistors were actually 47 ohms, making the gain of my prototype a measly three instead of the 101 I thought it was. It turns out that a gain of 101 is way too high without a volume knob. Also, one of the 47-ohm resistors was in a stabilizing RC circuit, likely causing the ringing oscillations I had heard in my PCB version.


Practice amp populated

I fixed the RC circuit when building board number two, but stuck with the 2.2 ohm resistor and accompanying large gain. I might use this version if I decide to put a volume knob in front of the amplifier, but I do find it’s a little too noisy overall for my tastes. I thus went back to board number one, desoldered the 47 ohm and 2.2 ohm resistors and replaced them with 1 ohm and 22 ohm resistors respectively, lowering the gain of that board to a modest eleven.


Practice amp prototypePractice amp PCB guts

Pictured above are the before and after of prototype and PCB versions. There are certainly a few things I would change if I ever make another revision of the PCB. For one, the annular rings on the heat sink and connector footprints were too small, so soldering them was difficult with so little of the pad exposed. I might also do away with or reduce the size of the ground plane; even with the thermal reliefs, soldering and desoldering the ground connections was no easy task. But on the whole, making the PCB was an interesting experience and I look forward to having another excuse to do so again.

I’ll put the design files up on github one of these days.

KiCad Level 0 Achieved

When we moved to Canada, I sold any guitar gear that wouldn’t fit in a backpack. This has left me without the ability to plug in my electric guitar for the last ten months, a situation that must be rectified (ha, ha). I just need a small practice amp, and those can be found on an IC these days. One needs only to add a little soldering, attach a speaker, repeat as necessary for whatever mistakes one makes, and ta-da: a small custom amp that one could have bought mass-produced from China for 1/50th of the price of building one’s own. But of course there is value in the making, or so we tell ourselves.

Anyway, I’ve built my circuit (more or less the same one as on the op-amp’s datasheet) on a breadboard and it sounds fine except for the complete lack of shielding. Since it’s now possible to get one-off PCBs without spending a fortune, and using a real board is more fun than protoboards, let’s do that!

Back in the dark ages, I used to work on a PCB software that sold for $30k/license (I thought that was ridiculous then too). While I seem to have stuffed some arcane knowledge about keepouts and autorouters somewhere in the back of my brain, most of the PCB black magic did not rub off on me. After a 15-year hiatus from that domain, I can firmly label myself “PCB novice.”

Eagle Light (free as in beer) is the choice of many a hobbyist. I have poked at Eagle before, but never liked it enough to do anything substantive. These days, getting it to run at all means finding library versions that aren’t used in any current Linux distribution, building them, doing some LD_LIBRARY hacks, setting up a 32-bit runtime, and so on. Yes, I also did that this weekend. Eagle has a nice big standard library, but I feel learning it at all is a bit of a dead end when there are usable, truly free alternatives. And KiCad looks like it just may fit the bill. I apt-get installed it and was off to the races.

Overall, KiCad is a nice piece of FOSS software that can easily do whatever one would do in Eagle. Unfortunately, it still reminds me of what I hate about PCB software in general. I find designing a PCB to already be extremely fiddly and unfun; as a non-expert, I just want to build a schematic and then place and route things with sensible defaults. Instead, what I wound up doing was learning to use the symbol editor, and the module editor, gathering various facts about drill and pad sizes, and picking up KiCad’s various quirks such as how you have to constantly reload newly created libraries, and how the UI frequently makes no sense. Of course, UIs never make any sense in EDA tools, so you sometimes just have to go with it. Doing librarian work is ultra-boring, especially for tiny boards like this.

Still, in the course of a few hours, I built the basic schematic (including adding three new symbols), passed ERCs, created three missing footprints, and did an initial routing of the board. Not too bad.

In the last decade component libraries have grown 3-D models, which is kind of neat. On the one hand, it’s not terribly useful since the canned components are unlikely to closely match reality, and editing 3-D models of components is even less interesting to me than editing their footprint. But, it does make for decent screenshots like the one above.