请教s3c6410开发板SD卡不能热插拔问题

duxiaowen   2010-5-16 23:19 楼主
本人在使用s3c6410开发板过程中,SD卡插入其中的一个通道不能实现热插拔。开发板上电以后SD卡插入不能识别,要关掉重新上电才能识别到SD卡的存在,而且在识别以后把SD卡拔出,在Wince界面中仍然有SD卡图标,双击打开图像可以看到原存文件的存在。这个让我很纠结,本人以为是SD卡内容已经部分映射到Nandflash当中,其中SD驱动存在问题,SD卡监测程序有待改进,先把驱动程序贴出,想大家给我分析分析:
// New Constructor for card detect of HSMMC ch0 on SMDK6410.
CSDHController::CSDHController() : CSDHCBase() {
    m_htCardDetectThread = NULL;
    m_hevCardDetectEvent = NULL;  
    m_dwSDDetectSysIntr = SYSINTR_UNDEFINED;   
}
#endif

BOOL CSDHController::Init(LPCTSTR pszActiveKey) {
    RETAILMSG(1,(TEXT("[HSMMC0] Initializing the HSMMC Host Controller\n")));

    // 08.03.22 by KYS
    // HSMMC Ch0 initialization
    if (!InitCh()) return FALSE;
    return CSDHCBase::Init(pszActiveKey);
}

VOID CSDHController::PowerUp() {
    RETAILMSG(1,(TEXT("[HSMMC0] Power Up the HSMMC Host Controller\n")));

    // 08.03.22 by KYS
    // HSMMC Ch0 initialization for "WakeUp"
    if (!InitCh()) return;
    CSDHCBase::PowerUp();
}

extern "C" PCSDHCBase CreateHSMMCHCObject() {
    return new CSDHController;
}

VOID CSDHController::DestroyHSMMCHCObject(PCSDHCBase pSDHC) {
    DEBUGCHK(pSDHC);
    delete pSDHC;
}

// 08.03.14 by KYS
// The function that initilize SYSCON for a clock gating.
BOOL CSDHController::InitClkPwr() {
    volatile S3C6410_SYSCON_REG *pCLKPWR = NULL;

    pCLKPWR = (volatile S3C6410_SYSCON_REG *)DrvLib_MapIoSpace(S3C6410_BASE_REG_PA_SYSCON, sizeof(S3C6410_SYSCON_REG), FALSE);
    if (pCLKPWR == NULL) {
        RETAILMSG(1, (TEXT("[HSMMC0] Clock & Power Management Special Register is *NOT* mapped.\n")));
        return FALSE;
    }

#ifdef _SRCCLK_48MHZ_
    RETAILMSG(_DBG_, (TEXT("[HSMMC0] Setting registers for the USB48MHz (EXTCLK for SDCLK) : SYSCon.\n")));
    // SCLK_HSMMC#_48 : CLK48M_PHY(OTH PHY 48MHz Clock Source from SYSCON block)
    // To use the USB clock, must be set the "USB_SIG_MASK" bit in the syscon register.
    pCLKPWR->OTHERS    |= (0x1<<16);  // set USB_SIG_MASK
    pCLKPWR->HCLK_GATE |= (0x1<<17);    // Gating HCLK for HSMMC1
    pCLKPWR->SCLK_GATE |= (0x1<<27);    // Gating special clock for HSMMC1 (SCLK_MMC1_48)
#else
    RETAILMSG(_DBG_, (TEXT("[HSMMC0] Setting registers for the EPLL (for SDCLK) : SYSCon.\n")));
    // SCLK_HSMMC#  : EPLLout, MPLLout, PLL_source_clk or CLK27 clock
    // (from SYSCON block, can be selected by MMC#_SEL[1:0] fields of the CLK_SRC register in SYSCON block)
    // Set the clock source to EPLL out for CLKMMC1
    pCLKPWR->CLK_SRC   = (pCLKPWR->CLK_SRC & ~(0x3<<18) & ~(0x1<<2)) |  // Control MUX(MMC1:MOUT EPLL)
        (0x1<<2); // Control MUX(EPLL:FOUT EPLL)
    pCLKPWR->HCLK_GATE |= (0x1<<17);  // Gating HCLK for HSMMC1
    pCLKPWR->SCLK_GATE  = (pCLKPWR->SCLK_GATE) | (0x1<<25);  // Gating special clock for HSMMC1 (SCLK_MMC1)
#endif

    DrvLib_UnmapIoSpace((PVOID)pCLKPWR);
    return TRUE;
}

// 08.03.14 by KYS
// The function that initilize GPIO for DAT, CD and WP lines.
BOOL CSDHController::InitGPIO() {
    volatile S3C6410_GPIO_REG *pIOPreg = NULL;

    pIOPreg = (volatile S3C6410_GPIO_REG *)DrvLib_MapIoSpace(S3C6410_BASE_REG_PA_GPIO, sizeof(S3C6410_GPIO_REG), FALSE);
    if (pIOPreg == NULL) {
        RETAILMSG(1, (TEXT("[HSMMC0] GPIO registers is *NOT* mapped.\n")));
        return FALSE;
    }
    RETAILMSG(_DBG_, (TEXT("[HSMMC0] Setting registers for the GPIO.\n")));
    pIOPreg->GPGCON  = (pIOPreg->GPGCON & ~(0xFFFFFF)) | (0x222222);  // 4'b0010 for the MMC 0
    pIOPreg->GPGPUD &= ~(0xFFF); // Pull-up/down disabled

#ifdef _SMDK6410_CH0_WP_
    pIOPreg->GPNCON &= ~(0x3<<24);  // WP_SD0
    pIOPreg->GPNPUD &= ~(0x3<<24);  // Pull-up/down disabled
#endif

#ifndef _SMDK6410_CH0_EXTCD_
    pIOPreg->GPGCON = (pIOPreg->GPGCON & ~(0xF<<24)) | (0x2<<24); // SD CD0
    pIOPreg->GPGPUD &= ~(0x3<<12); // Pull-up/down disabled
#endif

#ifdef _SMDK6410_CH0_EXTCD_
    // 08.03.22 by KYS
    // Setting for card detect pin of HSMMC ch0 on SMDK6410.
    pIOPreg->GPNCON    = ( pIOPreg->GPNCON & ~(0x3<<26) ) | (0x2<<26);    // SD_CD0 by EINT13
    pIOPreg->GPNPUD         = ( pIOPreg->GPNPUD & ~(0x3<<26) ) | (0x0<<26);  // pull-up/down disabled

    pIOPreg->EINT0CON0 = ( pIOPreg->EINT0CON0 & ~(0x7<<24)) | (0x7<<24);    // Both edge triggered
    pIOPreg->EINT0PEND = ( pIOPreg->EINT0PEND | (0x1<<13) );     //clear EINT19 pending bit
    pIOPreg->EINT0MASK = ( pIOPreg->EINT0MASK & ~(0x1<<13));     //enable EINT19
#endif
    DrvLib_UnmapIoSpace((PVOID)pIOPreg);
    return TRUE;
}

// 08.03.14 by KYS
// The function that initilize the register for HSMMC Control.
BOOL CSDHController::InitHSMMC() {
    volatile S3C6410_HSMMC_REG *pHSMMC = NULL;

    pHSMMC = (volatile S3C6410_HSMMC_REG *)DrvLib_MapIoSpace(S3C6410_BASE_REG_PA_HSMMC0, sizeof(S3C6410_HSMMC_REG), FALSE);
    if (pHSMMC == NULL) {
        RETAILMSG(1, (TEXT("[HSMMC0] HSMMC Special Register is *NOT* mapped.\n")));
        return FALSE;
    }

#ifdef _SRCCLK_48MHZ_
    RETAILMSG(_DBG_, (TEXT("[HSMMC0] Setting registers for the USB48MHz (EXTCLK) : HSMMCCon.\n")));
    // Set the clock source to USB_PHY for CLKMMC1
    pHSMMC->CONTROL2 = (pHSMMC->CONTROL2 & ~(0xffffffff)) |
        (0x3<<9) |  // Debounce Filter Count 0x3=64 iSDCLK
        (0x1<<8) |  // SDCLK Hold Enable
        (0x3<<4);   // Base Clock Source = External Clock
#else
    RETAILMSG(_DBG_, (TEXT("[HSMMC0] Setting registers for the EPLL : HSMMCCon.\n")));
    // Set the clock source to EPLL out for CLKMMC1
    pHSMMC->CONTROL2 = (pHSMMC->CONTROL2 & ~(0xffffffff)) |
        (0x3<<9) |  // Debounce Filter Count 0x3=64 iSDCLK
        (0x1<<8) |  // SDCLK Hold Enable
        (0x2<<4);   // Base Clock Source = EPLL out
#endif

    DrvLib_UnmapIoSpace((PVOID)pHSMMC);
    return TRUE;
}

BOOL CSDHController::InitCh() {
    if (!InitClkPwr()) return FALSE;
    if (!InitGPIO()) return FALSE;
    if (!InitHSMMC()) return FALSE;
    return TRUE;
}

#ifdef _SMDK6410_CH0_EXTCD_
// 08.03.22 by KYS
// New function to Card detect thread of HSMMC ch0 on SMDK6410.
DWORD CSDHController::CardDetectThread() {
    BOOL  bSlotStateChanged = FALSE;
    DWORD dwWaitResult  = WAIT_TIMEOUT;
    PCSDHCSlotBase pSlotZero = GetSlot(0);

    CeSetThreadPriority(GetCurrentThread(), 100);

    while(1) {
        // Wait for the next insertion/removal interrupt
        dwWaitResult = WaitForSingleObject(m_hevCardDetectEvent, INFINITE);

        Lock();
        pSlotZero->HandleInterrupt(SDSLOT_INT_CARD_DETECTED);
        Unlock();
        InterruptDone(m_dwSDDetectSysIntr);

        EnableCardDetectInterrupt();
    }

    return TRUE;
}

// 08.03.22 by KYS
// New function to request a SYSINTR for Card detect interrupt of HSMMC ch0 on SMDK6410.
BOOL CSDHController::InitializeHardware() {
    m_dwSDDetectIrq = SD_CD0_IRQ;

    // convert the SDI hardware IRQ into a logical SYSINTR value
    if (!KernelIoControl(IOCTL_HAL_REQUEST_SYSINTR, &m_dwSDDetectIrq, sizeof(DWORD), &m_dwSDDetectSysIntr, sizeof(DWORD), NULL)) {
        // invalid SDDetect SYSINTR value!
        RETAILMSG(1, (TEXT("[HSMMC0] invalid SD detect SYSINTR value!\n")));
        m_dwSDDetectSysIntr = SYSINTR_UNDEFINED;
        return FALSE;
    }

    return CSDHCBase::InitializeHardware();
}

// 08.03.22 by KYS
// New Start function for Card detect of HSMMC ch0 on SMDK6410.
SD_API_STATUS CSDHController::Start() {
    SD_API_STATUS status = SD_API_STATUS_INSUFFICIENT_RESOURCES;

    m_fDriverShutdown = FALSE;

    // allocate the interrupt event
    m_hevInterrupt = CreateEvent(NULL, FALSE, FALSE,NULL);

    if (NULL == m_hevInterrupt)    {
        goto EXIT;
    }

    // initialize the interrupt event
    if (!InterruptInitialize (m_dwSysIntr, m_hevInterrupt, NULL, 0)) {
        goto EXIT;
    }

    m_fInterruptInitialized = TRUE;

    // create the interrupt thread for controller interrupts
    m_htIST = CreateThread(NULL, 0, ISTStub, this, 0, NULL);
    if (NULL == m_htIST) {
        goto EXIT;
    }

    // allocate the card detect event
    m_hevCardDetectEvent = CreateEvent(NULL, FALSE, FALSE,NULL);

    if (NULL == m_hevCardDetectEvent) {
        goto EXIT;
    }

    // initialize the interrupt event
    if (!InterruptInitialize (m_dwSDDetectSysIntr, m_hevCardDetectEvent, NULL, 0)) {
        goto EXIT;
    }

    // create the card detect interrupt thread   
    m_htCardDetectThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)SD_CardDetectThread, this, 0, NULL);
    if (NULL == m_htCardDetectThread)    {
        goto EXIT;
    }

    for (DWORD dwSlot = 0; dwSlot < m_cSlots; ++dwSlot) {
        PCSDHCSlotBase pSlot = GetSlot(dwSlot);
        status = pSlot->Start();

        if (!SD_API_SUCCESS(status)) {
            goto EXIT;
        }
    }

    // wake up the interrupt thread to check the slot
    ::SetInterruptEvent(m_dwSDDetectSysIntr);   

    status = SD_API_STATUS_SUCCESS;

EXIT:
    if (!SD_API_SUCCESS(status)) {
        // Clean up
        Stop();
    }

    return status;
}

// 08.03.22 by KYS
// New function for enabling the Card detect interrupt of HSMMC ch0 on SMDK6410.
BOOL CSDHController::EnableCardDetectInterrupt() {
    volatile S3C6410_GPIO_REG  *pIOPreg = NULL;
    pIOPreg = (volatile S3C6410_GPIO_REG *)DrvLib_MapIoSpace(S3C6410_BASE_REG_PA_GPIO, sizeof(S3C6410_GPIO_REG), FALSE);
    if (pIOPreg == NULL)
    {
        RETAILMSG (1,(TEXT("[HSMMC0] GPIO registers is *NOT* mapped.\n")));
        return FALSE;
    }

    pIOPreg->EINT0PEND = ( pIOPreg->EINT0PEND |  (0x1<<13));     //clear EINT19 pending bit
    pIOPreg->EINT0MASK = ( pIOPreg->EINT0MASK & ~(0x1<<13));     //enable EINT19
    DrvLib_UnmapIoSpace((PVOID)pIOPreg);
    return TRUE;
}
#endif    // The end of BSP_SMDK6410

回复评论 (12)

应该是SD卡插入管腳的见測函数没 作好
点赞  2010-5-16 23:24
pIOPreg->EINT0PEND = ( pIOPreg->EINT0PEND | (0x1<<13)); //clear EINT19 pending bit  
  pIOPreg->EINT0MASK = ( pIOPreg->EINT0MASK & ~(0x1<<13)); //enable EINT19

在这个中断检测里面打出信息来看下,跟踪一下就好了。

是不是很有什么宏没有开。
点赞  2010-5-16 23:51
一般都应该做好的,

程序如果没有问题,

就是你的sd卡座没有焊接好,

虚焊导致的,

你先看看是不是虚焊吧,
点赞  2010-5-16 23:52
楼主 这个问题,应该好好看下 你的SD在位检测 和拔掉 对应的处理函数;

不仅需要在BOOT启动时,对SD在位进行检测,而且还需在OS跑起后,开一个线程或中断,实现SD在位检测;

另外同样需要拔掉 对应的处理函数,当SD拔掉后,删掉其在DDR或SDRAM中的内容。

楼主可以看看:
http://baizhx.blog.sohu.com/106555810.html
http://download.eeworld.net/source/1177533

点赞  2010-5-17 09:19
楼主 好好看看这篇文章,讲得非常详细:
http://www.cnblogs.com/songtitan/archive/2007/02/11/647293.html
点赞  2010-5-17 09:20
引用: 引用 3 楼 laorenshen 的回复:
一般都应该做好的,

程序如果没有问题,

就是你的sd卡座没有焊接好,

虚焊导致的,

你先看看是不是虚焊吧,

买的开发板,用了这么久,应该不会存在虚焊的现象吧。
点赞  2010-5-17 14:58
引用: 引用 4 楼 kyzf 的回复:
楼主 这个问题,应该好好看下 你的SD在位检测 和拔掉 对应的处理函数;

不仅需要在BOOT启动时,对SD在位进行检测,而且还需在OS跑起后,开一个线程或中断,实现SD在位检测;

另外同样需要拔掉 对应的处理函数,当SD拔掉后,删掉其在DDR或SDRAM中的内容。

楼主可以看看:
http://baizhx.blog.sohu.com/106555810.html
http:……

谢谢,我先看看。我用的是winc6.0的系统,这个和5.0有多大区别?
点赞  2010-5-17 14:59
xiexie ,kankan
点赞  2010-5-17 15:02
弱弱的问一句,是不是6410开发板的sd卡两个通道中,一般只能实现其中一个通道热插拔,其他一个通道不行啊?这个通道做其他用途?谢谢!
点赞  2010-5-22 08:26
算了,结了吧!
点赞  2010-5-25 23:51

原因发现了吗

楼主,你好!!在网络上看到你的帖子,我也碰到跟你一样的有关SD卡的问题:"开发板上电以后SD卡插入不能识别,要关掉重新上电才能识别到SD卡的存在",我弄了好久也没弄出来。。不知道您是怎么解决的?可以分享您当时的问题原因吗?
点赞  2012-6-7 15:06

你好

我的邮箱是:    lishengwu@163.com

 

 

谢谢!!!!

 

不胜感激!

点赞  2012-6-7 15:08
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复