Skip to content

Commit 99b0d00

Browse files
committed
Merge branch 'nativeUSB_serialNumber'
2 parents f98db5d + cc53512 commit 99b0d00

File tree

2 files changed

+95
-114
lines changed

2 files changed

+95
-114
lines changed

src/MIDIUSB.cpp

Lines changed: 38 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -16,41 +16,11 @@
1616

1717
#include "MIDIUSB.h"
1818

19-
#define MIDI_BUFFER_SIZE 64
20-
21-
#if defined(ARDUINO_ARCH_AVR)
22-
23-
#include "PluggableUSB.h"
24-
#define EPTYPE_DESCRIPTOR_SIZE uint8_t
25-
26-
#else
27-
28-
#include "USB/PluggableUSB.h"
29-
#define EPTYPE_DESCRIPTOR_SIZE uint32_t
30-
31-
#if defined(ARDUINO_ARCH_SAM)
32-
#define USB_SendControl USBD_SendControl
33-
#define USB_Available USBD_Available
34-
#define USB_Recv USBD_Recv
35-
#define USB_Send USBD_Send
36-
#define USB_Flush USBD_Flush
37-
#endif
38-
39-
#if defined(__SAMD21G18A__)
40-
#define USB_SendControl USBDevice.sendControl
41-
#define USB_Available USBDevice.available
42-
#define USB_Recv USBDevice.recv
43-
#define USB_Send USBDevice.send
44-
#define USB_Flush USBDevice.flush
45-
#endif
46-
47-
#endif
48-
49-
static uint8_t MIDI_AC_INTERFACE; // MIDI AC Interface
50-
static uint8_t MIDI_INTERFACE;
51-
static uint8_t MIDI_FIRST_ENDPOINT;
52-
static uint8_t MIDI_ENDPOINT_OUT;
53-
static uint8_t MIDI_ENDPOINT_IN;
19+
#define MIDI_AC_INTERFACE pluggedInterface // MIDI AC Interface
20+
#define MIDI_INTERFACE pluggedInterface+1
21+
#define MIDI_FIRST_ENDPOINT pluggedEndpoint
22+
#define MIDI_ENDPOINT_OUT pluggedEndpoint
23+
#define MIDI_ENDPOINT_IN pluggedEndpoint+1
5424

5525
#define MIDI_RX MIDI_ENDPOINT_OUT
5626
#define MIDI_TX MIDI_ENDPOINT_IN
@@ -64,25 +34,47 @@ struct ring_bufferMIDI
6434

6535
ring_bufferMIDI midi_rx_buffer = {{0,0,0,0 }, 0, 0};
6636

67-
static MIDIDescriptor _midiInterface;
37+
MIDI_ MidiUSB;
6838

69-
int MIDI_GetInterface(uint8_t* interfaceNum)
39+
int MIDI_::getInterface(uint8_t* interfaceNum)
7040
{
71-
interfaceNum[0] += 2; // uses 2
72-
return USB_SendControl(0,&_midiInterface,sizeof(_midiInterface));
41+
interfaceNum[0] += 2; // uses 2 interfaces
42+
MIDIDescriptor _midiInterface =
43+
{
44+
D_IAD(MIDI_AC_INTERFACE, 2, MIDI_AUDIO, MIDI_AUDIO_CONTROL, 0),
45+
D_INTERFACE(MIDI_AC_INTERFACE,0,MIDI_AUDIO,MIDI_AUDIO_CONTROL,0),
46+
D_AC_INTERFACE(0x1, MIDI_INTERFACE),
47+
D_INTERFACE(MIDI_INTERFACE,2, MIDI_AUDIO,MIDI_STREAMING,0),
48+
D_AS_INTERFACE,
49+
D_MIDI_INJACK(MIDI_JACK_EMD, 0x1),
50+
D_MIDI_INJACK(MIDI_JACK_EXT, 0x2),
51+
D_MIDI_OUTJACK(MIDI_JACK_EMD, 0x3, 1, 2, 1),
52+
D_MIDI_OUTJACK(MIDI_JACK_EXT, 0x4, 1, 1, 1),
53+
D_MIDI_JACK_EP(USB_ENDPOINT_OUT(MIDI_ENDPOINT_OUT),USB_ENDPOINT_TYPE_BULK,MIDI_BUFFER_SIZE),
54+
D_MIDI_AC_JACK_EP(1, 1),
55+
D_MIDI_JACK_EP(USB_ENDPOINT_IN(MIDI_ENDPOINT_IN),USB_ENDPOINT_TYPE_BULK,MIDI_BUFFER_SIZE),
56+
D_MIDI_AC_JACK_EP (1, 3)
57+
};
58+
return USB_SendControl(0, &_midiInterface, sizeof(_midiInterface));
7359
}
7460

75-
bool MIDI_Setup(USBSetup& setup, uint8_t i)
61+
bool MIDI_::setup(USBSetup& setup __attribute__((unused)))
7662
{
7763
//Support requests here if needed. Typically these are optional
7864
return false;
7965
}
8066

81-
int MIDI_GetDescriptor(int8_t t)
67+
int MIDI_::getDescriptor(USBSetup& setup __attribute__((unused)))
8268
{
8369
return 0;
8470
}
8571

72+
uint8_t MIDI_::getShortName(char* name)
73+
{
74+
memcpy(name, "MIDI", 4);
75+
return 4;
76+
}
77+
8678
void MIDI_::accept(void)
8779
{
8880
ring_bufferMIDI *buffer = &midi_rx_buffer;
@@ -183,49 +175,9 @@ void MIDI_::sendMIDI(midiEventPacket_t event)
183175
write(data, 4);
184176
}
185177

186-
MIDI_::MIDI_(void)
187-
{
188-
static EPTYPE_DESCRIPTOR_SIZE endpointType[2];
189-
190-
endpointType[0] = EP_TYPE_BULK_OUT_MIDI; // MIDI_ENDPOINT_OUT
191-
endpointType[1] = EP_TYPE_BULK_IN_MIDI; // MIDI_ENDPOINT_IN
192-
193-
static PUSBCallbacks cb = {
194-
.setup = &MIDI_Setup,
195-
.getInterface = &MIDI_GetInterface,
196-
.getDescriptor = &MIDI_GetDescriptor,
197-
.numEndpoints = 2,
198-
.numInterfaces = 2,
199-
.endpointType = endpointType,
200-
};
201-
202-
static PUSBListNode node(&cb);
203-
204-
MIDI_ENDPOINT_OUT = PUSB_AddFunction(&node, &MIDI_AC_INTERFACE);
205-
MIDI_ENDPOINT_IN = MIDI_ENDPOINT_OUT + 1;
206-
MIDI_INTERFACE = MIDI_AC_INTERFACE + 1;
207-
208-
_midiInterface =
209-
{
210-
D_IAD(MIDI_AC_INTERFACE, 2, MIDI_AUDIO, MIDI_AUDIO_CONTROL, 0),
211-
D_INTERFACE(MIDI_AC_INTERFACE,0,MIDI_AUDIO,MIDI_AUDIO_CONTROL,0),
212-
D_AC_INTERFACE(0x1, MIDI_INTERFACE),
213-
D_INTERFACE(MIDI_INTERFACE,2, MIDI_AUDIO,MIDI_STREAMING,0),
214-
D_AS_INTERFACE,
215-
D_MIDI_INJACK(MIDI_JACK_EMD, 0x1),
216-
D_MIDI_INJACK(MIDI_JACK_EXT, 0x2),
217-
D_MIDI_OUTJACK(MIDI_JACK_EMD, 0x3, 1, 2, 1),
218-
D_MIDI_OUTJACK(MIDI_JACK_EXT, 0x4, 1, 1, 1),
219-
D_MIDI_JACK_EP(USB_ENDPOINT_OUT(MIDI_ENDPOINT_OUT),USB_ENDPOINT_TYPE_BULK,512),
220-
D_MIDI_AC_JACK_EP(1, 1),
221-
D_MIDI_JACK_EP(USB_ENDPOINT_IN(MIDI_ENDPOINT_IN),USB_ENDPOINT_TYPE_BULK,512),
222-
D_MIDI_AC_JACK_EP (1, 3)
223-
};
224-
}
225-
226-
int8_t MIDI_::begin()
178+
MIDI_::MIDI_(void) : PluggableUSBModule(2, 2, epType)
227179
{
228-
}
229-
230-
231-
MIDI_ MidiUSB;
180+
epType[0] = EP_TYPE_BULK_OUT_MIDI; // MIDI_ENDPOINT_OUT
181+
epType[1] = EP_TYPE_BULK_IN_MIDI; // MIDI_ENDPOINT_IN
182+
PluggableUSB().plug(this);
183+
}

src/MIDIUSB.h

Lines changed: 57 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,49 +19,52 @@ typedef struct
1919
}midiEventPacket_t;
2020

2121
#if defined(ARDUINO_ARCH_AVR)
22+
23+
#include "PluggableUSB.h"
24+
#define EPTYPE_DESCRIPTOR_SIZE uint8_t
2225
#define EP_TYPE_BULK_OUT_MIDI EP_TYPE_BULK_OUT
2326
#define EP_TYPE_BULK_IN_MIDI EP_TYPE_BULK_IN
24-
#endif
27+
#define MIDI_BUFFER_SIZE 64
28+
29+
#else
30+
31+
#include "USB/PluggableUSB.h"
32+
#define EPTYPE_DESCRIPTOR_SIZE uint32_t
33+
#define MIDI_BUFFER_SIZE 512
2534

2635
#if defined(ARDUINO_ARCH_SAM)
27-
#define EP_TYPE_BULK_IN_MIDI (UOTGHS_DEVEPTCFG_EPSIZE_64_BYTE | \
36+
#define USB_SendControl USBD_SendControl
37+
#define USB_Available USBD_Available
38+
#define USB_Recv USBD_Recv
39+
#define USB_Send USBD_Send
40+
#define USB_Flush USBD_Flush
41+
42+
#define EP_TYPE_BULK_IN_MIDI (UOTGHS_DEVEPTCFG_EPSIZE_512_BYTE | \
2843
UOTGHS_DEVEPTCFG_EPDIR_IN | \
2944
UOTGHS_DEVEPTCFG_EPTYPE_BLK | \
3045
UOTGHS_DEVEPTCFG_EPBK_1_BANK | \
3146
UOTGHS_DEVEPTCFG_NBTRANS_1_TRANS | \
3247
UOTGHS_DEVEPTCFG_ALLOC)
3348

34-
#define EP_TYPE_BULK_OUT_MIDI (UOTGHS_DEVEPTCFG_EPSIZE_64_BYTE | \
49+
#define EP_TYPE_BULK_OUT_MIDI (UOTGHS_DEVEPTCFG_EPSIZE_512_BYTE | \
3550
UOTGHS_DEVEPTCFG_EPTYPE_BLK | \
3651
UOTGHS_DEVEPTCFG_EPBK_1_BANK | \
3752
UOTGHS_DEVEPTCFG_NBTRANS_1_TRANS | \
3853
UOTGHS_DEVEPTCFG_ALLOC)
3954
#endif
4055

4156
#if defined(__SAMD21G18A__)
57+
#define USB_SendControl USBDevice.sendControl
58+
#define USB_Available USBDevice.available
59+
#define USB_Recv USBDevice.recv
60+
#define USB_Send USBDevice.send
61+
#define USB_Flush USBDevice.flush
62+
4263
#define EP_TYPE_BULK_IN_MIDI USB_ENDPOINT_TYPE_BULK | USB_ENDPOINT_IN(0);
4364
#define EP_TYPE_BULK_OUT_MIDI USB_ENDPOINT_TYPE_BULK | USB_ENDPOINT_OUT(0);
44-
#endif
4565

46-
class MIDI_
47-
{
48-
// private:
49-
// RingBuffer *_midi_rx_buffer;
50-
private:
51-
void accept(void);
52-
public:
53-
MIDI_(void);
54-
55-
int8_t begin();
56-
57-
uint32_t available(void);
58-
midiEventPacket_t read(void);
59-
void flush(void);
60-
void sendMIDI(midiEventPacket_t event);
61-
size_t write(const uint8_t *buffer, size_t size);
62-
operator bool();
63-
};
64-
extern MIDI_ MidiUSB;
66+
#endif
67+
#endif
6568

6669
#define MIDI_AUDIO 0x01
6770
#define MIDI_AUDIO_CONTROL 0x01
@@ -71,7 +74,7 @@ extern MIDI_ MidiUSB;
7174
#define MIDI_JACK_EMD 0x01
7275
#define MIDI_JACK_EXT 0X02
7376

74-
#pragma pack(1)
77+
_Pragma("pack(1)")
7578

7679
typedef struct
7780
{
@@ -156,7 +159,7 @@ typedef struct
156159
} MIDIDescriptor;
157160

158161
#define D_AC_INTERFACE(_streamingInterfaces, _MIDIInterface) \
159-
{ 9, MIDI_CS_INTERFACE, 0x1, 0x0100, 0x0009, _streamingInterfaces, _MIDIInterface }
162+
{ 9, MIDI_CS_INTERFACE, 0x1, 0x0100, 0x0009, _streamingInterfaces, (uint8_t)(_MIDIInterface) }
160163

161164
#define D_AS_INTERFACE \
162165
{ 0x7, MIDI_CS_INTERFACE, 0x01,0x0100, 0x0041}
@@ -176,9 +179,35 @@ typedef struct
176179
#define D_CDCCS(_subtype,_d0,_d1) { 5, 0x24, _subtype, _d0, _d1 }
177180
#define D_CDCCS4(_subtype,_d0) { 4, 0x24, _subtype, _d0 }
178181

179-
#pragma pack()
182+
_Pragma("pack()")
180183

181184
#define WEAK __attribute__ ((weak))
182185

183-
#endif
184-
#endif
186+
class MIDI_ : public PluggableUSBModule
187+
{
188+
// private:
189+
// RingBuffer *_midi_rx_buffer;
190+
private:
191+
void accept(void);
192+
EPTYPE_DESCRIPTOR_SIZE epType[2];
193+
194+
protected:
195+
// Implementation of the PUSBListNode
196+
int getInterface(uint8_t* interfaceNum);
197+
int getDescriptor(USBSetup& setup);
198+
bool setup(USBSetup& setup);
199+
uint8_t getShortName(char* name);
200+
201+
public:
202+
MIDI_(void);
203+
uint32_t available(void);
204+
midiEventPacket_t read(void);
205+
void flush(void);
206+
void sendMIDI(midiEventPacket_t event);
207+
size_t write(const uint8_t *buffer, size_t size);
208+
operator bool();
209+
};
210+
extern MIDI_ MidiUSB;
211+
212+
#endif /* USBCON */
213+
#endif /* MIDIUSB_h */

0 commit comments

Comments
 (0)