f7c59b51ec0e971b5cc1c5419c37004466b805f3
[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 #define __INCLUDE_FROM_USB_DRIVER
32 #include "../../HighLevel/USBMode.h"
33 #if defined(USB_CAN_BE_DEVICE)
34
35 #define __INCLUDE_FROM_MIDI_DRIVER
36 #define __INCLUDE_FROM_MIDI_DEVICE_C
37 #include "MIDI.h"
38
39 bool MIDI_Device_ConfigureEndpoints(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo)
40 {
41 memset(&MIDIInterfaceInfo->State, 0x00, sizeof(MIDIInterfaceInfo->State));
42
43 for (uint8_t EndpointNum = 1; EndpointNum < ENDPOINT_TOTAL_ENDPOINTS; EndpointNum++)
44 {
45 uint16_t Size;
46 uint8_t Type;
47 uint8_t Direction;
48 bool DoubleBanked;
49
50 if (EndpointNum == MIDIInterfaceInfo->Config.DataINEndpointNumber)
51 {
52 Size = MIDIInterfaceInfo->Config.DataINEndpointSize;
53 Direction = ENDPOINT_DIR_IN;
54 Type = EP_TYPE_BULK;
55 DoubleBanked = MIDIInterfaceInfo->Config.DataINEndpointDoubleBank;
56 }
57 else if (EndpointNum == MIDIInterfaceInfo->Config.DataOUTEndpointNumber)
58 {
59 Size = MIDIInterfaceInfo->Config.DataOUTEndpointSize;
60 Direction = ENDPOINT_DIR_OUT;
61 Type = EP_TYPE_BULK;
62 DoubleBanked = MIDIInterfaceInfo->Config.DataOUTEndpointDoubleBank;
63 }
64 else
65 {
66 continue;
67 }
68
69 if (!(Endpoint_ConfigureEndpoint(EndpointNum, Type, Direction, Size,
70 DoubleBanked ? ENDPOINT_BANK_DOUBLE : ENDPOINT_BANK_SINGLE)))
71 {
72 return false;
73 }
74 }
75
76 return true;
77 }
78
79 uint8_t MIDI_Device_SendEventPacket(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo,
80 const MIDI_EventPacket_t* const Event)
81 {
82 if (USB_DeviceState != DEVICE_STATE_Configured)
83 return ENDPOINT_RWSTREAM_DeviceDisconnected;
84
85 Endpoint_SelectEndpoint(MIDIInterfaceInfo->Config.DataINEndpointNumber);
86
87 if (Endpoint_IsReadWriteAllowed())
88 {
89 uint8_t ErrorCode;
90
91 if ((ErrorCode = Endpoint_Write_Stream_LE(Event, sizeof(MIDI_EventPacket_t), NO_STREAM_CALLBACK)) != ENDPOINT_RWSTREAM_NoError)
92 return ErrorCode;
93
94 if (!(Endpoint_IsReadWriteAllowed()))
95 Endpoint_ClearIN();
96 }
97
98 return ENDPOINT_RWSTREAM_NoError;
99 }
100
101 uint8_t MIDI_Device_Flush(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo)
102 {
103 if (USB_DeviceState != DEVICE_STATE_Configured)
104 return ENDPOINT_RWSTREAM_DeviceDisconnected;
105
106 uint8_t ErrorCode;
107
108 Endpoint_SelectEndpoint(MIDIInterfaceInfo->Config.DataINEndpointNumber);
109
110 if (Endpoint_BytesInEndpoint())
111 {
112 Endpoint_ClearIN();
113
114 if ((ErrorCode = Endpoint_WaitUntilReady()) != ENDPOINT_READYWAIT_NoError)
115 return ErrorCode;
116 }
117
118 return ENDPOINT_READYWAIT_NoError;
119 }
120
121 bool MIDI_Device_ReceiveEventPacket(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo,
122 MIDI_EventPacket_t* const Event)
123 {
124 if (USB_DeviceState != DEVICE_STATE_Configured)
125 return false;
126
127 Endpoint_SelectEndpoint(MIDIInterfaceInfo->Config.DataOUTEndpointNumber);
128
129 if (!(Endpoint_IsReadWriteAllowed()))
130 return false;
131
132 Endpoint_Read_Stream_LE(Event, sizeof(MIDI_EventPacket_t), NO_STREAM_CALLBACK);
133
134 if (!(Endpoint_IsReadWriteAllowed()))
135 Endpoint_ClearOUT();
136
137 return true;
138 }
139
140 #endif
141