audio_example.c File Reference


Detailed Description

Main file of the USB AUDIO example.

Author:
Atmel Corporation: http://www.atmel.com
Support and FAQ: http://support.atmel.no/

Definition in file audio_example.c.

#include <stddef.h>
#include <stdio.h>
#include <avr32/io.h>
#include "nlao_cpu.h"
#include "nlao_usart.h"
#include "compiler.h"
#include "board.h"
#include "print_funcs.h"
#include "intc.h"
#include "gpio.h"
#include "pm.h"
#include "flashc.h"
#include "twi.h"
#include "usart.h"
#include "conf_usb.h"
#include "usb_task.h"
#include "device_audio_task.h"
#include "device_hid_task.h"
#include "host_audio_task.h"
#include "audio_example.h"
#include "controller.h"
#include "tpa6130.h"
#include "conf_tpa6130.h"
#include "et024006dhu.h"
#include "avr32_logo.h"

Go to the source code of this file.

Functions

int _init_startup (void)
 Low-level initialization routine called during startup, before the main function.
static void init_codec_gclk (void)
 Sets up generic clock for the audio codec.
static void init_hmatrix (void)
 Initializes the HSB bus matrix.
static void init_stdio (void)
 Initializes STDIO.
static void init_sys_clocks (void)
 Initializes the MCU system clocks.
static void init_twi (U32 fpba_hz)
static void init_usb_clock (void)
 Initializes the USB clock.
int main (void)
 Main function. Execution starts here.
size_t wcstombs (char *s, const wchar_t *pwcs, size_t n)
 Replaces Newlib's implementation of the standard wcstombs function because of portability issues with wchar_t.


Function Documentation

int _init_startup ( void   ) 

Low-level initialization routine called during startup, before the main function.

This version comes in replacement to the default one provided by the Newlib add-ons library. Newlib add-ons' _init_startup only calls init_exceptions, but Newlib add-ons' exception and interrupt vectors are defined in the same section and Newlib add-ons' interrupt vectors are not compatible with the interrupt management of the INTC module. More low-level initializations are besides added here.

Definition at line 363 of file audio_example.c.

References init_stdio().

00364 {
00365   // Import the Exception Vector Base Address.
00366   extern void _evba;
00367 
00368   // Load the Exception Vector Base Address in the corresponding system register.
00369   Set_system_register(AVR32_EVBA, (int)&_evba);
00370 
00371   // Enable exceptions.
00372   Enable_global_exception();
00373 
00374   // Initialize interrupt handling.
00375   INTC_init_interrupts();
00376 
00377   init_stdio();
00378 
00379   // Don't-care value for GCC.
00380   return 1;
00381 }

static void init_codec_gclk ( void   )  [static]

Sets up generic clock for the audio codec.

Definition at line 209 of file audio_example.c.

Referenced by init_sys_clocks().

00210 {
00211 #if(DEFAULT_DACS == AUDIO_MIXER_DAC_ABDAC)
00212   // Configure the ABDAC generic clock
00213   // We do not activate it here since this is done by activating the
00214   // ABDAC in the driver.
00215   pm_gc_setup(&AVR32_PM, AVR32_PM_GCLK_ABDAC,
00216     AVR32_GC_USES_OSC, AVR32_GC_USES_OSC1, 0, 0);
00217 
00218 #elif(DEFAULT_DACS == AUDIO_MIXER_DAC_AIC23B)
00219   int gc = 0;
00220   gpio_enable_module_pin(TLV320_PM_GCLK_PIN, TLV320_PM_GCLK_FUNCTION);
00221 
00222 # if(AIC23B_MCLK_HZ == 11289600)
00223   pm_gc_setup(&AVR32_PM, gc, AVR32_GC_USES_OSC, AVR32_GC_USES_OSC1, 0, 0);
00224 # elif(AIC23B_MCLK_HZ == 12000000)
00225   pm_gc_setup(&AVR32_PM, gc, AVR32_GC_USES_OSC, AVR32_GC_USES_OSC0, 0, 0);
00226 # else
00227 #   error Wrong Master clock configuration
00228 # endif
00229 
00230   pm_gc_enable(&AVR32_PM, gc);
00231 #endif
00232 }

static void init_hmatrix ( void   )  [static]

Initializes the HSB bus matrix.

Definition at line 194 of file audio_example.c.

Referenced by main().

00195 {
00196   // For the internal-flash HMATRIX slave, use last master as default.
00197   union
00198   {
00199     unsigned long                 scfg;
00200     avr32_hmatrix_scfg_t          SCFG;
00201   } u_avr32_hmatrix_scfg = {AVR32_HMATRIX.scfg[AVR32_HMATRIX_SLAVE_FLASH]};
00202   u_avr32_hmatrix_scfg.SCFG.defmstr_type = AVR32_HMATRIX_DEFMSTR_TYPE_LAST_DEFAULT;
00203   AVR32_HMATRIX.scfg[AVR32_HMATRIX_SLAVE_FLASH] = u_avr32_hmatrix_scfg.scfg;
00204 }

static void init_stdio ( void   )  [static]

Initializes STDIO.

Definition at line 306 of file audio_example.c.

References FPBA_HZ, STDIO_USART, STDIO_USART_BAUDRATE, STDIO_USART_RX_FUNCTION, STDIO_USART_RX_PIN, STDIO_USART_TX_FUNCTION, and STDIO_USART_TX_PIN.

Referenced by _init_startup().

00307 {
00308 #if (defined __GNUC__) && (defined __AVR32__)
00309 
00310   static const gpio_map_t STDIO_USART_GPIO_MAP =
00311   {
00312     {STDIO_USART_RX_PIN, STDIO_USART_RX_FUNCTION},
00313     {STDIO_USART_TX_PIN, STDIO_USART_TX_FUNCTION}
00314   };
00315 
00316   // Initialize the USART used for STDIO.
00317   set_usart_base((void *)STDIO_USART);
00318   gpio_enable_module(STDIO_USART_GPIO_MAP,
00319                      sizeof(STDIO_USART_GPIO_MAP) / sizeof(STDIO_USART_GPIO_MAP[0]));
00320   usart_init(STDIO_USART_BAUDRATE);
00321 
00322 #elif (defined __ICCAVR32__)
00323 
00324   static const gpio_map_t STDIO_USART_GPIO_MAP =
00325   {
00326     {STDIO_USART_RX_PIN, STDIO_USART_RX_FUNCTION},
00327     {STDIO_USART_TX_PIN, STDIO_USART_TX_FUNCTION}
00328   };
00329 
00330   static const usart_options_t STDIO_USART_OPTIONS =
00331   {
00332     .baudrate     = STDIO_USART_BAUDRATE,
00333     .charlength   = 8,
00334     .paritytype   = USART_NO_PARITY,
00335     .stopbits     = USART_1_STOPBIT,
00336     .channelmode  = USART_NORMAL_CHMODE
00337   };
00338 
00339   // Initialize the USART used for STDIO.
00340   extern volatile avr32_usart_t *volatile stdio_usart_base;
00341   stdio_usart_base = STDIO_USART;
00342   gpio_enable_module(STDIO_USART_GPIO_MAP,
00343                      sizeof(STDIO_USART_GPIO_MAP) / sizeof(STDIO_USART_GPIO_MAP[0]));
00344   usart_init_rs232(STDIO_USART, &STDIO_USART_OPTIONS, FPBA_HZ);
00345 
00346 #endif
00347 }

static void init_sys_clocks ( void   )  [static]

Initializes the MCU system clocks.

Definition at line 243 of file audio_example.c.

References FMCK_HZ, FPBA_HZ, g_fcpu_hz, g_fhsb_hz, g_fpba_hz, g_fpbb_hz, init_codec_gclk(), init_usb_clock(), and SYS_CLOCK_PLL_MUL.

Referenced by main().

00244 {
00245     // Switch to OSC0 to speed up the booting
00246     pm_switch_to_osc0(&AVR32_PM, FOSC0, OSC0_STARTUP);
00247 
00248     // Start oscillator1
00249     pm_enable_osc1_crystal(&AVR32_PM, FOSC1);
00250     //
00251     pm_enable_clk1(&AVR32_PM, OSC1_STARTUP);
00252 
00253     // Set PLL0 (fed from OSC1 = 11.2896 MHz) to 112.896 MHz
00254     // We use OSC1 since we need a correct master clock for the SSC module to generate
00255     // the correct sample rate
00256     pm_pll_setup(&AVR32_PM, 0,  // pll.
00257       SYS_CLOCK_PLL_MUL-1,   // mul.
00258       1,   // div.
00259       1,   // osc.
00260       16); // lockcount.
00261 
00262     // Set PLL operating range and divider (fpll = fvco/2)
00263     // -> PLL0 output = 62.0928 MHz
00264     pm_pll_set_option(&AVR32_PM, 0, // pll.
00265       1,  // pll_freq.
00266       1,  // pll_div2.
00267       0); // pll_wbwdisable.
00268 
00269     // start PLL0 and wait for the lock
00270     pm_pll_enable(&AVR32_PM, 0);
00271     pm_wait_for_pll0_locked(&AVR32_PM);
00272     // Set all peripheral clocks torun at master clock rate
00273     pm_cksel(&AVR32_PM,
00274       0,   // pbadiv.
00275       0,   // pbasel.
00276       0,   // pbbdiv.
00277       0,   // pbbsel.
00278       0,   // hsbdiv.
00279       0);  // hsbsel.
00280 
00281     // Set one waitstate for the flash
00282     flashc_set_wait_state(1);
00283 
00284     // Switch to PLL0 as the master clock
00285     pm_switch_to_clock(&AVR32_PM, AVR32_PM_MCCTRL_MCSEL_PLL0);
00286 
00287 #if (defined USB_RESYNC_METHOD) && (USB_RESYNC_METHOD == USB_RESYNC_METHOD_EXT_CLOCK_SYNTHESIZER)
00288     // Holds frequencies parameters
00289     g_fcpu_hz = g_fhsb_hz = g_fpba_hz = g_fpbb_hz = FMCK_HZ(11289600);
00290 #endif
00291 
00292 #if (defined __GNUC__) && (defined __AVR32__)
00293   // Give the used PBA clock frequency to Newlib, so it can work properly.
00294   set_cpu_hz(FPBA_HZ);
00295 #endif
00296   init_usb_clock();
00297   init_codec_gclk();
00298 }

static void init_twi ( U32  fpba_hz  )  [static]

Definition at line 433 of file audio_example.c.

References AIC23B_TWI, AIC23B_TWI_ADDRESS, AIC23B_TWI_MASTER_SPEED, AIC23B_TWI_SCL_FUNCTION, AIC23B_TWI_SCL_PIN, AIC23B_TWI_SDA_FUNCTION, AIC23B_TWI_SDA_PIN, and TPA6130_TWI_MASTER_SPEED.

Referenced by main().

00434 {
00435 #if(DEFAULT_DACS == AUDIO_MIXER_DAC_ABDAC)
00436   const gpio_map_t TPA6130_TWI_GPIO_MAP =
00437   {
00438   {TPA6130_TWI_SCL_PIN, TPA6130_TWI_SCL_FUNCTION},
00439   {TPA6130_TWI_SDA_PIN, TPA6130_TWI_SDA_FUNCTION}
00440   };
00441 
00442   twi_options_t TPA6130_TWI_OPTIONS =
00443   {
00444     .speed = TPA6130_TWI_MASTER_SPEED,
00445     .chip = TPA6130_TWI_ADDRESS
00446   };
00447   TPA6130_TWI_OPTIONS.pba_hz = fpba_hz;
00448 
00449   // Assign I/Os to TWI.
00450   gpio_enable_module(TPA6130_TWI_GPIO_MAP,
00451     sizeof(TPA6130_TWI_GPIO_MAP) / sizeof(TPA6130_TWI_GPIO_MAP[0]));
00452 
00453   // Initialize as master.
00454   twi_master_init(TPA6130_TWI, &TPA6130_TWI_OPTIONS);
00455 
00456 #elif(DEFAULT_DACS == AUDIO_MIXER_DAC_AIC23B)
00457   static const gpio_map_t AIC23B_TWI_GPIO_MAP =
00458   {
00459     {AIC23B_TWI_SCL_PIN, AIC23B_TWI_SCL_FUNCTION},
00460     {AIC23B_TWI_SDA_PIN, AIC23B_TWI_SDA_FUNCTION}
00461   };
00462 
00463   static twi_options_t AIC23B_TWI_OPTIONS =
00464   {
00465     .speed  = AIC23B_TWI_MASTER_SPEED,
00466     .chip   = AIC23B_TWI_ADDRESS
00467   };
00468   AIC23B_TWI_OPTIONS.pba_hz = fpba_hz;
00469 
00470   gpio_enable_module(AIC23B_TWI_GPIO_MAP,
00471                      sizeof(AIC23B_TWI_GPIO_MAP) / sizeof(AIC23B_TWI_GPIO_MAP[0]));
00472   twi_master_init(AIC23B_TWI, &AIC23B_TWI_OPTIONS);
00473 
00474 #endif
00475 }

static void init_usb_clock ( void   )  [static]

Initializes the USB clock.

Definition at line 236 of file audio_example.c.

Referenced by init_sys_clocks().

00237 {
00238     pm_configure_usb_clock();
00239 }

int main ( void   ) 

Main function. Execution starts here.

Return values:
42 Fatal error.

Definition at line 482 of file audio_example.c.

References AUDIO_DEMO_STRING, avr32_logo, AVR32_LOGO_HEIGHT, AVR32_LOGO_WIDTH, controller_init(), DEFAULT_DAC_BITS_PER_SAMPLE, DEFAULT_DAC_NUM_CHANNELS, DEFAULT_DAC_SAMPLE_RATE_HZ, DEFAULT_DAC_SWAP_CHANNELS, DEFAULT_DACS, device_audio_task(), device_audio_task_init(), device_hid_task(), device_hid_task_init(), FCPU_HZ, FHSB_HZ, FPBA_HZ, FPBB_HZ, init_hmatrix(), init_sys_clocks(), and init_twi().

00483 {
00484   init_hmatrix();
00485 
00486   // Configure standard I/O streams as unbuffered.
00487 #if (defined __GNUC__) && (defined __AVR32__)
00488   setbuf(stdin, NULL);
00489 #endif
00490   setbuf(stdout, NULL);
00491 
00492 #if (defined USB_RESYNC_METHOD) && (USB_RESYNC_METHOD == USB_RESYNC_METHOD_EXT_CLOCK_SYNTHESIZER)
00493   // Initialize the TWI using the internal RCOSC
00494   init_twi_CS2200(AVR32_PM_RCOSC_FREQUENCY);
00495 
00496   // Initialize the CS2200 and produce a default 11.2896 MHz frequency
00497   cs2200_setup(11289600);
00498 #endif
00499 
00500   // Initializes the MCU system clocks
00501   init_sys_clocks();
00502 
00503   // Initialize the TWI
00504   init_twi(FPBA_HZ);
00505 
00506   audio_mixer_enable_dacs(DEFAULT_DACS);
00507   audio_mixer_dacs_start(DEFAULT_DAC_SAMPLE_RATE_HZ,
00508                          DEFAULT_DAC_NUM_CHANNELS,
00509                          DEFAULT_DAC_BITS_PER_SAMPLE,
00510                          DEFAULT_DAC_SWAP_CHANNELS);
00511 
00512    // Initialize the display
00513    et024006_Init(  FCPU_HZ, FHSB_HZ);
00514 
00515    // Set Backlight
00516    gpio_set_gpio_pin(ET024006DHU_BL_PIN);
00517 
00518    // Clear the display i.e. make it black
00519 #if (defined BOARD) && (BOARD==EVK1104)
00520 #else
00521    // Clear the display
00522    et024006_DrawFilledRect(0, 0, ET024006_WIDTH, ET024006_HEIGHT, WHITE );
00523 
00524    // Display a logo.
00525    et024006_PutPixmap(avr32_logo, AVR32_LOGO_WIDTH, 0, 0
00526       ,(ET024006_WIDTH - AVR32_LOGO_WIDTH)/2
00527       ,(ET024006_HEIGHT - AVR32_LOGO_HEIGHT)/2, AVR32_LOGO_WIDTH, AVR32_LOGO_HEIGHT);
00528 #endif
00529    et024006_PrintString(AUDIO_DEMO_STRING, (const unsigned char *)&FONT8x16, 30, 5, BLACK, -1);
00530    et024006_PrintString("Please plug the USB.", (const unsigned char *)&FONT8x8, 30, 30, BLACK, -1);
00531 
00532    // Initialize USB task
00533    usb_task_init();
00534    
00535    // Initialize Controller
00536    controller_init(FCPU_HZ, FHSB_HZ, FPBB_HZ, FPBA_HZ);
00537 
00538 #if USB_DEVICE_FEATURE == ENABLED
00539    // Initialize device audio USB task
00540    device_audio_task_init();
00541 
00542    // Initialize the HID USB task
00543    device_hid_task_init();
00544 #endif
00545 #if USB_HOST_FEATURE == ENABLED
00546    // Initialize host audio USB task
00547    host_audio_task_init();
00548 #endif
00549 
00550 #ifdef FREERTOS_USED
00551    // Start OS scheduler
00552    vTaskStartScheduler();
00553    portDBG_TRACE("FreeRTOS returned.");
00554    return 42;
00555 #else
00556    // No OS here. Need to call each task in round-robin mode.
00557    while (TRUE)
00558    {
00559      usb_task();
00560   #if USB_DEVICE_FEATURE == ENABLED
00561      device_audio_task();
00562      device_hid_task();
00563   #endif
00564   #if USB_HOST_FEATURE == ENABLED
00565      host_audio_task();
00566   #endif
00567    }
00568 #endif  // FREERTOS_USED
00569 }

size_t wcstombs ( char *  s,
const wchar_t *  pwcs,
size_t  n 
)

Replaces Newlib's implementation of the standard wcstombs function because of portability issues with wchar_t.

Warning:
This function must be used with the `-fshort-wchar' code generation option.

Definition at line 176 of file audio_example.c.

00177 {
00178   size_t count = 0;
00179 
00180   while (n--)
00181   {
00182     if ((*s++ = (char)*pwcs++) == '\0')
00183       break;
00184     count++;
00185   }
00186 
00187   return count;
00188 }


Generated on Thu Dec 17 20:03:10 2009 for AVR32 - USB AUDIO Example by  doxygen 1.5.5