[PATCH 1/2] RTEMS: Handle MIDI framing in the interrupt handler

Werner Almesberger werner at almesberger.net
Fri Dec 2 15:40:11 EST 2011


This patch moves MIDI message detection from higher layers to the MIDI
interrupt handler. This way, MIDI receivers only need to wake up once
per complete message and not for every single byte.

The /dev/midi queue size is changed from 64 bytes to 32 messages
(typically 3 bytes each).

Index: rtems/c/src/lib/libbsp/lm32/shared/milkymist_midi/midi.c
===================================================================
--- rtems.orig/c/src/lib/libbsp/lm32/shared/milkymist_midi/midi.c	2011-12-01 19:45:37.000000000 -0300
+++ rtems/c/src/lib/libbsp/lm32/shared/milkymist_midi/midi.c	2011-12-02 16:05:30.000000000 -0300
@@ -26,6 +26,8 @@
 #define DEVICE_NAME "/dev/midi"
 
 static rtems_id midi_q;
+static unsigned char *midi_p = NULL;
+static unsigned char midi_msg[3];
 
 static rtems_isr interrupt_handler(rtems_vector_number n)
 {
@@ -34,7 +36,23 @@
   while (MM_READ(MM_MIDI_STAT) & MIDI_STAT_RX_EVT) {
     msg = MM_READ(MM_MIDI_RXTX);
     MM_WRITE(MM_MIDI_STAT, MIDI_STAT_RX_EVT);
-    rtems_message_queue_send(midi_q, &msg, 1);
+
+    if ((msg & 0xf8) == 0xf8)
+      continue; /* ignore system real-time */
+
+    if (msg & 0x80)
+      midi_p = midi_msg; /* status byte */
+
+    if (!midi_p)
+      continue; /* ignore extra or unsynchronized data */
+
+    *midi_p++ = msg;
+
+    if (midi_p == midi_msg+3) {
+      /* received a complete MIDI message */
+      rtems_message_queue_send(midi_q, midi_msg, 3);
+      midi_p = NULL;
+    }
   }
   lm32_interrupt_ack(1 << MM_IRQ_MIDI);
 }
@@ -53,8 +71,8 @@
 
  sc = rtems_message_queue_create(
     rtems_build_name('M', 'I', 'D', 'I'),
-    64,
-    1,
+    32,
+    3,
     0,
     &midi_q
   );




More information about the discussion mailing list


interactive