📄 sd_pap.c
字号:
/***************************************************************************
* File: sd_pap.c - Plug and Play
*
* The content of this file or document is CONFIDENTIAL and PROPRIETARY
* to Jade Technologies Co., Ltd. It is subject to the terms of a
* License Agreement between Licensee and Jade Technologies Co., Ltd.
* restricting among other things, the use, reproduction, distribution
* and transfer. Each of the embodiments, including this information
* and any derivative work shall retain this copyright notice.
*
* Copyright (c) 2005 Jade Technologies Co., Ltd.
* All rights reserved.
****************************************************************************/
#include "sd.h"
//------------------------------------------------------------------------------
// Global Variables in SD_PAP.C
//------------------------------------------------------------------------------
extern int g_CardStatus;
extern int g_ExcCnt;
extern HANDLE IdleEvent;
//------------------------------------------------------------------------------
//
// Inform OS that the card is inserted
// See also: SDI_DetectStatus
//
//------------------------------------------------------------------------------
void SDI_DetectInsert(void)
{
GUID uid = { 0xA4E7EDDA , 0xE575 , 0x4252 ,
{0x9D, 0x6B, 0x41, 0x95,0xD4,0x8B,0xB8,0x65 } };
RETAILMSG(1, (_T("SD: DetectInsert(*)\r\n")));
AdvertiseInterface( &uid , L"SDI1:" , TRUE );
}
//------------------------------------------------------------------------------
//
// Inform OS that the card is removed
// See also: SDI_DetectStatus
//
//------------------------------------------------------------------------------
void SDI_DetectRemove(void)
{
GUID uid = { 0xA4E7EDDA , 0xE575 , 0x4252 ,
{0x9D, 0x6B, 0x41, 0x95,0xD4,0x8B,0xB8,0x65 } };
RETAILMSG(1, (_T("SD: DetectRemove(*)\r\n")));
AdvertiseInterface( &uid , L"SDI1:" , FALSE );
}
//------------------------------------------------------------------------------
//
// Plug & Play Thread - created when the driver is loaded, and check the status
// of the card. When the status changed, the routine called the two functions
// above, to force OS open or close an instance.
//
// Arguments:
// controller - handle of the instance passed from the main thread.
//
//------------------------------------------------------------------------------
unsigned long WINAPI SDI_DetectStatus(LPVOID controller)
{
SetThreadPriority(GetCurrentThread, 100);
if (controller == NULL)
return 0;
while (g_CardStatus != STATE_DEINITING)
{
if (g_CardStatus == STATE_OPENED)
{
if (!SDI_IsCardIn((SD_controller*)controller, NULL))
{
SDI_DetectRemove();
WaitForSingleObject(IdleEvent, 3000); // wait for card uninstall from FAT
}
}
else if (g_CardStatus == STATE_CLOSED || g_CardStatus == STATE_INITING)
{
if (SDI_IsCardIn((SD_controller*)controller, NULL))
{
SDI_DetectInsert();
if (WaitForSingleObject(IdleEvent, 3000) == WAIT_TIMEOUT) // wait for card install in FAT
{
if ((g_ExcCnt++) > 5)
g_CardStatus = STATE_DEAD;
}
}
}
else if (g_CardStatus == STATE_DEAD)
{
RETAILMSG(1, (_T("SD: Card is DEAD\r\n")));
break;
}
Sleep(2000);
}
g_CardStatus = STATE_DEAD;
return 1;
}
//-----------------------------------------------------------------------------
//
// Check if the card is in the socket.
//
// Arguments:
// controller - handle of the instance.
// errorcode - record error.
//
//------------------------------------------------------------------------------
#define SD_PNP_GPIO_NUM 5
#define SD_PNP_GPIO_PIN 4
BOOL SDI_IsCardIn(SD_controller* controller, unsigned long* errorcode)
{
unsigned long count = 3;
unsigned long value;
unsigned long temp;
OEM_IOCTL_GPIO gpio;
if (g_CardStatus == STATE_OPENED)
{
temp = 1;
while(count--)
{
gpio.num = SD_PNP_GPIO_NUM; gpio.pin = SD_PNP_GPIO_PIN; gpio.code = GPIO_Read;
value = 0; gpio.value = &value;
KernelIoControl(HAL_IOCTL_GPIO , &gpio , sizeof(OEM_IOCTL_GPIO) , 0 , 0 , 0 );
temp &= ((value>>SD_PNP_GPIO_PIN)&0x01);
Sleep(100);
}
}
else
{
temp = 0;
while(count--)
{
gpio.num = SD_PNP_GPIO_NUM; gpio.pin = SD_PNP_GPIO_PIN; gpio.code = GPIO_Read;
value = 0; gpio.value = &value;
KernelIoControl(HAL_IOCTL_GPIO , &gpio , sizeof(OEM_IOCTL_GPIO) , 0 , 0 , 0 );
temp |= ((value>>SD_PNP_GPIO_PIN)&0x01);
Sleep(100);
}
}
if (temp == 1)
return FALSE;
else
return TRUE;
}
//is sdcard protect
#define SD_PROTECT_GPIO_NUM 5
#define SD_PROTECT_GPIO_PIN 5
BOOL SDI_IsCardProtect(void)
{
unsigned long count = 3;
unsigned long value;
unsigned long temp;
OEM_IOCTL_GPIO gpio;
temp = 1;
while(count--)
{
gpio.num = SD_PROTECT_GPIO_NUM; gpio.pin = SD_PROTECT_GPIO_PIN; gpio.code = GPIO_Read;
value = 0; gpio.value = &value;
KernelIoControl(HAL_IOCTL_GPIO , &gpio , sizeof(OEM_IOCTL_GPIO) , 0 , 0 , 0 );
temp &= ((value>>SD_PROTECT_GPIO_PIN)&0x01);
Sleep(100);
}
if (temp == 1)//为1则则返回错误
return FALSE;
else
return TRUE;//否则则错误
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -