main.c File Reference


Detailed Description

Main file of the USB mass-storage example.

Author:
Atmel Corporation: http://www.atmel.com
Support and FAQ: http://support.atmel.no/

Definition in file main.c.

#include "nlao_cpu.h"
#include "nlao_usart.h"
#include <stdio.h>
#include "compiler.h"
#include "preprocessor.h"
#include "board.h"
#include "print_funcs.h"
#include "intc.h"
#include "pm.h"
#include "gpio.h"
#include "usart.h"
#include "ctrl_access.h"
#include "main.h"
#include "spi.h"
#include "conf_at45dbx.h"
#include "conf_sd_mmc_spi.h"
#include "mci.h"
#include "conf_sd_mmc_mci.h"
#include "conf_usb.h"
#include "usb_task.h"
#include "device_mass_storage_task.h"
#include "host_mass_storage_task.h"
#include "mmi_task.h"

Go to the source code of this file.

Defines

#define AT45DBX_ENABLE_NPCS_PIN(NPCS, unused)   {AT45DBX_SPI_NPCS##NPCS##_PIN, AT45DBX_SPI_NPCS##NPCS##_FUNCTION},

Functions

int _init_startup (void)
 Low-level initialization routine called during startup, before the main function.
static void at45dbx_resources_init (void)
 Initializes AT45DBX resources: GPIO, SPI and AT45DBX.
static void init_stdio (void)
 Initializes STDIO.
int main (void)
 Main function. Execution starts here.
static void sd_mmc_mci_resources_init (void)
 Initializes SD/MMC resources: GPIO, MCI and SD/MMC.
static void sd_mmc_spi_resources_init (void)
 Initializes SD/MMC resources: GPIO, SPI and SD/MMC.

Variables

System Clock Frequencies
pm_freq_param_t pm_freq_param


Define Documentation

#define AT45DBX_ENABLE_NPCS_PIN ( NPCS,
unused   )     {AT45DBX_SPI_NPCS##NPCS##_PIN, AT45DBX_SPI_NPCS##NPCS##_FUNCTION},

Referenced by at45dbx_resources_init().


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 277 of file main.c.

00278 {
00279   // Import the Exception Vector Base Address.
00280   extern void _evba;
00281 
00282   // Load the Exception Vector Base Address in the corresponding system register.
00283   Set_system_register(AVR32_EVBA, (int)&_evba);
00284 
00285   // Enable exceptions.
00286   Enable_global_exception();
00287 
00288   // Initialize interrupt handling.
00289   INTC_init_interrupts();
00290 
00291   // Don't-care value for GCC.
00292   return 1;
00293 }

static void at45dbx_resources_init ( void   )  [static]

Initializes AT45DBX resources: GPIO, SPI and AT45DBX.

Definition at line 321 of file main.c.

References AT45DBX_ENABLE_NPCS_PIN, AT45DBX_MEM_CNT, AT45DBX_SPI_BITS, AT45DBX_SPI_FIRST_NPCS, AT45DBX_SPI_MASTER_SPEED, and pm_freq_param.

Referenced by main().

00322 {
00323   static const gpio_map_t AT45DBX_SPI_GPIO_MAP =
00324   {
00325     {AT45DBX_SPI_SCK_PIN,          AT45DBX_SPI_SCK_FUNCTION         },  // SPI Clock.
00326     {AT45DBX_SPI_MISO_PIN,         AT45DBX_SPI_MISO_FUNCTION        },  // MISO.
00327     {AT45DBX_SPI_MOSI_PIN,         AT45DBX_SPI_MOSI_FUNCTION        },  // MOSI.
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   // SPI options.
00335   spi_options_t spiOptions =
00336   {
00337     .reg          = AT45DBX_SPI_FIRST_NPCS,   // Defined in conf_at45dbx.h.
00338     .baudrate     = AT45DBX_SPI_MASTER_SPEED, // Defined in conf_at45dbx.h.
00339     .bits         = AT45DBX_SPI_BITS,         // Defined in conf_at45dbx.h.
00340     .spck_delay   = 0,
00341     .trans_delay  = 0,
00342     .stay_act     = 1,
00343     .spi_mode     = 0,
00344     .modfdis      = 1
00345   };
00346 
00347   // Assign I/Os to SPI.
00348   gpio_enable_module(AT45DBX_SPI_GPIO_MAP,
00349                      sizeof(AT45DBX_SPI_GPIO_MAP) / sizeof(AT45DBX_SPI_GPIO_MAP[0]));
00350 
00351   // If the SPI used by the AT45DBX is not enabled.
00352   if (!spi_is_enabled(AT45DBX_SPI))
00353   {
00354     // Initialize as master.
00355     spi_initMaster(AT45DBX_SPI, &spiOptions);
00356 
00357     // Set selection mode: variable_ps, pcs_decode, delay.
00358     spi_selectionMode(AT45DBX_SPI, 0, 0, 0);
00359 
00360     // Enable SPI.
00361     spi_enable(AT45DBX_SPI);
00362   }
00363 
00364   // Initialize data flash with SPI PB clock.
00365   at45dbx_init(spiOptions, pm_freq_param.pba_f);
00366 }

static void init_stdio ( void   )  [static]

Initializes STDIO.

Definition at line 221 of file main.c.

References APPLI_PBA_SPEED, STDIO_USART, STDIO_USART_BAUDRATE, STDIO_USART_RX_FUNCTION, STDIO_USART_RX_PIN, STDIO_USART_TX_FUNCTION, and STDIO_USART_TX_PIN.

Referenced by main().

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   // Initialize the USART used for STDIO.
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   // Initialize the USART used for STDIO.
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 }

int main ( void   ) 

Main function. Execution starts here.

Return values:
42 Fatal error.

Definition at line 470 of file main.c.

References at45dbx_resources_init(), device_mass_storage_task(), device_mass_storage_task_init(), init_stdio(), mmi_task(), mmi_task_init(), pm_freq_param, sd_mmc_mci_resources_init(), and sd_mmc_spi_resources_init().

00471 {
00472   // Set CPU and PBA clock
00473   if( PM_FREQ_STATUS_FAIL==pm_configure_clocks(&pm_freq_param) )
00474      return 42;
00475 
00476   // Initialize the USART for debug purpose.
00477   init_stdio();
00478 
00479   union
00480   {
00481     unsigned long                 scfg;
00482     avr32_hmatrix_scfg_t          SCFG;
00483   } u_avr32_hmatrix_scfg;
00484 
00485   // For the internal-flash HMATRIX slave, use last master as default.
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   // For the internal-SRAM HMATRIX slave, use last master as default.
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   // For the EBI HMATRIX slave, use last master as default.
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   // Initialize USART link.
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   // Initialize USB clock.
00521   pm_configure_usb_clock();
00522 
00523   // Initialize USB tasks.
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 }

static void sd_mmc_mci_resources_init ( void   )  [static]

Initializes SD/MMC resources: GPIO, MCI and SD/MMC.

Definition at line 426 of file main.c.

References pm_freq_param.

Referenced by main().

00427 {
00428   static const gpio_map_t SD_MMC_MCI_GPIO_MAP =
00429   {
00430     {SD_SLOT_8BITS_CLK_PIN,   SD_SLOT_8BITS_CLK_FUNCTION  },  // SD CLK.
00431     {SD_SLOT_8BITS_CMD_PIN,   SD_SLOT_8BITS_CMD_FUNCTION  },  // SD CMD.
00432     {SD_SLOT_8BITS_DATA0_PIN, SD_SLOT_8BITS_DATA0_FUNCTION},  // SD DAT[0].
00433     {SD_SLOT_8BITS_DATA1_PIN, SD_SLOT_8BITS_DATA1_FUNCTION},  // DATA Pin.
00434     {SD_SLOT_8BITS_DATA2_PIN, SD_SLOT_8BITS_DATA2_FUNCTION},  // DATA Pin.
00435     {SD_SLOT_8BITS_DATA3_PIN, SD_SLOT_8BITS_DATA3_FUNCTION},  // DATA Pin.
00436     {SD_SLOT_8BITS_DATA4_PIN, SD_SLOT_8BITS_DATA4_FUNCTION},  // DATA Pin.
00437     {SD_SLOT_8BITS_DATA5_PIN, SD_SLOT_8BITS_DATA5_FUNCTION},  // DATA Pin.
00438     {SD_SLOT_8BITS_DATA6_PIN, SD_SLOT_8BITS_DATA6_FUNCTION},  // DATA Pin.
00439     {SD_SLOT_8BITS_DATA7_PIN, SD_SLOT_8BITS_DATA7_FUNCTION}   // DATA Pin.
00440   };
00441 
00442   // MCI options.
00443   static const mci_options_t MCI_OPTIONS =
00444   {
00445     .card_speed = 400000,
00446     .card_slot  = SD_SLOT_8BITS, // Default card initialization.
00447   };
00448 
00449   // Assign I/Os to MCI.
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   // Enable pull-up for Card Detect.
00454   gpio_enable_pin_pull_up(SD_SLOT_8BITS_CARD_DETECT);
00455 
00456   // Enable pull-up for Write Protect.
00457   gpio_enable_pin_pull_up(SD_SLOT_8BITS_WRITE_PROTECT);
00458 
00459   // Initialize SD/MMC with MCI PB clock.
00460   sd_mmc_mci_init(&MCI_OPTIONS, pm_freq_param.pba_f, pm_freq_param.cpu_f);
00461 }

static void sd_mmc_spi_resources_init ( void   )  [static]

Initializes SD/MMC resources: GPIO, SPI and SD/MMC.

Definition at line 375 of file main.c.

References pm_freq_param.

Referenced by main().

00376 {
00377   static const gpio_map_t SD_MMC_SPI_GPIO_MAP =
00378   {
00379     {SD_MMC_SPI_SCK_PIN,  SD_MMC_SPI_SCK_FUNCTION },  // SPI Clock.
00380     {SD_MMC_SPI_MISO_PIN, SD_MMC_SPI_MISO_FUNCTION},  // MISO.
00381     {SD_MMC_SPI_MOSI_PIN, SD_MMC_SPI_MOSI_FUNCTION},  // MOSI.
00382     {SD_MMC_SPI_NPCS_PIN, SD_MMC_SPI_NPCS_FUNCTION}   // Chip Select NPCS.
00383   };
00384 
00385   // SPI options.
00386   spi_options_t spiOptions =
00387   {
00388     .reg          = SD_MMC_SPI_NPCS,
00389     .baudrate     = SD_MMC_SPI_MASTER_SPEED,  // Defined in conf_sd_mmc_spi.h.
00390     .bits         = SD_MMC_SPI_BITS,          // Defined in conf_sd_mmc_spi.h.
00391     .spck_delay   = 0,
00392     .trans_delay  = 0,
00393     .stay_act     = 1,
00394     .spi_mode     = 0,
00395     .modfdis      = 1
00396   };
00397 
00398   // Assign I/Os to SPI.
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   // If the SPI used by the SD/MMC is not enabled.
00403   if (!spi_is_enabled(SD_MMC_SPI))
00404   {
00405     // Initialize as master.
00406     spi_initMaster(SD_MMC_SPI, &spiOptions);
00407 
00408     // Set selection mode: variable_ps, pcs_decode, delay.
00409     spi_selectionMode(SD_MMC_SPI, 0, 0, 0);
00410 
00411     // Enable SPI.
00412     spi_enable(SD_MMC_SPI);
00413   }
00414 
00415   // Initialize SD/MMC with SPI PB clock.
00416   sd_mmc_spi_init(spiOptions, pm_freq_param.pba_f);
00417 }


Variable Documentation

pm_freq_param_t pm_freq_param

Initial value:

{
   .cpu_f  =       APPLI_CPU_SPEED
,  .pba_f    =     APPLI_PBA_SPEED
,  .osc0_f     =   FOSC0
,  .osc0_startup = OSC0_STARTUP
}

Definition at line 207 of file main.c.

Referenced by at45dbx_resources_init(), main(), mmi_task(), sd_mmc_mci_resources_init(), and sd_mmc_spi_resources_init().


Generated on Fri Feb 19 02:22:54 2010 for AVR32 - EVK1104 Demo by  doxygen 1.5.5