summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/lm32/shared/milkymist_midi/midi.c
blob: 76097eeb190f8e50274284a1f43916dad9deeb14 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*  midi.c
 *
 *  Milkymist MIDI driver for RTEMS
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.com/license/LICENSE.
 *
 *  COPYRIGHT (c) 2010, 2011 Sebastien Bourdeauducq
 */

#define RTEMS_STATUS_CHECKS_USE_PRINTK

#include <stdlib.h>
#include <sys/types.h>
#include <rtems.h>
#include <rtems/status-checks.h>
#include <bsp.h>
#include <bsp/irq-generic.h>
#include <rtems/libio.h>
#include "../include/system_conf.h"
#include "milkymist_midi.h"

#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)
{
  unsigned char msg;

  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);

    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);
}

rtems_device_driver midi_initialize(
  rtems_device_major_number major,
  rtems_device_minor_number minor,
  void *arg
)
{
  rtems_status_code sc;
  rtems_isr_entry dummy;

  sc = rtems_io_register_name(DEVICE_NAME, major, 0);
  RTEMS_CHECK_SC(sc, "create MIDI input device");

 sc = rtems_message_queue_create(
    rtems_build_name('M', 'I', 'D', 'I'),
    32,
    3,
    0,
    &midi_q
  );
  RTEMS_CHECK_SC(sc, "create MIDI queue");

  rtems_interrupt_catch(interrupt_handler, MM_IRQ_MIDI, &dummy);
  bsp_interrupt_vector_enable(MM_IRQ_MIDI);
  /* Only MIDI THRU mode is supported atm */
  MM_WRITE(MM_MIDI_CTRL, MIDI_CTRL_RX_INT|MIDI_CTRL_THRU);

  return RTEMS_SUCCESSFUL;
}

rtems_device_driver midi_open(
  rtems_device_major_number major,
  rtems_device_minor_number minor,
  void *arg
)
{
  uint32_t count;

  rtems_message_queue_flush(midi_q, &count);
  return RTEMS_SUCCESSFUL;
}

rtems_device_driver midi_read(
  rtems_device_major_number major,
  rtems_device_minor_number minor,
  void *arg
)
{
  rtems_libio_rw_args_t *rw_args = (rtems_libio_rw_args_t *)arg;
  rtems_status_code sc;

  sc = rtems_message_queue_receive(
    midi_q,
    rw_args->buffer,
    (size_t *)&rw_args->bytes_moved,
    RTEMS_WAIT,
    RTEMS_NO_TIMEOUT
  );

  if(sc == RTEMS_SUCCESSFUL)
    return RTEMS_SUCCESSFUL;
  else {
    rw_args->bytes_moved = 0;
    return RTEMS_UNSATISFIED;
  }
}