Friday 29 July 2011

Using an Arduino as an USB UART

As you know I'm working on some modest electronic projects involving microcontrollers. And when you debug these little beasts, having a text output is no luxury. The best way to do this is by using a USB UART adapter. But when you don't have one, maybe you are still lucky and have an Arduino Duemilanove lying around. This Arduino uses an FTDI chip as a USB USART interface for the AVR chip. You can check the datasheet of the FTDI chip here (pdf), and the schematics of the Arduino here (pdf).

Sweet, all the parts are here, now let's put all this together.

First you need to program your Arduino to set the RX and TX pins to Input. This will set the logic gates of the pins to a open state that won't conflict with the signals comming from our own chip. To do this just create a new sketch in the Arduino development environment and copy this code:

void setup()
{
    // put RX and TX of the AVR to a rest
  pinMode(0, INPUT);
  pinMode(1, INPUT);
}
void loop() {
}

Upload this to the Arduino and run the Serial Monitor. Note you have to leave the RT/TX unplugged when programming the Arduino. You can now set the data transfer rate to an appropriate value. For my case I will use 115200 bauds.

Now we can hook our own microcontroller to the FTDI chip. Link the TX of your chip to the TX of the board (it actually corresponds to the RX of the FTDI chip). That's pretty much the only hardware manipulation necessary.

Last step you need to setup the USART of you microchip. Here is an example of how I set it for my project using a PIC18f. This example uses the sdcc iolib:
  usart_open(
             USART_TX_INT_OFF &
             USART_RX_INT_OFF &
             USART_BRGH_HIGH &
             USART_EIGHT_BIT &
             USART_ASYNCH_MODE,
             103);
  BAUDCONbits.BRG16 = 1;
  stdout = STREAM_USART;
  printf("Starting USB\n");

You will need to calculate the proper Baud Generator value for your project. Refer to the datasheet of your microcontroller for this. Here 103 corresponds to the following calculation taken from the PIC18F2450 data sheet (using 48Mhz clock, wants 115200 bauds, 4 is because using Aynch transfer with BRG16 and BRGH): ((48000000 / 115200) / 4) - 1.

Just compile that, upload to the boad and the debug message magicly apears on the Arduino console:
Starting USB

Bad kitty ! Bad !!

Can't resist the urge of sharing my frustrations with the new coming out of Mac OS X Lion. As a start, let me explain that I didn't upgrade to Snow Leopard when it came out. This version didn't bring groundbreaking changes (well, none I needed) and I kept working with regular Leopard and everything was nice. But as times goes, some few software begging to be available for Snow Leopard only, and Leopard get more and more on the verge of being deprecated. Nothing more natural than that. At this time Lion was already pointing the tip of its mustaches. So I thought "hey, let's just skip Snow Leopard completely to jump on Lion, just need to hold for a month or two with some old software". Then the first bad new came: it is not possible to install Lion without Snow Leopard. OK... fair enough, I'm still waiting, hoping Snow Leopard will get a prince-cut on Lion release. Wrong again! Lion is out, and Snow Leopard if still at 30 bucks. OK I get the message Apple, I'm going to buy your goddamn manslaughter feline (I already begin to feel the sharp teeths and claws digging though my skin). I went to the local Apple store, guess what? No more Show Leopard. I went to another local Apple dealers, same thing. OK... So I'm going to buy it online. Guess what? I ordered it on July 28th and it will shipped only in August 11th! Won't get it before the 15th. Almost three weeks! It better not hiss on me when I'll install it.

Wednesday 6 July 2011

Install Ruby with TK on Windows XP

Developping a small graphic cross-platform application with Ruby? Seems TCL/TK is the way to go as Ruby support for TK is part of the core development, and TCL/TK is available for most platforms (Windows/MAC/Linux). Even if everything for Ruby/TK is packaged by default on MAC OS X (and I guess Linux), it's not the same for Windows. The Windows RubyInstaller does not provide TK extentions anymore, and despite the numberous Howtos on the net, none really precises which version of each component you need. And the different versions require different installations. So here is a quick how to install Ruby with TK support on Windows XP.

Basically you have two options:
  • Using Ruby 1.8. In this case you need to install some existing ruby tk binaries.
  • Using Ruby 1.9. In this case you need the ruby tk gem to build the binding yourself. I didn't managed to get this option working, but I still explain here how the installation works.

Install TCL/TK

This step is common to both installations. Use ActiveTcl convenient installer you can find here: http://www.activestate.com/activetcl
For Ruby 1.8 you need version 8.4 instead of 8.5 (the latest at this time).

Install Ruby

Use the convenient RubyInstaller for Windows you can find here: http://www.ruby-lang.org/en/downloads/

Install TK binding for Ruby 1.8

Get the archive at https://github.com/rdp/ruby_windows_tk and decompress it on your drive. You just need to cd in this folder and run:

ruby install.rb
This binary installation is meant for Ruby 1.8.5, but it seems to work pretty well for Ruby 1.8.7 (latest at this moment).

Install TK binding for Ruby 1.9

In this case you will need to build the binding yourself, so first we have to install the RubyInstaller Development Kit. It contains the compiler dans files to comile gems and you can download it there:http://rubyinstaller.org/downloads/

Note to install the devkit you have to follow these few steps (taken from: https://github.com/oneclick/rubyinstaller/wiki/Development-Kit)
  • Run the bin and uncompress data in <DEVKIT_INSTALL_DIR>
  • Start Command Prompt With Ruby
  • cd <DEVKIT_INSTALL_DIR>
  • ruby dk.rb init
  • ruby dk.rb install
Now let's install the tk gem. As explained here https://github.com/rdp/tk_as_gem, Ruby TK gem is a little hack with the regular Ruby 1.9 sources for TK embedded in a gem. To build it use the following command line:
gem install tk_as_gem -- --with-tcl-dir=c:\Tcl --with-tk-dir=c:\Tcl
Testing Ruby TK

Now everything is in place, run the follogin code in the ruby console:

require 'tk'
root = TkRoot.new { title "Ex1" }
TkLabel.new(root) {   text  'Hello, World!'   pack  { padx 15 ; pady 15; side 'left' } }
Tk.mainloop

This should display a simple window with a label.