我用的是stm32f101系列的芯片,串口1的智能卡模式,和IC卡通信的时候只能收到ATR的第一个字节,求救大神
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(SC_PIN_3_5V_GPIO_CLK,ENABLE);
RCC_APB2PeriphClockCmd(SC_PIN_RESET_GPIO_CLK,ENABLE);
RCC_APB2PeriphClockCmd(SC_PIN_CMDVCC_GPIO_CLK,ENABLE);
RCC_APB2PeriphClockCmd(SC_USART_GPIO_CLK,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
/* Enable USART clock */
RCC_APB2PeriphClockCmd(SC_USART_CLK, ENABLE);
/*配置IC卡模块GPIO,ISO7816用串口1*/
//PA.9=TX,ISO7816 I/O通信引脚
GPIO_InitStructure.GPIO_Pin = SC_USART_PIN_TX;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; //复用开漏输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(SC_USART_GPIO, &GPIO_InitStructure);
//PA.8=CK,ISO7816时钟
GPIO_InitStructure.GPIO_Pin = SC_USART_PIN_CK;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(SC_USART_GPIO, &GPIO_InitStructure);
//PB.0=RST,IC卡复位引脚
GPIO_InitStructure.GPIO_Pin = SC_PIN_RESET;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_Init(SC_PIN_RESET_GPIO, &GPIO_InitStructure);
//PB.1=CMDVCC,IC卡电源引脚
GPIO_InitStructure.GPIO_Pin = SC_PIN_CMDVCC;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_Init(SC_PIN_CMDVCC_GPIO, &GPIO_InitStructure);
//PA.7=5V_3V,5v/3v选择引脚
GPIO_InitStructure.GPIO_Pin = SC_PIN_3_5V;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_Init(SC_PIN_3_5V_GPIO, &GPIO_InitStructure);
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStructure;
RCC_ClocksTypeDef RCC_ClocksStatus;
/* Enable GPIO clocks */
RCC_APB2PeriphClockCmd(SC_PIN_3_5V_GPIO_CLK , ENABLE);
RCC_APB2PeriphClockCmd(SC_PIN_RESET_GPIO_CLK, ENABLE);
RCC_APB2PeriphClockCmd(SC_PIN_CMDVCC_GPIO_CLK, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(SC_USART_GPIO_CLK , ENABLE);
/* Enable USART clock */
RCC_APB2PeriphClockCmd(SC_USART_CLK, ENABLE);
/* SC_USART configuration ----------------------------------------------------*/
/* SC_USART configured as follow:
- Word Length = 9 Bits
- 0.5 Stop Bit
- Even parity
- BaudRate = 9677 baud
- Hardware flow control disabled (RTS and CTS signals)
- Tx and Rx enabled
- USART Clock enabled
*/
/* USART Clock set to 3.2/3.6 MHz (PCLK1 (32/36 MHZ) / 10) */
USART_SetPrescaler(SC_USART, 0x05);
/* USART Guard Time set to 16 Bit */
USART_SetGuardTime(SC_USART,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(SC_USART, &USART_ClockInitStructure);
RCC_GetClocksFreq(&RCC_ClocksStatus);
USART_InitStructure.USART_BaudRate =9600;
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(SC_USART, &USART_InitStructure);
/* Enable the SC_USART Parity Error Interrupt */
USART_ITConfig(SC_USART, USART_IT_PE, ENABLE);
/* Enable the SC_USART Framing Error Interrupt */
USART_ITConfig(SC_USART, USART_IT_ERR, ENABLE);
/* Enable SC_USART */
USART_Cmd(SC_USART, ENABLE);
/* Enable the NACK Transmission */
USART_SmartCardNACKCmd(SC_USART, ENABLE);
/* Enable the Smartcard Interface */
USART_SmartCardCmd(SC_USART, ENABLE);
/* Set RSTIN HIGH */
SC_Reset(Bit_SET);
/* Select 5V */
SC_VoltageConfig(SC_VOLTAGE_5V);
/* Disable CMDVCC */
SC_PowerCmd(DISABLE); |