00001
00098 #include <stddef.h>
00099 #include <stdio.h>
00100
00101 #include <avr32/io.h>
00102
00103 #ifndef FREERTOS_USED
00104 #if (defined __GNUC__)
00105 # include "nlao_cpu.h"
00106 # include "nlao_usart.h"
00107 #endif
00108 #else
00109 # include "usart.h"
00110 #endif
00111 #include "compiler.h"
00112 #include "board.h"
00113 #include "print_funcs.h"
00114 #include "intc.h"
00115 #include "gpio.h"
00116 #include "pm.h"
00117 #include "flashc.h"
00118 #if UC3A3
00119 #include "twim.h"
00120 #else
00121 #include "twi.h"
00122 #endif
00123 #include "usart.h"
00124
00125 #ifdef FREERTOS_USED
00126 # include "FreeRTOS.h"
00127 # include "task.h"
00128 #endif
00129 #include "conf_usb.h"
00130 #include "usb_task.h"
00131 #if USB_DEVICE_FEATURE == ENABLED
00132 # include "device_audio_task.h"
00133 # include "device_hid_task.h"
00134 #endif
00135 #if USB_HOST_FEATURE == ENABLED
00136 # include "host_audio_task.h"
00137 #endif
00138 #include "audio_example.h"
00139 #include "controller.h"
00140
00141 #if(DEFAULT_DACS == AUDIO_MIXER_DAC_ABDAC)
00142 # include "tpa6130.h"
00143 # include "conf_tpa6130.h"
00144 #elif(DEFAULT_DACS == AUDIO_MIXER_DAC_AIC23B)
00145 # include "tlv320aic23b.h"
00146 #endif
00147
00148 #include "et024006dhu.h"
00149 #include "avr32_logo.h"
00150
00151 #if (defined USB_RESYNC_METHOD) && (USB_RESYNC_METHOD == USB_RESYNC_METHOD_EXT_CLOCK_SYNTHESIZER)
00152 #include "cs2200.h"
00153 #endif
00154
00155
00156
00157
00158
00159
00160
00161 #if (defined USB_RESYNC_METHOD) && (USB_RESYNC_METHOD == USB_RESYNC_METHOD_EXT_CLOCK_SYNTHESIZER)
00163 U32 g_fcpu_hz=0;
00164
00166 U32 g_fhsb_hz=0;
00167
00169 U32 g_fpba_hz=0;
00170
00172 U32 g_fpbb_hz=0;
00173 #endif
00174
00175 #if (defined __GNUC__) && (defined __AVR32__)
00176
00183 size_t wcstombs(char *s, const wchar_t *pwcs, size_t n)
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 }
00196
00197 #endif
00198
00201 static void init_hmatrix(void)
00202 {
00203
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 }
00212
00213
00216 static void init_codec_gclk(void)
00217 {
00218 #if(DEFAULT_DACS == AUDIO_MIXER_DAC_ABDAC)
00219
00220
00221
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 }
00240
00243 static void init_usb_clock(void)
00244 {
00245 pm_configure_usb_clock();
00246 }
00247
00250 static void init_sys_clocks(void)
00251 {
00252
00253 pm_switch_to_osc0(&AVR32_PM, FOSC0, OSC0_STARTUP);
00254
00255
00256 pm_enable_osc1_crystal(&AVR32_PM, FOSC1);
00257
00258 pm_enable_clk1(&AVR32_PM, OSC1_STARTUP);
00259
00260
00261
00262
00263 pm_pll_setup(&AVR32_PM, 0,
00264 SYS_CLOCK_PLL_MUL-1,
00265 1,
00266 1,
00267 16);
00268
00269
00270
00271 pm_pll_set_option(&AVR32_PM, 0,
00272 1,
00273 1,
00274 0);
00275
00276
00277 pm_pll_enable(&AVR32_PM, 0);
00278 pm_wait_for_pll0_locked(&AVR32_PM);
00279
00280 pm_cksel(&AVR32_PM,
00281 0,
00282 0,
00283 0,
00284 0,
00285 0,
00286 0);
00287
00288
00289 flashc_set_wait_state(1);
00290
00291
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
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
00301 set_cpu_hz(FPBA_HZ);
00302 #endif
00303 init_usb_clock();
00304 init_codec_gclk();
00305 }
00306
00307
00308
00309 #ifndef FREERTOS_USED
00310
00313 static void init_stdio(void)
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
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
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 }
00355
00356
00357 #if (defined __GNUC__)
00358
00370 int _init_startup(void)
00371 {
00372
00373 extern void _evba;
00374
00375
00376 Set_system_register(AVR32_EVBA, (int)&_evba);
00377
00378
00379 Enable_global_exception();
00380
00381
00382 INTC_init_interrupts();
00383
00384 init_stdio();
00385
00386
00387 return 1;
00388 }
00389
00390 #elif (defined __ICCAVR32__)
00391
00395 int __low_level_init(void)
00396 {
00397
00398 Enable_global_exception();
00399
00400
00401 INTC_init_interrupts();
00402
00403 init_stdio();
00404
00405
00406 return 1;
00407 }
00408
00409 #endif // Compiler
00410
00411 #endif // FREERTOS_USED
00412
00413
00414 #if (defined USB_RESYNC_METHOD) && (USB_RESYNC_METHOD == USB_RESYNC_METHOD_EXT_CLOCK_SYNTHESIZER)
00415
00417 static void init_twi_CS2200(U32 fpba_hz)
00418 {
00419 volatile U32 i;
00420
00421 static const gpio_map_t CS2200_TWI_GPIO_MAP =
00422 {
00423 {CS2200_TWI_SCL_PIN, CS2200_TWI_SCL_FUNCTION},
00424 {CS2200_TWI_SDA_PIN, CS2200_TWI_SDA_FUNCTION}
00425 };
00426
00427 static twi_options_t CS2200_TWI_OPTIONS =
00428 {
00429 .speed = CS2200_TWI_MASTER_SPEED,
00430 .chip = CS2200_TWI_SLAVE_ADDRESS
00431 };
00432 CS2200_TWI_OPTIONS.pba_hz = fpba_hz;
00433
00434 gpio_enable_module(CS2200_TWI_GPIO_MAP,
00435 sizeof(CS2200_TWI_GPIO_MAP) / sizeof(CS2200_TWI_GPIO_MAP[0]));
00436 twi_master_init(CS2200_TWI, &CS2200_TWI_OPTIONS);
00437 }
00438 #endif
00439
00440 static void init_twi(U32 fpba_hz)
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
00457 gpio_enable_module(TPA6130_TWI_GPIO_MAP,
00458 sizeof(TPA6130_TWI_GPIO_MAP) / sizeof(TPA6130_TWI_GPIO_MAP[0]));
00459
00460
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 }
00483
00484
00489 int main(void)
00490 {
00491 init_hmatrix();
00492
00493
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
00501 init_twi_CS2200(AVR32_PM_RCOSC_FREQUENCY);
00502
00503
00504 cs2200_setup(11289600);
00505 #endif
00506
00507
00508 init_sys_clocks();
00509
00510
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
00520 et024006_Init( FCPU_HZ, FHSB_HZ);
00521
00522
00523 gpio_set_gpio_pin(ET024006DHU_BL_PIN);
00524
00525
00526 et024006_DrawFilledRect(0, 0, ET024006_WIDTH, ET024006_HEIGHT, WHITE );
00527
00528
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
00537 usb_task_init();
00538
00539
00540 controller_init(FCPU_HZ, FHSB_HZ, FPBB_HZ, FPBA_HZ);
00541
00542 #if USB_DEVICE_FEATURE == ENABLED
00543
00544 device_audio_task_init();
00545
00546
00547 device_hid_task_init();
00548 #endif
00549 #if USB_HOST_FEATURE == ENABLED
00550
00551 host_audio_task_init();
00552 #endif
00553
00554 #ifdef FREERTOS_USED
00555
00556 vTaskStartScheduler();
00557 portDBG_TRACE("FreeRTOS returned.");
00558 return 42;
00559 #else
00560
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 }