00001
00155
00156
00157 #ifndef FREERTOS_USED
00158 #if __GNUC__
00159 # include "nlao_cpu.h"
00160 # include "nlao_usart.h"
00161 #endif
00162 #endif
00163 #include <stdio.h>
00164 #include "compiler.h"
00165 #include "preprocessor.h"
00166 #include "board.h"
00167 #include "print_funcs.h"
00168 #include "intc.h"
00169 #include "pm.h"
00170 #include "gpio.h"
00171 #include "usart.h"
00172 #include "print_funcs.h"
00173 #include "ctrl_access.h"
00174 #include "main.h"
00175 #if (AT45DBX_MEM == ENABLE)
00176 # include "spi.h"
00177 # include "conf_at45dbx.h"
00178 # endif
00179 #if (SD_MMC_SPI_MEM == ENABLE)
00180 # include "spi.h"
00181 # include "conf_sd_mmc_spi.h"
00182 #endif
00183 #if (SD_MMC_MCI_0_MEM == ENABLE) || (SD_MMC_MCI_1_MEM == ENABLE)
00184 # include "mci.h"
00185 # include "conf_sd_mmc_mci.h"
00186 #endif
00187 #ifdef FREERTOS_USED
00188 # include "FreeRTOS.h"
00189 # include "task.h"
00190 #endif
00191 #include "conf_usb.h"
00192 #include "usb_task.h"
00193 #if (USB_DEVICE_FEATURE == ENABLED)
00194 # include "device_mass_storage_task.h"
00195 #endif
00196 #if (USB_HOST_FEATURE == ENABLED)
00197 # include "host_mass_storage_task.h"
00198 #endif
00199 #include "mmi_task.h"
00200
00201
00202
00203
00206
00207 pm_freq_param_t pm_freq_param=
00208 {
00209 .cpu_f = APPLI_CPU_SPEED
00210 , .pba_f = APPLI_PBA_SPEED
00211 , .osc0_f = FOSC0
00212 , .osc0_startup = OSC0_STARTUP
00213 };
00215
00216
00217 #ifndef FREERTOS_USED
00218
00221 static void init_stdio(void)
00222 {
00223 #if __GNUC__ && __AVR32__
00224
00225 static const gpio_map_t STDIO_USART_GPIO_MAP =
00226 {
00227 {STDIO_USART_RX_PIN, STDIO_USART_RX_FUNCTION},
00228 {STDIO_USART_TX_PIN, STDIO_USART_TX_FUNCTION}
00229 };
00230
00231
00232 set_usart_base((void *)STDIO_USART);
00233 gpio_enable_module(STDIO_USART_GPIO_MAP,
00234 sizeof(STDIO_USART_GPIO_MAP) / sizeof(STDIO_USART_GPIO_MAP[0]));
00235 usart_init(STDIO_USART_BAUDRATE);
00236
00237 #elif __ICCAVR32__
00238
00239 static const gpio_map_t STDIO_USART_GPIO_MAP =
00240 {
00241 {STDIO_USART_RX_PIN, STDIO_USART_RX_FUNCTION},
00242 {STDIO_USART_TX_PIN, STDIO_USART_TX_FUNCTION}
00243 };
00244
00245 static const usart_options_t STDIO_USART_OPTIONS =
00246 {
00247 .baudrate = STDIO_USART_BAUDRATE,
00248 .charlength = 8,
00249 .paritytype = USART_NO_PARITY,
00250 .stopbits = USART_1_STOPBIT,
00251 .channelmode = USART_NORMAL_CHMODE
00252 };
00253
00254
00255 extern volatile avr32_usart_t *volatile stdio_usart_base;
00256 stdio_usart_base = STDIO_USART;
00257 gpio_enable_module(STDIO_USART_GPIO_MAP,
00258 sizeof(STDIO_USART_GPIO_MAP) / sizeof(STDIO_USART_GPIO_MAP[0]));
00259 usart_init_rs232(STDIO_USART, &STDIO_USART_OPTIONS, APPLI_PBA_SPEED);
00260
00261 #endif
00262 }
00263
00264 #if __GNUC__
00265
00277 int _init_startup(void)
00278 {
00279
00280 extern void _evba;
00281
00282
00283 Set_system_register(AVR32_EVBA, (int)&_evba);
00284
00285
00286 Enable_global_exception();
00287
00288
00289 INTC_init_interrupts();
00290
00291
00292 return 1;
00293 }
00294
00295 #elif __ICCAVR32__
00296
00300 int __low_level_init(void)
00301 {
00302
00303 Enable_global_exception();
00304
00305
00306 INTC_init_interrupts();
00307
00308
00309 return 1;
00310 }
00311
00312 #endif // Compiler
00313
00314 #endif // FREERTOS_USED
00315
00316
00317 #if AT45DBX_MEM == ENABLE
00318
00321 static void at45dbx_resources_init(void)
00322 {
00323 static const gpio_map_t AT45DBX_SPI_GPIO_MAP =
00324 {
00325 {AT45DBX_SPI_SCK_PIN, AT45DBX_SPI_SCK_FUNCTION },
00326 {AT45DBX_SPI_MISO_PIN, AT45DBX_SPI_MISO_FUNCTION },
00327 {AT45DBX_SPI_MOSI_PIN, AT45DBX_SPI_MOSI_FUNCTION },
00328 #define AT45DBX_ENABLE_NPCS_PIN(NPCS, unused) \
00329 {AT45DBX_SPI_NPCS##NPCS##_PIN, AT45DBX_SPI_NPCS##NPCS##_FUNCTION}, // Chip Select NPCS.
00330 MREPEAT(AT45DBX_MEM_CNT, AT45DBX_ENABLE_NPCS_PIN, ~)
00331 #undef AT45DBX_ENABLE_NPCS_PIN
00332 };
00333
00334
00335 spi_options_t spiOptions =
00336 {
00337 .reg = AT45DBX_SPI_FIRST_NPCS,
00338 .baudrate = AT45DBX_SPI_MASTER_SPEED,
00339 .bits = AT45DBX_SPI_BITS,
00340 .spck_delay = 0,
00341 .trans_delay = 0,
00342 .stay_act = 1,
00343 .spi_mode = 0,
00344 .modfdis = 1
00345 };
00346
00347
00348 gpio_enable_module(AT45DBX_SPI_GPIO_MAP,
00349 sizeof(AT45DBX_SPI_GPIO_MAP) / sizeof(AT45DBX_SPI_GPIO_MAP[0]));
00350
00351
00352 if (!spi_is_enabled(AT45DBX_SPI))
00353 {
00354
00355 spi_initMaster(AT45DBX_SPI, &spiOptions);
00356
00357
00358 spi_selectionMode(AT45DBX_SPI, 0, 0, 0);
00359
00360
00361 spi_enable(AT45DBX_SPI);
00362 }
00363
00364
00365 at45dbx_init(spiOptions, pm_freq_param.pba_f);
00366 }
00367
00368 #endif // AT45DBX_MEM == ENABLE
00369
00370
00371 #if SD_MMC_SPI_MEM == ENABLE
00372
00375 static void sd_mmc_spi_resources_init(void)
00376 {
00377 static const gpio_map_t SD_MMC_SPI_GPIO_MAP =
00378 {
00379 {SD_MMC_SPI_SCK_PIN, SD_MMC_SPI_SCK_FUNCTION },
00380 {SD_MMC_SPI_MISO_PIN, SD_MMC_SPI_MISO_FUNCTION},
00381 {SD_MMC_SPI_MOSI_PIN, SD_MMC_SPI_MOSI_FUNCTION},
00382 {SD_MMC_SPI_NPCS_PIN, SD_MMC_SPI_NPCS_FUNCTION}
00383 };
00384
00385
00386 spi_options_t spiOptions =
00387 {
00388 .reg = SD_MMC_SPI_NPCS,
00389 .baudrate = SD_MMC_SPI_MASTER_SPEED,
00390 .bits = SD_MMC_SPI_BITS,
00391 .spck_delay = 0,
00392 .trans_delay = 0,
00393 .stay_act = 1,
00394 .spi_mode = 0,
00395 .modfdis = 1
00396 };
00397
00398
00399 gpio_enable_module(SD_MMC_SPI_GPIO_MAP,
00400 sizeof(SD_MMC_SPI_GPIO_MAP) / sizeof(SD_MMC_SPI_GPIO_MAP[0]));
00401
00402
00403 if (!spi_is_enabled(SD_MMC_SPI))
00404 {
00405
00406 spi_initMaster(SD_MMC_SPI, &spiOptions);
00407
00408
00409 spi_selectionMode(SD_MMC_SPI, 0, 0, 0);
00410
00411
00412 spi_enable(SD_MMC_SPI);
00413 }
00414
00415
00416 sd_mmc_spi_init(spiOptions, pm_freq_param.pba_f);
00417 }
00418
00419 #endif // SD_MMC_SPI_MEM == ENABLE
00420
00421
00422 #if SD_MMC_MCI_0_MEM == ENABLE || SD_MMC_MCI_1_MEM == ENABLE
00423
00426 static void sd_mmc_mci_resources_init(void)
00427 {
00428 static const gpio_map_t SD_MMC_MCI_GPIO_MAP =
00429 {
00430 {SD_SLOT_8BITS_CLK_PIN, SD_SLOT_8BITS_CLK_FUNCTION },
00431 {SD_SLOT_8BITS_CMD_PIN, SD_SLOT_8BITS_CMD_FUNCTION },
00432 {SD_SLOT_8BITS_DATA0_PIN, SD_SLOT_8BITS_DATA0_FUNCTION},
00433 {SD_SLOT_8BITS_DATA1_PIN, SD_SLOT_8BITS_DATA1_FUNCTION},
00434 {SD_SLOT_8BITS_DATA2_PIN, SD_SLOT_8BITS_DATA2_FUNCTION},
00435 {SD_SLOT_8BITS_DATA3_PIN, SD_SLOT_8BITS_DATA3_FUNCTION},
00436 {SD_SLOT_8BITS_DATA4_PIN, SD_SLOT_8BITS_DATA4_FUNCTION},
00437 {SD_SLOT_8BITS_DATA5_PIN, SD_SLOT_8BITS_DATA5_FUNCTION},
00438 {SD_SLOT_8BITS_DATA6_PIN, SD_SLOT_8BITS_DATA6_FUNCTION},
00439 {SD_SLOT_8BITS_DATA7_PIN, SD_SLOT_8BITS_DATA7_FUNCTION}
00440 };
00441
00442
00443 static const mci_options_t MCI_OPTIONS =
00444 {
00445 .card_speed = 400000,
00446 .card_slot = SD_SLOT_8BITS,
00447 };
00448
00449
00450 gpio_enable_module(SD_MMC_MCI_GPIO_MAP,
00451 sizeof(SD_MMC_MCI_GPIO_MAP) / sizeof(SD_MMC_MCI_GPIO_MAP[0]));
00452
00453
00454 gpio_enable_pin_pull_up(SD_SLOT_8BITS_CARD_DETECT);
00455
00456
00457 gpio_enable_pin_pull_up(SD_SLOT_8BITS_WRITE_PROTECT);
00458
00459
00460 sd_mmc_mci_init(&MCI_OPTIONS, pm_freq_param.pba_f, pm_freq_param.cpu_f);
00461 }
00462
00463 #endif // SD_MMC_MCI_0_MEM == ENABLE || SD_MMC_MCI_1_MEM == ENABLE
00464
00465
00470 int main(void)
00471 {
00472
00473 if( PM_FREQ_STATUS_FAIL==pm_configure_clocks(&pm_freq_param) )
00474 return 42;
00475
00476
00477 init_stdio();
00478
00479 union
00480 {
00481 unsigned long scfg;
00482 avr32_hmatrix_scfg_t SCFG;
00483 } u_avr32_hmatrix_scfg;
00484
00485
00486 u_avr32_hmatrix_scfg.scfg = AVR32_HMATRIX.scfg[AVR32_HMATRIX_SLAVE_FLASH];
00487 u_avr32_hmatrix_scfg.SCFG.defmstr_type = AVR32_HMATRIX_DEFMSTR_TYPE_LAST_DEFAULT;
00488 AVR32_HMATRIX.scfg[AVR32_HMATRIX_SLAVE_FLASH] = u_avr32_hmatrix_scfg.scfg;
00489
00490
00491 u_avr32_hmatrix_scfg.scfg = AVR32_HMATRIX.scfg[AVR32_HMATRIX_SLAVE_SRAM];
00492 u_avr32_hmatrix_scfg.SCFG.defmstr_type = AVR32_HMATRIX_DEFMSTR_TYPE_LAST_DEFAULT;
00493 AVR32_HMATRIX.scfg[AVR32_HMATRIX_SLAVE_SRAM] = u_avr32_hmatrix_scfg.scfg;
00494
00495
00496 u_avr32_hmatrix_scfg.scfg = AVR32_HMATRIX.scfg[AVR32_HMATRIX_SLAVE_EBI];
00497 u_avr32_hmatrix_scfg.SCFG.defmstr_type = AVR32_HMATRIX_DEFMSTR_TYPE_LAST_DEFAULT;
00498 AVR32_HMATRIX.scfg[AVR32_HMATRIX_SLAVE_EBI] = u_avr32_hmatrix_scfg.scfg;
00499
00500
00501 init_dbg_rs232(pm_freq_param.pba_f);
00502
00503 #if AT45DBX_MEM == ENABLE
00504 at45dbx_resources_init();
00505 #endif
00506 #if SD_MMC_SPI_MEM == ENABLE
00507 sd_mmc_spi_resources_init();
00508 #endif
00509 #if SD_MMC_MCI_0_MEM == ENABLE || SD_MMC_MCI_1_MEM == ENABLE
00510 sd_mmc_mci_resources_init();
00511 #endif
00512 #ifdef FREERTOS_USED
00513 if (!ctrl_access_init())
00514 {
00515 portDBG_TRACE("The module CTRL_ACCESS could not be initialized.");
00516 return 42;
00517 }
00518 #endif // FREERTOS_USED
00519
00520
00521 pm_configure_usb_clock();
00522
00523
00524 usb_task_init();
00525 #if USB_DEVICE_FEATURE == ENABLED
00526 device_mass_storage_task_init();
00527 #endif
00528 #if USB_HOST_FEATURE == ENABLED
00529 host_mass_storage_task_init();
00530 #endif
00531 mmi_task_init(pm_freq_param.cpu_f, pm_freq_param.pba_f);
00532
00533 #ifdef FREERTOS_USED
00534 vTaskStartScheduler();
00535 portDBG_TRACE("FreeRTOS returned.");
00536 return 42;
00537 #else
00538 while (TRUE)
00539 {
00540 usb_task();
00541 #if USB_DEVICE_FEATURE == ENABLED
00542 device_mass_storage_task();
00543 #endif
00544 #if USB_HOST_FEATURE == ENABLED
00545 host_mass_storage_task();
00546 #endif
00547 mmi_task();
00548 }
00549 #endif // FREERTOS_USED
00550 }