📄 xllp_usim.c
字号:
/******************************************************************************
**
** COPYRIGHT (C) 2001, 2002 Intel Corporation.
**
** This software as well as the software described in it is furnished under
** license and may only be used or copied in accordance with the terms of the
** license. The information in this file is furnished for informational use
** only, is subject to change without notice, and should not be construed as
** a commitment by Intel Corporation. Intel Corporation assumes no
** responsibility or liability for any errors or inaccuracies that may appear
** in this document or any software that may be provided in association with
** this document.
** Except as permitted by such license, no part of this document may be
** reproduced, stored in a retrieval system, or transmitted in any form or by
** any means without the express written consent of Intel Corporation.
**
** FILENAME: xllp_usim.c
**
** PURPOSE: contains all primitive functions for Bulverde USIM register access
** and control
**
******************************************************************************/
#include<stdio.h>
#include "xllp_usim.h"
#include "xllp_usim_ATR.h"
#include "xllp_usim_registers.h"
#include "xllp_usim_os_depend.h"
#include "xllp_ost.h"
#include "xllp_Pm.h"
static volatile P_XLLP_OST_T TimerRegBaseP = (P_XLLP_OST_T)OST_REGISTER_BASE;
/******************************************************************************
Function Name: XllpUsimActivateCard
Description: Activates the card for the first time. "Cold Reset"
Global Registers Modified: CCR, CLKR
Input Arguments:
USIM Base address: base address of USIM controller
VccUsim: value indicating VCC_USIM voltage setting
Output Arguments:
None
Return Value:
None
*******************************************************************************/
void XllpUsimActivateCard(void *XllpUsimBase, Xllp_Usim_Card_Voltage_T VccUsim)
{
XLLP_USIM_T *USIM = (XLLP_USIM_T *) XllpUsimBase;
/* Set CCR.RST_CARD_N to logic 0.*/
USIM->CCR &= ~XLLP_USIM_CCR_RST_CARD_N;
/*Turn on the VCC voltage to the lowest voltage level */
USIM->CCR &= ~(0x3<<XLLP_USIM_CCR_VCC_SHIFT) ;
USIM->CCR |= XLLP_USIM_CardVoltage1_8 << XLLP_USIM_CCR_VCC_SHIFT ;
/* Enable the I/O line to return to Vhigh by setting the CCR.TXD_FORCE bit to logic 0. */
USIM->CCR &= ~XLLP_USIM_CCR_TXD_FORCE;
// Set CLKR.STOP_CLK_USIM to 0 in Clock Register (CLKR)
USIM->CCR &= ~XLLP_USIM_CLKR_STOP_CLK_USIM;
// if (VccUsim == XLLP_USIM_CardVoltage1_8)
/*Turn on the VCC voltage to the lowest voltage level */
// USIM->CCR = (XLLP_USIM_CardVoltage1_8 << XLLP_USIM_CCR_VCC_SHIFT) | ( USIM->CCR & ~XLLP_USIM_CCR_VCC);
// else
/*Turn on the VCC voltage to the higher voltage level */
// USIM->CCR = (XLLP_USIM_CardVoltage3_0 << XLLP_USIM_CCR_VCC_SHIFT) | ( USIM->CCR & ~XLLP_USIM_CCR_VCC);
/* Activate the card clock by setting the stop bit, CLKR.STOP_UCLK, to 0 in the Clock Register */
USIM->CLKR &= ~XLLP_USIM_CLKR_STOP_UCLK;
/* Activate the card clock by setting the stop bit, CLKR.STOP_UCLK, to 0 in the Clock Register */
USIM->CLKR &= ~(0x1<<15);
/**
* Verify that CCR.RST_CARD_N was asserted for at least 400 UCLK cycles. Note that when
* UCLK is set to the lowest frequency allowed by the standard (1MHz), 400 cycles take
* 0.4ms
**/
/**
* IMPORTANT:
* INSERT A DELAY OF ABOUT 400msec HERE ... LIKE THE FOLLOWING:
*
*/
XllpOstDelayMilliSeconds(TimerRegBaseP, 1); // Waiting 1000 Uclks > 400 Uclks
/**
* Deassert CCR.RST_CARD_N by writing a logic 1 to the CCR.RST_CARD_N bit.
**/
USIM->CCR |= XLLP_USIM_CCR_RST_CARD_N ;
/**
* The card
* will Answer To Reset (ATR) within 400-40000 card clock cycles. See tc below in
* Figure 28-12.
**/
XllpOstDelayMilliSeconds(TimerRegBaseP, 10); // Waiting 40000 Uclks for ATR
}
/******************************************************************************
Function Name: XllpUsimWaitUclkCycles -- NOT USED .. DON'T USE!
Description: Waits a number of specified UCLK Cycles
Global Registers: OSCR0
Input Arguments:
Number of Cyles to wait
Output Arguments:
None
Return Value:
None
*******************************************************************************/
//void XllpUsimWaitUclkCycles(XLLP_INT32_T Cycles)
//{
///* OSCR0 time increments on a 3.25MHz clock */
///* To achieve 0.4Ms, the counter has to increment (0.4*10^-3)*(3.25*10^6) times */
//
// P_XLLP_VUINT32_T oscr = (P_XLLP_VUINT32_T) OSCR0;
// XLLP_VUINT32_T Old, New, Target, i;
//
// Old = *oscr;
// Target = Old + (XLLP_UINT32_T) (Cycles * 3250 / 1000);
//
// if(Target > Old) /* check to make sure there was no overflow in the addition! */
// {
// New = *oscr;
// while(New < Target)
// {
// for(i=0; i<500; i++); /* Read the OStimer less often! */
//
// New = *oscr;
// }
// }
// else
// {
// New = *oscr;
// while(New & 0x80000000) /* wait until the OSCR loops back */
// {
// for(i=0; i<500; i++); /* Read the OStimer less often! */
//
// New = *oscr;
// }
//
// /* Now loop until you hit the Target */
// while (New < Target)
// {
// for(i=0; i<500; i++); /* Read the OStimer less often! */
// New = *oscr;
// }
// }
//
//}
/******************************************************************************
Function Name: XllpUsimResetCard
Description: Resets the USIM card: "Warm Reset"
Global Registers: CCR
Input Arguments:
USIM Base address
Output Arguments:
None
Return Value:
None
*******************************************************************************/
void XllpUsimResetCard(void *XllpUsimBase)
{
XLLP_USIM_T *USIM = (XLLP_USIM_T *) XllpUsimBase;
/* Set CCR.RST_CARD_N to logic 0.*/
USIM->CCR &= ~XLLP_USIM_CCR_RST_CARD_N;
/**
* Verify that CCR.RST_CARD_N was asserted for at least 400 UCLK cycles. Note that when
* UCLK is set to the lowest frequency allowed by the standard, 400 cycles take
* 0.4ms
**/
// NEED A DELAY OF ABOUT 400msec HERE
XllpOstDelayMilliSeconds(TimerRegBaseP, 1); // Waiting 1000 Uclks > 400 Uclks
// Deassert CCR.RST_CARD_N by writing a logic 1 to the CCR.RST_CARD_N bit.
USIM->CCR |= XLLP_USIM_CCR_RST_CARD_N;
// The card will Answer To Reset (ATR) within 400-40000 card clock cycles.
}
/******************************************************************************
Function Name: XllpUsimDeactivateCard
Description: Decattivates the card and renders it unfunctional.
Global Registers: CCR, CLKR
Input Arguments:
USIM Base address
Output Arguments:
None
Return Value:
None
*******************************************************************************/
void XllpUsimDeactivateCard(void *XllpUsimBase)
{
XLLP_USIM_T *USIM = (XLLP_USIM_T *) XllpUsimBase;
/* Set CCR.RST_CARD_N to logic 0.*/
USIM->CCR &= ~XLLP_USIM_CCR_RST_CARD_N;
/**
* Stop the clock on Vlow by setting the CLKR.STOP_LEVEL bit to 0, and set the
* CLKR.STOP_UCLK bit to 1.
**/
USIM->CLKR &= ~XLLP_USIM_CLKR_STOP_LEVEL;
USIM->CLKR |= XLLP_USIM_CLKR_STOP_UCLK;
/* Force the I/O line to ground level by setting a logic 1 to the CCR.TXD_FORCE bit.*/
USIM->CCR |= XLLP_USIM_CCR_TXD_FORCE;
/* Turn the VCC voltage to ground level by setting the CCR.VCC bits to 00. */
USIM->CCR |= XLLP_USIM_CardVoltage0_0 << XLLP_USIM_CCR_VCC_SHIFT;
}
/******************************************************************************
Function Name: XllpUsimSetBaudRate
Description: Sets the baud rate for the card
Global Registers: CCR
Input Arguments:
USIM Base address, Factor, and Divisor which can be determined from the TA(1) byte
received after the Answer to Reset (ATR).
Output Arguments:
None
Return Value:
None
*******************************************************************************/
void XllpUsimSetBaudRate(void *XllpUsimBase,XLLP_INT32_T ClockDivisor , XLLP_INT32_T Factor, XLLP_INT32_T Divisor)
{
XLLP_USIM_T *USIM = (XLLP_USIM_T *) XllpUsimBase;
USIM->CLKR = (USIM->CLKR & ~0xFF) | ClockDivisor;
USIM->FLR = (USIM->CLKR & ~0xFF) | Factor ;
USIM->DLR = (USIM->CLKR & ~0xFFFF) | Divisor;
}
/******************************************************************************
Function Name: XllpUsimReceiveCharacter
Description: Reads the character received including the parity bit status
Global Registers: RBR
Input Arguments:
XllpUsimBase: Base address of the USIM module
Output Arguments:
ReceivedChar: A pointer that points to the to-be received character
Return Value:
XLLP_STATUS_T: OK if successful; CHAR_NOT_RECEIVED_YET if there was an error
*******************************************************************************/
XLLP_USIM_STATUS_T XllpUsimReceiveCharacter(void *XllpUsimBase, XLLP_INT32_T *ReceivedChar)
{
XLLP_USIM_T *USIM = (XLLP_USIM_T *) XllpUsimBase;
*ReceivedChar = USIM->RBR & XLLP_USIM_RBR_VALID;
if( (USIM->RBR & XLLP_USIM_RBR_PERR)>> 8 == 1 )
return XLLP_USIM_PARITY_ERROR;
else
return XLLP_USIM_OK;
}
/******************************************************************************
Function Name: XllpUsimReceiveCharacterWait
Description: This function reads a character from the receive fifo (if availbale). If not
it will wait for a user-specified period of time before trying again. If any of
two attempts succeeds, the function returns 'OK'; otherwise it returns an error.
Global Registers: RBR
Input Arguments:
XllpUsimBase: Base address of the USIM module
TimeInMilliSeconds: Time in milliseconds to wait before attempting a second time
Output Arguments:
ReceivedChar: A pointer that points to the to-be received character
Return Value:
XLLP_STATUS_T: OK if successful; CHAR_NOT_RECEIVED_YET if there was an error
*******************************************************************************/
XLLP_USIM_STATUS_T XllpUsimReceiveCharacterWait(void *XllpUsimBase,
XLLP_INT8_T *ReceivedChar, XLLP_INT32_T TimeInMilliSeconds)
{
XLLP_USIM_T *USIM = (XLLP_USIM_T *) XllpUsimBase;
XLLP_INT32_T ReceiveFifoBytes; // Number of characters in the receive fifo
ReceiveFifoBytes = (USIM->FSR & XLLP_USIM_FSR_RX_LENGTH) >> XLLP_USIM_FSR_RX_LENGTH_SHIFT;
if(ReceiveFifoBytes) // If there are bytes in the receive fifo ...
{
*ReceivedChar = (XLLP_INT8_T) USIM->RBR & XLLP_USIM_RBR_VALID;
return XLLP_USIM_OK;
}
else // wait for 'TimeInMilliSeconds' milli seconds before trying again ...
{
XllpOstDelayMilliSeconds(TimerRegBaseP, TimeInMilliSeconds);
ReceiveFifoBytes = (USIM->FSR & XLLP_USIM_FSR_RX_LENGTH) >> XLLP_USIM_FSR_RX_LENGTH_SHIFT;
if(ReceiveFifoBytes)
{
*ReceivedChar = (XLLP_INT8_T) USIM->RBR & XLLP_USIM_RBR_VALID;
return XLLP_USIM_OK;
}
else
return XLLP_USIM_CHAR_NOT_RECEIVED_YET;
}
}
/******************************************************************************
Function Name: XllpUsimSendCharacter
Description: Sends a character.
Global Registers: THR
Input Arguments:
XllpUsimBase: Base address of the USIM module
CharToBeSent: Character to be sent (8 bits)
Output Arguments:
None
Return Value:
None
*******************************************************************************/
void XllpUsimSendCharacter(void *XllpUsimBase, XLLP_UINT8_T CharToBeSent)
{
XLLP_USIM_T *USIM = (XLLP_USIM_T *) XllpUsimBase;
// wait until transmitter data fifo is less than FCR[TX_TL]
while (!(USIM->LSR & 0x800));
// send character to transmitter fifo
USIM->THR = CharToBeSent & XLLP_USIM_THR_TB;
}
/******************************************************************************
Function Name: XllpUsimSetCardClock
Description: sets the USIM card clock based on the equation
clk_card = 48Mhz/ (2 * ClockDivisor)
Global Registers: CLKR
Input Arguments:
XllpUsimBase: Base address of the USIM module
ClockDivisor: Can't be zero
Output Arguments:
None
Return Value:
None
*******************************************************************************/
void XllpUsimSetCardClock(void *XllpUsimBase, XLLP_UINT8_T ClockDivisor)
{
XLLP_USIM_T *USIM = (XLLP_USIM_T *) XllpUsimBase;
if(ClockDivisor != 0)
USIM->CLKR |= ClockDivisor & XLLP_USIM_CLKR_DIVISOR;
// else do not change the current clock rate!
}
/******************************************************************************
Function Name: XllpUsimModifyInterruptMask
Description: Modifies interrupt mask.
Global Registers: IER
Input Arguments:
XllpUsimBase: Base address of the USIM module
EnableMask: 'Ones' indicate Interrupts you want to enable.
DisableMask: 'Ones' indicate Interrupts you want to disable.
Output Arguments:
None
Return Value:
None
*******************************************************************************/
void XllpUsimModifyInterruptMask(void *XllpUsimBase,
XLLP_UINT32_T EnableMask,
XLLP_UINT32_T DisableMask)
{
XLLP_USIM_T *USIM = (XLLP_USIM_T *) XllpUsimBase;
USIM->IER = ( (USIM->IER & ~DisableMask) | EnableMask);
}
/******************************************************************************
Function Name: XllpUsimGetInterruptSources
Description: Obtains the current interrupts that need to be serviced
Global Registers: IIR
Input Arguments:
XllpUsimBase: Base address of the USIM module
Output Arguments:
InterruptSources: Current interrupts that need to be serviced
Return Value:
None
*******************************************************************************/
void XllpUsimGetInterruptSources(void *XllpUsimBase, XLLP_UINT32_T InterruptSources)
{
XLLP_USIM_T *USIM = (XLLP_USIM_T *) XllpUsimBase;
InterruptSources = USIM->IIR & XLLP_USIM_IIR_VALID_MASK;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -