//# MENU SD_USB void menu_sdcard(void){ lcdClear(); sdmmc_setup(); uint32_t r = sdmmc_acquire(); if(r) { lcdPrintln("SD card found."); lcdPrintln("No. of blocks:"); lcdPrintln(IntToStr(Chip_SDMMC_GetDeviceBlocks(LPC_SDMMC), 10, 0)); lcdPrintln("in USB MSC mode"); // SD I/O needs higher interrupt prio than USB so it works within // the USB callbacks NVIC_SetPriority(USB0_IRQn, 2); NVIC_SetPriority(SDIO_IRQn, 0); SD_MSCenable(); } else { lcdPrintln("No SD card found."); } lcdDisplay(); while(getInputRaw()==BTN_NONE){ /* wait */ __asm__ volatile ("nop"); }; lcdPrintln("goodbye!"); lcdDisplay(); MSCdisable(); Chip_SDIF_DeInit(LPC_SDMMC); }
/*********************************************************************//** * @brief Main SDIO program body * @param[in] None * @return int **********************************************************************/ void Sdio_init(void) { uint32_t dms, sdio_clk; int32_t loops, blk; uint8_t buf[50]; errorc = 1; /* Configure the IO's for the LEDs */ /* Setup muxing for SD interface */ /*Xplorer*/ scu_pinmux(0x1 ,11 , SDFASTINOUTPIN, FUNC7); /* P1.11 SDIO D2 */ scu_pinmux(0x1 ,10 , SDFASTINOUTPIN, FUNC7); /* P1.10 SDIO D1 */ scu_pinmux(0x1 ,9 , SDFASTINOUTPIN, FUNC7); /* P1.9 SDIO D0 */ scu_pinmux(0x1 ,6, SDFASTINOUTPIN, FUNC7); /* P1.6 SDIO command */ scu_pinmux(0x1 ,12 , SDFASTINOUTPIN, FUNC7); /* P1.12 SDIO D3 */ LPC_SCU->SFSCLK_2 = 0x04; /* CLK2 Enable*/ CGU_EntityConnect(CGU_CLKSRC_PLL1, CGU_BASE_SDIO); /* Generate interrupt @ 100 Hz */ /* The SDIO driver needs to know the SDIO clock rate */ sdio_clk = CGU_GetPCLKFrequency(CGU_PERIPHERAL_SDIO); /* This init sdio with sdio_clk */ sdif_init(sdio_clk, sdmmc_irq); /* Wait for a card to be inserted (note CD is not on the SDMMC power rail and can be polled without enabling SD slot power */ /* Allow some time for the power supply to settle and the card to fully seat in the slot */ dms = u32Milliseconds + 250; while (dms > u32Milliseconds); /* Enumerate the card once detected. Note this function may block for a little while. */ if (!sdmmc_acquire(sdmmc_setup_wakeup, sdmmc_irq_driven_wait, sdmmc_waitms, &sdcardinfo)) { errorc = -1; goto error_exit; } lpc_printf("SD card detected and initialized..!\r\n"); /* Setup card specific callbacks - use driver functions, but these can be changed to custom functions or unique functions per slot. These aren't used in the drivers, so setup of these is optional, but they are setup here to be complete. */ sdcardinfo.sdck_det = sdif_card_ndetect; sdcardinfo.sdck_wp = sdif_card_wp_on; sdcardinfo.sd_setpow = sdif_power_onoff; sdcardinfo.sd_setrst = sdif_reset; memset(lbuff, 0, sizeof(lbuff)); if (sdmmc_read_blocks((void *) lbuff, 0, 0) == 0) { errorc = -2; goto error_exit; } #ifdef ENABLE_WRITE_MODE lpc_printf("Writing data to sector 1 of SD card and verifying:\r\n"); /* Write a single sector of data (512 bytes) */ if (!sdcardinfo.sdck_wp()) { prep_data(0, 1); /* Warning: This may corrupt SD card data! */ if (sdmmc_write_blocks((void *) lbuff, 1, 1) == 0) { errorc = -3; goto error_exit; } /* Wait for write to finish (at the card) */ wait_for_program_finish(); } #endif memset(lbuff, 0, sizeof(lbuff)); /* Read a single sector of data (512 bytes) */ if (sdmmc_read_blocks((void *) lbuff, 1, 1) == 0) { errorc = -4; goto error_exit; } #ifdef ENABLE_WRITE_MODE /* Verify data and halt if an error occurs */ if (!sdcardinfo.sdck_wp()) { if (verify_data(1, 0, 1) == 0) { errorc = -5; goto error_exit; } lpc_printf("Verified!\r\n"); } #endif #ifdef USE_MULTPLE_TRANSFER #ifdef ENABLE_WRITE_MODE lpc_printf("Writing data in Multitransfer mode and verifying:\r\n"); /* Write data using multiple sector write */ if (!sdcardinfo.sdck_wp()) { prep_data(0x10, 3); /* Warning: This may corrupt SD card data! */ if (sdmmc_write_blocks((void *) lbuff, 1, MULT_XFER_MAX_SECTORS) == 0) { errorc = -6; goto error_exit; } wait_for_program_finish(); } #endif memset(lbuff, 0, sizeof(lbuff)); /* Read data using multiple sector read */ if (sdmmc_read_blocks((void *) lbuff, 1, MULT_XFER_MAX_SECTORS) == 0) { errorc = -7; goto error_exit; } #ifdef ENABLE_WRITE_MODE /* Verify data and halt if an error occurs */ if (!sdcardinfo.sdck_wp()) { if (verify_data(MULT_XFER_MAX_SECTORS, 0x10, 3) == 0) { errorc = -8; goto error_exit; } lpc_printf("Verified!\r\n"); } #endif lpc_printf("measure continuous read speed...\r\n"); tstartr = u32Milliseconds; loops = 1000; blk = 64; while (loops-- > 0) { if (sdmmc_read_blocks((void *) lbuff, blk, (blk + MULT_XFER_MAX_SECTORS - 1)) == 0) { errorc = -8; goto error_exit; } blk += MULT_XFER_MAX_SECTORS; } tstopr = u32Milliseconds; memset(buf, 0, sizeof(buf)); lpc_printf("read speed = %d kB/s\r\n", MULT_XFER_MAX_SECTORS*512*1000/(tstopr-tstartr)); #ifdef ENABLE_WRITE_MODE lpc_printf("measure continuous write speed...\r\n"); if (!sdcardinfo.sdck_wp()) { tstartw = u32Milliseconds; loops = 200; blk = 64; while (loops-- > 0) { if (sdmmc_write_blocks((void *) lbuff, blk, (blk + MULT_XFER_MAX_SECTORS - 1)) == 0) { errorc = -9; goto error_exit; } blk += MULT_XFER_MAX_SECTORS; wait_for_program_finish(); } tstopw = u32Milliseconds; } memset(buf, 0, sizeof(buf)); lpc_printf("write speed = %d kB/s\r\n", MULT_XFER_MAX_SECTORS*512*200/(tstopw-tstartw)); #endif #endif goto exit; error_exit: lpc_printf("SD Card read/write operation not succeeded.\r\n"); Sdio_verification = FALSE; exit: return; }