Subject: Re: MIDI pins used as GPIO?
To: Emmanuel Dreyfus <manu@netbsd.org>
From: Magnus Eriksson <magetoo@fastmail.fm>
List: netbsd-users
Date: 06/04/2006 20:08:49
On Sun, 4 Jun 2006, Emmanuel Dreyfus wrote:

   This turned out kind of long and rambling, but perhaps it can help 
anyway.  Sorry.


> Is there any way the MIDI pins of a soundcard game port can be used as
> general purpose input/output? I'd like to set the MIDI out pin as 0 or +5V
> from userland, is it possible, or does that require some kernel hacking?

  *Any* way?  Sure, anything is possible.  People have been using serial 
ports to play audio, after all.

   But you couldn't have picked a worse way of interfacing to the outside 
world (assuming you actually have a choice).  The serial, I mean, "COM" 
ports at least have a couple of pins you can directly control.


  (But for general purpose *input*, on the other hand, the game port might 
actually be a good choice.  The fire buttons are just on/off switches, and 
they can be read directly.)


> A quick try at writing to /dev/rmidi0 seems to produce no result, but maybe
> I'm missing something?

   I've never tried it, but the man page seems to say it really is that 
simple, so you should at least get something.

   The problem is, it's a serial interface.  (just skip ahead if this is 
too obvious)  Just like the "regular" serial ports, anything you write 
will appear as a short burst of serial data.  Anything you write will be 
gone in a matter of milliseconds.

   The major difference to normal serial ports is that MIDI operates at a 
fixed baud rate, 31 250 bits per second, instead of being settable to lots 
of different speeds.  (31250, by the way, is one million divided by 32.) 
The voltage levels are TTL (0 or 5 volts), which could be an advantage for 
general purpose I/O compared to the COM ports.  (which use +12 and -12 
volts -- and +12 is logical "0")


   So you would have to be constantly writing to that port in order to 
accomplish at least "constant noise", which is certainly possible, but not 
very convenient.  Then you'd have to filter that noise somehow, in order 
to get a reasonably constant output.  That's certainly possible too, but 
unless you already know a bit of electronics, it will take some thinking.


   Alternatively, you could have a small microcontroller hooked up to the 
MIDI pins, and let it respond to various control bytes by setting output 
pins high or low.  But that's going to be inconvenient too, unless you 
already have that sort of thing lying around..


   There are some advantages too, though.  The game port provides +5V and 
ground pins, something that neither the COM or parallel ports do (without 
horrible hacks).  USB has that too now, of course.


   I would suggest you use a normal serial port, and use the DTR line for 
example, something I believe takes only an ioctl() to set or clear.  You'd 
need to convert the (nominally) +-12V level to something you can use, but 
that's not too hard, and at least it's conceptually simple.

   If that's not possible, the parallel port should be relatively easy to 
use too.  I have no idea how easy/hard that is to use, software-wise, 
though.


   If you really, *really* need to use the game port, I guess you need to 
read a lot of man pages.  :-)

   For the hardware part, you could do worse than to start your research at 
ePanorama:

http://www.epanorama.net/documents/joystick/pc_joystick.html


  (And if you absolutely have to use the MIDI pins, you're screwed.)  :-)


MAgnus