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 370 of file audio_example.c.

References init_stdio().

00371 {
00372   // Import the Exception Vector Base Address.
00373   extern void _evba;
00374 
00375   // Load the Exception Vector Base Address in the corresponding system register.
00376   Set_system_register(AVR32_EVBA, (int)&_evba);
00377 
00378   // Enable exceptions.
00379   Enable_global_exception();
00380 
00381   // Initialize interrupt handling.
00382   INTC_init_interrupts();
00383 
00384   init_stdio();
00385 
00386   // Don't-care value for GCC.
00387   return 1;
00388 }

static void init_codec_gclk ( void   )  [static]

Sets up generic clock for the audio codec.

Definition at line 216 of file audio_example.c.

Referenced by init_sys_clocks().

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

static void init_hmatrix ( void   )  [static]

Initializes the HSB bus matrix.

Definition at line 201 of file audio_example.c.

Referenced by main().

00202 {
00203   // For the internal-flash HMATRIX slave, use last master as default.
00204   union
00205   {
00206     unsigned long                 scfg;
00207     avr32_hmatrix_scfg_t          SCFG;
00208   } u_avr32_hmatrix_scfg = {AVR32_HMATRIX.scfg[AVR32_HMATRIX_SLAVE_FLASH]};
00209   u_avr32_hmatrix_scfg.SCFG.defmstr_type = AVR32_HMATRIX_DEFMSTR_TYPE_LAST_DEFAULT;
00210   AVR32_HMATRIX.scfg[AVR32_HMATRIX_SLAVE_FLASH] = u_avr32_hmatrix_scfg.scfg;
00211 }

static void init_stdio ( void   )  [static]

Initializes STDIO.

Definition at line 313 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().

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

static void init_sys_clocks ( void   )  [static]

Initializes the MCU system clocks.

Definition at line 250 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().

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

static void init_twi ( U32  fpba_hz  )  [static]

Definition at line 440 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().

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

static void init_usb_clock ( void   )  [static]

Initializes the USB clock.

Definition at line 243 of file audio_example.c.

Referenced by init_sys_clocks().

00244 {
00245     pm_configure_usb_clock();
00246 }

int main ( void   ) 

Main function. Execution starts here.

Return values:
42 Fatal error.

Definition at line 489 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().

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

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 183 of file audio_example.c.

00184 {
00185   size_t count = 0;
00186 
00187   while (n--)
00188   {
00189     if ((*s++ = (char)*pwcs++) == '\0')
00190       break;
00191     count++;
00192   }
00193 
00194   return count;
00195 }


Generated on Fri Feb 19 02:32:15 2010 for AVR32 - USB AUDIO Example by  doxygen 1.5.5