Skip to content

Commit e227bdc

Browse files
committed
[USB] Replace NUM_INTERFACE define with a NUM_INTERFACE() function
Done to avoid the need for manually keeping NUM_INTERFACE in sync when experimenting with different USB device types with different number of interfaces. The function needs to be constexpr, since it's used to initialize USB configuration descriptors in PROGMEM.
1 parent e0e1025 commit e227bdc

File tree

3 files changed

+57
-32
lines changed

3 files changed

+57
-32
lines changed

teensy4/usb.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ void usb_isr(void)
352352
//printf("sof %d\n", usb_reboot_timer);
353353
if (usb_reboot_timer) {
354354
if (--usb_reboot_timer == 0) {
355-
usb_stop_sof_interrupts(NUM_INTERFACE);
355+
usb_stop_sof_interrupts(NUM_INTERFACE());
356356
_reboot_Teensyduino_();
357357
}
358358
}
@@ -809,7 +809,7 @@ static void endpoint0_complete(void)
809809
memcpy(usb_cdc_line_coding, endpoint0_buffer, 7);
810810
printf("usb_cdc_line_coding, baud=%u\n", usb_cdc_line_coding[0]);
811811
if (usb_cdc_line_coding[0] == 134) {
812-
usb_start_sof_interrupts(NUM_INTERFACE);
812+
usb_start_sof_interrupts(NUM_INTERFACE());
813813
usb_reboot_timer = 80; // TODO: 10 if only 12 Mbit/sec
814814
}
815815
}
@@ -819,7 +819,7 @@ static void endpoint0_complete(void)
819819
memcpy(usb_cdc2_line_coding, endpoint0_buffer, 7);
820820
printf("usb_cdc2_line_coding, baud=%u\n", usb_cdc2_line_coding[0]);
821821
if (usb_cdc2_line_coding[0] == 134) {
822-
usb_start_sof_interrupts(NUM_INTERFACE);
822+
usb_start_sof_interrupts(NUM_INTERFACE());
823823
usb_reboot_timer = 80; // TODO: 10 if only 12 Mbit/sec
824824
}
825825
}
@@ -829,7 +829,7 @@ static void endpoint0_complete(void)
829829
memcpy(usb_cdc3_line_coding, endpoint0_buffer, 7);
830830
printf("usb_cdc3_line_coding, baud=%u\n", usb_cdc3_line_coding[0]);
831831
if (usb_cdc3_line_coding[0] == 134) {
832-
usb_start_sof_interrupts(NUM_INTERFACE);
832+
usb_start_sof_interrupts(NUM_INTERFACE());
833833
usb_reboot_timer = 80; // TODO: 10 if only 12 Mbit/sec
834834
}
835835
}
@@ -845,7 +845,7 @@ static void endpoint0_complete(void)
845845
if (endpoint0_buffer[0] == 0xA9 && endpoint0_buffer[1] == 0x45
846846
&& endpoint0_buffer[2] == 0xC2 && endpoint0_buffer[3] == 0x6B) {
847847
printf("seremu reboot request\n");
848-
usb_start_sof_interrupts(NUM_INTERFACE);
848+
usb_start_sof_interrupts(NUM_INTERFACE());
849849
usb_reboot_timer = 80; // TODO: 10 if only 12 Mbit/sec
850850
} else {
851851
// any other feature report means Arduino Serial Monitor is open

teensy4/usb_desc.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ extern "C" PROGMEM const uint8_t usb_config_descriptor_480[CONFIG_DESC_SIZE] = {
674674
2, // bDescriptorType;
675675
LSB(CONFIG_DESC_SIZE), // wTotalLength
676676
MSB(CONFIG_DESC_SIZE),
677-
NUM_INTERFACE, // bNumInterfaces
677+
NUM_INTERFACE(), // bNumInterfaces
678678
1, // bConfigurationValue
679679
0, // iConfiguration
680680
0xC0, // bmAttributes
@@ -1688,7 +1688,7 @@ extern "C" PROGMEM const uint8_t usb_config_descriptor_12[CONFIG_DESC_SIZE] = {
16881688
2, // bDescriptorType;
16891689
LSB(CONFIG_DESC_SIZE), // wTotalLength
16901690
MSB(CONFIG_DESC_SIZE),
1691-
NUM_INTERFACE, // bNumInterfaces
1691+
NUM_INTERFACE(), // bNumInterfaces
16921692
1, // bConfigurationValue
16931693
0, // iConfiguration
16941694
0xC0, // bmAttributes

teensy4/usb_desc.h

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ To modify a USB Type, delete the XYZ_INTERFACE lines for any
6363
interfaces you wish to remove, and copy them from another USB Type
6464
for any you want to add.
6565
66-
Give each interface a unique number, and edit NUM_INTERFACE to
67-
reflect the total number of interfaces.
66+
Give each interface a unique number.
6867
6968
Next, assign unique endpoint numbers to all the endpoints across
7069
all the interfaces your device has. You can reuse an endpoint
@@ -121,7 +120,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
121120
#define EP0_SIZE 64
122121
#define NUM_ENDPOINTS 4
123122
#define NUM_USB_BUFFERS 12
124-
#define NUM_INTERFACE 3
125123
#define CDC_IAD_DESCRIPTOR 1 // Serial
126124
#define CDC_STATUS_INTERFACE 0
127125
#define CDC_DATA_INTERFACE 1
@@ -147,7 +145,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
147145
#define PRODUCT_NAME_LEN 11
148146
#define EP0_SIZE 64
149147
#define NUM_ENDPOINTS 5
150-
#define NUM_INTERFACE 4
151148
#define CDC_IAD_DESCRIPTOR 1 // Serial
152149
#define CDC_STATUS_INTERFACE 0
153150
#define CDC_DATA_INTERFACE 1
@@ -178,7 +175,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
178175
#define PRODUCT_NAME_LEN 13
179176
#define EP0_SIZE 64
180177
#define NUM_ENDPOINTS 7
181-
#define NUM_INTERFACE 6
182178
#define CDC_IAD_DESCRIPTOR 1 // Serial
183179
#define CDC_STATUS_INTERFACE 0
184180
#define CDC_DATA_INTERFACE 1
@@ -217,7 +213,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
217213
#define EP0_SIZE 64
218214
#define NUM_ENDPOINTS 4
219215
#define NUM_USB_BUFFERS 14
220-
#define NUM_INTERFACE 3
221216
#define SEREMU_INTERFACE 1 // Serial emulation
222217
#define SEREMU_TX_ENDPOINT 2
223218
#define SEREMU_TX_SIZE 64
@@ -247,7 +242,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
247242
#define EP0_SIZE 64
248243
#define NUM_ENDPOINTS 6
249244
#define NUM_USB_BUFFERS 24
250-
#define NUM_INTERFACE 5
251245
#define SEREMU_INTERFACE 2 // Serial emulation
252246
#define SEREMU_TX_ENDPOINT 2
253247
#define SEREMU_TX_SIZE 64
@@ -289,7 +283,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
289283
#define PRODUCT_NAME_LEN 30
290284
#define EP0_SIZE 64
291285
#define NUM_ENDPOINTS 7
292-
#define NUM_INTERFACE 6
293286
#define CDC_IAD_DESCRIPTOR 1
294287
#define CDC_STATUS_INTERFACE 0
295288
#define CDC_DATA_INTERFACE 1 // Serial
@@ -333,7 +326,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
333326
#define PRODUCT_NAME_LEN 20
334327
#define EP0_SIZE 64
335328
#define NUM_ENDPOINTS 5
336-
#define NUM_INTERFACE 4
337329
#define SEREMU_INTERFACE 1 // Serial emulation
338330
#define SEREMU_TX_ENDPOINT 2
339331
#define SEREMU_TX_SIZE 64
@@ -367,7 +359,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
367359
#define PRODUCT_NAME_LEN 26
368360
#define EP0_SIZE 64
369361
#define NUM_ENDPOINTS 6
370-
#define NUM_INTERFACE 5
371362
#define SEREMU_INTERFACE 2 // Serial emulation
372363
#define SEREMU_TX_ENDPOINT 2
373364
#define SEREMU_TX_SIZE 64
@@ -406,7 +397,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
406397
#define PRODUCT_NAME_LEN 11
407398
#define EP0_SIZE 64
408399
#define NUM_ENDPOINTS 4
409-
#define NUM_INTERFACE 2
410400
#define SEREMU_INTERFACE 1 // Serial emulation
411401
#define SEREMU_TX_ENDPOINT 2
412402
#define SEREMU_TX_SIZE 64
@@ -435,7 +425,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
435425
#define PRODUCT_NAME_LEN 13
436426
#define EP0_SIZE 64
437427
#define NUM_ENDPOINTS 3
438-
#define NUM_INTERFACE 2
439428
#define SEREMU_INTERFACE 1 // Serial emulation
440429
#define SEREMU_TX_ENDPOINT 2
441430
#define SEREMU_TX_SIZE 64
@@ -464,7 +453,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
464453
#define PRODUCT_NAME_LEN 14
465454
#define EP0_SIZE 64
466455
#define NUM_ENDPOINTS 3
467-
#define NUM_INTERFACE 2
468456
#define SEREMU_INTERFACE 1 // Serial emulation
469457
#define SEREMU_TX_ENDPOINT 2
470458
#define SEREMU_TX_SIZE 64
@@ -492,7 +480,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
492480
#define PRODUCT_NAME_LEN 11
493481
#define EP0_SIZE 64
494482
#define NUM_ENDPOINTS 4
495-
#define NUM_INTERFACE 3
496483
#define CDC_IAD_DESCRIPTOR 1
497484
#define CDC_STATUS_INTERFACE 0
498485
#define CDC_DATA_INTERFACE 1 // Serial
@@ -526,7 +513,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
526513
#define PRODUCT_NAME_LEN 13
527514
#define EP0_SIZE 64
528515
#define NUM_ENDPOINTS 4
529-
#define NUM_INTERFACE 3
530516
#define CDC_IAD_DESCRIPTOR 1
531517
#define CDC_STATUS_INTERFACE 0
532518
#define CDC_DATA_INTERFACE 1 // Serial
@@ -560,7 +546,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
560546
#define PRODUCT_NAME_LEN 14
561547
#define EP0_SIZE 64
562548
#define NUM_ENDPOINTS 4
563-
#define NUM_INTERFACE 3
564549
#define CDC_IAD_DESCRIPTOR 1
565550
#define CDC_STATUS_INTERFACE 0
566551
#define CDC_DATA_INTERFACE 1 // Serial
@@ -595,7 +580,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
595580
#define PRODUCT_NAME_LEN 18
596581
#define EP0_SIZE 64
597582
#define NUM_ENDPOINTS 4
598-
#define NUM_INTERFACE 2
599583
#define RAWHID_INTERFACE 0 // RawHID
600584
#define RAWHID_TX_ENDPOINT 3
601585
#define RAWHID_TX_SIZE 64
@@ -623,7 +607,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
623607
#define PRODUCT_NAME_LEN 26
624608
#define EP0_SIZE 64
625609
#define NUM_ENDPOINTS 3
626-
#define NUM_INTERFACE 2
627610
#define FLIGHTSIM_INTERFACE 0 // Flight Sim Control
628611
#define FLIGHTSIM_TX_ENDPOINT 3
629612
#define FLIGHTSIM_TX_SIZE 64
@@ -651,7 +634,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
651634
#define PRODUCT_NAME_LEN 26
652635
#define EP0_SIZE 64
653636
#define NUM_ENDPOINTS 4
654-
#define NUM_INTERFACE 3
655637
#define FLIGHTSIM_INTERFACE 0 // Flight Sim Control
656638
#define FLIGHTSIM_TX_ENDPOINT 3
657639
#define FLIGHTSIM_TX_SIZE 64
@@ -683,7 +665,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
683665
#define PRODUCT_NAME_LEN 15
684666
#define EP0_SIZE 64
685667
#define NUM_ENDPOINTS 4
686-
#define NUM_INTERFACE 2
687668
#define MTP_INTERFACE 1 // MTP Disk
688669
#define MTP_TX_ENDPOINT 3
689670
#define MTP_TX_SIZE_12 64
@@ -715,7 +696,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
715696
#define PRODUCT_NAME_LEN 15
716697
#define EP0_SIZE 64
717698
#define NUM_ENDPOINTS 5
718-
#define NUM_INTERFACE 3
719699
#define CDC_IAD_DESCRIPTOR 1
720700
#define CDC_STATUS_INTERFACE 0
721701
#define CDC_DATA_INTERFACE 1 // Serial
@@ -752,7 +732,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
752732
#define PRODUCT_NAME_LEN 12
753733
#define EP0_SIZE 64
754734
#define NUM_ENDPOINTS 4
755-
#define NUM_INTERFACE 4
756735
#define SEREMU_INTERFACE 0 // Serial emulation
757736
#define SEREMU_TX_ENDPOINT 2
758737
#define SEREMU_TX_SIZE 64
@@ -779,7 +758,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
779758
#define PRODUCT_NAME_LEN 17
780759
#define EP0_SIZE 64
781760
#define NUM_ENDPOINTS 6
782-
#define NUM_INTERFACE 6
783761
#define CDC_IAD_DESCRIPTOR 1
784762
#define CDC_STATUS_INTERFACE 0
785763
#define CDC_DATA_INTERFACE 1 // Serial
@@ -821,7 +799,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
821799
#define PRODUCT_NAME_LEN 20
822800
#define EP0_SIZE 64
823801
#define NUM_ENDPOINTS 8
824-
#define NUM_INTERFACE 6
825802
#define CDC_IAD_DESCRIPTOR 1
826803
#define CDC_STATUS_INTERFACE 0
827804
#define CDC_DATA_INTERFACE 1 // Serial
@@ -867,7 +844,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
867844
#define PRODUCT_NAME_LEN 14
868845
#define EP0_SIZE 64
869846
#define NUM_ENDPOINTS 15
870-
#define NUM_INTERFACE 13
871847
#define CDC_IAD_DESCRIPTOR 1
872848
#define CDC_STATUS_INTERFACE 0
873849
#define CDC_DATA_INTERFACE 1 // Serial
@@ -967,3 +943,52 @@ extern const usb_descriptor_list_t usb_descriptor_list[];
967943
#endif // NUM_ENDPOINTS
968944
#endif // USB_DESC_LIST_DEFINE
969945

946+
constexpr uint8_t NUM_INTERFACE() {
947+
uint8_t interfaceCount = 0;
948+
#ifdef CDC_DATA_INTERFACE
949+
interfaceCount += 2;
950+
#endif
951+
#ifdef CDC2_DATA_INTERFACE
952+
interfaceCount += 2;
953+
#endif
954+
#ifdef CDC3_DATA_INTERFACE
955+
interfaceCount += 2;
956+
#endif
957+
#ifdef MIDI_INTERFACE
958+
interfaceCount += 1;
959+
#endif
960+
#ifdef KEYBOARD_INTERFACE
961+
interfaceCount += 1;
962+
#endif
963+
#ifdef MOUSE_INTERFACE
964+
interfaceCount += 1;
965+
#endif
966+
#ifdef RAWHID_INTERFACE
967+
interfaceCount += 1;
968+
#endif
969+
#ifdef FLIGHTSIM_INTERFACE
970+
interfaceCount += 1;
971+
#endif
972+
#ifdef SEREMU_INTERFACE
973+
interfaceCount += 1;
974+
#endif
975+
#ifdef JOYSTICK_INTERFACE
976+
interfaceCount += 1;
977+
#endif
978+
#ifdef MTP_INTERFACE
979+
interfaceCount += 1;
980+
#endif
981+
#ifdef KEYMEDIA_INTERFACE
982+
interfaceCount += 1;
983+
#endif
984+
#ifdef AUDIO_INTERFACE
985+
interfaceCount += 3;
986+
#endif
987+
#ifdef MULTITOUCH_INTERFACE
988+
interfaceCount += 1;
989+
#endif
990+
#ifdef EXPERIMENTAL_INTERFACE
991+
interfaceCount += 1;
992+
#endif
993+
return interfaceCount;
994+
}

0 commit comments

Comments
 (0)