Added support to the MIDI Class drivers for packed data, where multiple MIDI events...
[pub/USBasp.git] / LUFA / Drivers / USB / Class / Device / MIDI.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2010 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 #include "../../HighLevel/USBMode.h"
32 #if defined(USB_CAN_BE_DEVICE)
33
34 #include "MIDI.h"
35
36 void MIDI_Device_ProcessControlRequest(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo)
37 {
38
39 }
40
41 bool MIDI_Device_ConfigureEndpoints(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo)
42 {
43 memset(&MIDIInterfaceInfo->State, 0x00, sizeof(MIDIInterfaceInfo->State));
44
45 if (MIDIInterfaceInfo->Config.DataINEndpointNumber)
46 {
47 if (!(Endpoint_ConfigureEndpoint(MIDIInterfaceInfo->Config.DataINEndpointNumber, EP_TYPE_BULK,
48 ENDPOINT_DIR_IN, MIDIInterfaceInfo->Config.DataINEndpointSize,
49 MIDIInterfaceInfo->Config.DataINEndpointDoubleBank ? ENDPOINT_BANK_DOUBLE : ENDPOINT_BANK_SINGLE)))
50 {
51 return false;
52 }
53 }
54
55 if (MIDIInterfaceInfo->Config.DataOUTEndpointNumber)
56 {
57 if (!(Endpoint_ConfigureEndpoint(MIDIInterfaceInfo->Config.DataOUTEndpointNumber, EP_TYPE_BULK,
58 ENDPOINT_DIR_OUT, MIDIInterfaceInfo->Config.DataOUTEndpointSize,
59 MIDIInterfaceInfo->Config.DataOUTEndpointDoubleBank ? ENDPOINT_BANK_DOUBLE : ENDPOINT_BANK_SINGLE)))
60 {
61 return false;
62 }
63 }
64
65 return true;
66 }
67
68 uint8_t MIDI_Device_SendEventPacket(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo, MIDI_EventPacket_t* const Event)
69 {
70 if (USB_DeviceState != DEVICE_STATE_Configured)
71 return ENDPOINT_RWSTREAM_DeviceDisconnected;
72
73 Endpoint_SelectEndpoint(MIDIInterfaceInfo->Config.DataINEndpointNumber);
74
75 if (Endpoint_IsReadWriteAllowed());
76 {
77 uint8_t ErrorCode;
78
79 if ((ErrorCode = Endpoint_Write_Stream_LE(Event, sizeof(MIDI_EventPacket_t), NO_STREAM_CALLBACK)) != ENDPOINT_RWSTREAM_NoError)
80 return ErrorCode;
81
82 if (!(Endpoint_IsReadWriteAllowed()))
83 Endpoint_ClearIN();
84 }
85
86 return ENDPOINT_RWSTREAM_NoError;
87 }
88
89 uint8_t MIDI_Device_Flush(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo)
90 {
91 if (USB_DeviceState != DEVICE_STATE_Configured)
92 return ENDPOINT_RWSTREAM_DeviceDisconnected;
93
94 uint8_t ErrorCode;
95
96 Endpoint_SelectEndpoint(MIDIInterfaceInfo->Config.DataINEndpointNumber);
97
98 if (Endpoint_BytesInEndpoint())
99 {
100 Endpoint_ClearIN();
101
102 if ((ErrorCode = Endpoint_WaitUntilReady()) != ENDPOINT_READYWAIT_NoError)
103 return ErrorCode;
104 }
105
106 return ENDPOINT_READYWAIT_NoError;
107 }
108
109 bool MIDI_Device_ReceiveEventPacket(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo, MIDI_EventPacket_t* const Event)
110 {
111 if (USB_DeviceState != DEVICE_STATE_Configured)
112 return false;
113
114 Endpoint_SelectEndpoint(MIDIInterfaceInfo->Config.DataOUTEndpointNumber);
115
116 if (!(Endpoint_IsReadWriteAllowed()))
117 return false;
118
119 Endpoint_Read_Stream_LE(Event, sizeof(MIDI_EventPacket_t), NO_STREAM_CALLBACK);
120
121 if (!(Endpoint_IsReadWriteAllowed()))
122 Endpoint_ClearOUT();
123
124 return true;
125 }
126
127 #endif