00001
00212
00213
00214 #ifndef FREERTOS_USED
00215 #if (defined __GNUC__)
00216 #include "nlao_cpu.h"
00217 #endif
00218 #else
00219 #include <stdio.h>
00220 #endif
00221 #include "compiler.h"
00222 #include "preprocessor.h"
00223 #include "mass_storage_example.h"
00224 #include "board.h"
00225 #include "print_funcs.h"
00226 #include "intc.h"
00227 #include "power_clocks_lib.h"
00228 #include "gpio.h"
00229 #include "ctrl_access.h"
00230 #if (defined AT45DBX_MEM) && (AT45DBX_MEM == ENABLE)
00231 #include "spi.h"
00232 #include "conf_at45dbx.h"
00233 #endif
00234 #if (defined SD_MMC_SPI_MEM) && (SD_MMC_SPI_MEM == ENABLE)
00235 #include "spi.h"
00236 #include "conf_sd_mmc_spi.h"
00237 #endif
00238 #if ((defined SD_MMC_MCI_0_MEM) && (SD_MMC_MCI_0_MEM == ENABLE)) || ((defined SD_MMC_MCI_1_MEM) && (SD_MMC_MCI_1_MEM == ENABLE))
00239 #include "mci.h"
00240 #include "conf_sd_mmc_mci.h"
00241 #endif
00242 #ifdef FREERTOS_USED
00243 #include "FreeRTOS.h"
00244 #include "task.h"
00245 #endif
00246 #include "conf_usb.h"
00247 #include "usb_task.h"
00248 #if USB_DEVICE_FEATURE == ENABLED
00249 #include "device_mass_storage_task.h"
00250 #endif
00251 #if USB_HOST_FEATURE == ENABLED
00252 #include "host_mass_storage_task.h"
00253 #endif
00254 #include "ushell_task.h"
00255
00256
00257
00258
00261
00262 static pcl_freq_param_t pcl_freq_param =
00263 {
00264 .cpu_f = FCPU_HZ,
00265 .pba_f = FPBA_HZ,
00266 .osc0_f = FOSC0,
00267 .osc0_startup = OSC0_STARTUP
00268 };
00270
00273 void init_hmatrix(void)
00274 {
00275 union
00276 {
00277 unsigned long scfg;
00278 avr32_hmatrix_scfg_t SCFG;
00279 } u_avr32_hmatrix_scfg;
00280
00281
00282 u_avr32_hmatrix_scfg.scfg = AVR32_HMATRIX.scfg[AVR32_HMATRIX_SLAVE_FLASH];
00283 u_avr32_hmatrix_scfg.SCFG.defmstr_type = AVR32_HMATRIX_DEFMSTR_TYPE_LAST_DEFAULT;
00284 AVR32_HMATRIX.scfg[AVR32_HMATRIX_SLAVE_FLASH] = u_avr32_hmatrix_scfg.scfg;
00285 }
00286
00287
00288 #ifndef FREERTOS_USED
00289
00290 #if (defined __GNUC__)
00291
00303 int _init_startup(void)
00304 {
00305
00306 extern void _evba;
00307
00308
00309 Set_system_register(AVR32_EVBA, (int)&_evba);
00310
00311
00312 Enable_global_exception();
00313
00314
00315 INTC_init_interrupts();
00316
00317
00318 set_cpu_hz(pcl_freq_param.pba_f);
00319
00320
00321 return 1;
00322 }
00323
00324 #elif __ICCAVR32__
00325
00329 int __low_level_init(void)
00330 {
00331
00332 #if BOARD == UC3C_EK
00333
00334 AVR32_WDT.ctrl = 0x55000000;
00335 AVR32_WDT.ctrl = 0xAA000000;
00336 #endif
00337
00338
00339 Enable_global_exception();
00340
00341
00342 INTC_init_interrupts();
00343
00344
00345 return 1;
00346 }
00347
00348 #endif // Compiler
00349
00350 #endif // FREERTOS_USED
00351
00352
00353 #if (defined AT45DBX_MEM) && (AT45DBX_MEM == ENABLE)
00354
00357 static void at45dbx_resources_init(void)
00358 {
00359 static const gpio_map_t AT45DBX_SPI_GPIO_MAP =
00360 {
00361 {AT45DBX_SPI_SCK_PIN, AT45DBX_SPI_SCK_FUNCTION },
00362 {AT45DBX_SPI_MISO_PIN, AT45DBX_SPI_MISO_FUNCTION },
00363 {AT45DBX_SPI_MOSI_PIN, AT45DBX_SPI_MOSI_FUNCTION },
00364 #define AT45DBX_ENABLE_NPCS_PIN(NPCS, unused) \
00365 {AT45DBX_SPI_NPCS##NPCS##_PIN, AT45DBX_SPI_NPCS##NPCS##_FUNCTION}, // Chip Select NPCS.
00366 MREPEAT(AT45DBX_MEM_CNT, AT45DBX_ENABLE_NPCS_PIN, ~)
00367 #undef AT45DBX_ENABLE_NPCS_PIN
00368 };
00369
00370
00371 spi_options_t spiOptions =
00372 {
00373 .reg = AT45DBX_SPI_FIRST_NPCS,
00374 .baudrate = AT45DBX_SPI_MASTER_SPEED,
00375 .bits = AT45DBX_SPI_BITS,
00376 .spck_delay = 0,
00377 .trans_delay = 0,
00378 .stay_act = 1,
00379 .spi_mode = 0,
00380 .modfdis = 1
00381 };
00382
00383
00384 gpio_enable_module(AT45DBX_SPI_GPIO_MAP,
00385 sizeof(AT45DBX_SPI_GPIO_MAP) / sizeof(AT45DBX_SPI_GPIO_MAP[0]));
00386
00387
00388 if (!spi_is_enabled(AT45DBX_SPI))
00389 {
00390
00391 spi_initMaster(AT45DBX_SPI, &spiOptions);
00392
00393
00394 spi_selectionMode(AT45DBX_SPI, 0, 0, 0);
00395
00396
00397 spi_enable(AT45DBX_SPI);
00398 }
00399
00400
00401 at45dbx_init(spiOptions, pcl_freq_param.pba_f);
00402 }
00403
00404 #endif // AT45DBX_MEM == ENABLE
00405
00406
00407 #if (defined SD_MMC_SPI_MEM) && (SD_MMC_SPI_MEM == ENABLE)
00408
00411 static void sd_mmc_spi_resources_init(void)
00412 {
00413 static const gpio_map_t SD_MMC_SPI_GPIO_MAP =
00414 {
00415 {SD_MMC_SPI_SCK_PIN, SD_MMC_SPI_SCK_FUNCTION },
00416 {SD_MMC_SPI_MISO_PIN, SD_MMC_SPI_MISO_FUNCTION},
00417 {SD_MMC_SPI_MOSI_PIN, SD_MMC_SPI_MOSI_FUNCTION},
00418 {SD_MMC_SPI_NPCS_PIN, SD_MMC_SPI_NPCS_FUNCTION}
00419 };
00420
00421
00422 spi_options_t spiOptions =
00423 {
00424 .reg = SD_MMC_SPI_NPCS,
00425 .baudrate = SD_MMC_SPI_MASTER_SPEED,
00426 .bits = SD_MMC_SPI_BITS,
00427 .spck_delay = 0,
00428 .trans_delay = 0,
00429 .stay_act = 1,
00430 .spi_mode = 0,
00431 .modfdis = 1
00432 };
00433
00434
00435 gpio_enable_module(SD_MMC_SPI_GPIO_MAP,
00436 sizeof(SD_MMC_SPI_GPIO_MAP) / sizeof(SD_MMC_SPI_GPIO_MAP[0]));
00437
00438
00439 if (!spi_is_enabled(SD_MMC_SPI))
00440 {
00441
00442 spi_initMaster(SD_MMC_SPI, &spiOptions);
00443
00444
00445 spi_selectionMode(SD_MMC_SPI, 0, 0, 0);
00446
00447
00448 spi_enable(SD_MMC_SPI);
00449 }
00450
00451
00452 sd_mmc_spi_init(spiOptions, pcl_freq_param.pba_f);
00453 }
00454
00455 #endif // SD_MMC_SPI_MEM == ENABLE
00456
00457
00458 #if ((defined SD_MMC_MCI_0_MEM) && (SD_MMC_MCI_0_MEM == ENABLE)) || ((defined SD_MMC_MCI_1_MEM) && (SD_MMC_MCI_1_MEM == ENABLE))
00459
00462 static void sd_mmc_mci_resources_init(void)
00463 {
00464 static const gpio_map_t SD_MMC_MCI_GPIO_MAP =
00465 {
00466 {SD_SLOT_8BITS_CLK_PIN, SD_SLOT_8BITS_CLK_FUNCTION },
00467 {SD_SLOT_8BITS_CMD_PIN, SD_SLOT_8BITS_CMD_FUNCTION },
00468 {SD_SLOT_8BITS_DATA0_PIN, SD_SLOT_8BITS_DATA0_FUNCTION},
00469 {SD_SLOT_8BITS_DATA1_PIN, SD_SLOT_8BITS_DATA1_FUNCTION},
00470 {SD_SLOT_8BITS_DATA2_PIN, SD_SLOT_8BITS_DATA2_FUNCTION},
00471 {SD_SLOT_8BITS_DATA3_PIN, SD_SLOT_8BITS_DATA3_FUNCTION},
00472 {SD_SLOT_8BITS_DATA4_PIN, SD_SLOT_8BITS_DATA4_FUNCTION},
00473 {SD_SLOT_8BITS_DATA5_PIN, SD_SLOT_8BITS_DATA5_FUNCTION},
00474 {SD_SLOT_8BITS_DATA6_PIN, SD_SLOT_8BITS_DATA6_FUNCTION},
00475 {SD_SLOT_8BITS_DATA7_PIN, SD_SLOT_8BITS_DATA7_FUNCTION}
00476 };
00477
00478
00479 static const mci_options_t MCI_OPTIONS =
00480 {
00481 .card_speed = 400000,
00482 .card_slot = SD_SLOT_8BITS,
00483 };
00484
00485
00486 gpio_enable_module(SD_MMC_MCI_GPIO_MAP,
00487 sizeof(SD_MMC_MCI_GPIO_MAP) / sizeof(SD_MMC_MCI_GPIO_MAP[0]));
00488
00489
00490 gpio_enable_pin_pull_up(SD_SLOT_8BITS_CARD_DETECT);
00491
00492
00493 gpio_enable_pin_pull_up(SD_SLOT_8BITS_WRITE_PROTECT);
00494
00495
00496 sd_mmc_mci_init(&MCI_OPTIONS, pcl_freq_param.pba_f, pcl_freq_param.cpu_f);
00497 }
00498
00499 #endif // SD_MMC_MCI_0_MEM == ENABLE || SD_MMC_MCI_1_MEM == ENABLE
00500
00501
00506 int main(void)
00507 {
00508
00509 if (pcl_configure_clocks(&pcl_freq_param) != PASS)
00510 return 42;
00511
00512
00513 init_dbg_rs232(pcl_freq_param.pba_f);
00514
00515 #if (defined AT45DBX_MEM) && (AT45DBX_MEM == ENABLE)
00516 at45dbx_resources_init();
00517 #endif
00518 #if SD_MMC_SPI_MEM == ENABLE
00519 sd_mmc_spi_resources_init();
00520 #endif
00521 #if ((defined SD_MMC_MCI_0_MEM) && (SD_MMC_MCI_0_MEM == ENABLE)) || ((defined SD_MMC_MCI_1_MEM) && (SD_MMC_MCI_1_MEM == ENABLE))
00522 sd_mmc_mci_resources_init();
00523 #endif
00524 #ifdef FREERTOS_USED
00525 if (!ctrl_access_init())
00526 {
00527 portDBG_TRACE("The module CTRL_ACCESS could not be initialized.");
00528 return 42;
00529 }
00530 #endif // FREERTOS_USED
00531
00532
00533 init_hmatrix();
00534
00535
00536 pcl_configure_usb_clock();
00537
00538
00539 usb_task_init();
00540 #if USB_DEVICE_FEATURE == ENABLED
00541 device_mass_storage_task_init();
00542 #endif
00543 #if USB_HOST_FEATURE == ENABLED
00544 host_mass_storage_task_init();
00545 #endif
00546 ushell_task_init(pcl_freq_param.pba_f);
00547
00548 #ifdef FREERTOS_USED
00549 vTaskStartScheduler();
00550 portDBG_TRACE("FreeRTOS returned.");
00551 return 42;
00552 #else
00553 while (TRUE)
00554 {
00555 usb_task();
00556 #if USB_DEVICE_FEATURE == ENABLED
00557 device_mass_storage_task();
00558 #endif
00559 #if USB_HOST_FEATURE == ENABLED
00560 host_mass_storage_task();
00561 #endif
00562 ushell_task();
00563 }
00564 #endif // FREERTOS_USED
00565 }