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

📄 xllp_usim.c

📁 PXA270硬件测试源代码
💻 C
📖 第 1 页 / 共 3 页
字号:

  Global Registers: EGTR

  Input Arguments:
  XllpUsimBase: Base address of the USIM module
  ExtraTime: The extra guard time needed in moments

  Output Arguments:
	None
  
  Return Value:
	None

*******************************************************************************/
void XllpUsimSetExtraGuardTime(void *XllpUsimBase, XLLP_UINT8_T ExtraTime)
{
	XLLP_USIM_T *USIM = (XLLP_USIM_T *) XllpUsimBase;
	USIM->EGTR = ExtraTime & XLLP_USIM_EGTR_EGTM;
}


/******************************************************************************

  Function Name: XllpUsimSetBlockGuardTime

  Description: sets the block guard time

  Global Registers: BGTR

  Input Arguments:
  XllpUsimBase: Base address of the USIM module
  ExtraTime: Extra time needed

  Output Arguments:
	None
  
  Return Value:
	None

*******************************************************************************/
void XllpUsimSetBlockGuardTime(void *XllpUsimBase, XLLP_UINT8_T ExtraTime)
{
	XLLP_USIM_T *USIM = (XLLP_USIM_T *) XllpUsimBase;
	USIM->BGTR = ExtraTime & XLLP_USIM_BGTR_BGT;
}


/******************************************************************************

  Function Name: XllpUsimSetTimeOutMoments

  Description: Specifies the number of moments that have to elapse before a time-out 
			   interrupt is generated.

  Global Registers: TOR

  Input Arguments:
  XllpUsimBase: Base address of the USIM module
  Moments: Number of moments to wait

  Output Arguments:
	None
  
  Return Value:
	None

*******************************************************************************/
void XllpUsimSetTimeOutMoments(void *XllpUsimBase, XLLP_UINT8_T Moments)
{
	XLLP_USIM_T *USIM = (XLLP_USIM_T *) XllpUsimBase;
	USIM->TOR= Moments & XLLP_USIM_TOR_TO;
}


/******************************************************************************

  Function Name: XllpUsimGetCharacterWaitingTime

  Description: Read the Character Waiting Time

  Global Registers: CWTR

  Input Arguments:
  XllpUsimBase: Base address of the USIM module

  Output Arguments:
	CharacterWaitingTime: Character Waiting Time
  
  Return Value:
	None

*******************************************************************************/
void XllpUsimGetCharacterWaitingTime(void *XllpUsimBase, XLLP_UINT32_T CharacterWaitingTime)
{
	XLLP_USIM_T *USIM = (XLLP_USIM_T *) XllpUsimBase;
	CharacterWaitingTime = USIM->CWTR & XLLP_USIM_CWTR_CWT;
}


/******************************************************************************

  Function Name: XllpUsimGetBlockWaitingTime

  Description: Read the Block Waiting Time

  Global Registers: BWTR

  Input Arguments:
  XllpUsimBase: Base address of the USIM module

  Output Arguments:
	BlockWaitingTime: Waiting Block Time.
  
  Return Value:
	None

*******************************************************************************/
void XllpUsimGetBlockWaitingTime(void *XllpUsimBase, XLLP_UINT32_T BlockWaitingTime)
{
	XLLP_USIM_T *USIM = (XLLP_USIM_T *) XllpUsimBase;
	BlockWaitingTime = USIM->BWTR & XLLP_USIM_BWTR_BWT;
}


/******************************************************************************

  Function Name: XllpUsimInitializeInterface

  Description: Initializes the USIM interface and the USIM card. Before Initializing the 
			   USIM interface and the USIM card, you should intialize the ATR structure in
			   which you will be storing the card's configuration information.

  Global Registers: 

  Input Arguments:
  XllpUsimBase: Base address of the USIM module
  XllpGpioBase: Base address of the GPIO module
  VccUsim:      Value indicating VCC_USIM voltage setting


  Output Arguments:
	None

  Return Value:
	None

*******************************************************************************/ 
void XllpUsimInitializeInterface(void *XllpUsimBase, void *XllpGpioBase,
									  void *XllpPwrMgrBase, Xllp_Usim_Card_Voltage_T VccUsim)
{
	// Initialization constants ... 
	//XLLP_UINT8_T ActivationFail = 0;
	XLLP_UINT8_T ClockDivisor = 6;									// Initial fs:  1Mhz < fs < 5Mhz 
	XLLP_UINT32_T InitialFactor = 11;								// Initially, Divisor*(Factor+1) = 372/1	
	XLLP_UINT32_T InitialDivisor = 372;								// Initially, Divisor*(Factor+1) = 372/1
	XLLP_UINT32_T InterruptEnableMask = 0;							// Not enabling interrupts initially
	XLLP_UINT32_T InterruptDisableMask = XLLP_USIM_IER_VALID_MASK;	// Disabling all interrupts initially

	XLLP_USIM_T *USIM = (XLLP_USIM_T *) XllpUsimBase;
	XLLP_CLKMGR_T *Clkmgr_ptr = (XLLP_CLKMGR_T *) 0x41300000;						// Pointer to the Clkmgr module on Bulverde


    Clkmgr_ptr -> cken |= XLLP_CLKEN_USIM;              // Enable clock to the USIM controller
	XllpUsimGpioConfiguration(XllpGpioBase);            // Configure all USIM signals for correct alternate function
	
	XllpUsimHoldTransmission(USIM);						// Ensure there is no transmission
	XllpUsimDeactivateCard(USIM);						// In case card was activated!

	XllpUsimModifyInterruptMask(USIM, InterruptEnableMask, InterruptDisableMask);	// Disabling all interrupts ...
	XllpUsimClearFIFO(USIM, XLLP_USIM_Clear_Fifos);				// Clear transmit and receive fifos
	XllpUsimClearT0Error(USIM);
	XllpUsimModifyParityErrorMaskInFIFO(USIM, Disable);			// Disable parity reporting .. parity will not appear on the bus
	
	XllpUsimSetTransportProtocol(USIM, XLLP_USIM_ReceiverT0_TransmitterT0);
	XllpUsimSelectParityType(USIM, XLLP_USIM_Even_Parity);
	XllpUsimSetConvention(USIM, XLLP_USIM_DirectConvention);	// First byte of ATR response will eventually determine convention!

	// No extra guard time is used to send characters from the card to the interface device.
	// No block guard time is used in T=0 protocol
	// not setting time out moments 

	XllpUsimSetCardClock(USIM, ClockDivisor);					// fs = 48Mhz / (2 * 6) = 4 Mhz
	// Ensure that 48Mhz / (2 * ClockDivisor) < Xllp_Usim_MaximumCardSamplingFrequency[Fi]
	XllpUsimSetBaudRate(USIM, ClockDivisor, InitialFactor, InitialDivisor);	// Initial Baud rate = 9600 Hz
	XllpUsimActivateCard(USIM, VccUsim);


  // Added to enable USIM.  New requirement per specification.
  // This is different from A1 Bulverde silicon
  // Must set bits in Power Management Register PUCR to enable USIM
	//set 
	((XLLP_PWRMGR_T *)XllpPwrMgrBase)->PUCR |= 0x9 ;
	((XLLP_PWRMGR_T *)XllpPwrMgrBase)->PUCR |= 0x20 ;

	XllpUsimContinueTransmission(USIM);

}

/******************************************************************************

  Function Name: XllpUsimInitPtsStructure 

  Description: Used to create the PSS structure based off information provided by the 
  	ATR(TA[1]) 

  Global Registers: N/A

  Input Arguments:
    XllpUsimBase:  		Base for the USIM registers
	PtsT:  				A pointer to the PSS structure data
	XLLP_USIM_ATR_T : 	A pointer to the ATR structure data

  Output Arguments:
	None
  
  Return Value:
    None
	

*******************************************************************************/
void XllpUsimInitPtsStructure(XLLP_USIM_T* USIM, PtsT* Pts_ptr, XLLP_USIM_ATR_T* Atr_ptr)
{

	Pts_ptr->OptionalCharactersLength = 0;

	// Intial Character:  always 0xFF
	Pts_ptr->PTSS = 0xFF;							
	// PPS1 present and T=0 protocol
	Pts_ptr->PTSO = 0x10;            				
	// Set to baud rate recommended by USIM
	Pts_ptr->PTS[1] = Atr_ptr->TA[1];         		
	//Determine the EOR value for PCK
	Pts_ptr->PCK = Pts_ptr->PTSS ^ Pts_ptr->PTSO ^ Pts_ptr->PTS[1];  
	// Calculate Parameter Character length
	// Test for PPS1 present
	if (Pts_ptr->PTSO & 0x10) 
		Pts_ptr->OptionalCharactersLength += 1;
	// Test for PPS2 present
	if (Pts_ptr->PTSO & 0x20) 
		Pts_ptr->OptionalCharactersLength += 1;
	// Test for PPS3 present
	if (Pts_ptr->PTSO & 0x40) 
		Pts_ptr->OptionalCharactersLength += 1;

}

/******************************************************************************

  Function Name: XllpUsimSendaPtsCommand

  Description: Send a Protocol Type Selection (PTS) command to change the operating 
			   protocol and to change the baud rate by sending the needed Fi and Di 
			   as coded for TA[1]. The card responds by echoing the same PTS command. 
			   If bit 5 of PTS1 is zero, then the default values of F and D will be used.

  Global Registers: N/A

  Input Arguments:
  XllpUsimBase: Base address of the USIM module
  PTS: A pointer to a PTS structure

  Output Arguments:
	None
  
  Return Value:
	

*******************************************************************************/
void XllpUsimSendaPtsCommand(void *XllpUsimBase, PtsT* PTS)
{
	XLLP_UINT32_T i;
	XLLP_UINT32_T OptCharsLength = PTS->OptionalCharactersLength;
	XLLP_USIM_T *USIM = (XLLP_USIM_T *) XllpUsimBase;

	XllpUsimSendCharacter(USIM, PTS->PTSS);
	XllpUsimSendCharacter(USIM, PTS->PTSO);
	for(i = 1; i <= OptCharsLength; i++)
		XllpUsimSendCharacter(USIM, PTS->PTS[i]);
	XllpUsimSendCharacter(USIM, PTS->PCK);
}

/******************************************************************************

  Function Name: XllpUsimComparePtsCommand

  Description: Compares Protocol Type Selection (PTS) command to the Pts structure.
			   A successfulr PTS command will echo the command back. 

  Global Registers: N/A

  Input Arguments:
  XllpUsimBase: Base address of the USIM module
  PTS: A pointer to a PTS structure

  Output Arguments:
	None
  
  Return Value:
	

*******************************************************************************/
XLLP_USIM_STATUS_T XllpUsimComparePtsCommand(void *XllpUsimBase, PtsT* PTS)
{
	XLLP_UINT32_T i;
	XLLP_UINT32_T OptCharsLength = PTS->OptionalCharactersLength;
	XLLP_USIM_T *USIM = (XLLP_USIM_T *) XllpUsimBase;


	if((XllpUsimReceiveCharacterWait(USIM, &(PTS->PTSS), 10) & XLLP_USIM_RBR_RB)
																			!= XLLP_USIM_OK)
		return XLLP_USIM_ATR_FAILURE;

	if((XllpUsimReceiveCharacterWait(USIM, &(PTS->PTSO), 10) & XLLP_USIM_RBR_RB)
																			!= XLLP_USIM_OK)
		return XLLP_USIM_ATR_FAILURE;

	for(i = 1; i <= OptCharsLength; i++)
		if((XllpUsimReceiveCharacterWait(USIM, &(PTS->PTS[i]), 10) & XLLP_USIM_RBR_RB)
																			!= XLLP_USIM_OK)
			return XLLP_USIM_ATR_FAILURE;

	if((XllpUsimReceiveCharacterWait(USIM, &(PTS->PCK), 10) & XLLP_USIM_RBR_RB)
																			!= XLLP_USIM_OK)
		return XLLP_USIM_ATR_FAILURE;

	return XLLP_USIM_OK;

}

/******************************************************************************

  Function Name: XllpUsimGpioConfiguration 

  Description: Update the GPIO direction register and alternate function register
               to support USIM signals which also support alternate functions.

  Global Registers: N/A

  Input Arguments:
    pGPIO: A pointer to the GPIO structure

  Output Arguments:
	None
  
  Return Value:
    None
	

*******************************************************************************/
void XllpUsimGpioConfiguration(P_XLLP_GPIO_T pGPIO)
{

	XLLP_UINT32_T *PUCR_ptr;
	PUCR_ptr = (XLLP_UINT32_T *) 0x40F0004C;    // Pointer to Power Manager USIM Card Control/Status Register

	// Configure nVUS2->GPIO116(U_DET) as an input
	// Configure nUVS1->GPIO115(U_EN) as an output
	// Configure nUVS0->GPIO114 as an output
	pGPIO -> GPDR3 |= XLLP_GPIO_BIT_U_EN ;
	pGPIO -> GPDR3 &= ~XLLP_GPIO_BIT_U_DET;

/*******************************************************/
	// Configure U_DET and U_EN alternate function
	// Don't want to do this now as these GPIO pins will be toed to the USIM by
	// the PUCR in the Power Manager
/*******************************************************/


	// Configure GPIO114 to out and alternate function 2
	pGPIO -> GPDR3 |= (0x1<<18);

	pGPIO -> GAFR3_U &= ~(0x3<<4) ;
	pGPIO -> GAFR3_U |= 0x2<<4 ;

	//pGPIO -> GAFR3_U  = (pGPIO -> GAFR3_U & ~XLLP_GPIO_AF_BIT_U_EN_MASK & ~XLLP_GPIO_AF_BIT_U_DET_MASK)
	  //                |XLLP_GPIO_AF_BIT_U_EN | XLLP_GPIO_AF_BIT_U_DET;
 	// Set U_EN as an output and U_DET as an input
	//pGPIO -> GPDR3    = (pGPIO -> GPDR3 | XLLP_GPIO_BIT_U_EN) & ~XLLP_GPIO_BIT_U_DET;                           

	// Configure URST and UCLK alternate function
	pGPIO -> GAFR2_U  = (pGPIO -> GAFR2_U & ~XLLP_GPIO_AF_BIT_UCLK_MASK & ~XLLP_GPIO_AF_BIT_URST_MASK)
	                  | XLLP_GPIO_AF_BIT_UCLK | XLLP_GPIO_AF_BIT_URST ;
    // Set URST and UCLK as outputs
	pGPIO -> GPDR2    |= XLLP_GPIO_BIT_URST | XLLP_GPIO_BIT_UCLK;      

	// Enable USIM card Detect after alternate signals have been configured
	*PUCR_ptr |= 0x1;
	// Configure U_EN to connect to GPIO115 and USIM card detect
	*PUCR_ptr |= 0x9;
}


/*****************************
Info about the USIM protocol:*
******************************
URLs of interest:
http://www.maxking.com/iso7816-1.htm
http://lkv.customs.ru/sat/sat.xpress.ru/SmartCard/ISO7816-3.htm 
__________________________________________________________________
************************
* Initial Bit Duration:
************************
The nominal bit duration used on I/O is defined as one Elementary Time
Unit (etu). 

For cards having internal clock, the initial etu is 1/9600 s.

For cards using the external clock, there is a linear relationship
between the Elementary Time Unit used on I/O  and the period provided
by the interface device on CLK.
 
The initial etu is 372/fi s  where fi is in Hertz. The initial frequency 
fi is provided by the interface device on CLK
during the Answer to Reset. In order to read the initial character (TS), 
all cards shall initially be operated with fi in the range of 1 MHz to 5 MHz.

_________________________________________________________________

*********************************************
Decoding of the TA1 byte of the ATR response:
*********************************************

Fi,Di	 F		 D	
____	____	____
0000	RFU*    RFU*
0001	372		1
0010	558		2
0011 	744		4
0100	1116	8
0101	1488	16
0110	1860	RFU*
0111	RFU*	RFU*
1000	RFU*	RFU*
1001	512		RFU*
1010	768		1/2
1011	1024	1/4 
1100	1536	1/8
1101	2048	1/16
1110	RFU*	1/32
1111	RFU*	1/64

*RFU : Reserved for Future Use
Fi, Di are the bits that are received in the TA(1) byte after ATR
See \\an1sfls001\hcd_proj\Doc\Bulverde\USIM\iso7816.html


____________________________________________________________________

*********************************************
Decoding of the TBi byte of the ATR response:
*********************************************
**I values:
II	I
00	25
01	50
10	100
11	RFU

**P values:

Pi
0		Powered internally
1-4		RFU
5-25	Voltage. 5 indicates 5V, 25 indicates 25V
26-31	RFU
_____________________________________________________________________

********************************
Command Header in T=0 protocol:
********************************
CLA - the instruction class (FF is reserved for PTS) 
INS - the instruction code (e.g read memory) 
P1 - instruction code qualifier (e.g memory address) 
P2 - additional INS code qualifier 
P3 - the length of the data block 
______________________________________________________________________


*/

⌨️ 快捷键说明

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