cancel
Showing results for 
Search instead for 
Did you mean: 

HAL version of USB ''Class Compliant'' MIDI, can it be done?

Christopher Pappas
Senior II
Posted on September 02, 2016 at 15:52

Hey all!

I am trying to see if I can eliminate the serial port version of MIDI and switch over to using USB MIDI controllers/keyboards instead.

Searching on this forum (and Google as well), I see many attempts at creating this, mostly poorly documented and or proprietary (i.e. - MIOS), and other open source code that doesn't seem to survive the transition from GCC to KEIL, and the rest of what is available is written for SPL only.

Is there an example of a USB MIDI ''Class-Compliant'' code that can be used on the USB-OTG port of STM32 Nucleo boards?

Can an existing piece of code (like the USB Flash Drive FatFS 3rd-party examples in the v1.4.0 firmware) be modified to connect to USB ''Class-Compliant'' devices such as controllers or keyboards?

Does it seem like a feasible task, or is it just too much work to get this to work on HAL?

Does anybody have suggestions or ideas or comments about this subject?

Thanks!!

Christopher

#usb-midi-class #usb-midi-host-nucleo-144 #class-compliant-hal-midi

Note: this post was migrated and contained many threaded conversations, some content may be missing.
1 ACCEPTED SOLUTION

Accepted Solutions
alibabashack
Associate
Posted on September 05, 2016 at 18:35

Hi Christopher,

it is absolutely doable to change a USB serial port device implementation (actually a so called CDC device - Communication Device Class) to a MIDI device compliant with the USB audio device class specification. I did this a few years ago for some ARM7 based atmel controller as a school project and created a DJ controller based on this. I first suggest you to forget about all the bad examples you've seen so far and dig into the USB specification available at http://www.usb.org/developers/docs/ (see USB device class specs -> audio). Learn how device descriptors work and transform the CDC's device descriptor into an audio class descriptor with a midi interface. I recommend to use a LINUX based PC to check the device descriptor using the lsusb command. It's a nightmare under Windows. When you got the descriptor working, implement the endpoints, which should be relatively simple since they are basically the same as in a CDC device. You just have to send/receive a fixed number of bytes per MIDI message.

I had a lot fun implementing this and it is an excellent learning opportunity.

One more tip: At least under Windows XP I had to implement a MIDI IN as well as a MIDI OUT port/endpoint, although i only planned to use a MIDI IN. Otherwise the device was not showing up as a MIDI device.

View solution in original post

21 REPLIES 21
alibabashack
Associate
Posted on September 05, 2016 at 18:35

Hi Christopher,

it is absolutely doable to change a USB serial port device implementation (actually a so called CDC device - Communication Device Class) to a MIDI device compliant with the USB audio device class specification. I did this a few years ago for some ARM7 based atmel controller as a school project and created a DJ controller based on this. I first suggest you to forget about all the bad examples you've seen so far and dig into the USB specification available at http://www.usb.org/developers/docs/ (see USB device class specs -> audio). Learn how device descriptors work and transform the CDC's device descriptor into an audio class descriptor with a midi interface. I recommend to use a LINUX based PC to check the device descriptor using the lsusb command. It's a nightmare under Windows. When you got the descriptor working, implement the endpoints, which should be relatively simple since they are basically the same as in a CDC device. You just have to send/receive a fixed number of bytes per MIDI message.

I had a lot fun implementing this and it is an excellent learning opportunity.

One more tip: At least under Windows XP I had to implement a MIDI IN as well as a MIDI OUT port/endpoint, although i only planned to use a MIDI IN. Otherwise the device was not showing up as a MIDI device.
Christopher Pappas
Senior II
Posted on September 07, 2016 at 05:47

Thanks Alexander, that is good advice. 

I fired up CubeMX, and banged out some quick USB Host AUDIO Class code, and hardwired it to print to the serial port. I ported the USBH_MIDI class files over to Keil, and swapped out the USBH_audio class files.

When I plug in a Casio CTK-3200 keyboard, it seems to be working properly in Debug Mode. As soon as the MIDI class is started, the Init routines run, the endpoints are linked to MIDI In and MIDI Out, and MIDI Process seems to be armed and ready to accept MIDI input from the keyboard.

However, when I play a note on the keyboard, I notice no data is being received. The MIDI core is idling, as if nothing is being output from the keyboard.

More reading and learning for me now!!

Thanks again!!

Christopher
Christopher Pappas
Senior II
Posted on September 18, 2016 at 20:12

Ok, I got everything working fine now.

Thanks again to everybody here who assisted me!!

Christopher

Alexander Alexandrov
Associate II
Posted on February 06, 2017 at 03:00

Hi Christopher! Can you tell us more about how you got working usb-midi on HAL?

Posted on February 06, 2017 at 04:34

I actually took the long route to implementing a USB Midi Host with MIDI In only.

I started by using CubeMX to creatinga USB Audio Host.

Then I removedthe files usbh_audio.h and usb_audio.c, and substituted usbh_midi.h and usbh_midi.c files.

I used the files from

https://github.com/MrBlueXav/Dekrispator_v2

, the incredible Dekrispator synthesizer project authored byXavier Halgand.

With some very helpful assistance from ST community member Chinzai Tsuneo (

https://community.st.com/0D50X00009XkYz2SAF

), I was able to get it up and running on my NUCLEO-F746 board.

An easier and faster way to get this working is just use Xaviers project directly and modify it to fit your needs.

Posted on February 06, 2017 at 05:34

Thank you very much for your answer! I'm sorry, I didn't notice that you are discussing the Host Class.

Maybe you have some idea of how to implement a MIDI Device Class that will be compatible with project in the CubeMX?
Posted on February 06, 2017 at 06:07

I have tried to get USB_MIDI Device Class to work (again by just substituting the 2 files I listed before with the device class equivalents usbd_midi.h and usbd_midi.c), but I have not had success at getting a functional solution.

Many people are suggesting

https://github.com/sebseb7/stm32-midi-demo

 and

https://github.com/guitarfriiik/stm32_usb_midi

 as places to begin, but I've noticed that they are written for SPL and not HAL.

Maybe they can be migrated to HAL? 

Posted on February 06, 2017 at 19:21

I searched on request 'usbd_midi.h' and get two results.

Then I created a new project in CubeMX with a CDC class device and replace usb_device, usbd_cdc_if to usbd_midi_if and usbd_cdc to usbd_midi from this

https://github.com/tadfmac/mi-muz/tree/master/applications/tuch/STM32/source/mimuz-tuch

. It seems that it works fine.

Now I'm looking for a way to add a second MIDI cable in this configuration?

Posted on February 06, 2017 at 22:10

Great work, Alexander!!  And nice find from the TripArts github repository. It's good to hear you are up and running now.

I gave it a try here too to see if it would work on a spare NUCLEO-F767 board I have here. It compiled okay with a few modifications to the files. It is showing up now as a USB Composite device on the USB Debugger app, and I can connect and communicate with it using MIDI-OX. At some point, I will merge this code with my VA PolySynth project to enable it to play midi files from a sequencer.