1 | /* midi.c |
---|
2 | * |
---|
3 | * Milkymist MIDI driver for RTEMS |
---|
4 | * |
---|
5 | * The license and distribution terms for this file may be |
---|
6 | * found in the file LICENSE in this distribution or at |
---|
7 | * http://www.rtems.com/license/LICENSE. |
---|
8 | * |
---|
9 | * $Id$ |
---|
10 | * |
---|
11 | * COPYRIGHT (c) 2010, 2011 Sebastien Bourdeauducq |
---|
12 | */ |
---|
13 | |
---|
14 | #define RTEMS_STATUS_CHECKS_USE_PRINTK |
---|
15 | |
---|
16 | #include <stdlib.h> |
---|
17 | #include <sys/types.h> |
---|
18 | #include <rtems.h> |
---|
19 | #include <rtems/status-checks.h> |
---|
20 | #include <bsp.h> |
---|
21 | #include <bsp/irq-generic.h> |
---|
22 | #include <rtems/libio.h> |
---|
23 | #include "../include/system_conf.h" |
---|
24 | #include "milkymist_midi.h" |
---|
25 | |
---|
26 | #define DEVICE_NAME "/dev/midi" |
---|
27 | |
---|
28 | static rtems_id midi_q; |
---|
29 | |
---|
30 | static rtems_isr interrupt_handler(rtems_vector_number n) |
---|
31 | { |
---|
32 | unsigned char msg; |
---|
33 | |
---|
34 | lm32_interrupt_ack(1 << MM_IRQ_MIDIRX); |
---|
35 | msg = MM_READ(MM_MIDI_RXTX); |
---|
36 | rtems_message_queue_send(midi_q, &msg, 1); |
---|
37 | } |
---|
38 | |
---|
39 | rtems_device_driver midi_initialize( |
---|
40 | rtems_device_major_number major, |
---|
41 | rtems_device_minor_number minor, |
---|
42 | void *arg |
---|
43 | ) |
---|
44 | { |
---|
45 | rtems_status_code sc; |
---|
46 | rtems_isr_entry dummy; |
---|
47 | |
---|
48 | sc = rtems_io_register_name(DEVICE_NAME, major, 0); |
---|
49 | RTEMS_CHECK_SC(sc, "create MIDI input device"); |
---|
50 | |
---|
51 | sc = rtems_message_queue_create( |
---|
52 | rtems_build_name('M', 'I', 'D', 'I'), |
---|
53 | 64, |
---|
54 | 1, |
---|
55 | 0, |
---|
56 | &midi_q |
---|
57 | ); |
---|
58 | RTEMS_CHECK_SC(sc, "create MIDI queue"); |
---|
59 | |
---|
60 | rtems_interrupt_catch(interrupt_handler, MM_IRQ_MIDIRX, &dummy); |
---|
61 | bsp_interrupt_vector_enable(MM_IRQ_MIDIRX); |
---|
62 | |
---|
63 | /* Only MIDI THRU mode is supported atm */ |
---|
64 | MM_WRITE(MM_MIDI_THRU, 1); |
---|
65 | |
---|
66 | return RTEMS_SUCCESSFUL; |
---|
67 | } |
---|
68 | |
---|
69 | rtems_device_driver midi_open( |
---|
70 | rtems_device_major_number major, |
---|
71 | rtems_device_minor_number minor, |
---|
72 | void *arg |
---|
73 | ) |
---|
74 | { |
---|
75 | uint32_t count; |
---|
76 | |
---|
77 | rtems_message_queue_flush(midi_q, &count); |
---|
78 | return RTEMS_SUCCESSFUL; |
---|
79 | } |
---|
80 | |
---|
81 | rtems_device_driver midi_read( |
---|
82 | rtems_device_major_number major, |
---|
83 | rtems_device_minor_number minor, |
---|
84 | void *arg |
---|
85 | ) |
---|
86 | { |
---|
87 | rtems_libio_rw_args_t *rw_args = (rtems_libio_rw_args_t *)arg; |
---|
88 | rtems_status_code sc; |
---|
89 | |
---|
90 | sc = rtems_message_queue_receive( |
---|
91 | midi_q, |
---|
92 | rw_args->buffer, |
---|
93 | (size_t *)&rw_args->bytes_moved, |
---|
94 | RTEMS_WAIT, |
---|
95 | RTEMS_NO_TIMEOUT |
---|
96 | ); |
---|
97 | |
---|
98 | if(sc == RTEMS_SUCCESSFUL) |
---|
99 | return RTEMS_SUCCESSFUL; |
---|
100 | else { |
---|
101 | rw_args->bytes_moved = 0; |
---|
102 | return RTEMS_UNSATISFIED; |
---|
103 | } |
---|
104 | } |
---|