stm32F103 usbcfg中的usb audio 发送函数,请举一个详细的例子
时间: 2024-02-15 11:06:05 浏览: 130
STM32F103固件函数库用户手册(中文).zip_STM32F101xx_STM32F103xx_stm32f103 库函数
好的,以下是一个使用USBCFG实现的USB音频发送的详细例子:
首先,在USBCFG的配置文件中,需要添加音频流输入(Audio Streaming IN)的描述符,如下所示:
```c
/* USB AUDIO device Configuration Descriptor */
const uint8_t USBD_CfgDesc[USB_AUDIO_CONFIG_DESC_SIZ] =
{
0x09, /* bLength: Configuation Descriptor size */
USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */
USB_AUDIO_CONFIG_DESC_SIZ, /* wTotalLength: configuration descriptor set total length */
0x00,
0x01, /* bNumInterfaces: 1 interface */
0x01, /* bConfigurationValue: configuration value */
0x00, /* iConfiguration: index of string descriptor describing the configuration */
0xC0, /* bmAttributes: self powered */
0x32, /* MaxPower 100 mA */
/************** Descriptor of Audio interface ********************/
/* Audio interface descriptor (9 bytes) */
0x09, /* bLength: Interface Descriptor size */
USB_DESC_TYPE_INTERFACE, /* bDescriptorType: Interface descriptor type */
0x00, /* bInterfaceNumber: Number of Interface */
0x00, /* bAlternateSetting: Alternate setting */
0x00, /* bNumEndpoints: One endpoints used */
0x01, /* bInterfaceClass: Audio */
0x01, /* bInterfaceSubClass: Audio Streaming */
0x00, /* bInterfaceProtocol: Unused */
0x00, /* iInterface: Index of string descriptor */
/****************** Descriptor of Audio Streaming ******************/
/* Audio Streaming Interface Descriptor (7bytes) */
0x07, /* bLength: Endpoint Descriptor size */
USB_AUDIO_INTERFACE_DESCRIPTOR_TYPE, /* bDescriptorType: Interface descriptor type */
USB_AUDIO_STREAMING_GENERAL, /* bDescriptorSubtype: General */
0x01, /* bTerminalLink: Terminal ID of the Output Terminal */
0x00, /* bDelay: Delay introduced by the data path */
0x01, /* wFormatTag: PCM Format */
/******************** Audio Format Descriptor **************************/
/* Audio Format Descriptor (11 bytes) */
0x0B, /* bLength: Endpoint Descriptor size */
USB_AUDIO_INTERFACE_DESCRIPTOR_TYPE, /* bDescriptorType: Interface descriptor type */
USB_AUDIO_STREAMING_FORMAT_TYPE, /* bDescriptorSubtype: Format */
0x01, /* bFormatType: Type of the format */
0x02, /* bNrChannels: Number of channels */
0x02, /* bSubFrameSize: Size of a sample */
0x10, /* bBitResolution: Number of bits per sample */
0x01, /* bSampleFrequencyType: Type of the sampling frequency */
0x40, /* tSampleFrequency: Sampling frequency */
/******************** Endpoint Descriptor **************************/
/* Endpoint 1 Descriptor (9 bytes) */
0x09, /* bLength: Endpoint Descriptor size */
USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */
AUDIO_IN_EP, /* bEndpointAddress: Endpoint Address (IN) */
0x05, /* bmAttributes: Bulk */
AUDIO_PACKET_SZE, /* wMaxPacketSize: 64 Bytes max */
0x00,
0x00, /* bInterval: Polling Interval (ignored) */
};
```
其中,`AUDIO_IN_EP`是音频流输入的端点地址,`AUDIO_PACKET_SZE`是音频数据包的大小。
接着,在USBCFG的源文件中,需要编写音频流输入发送函数,如下所示:
```c
/**
* @brief USBD_AUDIO_Sync
* Copy buffer from user into data buffer used for sync transfer
* @param pdev: device instance
* @param offset: offset in the audio buffer where the copy should be done
* @retval status
*/
uint8_t USBD_AUDIO_Sync(void *pdev, uint32_t offset)
{
// 等待直到USB准备好发送数据
while (USBD_AUDIO_StreamIn_IsReady(pdev) != USBD_OK);
// 获取音频数据缓冲区指针
uint8_t* buf = USBD_AUDIO_GetInBuffer(pdev, offset);
// 将音频数据复制到缓冲区
memcpy(buf, audio_buffer, AUDIO_BUFFER_SIZE);
// 发送音频数据包
USBD_AUDIO_StreamIn(pdev, buf, AUDIO_PACKET_SZE);
return USBD_OK;
}
```
在该示例中,`USBD_AUDIO_StreamIn_IsReady()`函数用于检查USB准备好发送数据的状态,`USBD_AUDIO_GetInBuffer()`函数用于获取音频数据缓冲区指针,`memcpy()`函数用于将音频数据复制到缓冲区,`USBD_AUDIO_StreamIn()`函数用于将音频数据包发送到主机。
需要注意的是,该示例中的`audio_buffer`是音频数据缓冲区,`AUDIO_BUFFER_SIZE`是缓冲区的大小,`AUDIO_PACKET_SZE`是音频数据包的大小。需要根据您的应用场景和需求进行更加详细的配置和设置,以保证USB音频发送的功能和稳定性。
阅读全文