⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 carddetection.c

📁 card 的检测读取等功能操作,主要用于多媒体,数码的各种卡的操作.
💻 C
字号:
/* **************************************************************************************
 *  Copyright (c) 2004 ZORAN Corporation, All Rights Reserved
 *  THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF ZORAN CORPORATION
 *
 *  File: $Workfile: CardDetection.c $
 *
 * Description:
 * ============
 * Provides services of card detection.
 *
 * Log:
 * ====
 * $Revision: $
 * Last Modified by $Author: $ at $Modtime: $
 ****************************************************************************************
 * Updates:
 ****************************************************************************************
 * $Log: $
 * 1. CardDetection criterion:(Suggested by Hans, Simon and Felix)
 *		a. When playing disc, check nothing;
 *          b. When playing card, only check the current working card, do not check USB;
 *		c. When playing USB, only check the working USB.
 * 2. USB and Card divided.
 *
 **************************************************************************************** */

#include "config.h"

#ifdef SUPPORT_FLASH_CARD

#ifdef D_GINGER_I86
#ifdef _DEBUG
#undef IFTRACE
#define IFTRACE if (gTraceFCU)
#endif
#endif

#ifdef D_GINGER_I96
#ifdef DEBUG_DRIVE_TRACE
#undef IFTRACE
#define IFTRACE if (gTraceDrive)
#include "Debug\DbgMain.h"
#endif
#endif

#include "MediaCards\Fcu\_I86_FCU_LL_\Fcu.h"
#include "Kernel\uITRON\rtos.h"
#include "Kernel\eventdef.h"
#include "MediaCards\Include\CardType.h"
#include "services\include\_heap.h"

#ifdef I86_USB_SUPPORT
#include "Mediacards\usb\UsbDeviceListManagement.h"
#include "Mediacards\usb\usb.h"
#include "Mediacards\usb\vusb.h"
#include "MediaCards\Media\Mass_Storage\fx_mass_storage.h"
#endif // I86_USB_SUPPORT 

#ifndef VERILOG_DRIVER
#include "playcore\exception\exception.h"
#endif // VERILOG_DRIVER

#ifdef D_CARD_DETECTION
CONST UINT CARD_TYPE_DETECT_BIT_MASK[] = 
{
	0x1000u, //SD
	0x0000u,//MMC
	0x2000u,//MS
	0x0000u,//MSPRO
#ifdef NO_CF_CARD
	0x0000u,//CF
#else
	0x0400u,//CF
#endif	
#ifdef D_GINGER_I86
#ifdef D_SUPPORT_SM_CARD	
	0x0800u,//SM
#else
	0x0000u,//SM
#endif
	0x4000u//XD
#endif//D_GINGER_I86
#ifdef D_GINGER_I96
#ifdef SUPPORT_XD_SM_DETECTION	
	0x0000u,//SM //ERM_7JAN04
#ifdef NO_XD_CARD
	0x0000u//XD
#else
	0x4000u//XD
#endif
#else
	0x0000u,//SM
	0x0000u//XD
#endif	
#endif//D_GINGER_I96
};
#endif//D_CARD_DETECTION

#ifdef D_GINGER_I86
#include "Drive\CardReader\CardReaderDriver.h"
#ifdef FILEX_ENABLE_WRITE
#include "Playcore\timing\timing.h"
#include "Playcore\FileSys\FileX\fx_api.h"
#include "Playcore\FileSys\FileX\fx_fil.h"
#endif
#include "customer\hardware_setting\io_hal_api.h"
extern BOOL gTraceFCU;
extern BOOL NPM_IsTraySpinningDownOrOpening(void);
static CARD_STATUS * gaCurrAvailCards = NULL;

#ifdef I86_USB_SUPPORT
BOOL gbIsUSBDriver = TRUE;//use to judge the code running in V8, TRUE for USB.
#ifdef D_I86_CARD_FORMAT
void Card_PostFormat(void);
#endif //D_I86_CARD_FORMAT
#endif

#endif//D_GINGER_I86

#ifdef D_GINGER_I96
#include "MediaCards\Media\Mass_Storage\HostMassStorage.h"
#ifdef D_AV_RECEIVER
#include "coreapi\coreapi.h"
#endif
#include "customer\io_hal_api.h"

static CARD_STATUS far* gaCurrAvailCards = NULL;

#ifdef I86_USB_SUPPORT
BOOL far gbIsUSBDriver = TRUE;//use to judge the code running in V8, TRUE for USB.
BOOL far gbIsCardDriver = FALSE;
USB_STATUS far gbUSBStatus = USB_STATUS_NOT_OPEN;//status = 0 for not open, 1 for opening, 2 for opend.
extern CORE_STATE gcs;
#endif
#endif//D_GINGER_I96

#define CARD_DETECTION_COUNT 2
#define CARD_DETECTION_READ_COUNT 5
#define CARD_DETECTION_MIN_VALID_COUNT 3

static void CardDetection_DetectCards(CARD_STATUS * arrayCard);

/*
Desc: Initialize CardDetection module, this includes,
      Allocate memory for gaCurrAvailCards, which requires NUM_OF_CARDS bytes.
      Do CardDetection_DetectCards to initialize gaCurrAvailCards.
In: None
Out: gaCurrAvailCards is initialized to reflect the currect available cards.
Return: FALSE, initialized more than once or low system resource.
        TRUE, succeeds.
*/
BOOL CardDetection_Init(void)
{
    int i;
	dbg_printf(("CardDetection Init.\n"));



	

	if(NULL != gaCurrAvailCards)
		{
		tr_printf(("Card detection, FATAL! already initialized.\n"));
		return FALSE;
		}

	gaCurrAvailCards = MEM_Allocate(DEFAULT_HEAP, NUM_OF_CARDS);

	if(NULL == gaCurrAvailCards)
		{
		tr_printf(("Card detection, FATAL! low system resource, malloc gaCurrAvailCards fails.\n"));
		return FALSE;
		}
	
#if defined (NORMAL_BOART)||defined(USE_SIMPLE_USB_DETECTION)//chris added
	//set up USB detection unit
#ifdef I86_USB_SUPPORT   
	UsbPreAttachInit(FALSE);
	gbIsUSBDriver = TRUE;
	UsbMem_Init(); // initalized usb internal use heap
	UsbHostOpen( USB_NUMBER_OF_DEVICES ); // it also enable the USB attach interrupt
#ifdef D_SUPPORT_USB_HUB	
	USB_InitHubDeviceList();
	USB_InitMassStorageList();
#endif//D_SUPPORT_USB_HUB
#endif//I86_USB_SUPPORT
#else
	#ifdef I86_USB_SUPPORT   
	UsbPreAttachInit(FALSE);
	gbIsUSBDriver = FALSE;
	UsbMem_Init(); // initalized usb internal use heap
	UsbHostOpen( 0 ); // it also enable the USB attach interrupt

#endif//I86_USB_SUPPORT
#endif





	for(i = 0; i < NUM_OF_CARDS; i++)
		{
		gaCurrAvailCards[i] = NOT_AVAILABLE;
		}

	return TRUE;
}

/*
Desc: Destroy CardDetection module. That is,
      free gaCurrAvailCards.
In: None
Out: gaCurrAvailCards is freed and set to NULL.
Return: FALSE, CardDetection module is already destroyed.
        TRUE, successful.
*/
BOOL CardDetection_Term(void)
{
	dbg_printf(("CardDetection Term.\n"));

	if(NULL == gaCurrAvailCards)
		{
		tr_printf(("Card detection, warning! CardDetection already destroyed.\n"));
		return FALSE;
		}

	MEM_Free(DEFAULT_HEAP, gaCurrAvailCards);

	gaCurrAvailCards = NULL;

	return TRUE;
}

/*
Desc: Check if the specified card is available.
In: CARD_TYPE type, card type, if type is NUM_OF_CARDS, return TRUE when any card is available.
Out: None.
Return: TRUE, the specified card is available.
        FALSE, the specified card is not available.
*/
BOOL CardDetection_IsCardAvailable(CARD_TYPE type)
{
	BYTE arrayCard[NUM_OF_CARDS];
#ifdef D_CARD_DETECTION
	volatile UINT16 *uiCS2 = MK_FP(0x1000,0x0000);
	volatile UINT16 uiChipSel2;
	int prev_IE;
  	int i;


	if(NULL == gaCurrAvailCards)
		{
		tr_printf(("Card detection, FATAL! CardDetection not initialized.\n"));
		return FALSE;
		}

	prev_IE=InterruptDisable();
	outport( 0xa1a, 0x3f );
	uiChipSel2 = *uiCS2; 
	set_SR(prev_IE);

#ifdef I86_USB_SUPPORT
	for(i=0; i<NUM_OF_CARDS-1; i++)
#else
	for(i=0; i<NUM_OF_CARDS; i++)
#endif
		{
			if((~(*uiCS2)) & CARD_TYPE_DETECT_BIT_MASK[i])
				{
				arrayCard[i] = AVAILABLE;
				}
		}
#endif
   
#ifdef I86_USB_SUPPORT
	if(!gbIsUSBDriver)
	{
		FcuTerminate();
		UsbInit(FALSE);
	}
	arrayCard[CARD_TYPE_USB] = (UsbGetAttachIntStatReg() == FALSE) ? NOT_AVAILABLE: AVAILABLE;
	if(!gbIsUSBDriver)
	{
		UsbTerminate();
		FcuInit();
	}
#endif


	if(NUM_OF_CARDS == type)
		{
		UINT8 cIndex;

		for(cIndex = 0; cIndex < NUM_OF_CARDS; cIndex++)
			{
			if(AVAILABLE == arrayCard[cIndex])
			return TRUE;
			}

		return FALSE;
		}
	else
		{
		if(AVAILABLE == arrayCard[type])
			return TRUE;
		else
			return FALSE;
		}
}

/*
Desc: This function checks if any card is inserted or removed and
      reports the events to core task.
      It should be called regularly(for instance, every 1 second).
In: None.
Out: gaCurrAvailCards is updated. Core task is notified.
Return: FALSE, Card detection is not initialized.
        TRUE, successful.
*/
BOOL CardDetection_RegularCheck(void)
{
	WORD i;
	CARD_STATUS aBCardAvail[NUM_OF_CARDS];

	if(NULL == gaCurrAvailCards)
		{
		return FALSE;
		}

	for(i = 0; i < NUM_OF_CARDS; i++)
		aBCardAvail[i]=gaCurrAvailCards[i];

	wai_sem(SEM_FSSUPER_ID);

	CardDetection_DetectCards(aBCardAvail);

	sig_sem(SEM_FSSUPER_ID);	

	for(i = 0; i < NUM_OF_CARDS; i++)
		{
		if(gaCurrAvailCards[i] != aBCardAvail[i])
			{
			if(AVAILABLE == aBCardAvail[i])
				{					
				ie_send_ex (IE_CORE_CARD_INSERTED, (void *)i);
				}
			else if(NOT_AVAILABLE == aBCardAvail[i] && AVAILABLE == gaCurrAvailCards[i])
				{
				ie_send_ex (IE_CORE_CARD_REMOVED, (void *)i);
				printf("remove card ===%d\n",i);
				}

			gaCurrAvailCards[i] = aBCardAvail[i];
			}
		}

	return TRUE;
}

/*
Desc: This function checks if any card is inserted or removed In: None.
Out: gaCurrAvailCards is updated. Core task is notified.
Return: FALSE, Card detection is not initialized.
        TRUE, successful.
*/

BOOL CardDetection_RegularCheckNotSendEvent(void)
{
	BYTE i;
	CARD_STATUS aBCardAvail[NUM_OF_CARDS];

	if(NULL == gaCurrAvailCards)
		{
		return FALSE;
		}
   
	CardDetection_DetectCards(aBCardAvail);

	for(i = 0; i < NUM_OF_CARDS; i++)
		{
		if(gaCurrAvailCards[i] != aBCardAvail[i])
			{
			gaCurrAvailCards[i] = aBCardAvail[i];
			}

		}

	return TRUE;
}

/*
Desc: This function checks which card is available in the slot.
In: BYTE * arrayCard, pointer to an array whose size is NUM_OF_CARDS.
Out: BYTE * arrayCard is filled with AVAILABLE or NOT_AVAILABLE for each card.
Return: None.
*/
//TODO: rewrite this function for I86.
#pragma argsused
static void CardDetection_DetectCards(CARD_STATUS * arrayCard)
{
#ifdef D_CARD_DETECTION
	BYTE i;
	BYTE j;
	unsigned int cardDetectionData[CARD_DETECTION_READ_COUNT];
#ifdef I86_USB_SUPPORT
	BYTE	cardDetectionCount[NUM_OF_CARDS-1] = {0};
#else
	BYTE	cardDetectionCount[NUM_OF_CARDS] = {0};
#endif

#ifdef D_GINGER_I86
	WORD *cardAddr;
	UINT prev_IE;

		prev_IE = InterruptDisable();
		outport(0xa18, 0x1);
		outport(0xa1a, 0x1);
		outport(0xa1e, 0x7);
		cardAddr  = MK_FP(0x1000,0);//(*((unsigned int *)0x10000000ul));   //cs2 address 0x100000

		for(i=0; i<CARD_DETECTION_READ_COUNT; i++)
		{
			cardDetectionData[i] = (*cardAddr);
		}
		set_SR(prev_IE);    
#endif

#ifdef D_GINGER_I96
#ifndef D_SD_MMC_CARD_READER
	volatile WORD *cardAddr;
	UINT prev_IE;
#endif

#ifndef D_SD_MMC_CARD_READER	
    cardAddr = MK_FP(0x1000,0x0000);//(*((unsigned int *)0x10000000ul));   //cs2 address 0x100000
#endif    
#ifdef D_SD_MMC_CARD_READER
		for(i=0; i<CARD_DETECTION_READ_COUNT; i++)
			cardDetectionData[i] = CARD_LL_SDMMC_Insert() ? (~CARD_TYPE_DETECT_BIT_MASK[CARD_TYPE_SD]):0xffff;
#else
	
      prev_IE=InterruptDisable();
      outport( 0xa1a, 0x3f );
	for(i=0; i<CARD_DETECTION_READ_COUNT; i++)
		{
			cardDetectionData[i] = (*cardAddr);
		}
      set_SR(prev_IE);
#endif
#endif
		      


#ifdef I86_USB_SUPPORT
	for(i=0; i<NUM_OF_CARDS-1; i++)
#else
	for(i=0; i<NUM_OF_CARDS; i++)
#endif
	{
		for(j=0; j<CARD_DETECTION_READ_COUNT; j++)
		{
			if((~cardDetectionData[j]) & CARD_TYPE_DETECT_BIT_MASK[i])
			{
				cardDetectionCount[i]++;
			}
		}
	}

  
#ifdef I86_USB_SUPPORT
	for(i=0; i<NUM_OF_CARDS-1; i++)
#else
	for(i=0; i<NUM_OF_CARDS; i++)
#endif
	{
		if(cardDetectionCount[i] > CARD_DETECTION_MIN_VALID_COUNT/*(~cardDetectionData[4]) & CARD_TYPE_DETECT_BIT_MASK[i]*/)
			
		{
			if(arrayCard[i] != AVAILABLE)
				arrayCard[i]++;
			
			if(arrayCard[i] >= CARD_DETECTION_COUNT && arrayCard[i] != AVAILABLE)
				arrayCard[i] = AVAILABLE;
		}
		else
			arrayCard[i] = NOT_AVAILABLE;
	}
#endif//D_CARD_DETECTION

#ifdef I86_USB_SUPPORT
	#ifdef D_USB_DETECTION	
	#if defined(NORMAL_BOART)||defined(USE_SIMPLE_USB_DETECTION)

	USB_Detection();
	#endif
	#endif
#endif

	return;
}

#ifdef D_I86_CARD_FORMAT
void Card_PostFormat(void)
{
	int i;
	UINT prev_IE;
	
	if (NULL != gaCurrAvailCards)
	{
		prev_IE = InterruptDisable();
		for(i = 0; i < NUM_OF_CARDS; i++)
		{
			gaCurrAvailCards[i] = NOT_AVAILABLE;
		}
		set_SR(prev_IE); 
	}
	return;
}
#endif //D_I86_CARD_FORMAT

#endif//SUPPORT_FLASH_CARD

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -