Implementation. STMicroelectronics STSW-STM32121
Anuncio
Anuncio
UM0424 USB voice speaker demo
9.4.2 Implementation
This section describes the hardware and software solution used to implement a USB audio speaker using the STM32 microcontroller.
Hardware implementation
●
●
In the case of the STM3210B-EVAL board, to implement the PWM feature the following
STM32 built-in timers are used:
TIM2 in output compare timing mode to act as system timer.
TIM4 in PWM mode
In the case of the STM3210E-EVAL board, the I
2
S standalone audio peripheral directly generates the audio data.
In the case of the STM32L152-EVAL board, the embedded DAC peripheral directly generates the audio data (frame synchronization is controlled using TIM6 timer).
Firmware implementation
The aim of the STM32 speaker demo is to store the data (Audio Stream) received from the host in a specific buffer called Stream_Buffer and to use the PWM to play one stream (8-bit format) every 45.45 µs (~ 22 kHz).
● Hardware configuration interface:
The hardware configuration interface is a layer between the USB application (in our case the USB device Audio Speaker) and the internal/external hardware of the STM32 microcontroller. This internal and external hardware is managed by the STM32’s standard peripheral library, so from the firmware point of view, the hardware configuration interface is the firmware layer between the USB-FS_Device application
and the standard peripheral library. Figure 20 shows the interaction between the
different firmware components and the hardware environment.
The hardware configuration layer is represented by the two files hw_config.c
and hw_config.h
. For the USB audio speaker demo, the hardware management layer manages the following hardware requirements:
– System and USB peripheral clock configuration
– Timer configuration (when STM3210B-EVAL is used)
– I
2
S configuration (when STM3210E-EVAL is used)
– DAC and Timer configuration (when STM32L152-EVAL is used)
Doc ID 13465 Rev 12 59/85
USB voice speaker demo
Figure 20.
Hardware and firmware interaction diagram
S
TM
3
2 U
S
B-F
S
_Device
au
dio
s
pe
a
ker
U
S
B-F
S
_Device
a
pplic
a
tion
U
S
B-F
S
_Device li
b
r
a
ry
UM0424
S
TM
3
2
S
t
a
nd
a
rd peripher
a
l li
b
r
a
ry
60/85
U
S
B-F
S
_Device peripher
a
l
H
a
rdw
a
re (
S
TM
3
2F +
b
o
a
rd)
H
a
rdw
a
re config interf
a
ce
a i14 3 09c
●
●
●
Endpoint configurations:
In the STM32 USB device speaker demo, two endpoints are used to communicate with the PC host: endpoint 0 and endpoint 1. Note that endpoint 1 is an Isochronous OUT endpoint and this kind of endpoint is managed by the STM32 USB device peripheral using the double buffer mode so the firmware has to provide two data buffers in the
Packet Memory Area for this endpoint. The following C code describes the method used to configure an isochronous OUT endpoint (see the usb_prop.c
file,
Speaker_Reset () function).
/ * Initialize Endpoint 1 */
SetEPType(ENDP1, EP_ISOCHRONOUS);
SetEPDblBuffAddr(ENDP1,ENDP1_BUF0Addr,ENDP1_BUF1Addr);
SetEPDblBuffCount(ENDP1, EP_DBUF_OUT, 22);
ClearDTOG_RX(ENDP1);
ClearDTOG_TX(ENDP1);
ToggleDTOG_TX(ENDP1);
SetEPRxStatus(ENDP1, EP_RX_VALID);
SetEPTxStatus(ENDP1, EP_TX_DIS);
Class-specific request
This implementation supports only Mute control. This feature is managed by the
Mute_command function ( usb_prop.c file).
Isochronous data transfer management
As detailed before, the STM32 manages the isochronous data transfer using the double buffer mode. So to copy the received data from the PMA to the Stream_Buffer , the swapping between the two PMA buffers (ENDP1_BUF0Addr and
ENDP1_BUF1Addr) has to be managed. Swapping access to the PMA is managed according to the buffer usage between the USB peripheral and the firmware. This operation is provided by the EP1_OUT_Callback () function ( usb_endp.c file). After
Doc ID 13465 Rev 12
UM0424
Note:
USB voice speaker demo
● the end of the copy process, a global variable called IN_Data_Offset is updated by the number of bytes received and copied in the Stream_Buffer.
Audio Playing Implementation:
To play back the audio samples received from the host when using the STM3210B-
EVAL board, Timer TIM4 is programmed to generate a 125.5 kHz PWM signal and the
TIM2 is programmed to generate an interrupt at a frequency equal to 22 kHz. On each
TIM2 interrupt one Audio Stream is used to update the pulse of the PWM. A global variable ( Out_Data_Offset ) is used to point to the next Stream to play in Stream buffer.
When the I
2
S audio peripheral is used in the STM3210E-EVAL board, the
Out_Data_Offset variable controls the streaming flow to synchronize the data from the
USB with the Stream buffer used by the I
2
S peripheral.
When the DAC peripheral is used in the STM32L152-EVAL board, the
Out_Data_Offset variable controls the streaming flow to synchronize the data from the
USB with the Stream buffer used by the DAC peripheral.
Both “IN_Data_Offset” and “Out_Data_Offset” are initialized to 0 in each Start of frame interrupt (see usb_istr.c file, SOF_Callback() function) to avoid overflowing the
“Stream_Buffer”.
Doc ID 13465 Rev 12 61/85
USB voice speaker demo UM0424
Audio speaker descriptors
Table 21.
Device descriptors
Field Value bLength bDescriptortype bcdUSB bDeviceClass bDeviceSubClass bDeviceProtocol bMaxPacketSize0 idVendor idProduct bcdDevice iManufacturer iProduct iSerialNumber bNumConfigurations
0x12
0x01
0x0200
0x00
0x00
0x00
0x40
0x0483
0x5730
0x0100
0x01
0x02
0x03
0x01
Description
Size of this descriptor in bytes
Descriptor type (Device descriptor)
USB specification Release number: 2.0
Device class
Device subclass
Device protocol
Max packet size of Endpoint 0: 64 bytes;
Vendor identifier (STMicroelectronics)
Product identifier
Device release number: 1.00
Index of the manufacturer string descriptor: 1
Index of the product string descriptor: 2
Index of the serial number string descriptor: 3
Number of possible configurations: 1
Table 22.
Configuration descriptors
Field Value bLength bDescriptortype wTotalLength bNumInterfaces bConfigurationValue iConfiguration bmAttributes
Maxpower
0x09
0x02
0x006D
0x02
0x01
0x00
0x80
0x32
Description
Size of this descriptor in bytes
Descriptor type (Configuration descriptor)
Total length (in bytes) of the returned data by this descriptor (including interface endpoint descriptors)
Number of interfaces supported by this configuration (two interfaces)
Configuration value
Index of the Configuration String descriptor
Configuration characteristics: Bus powered
Maximum power consumption through USB bus: 100 mA
Table 23.
Interface descriptors
Field Value Description
USB speaker standard interface AC descriptor (Interface 0, alternate setting 0) bLength bDescriptortype bInterfaceNumber
0x09
0x04
0x00
Size of this descriptor in bytes
Descriptor type: Interface descriptor
Interface number
62/85 Doc ID 13465 Rev 12
UM0424 USB voice speaker demo
Table 23.
Interface descriptors (continued)
Field bAlternateSetting bNumEndpoints bInterfaceClass bInterfaceSubClass bInterfaceProtocol iInterface
0x00
0x00
0x01
0x01
0x00
0x00
Value Description
Alternate setting number
Number of used endpoints: 0 (only endpoint 0 is used for this interface)
Interface class: USB DEVICE CLASS AUDIO
Interface subclass: AUDIO SUBCLASS AUDIOCONTROL
Interface protocol: AUDIO PROTOCOL UNDEFINED
Index of the interface string descriptor
USB speaker class-specific AC interface descriptor bLength bDescriptortype bDescriptorSubtype bcdADC wTotalLength bInCollection baInterfaceNr
0x09
0x24
0x01
0x0100
0x0027
0x01
0x01
Size of this descriptor in bytes
Descriptor type: AUDIO INTERFACE DESCRIPTOR
TYPE
Descriptor Subtype: AUDIO CONTROL HEADER bcdADC:1.00
Total Length: 39
Number of streaming interfaces: 1 baInterfaceNr: 1
USB speaker input terminal descriptor bLength 0x0C bDescriptortype bDescriptorSubtype bTerminalID wTerminalType bAssocTerminal bNrChannels wChannelConfig iChannelNames iTerminal
0x24
0x02
0x01
0x0101
0x00
0x01
0x0000
0x00
0x00
Size of this descriptor in bytes: 12
Descriptor type: AUDIO INTERFACE DESCRIPTOR
TYPE
Descriptor Subtype: AUDIO CONTROL INPUT
TERMINAL
Terminal ID: 1
Terminal type: AUDIO TERMINAL USB STREAMING
No association
One channel
Channel Configuration: MONO
Unused
Unused
USB speaker audio feature unit descriptor bLength bDescriptortype bDescriptorSubtype bUnitID bSourceID bControlSize bmaControls
0x09
0x24
0x06
0x02
0x01
0x01
0x0001
Size of this descriptor in bytes
Descriptor type: AUDIO INTERFACE DESCRIPTOR
TYPE
DescriptorSubtype: AUDIO CONTROL FEATURE UNIT
Unit ID: 2
Source ID:1
Control Size:1
Only the control of the MUTE is supported
Doc ID 13465 Rev 12 63/85
USB voice speaker demo UM0424
Table 23.
Interface descriptors (continued)
Field Value Description iTerminal 0x00
USB speaker output terminal descriptor
Unused bLength bDescriptortype bDescriptorSubtype bTerminalID wTerminalType bAssocTerminal bSourceID iTerminal
0x09
0x24
0x03
0x03
0x0301
0x00
0x02
0x00
Size of this descriptor in bytes
Descriptor type: AUDIO INTERFACE DESCRIPTOR
TYPE
Descriptor subtype: AUDIO CONTROL OUTPUT
TERMINAL
Terminal ID: 3
Terminal Type: AUDIO TERMINAL SPEAKER
No association
Source ID:2
Unused
USB speaker standard AS interface descriptor - audio streaming zero bandwidth
(Interface 1, alternate setting 0) bLength 0x09 bDescriptortype bInterfaceNumber bAlternateSetting bNumEndpoints bInterfaceClass bInterfaceSubClass bInterfaceProtocol iInterface
0x24
0x01
0x00
0x00
0x01
0x02
0x00
0x00
Size of this descriptor in bytes
Descriptor type: AUDIO INTERFACE DESCRIPTOR
TYPE
Interface Number: 1
Alternate Setting: 0 not used (zero bandwidth)
Interface class: USB DEVICE CLASS AUDIO
Interface subclass: AUDIO SUBCLASS
AUDIOSTREAMING
Interface protocol: AUDIO PROTOCOL UNDEFINED
Unused
USB speaker standard AS interface descriptor - audio streaming operational
(Interface 1, Alternate setting 1) bLength 0x09 bDescriptortype bInterfaceNumber bAlternateSetting bNumEndpoints bInterfaceClass bInterfaceSubClass bInterfaceProtocol iInterface
0x24
0x01
0x01
0x01
0x01
0x02
0x00
0x00
Size of this descriptor in bytes
Descriptor type: AUDIO INTERFACE DESCRIPTOR
TYPE
Interface number: 1
Alternate Setting: 1
One Endpoint.
Interface class: USB CLASS AUDIO
Interface subclass: AUDIO SUBCLASS
AUDIOSTREAMING
Interface protocol: AUDIO PROTOCOL UNDEFINED
Unused
64/85 Doc ID 13465 Rev 12
UM0424 USB voice speaker demo
Table 23.
Interface descriptors (continued)
Field Value Description
USB speaker audio streaming interface descriptor bLength bDescriptortype bInterfaceNumber bAlternateSetting bNumEndpoints wFormatTag
0x07
0x24
0x01
0x01
0x01
0x0002
Size of this descriptor in bytes
Descriptor type: AUDIO INTERFACE DESCRIPTOR
TYPE
Interface number: 1
Alternate Setting: 1
One Endpoint.
PCM8 format
USB speaker audio type I format interface descriptor bLength bDescriptortype bDescriptorSubtype bFormatType bNrChannels bSubFrameSize bBitResolution bSamFreqType tSamFreq
0x0B
0x24
0x03
0x01
0x01
0x01
0x08
0x01
0x0055F0
Size of this descriptor in bytes
Descriptor type: AUDIO INTERFACE DESCRIPTOR
TYPE
Descriptor subtype: AUDIO STREAMING FORMAT TYPE
Format type: Type I
Number of channels: one channel
Subframe size: one byte per audio subframe
Bit resolution: 8 bits per sample
One frequency supported
22 kHz
Table 24.
Endpoint descriptors
Field Value Description
Endpoint 1 - standard descriptor bLength bDescriptortype bEndpointAddress bmAttributes wMaxPacketSize bInterval
0x07
0x05
0x01
0x01
0x0016
0x00
Size of this descriptor in bytes
Descriptor type (endpoint descriptor)
OUT Endpoint address 1.
Isochronous Endpoint
22 bytes
Unused
Endpoint 1 - Audio streaming descriptor bLength bDescriptortype bDescriptor bmAttributes
0x07
0x25
0x01
0x80
Size of this descriptor in bytes
Descriptor type: AUDIO ENDPOINT DESCRIPTOR TYPE
AUDIO ENDPOINT GENERAL bmAttributes : 0x80
Doc ID 13465 Rev 12 65/85
USB voice speaker demo
Table 24.
Endpoint descriptors (continued)
Field Value bLockDelayUnits wLockDelay
0x00
0x0000
Unused
Unused
Description
UM0424
66/85 Doc ID 13465 Rev 12
Descargar
Anuncio