summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/lm32/shared/milkymist_midi/midi.c
blob: e71350bf1fc3cbac83d59b3a2d4f5c3145909e51 (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
/*  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.
 *
 *  $Id$
 *
 *  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 rtems_isr interrupt_handler(rtems_vector_number n)
{
  unsigned char msg;

  lm32_interrupt_ack(1 << MM_IRQ_MIDIRX);
  msg = MM_READ(MM_MIDI_RXTX);
  rtems_message_queue_send(midi_q, &msg, 1);
}

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'),
    64,
    1,
    0,
    &midi_q
  );
  RTEMS_CHECK_SC(sc, "create MIDI queue");

  rtems_interrupt_catch(interrupt_handler, MM_IRQ_MIDIRX, &dummy);
  bsp_interrupt_vector_enable(MM_IRQ_MIDIRX);

  /* Only MIDI THRU mode is supported atm */
  MM_WRITE(MM_MIDI_THRU, 1);

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