Skip to content

Commit bf1d9db

Browse files
committed
update(demo): format uac demos with macros
Signed-off-by: sakumisu <[email protected]>
1 parent a20f53c commit bf1d9db

11 files changed

+305
-472
lines changed

class/audio/usb_audio.h

Lines changed: 46 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,6 @@
8888
#define AUDIO_ENDPOINT_UNDEFINED 0x00U
8989
#define AUDIO_ENDPOINT_GENERAL 0x01U
9090

91-
/* Feature Unit Control Bits */
92-
#define AUDIO_CONTROL_MUTE 0x0001
93-
#define AUDIO_CONTROL_VOLUME 0x0002
94-
#define AUDIO_CONTROL_BASS 0x0004
95-
#define AUDIO_CONTROL_MID 0x0008
96-
#define AUDIO_CONTROL_TREBLE 0x0010
97-
#define AUDIO_CONTROL_GRAPHIC_EQUALIZER 0x0020
98-
#define AUDIO_CONTROL_AUTOMATIC_GAIN 0x0040
99-
#define AUDIO_CONTROL_DEALY 0x0080
100-
#define AUDIO_CONTROL_BASS_BOOST 0x0100
101-
#define AUDIO_CONTROL_LOUDNESS 0x0200
102-
10391
/* Encoder Type Codes */
10492
#define AUDIO_ENCODER_UNDEF 0x00
10593
#define AUDIO_ENCODER_OTHER 0x01
@@ -245,22 +233,34 @@
245233
#define AUDIO_FU_CONTROL_OVERFLOW 0x0f
246234
#define AUDIO_FU_CONTROL_LATENCY 0x10
247235

248-
#define AUDIO_V2_FU_CONTROL_UNDEF 0x00
249-
#define AUDIO_V2_FU_CONTROL_MUTE (0x03 << 0)
250-
#define AUDIO_V2_FU_CONTROL_VOLUME (0x03 << 2)
251-
#define AUDIO_V2_FU_CONTROL_BASS (0x03 << 4)
252-
#define AUDIO_V2_FU_CONTROL_MID (0x03 << 6)
253-
#define AUDIO_V2_FU_CONTROL_TREBLE (0x03 << 8)
254-
#define AUDIO_V2_FU_CONTROL_EQUALIZER (0x03 << 10)
255-
#define AUDIO_V2_FU_CONTROL_AGC (0x03 << 12)
256-
#define AUDIO_V2_FU_CONTROL_DELAY (0x03 << 14)
257-
#define AUDIO_V2_FU_CONTROL_BASS_BOOST (0x03 << 16)
258-
#define AUDIO_V2_FU_CONTROL_LOUDNESS (0x03 << 18)
259-
#define AUDIO_V2_FU_CONTROL_INP_GAIN (0x03 << 20)
260-
#define AUDIO_V2_FU_CONTROL_INP_GAIN_PAD (0x03 << 22)
261-
#define AUDIO_V2_FU_CONTROL_PHASE_INVERT (0x03 << 24)
262-
#define AUDIO_V2_FU_CONTROL_UNDERFLOW (0x03 << 26)
263-
#define AUDIO_V2_FU_CONTROL_OVERFLOW (0x03 << 28)
236+
/* Feature Unit Control Bits */
237+
#define AUDIO_CONTROL_MUTE 0x0001
238+
#define AUDIO_CONTROL_VOLUME 0x0002
239+
#define AUDIO_CONTROL_BASS 0x0004
240+
#define AUDIO_CONTROL_MID 0x0008
241+
#define AUDIO_CONTROL_TREBLE 0x0010
242+
#define AUDIO_CONTROL_GRAPHIC_EQUALIZER 0x0020
243+
#define AUDIO_CONTROL_AUTOMATIC_GAIN 0x0040
244+
#define AUDIO_CONTROL_DEALY 0x0080
245+
#define AUDIO_CONTROL_BASS_BOOST 0x0100
246+
#define AUDIO_CONTROL_LOUDNESS 0x0200
247+
248+
#define AUDIO_V2_CONTROL_UNDEF 0x00
249+
#define AUDIO_V2_CONTROL_MUTE (0x03 << 0)
250+
#define AUDIO_V2_CONTROL_VOLUME (0x03 << 2)
251+
#define AUDIO_V2_CONTROL_BASS (0x03 << 4)
252+
#define AUDIO_V2_CONTROL_MID (0x03 << 6)
253+
#define AUDIO_V2_CONTROL_TREBLE (0x03 << 8)
254+
#define AUDIO_V2_CONTROL_EQUALIZER (0x03 << 10)
255+
#define AUDIO_V2_CONTROL_AGC (0x03 << 12)
256+
#define AUDIO_V2_CONTROL_DELAY (0x03 << 14)
257+
#define AUDIO_V2_CONTROL_BASS_BOOST (0x03 << 16)
258+
#define AUDIO_V2_CONTROL_LOUDNESS (0x03 << 18)
259+
#define AUDIO_V2_CONTROL_INP_GAIN (0x03 << 20)
260+
#define AUDIO_V2_CONTROL_INP_GAIN_PAD (0x03 << 22)
261+
#define AUDIO_V2_CONTROL_PHASE_INVERT (0x03 << 24)
262+
#define AUDIO_V2_CONTROL_UNDERFLOW (0x03 << 26)
263+
#define AUDIO_V2_CONTROL_OVERFLOW (0x03 << 28)
264264

265265
/* Parametric Equalizer Section Effect Unit Control Selectors */
266266
#define AUDIO_PE_CONTROL_UNDEF 0x00
@@ -605,7 +605,7 @@ struct audio_cs_if_ac_header_descriptor {
605605
uint8_t baInterfaceNr[];
606606
} __PACKED;
607607

608-
#define AUDIO_SIZEOF_AC_HEADER_DESC(n) (8 + n)
608+
#define AUDIO_SIZEOF_AC_HEADER_DESC(bInCollection) (8 + (bInCollection))
609609

610610
struct audio_cs_if_ac_input_terminal_descriptor {
611611
uint8_t bLength;
@@ -646,7 +646,7 @@ struct audio_cs_if_ac_feature_unit_descriptor {
646646
uint8_t iFeature;
647647
} __PACKED;
648648

649-
#define AUDIO_SIZEOF_AC_FEATURE_UNIT_DESC(ch, n) (7 + (ch + 1) * n)
649+
#define AUDIO_SIZEOF_AC_FEATURE_UNIT_DESC(bNrChannels, bControlSize) (7 + ((bNrChannels) + 1) * (bControlSize))
650650

651651
struct audio_cs_if_ac_selector_unit_descriptor {
652652
uint8_t bLength;
@@ -658,7 +658,7 @@ struct audio_cs_if_ac_selector_unit_descriptor {
658658
uint8_t iSelector;
659659
} __PACKED;
660660

661-
#define AUDIO_SIZEOF_AC_SELECTOR_UNIT_DESC(n) (6 + n)
661+
#define AUDIO_SIZEOF_AC_SELECTOR_UNIT_DESC(bNrInPins) (6 + (bNrInPins))
662662

663663
struct audio_cs_if_as_general_descriptor {
664664
uint8_t bLength;
@@ -683,7 +683,7 @@ struct audio_cs_if_as_format_type_descriptor {
683683
uint8_t tSamFreq[3];
684684
} __PACKED;
685685

686-
#define AUDIO_SIZEOF_FORMAT_TYPE_DESC(n) (8 + 3 * n)
686+
#define AUDIO_SIZEOF_FORMAT_TYPE_DESC(bSamFreqType) (8 + 3 * (bSamFreqType))
687687

688688
struct audio_ep_descriptor {
689689
uint8_t bLength;
@@ -738,7 +738,7 @@ struct audio_cs_ep_ep_general_descriptor {
738738
PP_NARG(__VA_ARGS__), /* bInCollection */ \
739739
__VA_ARGS__ /* baInterfaceNr */
740740

741-
#define AUDIO_AC_DESCRIPTOR_INIT_LEN(n) (0x08 + 0x09 + 0x08 + n)
741+
#define AUDIO_AC_DESCRIPTOR_LEN(bInCollection) (0x08 + 0x09 + 0x08 + bInCollection)
742742

743743
#define AUDIO_AC_INPUT_TERMINAL_DESCRIPTOR_INIT(bTerminalID, wTerminalType, bNrChannels, wChannelConfig) \
744744
0x0C, /* bLength */ \
@@ -880,8 +880,8 @@ struct audio_cs_ep_ep_general_descriptor {
880880
0x03, /* bRefresh, 8ms */ \
881881
0x00 /* bSynchAddress */
882882

883-
#define AUDIO_AS_DESCRIPTOR_INIT_LEN(n) (0x09 + 0x09 + 0x07 + 0x08 + 3 * n + 0x09 + 0x07)
884-
#define AUDIO_AS_FEEDBACK_DESCRIPTOR_INIT_LEN(n) (0x09 + 0x09 + 0x07 + 0x08 + 3 * n + 0x09 + 0x07 + 0x09)
883+
#define AUDIO_AS_DESCRIPTOR_LEN(bSamFreqType) (0x09 + 0x09 + 0x07 + 0x08 + 3 * (bSamFreqType) + 0x09 + 0x07)
884+
#define AUDIO_AS_FEEDBACK_DESCRIPTOR_LEN(bSamFreqType) (0x09 + 0x09 + 0x07 + 0x08 + 3 * (bSamFreqType) + 0x09 + 0x07 + 0x09)
885885

886886
#define AUDIO_AS_ALTSETTING_DESCRIPTOR_INIT(bInterfaceNumber, bAlternateSetting, bTerminalLink, bNrChannels, bSubFrameSize, bBitResolution, bEndpointAddress, bmAttributes, wMaxPacketSize, bInterval, ...) \
887887
0x09, /* bLength */ \
@@ -924,9 +924,9 @@ struct audio_cs_ep_ep_general_descriptor {
924924
0x00, /* wLockDelay */ \
925925
0x00
926926

927-
#define AUDIO_AS_ALTSETTING_DESCRIPTOR_INIT_LEN(n) (0x09 + 0x07 + 0x08 + 3 * n + 0x09 + 0x07)
927+
#define AUDIO_AS_ALTSETTING_DESCRIPTOR_LEN(bSamFreqType) (0x09 + 0x07 + 0x08 + 3 * (bSamFreqType) + 0x09 + 0x07)
928928

929-
#define AUDIO_AS_ALTSETTING0_DESCRIPTOR_INIT(bInterfaceNumber) \
929+
#define AUDIO_AS_ALTSETTING0_DESCRIPTOR_INIT(bInterfaceNumber) \
930930
0x09, /* bLength */ \
931931
USB_DESCRIPTOR_TYPE_INTERFACE, /* bDescriptorType */ \
932932
bInterfaceNumber, /* bInterfaceNumber */ \
@@ -937,19 +937,6 @@ struct audio_cs_ep_ep_general_descriptor {
937937
AUDIO_PROTOCOL_UNDEFINED, /* bInterfaceProtocol */ \
938938
0x00 /* iInterface */
939939

940-
#define AUDIO_MS_STANDARD_DESCRIPTOR_INIT(bInterfaceNumber, bNumEndpoints) \
941-
0x09, /* bLength */ \
942-
USB_DESCRIPTOR_TYPE_INTERFACE, /* bDescriptorType */ \
943-
bInterfaceNumber, /* bInterfaceNumber */ \
944-
0x00, /* bAlternateSetting */ \
945-
bNumEndpoints, /* bNumEndpoints */ \
946-
USB_DEVICE_CLASS_AUDIO, /* bInterfaceClass */ \
947-
AUDIO_SUBCLASS_MIDISTREAMING, /* bInterfaceSubClass */ \
948-
AUDIO_PROTOCOL_UNDEFINED, /* bInterfaceProtocol */ \
949-
0x00 /* iInterface */
950-
951-
#define AUDIO_MS_STANDARD_DESCRIPTOR_INIT_LEN 0x09
952-
953940
struct audio_v2_channel_cluster_descriptor {
954941
uint8_t bNrChannels;
955942
uint32_t bmChannelConfig;
@@ -993,7 +980,7 @@ struct audio_v2_cs_if_ac_clock_selector_descriptor {
993980
uint8_t iClockSelector;
994981
} __PACKED;
995982

996-
#define AUDIO_SIZEOF_AC_CLOCK_SELECTOR_DESC(n) (7 + n)
983+
#define AUDIO_SIZEOF_AC_CLOCK_SELECTOR_DESC(bNrInPins) (7 + (bNrInPins))
997984

998985
struct audio_v2_cs_if_ac_clock_multiplier_descriptor {
999986
uint8_t bLength;
@@ -1005,7 +992,7 @@ struct audio_v2_cs_if_ac_clock_multiplier_descriptor {
1005992
uint8_t iClockMultiplier;
1006993
} __PACKED;
1007994

1008-
#define AUDIO_SIZEOF_AC_CLOCK_MULTIPLIER_DESC() (7)
995+
#define AUDIO_SIZEOF_AC_CLOCK_MULTIPLIER_DESC (7)
1009996

1010997
struct audio_v2_cs_if_ac_input_terminal_descriptor {
1011998
uint8_t bLength;
@@ -1049,7 +1036,7 @@ struct audio_v2_cs_if_ac_feature_unit_descriptor {
10491036
uint8_t iFeature;
10501037
} __PACKED;
10511038

1052-
#define AUDIO_V2_SIZEOF_AC_FEATURE_UNIT_DESC(ch) (6 + (ch + 1) * 4)
1039+
#define AUDIO_V2_SIZEOF_AC_FEATURE_UNIT_DESC(bNrChannels) (6 + ((bNrChannels) + 1) * 4)
10531040

10541041
struct audio_v2_cs_if_as_general_descriptor {
10551042
uint8_t bLength;
@@ -1124,7 +1111,7 @@ struct audio_v2_control_range3_param_block {
11241111
WBVAL(wTotalLength), /* wTotalLength */ \
11251112
bmControls /* bmControls */ \
11261113

1127-
#define AUDIO_V2_AC_DESCRIPTOR_INIT_LEN (0x08 + 0x09 + 0x09)
1114+
#define AUDIO_V2_AC_DESCRIPTOR_LEN (0x08 + 0x09 + 0x09)
11281115

11291116
#define AUDIO_V2_AC_CLOCK_SOURCE_DESCRIPTOR_INIT(bClockID, bmAttributes, bmControls) \
11301117
0x08, /* bLength */ \
@@ -1262,7 +1249,7 @@ struct audio_v2_control_range3_param_block {
12621249
0x00, /* wLockDelay */ \
12631250
0x00
12641251

1265-
#define AUDIO_V2_AS_ALTSETTING0_DESCRIPTOR_INIT(bInterfaceNumber) \
1252+
#define AUDIO_V2_AS_ALTSETTING0_DESCRIPTOR_INIT(bInterfaceNumber) \
12661253
0x09, /* bLength */ \
12671254
USB_DESCRIPTOR_TYPE_INTERFACE, /* bDescriptorType */ \
12681255
bInterfaceNumber, /* bInterfaceNumber */ \
@@ -1331,9 +1318,10 @@ struct audio_v2_control_range3_param_block {
13311318

13321319
// clang-format on
13331320

1334-
#define AUDIO_V2_AS_DESCRIPTOR_INIT_LEN (0x09 + 0x09 + 0x10 + 0x06 + 0x07 + 0x08)
1335-
#define AUDIO_V2_AS_ALTSETTING_DESCRIPTOR_INIT_LEN (0x09 + 0x10 + 0x06 + 0x07 + 0x08)
1336-
#define AUDIO_V2_AS_FEEDBACK_DESCRIPTOR_INIT_LEN (0x09 + 0x09 + 0x10 + 0x06 + 0x07 + 0x08 + 0x07)
1321+
#define AUDIO_V2_AS_DESCRIPTOR_LEN (0x09 + 0x09 + 0x10 + 0x06 + 0x07 + 0x08)
1322+
#define AUDIO_V2_AS_ALTSETTING0_DESCRIPTOR_LEN (0x09)
1323+
#define AUDIO_V2_AS_ALTSETTING_DESCRIPTOR_LEN (0x09 + 0x10 + 0x06 + 0x07 + 0x08)
1324+
#define AUDIO_V2_AS_FEEDBACK_DESCRIPTOR_LEN (0x09 + 0x09 + 0x10 + 0x06 + 0x07 + 0x08 + 0x07)
13371325

13381326
#define AUDIO_SAMPLE_FREQ_NUM(num) (uint8_t)(num), (uint8_t)((num >> 8))
13391327
#define AUDIO_SAMPLE_FREQ_3B(frq) (uint8_t)(frq), (uint8_t)((frq >> 8)), (uint8_t)((frq >> 16))

class/midi/usb_midi.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#ifndef USB_MIDI_H
77
#define USB_MIDI_H
88

9+
#include "usb_audio.h"
10+
911
/* bDescriptorSubType */
1012
#define MIDI_VC_HEADER_DESCRIPTOR_SUBTYPE 0x01U
1113
#define MIDI_MS_HEADER_DESCRIPTOR_SUBTYPE 0x01U
@@ -201,6 +203,19 @@ struct midi_cs_ep_ms_general_descriptor {
201203
#define MIDI_SIZEOF_MS_GENERAL_DESC(n) (4 + n)
202204

203205
// clang-format off
206+
#define MIDI_STANDARD_DESCRIPTOR_INIT(bInterfaceNumber, bNumEndpoints) \
207+
0x09, /* bLength */ \
208+
USB_DESCRIPTOR_TYPE_INTERFACE, /* bDescriptorType */ \
209+
bInterfaceNumber, /* bInterfaceNumber */ \
210+
0x00, /* bAlternateSetting */ \
211+
bNumEndpoints, /* bNumEndpoints */ \
212+
USB_DEVICE_CLASS_AUDIO, /* bInterfaceClass */ \
213+
AUDIO_SUBCLASS_MIDISTREAMING, /* bInterfaceSubClass */ \
214+
AUDIO_PROTOCOL_UNDEFINED, /* bInterfaceProtocol */ \
215+
0x00 /* iInterface */
216+
217+
#define MIDI_STANDARD_DESCRIPTOR_LEN 0x09
218+
204219
#define MIDI_CS_HEADER_DESCRIPTOR_INIT(wTotalLength) \
205220
0x07, /* bLength */ \
206221
USB_CS_DESCRIPTOR_TYPE_INTERFACE, /* bDescriptorType */ \

demo/audio_v1_mic_multichan_template.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
/* AUDIO Class Config */
2525
#define AUDIO_FREQ 16000U
2626

27-
#define IN_CHANNEL_NUM 1
27+
#define IN_CHANNEL_NUM 2
2828

2929
#if IN_CHANNEL_NUM == 1
3030
#define INPUT_CTRL 0x03, 0x03
31-
#define INPUT_CH_ENABLE 0x0000
31+
#define INPUT_CH_ENABLE 0x0001
3232
#elif IN_CHANNEL_NUM == 2
3333
#define INPUT_CTRL 0x03, 0x03, 0x03
3434
#define INPUT_CH_ENABLE 0x0003
@@ -56,12 +56,12 @@
5656
/* 16bit(2 Bytes) 单声道(Mono:1) */
5757
#define AUDIO_IN_PACKET ((uint32_t)((AUDIO_FREQ * 2 * IN_CHANNEL_NUM) / 1000))
5858

59-
#define USB_AUDIO_CONFIG_DESC_SIZ (unsigned long)(9 + \
60-
AUDIO_AC_DESCRIPTOR_INIT_LEN(1) + \
61-
AUDIO_SIZEOF_AC_INPUT_TERMINAL_DESC + \
62-
AUDIO_SIZEOF_AC_FEATURE_UNIT_DESC(IN_CHANNEL_NUM, 1) + \
63-
AUDIO_SIZEOF_AC_OUTPUT_TERMINAL_DESC + \
64-
AUDIO_AS_DESCRIPTOR_INIT_LEN(1))
59+
#define USB_CONFIG_SIZE (unsigned long)(9 + \
60+
AUDIO_AC_DESCRIPTOR_LEN(1) + \
61+
AUDIO_SIZEOF_AC_INPUT_TERMINAL_DESC + \
62+
AUDIO_SIZEOF_AC_FEATURE_UNIT_DESC(IN_CHANNEL_NUM, 1) + \
63+
AUDIO_SIZEOF_AC_OUTPUT_TERMINAL_DESC + \
64+
AUDIO_AS_DESCRIPTOR_LEN(1))
6565

6666
#define AUDIO_AC_SIZ (AUDIO_SIZEOF_AC_HEADER_DESC(1) + \
6767
AUDIO_SIZEOF_AC_INPUT_TERMINAL_DESC + \
@@ -74,7 +74,7 @@ static const uint8_t device_descriptor[] = {
7474
};
7575

7676
static const uint8_t config_descriptor[] = {
77-
USB_CONFIG_DESCRIPTOR_INIT(USB_AUDIO_CONFIG_DESC_SIZ, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
77+
USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
7878
AUDIO_AC_DESCRIPTOR_INIT(0x00, 0x02, AUDIO_AC_SIZ, 0x00, 0x01),
7979
AUDIO_AC_INPUT_TERMINAL_DESCRIPTOR_INIT(0x01, AUDIO_INTERM_MIC, IN_CHANNEL_NUM, INPUT_CH_ENABLE),
8080
AUDIO_AC_FEATURE_UNIT_DESCRIPTOR_INIT(AUDIO_IN_FU_ID, 0x01, 0x01, INPUT_CTRL),
@@ -137,7 +137,7 @@ const struct usb_descriptor audio_v1_descriptor = {
137137
#else
138138
const uint8_t audio_v1_descriptor[] = {
139139
USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xef, 0x02, 0x01, USBD_VID, USBD_PID, 0x0001, 0x01),
140-
USB_CONFIG_DESCRIPTOR_INIT(USB_AUDIO_CONFIG_DESC_SIZ, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
140+
USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
141141
AUDIO_AC_DESCRIPTOR_INIT(0x00, 0x02, AUDIO_AC_SIZ, 0x00, 0x01),
142142
AUDIO_AC_INPUT_TERMINAL_DESCRIPTOR_INIT(0x01, AUDIO_INTERM_MIC, IN_CHANNEL_NUM, INPUT_CH_ENABLE),
143143
AUDIO_AC_FEATURE_UNIT_DESCRIPTOR_INIT(AUDIO_IN_FU_ID, 0x01, 0x01, INPUT_CTRL),

0 commit comments

Comments
 (0)