3 Copyright (C) Dean Camera, 2011.
5 dean [at] fourwalledcubicle [dot] com
10 Copyright 2011 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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
31 #define __INCLUDE_FROM_USB_DRIVER
32 #include "../../HighLevel/USBMode.h"
33 #if defined(USB_CAN_BE_DEVICE)
35 #define __INCLUDE_FROM_MIDI_DRIVER
36 #define __INCLUDE_FROM_MIDI_DEVICE_C
39 bool MIDI_Device_ConfigureEndpoints(USB_ClassInfo_MIDI_Device_t
* const MIDIInterfaceInfo
)
41 memset(&MIDIInterfaceInfo
->State
, 0x00, sizeof(MIDIInterfaceInfo
->State
));
43 for (uint8_t EndpointNum
= 1; EndpointNum
< ENDPOINT_TOTAL_ENDPOINTS
; EndpointNum
++)
50 if (EndpointNum
== MIDIInterfaceInfo
->Config
.DataINEndpointNumber
)
52 Size
= MIDIInterfaceInfo
->Config
.DataINEndpointSize
;
53 Direction
= ENDPOINT_DIR_IN
;
55 DoubleBanked
= MIDIInterfaceInfo
->Config
.DataINEndpointDoubleBank
;
57 else if (EndpointNum
== MIDIInterfaceInfo
->Config
.DataOUTEndpointNumber
)
59 Size
= MIDIInterfaceInfo
->Config
.DataOUTEndpointSize
;
60 Direction
= ENDPOINT_DIR_OUT
;
62 DoubleBanked
= MIDIInterfaceInfo
->Config
.DataOUTEndpointDoubleBank
;
69 if (!(Endpoint_ConfigureEndpoint(EndpointNum
, Type
, Direction
, Size
,
70 DoubleBanked ? ENDPOINT_BANK_DOUBLE
: ENDPOINT_BANK_SINGLE
)))
79 void MIDI_Device_USBTask(USB_ClassInfo_MIDI_Device_t
* const MIDIInterfaceInfo
)
81 if (USB_DeviceState
!= DEVICE_STATE_Configured
)
84 #if !defined(NO_CLASS_DRIVER_AUTOFLUSH)
85 MIDI_Device_Flush(MIDIInterfaceInfo
);
89 uint8_t MIDI_Device_SendEventPacket(USB_ClassInfo_MIDI_Device_t
* const MIDIInterfaceInfo
,
90 const MIDI_EventPacket_t
* const Event
)
92 if (USB_DeviceState
!= DEVICE_STATE_Configured
)
93 return ENDPOINT_RWSTREAM_DeviceDisconnected
;
97 Endpoint_SelectEndpoint(MIDIInterfaceInfo
->Config
.DataINEndpointNumber
);
99 if ((ErrorCode
= Endpoint_Write_Stream_LE(Event
, sizeof(MIDI_EventPacket_t
), NO_STREAM_CALLBACK
)) != ENDPOINT_RWSTREAM_NoError
)
102 if (!(Endpoint_IsReadWriteAllowed()))
105 return ENDPOINT_RWSTREAM_NoError
;
108 uint8_t MIDI_Device_Flush(USB_ClassInfo_MIDI_Device_t
* const MIDIInterfaceInfo
)
110 if (USB_DeviceState
!= DEVICE_STATE_Configured
)
111 return ENDPOINT_RWSTREAM_DeviceDisconnected
;
115 Endpoint_SelectEndpoint(MIDIInterfaceInfo
->Config
.DataINEndpointNumber
);
117 if (Endpoint_BytesInEndpoint())
121 if ((ErrorCode
= Endpoint_WaitUntilReady()) != ENDPOINT_READYWAIT_NoError
)
125 return ENDPOINT_READYWAIT_NoError
;
128 bool MIDI_Device_ReceiveEventPacket(USB_ClassInfo_MIDI_Device_t
* const MIDIInterfaceInfo
,
129 MIDI_EventPacket_t
* const Event
)
131 if (USB_DeviceState
!= DEVICE_STATE_Configured
)
134 Endpoint_SelectEndpoint(MIDIInterfaceInfo
->Config
.DataOUTEndpointNumber
);
136 if (!(Endpoint_IsReadWriteAllowed()))
139 Endpoint_Read_Stream_LE(Event
, sizeof(MIDI_EventPacket_t
), NO_STREAM_CALLBACK
);
141 if (!(Endpoint_IsReadWriteAllowed()))