NEW DATABASE - 350 MILLION DATASHEETS FROM 8500 MANUFACTURERS
AVR32716 EVK1105 AT32UC3 7817B AT32UC3A0512 AVR32 AVR32788 AVR32769 CS2200-CP - Datasheet Archive
Features · Stand Alone USB Device Audio Class Processing Application. · USB audio stream re-synchronization
AVR32716 AVR32716: AVR UC3 USB Audio Class Features · Stand Alone USB Device Audio Class Processing Application. · USB audio stream re-synchronization · Standalone - Low Memory Footprint (Code & RAM) and no Operating System dependency · Audio output over I2S using Synchronous Serial Controller (SSC) or internal Audio Bitstream DAC (ABDAC) · USB "Stereo with Microphone" USB device with HID Control with a keypad · Source Code for GCC & IAR compilers 32-bit Microcontroller 1. Introduction This application is a "Stereo headset with Microphone" USB device which demonstrates the usage of the Atmel AVR® UC3 USB AUDIO class through a real-time audio application. Application Note This application is designed to work with the EVK1105 EVK1105 evaluation kit. The application source code is provided for GCC and IARTM compilers. For more information about the AVR UC3 architecture, please refer to the appropriate documents available from http://www.atmel.com/avr32. To be able to use this document efficiently, the reader should be experienced in using USB protocols and classes. This application is based on the AVR UC3 Software Framework. The source code is available in the AVR UC3 Software Framework. Figure 1-1. Block diagram O S C 12 MHz 11.2896 MHz or 12 MHz Clock synthesizer Clocks Clocks Host ABDAC Amplifier SSC/I2S DAC USB AT32UC3 AT32UC3 GUI Controller 2. Requirements The software provided with this application note requires several components: · A computer running Microsoft® Windows® 2000/XP/Vista/7 or Linux® · AVR32Studio and the GNU toolchain (GCC) or IAR Embedded Workbench® for AVR UC3 compiler. · A JTAGICE mkII or AVROne! debugger 7817B 7817BAVR UC302/10 3. Related Parts Even if the application note is dedicated to the EVK1105 EVK1105 (AT32UC3A0512 AT32UC3A0512), this documentation also applies to the following AT32UC3 AT32UC3 parts: · AT32UC3A0xxx · AT32UC3A1xxx · AT32UC3A3xxx · AT32UC3B0xxx · AT32UC3B1xxx 4. Related Items The software provided with this application note requires several components: · AVR32 AVR32 GNU Toolchain: AVR UC3 GNU Toolchain is a set of standalone command line programs used to create applications for AVR UC3 microcontrollers (compiler, assembler, linker, debugger). or IAR Embedded Workbench for Atmel AVR UC3: IAR Embedded Workbench provides a suite of AVR UC3 development tools for embedded systems (compiler, assembler, linker, debugger). http://www.iar.com/ · · AVR UC3 Software Framework: This framework provides software drivers and libraries to build any application for AVR UC3. · USB AUDIO Stereo Headset with Microphone Software: The software on which this documentation is based. It is available in the AVR UC3 Software Framework. http://www.atmel.com · AT32UC3A0512 AT32UC3A0512 Datasheet: · AVR32788 AVR32788: AVR®32 How to use the SSC in I2S mode:This application note describes how the I²S protocol is handled on AVR UC3 devices and gives important information about how to get the best configuration for different sample rates. · 2 EVK1105 EVK1105: The EVK1105 EVK1105 is an evaluation kit and development system for the AT32UC3A0512 AT32UC3A0512 microcontroller. AVR32769 AVR32769: How to Compile the standalone AVR UC3 Software Framework in AVR32 AVR32 Studio V2: This application note describes how an AVR32Studio project can be easily created: AVR32716 AVR32716 7817B 7817BAVR UC302/10 AVR32716 AVR32716 5. Theory of Operation 5.1 Overview The USB is very well suited for transport of audio (voice and sound). In addition, the USB has more than enough bandwidth for sound, even high-quality audio. Many applications related to voice telephony, audio playback, and recording can take advantage of the USB. This application note demonstrates, through a stand alone application, the usage of the Atmel AVR UC3 USB audio class. The application operates in the USB device mode, enumerating as a "stereo headset with microphone". It uses the USB Audio and HID (Consumer) classes. An essential issue in audio is synchronization of the data streams. Indeed, the smallest artifacts are easily detected by the human ear. Therefore, a robust synchronization scheme on isochronous transfers needs to be added in order to have a high quality sound output. The USB specification describes several type of audio synchronization endpoint: · Asynchronous: Asynchronous isochronous audio endpoints produce or consume data at a rate that is locked either to a clock external to the USB or to a free-running internal clock. These endpoints cannot be synchronized to a start of frame (SOF) or to any other clock in the USB domain. · Synchronous: The clock system of synchronous isochronous audio endpoints can be controlled externally through SOF synchronization. Such an endpoint must do one of the following: · Slave its sample clock to the 1ms SOF tick. · Control the rate of USB SOF generation so that its data rate becomes automatically locked to SOF. · Adaptive: Adaptive isochronous audio endpoints are able to source or sink data at any rate within their operating range. This implies that these endpoints must run an internal process that allows them to match their natural data rate to the data rate that is imposed at their interface. The 32-bit AVR UC3 Flash Microcontroller family does not allow the use of synchronous stream. Indeed, the product is not able to slave the DAC clock to the 1ms SOF tick received in the USB device mode. The current audio class application presented here is thus using adaptive synchronization type. 5.1.1 Benefits of the Adaptive synchronization type Thanks to the Adaptive synchronization, an audio application can support several sampling frequencies (e.g. 32, 44.1 and 48 kHz). All the supported frequencies of the speaker and the microphone will be declared to the Host during the USB enumeration of the device. Then, depending on the sampling frequency of the "file" being played on the PC, the USB will ask the device to change the sampling frequency accordingly (e.g. switching from 44.1 to 48 kHz) before starting the playback. Note that you can, if you want, support only one sampling frequency for the speaker output into the final application. In that case, the operating system is in charge of resampling, if needed, the audio stream to that single frequency. This is what the current application is doing: it supports only the 44.1 kHz sampling frequency (due to the crystals configuration of the EVK1105 EVK1105). The PC will anyway be able to play 32 or 3 7817B 7817BAVR UC302/10 48 kHz files. It will resample them to 44.1 kHz before sending the digital audio stream through the USB. 5.1.2 Synchronization methods The USB specification describes 3 different type of synchronization endpoints, as said earlier in that document. Therefore, there are several methods that are usually implemented in final consumer products: · Method A: Insertion / removal of audio samples. This method is well suited for low cost designs. It consists in adding or removing samples on-the-fly in order to prevent overrun or underrun situations. This method will give good results as long as the incoming USB audio sampling frequency is very close to the DAC input clock frequency. However, if the difference between the 2 frequencies is too high, this will add audio distortion. This is one of the 3 methods covered by this application note. · Method B: Use of an external clock synthesizer to dynamically adapt the external DAC frequency. This method gives the best results. It consists in slightly increasing or decreasing the sample frequency of the DAC by few PPM in order to avoid overrun/underrun. This is undetectable by the human ear and guaranty a perfect 1 to 1 copy of the digital audio stream. This is the second method covered by this application note, achieved by adding an external component (e.g. the Cirrus Logic CS 2200). This solution gives the best performances. · Method C: Adaptive resampling. It consists in resampling the incoming stream at a frequency which may slightly change over the time, in order to avoid underrun or overrun situation. This method needs CPU resources and resampling adds distortion to the audio signal (less than the method A though). This is the last method covered by this application note. · Method D: Use of a feedback endpoint. This method, fully described in the USB specification, consists in giving to the Host an accurate indicator of the required stream rate (according to the slave). The feedback endpoint seems to not be fully supported on Windows XP. 5.1.3 Summary The pros and cons of each synchronization methods is given in the following table: 4 AVR32716 AVR32716 7817B 7817BAVR UC302/10 AVR32716 AVR32716 . Table 5-1. Pros & cons of each synchronization method Method Pros Cons Very simple. Method A: Insertion/removal of audio samples Good audio results for low-cost design: audio distortion almost not audible by the human hear. E.g. add/remove samples every 208ms for 100 ppm diff. Audio distortion audible on pure sinus wave. 12MHz and 11.2896 MHz crystal configuration: only 44.1 kHz support. Few CPU and RAM resources Very simple. Few CPU and RAM resources. Method B: Use of an external clock synthesizer Perfect audio quality: direct 1:1 audio copy from the USB to the DAC. BOM cost is increased. No distortion / no data processing Support any sampling frequencies. Method C: Adaptive resampling Good audio results, but still audio distortion due to the resampling. Needs CPU & RAM resources (see 9.3.3 Method C: Adaptive Resampling (page 21) Audio resampling implies loss of quality compared to method B. Described in the USB 2.0 standard. Method D: Use of a Feedback endpoint (under analysis) Very simple. Few CPU and RAM resources. Seems not natively supported by Windows XP Operating Systems. Should give very good audio quality (needs to be confirmed) 5.1.4 HID control of the application The application note also supports the control of applications such as Windows Media player through the use of an HID endpoint. During the USB enumeration, the device declares an HID "consumer" interface which handles typical remote control (play/pause/next/previous/volume up and down). It is thus possible to change the track (next/previous) by respectively using the right and left key of the EVK1105 EVK1105. The master volume can be controlled too. 5.1.5 Audio synchronization methods used in this application As said earlier, the application is re-synchronizing the USB audio stream thanks to 3 possible methods. As a consequence, the application is splitted into three different projects: 1. The first project is using the method A and is located here: / / 2. The second project is using the method B (through the use of an external clock synthesizer - the Cirrus Logic CS2200-CP CS2200-CP). The project is located here: / / 3. The third project is using the method C and is located here: / / 5 7817B 7817BAVR UC302/10 6. Implementation details We will describe here how are working these 3 synchronization methods. 6.1 Method A: Add/Remove of audio samples 6.1.1 Hardware consideration The EVK1105 EVK1105 does not need any hardware patch or modification to run in this configuration. 6.1.2 Supported sampling frequencies The EVK1105 EVK1105 has a 11.2896 MHz crystal that is well suited for 44.1 kHz sampling frequencies. However, this crystal can not be used to get accurate 32 kHz or 48 kHz frequencies. As a consequence, during the USB enumeration process, the UC3 will only present to the host one sampling frequency: 44.1 kHz. Any other sampling frequencies played from a PC/MAC will be re-sampled at 44.1 kHz (e.g. a 48 kHz file played on a PC will be re-sampled at 44.1 kHz). 6.1.3 Clock consideration The CPU frequency is configured to 62.09 MHz (11.2896 x 5.5). Note that with this method, the oversampling frequency sent to the external DAC (when using the SSC I2S) is fixed and exactly 11.2896 MHz (256 x 44100). 6.1.4 Algorithm The basics of the synchronization is simple: the software uses audio buffers in the internal RAM and constantly monitors the reader (the DAC) and writer (the USB) indexes. First, the system waits until half of the total number of buffers contains audio samples before starting the playback. Figure 6-1. Buffering scheme USB ip (device) USB USB_STREAM_ BUFFER_SIZE PDCA 0 DAC Reader index Bank 0 1 Every 1 ms Bank 1 read 2 Add/Remove audio samples 3 Copy in buffers t m en Incre ex USB Writer index ind PDCA Interrupt increments index USB_STREAM_ BUFFER_NUMBER Transfer on SSC/ABDAC DAC ./. N-2 Resynchronization N-1 RAM Audio Buffers UC3 Then, the playback can start and the system monitors the read and write indexes. As soon as these 2 pointers becomes too far or too close, the software will add (copy) or remove samples on a regular basis (1 stereo sample at a time), in order to avoid audible effects. 6 AVR32716 AVR32716 7817B 7817BAVR UC302/10 AVR32716 AVR32716 The period (in micro-second) of adding/removing samples is given by: 1000000 -Fusb Fdac For example, a 100 ppm difference between to the 2 frequencies will lead to an audio sample addition/removal every 208 ms. The period of the buffer indexes monitoring is configurable and should not be too fast, otherwise the control loop will be unstable. It can not be too slow either, otherwise the corrections will not prevent overrun or underrun. Considering that N represents the last sample of a received packet, the sample removal is done using the following scheme: · Sample N-2 is rebuilt and equal to the average of N, N-1, N-2 and N-3. · Sample N is moved at N-1. · The packet size is reduced of 1 sample. This is shown in the following figure: Figure 6-2. Audio sample removal Sample level Sample level N-5 N-4 N-3 N-2 N-1 N t N-5 N-4 N-3 N-2 N-1 N t The sample insertion is done using the following scheme: · Sample N is moved at N+1 · Sample N is rebuilt and equal to the average of N-1 and N+1. · The packet size is increased of 1 sample. This is shown in the following figure: Figure 6-3. Audio sample insertion Sample level Sample level N-4 N-3 N-2 N-1 N t N-4 N-3 N-2 N-1 N N+1 t 7 7817B 7817BAVR UC302/10 6.2 6.2.1 Method B: Use of an external clock synthesizer Hardware consideration The EVK1105 EVK1105 does not have any external clock synthesizer onboard. Thus, the kit must be modified in order to use this solution. The 12 MHz and 11.2896 MHz crystals must be removed, and the clock synthesizer must be connected as described on the following figure: Figure 6-4. Cirrus Logic CS2200-CP CS2200-CP connection +3V3 Cirrus Logic CS2200-CP CS2200-CP VDD Xin 0 / OSC0 PLL_CLOCK 12 MHz SDA SCL SCL CLK_OUT AD0/CS AUX_OUT XTI/REF TST_IN XTO 22 pF 12 MHz Xin 1 / OSC1 SDA GND 100 nF 22 pF The CS2200-CP CS2200-CP is an extremely versatile system clocking device that utilizes a programmable phase lock loop. The CS2200-CP CS2200-CP is based on an analog PLL architecture comprised of a DeltaSigma Fractional-N Frequency Synthesizer. This architecture allows for frequency synthesis and clock generation from a stable reference clock. This component will produce a low-jitter clock (typ 70 ps) and a very small frequency resolution (+/- 0.5 ppm). 6.2.2 Supported sampling frequencies Thanks to the CS2200-CP CS2200-CP, the application is able to manage 3 sampling frequencies: 32 kHz, 44.1 kHz and 48 kHz. This means that, during the USB enumeration process, the UC3 will declare these 3 frequencies to the USB host. Any other sampling frequencies played from a PC/MAC will be re-sampled into one of the supported frequencies (e.g. A 22.05 kHz file played on a PC will be re-sampled at 48 kHz) 6.2.3 Clock consideration The CPU frequency is not fixed and will vary according to the sampling frequency of the file being played. For a 32 kHz file, the CS2200-CP CS2200-CP will be configured to output a 8.192 MHz (32000 x 256) master clock and the CPU frequency will be 40.9 MHz (8.192 x 5). For a 44.1 kHz file, the CS2200-CP CS2200-CP will be configured to output a 11.2896 MHz (44100 x 256) master clock and the CPU frequency will be 56 MHz (11.2896 x 5). For a 48 kHz file, the CS2200-CP CS2200-CP will be configured to output a 12.288 MHz (48000 x 256) master clock and the CPU frequency will be 61 MHz (12.288 x 5). Note that with this method, the oversampling frequency sent to the external DAC (when using the SSC I2S) is exactly 256 x Fs. 6.2.4 Algorithm Here is the algorithm used by this synchronization method: 8 AVR32716 AVR32716 7817B 7817BAVR UC302/10 AVR32716 AVR32716 · The application bufferises the audio stream in a ring buffer in the internal RAM. This ring buffer represents 16 buffers (USB_STREAM_BUFFER_NUMBER) of 1 ms of data, i.e. 16 ms · The application waits that at least 8 buffers are full before launching the playback. · Each packet received from the USB is put in a buffer. This increments a writer index. · Buffer are sent to the SSC by the PDCA. This increments a reader index. · The application has been designed to support an initial frequency difference of 500 PPM. This may happen when you are connected on a PC. The USB specification allows a possible maximum frequency deviation of 500 PPM. · For that, we add +/-10 PPM every 320ms: · Buffering represents 16 ms of audio. Playback starts with half buffer: 8ms. · When input and output clock differ by 500ppm, the buffer will over/underflow after 8ms/500ppm = 16 sec. · We make correction by steps of 10ppm, we need 50 steps to correct the 500ppm difference. · These 50 corrections must be made within 16s. This means every 16/50 = 320 ms. Figure 6-5. Buffering scheme USB ip (device) USB USB_STREAM_ BUFFER_SIZE DAC Reader index Bank 0 1 Every 1 ms Bank 1 2 Copy in buffers 3 Resynchronization CS2200 CS2200 +/- 10 PPM PDCA 0 Audio Buffer level monitoring PDCA Interrupt increments index USB_STREAM_ BUFFER_NUMBER Transfer on SSC/ABDAC DAC Increment index ./. USB Writer index N-2 N-1 RAM Audio Buffers UC3 · The application periodically monitors (every 320ms) the reader and writer indexes and take actions using the following algorithm: If buffer_level > 50% and if trend is positive Then increment CS2200 CS2200 out clock by 10 ppm Else if buffer_level < 50% and if trend is negative Then decrement CS2200 CS2200 out clock by 10 ppm The trend is the tendency of the buffer level, i.e. the increase or the decrease direction. This is illustrated on the following figure: 9 7817B 7817BAVR UC302/10 Figure 6-6. RAM Buffer level with the CS2200-CP CS2200-CP clock synthesizer: RAM Buffer level Buffer level without correction 100% 50% 0% +10 +10 +10 +10 ppm ppm ppm ppm Increase freq of 10 ppm every 0.3 s No action -10 -10 -10 -10 -10 ppm ppm ppm ppm ppm Decrease freq of 10 ppm every 0.3 s No action +10 +10 +10 +10 +10 ppm ppm ppm ppm ppm No action Increase freq by 10 ppm every 0.3 s -10 -10 -10 -10 -10 ppm ppm ppm ppm ppm No action Decrease freq of 10 ppm every 0.3 s Thanks to this method, there is no data distortion, no data loss. The frequency deviation is not audible by a human hear. This method is very well suited for high end consumer audio products. 6.3 Method C: Adaptive Resampling (or Sampling Rate Conversion) 6.3.1 Hardware consideration The EVK1105 EVK1105 does not need any hardware patch or modification to run in this configuration. 6.3.2 Supported sampling frequencies The EVK1105 EVK1105 has a 11.2896 MHz crystal that is well suited for 44.1 kHz sampling frequencies. However, this crystal can not be used to get accurate 32 kHz or 48 kHz frequencies. As a consequence, during the USB enumeration process, the UC3 will only present to the host one sampling frequency: 44.1 kHz. Any other sampling frequencies played from a PC/MAC will be re-sampled at 44.1 kHz (e.g. A 48 kHz file played on a PC will be re-sampled at 44.1 kHz). 6.3.3 Clock consideration The CPU frequency is configured to 62.09 MHz (11.2896 x 5.5). Note that with this method, the oversampling frequency sent to the external DAC (when using the SSC I2S) is fixed and exactly 11.2896 MHz (256 x 44100). 6.3.4 Algorithm Here is the algorithm used by this synchronization method: The data path is composed of a number of "input" audio buffers and "output" audio buffers.The input buffers are circular buffers. Each buffer follows the following process during one loop: · 10 BUFFER_STATE_EMPTY: the buffer is empty, no valid data are inside, it is waiting to be used. AVR32716 AVR32716 7817B 7817BAVR UC302/10 AVR32716 AVR32716 · · BUFFER_STATE_FULL: the buffer is full of valid data that are waiting to be processed. · Figure 6-7. BUFFER_STATE_FILLING: the buffer is being filled by data directly taken from the USB bus (IN requests). Only one buffer can be in this state at a time. BUFFER_STATE_RESAMPLING: the buffer is being re-sampled and the output is stored into the output buffers through a temporary buffer. Buffering scheme Resampling process USB bus IN IN Buffer full every 1ms every 1ms BUFFER_STATE_RESAMPLING Resampling Input buffers Buffer full IN N BUFFER_STATE _EMPTY Buffer full I C O N D I T I O N Temp buffer Output buffers IN BUFFER_STATE _FILLING BUFFER_STATE_FULL Buffer Buffer DAC After this final state, the buffer goes back to the BUFFER_STATE_EMPTY and renews the procedure. All buffers are subdivided into channels. If the AUDIO_STREAM_MAX_NB_CHANNELS define is equal to 2, then each buffer will be subdivided into 2 channels. Figure 6-8. Audio Buffer composition 1 input buffer This buffer has 2 channels, left (1) and right (2). The size of EACH channel is equal to AUDIO_STREAM_MAX_USB_PACKET_SIZE. Therefore, the buffer itself has a size of AUDIO_STREAM_MAX_USB_PACKET_SIZE x 2 Channel 2 Channel 1 All buffers have a fixed size which is equal to AUDIO_STREAM_MAX_USB_PACKET_SIZE * AUDIO_STREAM_MAX_NB_CHANNELS (=360*2=720 bytes by default). At startup, to pre-fill the input buffer, the SYNCHRONIZATION flag is initially raised. When the number of full input buffers is enough, this flag is cleared which validate one item of the "condition" (see Condition). 6.3.4.1 Condition The condition is validated if all the following statements are true: 11 7817B 7817BAVR UC302/10 · · 6.3.4.2 The DAC is ready to receive a new buffer. The SYNCHRONIZATION flag is not raised. Resampling process This process is linear and so is processed in one time. The current full buffer will change to the state "re-sampling". During this time, the samples of each channel of the current buffer are resampled and reformatted using a temporary buffer before being stored inside the free output buffer. The temporary buffer has a fixed size. Its size is calculated from the output of a resampling buffer. For example, if the USB streaming outputs a 22050Hz sound, and we want to output on the DAC a 44100Hz sound. The ratio is 2, therefore the temporary buffer will be equal to twice the size of one channel of the input buffer. By default, one channel of the input buffer is 360, therefore, the temporary buffer will be 720 bytes. 6.3.4.3 Output buffers The output buffers can be directly used by the DAC with no need of recopying the buffer. Therefore, if used with a PDCA, the output buffers should be of a number of 2, 1 used for the load buffer and 1 for the reload buffer. Their size is also fixed and is equal to the size of the temporary buffer (see Resampling process) multiplied by the number of channels. 6.3.4.4 Over/Under-run conditions handling The over-run or under-run conditions are taken care by over or under re-sampling the stream on the fly. For each frequency supported, 3 re-sampling frequencies are defined: · The normal output frequency defining the sampling frequency at which the stream should be played when no over/under-run conditions are met: 44.1 kHz for example. · The over-sampling frequency, which will be used once the stream is facing an over-run condition. We need to produce less sample to slow-down the audio stream. The oversampling frequency can be 44 kHz for example. · The under-sampling frequency, which will be used once the stream is facing an under-run condition. We need to produce more sample to speed-up the audiostream. The oversampling frequency can be 44.2 kHz for example. To determine an over or under running condition, the algorithm will check the input buffer statuses. If there are not enough buffers full, then, it assumes to be in an over-run condition; the same applies for the under-run condition. Then, the algorithm will automatically switch from the normal output frequency to the over/undersampling frequency to recover from the unexpected frequency excursion. Therefore, these conditions depend on the number of input buffer chosen. By default, the software is configured so that the over-run condition will triggered if only 1 full buffer is left while the under-run condition triggers if (NB_INPUT_BUFFER - 1) are full. In this setup, the optimal number of input buffers is 8: 12 AVR32716 AVR32716 7817B 7817BAVR UC302/10 AVR32716 AVR32716 Figure 6-9. Overrun/Under condition Number of full (BUFFER_STATE_FULL) input buffers : 1 Goes to the over-run condition. Re-samples the stream at a higher sampling frequency. 2 3 Normal condition. Re-samples the stream at the expected frequency. 4 5 6 7 8 Goes to the under-run condition. Re-samples the stream at a lower sampling frequency. Critical case. All buffers are full. In this case the algorithm stops and wait for matching the normal condition again. To ensure a perfect transition between two re-sampled buffers at a different frequency, a polynomial interpolation is done in addition to the re-sampling for few samples. This is taken care by the DSP library. Figure 6-10. Polynomial interpolation from the DSP library: 6.4 Special consideration for the SSC module. You may have noticed that the CPU frequency is changed dynamically depending on the sampling frequency of the audio stream. This is because the SSC module derives its clock internally only from the peripheral bus. When you want to change the SSC frequency, the whole system clock needs to be changed. There is an alternative which gives the possibility to isolate the SSC module from the system clock, by using a loopback of a generic clock on the SSC RX pin. But in this case, the microphone feature can not be used. This approach is fully described in the following application note: AVR32788 AVR32788: AVR32 AVR32 How to use the SSC in I2S mode. We strongly encourage the reader to take a look into this document in order to get familiar with the UC3 SSC module. 7. Software architecture The following figure shows the basic software architecture of the application: 13 7817B 7817BAVR UC302/10 Figure 7-1. Software architecture Audio Player application audio_example.c Task (specific) USB Audio USB HID device_audio_task.c device_hid_task.c USB stream player USB Task usb_task.c USB Device task usb_device_task.c usb_stream_player.c USB request usb_standard_request.c usb_specific_request.c DRIVERS usart.c spi.c twi.c ssc_i2s.c flashc.c pm.c gpio.c usb_drv.c The application does not require any operating system to run. The main() function is in charge of calling the software «tasks» (using a scheduler) that make audio streaming, HMI, and USB management possible. There are 3 tasks: · The USB task: This task handles the USB stack and events. · The USB Audio task: this task manages the USB audio stream. · The USB HID task: This task is in charge of the HID management, which consists in sending reports depending on the keyboard events (next/previous, .). The main loop of the application is a simple free-running task scheduler: while (TRUE) { usb_task(); device_audio_task(); device_hid_task(); } 8. Source Code Architecture 8.1 Package The EVK1105-USB-AUDIO-x-y-z.zip contains projects for GCC (or AVR32Studio) and IAR. Default hardware configuration of the project is to run on Atmel AVR UC3 Evaluation Kits, although any board can be used (refer to section 8.4.3 "Board Definition Files" on page 16). Note that this application is also included into the AVR UC3 Software Framework. 8.2 Documentation For full source code documentation, please refer to the auto-generated Doxygen source code documentation found in: · / 14 AVR32716 AVR32716 7817B 7817BAVR UC302/10 AVR32716 AVR32716 8.3 Projects / Compiler The IARTM projects are located here: / / / / / / The GCC makefile are located here: / / / / / / An AVR32Studio project can be easily created by following the steps from the "AVR32769 AVR32769: How to Compile the standalone AVR UC3 Software Framework in AVR32 AVR32 Studio V2" application note. 8.4 Implementation Details The following describes the code implementation of the USB audio player running on the EVK1105 EVK1105. Other available packages are similar so you will find useful information here that applies to every project configurations. 8.4.1 Main() The main() function of the program is located in the file: · / This function will: · Initialize audio output - refer to section 8.4.4 "Audio Rendering Interface" on page 16 · Do the clock configuration · Call the USB task -refer to section 8.4.2.1 "USB" on page 16 · Call the USB Audio task. This task will handle the audio stream. · Call the USB HID task. - This task will handle the HID report. The /SERVICES/USB/CLASS/AUDIO/EXAMPLES/EXAMPLE_DEVICE/ folder contains the following files: · ./audio_example.c/.h: contains the main() function. · ./avr32_logo.c/.h: the AVR UC3 bitmap image. · ./device_audio_task.c/.h: contains the task that manages the audio stream from/to the USB. · ./device_hid_task.c/.h: contains the task that manages the HID. · ./microphone_samples.c/.h: contains pre-built microphone samples. · ./CONF/*.h: configuration file for audio, display and USB. Please refer to section 8.5 "Project Configuration" on page 18 for more information on the configuration files. 15 7817B 7817BAVR UC302/10 8.4.2 AT32UC3A AT32UC3A Drivers The firmware uses the AVR UC3 driver library available in · /UTILS/LIBS/DRIVERS/AT32UC3A/ /UTILS/LIBS/DRIVERS/AT32UC3A/. The source code can be found into /DRIVERS. 8.4.2.1 USB The USB low level driver is located in: · /DRIVERS/USBB/ 8.4.3 8.4.3.1 Board Definition Files The application is designed to run on Atmel Evaluation Kits. All projects are configured with the following define: BOARD=EVKxxxx. The EVKxxxx definition can be found in the /BOARDS/EVKxxxx directory. Board customization · For `IAR' project, open the project options (Project -> Options), choose the «C/C+ Compiler», then «Preprocessor». Modify the `BOARD=EVKxxxx' definition by `BOARD=USER_BOARD'. · For `GCC', just modify in the `config.mk' file (/ /) the DEFS definition with `-D BOARD=USER_BOARD'. 8.4.4 8.4.4.1 Audio Rendering Interface The audio DAC mixer source code is located in /SERVICES/AUDIO/AUDIO_MIXER/audio_mixer.c,h. I2S using SSC module The /COMPONENTS/AUDIO/CODEC/TLV320AIC23B/ /COMPONENTS/AUDIO/CODEC/TLV320AIC23B/ directory contains 2 drivers: a standalone release for the external DAC TLV320AIC23B TLV320AIC23B, and another with the CS2200 CS2200 support. The UC3 communicates with the TLV320AIC23B TLV320AIC23B with the Two Wire Interface (TWI). The UC3 is the TWI master. The UC3 SSC module generates I2S frames using internal DMA (PDCA) to free CPU cycles for the application. Each time a new song is played, the SSC module is configured corresponding to the sample rate of the new song. The SSC clocks are composed of a bit clock and a frame sync: · The bit clock is the clock used to transmit a bit from the audio stream. For a 44.1 kHz sample rate, the bit clock will be 44100 x 2 (stereo) x 16 (bits per channel) i.e. 1.411 MHz. · The Frame sync is equal to the sample rate frequency, i.e. 44.1 kHz taking the same example. To get accurate 44.1kHz audio frequency samples, an external 11.2896 MHz oscillator is used as input to an internal PLL and source the CPU/HSB/PBA/PBB frequency with 62.0928 MHz. The TLV320AIC23B TLV320AIC23B uses a master clock (MCLK) of 11.2896 MHz, outputted by the UC3 through a generic clock. Then, the generic clock output (PA07) is connected to the MCLK of the TLV320AIC23B TLV320AIC23B. 16 AVR32716 AVR32716 7817B 7817BAVR UC302/10 AVR32716 AVR32716 The SSC clock divider in CMR register is given by: SSC.CMR.DIV = Round (FPBA / (2 x(FSampleRateSetPoint x NumberChannel x BitsPerSamples) The real frequency sample rate output by the SSC is given by: FActualSampleRate= FPBA / (2 x SSC.CMR.DIV x NumberChannel x BitsPerSamples) Note: Note: NumberChannel = 2, BitsPerSamples = 16, FPBA = 62.0928 MHz The music interval in semitones is: MusicInterval (semitones) = LOG( ( FActualSampleRate / FSampleRateSetPoint ) / (2 1/12 ) Table 8-1. Samples rate with SSC module (using FOSC0=12MHz and FOSC1=11.2896 MHz) Sample rate set point (Hz) Actual Sample Rate (Hz) Relative Error (%) Music Interval (semitones) 8000 8018 0.23 0.04 11025 11025 0.00 0.00 16000 15095 -0.59 -0.10 22050 22050 0.00 0.00 32000 32340 1.06 0.18 44100 44100 0.00 0.00 48000 48510 1.06 0.18 For further information, please refer to the "AVR32788 AVR32788: AVR®32 How to use the SSC in I2S mode" application note. 8.4.4.2 ABDAC The ABDAC driver is located in /DRIVERS/ABDAC. The /COMPONENTS/AUDIO/AMP/TPA6130A/ /COMPONENTS/AUDIO/AMP/TPA6130A/ directory contains 2 drivers: a standalone driver for the external amplifier driver (TPA6130A TPA6130A), and a second version with the CS2200 CS2200 support. The AVR UC3 ABDAC module is using the internal DMA (PDCA) to free CPU cycles for the application. To get accurate audio frequency samples, the two external oscillators 12 MHz (OSC1) and 11.2896 MHz (OSC0) are used to source (directly or through a PLL) the ABDAC generic clock. The PLL0 output frequency is 62.0928 MHz. The PLL1 output frequency is 48 MHz (used for USB). When used, the ABDAC generic clock divider is given by: ABDAC generic clock divider = Round (FGCLKInput / (2 x 256 x (FSampleRateSetPoint ) -1) Refer to /DRIVERS/ABDAC/abdac.c for the configuration of the ABDAC generic clock. 17 7817B 7817BAVR UC302/10 Table 8-2. Samples rate with ABDAC module (using FOSC0=12MHz and FOSC1=11.2896 MHz) Sample rate set point (Hz) Actual Sample Rate (Hz) Relative Error (%) Music Interval (semitones) 8000 PLL0 8085 1.06 0.18 11025 OSC1 11025 0.00 0.00 16000 PLL1 15625 -2.34 -0.41 22050 OSC1 22050 0.00 0.00 32000 PLL1 31250 -2.34 -0.41 44100 OSC1 44100 0.00 0.00 48000 8.5 ABDAC Generic clock input frequency PLL1 46875 -2.34 -0.41 Project Configuration The configuration files are located under the / / directory and should be modified to change, separately, audio player's module configurations. Configuration files are not linked to `IAR', `GCC' or `Avr32Studio' projects. The user can alter any of them, then rebuild the entire project in order to reflect the new configuration. 8.5.1 conf_audio_mixer.h Configures all parameters relative to the audio DACs. This file is made to support multiple configurations and can be easily upgraded to handle new DACs. 8.5.2 conf_conf_et024006dhu.h Configures all parameters relative to the LCD display. 8.5.3 conf_qt60168.h Configuration of the QTouch 60168 component used on the kit. 8.5.4 conf_tlv320aic23b.h Configuration of the external I2S DAC (which pins are used and which configuration interface). 8.5.5 conf_tpa6130.h Configuration of the external audio amplifier that is connected to the ABDAC (which pins are used and which configuration interface). 8.5.6 conf_usb.h Configuration file used for the USB. Some parameters are used for the configuration of the audio stream: · · USB_STREAM_BUFFER_NUMBER: is the number of audio buffers · USB_STREAM_RESYNC_PPM_STEPS: is the step (in PPM) used for the resynchronization. · 18 USB_STREAM_BUFFER_SIZE: is the size (in bytes) of an audio buffer TIMER_USB_STREAM_MONITOR is the monitoring period (in ms) of the buffer indexes. AVR32716 AVR32716 7817B 7817BAVR UC302/10 AVR32716 AVR32716 8.6 Compiling the application The following steps show you how to build the embedded firmware according to your environment. 8.6.1 If you are using AVR32 AVR32 Studio · The application is supported by the Software Framework plugin. Open it ("Framework" then "Select Drivers/Components/Services") and simply follows the instructions. 8.6.2 If you are using the standalone GCC with the AVR UC3 GNU Toolchain · - Open a shell, go to the / / directory and type: make rebuild program run 8.6.3 If you are using IAR Embedded Workbench® for Atmel AVR UC3 · - Open IAR and load the associated IAR project of this application (located in the directory / /). · - Press the "Debug" button at the top right of the IAR interface. The project should compile. Then the generated binary file is downloaded to the microcontroller to finally switch to the debug mode. · - Click on the "Go" button in the "Debug" menu or press F5. 8.7 Running the Application Connect the EVK1105 EVK1105 board using a mini-B plug to a PC. The firmware will enumerate in the USB device mode, as a stereo headset with microphone. The PC will automatically recognize the new audio device, meaning that previous PC speaker configuration will no longer work. To listen audio, just open your favorite audio application (e.g. Windows Media Player) and play music. The sound will be output on the stereo 3.5 mm mini jack connector. In order to listen to the microphone sound, follow the following instruction: · Launch the Sound Recorder Windows application and start recording: · Speak clearly in front of the microphone. You can see the data stream activity in the Sound Recorder application. 9. Memory footprint The below table summarizes the memory footprint with various configuration. 19 7817B 7817BAVR UC302/10 9.1 GCC The GCC version used is the 4.3.2. GCC options are -O3 -fsection-anchors -ffunction-sections options. Linker options are -Wl,-direct-data,-gc-sections. 9.2 IAR The IAR version used is the 3.20A. IAR options are "High (balanced). 9.3 Summary Here is the memory used by the application under both compilers, for all projects. 9.3.1 Method A: Add/Remove samples Table 9-1. memory footprint under GCC and IAR. Unit is in Kbyte. Internal Flash Designation Flash, including: · AVR32 AVR32 logo · Fonts (8x8, 8x16) Initialized data Uninitialized data Stack HEAP used Total 9.3.2 Internal Flash GCC Internal RAM IAR 83 60 20.3 20.3 2.3 2.3 1.4 0.7 4 5.1 11.2 83 60 0.4 0,7 4 5 10.1 Method B: Clock synthesizer Table 9-2. memory footprint under GCC and IAR. Unit is in Kbyte. Internal Flash Designation Flash, including: · AVR32 AVR32 logo · Fonts (8x8, 8x16) Initialized data Uninitialized data Stack HEAP used Total 20 Internal RAM Internal RAM GCC Internal Flash Internal RAM IAR 85 62 20.3 20.3 2.3 2.3 85 1.4 0.7 4 5.1 11.2 62 0.4 0,7 4 5 10.1 AVR32716 AVR32716 7817B 7817BAVR UC302/10 AVR32716 AVR32716 9.3.3 Method C: Adaptive Resampling Table 9-3. memory footprint under GCC and IAR. Unit is in Kbyte. Internal Flash Designation Flash, including: · AVR32 AVR32 logo · Fonts (8x8, 8x16) Initialized data Uninitialized data Stack HEAP used Total Internal RAM GCC Internal Flash Internal RAM IAR 91 67 20.3 20.3 2.3 2.3 91 1.4 0.7 4 26 32.1 67 0.4 0,7 4 26 31.1 21 7817B 7817BAVR UC302/10 10. FAQ Q: How can I have the best audio quality (i.e. without distortion)? A: The best audio quality is achieved thanks to the use of an external clock synthesizer (e.g. a CL CS2200-CP CS2200-CP). In this situation, you have a perfect 1:1 audio stream transfer from the PC/MAC to the DAC, without any data loss. This method is used in high end consumer audio products. Q: What about using a USB feedback endpoint for the synchronization? This synchronization method is under study. Q: Which USB Audio Class does this application note support? The current software is using the USB Audio Class version 1. This ensures the best support of the application across the various Windows and MAC Operating Systems. Q: Can I avoid to dynamically change the CPU frequency according to the played sampling frequency (SSC I2S) ? The SSC module derives its clock internally only from the peripheral bus. When you want to change the SSC frequency, the whole system clock needs to be changed. There is a solution listed here: 6.4 "Special consideration for the SSC module." on page 13 22 AVR32716 AVR32716 7817B 7817BAVR UC302/10 Headquarters International Atmel Corporation 2325 Orchard Parkway San Jose, CA 95131 USA Tel: 1(408) 441-0311 Fax: 1(408) 487-2600 Atmel Asia Unit 1-5 & 16, 19/F BEA Tower, Millennium City 5 418 Kwun Tong Road Kwun Tong, Kowloon Hong Kong Tel: (852) 2245-6100 Fax: (852) 2722-1369 Atmel Europe Le Krebs 8, Rue Jean-Pierre Timbaud BP 309 78054 Saint-Quentin-enYvelines Cedex France Tel: (33) 1-30-60-70-00 Fax: (33) 1-30-60-71-11 Atmel Japan 9F, Tonetsu Shinkawa Bldg. 1-24-8 Shinkawa Chuo-ku, Tokyo 104-0033 Japan Tel: (81) 3-3523-3551 Fax: (81) 3-3523-7581 Technical Support avr32@atmel.com Sales Contact www.atmel.com/contacts Product Contact Web Site www.atmel.com Literature Requests www.atmel.com/literature Disclaimer: The information in this document is provided in connection with Atmel products. No license, express or implied, by estoppel or otherwise, to any intellectual property right is granted by this document or in connection with the sale of Atmel products. EXCEPT AS SET FORTH IN ATMEL'S TERMS AND CONDITIONS OF SALE LOCATED ON ATMEL'S WEB SITE, ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS, IMPLIED OR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, CONSEQUENTIAL, PUNITIVE, SPECIAL OR INCIDENTAL DAMAGES (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF PROFITS, BUSINESS INTERRUPTION, OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT, EVEN IF ATMEL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of this document and reserves the right to make changes to specifications and product descriptions at any time without notice. Atmel does not make any commitment to update the information contained herein. Unless specifically provided otherwise, Atmel products are not suitable for, and shall not be used in, automotive applications. Atmel's products are not intended, authorized, or warranted for use as components in applications intended to support or sustain life. © 2010 Atmel Corporation. All rights reserved. Atmel ®, Atmel logo and combinations thereof, AVR®, AVR® logo and others are registered trademarks or trademarks of Atmel Corporation or its subsidiaries. Windows ® and others are registered trademarks or trademarks of Microsoft Corporation in the US and/or other countries. Other terms and product names may be trademarks of others. 7817B 7817BAVR UC302/10