00001
00091 #include <stddef.h>
00092 #include <stdio.h>
00093
00094 #include <avr32/io.h>
00095
00096 #ifndef FREERTOS_USED
00097 #if (defined __GNUC__)
00098 # include "nlao_cpu.h"
00099 # include "nlao_usart.h"
00100 #endif
00101 #else
00102 # include "usart.h"
00103 #endif
00104 #include "compiler.h"
00105 #include "board.h"
00106 #include "print_funcs.h"
00107 #include "intc.h"
00108 #include "gpio.h"
00109 #include "pm.h"
00110 #include "flashc.h"
00111 #if (defined __AVR32_UC3A3256__) || (defined __AT32UC3A3256__)
00112 #include "twim.h"
00113 #else
00114 #include "twi.h"
00115 #endif
00116 #include "usart.h"
00117
00118 #ifdef FREERTOS_USED
00119 # include "FreeRTOS.h"
00120 # include "task.h"
00121 #endif
00122 #include "conf_usb.h"
00123 #include "usb_task.h"
00124 #if USB_DEVICE_FEATURE == ENABLED
00125 # include "device_audio_task.h"
00126 # include "device_hid_task.h"
00127 #endif
00128 #if USB_HOST_FEATURE == ENABLED
00129 # include "host_audio_task.h"
00130 #endif
00131 #include "audio_example.h"
00132 #include "controller.h"
00133
00134 #if(DEFAULT_DACS == AUDIO_MIXER_DAC_ABDAC)
00135 # include "tpa6130.h"
00136 # include "conf_tpa6130.h"
00137 #elif(DEFAULT_DACS == AUDIO_MIXER_DAC_AIC23B)
00138 # include "tlv320aic23b.h"
00139 #endif
00140
00141 #include "et024006dhu.h"
00142 #include "avr32_logo.h"
00143
00144 #if (defined USB_RESYNC_METHOD) && (USB_RESYNC_METHOD == USB_RESYNC_METHOD_EXT_CLOCK_SYNTHESIZER)
00145 #include "cs2200.h"
00146 #endif
00147
00148
00149
00150
00151
00152
00153
00154 #if (defined USB_RESYNC_METHOD) && (USB_RESYNC_METHOD == USB_RESYNC_METHOD_EXT_CLOCK_SYNTHESIZER)
00156 U32 g_fcpu_hz=0;
00157
00159 U32 g_fhsb_hz=0;
00160
00162 U32 g_fpba_hz=0;
00163
00165 U32 g_fpbb_hz=0;
00166 #endif
00167
00168 #if (defined __GNUC__) && (defined __AVR32__)
00169
00176 size_t wcstombs(char *s, const wchar_t *pwcs, size_t n)
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 }
00189
00190 #endif
00191
00194 static void init_hmatrix(void)
00195 {
00196
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 }
00205
00206
00209 static void init_codec_gclk(void)
00210 {
00211 #if(DEFAULT_DACS == AUDIO_MIXER_DAC_ABDAC)
00212
00213
00214
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 }
00233
00236 static void init_usb_clock(void)
00237 {
00238 pm_configure_usb_clock();
00239 }
00240
00243 static void init_sys_clocks(void)
00244 {
00245
00246 pm_switch_to_osc0(&AVR32_PM, FOSC0, OSC0_STARTUP);
00247
00248
00249 pm_enable_osc1_crystal(&AVR32_PM, FOSC1);
00250
00251 pm_enable_clk1(&AVR32_PM, OSC1_STARTUP);
00252
00253
00254
00255
00256 pm_pll_setup(&AVR32_PM, 0,
00257 SYS_CLOCK_PLL_MUL-1,
00258 1,
00259 1,
00260 16);
00261
00262
00263
00264 pm_pll_set_option(&AVR32_PM, 0,
00265 1,
00266 1,
00267 0);
00268
00269
00270 pm_pll_enable(&AVR32_PM, 0);
00271 pm_wait_for_pll0_locked(&AVR32_PM);
00272
00273 pm_cksel(&AVR32_PM,
00274 0,
00275 0,
00276 0,
00277 0,
00278 0,
00279 0);
00280
00281
00282 flashc_set_wait_state(1);
00283
00284
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
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
00294 set_cpu_hz(FPBA_HZ);
00295 #endif
00296 init_usb_clock();
00297 init_codec_gclk();
00298 }
00299
00300
00301
00302 #ifndef FREERTOS_USED
00303
00306 static void init_stdio(void)
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
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
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 }
00348
00349
00350 #if (defined __GNUC__)
00351
00363 int _init_startup(void)
00364 {
00365
00366 extern void _evba;
00367
00368
00369 Set_system_register(AVR32_EVBA, (int)&_evba);
00370
00371
00372 Enable_global_exception();
00373
00374
00375 INTC_init_interrupts();
00376
00377 init_stdio();
00378
00379
00380 return 1;
00381 }
00382
00383 #elif (defined __ICCAVR32__)
00384
00388 int __low_level_init(void)
00389 {
00390
00391 Enable_global_exception();
00392
00393
00394 INTC_init_interrupts();
00395
00396 init_stdio();
00397
00398
00399 return 1;
00400 }
00401
00402 #endif // Compiler
00403
00404 #endif // FREERTOS_USED
00405
00406
00407 #if (defined USB_RESYNC_METHOD) && (USB_RESYNC_METHOD == USB_RESYNC_METHOD_EXT_CLOCK_SYNTHESIZER)
00408
00410 static void init_twi_CS2200(U32 fpba_hz)
00411 {
00412 volatile U32 i;
00413
00414 static const gpio_map_t CS2200_TWI_GPIO_MAP =
00415 {
00416 {CS2200_TWI_SCL_PIN, CS2200_TWI_SCL_FUNCTION},
00417 {CS2200_TWI_SDA_PIN, CS2200_TWI_SDA_FUNCTION}
00418 };
00419
00420 static twi_options_t CS2200_TWI_OPTIONS =
00421 {
00422 .speed = CS2200_TWI_MASTER_SPEED,
00423 .chip = CS2200_TWI_SLAVE_ADDRESS
00424 };
00425 CS2200_TWI_OPTIONS.pba_hz = fpba_hz;
00426
00427 gpio_enable_module(CS2200_TWI_GPIO_MAP,
00428 sizeof(CS2200_TWI_GPIO_MAP) / sizeof(CS2200_TWI_GPIO_MAP[0]));
00429 twi_master_init(CS2200_TWI, &CS2200_TWI_OPTIONS);
00430 }
00431 #endif
00432
00433 static void init_twi(U32 fpba_hz)
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
00450 gpio_enable_module(TPA6130_TWI_GPIO_MAP,
00451 sizeof(TPA6130_TWI_GPIO_MAP) / sizeof(TPA6130_TWI_GPIO_MAP[0]));
00452
00453
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 }
00476
00477
00482 int main(void)
00483 {
00484 init_hmatrix();
00485
00486
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
00494 init_twi_CS2200(AVR32_PM_RCOSC_FREQUENCY);
00495
00496
00497 cs2200_setup(11289600);
00498 #endif
00499
00500
00501 init_sys_clocks();
00502
00503
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
00513 et024006_Init( FCPU_HZ, FHSB_HZ);
00514
00515
00516 gpio_set_gpio_pin(ET024006DHU_BL_PIN);
00517
00518
00519 #if (defined BOARD) && (BOARD==EVK1104)
00520 #else
00521
00522 et024006_DrawFilledRect(0, 0, ET024006_WIDTH, ET024006_HEIGHT, WHITE );
00523
00524
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
00533 usb_task_init();
00534
00535
00536 controller_init(FCPU_HZ, FHSB_HZ, FPBB_HZ, FPBA_HZ);
00537
00538 #if USB_DEVICE_FEATURE == ENABLED
00539
00540 device_audio_task_init();
00541
00542
00543 device_hid_task_init();
00544 #endif
00545 #if USB_HOST_FEATURE == ENABLED
00546
00547 host_audio_task_init();
00548 #endif
00549
00550 #ifdef FREERTOS_USED
00551
00552 vTaskStartScheduler();
00553 portDBG_TRACE("FreeRTOS returned.");
00554 return 42;
00555 #else
00556
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 }