Statistics
| Branch: | Tag: | Revision:

root / LUFA / Drivers / USB / Class / Device / MIDI.c @ 978b99e5

History | View | Annotate | Download (4.39 KB)

1
/*
2
             LUFA Library
3
     Copyright (C) Dean Camera, 2011.
4

5
  dean [at] fourwalledcubicle [dot] com
6
           www.lufa-lib.org
7
*/
8

    
9
/*
10
  Copyright 2011  Dean Camera (dean [at] fourwalledcubicle [dot] com)
11

12
  Permission to use, copy, modify, distribute, and sell this
13
  software and its documentation for any purpose is hereby granted
14
  without fee, provided that the above copyright notice appear in
15
  all copies and that both that the copyright notice and this
16
  permission notice and warranty disclaimer appear in supporting
17
  documentation, and that the name of the author not be used in
18
  advertising or publicity pertaining to distribution of the
19
  software without specific, written prior permission.
20

21
  The author disclaim all warranties with regard to this
22
  software, including all implied warranties of merchantability
23
  and fitness.  In no event shall the author be liable for any
24
  special, indirect or consequential damages or any damages
25
  whatsoever resulting from loss of use, data or profits, whether
26
  in an action of contract, negligence or other tortious action,
27
  arising out of or in connection with the use or performance of
28
  this software.
29
*/
30

    
31
#define  __INCLUDE_FROM_USB_DRIVER
32
#include "../../Core/USBMode.h"
33

    
34
#if defined(USB_CAN_BE_DEVICE)
35

    
36
#define  __INCLUDE_FROM_MIDI_DRIVER
37
#define  __INCLUDE_FROM_MIDI_DEVICE_C
38
#include "MIDI.h"
39

    
40
bool MIDI_Device_ConfigureEndpoints(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo)
41
{
42
        memset(&MIDIInterfaceInfo->State, 0x00, sizeof(MIDIInterfaceInfo->State));
43

    
44
        for (uint8_t EndpointNum = 1; EndpointNum < ENDPOINT_TOTAL_ENDPOINTS; EndpointNum++)
45
        {
46
                uint16_t Size;
47
                uint8_t  Type;
48
                uint8_t  Direction;
49
                bool     DoubleBanked;
50

    
51
                if (EndpointNum == MIDIInterfaceInfo->Config.DataINEndpointNumber)
52
                {
53
                        Size         = MIDIInterfaceInfo->Config.DataINEndpointSize;
54
                        Direction    = ENDPOINT_DIR_IN;
55
                        Type         = EP_TYPE_BULK;
56
                        DoubleBanked = MIDIInterfaceInfo->Config.DataINEndpointDoubleBank;
57
                }
58
                else if (EndpointNum == MIDIInterfaceInfo->Config.DataOUTEndpointNumber)
59
                {
60
                        Size         = MIDIInterfaceInfo->Config.DataOUTEndpointSize;
61
                        Direction    = ENDPOINT_DIR_OUT;
62
                        Type         = EP_TYPE_BULK;
63
                        DoubleBanked = MIDIInterfaceInfo->Config.DataOUTEndpointDoubleBank;
64
                }
65
                else
66
                {
67
                        continue;
68
                }
69

    
70
                if (!(Endpoint_ConfigureEndpoint(EndpointNum, Type, Direction, Size,
71
                                                 DoubleBanked ? ENDPOINT_BANK_DOUBLE : ENDPOINT_BANK_SINGLE)))
72
                {
73
                        return false;
74
                }
75
        }
76

    
77
        return true;
78
}
79

    
80
void MIDI_Device_USBTask(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo)
81
{
82
        if (USB_DeviceState != DEVICE_STATE_Configured)
83
          return;
84

    
85
        #if !defined(NO_CLASS_DRIVER_AUTOFLUSH)
86
        MIDI_Device_Flush(MIDIInterfaceInfo);
87
        #endif
88
}
89

    
90
uint8_t MIDI_Device_SendEventPacket(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo,
91
                                    const MIDI_EventPacket_t* const Event)
92
{
93
        if (USB_DeviceState != DEVICE_STATE_Configured)
94
          return ENDPOINT_RWSTREAM_DeviceDisconnected;
95

    
96
        uint8_t ErrorCode;
97

    
98
        Endpoint_SelectEndpoint(MIDIInterfaceInfo->Config.DataINEndpointNumber);
99

    
100
        if ((ErrorCode = Endpoint_Write_Stream_LE(Event, sizeof(MIDI_EventPacket_t), NULL)) != ENDPOINT_RWSTREAM_NoError)
101
          return ErrorCode;
102

    
103
        if (!(Endpoint_IsReadWriteAllowed()))
104
          Endpoint_ClearIN();
105

    
106
        return ENDPOINT_RWSTREAM_NoError;
107
}
108

    
109
uint8_t MIDI_Device_Flush(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo)
110
{
111
        if (USB_DeviceState != DEVICE_STATE_Configured)
112
          return ENDPOINT_RWSTREAM_DeviceDisconnected;
113

    
114
        uint8_t ErrorCode;
115

    
116
        Endpoint_SelectEndpoint(MIDIInterfaceInfo->Config.DataINEndpointNumber);
117

    
118
        if (Endpoint_BytesInEndpoint())
119
        {
120
                Endpoint_ClearIN();
121

    
122
                if ((ErrorCode = Endpoint_WaitUntilReady()) != ENDPOINT_READYWAIT_NoError)
123
                  return ErrorCode;
124
        }
125

    
126
        return ENDPOINT_READYWAIT_NoError;
127
}
128

    
129
bool MIDI_Device_ReceiveEventPacket(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo,
130
                                    MIDI_EventPacket_t* const Event)
131
{
132
        if (USB_DeviceState != DEVICE_STATE_Configured)
133
          return false;
134

    
135
        Endpoint_SelectEndpoint(MIDIInterfaceInfo->Config.DataOUTEndpointNumber);
136

    
137
        if (!(Endpoint_IsReadWriteAllowed()))
138
          return false;
139

    
140
        Endpoint_Read_Stream_LE(Event, sizeof(MIDI_EventPacket_t), NULL);
141

    
142
        if (!(Endpoint_IsReadWriteAllowed()))
143
          Endpoint_ClearOUT();
144

    
145
        return true;
146
}
147

    
148
#endif
149