AN0013 应用笔记 |
AT32 USB Audio Development Note |
前言
这篇应用笔记描述了USB Audio Demo实现的说明,Demo包含了USB传输类型(Control,Isochronous)。
文档目的是为了让用户能够更快的使用Artery AT32Fxx系列的USB外设进行USB 设备开发。
目前这个文档介绍如下AT32Fxx USB 相关内容:
- usbd_drivers:关于USB协议层的库
- usbd_class\audio:isochronous的使用,实现USB Speaker 和 USB MicroPhone
参考资料:
- AT32F403A_407_Firmware_Library\project\at_start_f403a\examples\usb_device
- RM_AT32F4xx文档的闪存控制器USB device章节
- 《Universal Serial Bus Device Class Definition for Audio Devices》
注:本应用笔记对应的代码是基于雅特力提供的V2.x.x 板级支持包(BSP)而开发,对于其他版本BSP,需要注意使用上的区别。
支持型号列表:
支持型号 | 具备USB的型号 |
目录
3 Audio_Composite_Speaker_MicroPhone. 7
3.4 AT32 USB Audio Speaker and MicroPhone. 8
1.1.1 Speaker 和 MicroPhone 设备配置... 12
1.1.3 Speaker 数据处理... 错误!未定义书签。
表目录
表3. Audio设备描述->usbd_desc.c. 13
表4. Audio 配置描述->usbd_desc.c. 13
表6. Audio Class-specific AC接口描述->usbd_desc.c. 14
表7. MicroPhone Input Terminal描述->usbd_desc.c. 14
表8. MicroPhone Feature Unit描述->usbd_desc.c. 14
表9. MicroPhone Output Terminal描述->usbd_desc.c. 15
表10. Speaker Input Terminal描述->usbd_desc.c. 15
表11. Speaker Feature Unit描述->usbd_desc.c. 15
表12. Speaker Output Terminal描述->usbd_desc.c. 15
表13. MicroPhone Standard AS interface描述->usbd_desc.c. 16
表14. MicroPhone Standard AS interface描述->usbd_desc.c. 16
表15. MicroPhone Class-Specific AS Interface描述->usbd_desc.c. 16
表16. MicroPhone Type I Format type描述->usbd_desc.c. 17
表17. MicroPhone Standard Endpoint描述->usbd_desc.c. 17
表18. MicroPhone Class-specific iso Endpoint描述->usbd_desc.c. 17
表19. Speaker Standard AS interface描述->usbd_desc.c. 18
表20. Speaker Standard AS interface描述->usbd_desc.c. 18
表21. Speaker Class-Specific AS Interface描述->usbd_desc.c. 18
表22. Speaker Type I Format type描述->usbd_desc.c. 18
表23. Speaker Standard Endpoint描述->usbd_desc.c. 19
表24. Speaker Class-specific iso Endpoint描述->usbd_desc.c. 19
表25. Speaker Feed back Endpoint描述->usbd_desc.c. 19
图目录
图12. AT-START-F403A V1.0 实验板... 22
- usbd_drivers 协议库
这部分主要介绍AT32 USB库的结构和库的使用方法,AT32 USB是基于USB2.0全速设备,不支持USB2.0高速设备。这里库的作用是用来管理USB外设和实现USB的基本协议,使用户能够更快的上手开发。
图1. USB应用结构
图2. AT32工程结构
- at3 usb 库文件
表1. USB库包含的文件列表
文件名 | 内容 |
usbd_core | 实现USB Specification 协议 |
usbd_int | USB中断处理函数 |
usbd_sdr | USB标准设备请求 |
表2. USB用户接口文件列表
文件名 | 内容 |
audio_class | audio类的有关处理函数 |
audio_ desc | 实现应用程序对audio设备的描述 |
audio例程运行在AT-START评估板上,Audio Speaker 和 MicroPhone是基于Audio Arduino Demo Board 进行实现,实验过程中需要连接AT-START和Audio Arduino Board,更多信息参考《UM_Audio Arduino Daughter Board_V1.0》。 Audio 设备基于USB Audio Device 1.0,详细内容可参考《Universal Serial Bus Device Class Define for Audio Device V1.0》.
实现一个USB Audio的复合设备,包含一个Speaker和一个MicroPhone,可以同时进行播放声音和录制声音,Speaker使用isochronous OUT传输类型,MicroPhone使用isochronous IN传输类型。
Speaker功能:
- 支持16K,48K采样率(默认只支持16K)
- 支持采样率切换
- 支持16bit采样
- 支持静音
- 支持音量调节
- 支持Feedback
MicroPhone功能:
- 支持16K,48K采样率(默认只支持16K)
- 支持采样率切换
- 支持16bit采样
- 支持静音
- 支持音量调节
注意: Speaker和Microphone的采样率必须相同,否则Speaker和MicroPhone会有一个功能不能正常使用。
-
- isochronous传输简介
Isochronous传输主要用于传输数据量大,实时性要求高的场景,可以使Host到Device,也可以是Device到Host,在传输过程当中没有握手包ACK。
如下是一个isochronous OUT传输
图3. isochronous OUT传输
如下是一个isochronous IN传输
图4. isochronous IN传输
《Universal Serial Bus Device Class Define for Audio Device》 中定义了USB Audio Device的实现。里面定义如何实现一个Audio 设备以及一些控制如声音等。
AT32 MCU通过USB与PC进行数据传输,并通过I2S与WM8988 进行通信。
图5. AT32 MCU USB Audio
MCU外设资源:
USB:
- 端点0 Control:用于USB枚举和USB 控制(音量,采样率等)
- 使用端点1作为isochronous IN:AT32 MCU发送数据到PC,MicroPhone功能
- 使用端点2作为isochronous OUT:PC发送数据到AT32 MCU,Speaker功能
I2C:
- 使用I2C1发送控制信息到音频Board
I2S:
- 使用I2S1发送数据到音频Board (Speaker)
- 使用I2S2从音频board接收数据 (MicroPhone)
DMA1:
- 使用DMA1_Channel3传输I2S1的数据
- 使用DMA1_Channel4传输I2S2的数据
TMR1:
- 使用TMR1 产生12MHz Clock 给音频Board
Audio属性:
- Audio 格式:TYPE I/PCM8 Format /Stereo
- Audio Resolution: 16Bit
- Audio Sample Frequency:16KHz/48KHz
硬件环境:
- 参考《UM_Audio Arduino Daughter Board_V1.0》
- USB外设的初始化,如时钟配置、端点配置、端点缓冲区的配置
- USB作为Speaker设备的描述
- USB作为MicroPhone设备的描述
-
- USB时钟配置
-
USB 48MHz时钟来源:
USB时钟根据PLLCLK分频得到,分频参数可以设置为0, 1, 1.5, 2, 2.5, 3, 3.5, 4
图6. PLLCLK分频得到USB时钟
代码实现:
根据不同的系统频率,配置不同的USB分频参数。
图7. 配置不同的USB分频参数
void usb_clock48m_select(usb_clk48_s clk_s) { if(clk_s == USB_CLK_HICK) { crm_usb_clock_source_select(CRM_USB_CLOCK_SOURCE_HICK); /* enable the acc calibration ready interrupt */ crm_periph_clock_enable(CRM_ACC_PERIPH_CLOCK, TRUE); /* update the c1\c2\c3 value */ acc_write_c1(7980); acc_write_c2(8000); acc_write_c3(8020); /* open acc calibration */ acc_calibration_mode_enable(ACC_CAL_HICKTRIM, TRUE); } else { switch(system_core_clock) { /* 48MHz */ case 48000000: crm_usb_clock_div_set(CRM_USB_DIV_1); break; /* 72MHz */ case 72000000: crm_usb_clock_div_set(CRM_USB_DIV_1_5); break; …/* 其他主频 */ default: break; } } } |
-
-
- USB端点初始化
-
对要使用到的端点进行初始化,包括端点类型和端点最大支持的长度等。端点初始化在设备收到主机发送Reset信号时进行初始化。
图8. 端点初始化
usb_sts_type class_init_handler(void *udev) { usb_sts_type status = USB_OK; usbd_core_type *pudev = (usbd_core_type *)udev;
/* enable microphone in endpoint double buffer mode */ usbd_ept_dbuffer_enable(pudev, USBD_AUDIO_MIC_IN_EPT);
/* open microphone in endpoint */ usbd_ept_open(pudev, USBD_AUDIO_MIC_IN_EPT, EPT_ISO_TYPE, AUDIO_MIC_IN_MAXPACKET_SIZE);
/* enable speaker out endpoint double buffer mode */ usbd_ept_dbuffer_enable(pudev, USBD_AUDIO_SPK_OUT_EPT);
/* open speaker out endpoint */ usbd_ept_open(pudev, USBD_AUDIO_SPK_OUT_EPT, EPT_ISO_TYPE, AUDIO_SPK_OUT_MAXPACKET_SIZE); /* enable speaker feedback endpoint double buffer mode */ usbd_ept_dbuffer_enable(pudev, USBD_AUDIO_FEEDBACK_EPT);
/* open speaker feedback endpoint */ usbd_ept_open(pudev, USBD_AUDIO_FEEDBACK_EPT, EPT_ISO_TYPE, AUDIO_FEEDBACK_MAXPACKET_SIZE);
/* start receive speaker out data */ usbd_ept_recv(pudev, USBD_AUDIO_SPK_OUT_EPT, audio_struct.audio_spk_data, AUDIO_SPK_OUT_MAXPACKET_SIZE);
return status; } |
#define EPT0_TX_ADDR 0x40 /*!< usb endpoint 0 tx buffer address offset */ #define EPT0_RX_ADDR 0x80 /*!< usb endpoint 0 rx buffer address offset */ #define EPT1_TX_ADDR 0xC0 /*!< usb endpoint 1 tx buffer address offset */ #define EPT1_RX_ADDR 0x100 /*!< usb endpoint 1 rx buffer address offset */ #define EPT2_TX_ADDR 0x140 /*!< usb endpoint 2 tx buffer address offset */ #define EPT2_RX_ADDR 0x180 /*!< usb endpoint 2 rx buffer address offset */ |
-
-
- Speaker 和 MicroPhone 设备配置
-
主要实现对设备的描述,主机可以通过设备描述了解设备的具体功能。对应Speaker和MicroPhone的功能,用户可以选择具体需要那个功能或者两个功能都需要,设备的描述会根据当前配置进行修改。
功能配置
可以通过audio_conf.h中配置当前需要实现的功能。配置包括是否实现Speaker和MicroPhone,是否支持16KHz和48KHz采样率。配置代码如下:
图9. 功能配置
#define AUDIO_SUPPORT_SPK 1 #define AUDIO_SUPPORT_MIC 1 #define AUDIO_SUPPORT_FEEDBACK 1 /*配置是否支持Speaker、Micrphone、Feedback */ #define AUDIO_SUPPORT_FREQ_16K 1 #define AUDIO_SUPPORT_FREQ_48K 1 /*配置是否支持16K或者48K采用率*/ #define AUDIO_SUPPORT_FREQ (AUDIO_SUPPORT_FREQ_16K + \ AUDIO_SUPPORT_FREQ_48K \ )
#define AUDIO_FREQ_16K 16000 #define AUDIO_FREQ_48K 48000 #define AUDIO_BITW_16 16 /*如下为当前默认配置*/ #define AUDIO_MIC_CHANEL_NUM 2 #define AUDIO_MIC_DEFAULT_BITW AUDIO_BITW_16 #define AUDIO_SPK_CHANEL_NUM 2 #define AUDIO_SPK_DEFAULT_BITW AUDIO_BITW_16 #define AUDIO_SUPPORT_MAX_FREQ 48 #define AUDIO_DEFAULT_FREQ AUDIO_FREQ_48K #define AUDIO_DEFAULT_BITW AUDIO_BITW_16 |
Audio设备描述->usbd_desc.c
表3. Audio设备描述->usbd_desc.c
字段 | 值 | 描述 |
bLength | 0x12 | Size of this descriptor in bytes |
bDescriptorType | 0x01 | DEVICE Descriptor type |
bcdUSB | 0x0200 | USB Specification Release Number |
bDeviceClass | 0x00 | Device defined at interface level |
bDeviceSubClass | 0x00 | Unused |
bDeviceProtocol | 0x00 | Unused |
bMaxPacketSize0 | 0x40 | Maximum packet size for endpoint 0 0x40 bytes |
idVendor | 0x2E3C | Vendor ID |
idProduct | 0x5730 | Product ID |
bcdDevice | 0x0200 | Device release number |
iManufacturer | 0x01 | Index of string descriptor describing Manufacturer |
iProduct | 0x02 | Index of string descriptor describing product |
iSerialNumber | 0x03 | Index of string descriptor describing the device serial number |
bNumConfigurations | 0x01 | Number of possible configurations |
Audio 配置描述->usbd_desc.c
表4. Audio 配置描述->usbd_desc.c
字段 | 值 | 描述 |
bLength | 0x09 | Size of this descriptor in bytes |
bDescriptorType | 0x02 | CONFIGURATION Descriptor type |
wTotalLength | 0xC0(注释1) | Total length of data retured for this configuration |
bNumberInterface | 0x02(注释2) | Number of interfaces supported of this configuration |
bConfigurationValue | 0x01 | Value to use as SetConfiguration() argument |
iConfiguration | 0x00 | Index of string descriptor describing this configuration |
bmAttributes | 0xc0 | Configuration characteristics |
bMaxPower | 0x32 | bMaxPower |
注释1: wTotalLength根据配置不同而不同(支持的采样率,是否支持Micpone/Speaker)
注释2: bNumberInterface根据要是实现的功能不同而不同(是否支持Micpone/Speaker)
标准AC接口描述->usbd_desc.c
表5. 标准AC接口描述->usbd_desc.c
字段 | 值 | 描述 |
bLength | 0x0A | Size of this descriptor in bytes |
bDescriptorType | 0x04 | INTERFACE Descriptor type |
bInterfaceNumber | 0x00 | Number of this interface |
bAlternateSetting | 0x00 | Index of this setting |
bNumEndpoints | 0x00 | 0 endpoint |
bInterfaceClass | 0x01 | AUDIO |
bInterfaceSubClass | 0x01 | AUDIO CONTROL |
bInterfaceProtocol | 0x00 | Protocol code(assigned by the USB) |
iInterface | 0x00 | Index of string descriptor describing this interface |
Audio Class-specific AC接口描述->usbd_desc.c
表6. Audio Class-specific AC接口描述->usbd_desc.c
字段 | 值 | 描述 |
bLength | 0x0A | Size of this descriptor |
bDescriptorType | 0x24 | CS_INTERFACE descriptor type |
bDescriptorSubtype | 0x01 | HEADER descriptor subtype |
bcdADC | 0x0100 | Audio Device Class Specificaion Release Number |
wTotalLength | 0x46 | Total number of bytes returned for class-specific |
bIncollection | 0x02 | The number of AudioStreaming and MIDIStreaming interfaces |
baInterfaceNr | 0x02 | Interface number of the first AudioStreaming |
baInterfaceNr | 0x01 | Interface number of the second AudioStreaming |
MicroPhone Input Terminal描述->usbd_desc.c
表7. MicroPhone Input Terminal描述->usbd_desc.c
字段 | 值 | 描述 |
bLength | 0x0C | Size of this descriptor,in bytes. |
bDescriptorType | 0x24 | CS_INTERFACE descriptor type |
bDescriptorSubtype | 0x02 | INPUT_TERMINAL descriptor subtype |
bTerminalID | 0x01 | ID of this Input Terminal |
wTerminalType | 0x0201 | Terminal is Microphone |
bAssocTerminal | 0x00 | No association |
bNrChannels | 0x02 | Two channel |
wChannelConfig | 0x0003 | Stereo |
iChannelNames | 0x00 | Unused |
iTerminal | 0x00 | Unused |
MicroPhone Feature Unit描述->usbd_desc.c
表8. MicroPhone Feature Unit描述->usbd_desc.c
字段 | 值 | 描述 |
bLength | 0x09 | Size of this descriptor,in bytes. |
bDescriptorType | 0x24 | CS_INTERFACE descriptor type |
bDescriptorSubtype | 0x06 | FEATURE Unit ID descriptor subtype |
bUnitID | 0x02 | ID of this Unit ID |
bSourceID | 0x01 | Input Terminal ID |
bControlSize | 0x01 | Control size |
bmaControl | 0x01 | MUTE is Support |
bmaControl | 0x02 | Volume is Support |
iTerminal | 0x00 | Unused |
MicroPhone Output Terminal描述->usbd_desc.c
表9. MicroPhone Output Terminal描述->usbd_desc.c
字段 | 值 | 描述 |
bLength | 0x09 | Size of this descriptor,in bytes. |
bDescriptorType | 0x24 | CS_INTERFACE descriptor type |
bDescriptorSubtype | 0x03 | OUTPUT_TERMINAL descriptor subtype |
bTerminalID | 0x03 | ID of this Output Terminal |
wTerminalType | 0x0101 | USB Streaming |
bAssocTerminal | 0x00 | Unused |
bSourceID | 0x02 | Feature Unit ID |
iTerminal | 0x00 | Unused |
Speaker Input Terminal描述->usbd_desc.c
表10. Speaker Input Terminal描述->usbd_desc.c
字段 | 值 | 描述 |
bLength | 0x0C | Size of this descriptor,in bytes. |
bDescriptorType | 0x24 | CS_INTERFACE descriptor type |
bDescriptorSubtype | 0x02 | INPUT_TERMINAL descriptor subtype |
bTerminalID | 0x04 | ID of this Input Terminal |
wTerminalType | 0x0101 | USB Streaming |
bAssocTerminal | 0x00 | No association |
bNrChannels | 0x02 | Two channel |
wChannelConfig | 0x0003 | Stereo |
iChannelNames | 0x00 | Unused |
iTerminal | 0x00 | Unused |
Speaker Feature Unit描述->usbd_desc.c
表11. Speaker Feature Unit描述->usbd_desc.c
字段 | 值 | 描述 |
bLength | 0x09 | Size of this descriptor,in bytes. |
bDescriptorType | 0x24 | CS_INTERFACE descriptor type |
bDescriptorSubtype | 0x06 | FEATURE Unit ID descriptor subtype |
bUnitID | 0x05 | ID of this Unit ID |
bSourceID | 0x04 | Input Terminal ID |
bControlSize | 0x01 | Control size |
bmaControl | 0x01 | MUTE is Support |
bmaControl | 0x02 | Volume is Support |
iTerminal | 0x00 | Unuse |
Speaker Output Terminal描述->usbd_desc.c
表12. Speaker Output Terminal描述->usbd_desc.c
字段 | 值 | 描述 |
bLength | 0x09 | Size of this descriptor,in bytes. |
bDescriptorType | 0x24 | CS_INTERFACE descriptor type |
bDescriptorSubtype | 0x03 | OUTPUT_TERMINAL descriptor subtype |
bTerminalID | 0x06 | ID of this Output Terminal |
wTerminalType | 0x0301 | Speaker |
bAssocTerminal | 0x00 | Unused |
bSourceID | 0x05 | Feature Unit ID |
iTerminal | 0x00 | Unused |
MicroPhone Standard AS interface描述->usbd_desc.c
表13. MicroPhone Standard AS interface描述->usbd_desc.c
字段 | 值 | 描述 |
bLength | 0x09 | Size of this descriptor,in bytes. |
bDescriptorType | 0x04 | CS_INTERFACE descriptor type |
bInterfaceNumber | 0x01 | Index of this interface |
bAlternateSetting | 0x00 | Index of this alternate setting |
bNumEndpoint | 0x00 | 0 endpoint |
bInterfaceClass | 0x01 | AUDIO |
bInterfaceSubClass | 0x02 | AUDIO_STREAMING |
bInterfaceProtocol | 0x00 | Unused |
iInterface | 0x00 | Unused |
MicroPhone Standard AS interface描述->usbd_desc.c
表14. MicroPhone Standard AS interface描述->usbd_desc.c
字段 | 值 | 描述 |
bLength | 0x09 | Size of this descriptor,in bytes. |
bDescriptorType | 0x04 | CS_INTERFACE descriptor type |
bInterfaceNumber | 0x01 | Index of this interface |
bAlternateSetting | 0x01 | Index of this alternate setting |
bNumEndpoint | 0x01 | 1 endpoint |
bInterfaceClass | 0x01 | AUDIO |
bInterfaceSubClass | 0x02 | AUDIO_STREAMING |
bInterfaceProtocol | 0x00 | Unused |
iInterface | 0x00 | Unused |
MicroPhone Class-Specific AS Interface描述->usbd_desc.c
表15. MicroPhone Class-Specific AS Interface描述->usbd_desc.c
字段 | 值 | 描述 |
bLength | 0x07 | Size of this descriptor,in bytes. |
bDescriptorType | 0x24 | CS_INTERFACE descriptor type |
bDescriptorSubtype | 0x01 | GENERAL subtype |
bTerminalLink | 0x03 | Unit ID of the Output Terminal |
bDelay | 0x01 | Interface delay |
wFormatTag | 0x0001 | PCM Format |
MicroPhone Type I Format type描述->usbd_desc.c
表16. MicroPhone Type I Format type描述->usbd_desc.c
字段 | 值 | 描述 |
bLength | 0x0E | Size of this descriptor,in bytes. |
bDescriptorType | 0x24 | CS_INTERFACE descriptor type |
bDescriptorSubtype | 0x02 | FORMAT_TYPE subtype |
bFormatType | 0x01 | FORMAT_TYPE_I |
bNrChannels | 0x02 | Two Channel |
bSubFrameSize | 0x02 | Two bytes per audio subframe |
bBitResolution | 16 | 16 bits per sample |
bSamFreqType | 2 | Two frequency supported |
tSamFreq | 0x003E80 | 16000HZ |
tSamFreq | 0x00BB80 | 48000HZ |
MicroPhone Standard Endpoint描述->usbd_desc.c
表17. MicroPhone Standard Endpoint描述->usbd_desc.c
字段 | 值 | 描述 |
bLength | 0x09 | Size of this descriptor,in bytes. |
bDescriptorType | 0x05 | ENDPOINT descriptor |
bEndpointAddress | 0x81 | IN Endpoint 1 |
bmAttributes | 0x05 | Isochronous |
wMaxPacketSize | 0x0120 | 288 bytes per packet |
bInterval | 0x01 | One packet per frame |
bRefresh | 0x00 | Unused |
bSynchAddress | 0x00 | Unused |
MicroPhone Class-specific iso Endpoint描述->usbd_desc.c
表18. MicroPhone Class-specific iso Endpoint描述->usbd_desc.c
字段 | 值 | 描述 |
bLength | 0x07 | Size of this descriptor,in bytes. |
bDescriptorType | 0x25 | ENDPOINT descriptor |
bDescriptorSubtype | 0x01 | GENERAL subtype |
bmAttributes | 0x00 | No sampling frequency control |
bLockDelayUnits | 0x00 | Unused |
wLockDelay | 0x0000 | Unused |
Speaker Standard AS interface描述->usbd_desc.c
表19. Speaker Standard AS interface描述->usbd_desc.c
字段 | 值 | 描述 |
bLength | 0x09 | Size of this descriptor,in bytes. |
bDescriptorType | 0x04 | CS_INTERFACE descriptor type |
bInterfaceNumber | 0x02 | Index of this interface |
bAlternateSetting | 0x00 | Index of this alternate setting |
bNumEndpoint | 0x00 | 0 endpoint |
bInterfaceClass | 0x01 | AUDIO |
bInterfaceSubClass | 0x02 | AUDIO_STREAMING |
bInterfaceProtocol | 0x00 | Unused |
iInterface | 0x00 | Unused |
Speaker Standard AS interface描述->usbd_desc.c
表20. Speaker Standard AS interface描述->usbd_desc.c
字段 | 值 | 描述 |
bLength | 0x09 | Size of this descriptor,in bytes. |
bDescriptorType | 0x04 | CS_INTERFACE descriptor type |
bInterfaceNumber | 0x02 | Index of this interface |
bAlternateSetting | 0x01 | Index of this alternate setting |
bNumEndpoint | 0x02 | 2 endpoint,feed back |
bInterfaceClass | 0x01 | AUDIO |
bInterfaceSubClass | 0x02 | AUDIO_STREAMING |
bInterfaceProtocol | 0x00 | Unused |
iInterface | 0x00 | Unused |
Speaker Class-Specific AS Interface描述->usbd_desc.c
表21. Speaker Class-Specific AS Interface描述->usbd_desc.c
字段 | 值 | 描述 |
bLength | 0x07 | Size of this descriptor,in bytes. |
bDescriptorType | 0x24 | CS_INTERFACE descriptor type |
bDescriptorSubtype | 0x01 | GENERAL subtype |
bTerminalLink | 0x04 | ID |
bDelay | 0x01 | Interface delay |
wFormatTag | 0x0001 | PCM Format |
Speaker Type I Format type描述->usbd_desc.c
表22. Speaker Type I Format type描述->usbd_desc.c
字段 | 值 | 描述 |
bLength | 0x0E | Size of this descriptor,in bytes. |
bDescriptorType | 0x24 | CS_INTERFACE descriptor type |
bDescriptorSubtype | 0x02 | FORMAT_TYPE subtype |
bFormatType | 0x01 | FORMAT_TYPE_I |
bNrChannels | 0x02 | Two Channel |
bSubFrameSize | 0x02 | Two bytes per audio subframe |
bBitResolution | 16 | 16 bits per sample |
bSamFreqType | 2 | Two frequency supported |
tSamFreq | 0x003E80 | 16000HZ |
tSamFreq | 0x00BB80 | 48000HZ |
Speaker Standard Endpoint描述->usbd_desc.c
表23. Speaker Standard Endpoint描述->usbd_desc.c
字段 | 值 | 描述 |
bLength | 0x09 | Size of this descriptor,in bytes. |
bDescriptorType | 0x05 | ENDPOINT descriptor |
bEndpointAddress | 0x02 | OUT Endpoint 2 |
bmAttributes | 0x05 | Isochronous |
wMaxPacketSize | 0x0120 | 288 bytes per packet |
bInterval | 0x01 | One packet per frame |
bRefresh | 0x00 | Unused |
bSynchAddress | 0x83 | Feed back endpoint 3 |
Speaker Class-specific iso Endpoint描述->usbd_desc.c
表24. Speaker Class-specific iso Endpoint描述->usbd_desc.c
字段 | 值 | 描述 |
bLength | 0x07 | Size of this descriptor,in bytes. |
bDescriptorType | 0x25 | ENDPOINT descriptor |
bDescriptorSubtype | 0x01 | GENERAL subtype |
bmAttributes | 0x00 | No sampling frequency control |
bLockDelayUnits | 0x00 | Unused |
wLockDelay | 0x0000 | Unused |
Speaker Feed back Endpoint描述->usbd_desc.c
表25. Speaker Feed back Endpoint描述->usbd_desc.c
字段 | 值 | 描述 |
bLength | 0x09 | Size of this descriptor,in bytes. |
bDescriptorType | 0x05 | ENDPOINT descriptor |
bEndpointAddress | 0x83 | IN Endpoint 3 |
bmAttributes | 0x11 | Isochronous |
wMaxPacketSize | 0x0003 | 3 bytes per packet |
bInterval | 0x01 | One packet per frame |
bRefresh | 0x08 | Refresh time |
bSynchAddress | 0x00 | Unused |
-
-
- MicroPhone 数据处理
-
Mircphone数据从USB 设备端发送到HOST端,发送数据频率为1ms发送一帧数据,一帧数据的大小根据采样频率和位宽决定。一帧数据大小=(采样频率 / 1000 ) * (位宽 / 8 ) * 通道数。Demo通过端点1进行IN传输。
如下是软件实现过程:
图10. MicroPhone 数据处理
usb_sts_type class_in_handler(void *udev, uint8_t ept_num) { usb_sts_type status = USB_OK; uint32_t len = 0;
/* ...user code... trans next packet data */ if((ept_num & 0x7F) == (USBD_AUDIO_MIC_IN_EPT & 0x7F)) /*如果是端点1 */ { len = audio_codec_mic_get_data(audio_struct.audio_mic_data); /*获取当前帧数据长度*/ usbd_ept_send(udev, USBD_AUDIO_MIC_IN_EPT, audio_struct.audio_mic_data, len); /*发送数据到HOST*/ }
else if((ept_num & 0x7F) == (USBD_AUDIO_FEEDBACK_EPT & 0x7F)) /*如果是端点3 */ { len = audio_codec_spk_feedback(audio_struct.audio_feed_back); /*获取当前帧数据长度*/ usbd_ept_send(udev, USBD_AUDIO_FEEDBACK_EPT, audio_struct.audio_feed_back, len); /*发回当前feedback*/ }
return status; } |
-
-
- Speaker 数据处理
-
Speaker数据从HOST端发送到设备端,发送数据频率为1ms发送一帧数据,一帧数据的大小根据采样频率和位宽决定。一帧数据大小=(采样频率 / 1000 ) * (位宽 / 8 ) * 通道数。Demo通过端点2进行OUT传输,由于存在时钟不同步的问题,Speaker加上了Feedback功能,Feedback反映一段时间内设备的真实采样率,主机根据设备反馈的真实采样率,调整发数据的频率。详见class_in_handler(void *udev, uint8_t ept_num);
如下是软件实现过程:
图11. Speaker 数据处理
usb_sts_type class_out_handler(void *udev, uint8_t ept_num) { usb_sts_type status = USB_OK; usbd_core_type *pudev = (usbd_core_type *)udev; uint16_t g_rxlen;
/* get endpoint receive data length */ g_rxlen = usbd_get_recv_len(pudev, ept_num);
if((ept_num & 0x7F) == (USBD_AUDIO_SPK_OUT_EPT & 0x7F)) /*如果是端点2 */ { /* speaker data*/ audio_codec_spk_fifo_write(audio_struct.audio_spk_data, g_rxlen); /*将数据写入播放缓冲区*/
/* get next data */ usbd_ept_recv(pudev, USBD_AUDIO_SPK_OUT_EPT, audio_struct.audio_spk_data, AUDIO_SPK_OUT_MAXPACKET_SIZE); /*获取Speaker数据*/ } return status; } |
- USB Audio测试使用说明
本节介绍如何使用USB Audio Demo进行播放声音和进行录音。
- 指示灯 LED2/LED3/LED4
- USB(PA11/PA12)
- AT-START-F403A V1.0 实验板
图12. AT-START-F403A V1.0 实验板
本节介绍如何使用USB Audio Demo进行播放声音和进行录音。
- AT32-Audio-EV V2.0
图13. AT32-Audio-EV V2.0
本节介绍如何使用USB Audio Demo进行播放声音和进行录音。
连接AT-START-F403A V1.0和AT32-Audio-EV V2.0
下载Audio Demo到实验板(工程路径:)
AN0013_SourceCode_V2.0.0\utilities\AN0013_demo连接USB到PC Host(目前测试在WIN7系统下)
图14. 连接USB到PC Host
- 如果PC成功识别到Audio设备,在声音控制器里面会看到AT32 Audio的设备,分别是一个扬声器和一个麦克风的设备。
图15. PC端识别到扬声器和麦克风设备
打开Windows Media Player(或者其它播放软件),随意播放一首音乐,可通过耳机听到播放的声音。
图16. 播放软件播放声音
通过Windows自带“录音机”进行测试,测试步骤:
- 找到Window->”附件”->“录音机”,右键->属性
图17. 录音机软件开启
- 在目标选项里加入”/file outputfile.wav”,点击应用确定
图18. 录音机属性配置
- 打开录音机->开始录音
图19. 开始录音
保存后即可播放所录制的音频
表26. 文档版本历史
日期 | 版本 | 变更 |
2022.01.17 | 2.0.0 | 最初版本 |