Archive for Uncategorized

Bare metal code on the Raspberry Pi

I’ve had my Raspberry Pi for a while now, and it makes a great little Linux system.  I’ve even been exercising the graphics capabilities with our game Iridium Rising

But one thing I’ve been meaning to do is go straight to the bare metal – just run some code on the CPU with no OS to get in the way.  Luckily I’m not alone and there’s a decent forum thread on how to get started.

There was still a bit of work to pull everything together – so I’ve whacked up a git repo with a readme on how to get everything set up.

The end result is the OK light on your Pi blink, but everything is set up ready to do something a bit more interesting.

Stepper Motor

I bought a little stepper motor and H-bridge.  The plan eventually is to integrate it into a steampunk costume, but today I was just getting it wired up to try it out.

I wired things up according to the 4-wire diagram for a bipolar stepper motor from the excellent instructions here.

Here it is running backward and forward off a 9V battery, with the wax off a bottle of whisky on top!

The code is on github, as ever.

RaspberryPi bits

Along with half the rest of the known universe, I’m eagerly awaiting the release of the RaspberryPi.

The Pi is a fullly fledged computer that costs only $35 (or in future $25 for a model with less memory, no ethernet and just one USB socket).  It has plenty of potential for fun projects – there are some general purpose IO pins, and it should be possible to power it off a battery – or just for replacing my old Linux server with something silent, low power and it is just the size of a credit card!

The first batch is due to finish manufacturing on 20th Feb, so hopefully they will be shipping in early March and I’m hoping to manage to get my hands on one (though there’s going to be stiff competition, as there are only 10,000 boards in the first batch and well over 50,000 people that want one!)

The $35 only buys you the board itself – you need to supply your own power, SD card, keyboard, monitor, cables and everything else.  So I thought it was about time to get stocked up on the required subsidiary hardware.  I’ve ordered a couple of 4GB SD cards and a 16GB USB drive, a 4 port powered USB hub (which I’m hoping will be able to power the Pi – if that doesn’t work out I can use the charger from an HTC Desire as a temporary measure), and an HDMI to DVI cable (the Pi doesn’t have a VGA port).

All in all, I ended up paying more for this than the RaspberryPi itself is going to cost!  This got me thinking – one of the intended purposes of the Pi is for use in schools.  They are likely to have some of this stuff (keyboards and mice, for instance), but a combined power source, USB hub and WiFi/wired internet connection would be a very handy combination.  You could even make it power 2 Pis, with 2 independent hubs, then you could sit it between 2 students who were playing with the Pis.  I should post something on their forum…


Someone at work mentioned charlieplexing, which I thought was rather cool.  And potentially useful for debugging things on the ATTiny where you don’t have many pins.

So in a break from playing with the Scalextric bits, I’ve made up a little board with 6 LEDs and a 3 pin header, using a small circular proto board from proto-pic:

And here it is in action:

The code would be very straightforward if I hadn’t overused the ?: operator and bit operations.  Here it is for you to puzzle over:

  for (int j = 0; j < 64; j++)
    for (int k = 0; k < 100; k++)
      for (int i = 0; i < 6; i++)
        if (j & (1 << i))
          pinMode(5, (i < 4) ? OUTPUT : INPUT);
          pinMode(6, ((i < 2) || (i >= 4)) ? OUTPUT : INPUT);
          pinMode(7, (i >= 2) ? OUTPUT : INPUT);
          digitalWrite(5, (i & 1) ? HIGH : LOW);
          digitalWrite(6, (i & 1) ? LOW : HIGH);
          digitalWrite(7, (((i & 4) >> 2) ^ (i & 1)) ? LOW : HIGH);
          pinMode(5, INPUT);
          pinMode(7, INPUT);
          pinMode(6, INPUT);

The outer loop selects one of the 64 possible combinations of the 6 LEDs.

The inner loop lights or does not light each LED in turn.  Because it cycles through the LEDs fast enough all the lit LEDs appear lit simultaneously.

The middle loop just runs round lighting the same set of LEDs hundred times before moving onto the next set.