Friday, July 31, 2009

MIDI7S - a flexible USB - MIDI interface

MIDI7S - a flexible USB - MIDI interface


This page is work in progress and will be updated with more precise documentation when that is available.

Update june 2013

All the images had vanished from the blog, thanks blogger, but now I have restored them.   My work with midi has mostly moved on to use Maple boards and STM4FDiscovery board. The Teensy 3 board have midi implemented as standard and uses Arduino IDE for coding and programming making things much simpler.

System Components


Processor board


The AT91SAM7S64 processor has 3 uarts that can be used as MIDI ports and a USB interface. The Olimex SAM7-P64 board also has header for most uP pins, power supply and mmc card connector. This makes it a cheap and powerful platform for a USB MIDI system.



This processor is a bit old, but it is still a nice processor and I had this board and did not use for anything else. Today I would probably use something like a Olimex STM32-P103 board or design my own board for a STM32F103 processor.

MIDI interface board

MIDI out is driven directly from the microprocessor uart tx pin. Only one 220Ohm resistor is used , there should be two according to reference implementations. When driving MIDI from a 3.3 V source 2x70 Ohm is probably the best, for a typical VF in the optocoupler of 1.5V and series resistance of 220 Ohms in the receiver this gives I=(3.3-1.5)/360 = 5mA. The short curcuit current, 47 mA is a little bit high but not exceptional. The MIDI receivers uses 6N137 optoisolators

MIDI 1 IN / OUT

MIDI 2 IN / OUT

Schematic (one channel):


Board layout (one channel)


The actual construction was built on a strip-board

DOG-ME Display board

This a 2x20 character LCD display using serial communications


Communication Interfaces

USBMIDI

The USBMIDI configuration uses two logical USB MIDI Cables connected to the physical MIDI ports
/* MIDI IN JACK 1 receives data from the USB interface Ep 1 */
/* MIDI IN JACK 2 receives data from the serial uartmidi[0] */
/* MIDI OUT JACK 3 transmits data to the USB interface Ep 82 */
/* MIDI OUT JACK 4 transmits data to the serial uartmidi[0] */
/* MIDI OUT JACK 8 transmits data to the serial uartmidi[1] */

The sending of USB MIDI data through MIDI OUT JACK 3 using USB IN endpoint Ep82 needs work for buffering and handling of many small write requests

Serial MIDI

The serial MIDI interfaces are connected to USART0 and DEBUGUSART. USART1 is connected to a RS232 level shifter an can be used for "standard" serial communications.


Dog-ME Display interface (SPI 3.3V)

PA13 MOSI
PA14 SPCK
PA30 IRQ1/NPCS2 DOG_LCD_SELECT_PIN
PA31 NPCS1/PCK2 DOG_LCD_RS_PIN

Code

The code is written in C++ using a minimal profile for small embedded systems, no dynamic object creation, no exceptions.


USB MIDI device descriptor

http://www.usb.org/developers/devclass_docs/midi10.pdf

USB MIDI device descriptors as C code, corresponding header file

Transmission buffers

All uart communications uses buffered DMA transactions

spi to the display should also use DMA but with a DLYBCT of 50 giving a character spacing of approximatly 33us, the serial:: transmission logic can be reused (subclassed) here.

The AT91SAM7 variable chipselects and chipselect decode mode together with 32 bit DMA SPI transmissions allows us to control both data bits and the RS line with automatic transfers to the Dog-Me display.

Testing

The system has been used as an USB to MIDI inteface betwen a Linux computer and a U220 synth module. The usb midi ports are autodetected and added as Alsa MIDI ports. Some USB communication gliches has been observed, but not frequent and not yet reproducible. Loopback tests with a standard MIDI cable connected from MIDI out to MIDI in works well.

Round trip times on PC running Linux, Fedora 10, PC(Alsa MIDI)->USB MIDI IN->MIDI OUT->MIDI IN->USB MIDI OUT->PC are 1.5 - 3 ms (/home/lundin/delad/arbete/mikrop/usbmidi/miditest.c) with processing times up to 130uS using single message pings (090531 r357)
Direct echo within the module , no serial MIDI, gives times of .8 to 2 ms



Future


With the basic USB and MIDI communications working, it is possible to expand the capabilities of the system.
The analog and digital inputs can be used to generate MIDI events from different sensors and there is spare processor capacity for MIDI processing.

TWI EEPROM For settings

Control panel

I/O Extender

MIDI file player

Sensor to midi event generation

Friday, August 8, 2008

Using Python and XML for parsing USB descriptor structures to C

This project consists of a Python script that parses an XML description of USB descriptors and generates C code structures that can be included in a C firmware program for a USB device. This tool is primarily intended for developers of USB devices on embedded platforms. The XML representation of the descriptors gives a good view of the structure and the Python parser generates correct bDescriptorType values, bLength values and calculates total lengths. For elements with a variable number of child elements, such as bNumEndpoints in the Interface descriptor, the correct value is inferred from the XML structure. Most elements have a default value that is inserted in the generated C data structure. Symbolic values can be used for field values through XML entities defined in a DTD.

Related work

This project is part of a set of tools written in Python to simplify work with embedded development and USB. Most of the development work is done under Linux. Other components in this effort are:
  • usbmodule: A Python module that wraps libusb and gives raw access to USB devices, with experimental thread code to download and buffer incoming data before the application requests it, this works (for me) but it is unstructured, uncleaned and probable a bit buggy. This is similar to PyUSB http://pyusb.berlios.de/ , I have not had time to really check out this project, perhaps we should merge ....
  • A python GUI application for firmware downloads to embedded systems, first written to program Parallax Javelin systems from Linux, then extended to talk to serial monitors on LPC2xxx and AT91SAM7 arm processors. Today it is mostly used for SPI flashing of Atmel AVR8 processors using an FT232 interface in bitbang mode (this is where the USB from Python is used)

Example

An example XML representation of a device dsecriptor
<USB_Device_Descriptor name="devDescriptor" bcdUSB="0110" bDeviceClass="0" bDeviceSubclass="0" bDeviceProtocol="0" idVendor="EB03" idProduct="2463" bcdDevice="0000" iManufacturer="1" iProduct="2" iSerialNumber="3" bNumConfigs="1"/>
and the output in C code format that is generated
const char devDescriptor [] = {
// Device Descriptor
/* 0 */ 0x12, //bLength
/* 1 */ 0x1, //bDescriptorType
/* 2 */ 0x10,
//bcdUSB
/* 2 */ 0x1,
/* 4 */ 0x0,
//bDeviceClass
/* 5 */ 0x0,
//bDeviceSubclass
/* 6 */ 0x0, //bDeviceProtocol
/* 7 */ 0x8,
//bMaxPacketSize0
/* 8 */ 0x3,
//idVendor
/* 8 */ 0xeb,
/* 10 */ 0x63,
//idProduct
/* 10 */ 0x24,
/* 12 */ 0x0,
//bcdDevice
/* 12 */ 0x0,
/* 14 */ 0x1,
//iManufacturer
/* 15 */ 0x2, //iProduct
/* 16 */ 0x3,
//iSerialNumber
/* 17 */ 0x1
//bNumConfigs
};

Status

Currently the tool can generate what looks as correct C code structures, with examples from USB specification. The output has not been tested and used as descriptor structures in actual USB firmware. The Python code can be run as a simple command line tool. There is a lack of documentation for the XML tags and attributes used. Future work devoted to this will depend on the testing and also interest from third parties-

Future work

Use the output from this tool as descriptors for a MIDI interface implemented on an AT91SAM7S development board. Use the output from this tool as descriptors for a HID device implemented on an AT91SAM7S development board connected to . This document/page will also be updated and made more complete depending on external feedback.

Contact

Magnus Lundin If you are interested in this project contact me at: lundin at mlu dot mine dot nu

Welcome

Hi there, and welcome to all who are interested in Art and Microcomputers

And in the power to create your own designs.

A new blog !!!
I am not sure the world needs so many more blogs. But I will use this space to discuss some of my ideas on tools used in connection with art and microprocessors.

Hopefully I will also get some feedback from you out there, passionate users of microprocessors and connected hardware in artistic projects.

Some background:
I have a background as a programmer, mathematician and also as a dancer. I have done projects in this field since around 1991 when I worked on computer choreography tools with the Kacor group (Stockholm) . Since then I have done computer control for dance performance music, when that was still mostly done by swapping casette tapes and for the last ten years I have mostly worked with choreographer Åsa Unander-Scharin at Scen och Sinnesproduktion and produced some mechatronic installations with her, notably Orpheus and Petrushka .

This work has evolved from purely software to a very mixed mixed bag of software, hardware and firmware. During this process I have used and also constructed a wide array of tools. Some of this might be interesting to YOU ?

So, a new blog ... we will see

Yes I do have a "regular" job at the side to pay rent and other expenses.