最近在学习STM32,用UART读写IC卡时,复位后能接收到ATR,但接收完后,UART3的LBD被置位,发送命令到IC卡一点反应也没有,我原来在430上直接用I/0口连接IC卡的管脚做的,现在用UART的smart_card模式,通过一个ST8024连接到IC卡座,请问哪位大侠知道是什么原因,指教一下,小弟感激不尽。代码如下:
void PsamInit()
{
int n;
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStructure;
// Enable GPIOB, GPIOD and GPIOE clocks
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOD |
RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE);
// Enable USART3 clock
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
// Configure USART3 CK(PB.12) as alternate function push-pull
GPIO_InitStructure.GPIO_Pin = ICC_CLK_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(ICC_CLK_GPIO, &GPIO_InitStructure);
// Configure USART3 Tx (PB.10) as alternate function open-drain
GPIO_InitStructure.GPIO_Pin = ICC_IO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(ICC_IO_GPIO, &GPIO_InitStructure);
// Configure Smartcard Reset
GPIO_InitStructure.GPIO_Pin = ICC_RESET_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(ICC_RESET_GPIO, &GPIO_InitStructure);
// Configure Smartcard VCC
GPIO_InitStructure.GPIO_Pin = ICC_POWER_PIN;
GPIO_Init(ICC_POWER_GPIO, &GPIO_InitStructure);
// Configure VCC Selector
GPIO_InitStructure.GPIO_Pin = ICC_POWER_SELECT_PIN;
GPIO_Init(ICC_POWER_SELECT_GPIO, &GPIO_InitStructure);
// USART Clock set to 3.6 MHz (PCLK1 (36 MHZ) / 10)
USART_SetPrescaler(USART3, 0x05);
// USART Guard Time set to 16 Bit */
USART_SetGuardTime(USART3, 16);
USART_ClockInitStructure.USART_Clock = USART_Clock_Enable;
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
USART_ClockInitStructure.USART_CPHA = USART_CPHA_1Edge;
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Enable;
USART_ClockInit(USART3, &USART_ClockInitStructure);
USART_InitStructure.USART_BaudRate = 9677;
USART_InitStructure.USART_WordLength = USART_WordLength_9b;
USART_InitStructure.USART_StopBits = USART_StopBits_1_5;
USART_InitStructure.USART_Parity = USART_Parity_Even;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART3, &USART_InitStructure);
// Enable the USART3 Parity Error Interrupt
//USART_ITConfig(USART3, USART_IT_PE, ENABLE);
// Enable the USART3 Framing Error Interrupt
//USART_ITConfig(USART3, USART_IT_ERR, ENABLE);
// Enable USART3
USART_Cmd(USART3, ENABLE);
// Enable the NACK Transmission */
//USART_SmartCardNACKCmd(USART3, ENABLE);
// Enable the Smartcard Interface */
USART_SmartCardCmd(USART3, ENABLE);
GPIO_WriteBit(ICC_RESET_GPIO, ICC_RESET_PIN, Bit_RESET);
GPIO_WriteBit(ICC_POWER_SELECT_GPIO, ICC_POWER_SELECT_PIN, Bit_SET);
// 关闭ST8024供电(低有效)
GPIO_WriteBit(ICC_POWER_GPIO, ICC_POWER_PIN, Bit_SET);
}
bool PsamPowerOn(uint8 slot)
{
uint32 n;
uint8 j, bAllZero = 1, bAllOne = 1;
uint8 buf[MAX_RESET_CHAR_COUNT] = { 0 };
// 提供IC卡电压
GPIO_WriteBit(ICC_POWER_GPIO, ICC_POWER_PIN, Bit_RESET);
// 延时一段时间等待ST8024工作稳定
for(n = 0; n < ICC_RECV_BYTE_TIME_OUT * 200; n++);
GPIO_WriteBit(ICC_RESET_GPIO, ICC_RESET_PIN, Bit_SET);
n = 0;
do
{
if(PsamRecvByte(buf + n, ICC_RECV_BYTE_TIME_OUT) == ICC_DATA_ERROR)
{
break;
}
n++;
} while(n < MAX_RESET_CHAR_COUNT);
/////////////////////////////////****************//////
if(n == 0)
{
return FALSE;
}
return TRUE;
}
我先执行PsamInit(),然后执行PsamPowerOn(),到///////****一行后,正确接收到了ATR,但此时LBD置位,后面命令没法执行了。 |