Changed all Device mode LowLevel demos and Device Class drivers so that the control...
[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 #include "MIDI.h"
37
38 bool MIDI_Device_ConfigureEndpoints(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo)
39 {
40 memset(&MIDIInterfaceInfo->State, 0x00, sizeof(MIDIInterfaceInfo->State));
41
42 if (MIDIInterfaceInfo->Config.DataINEndpointNumber)
43 {
44 if (!(Endpoint_ConfigureEndpoint(MIDIInterfaceInfo->Config.DataINEndpointNumber, EP_TYPE_BULK,
45 ENDPOINT_DIR_IN, MIDIInterfaceInfo->Config.DataINEndpointSize,
46 MIDIInterfaceInfo->Config.DataINEndpointDoubleBank ? ENDPOINT_BANK_DOUBLE : ENDPOINT_BANK_SINGLE)))
47 {
48 return false;
49 }
50 }
51
52 if (MIDIInterfaceInfo->Config.DataOUTEndpointNumber)
53 {
54 if (!(Endpoint_ConfigureEndpoint(MIDIInterfaceInfo->Config.DataOUTEndpointNumber, EP_TYPE_BULK,
55 ENDPOINT_DIR_OUT, MIDIInterfaceInfo->Config.DataOUTEndpointSize,
56 MIDIInterfaceInfo->Config.DataOUTEndpointDoubleBank ? ENDPOINT_BANK_DOUBLE : ENDPOINT_BANK_SINGLE)))
57 {
58 return false;
59 }
60 }
61
62 return true;
63 }
64
65 uint8_t MIDI_Device_SendEventPacket(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo,
66 const MIDI_EventPacket_t* const Event)
67 {
68 if (USB_DeviceState != DEVICE_STATE_Configured)
69 return ENDPOINT_RWSTREAM_DeviceDisconnected;
70
71 Endpoint_SelectEndpoint(MIDIInterfaceInfo->Config.DataINEndpointNumber);
72
73 if (Endpoint_IsReadWriteAllowed())
74 {
75 uint8_t ErrorCode;
76
77 if ((ErrorCode = Endpoint_Write_Stream_LE(Event, sizeof(MIDI_EventPacket_t), NO_STREAM_CALLBACK)) != ENDPOINT_RWSTREAM_NoError)
78 return ErrorCode;
79
80 if (!(Endpoint_IsReadWriteAllowed()))
81 Endpoint_ClearIN();
82 }
83
84 return ENDPOINT_RWSTREAM_NoError;
85 }
86
87 uint8_t MIDI_Device_Flush(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo)
88 {
89 if (USB_DeviceState != DEVICE_STATE_Configured)
90 return ENDPOINT_RWSTREAM_DeviceDisconnected;
91
92 uint8_t ErrorCode;
93
94 Endpoint_SelectEndpoint(MIDIInterfaceInfo->Config.DataINEndpointNumber);
95
96 if (Endpoint_BytesInEndpoint())
97 {
98 Endpoint_ClearIN();
99
100 if ((ErrorCode = Endpoint_WaitUntilReady()) != ENDPOINT_READYWAIT_NoError)
101 return ErrorCode;
102 }
103
104 return ENDPOINT_READYWAIT_NoError;
105 }
106
107 bool MIDI_Device_ReceiveEventPacket(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo,
108 MIDI_EventPacket_t* const Event)
109 {
110 if (USB_DeviceState != DEVICE_STATE_Configured)
111 return false;
112
113 Endpoint_SelectEndpoint(MIDIInterfaceInfo->Config.DataOUTEndpointNumber);
114
115 if (!(Endpoint_IsReadWriteAllowed()))
116 return false;
117
118 Endpoint_Read_Stream_LE(Event, sizeof(MIDI_EventPacket_t), NO_STREAM_CALLBACK);
119
120 if (!(Endpoint_IsReadWriteAllowed()))
121 Endpoint_ClearOUT();
122
123 return true;
124 }
125
126 #endif