SD cards on Mbed

So, I don't understand why, but apparently hooking up SD cards to an Mbed isn't just "plug in these wires and import this library"

The official library OR something in my circuitry seems to have a bug. So that I won't forget how it all works though here is my quick write up:

First off, I'm using the new Sparkfun SD card breakout board, so I've "translated" the pinouts between SPI and SD formats:

**NEW** SparkFun SD Breakout Board
MicroSD Breakout    mbed
   D3  o-------------o 20    (DigitalOut cs)
   CMD  o-------------o 11    (SPI mosi)
   VCC o-------------o VOUT
   CLK o-------------o 13    (SPI sclk)
   GND o-------------o GND  
   D0  o-------------o 12    (SPI miso)
   CD  o
   D1  o
   D2  o

Then, for some reason, the SD card library for mbed would never work the first time I ran it. So, I had to fork it and make it so that "if initialization fails, try one more time, just in case". It's delightfully horrible, but I'm tired of trying to find the correct solution to the problem

That forked repo is located at SDFileSystem_tryagain

After doing all this, it finally "just works"

Notes about my setup:

  • The try again bit could be caused by noise. the SD card is about 2-3 inches from the mbed and requires long lines
  • I'm using a 4G SDHC card produced by SanDisk
  • I have no idea what I'm doing with hardware :)

Hopefully this helps some helpless soul searching on google. I know I had a ton of problems finding any reference to this problem, even though people have told me that it does happen

Posted: 4/16/2013 5:37:39 AM

MbedConsole -- Build Report

Well, I think I should finally document how to actually build the MbedConsole. It's very simple...

A full overview schematic(which I think I did a horrible job on) is below:


I think this design is simple enough that it doesn't need much further explanation. One note though is that you can possible increase or decrease the resistance for the 470ohm resistor connected across RGB. Having more resistance decreases the brightness, having less increases it.

Now then, a picture's worth a thousand words... so: (in chronological order)

Taya typing

Taya typing at the basic serial-vga repeater thing


Shell is mostly working, though still gets input from serial and not PS/2


Finally received the PS/2 breakout board and wired it up


Another angle


My final "impressive" feat. Drawing pretty pictures!

Anyway, the code is accessible at mbed and my portion of the code is BSD licensed. The projects current status:

  • PS/2 keyboard completely works except for the caplock and such LEDs
  • The PLEarlz language thing can function as a simple stack calculator. Other than that, it's still a deep work in progress.
  • VGA signal is extremely stable and consistent. 640x480 monochrome graphics updated at 60hz.
  • Interrupts are re-prioritized at startup so that the VGA signal never stutters.
  • Tons of RAM and Flash left over. Just have to find something to do with it!
  • No ethernet/USB support. The VGA framebuffer uses the memory that is reserved for those components

Anyway, if anyone would like to contribute, I'd love it. Right now my top priority is a simple interactive programming shell using some usable language. I've evaluated Scheme, Forth, and Basic, but haven't found anything I can directly port to the mbed. If anyone would like to try their hand porting a language or even writing something from scratch, I'd most appreciate it! (Also, extra points for using a BSD-style license :) ) I'm really bad at implementing a language in C.

Other than that there is a ton of resources left in this tiny microcontroller to tap. Next on my list:

  1. SD card support
  2. file management commands in the shell
  3. possibly bitmap support for reading from the filesystem
  4. a simple game
  5. Maybe even add a speaker for some basic sound support

And tons of other ideas. What do you think would be cool to implement?

Also, if you want to read more about my latest MbedConsole stuff, or look at the history of it, just look at the MbedConsole tag

Posted: 9/30/2012 5:20:38 PM

Displaying Images On MbedConsole

So, I finally got MbedConsole to display a fairly awesome monochrome graphic. Surprisingly, I had a very hard time finding any decent converters from an image to a monochrome byte array in C.

What I ended up finding a tool that works decently enough. The image appears to require a bit of massaging to like to work with it though.

  • Make the image's width a multiple of 8
  • Turn the image into grayscale and then turn the contrast all the way up to convert it to monochrome
  • Don't use indexed colors. Use grayscale or RGB
  • Use a decent format like GIF or PNG

Also, it requires (in my opinion) a bit of massaging to print it to the screen. The bits in each byte are arranged backwards in my opinion. So, my print loop ends up looking like this:

int tmp=0;
for(int y=0;y<HEIGHT;y++)
    for(int x=0;x<WIDTH;x++)
        int byte=tmp/8;
        int bit=tmp%8;
        bit=bit-7; //bits are reversed! Those bastards!
        vga_plot(x,y, (graphic[byte]&(1<<bit))==0);

Also, don't try to compute tmp using X and Y. Just don't, I wasted 2 hours before I gave up and just made it increment for each loop.

I would show the image now, but it's a bit of a surprise :) Don't want to spoil it.

Also, I think I'm going to scrap the programming environment for right now. It's just taking too long and I want to release this thing already.

Anyway, more news later.

Posted: 9/30/2012 5:41:16 AM

More Progress for MbedConsole

Well, I've been making more and more progress on Mbedconsole.

For starters, I finally got the PS/2 keyboard working completely. So now getc and friends now point to the PS/2 keyboard and not the serial port. Also, I've added a few more commands than what I had. I also did a lot of other small things like resetting interrupt priorities so that the VGA console should only be disrupted during semi-hosting events(ie, LocalStorage), and probably if the mbed crashes.


As you can see, there is also plearlz. That's actually less of a work in progress than you'd think. I don't have anything "working" right now because it's in the middle of refactor, but it can function as a simple stack calculator so far. I'll consider it complete when I can add words, create "local" variables, and have looping support(which is nearly there, but not quite)


As you can see, the wiring for this project is extremely simple. Ridiculously simple really. I don't have a single real component on my breadboard! All I have are a few resistors. The only thing I might consider adding in the future is a few caps on the power and ground lines of each component.

Don't worry! I'm not just hyping you up for nothing! I expect to release the code along with a more detailed build report probably in a week or two. It's getting very close to "complete" now. What I consider complete is a self-hosting development environment. I'll of course continue improving on it after I release it :)

Also, in case anyone is searching for a PS/2 scancode set 2 map in C source code, here you go. A complete scancode set 2 to ASCII translation table/array. Also BSD licensed. I had to make this by hand. I've come to the conclusion that no sane individual should ever have to do this! Scancode set 2 is SOOO much more complicated compared to set 1!

Posted: 9/28/2012 5:00:56 AM

MbedConsole Status

I just felt I should update on the status of MbedConsole. A formal release is coming soon! I'll probably release it before my Forth interpreter is completely done after all though.

Anyway, I received my nice PS/2 Keyboard Breakout today. I soldered some pins on it and got it to read scan codes from a keyboard with a bit code. So that part is working pretty well. The next big thing is getting it to correctly translate those scancodes to ASCII text. I've done this kind of thing before, so it shouldn't be too hard.

So far, about 8K of RAM is used up in all of the overhead, which I don't think is too terrible. My goal is to have 20K available to the Forth compiler/interpreter thing and I think I will be able to easily manage that even with a couple of utilities. Of course, I'm also already up to 32K of Flash, but that's no big deal since I have 512K

Anyway, I expect to release the code in probably another week or two. Hopefully with some small demo of how awesome it is.

Posted: 9/26/2012 3:28:54 AM

Mbed Status Update

So, I've been working more and more on my recent mbed project. I've decided to go for something really interesting and not too hard.

The project is named MbedConsole because I'm bad at names. Anyway it will feature

  1. PS/2 keyboard support
  2. Monochrome VGA. 640x480, with text and/or graphics
  3. A simple shell
  4. A Forth programming environment
  5. About 20K or so of usable RAM for the Forth environment
  6. SD card support of some kind maybe

All of this works with no external components but a few resistors. This means, it's going to be awesome!

About the Forth environment:

I'm not aiming at all for Forth compatibility, but it definitely has a Forth feel to it(everything is in a stack). The big thing that this is enables is semi-self hosting.. That means I can write some system software in Forth. Because Forth programs don't constantly occupy RAM, this means I can make a "real" computer with a whole 20K of memory. After getting the Forth environment created I'd like to rewrite my shell to use Forth as well. If I really want to get awesome I could even write my Forth implementation in Forth and leave on the virtual machine in native code... I'm not that brave.

Anyway, the Forth environment was at first planned to be interpreted only. Now however, I've found that compiling to a virtual machine is about 10 times easier than trying to interpret it directly. It also will save memory, and probably will be faster. The virtual machine is extremely simple. and should already be Turing complete. Forth however is not quite there yet. It's easy to parse, but string handling in C is never quick to implement.

The end goal for me is to have a very retro-feeling operating system running on the mbed powered by it's Forth interpreter. Things I want to have:

  • A simple command shell for file management
  • A way to save the Forth programs you write
  • A simple text editor of some sort
  • At least one super simple game

If I remove graphics support, I can also add in network support, but I highly doubt there will be anywhere near enough RAM to be useful for Forth after including a TCP/IP stack. I believe it takes about about 16K by itself :(

Anyway, this project will eventually be released under BSD license. I want to get it half way working before I release it.

Posted: 9/21/2012 5:10:27 AM

New mbed project

So, I recently discovered that the mbed is capable of generating VGA signals without dedicating the entire processor to the task. This is the VGA library I'm using. It has quite a few limitations.

  1. No ethernet/USB/CAN because it uses one of the reserved RAM blocks
  2. Monochrome only. This actually is fine for every application I have in mind.
  3. Using the local filesystem screws it up pretty bad. Basically whenever a filehandle is open, the screen will vary from flickering to not syncing at all. After all handles are closed it acts normal though

Anyway, after I got it hooked up the first thing I wanted to get on it is a simple console. I have that now. In the picture below though I set it up to just print characters typed into a serial terminal.

Taya typing

Here, Taya is enjoying typing and is begging me to let her "play a game" heh. So, now I know what the next step is I suppose.

Anyway, when I get something that actually does something useful, I'll post the source code as BSD licensed.

Posted: 9/17/2012 4:59:39 AM

VT100 Terminal Interface

Long time, no blog. I've finally been getting settled down here in Cleveland, so expect to see a bit more posts here.

Anyway, my recent project has been a bit of an interesting thing on the mbed. I built a super simple curses "replacement" named TermControl. It's not really a curses replacement. It's basically just a very dumb class that prints out arbitrary escape codes that just happen to control a VT100 compliant terminal(emulator). It's pretty cool though. I made it because ncurses was just way too huge for the job I needed. All I need is a way to control cursor position and occasionally clear the screen.

Anyway, It's MIT licensed so use it, extend it, whatever.

Posted: 8/28/2012 4:46:28 AM

mbed :)

I got my mbed today. It actually is extremely simple. It took me a bit to figure out the options to give to mount so that I could write to the umass device without sudo. But that's not mbed's fault. Anyway, Very simple and I've just compiled the hello world program and it works. One note for anyone who has problems with their mbed and programming: When you download the file onto the device(on Arch Linux at least), it won't actually put the program on the device until some undetermined time. To force it to put it on there now, you'll have to run the sync command after downloading a program.

Posted: 3/26/2011 3:56:14 PM