00001
00096 #include <avr32/io.h>
00097 #include "pm.h"
00098 #include "gpio.h"
00099 #include "intc.h"
00100 #include "print_funcs.h"
00101 #include "sd_mmc_mci.h"
00102 #include "sd_mmc_mci_mem.h"
00103 #include "cycle_counter.h"
00104
00106 #include "conf_sd_mmc_mci.h"
00107
00109 #define APPLI_CPU_SPEED 48000000
00110 #define APPLI_PBA_SPEED 48000000
00111 #define PBB_HZ pm_freq_param.pba_f
00112 #define BENCH_TIME_MS 3000 // unit is in ms.
00113 #define BENCH_START_SECTOR 0x15000
00114 #define EXAMPLE_SD_SLOT SD_SLOT_4BITS // SD_SLOT_8BITS or SD_SLOT_4BITS
00115 #define ALLOCATED_SECTORS 16
00116
00117 #if (defined __GNUC__) && (defined __AVR32__)
00118 __attribute__((__aligned__(4)))
00119 #elif (defined __ICCAVR32__)
00120 #pragma data_alignment = 4
00121 #endif
00122 U8 buffer_in[SD_MMC_SECTOR_SIZE*ALLOCATED_SECTORS];
00123
00124 #if (defined __GNUC__) && (defined __AVR32__)
00125 __attribute__((__aligned__(4)))
00126 #elif (defined __ICCAVR32__)
00127 #pragma data_alignment = 4
00128 #endif
00129 U8 buffer_saved[SD_MMC_SECTOR_SIZE];
00130
00131 pm_freq_param_t pm_freq_param=
00132 {
00133 .cpu_f = APPLI_CPU_SPEED
00134 , .pba_f = APPLI_PBA_SPEED
00135 , .osc0_f = FOSC0
00136 , .osc0_startup = OSC0_STARTUP
00137 };
00138
00141 static void init_hmatrix(void)
00142 {
00143
00144
00145 avr32_hmatrix_scfg_t scfg = AVR32_HMATRIX.SCFG[AVR32_HMATRIX_SLAVE_FLASH];
00146 scfg.defmstr_type = AVR32_HMATRIX_DEFMSTR_TYPE_LAST_DEFAULT;
00147 AVR32_HMATRIX.SCFG[AVR32_HMATRIX_SLAVE_FLASH] = scfg;
00148 }
00149
00150
00153 static void sd_mmc_mci_resources_init(void)
00154 {
00155
00156 #if EXAMPLE_SD_SLOT == SD_SLOT_8BITS
00157 static const gpio_map_t SD_MMC_MCI_GPIO_MAP =
00158 {
00159 {SD_SLOT_8BITS_CMD_PIN, SD_SLOT_8BITS_CMD_FUNCTION },
00160 {SD_SLOT_8BITS_CLK_PIN, SD_SLOT_8BITS_CLK_FUNCTION},
00161 {SD_SLOT_8BITS_DATA0_PIN, SD_SLOT_8BITS_DATA0_FUNCTION},
00162 {SD_SLOT_8BITS_DATA1_PIN, SD_SLOT_8BITS_DATA1_FUNCTION},
00163 {SD_SLOT_8BITS_DATA2_PIN, SD_SLOT_8BITS_DATA2_FUNCTION},
00164 {SD_SLOT_8BITS_DATA3_PIN, SD_SLOT_8BITS_DATA3_FUNCTION},
00165 {SD_SLOT_8BITS_DATA4_PIN, SD_SLOT_8BITS_DATA4_FUNCTION},
00166 {SD_SLOT_8BITS_DATA5_PIN, SD_SLOT_8BITS_DATA5_FUNCTION},
00167 {SD_SLOT_8BITS_DATA6_PIN, SD_SLOT_8BITS_DATA6_FUNCTION},
00168 {SD_SLOT_8BITS_DATA7_PIN, SD_SLOT_8BITS_DATA7_FUNCTION}
00169 };
00170 #elif EXAMPLE_SD_SLOT == SD_SLOT_4BITS
00171 static const gpio_map_t SD_MMC_MCI_GPIO_MAP =
00172 {
00173 {SD_SLOT_4BITS_CMD_PIN, SD_SLOT_4BITS_CMD_FUNCTION },
00174 {SD_SLOT_4BITS_CLK_PIN, SD_SLOT_4BITS_CLK_FUNCTION},
00175 {SD_SLOT_4BITS_DATA0_PIN, SD_SLOT_4BITS_DATA0_FUNCTION},
00176 {SD_SLOT_4BITS_DATA1_PIN, SD_SLOT_4BITS_DATA1_FUNCTION},
00177 {SD_SLOT_4BITS_DATA2_PIN, SD_SLOT_4BITS_DATA2_FUNCTION},
00178 {SD_SLOT_4BITS_DATA3_PIN, SD_SLOT_4BITS_DATA3_FUNCTION}
00179 };
00180 #else
00181 # error SD_SLOT not supported
00182 #endif
00183
00184
00185 static const mci_options_t MCI_OPTIONS =
00186 {
00187 .card_speed = 400000,
00188 .card_slot = EXAMPLE_SD_SLOT,
00189 };
00190
00191
00192 gpio_enable_module(SD_MMC_MCI_GPIO_MAP,
00193 sizeof(SD_MMC_MCI_GPIO_MAP) / sizeof(SD_MMC_MCI_GPIO_MAP[0]));
00194
00195
00196 sd_mmc_mci_init(&MCI_OPTIONS, PBB_HZ, pm_freq_param.cpu_f);
00197 }
00198
00201 static void display_perf_bps(U32 bandwidth)
00202 {
00203 if( bandwidth<10000 )
00204 {
00205 print_dbg("Bandwidth: ");
00206 print_dbg(CL_GREEN);
00207 print_dbg_ulong( bandwidth );
00208 print_dbg(CL_BLACK);
00209 print_dbg(" Bps\n");
00210 }
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220 else
00221 {
00222 print_dbg("Bandwidth: ");
00223 print_dbg(CL_GREEN);
00224
00225 print_dbg_ulong( bandwidth/1000 );
00226 print_dbg(CL_BLACK);
00227 print_dbg(" KBps\n");
00228
00229 }
00230 }
00231
00232 static Bool test_sd_mmc_write(U8 pattern_id)
00233 {
00234 U16 i = 0;
00235 U16 j = 0;
00236 Bool b_error;
00237 Ctrl_status status;
00238
00239 print_dbg("Testing pattern ");
00240 print_dbg_char_hex( pattern_id );
00241
00242 status = sd_mmc_mci_mem_2_ram(EXAMPLE_SD_SLOT, 0, buffer_saved);
00243 if( status!=CTRL_GOOD )
00244 {
00245 print_dbg("\nERROR 001: can not read device.\r\n");
00246 return FALSE;
00247 }
00248
00249
00250 for( i=0 ; i<SD_MMC_SECTOR_SIZE ; i++ )
00251 {
00252 switch( pattern_id )
00253 {
00254 case 0:
00255 buffer_in[i] = i;
00256 break;
00257
00258 case 1:
00259 buffer_in[i] = 0xAA;
00260 break;
00261
00262 case 2:
00263 buffer_in[i] = 0x55;
00264 break;
00265
00266 default:
00267 buffer_in[i] = (256-(U8)i);
00268 break;
00269 }
00270 }
00271
00272
00273 status = sd_mmc_mci_ram_2_mem(EXAMPLE_SD_SLOT, 0, buffer_in);
00274 if( status!=CTRL_GOOD )
00275 {
00276 print_dbg("\nERROR: can not write device.\r\n");
00277 return FALSE;
00278 }
00279
00280
00281 for( i=0 ; i<SD_MMC_SECTOR_SIZE ; i++ )
00282 buffer_in[i] = 0;
00283
00284
00285 status = sd_mmc_mci_mem_2_ram(EXAMPLE_SD_SLOT, 0, buffer_in);
00286 if( status!=CTRL_GOOD )
00287 {
00288 print_dbg("\nERROR 002: can not read device.\r\n");
00289 return FALSE;
00290 }
00291
00292
00293 b_error = FALSE;
00294 for( i=0 ; i<SD_MMC_SECTOR_SIZE ; i++ )
00295 {
00296 switch( pattern_id )
00297 {
00298 case 0:
00299 if( buffer_in[i]!=(i%256) )
00300 b_error = TRUE;
00301 break;
00302
00303 case 1:
00304 if( buffer_in[i]!=0xAA )
00305 b_error = TRUE;
00306 break;
00307
00308 case 2:
00309 if( buffer_in[i]!=0x55 )
00310 b_error = TRUE;
00311 break;
00312
00313 default:
00314 if( buffer_in[i]!=((256-(U8)i)%256) )
00315 b_error = TRUE;
00316 break;
00317 }
00318
00319 if( b_error )
00320 {
00321 print_dbg("\nERROR: pattern comparaison failed.\r\n");
00322 for( i=0 ; i<SD_MMC_SECTOR_SIZE ; i++ )
00323 {
00324 print_dbg_char_hex(buffer_in[i]);
00325 j++;
00326 if (j%32==0)
00327 print_dbg("\n"), j=0;
00328 else if (j%4==0)
00329 print_dbg(" ");
00330 }
00331 return FALSE;
00332 }
00333 }
00334
00335
00336 status = sd_mmc_mci_ram_2_mem(EXAMPLE_SD_SLOT, 0, buffer_saved);
00337 if( status!=CTRL_GOOD )
00338 {
00339 print_dbg("\nERROR: can not write device.\r\n");
00340 return FALSE;
00341 }
00342
00343
00344 status = sd_mmc_mci_mem_2_ram(EXAMPLE_SD_SLOT, 0, buffer_in);
00345 if( status!=CTRL_GOOD )
00346 {
00347 print_dbg("\nERROR 003: can not read device.\r\n");
00348 return FALSE;
00349 }
00350 for (i=0;i<(512);i++)
00351 {
00352 if( buffer_in[i]!=buffer_saved[i] )
00353 {
00354 print_dbg("\nERROR: old sector is not correctly restored.\r\n");
00355 return FALSE;
00356 }
00357 }
00358 print_dbg(" [PASS].\r\n");
00359 return TRUE;
00360 }
00361
00364 int main(void)
00365 {
00366 U32 n_sector = 0;
00367 U32 card_size;
00368 U32 bench_start_sector;
00369 U16 i = 0;
00370 U16 j = 0;
00371 t_cpu_time timer;
00372 Ctrl_status status;
00373
00374
00375 if( PM_FREQ_STATUS_FAIL==pm_configure_clocks(&pm_freq_param) )
00376 return 42;
00377
00378
00379 init_hmatrix();
00380
00381
00382 init_dbg_rs232(pm_freq_param.pba_f);
00383
00384
00385 print_dbg("\nInitialize SD/MMC driver");
00386
00387
00388 sd_mmc_mci_resources_init();
00389
00390
00391 #if (BOARD == EVK1104)
00392 #if EXAMPLE_SD_SLOT == SD_SLOT_8BITS
00393 print_dbg("\nInsert a MMC/SD card into the SD/MMC 8bits slot...");
00394 #elif EXAMPLE_SD_SLOT == SD_SLOT_4BITS
00395 print_dbg("\nInsert a MMC/SD card into the SD/MMC 4bits slot...");
00396 #else
00397 # error SD_SLOT not supported
00398 #endif
00399 while ( sd_mmc_mci_mem_check(EXAMPLE_SD_SLOT) != OK );
00400 #else
00401 # error Board not supported
00402 #endif
00403
00404 print_dbg("Card detected!\n");
00405
00406
00407 sd_mmc_mci_read_capacity(EXAMPLE_SD_SLOT, &card_size);
00408 print_dbg("\nCapacity = ");
00409 print_dbg_ulong(card_size*512);
00410 print_dbg(" Bytes\n");
00411
00412
00413
00414
00415 status = sd_mmc_mci_mem_2_ram(EXAMPLE_SD_SLOT, 0, buffer_in);
00416 if( status!=CTRL_GOOD )
00417 {
00418 print_dbg("\nERROR: can not read device.\r\n");
00419 return -1;
00420 }
00421
00422
00423 print_dbg("\nFirst sector of the card:\n");
00424 for (i=0;i<(512);i++)
00425 {
00426 print_dbg_char_hex(buffer_in[i]);
00427 j++;
00428 if (j%32==0)
00429 print_dbg("\n"), j=0;
00430 else if (j%4==0)
00431 print_dbg(" ");
00432 }
00433
00434
00435
00436
00437 print_dbg("Testing write.\r\n");
00438 if( !test_sd_mmc_write(0) ) return -1;
00439 if( !test_sd_mmc_write(1) ) return -1;
00440 if( !test_sd_mmc_write(2) ) return -1;
00441 if( !test_sd_mmc_write(3) ) return -1;
00442
00443
00444
00445
00446 print_dbg("Benching single-block read (without DMA). Please wait...");
00447 n_sector =
00448 bench_start_sector = (card_size<BENCH_START_SECTOR) ? 0 : BENCH_START_SECTOR;
00449
00450 cpu_set_timeout( cpu_ms_2_cy(BENCH_TIME_MS, pm_freq_param.cpu_f), &timer);
00451 while( !cpu_is_timeout(&timer) )
00452 {
00453 status = sd_mmc_mci_mem_2_ram(EXAMPLE_SD_SLOT, n_sector, buffer_in);
00454 if( status!=CTRL_GOOD )
00455 {
00456 print_dbg("\nERROR: can not read device.\r\n");
00457 return -1;
00458 }
00459 n_sector+=1;
00460 }
00461 display_perf_bps((((U64)n_sector-bench_start_sector)*512)/(BENCH_TIME_MS/1000));
00462
00463
00464
00465
00466
00467 print_dbg("Benching single-block read (with DMA). Please wait...");
00468 n_sector =
00469 bench_start_sector = (card_size<BENCH_START_SECTOR) ? 0 : BENCH_START_SECTOR;
00470
00471 cpu_set_timeout( cpu_ms_2_cy(BENCH_TIME_MS, pm_freq_param.cpu_f), &timer);
00472 while( !cpu_is_timeout(&timer) )
00473 {
00474 status = sd_mmc_mci_dma_mem_2_ram(EXAMPLE_SD_SLOT, n_sector, buffer_in);
00475 if( status!=CTRL_GOOD )
00476 {
00477 print_dbg("\nERROR: can not read device.\r\n");
00478 return -1;
00479 }
00480 n_sector+=1;
00481 }
00482 display_perf_bps((((U64)n_sector-bench_start_sector)*512)/(BENCH_TIME_MS/1000));
00483
00484
00485
00486
00487
00488 print_dbg("Benching multi-block read (without DMA). Please wait...");
00489 n_sector =
00490 bench_start_sector = (card_size<BENCH_START_SECTOR) ? 0 : BENCH_START_SECTOR;
00491
00492 cpu_set_timeout( cpu_ms_2_cy(BENCH_TIME_MS, pm_freq_param.cpu_f), &timer);
00493 while( !cpu_is_timeout(&timer) )
00494 {
00495 status = sd_mmc_mci_multiple_mem_2_ram(EXAMPLE_SD_SLOT, n_sector, buffer_in, ALLOCATED_SECTORS);
00496 if( status!=CTRL_GOOD )
00497 {
00498 print_dbg("\nERROR: can not read device.\r\n");
00499 return -1;
00500 }
00501 n_sector+=ALLOCATED_SECTORS;
00502 }
00503 display_perf_bps((((U64)n_sector-bench_start_sector)*512)/(BENCH_TIME_MS/1000));
00504
00505
00506
00507
00508
00509 print_dbg("Benching multi-block read (with DMA). Please wait...");
00510 n_sector =
00511 bench_start_sector = (card_size<BENCH_START_SECTOR) ? 0 : BENCH_START_SECTOR;
00512
00513 cpu_set_timeout( cpu_ms_2_cy(BENCH_TIME_MS, pm_freq_param.cpu_f), &timer);
00514 while( !cpu_is_timeout(&timer) )
00515 {
00516 status = sd_mmc_mci_dma_multiple_mem_2_ram(EXAMPLE_SD_SLOT, n_sector, buffer_in, ALLOCATED_SECTORS);
00517 if( status!=CTRL_GOOD )
00518 {
00519 print_dbg("\nERROR: can not read device.\r\n");
00520 return -1;
00521 }
00522 n_sector+=ALLOCATED_SECTORS;
00523 }
00524 display_perf_bps((((U64)n_sector-bench_start_sector)*512)/(BENCH_TIME_MS/1000));
00525
00526
00527
00528
00529
00530 print_dbg("Benching single-block write (without DMA). Please wait...");
00531 status = sd_mmc_mci_mem_2_ram(EXAMPLE_SD_SLOT, 0, buffer_in);
00532 if( status!=CTRL_GOOD )
00533 {
00534 print_dbg("\nERROR: can not read device.\r\n");
00535 return -1;
00536 }
00537
00538 n_sector =
00539 bench_start_sector = (card_size<BENCH_START_SECTOR) ? 0 : BENCH_START_SECTOR;
00540 cpu_set_timeout( cpu_ms_2_cy(BENCH_TIME_MS, pm_freq_param.cpu_f), &timer);
00541 while( !cpu_is_timeout(&timer) )
00542 {
00543 status = sd_mmc_mci_ram_2_mem(EXAMPLE_SD_SLOT, n_sector, buffer_in);
00544 if( status!=CTRL_GOOD )
00545 {
00546 print_dbg("\nERROR: can not write device.\r\n");
00547 return -1;
00548 }
00549 n_sector+=1;
00550 }
00551 display_perf_bps((((U64)n_sector-bench_start_sector)*512)/(BENCH_TIME_MS/1000));
00552
00553
00554
00555
00556
00557 print_dbg("Benching single-block write (with DMA). Please wait...");
00558 status = sd_mmc_mci_mem_2_ram(EXAMPLE_SD_SLOT, 0, buffer_in);
00559 if( status!=CTRL_GOOD )
00560 {
00561 print_dbg("\nERROR: can not read device.\r\n");
00562 return -1;
00563 }
00564
00565 n_sector =
00566 bench_start_sector = (card_size<BENCH_START_SECTOR) ? 0 : BENCH_START_SECTOR;
00567 cpu_set_timeout( cpu_ms_2_cy(BENCH_TIME_MS, pm_freq_param.cpu_f), &timer);
00568 while( !cpu_is_timeout(&timer) )
00569 {
00570 status = sd_mmc_mci_dma_ram_2_mem(EXAMPLE_SD_SLOT, n_sector, buffer_in);
00571 if( status!=CTRL_GOOD )
00572 {
00573 print_dbg("\nERROR: can not write device.\r\n");
00574 return -1;
00575 }
00576 n_sector+=1;
00577 }
00578 display_perf_bps((((U64)n_sector-bench_start_sector)*512)/(BENCH_TIME_MS/1000));
00579
00580
00581
00582
00583
00584 print_dbg("Benching multi-block write (without DMA). Please wait...");
00585 status = sd_mmc_mci_mem_2_ram(EXAMPLE_SD_SLOT, 0, buffer_in);
00586 if( status!=CTRL_GOOD )
00587 {
00588 print_dbg("\nERROR: can not read device.\r\n");
00589 return -1;
00590 }
00591
00592 n_sector =
00593 bench_start_sector = (card_size<BENCH_START_SECTOR) ? 0 : BENCH_START_SECTOR;
00594 cpu_set_timeout( cpu_ms_2_cy(BENCH_TIME_MS, pm_freq_param.cpu_f), &timer);
00595 while( !cpu_is_timeout(&timer) )
00596 {
00597 status = sd_mmc_mci_multiple_ram_2_mem(EXAMPLE_SD_SLOT, n_sector, buffer_in, ALLOCATED_SECTORS);
00598 if( status!=CTRL_GOOD )
00599 {
00600 print_dbg("\nERROR: can not write device.\r\n");
00601 return -1;
00602 }
00603 n_sector+=ALLOCATED_SECTORS;
00604 }
00605 display_perf_bps((((U64)n_sector-bench_start_sector)*512)/(BENCH_TIME_MS/1000));
00606
00607
00608
00609
00610
00611 print_dbg("Benching multi-block write (with DMA). Please wait...");
00612 status = sd_mmc_mci_mem_2_ram(EXAMPLE_SD_SLOT, 0, buffer_in);
00613 if( status!=CTRL_GOOD )
00614 {
00615 print_dbg("\nERROR: can not read device.\r\n");
00616 return -1;
00617 }
00618
00619 n_sector =
00620 bench_start_sector = (card_size<BENCH_START_SECTOR) ? 0 : BENCH_START_SECTOR;
00621 cpu_set_timeout( cpu_ms_2_cy(BENCH_TIME_MS, pm_freq_param.cpu_f), &timer);
00622 while( !cpu_is_timeout(&timer) )
00623 {
00624 status = sd_mmc_mci_dma_multiple_ram_2_mem(EXAMPLE_SD_SLOT, n_sector, buffer_in, ALLOCATED_SECTORS);
00625 if( status!=CTRL_GOOD )
00626 {
00627 print_dbg("\nERROR: can not write device.\r\n");
00628 return -1;
00629 }
00630 n_sector+=ALLOCATED_SECTORS;
00631 }
00632 display_perf_bps((((U64)n_sector-bench_start_sector)*512)/(BENCH_TIME_MS/1000));
00633
00634 return 0;
00635 }