00001
00097 #include <avr32/io.h>
00098 #include "compiler.h"
00099 #include "board.h"
00100 #include "power_clocks_lib.h"
00101 #include "gpio.h"
00102 #include "spi.h"
00103 #include "conf_sd_mmc_spi.h"
00104 #include "sd_mmc_spi.h"
00105 #include "usart.h"
00106 #include "print_funcs.h"
00107 #include "pdca.h"
00108 #include "intc.h"
00109
00110
00111
00112 const char dummy_data[] =
00113 #include "dummy.h"
00114 ;
00115
00116
00118 #define PBA_HZ FOSC0
00119
00121 #define BUFFERSIZE 64
00122
00123 #if BOARD==EVK1100
00124 #define AVR32_PDCA_CHANNEL_USED_RX AVR32_PDCA_PID_SPI1_RX
00125 #define AVR32_PDCA_CHANNEL_USED_TX AVR32_PDCA_PID_SPI1_TX
00126 #elif BOARD==EVK1101
00127 #define AVR32_PDCA_CHANNEL_USED_RX AVR32_PDCA_PID_SPI_RX
00128 #define AVR32_PDCA_CHANNEL_USED_TX AVR32_PDCA_PID_SPI_TX
00129 #elif BOARD==UC3C_EK
00130 #define AVR32_PDCA_CHANNEL_USED_RX AVR32_PDCA_PID_SPI1_RX
00131 #define AVR32_PDCA_CHANNEL_USED_TX AVR32_PDCA_PID_SPI1_TX
00132 #elif BOARD==EVK1105
00133 #define AVR32_PDCA_CHANNEL_USED_RX AVR32_PDCA_PID_SPI0_RX
00134 #define AVR32_PDCA_CHANNEL_USED_TX AVR32_PDCA_PID_SPI0_TX
00135 #else
00136 # error No known AVR32 board defined
00137 #endif
00138 #define AVR32_PDCA_CHANNEL_SPI_RX 0 // In the example we will use the pdca channel 0.
00139 #define AVR32_PDCA_CHANNEL_SPI_TX 1 // In the example we will use the pdca channel 1.
00140
00141
00142 volatile avr32_pdca_channel_t* pdca_channelrx ;
00143 volatile avr32_pdca_channel_t* pdca_channeltx ;
00144
00145
00146 volatile Bool end_of_transfer;
00147
00148
00149 volatile char ram_buffer[1000];
00150
00151
00152
00153 void wait()
00154 {
00155 volatile int i;
00156 for(i = 0 ; i < 5000; i++);
00157 }
00158
00159
00160
00161
00162
00163 #if defined (__GNUC__)
00164 __attribute__((__interrupt__))
00165 #elif defined (__ICCAVR32__)
00166 __interrupt
00167 #endif
00168 static void pdca_int_handler(void)
00169 {
00170
00171 Disable_global_interrupt();
00172
00173
00174 pdca_disable_interrupt_transfer_complete(AVR32_PDCA_CHANNEL_SPI_RX);
00175
00176 sd_mmc_spi_read_close_PDCA();
00177 wait();
00178
00179 pdca_disable(AVR32_PDCA_CHANNEL_SPI_TX);
00180 pdca_disable(AVR32_PDCA_CHANNEL_SPI_RX);
00181
00182
00183 Enable_global_interrupt();
00184
00185 end_of_transfer = TRUE;
00186 }
00187
00188
00191 static void sd_mmc_resources_init(void)
00192 {
00193
00194 static const gpio_map_t SD_MMC_SPI_GPIO_MAP =
00195 {
00196 {SD_MMC_SPI_SCK_PIN, SD_MMC_SPI_SCK_FUNCTION },
00197 {SD_MMC_SPI_MISO_PIN, SD_MMC_SPI_MISO_FUNCTION},
00198 {SD_MMC_SPI_MOSI_PIN, SD_MMC_SPI_MOSI_FUNCTION},
00199 {SD_MMC_SPI_NPCS_PIN, SD_MMC_SPI_NPCS_FUNCTION}
00200 };
00201
00202
00203 spi_options_t spiOptions =
00204 {
00205 .reg = SD_MMC_SPI_NPCS,
00206 .baudrate = SD_MMC_SPI_MASTER_SPEED,
00207 .bits = SD_MMC_SPI_BITS,
00208 .spck_delay = 0,
00209 .trans_delay = 0,
00210 .stay_act = 1,
00211 .spi_mode = 0,
00212 .modfdis = 1
00213 };
00214
00215
00216 gpio_enable_module(SD_MMC_SPI_GPIO_MAP,
00217 sizeof(SD_MMC_SPI_GPIO_MAP) / sizeof(SD_MMC_SPI_GPIO_MAP[0]));
00218
00219
00220 spi_initMaster(SD_MMC_SPI, &spiOptions);
00221
00222
00223 spi_selectionMode(SD_MMC_SPI, 0, 0, 0);
00224
00225
00226 spi_enable(SD_MMC_SPI);
00227
00228
00229 sd_mmc_spi_init(spiOptions, PBA_HZ);
00230 }
00231
00232
00235 void local_pdca_init(void)
00236 {
00237
00238 pdca_channel_options_t pdca_options_SPI_RX ={
00239
00240 .addr = ram_buffer,
00241
00242
00243 .size = 512,
00244 .r_addr = NULL,
00245 .r_size = 0,
00246 .pid = AVR32_PDCA_CHANNEL_USED_RX,
00247 .transfer_size = PDCA_TRANSFER_SIZE_BYTE
00248 };
00249
00250
00251 pdca_channel_options_t pdca_options_SPI_TX ={
00252
00253 .addr = (void *)&dummy_data,
00254
00255
00256 .size = 512,
00257 .r_addr = NULL,
00258 .r_size = 0,
00259 .pid = AVR32_PDCA_CHANNEL_USED_TX,
00260 .transfer_size = PDCA_TRANSFER_SIZE_BYTE
00261 };
00262
00263
00264 pdca_init_channel(AVR32_PDCA_CHANNEL_SPI_TX, &pdca_options_SPI_TX);
00265
00266
00267 pdca_init_channel(AVR32_PDCA_CHANNEL_SPI_RX, &pdca_options_SPI_RX);
00268
00270 INTC_register_interrupt(&pdca_int_handler, AVR32_PDCA_IRQ_0, AVR32_INTC_INT1);
00271
00272 }
00273
00274
00277 int main(void)
00278 {
00279 int i, j;
00280
00281
00282
00283 pcl_switch_to_osc(PCL_OSC0, FOSC0, OSC0_STARTUP);
00284
00285
00286 init_dbg_rs232(PBA_HZ);
00287
00288
00289 print_dbg("\nInit SD/MMC Driver");
00290 print_dbg("\nInsert SD/MMC...");
00291
00292
00293 INTC_init_interrupts();
00294
00295
00296 sd_mmc_resources_init();
00297
00298
00299 while ( sd_mmc_spi_mem_check() != OK );
00300 print_dbg("\nCard detected!");
00301
00302
00303 sd_mmc_spi_get_capacity();
00304 print_dbg("Capacity = ");
00305 print_dbg_ulong(capacity >> 20);
00306 print_dbg(" MBytes");
00307
00308
00309 Enable_global_interrupt();
00310
00311
00312 local_pdca_init();
00313
00314
00315 for(j = 1; j <= 3; j++)
00316 {
00317
00318 pdca_load_channel( AVR32_PDCA_CHANNEL_SPI_RX,
00319 &ram_buffer,
00320 512);
00321
00322 pdca_load_channel( AVR32_PDCA_CHANNEL_SPI_TX,
00323 (void *)&dummy_data,
00324 512);
00325
00326 end_of_transfer = FALSE;
00327
00328 if(sd_mmc_spi_read_open_PDCA (j) == OK)
00329 {
00330 print_dbg("\nFirst 512 Bytes of Transfer number ");
00331 print_dbg_ulong(j);
00332 print_dbg(" :\n");
00333
00334 spi_write(SD_MMC_SPI,0xFF);
00335 pdca_enable_interrupt_transfer_complete(AVR32_PDCA_CHANNEL_SPI_RX);
00336 pdca_channelrx =(volatile avr32_pdca_channel_t*) pdca_get_handler(AVR32_PDCA_CHANNEL_SPI_RX);
00337 pdca_channeltx =(volatile avr32_pdca_channel_t*) pdca_get_handler(AVR32_PDCA_CHANNEL_SPI_TX);
00338 pdca_channelrx->cr = AVR32_PDCA_TEN_MASK;
00339 pdca_channeltx->cr = AVR32_PDCA_TEN_MASK;
00340
00341 while(!end_of_transfer);
00342
00343
00344 for( i = 0; i < 20; i++)
00345 {
00346 print_dbg_char_hex( (U8)(*(ram_buffer + i)));
00347 }
00348 }
00349 else
00350 {
00351 print_dbg("\n! Unable to open memory \n");
00352 }
00353 }
00354 print_dbg("\nEnd of the example.\n");
00355
00356 while (1);
00357 }