AnsweredAssumed Answered

Incorrect baudrate with STM32F103 and Smartcard on USART2

Question asked by dan on Oct 7, 2015
Latest reply on Oct 8, 2015 by dan
Hi all,

I am using a STM32F103 chip where USART1 and USART2 are connected to Smartcard units. I have tried to communicate to the Smartcard by first sending a reset signal (according to ISO 77816 definition) and then read back the answer-to-reset (ATR) without any success. In fact, when the reset signal is applies, only one byte is sent from the card, while at least two bytes needs to be sent. I have tried the Smartcard example in the STM32 examples, but the result has been same all the time. I suspect that there is a timing issue, since when I change the prescaler or the baudrate value of USART2 to something else, I get different values, and even sometimes parity error. In my configuration, the HSE input clock line is connected to a 12 MHz external clock, where I have changed the constant value for HSE in the STM32F103 header to 12000000 Hz already. The code that I am running is:


   /* USART2 Clock set to 4.5MHz (PCLK1 = 36 MHZ / 8) */
   USART_SetPrescaler(USART2, 0x04);
   /* USART2 Guard Time set to 2 Bit */
   USART_SetGuardTime(USART2, 0x2);

   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(USART2, &USART_ClockInitStructure);

   USART_InitStructure.USART_BaudRate = 12096;
   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(USART2, &USART_InitStructure);

   /* Enable the USART2 Parity Error Interrupt */
   USART_ITConfig(USART2, USART_IT_PE, ENABLE);

   USART_Cmd(USART2, ENABLE);

   /* Enable the NACK Transmission */
   USART_SmartCardNACKCmd(USART2, ENABLE);

   /* Enable the Smartcard Interface */
   USART_SmartCardCmd(USART2, ENABLE);

   uint8_t index;
   uint8_t buff[40];

   /* Read Smartcard ATR response */
   for(index = 0; index < 40; index++, Counter = 0)
   {
     while((USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET) && (Counter != 1000))
     {
       Counter++;
     }

     if(Counter < 1000)
     {
       buff[index] = USART_ReceiveData(USART2);
     }
   }


According to the Smartcard guidelines from ST, the Smartcards can communicate at 9600 bits per second, so it is not clear to me, why in the example, the baudrate is set to 12096. However, I also have tested with baudrate of 9600, without any success. I also have read the clock values using the following code:


  RCC_ClocksTypeDef RCC_ClocksStatus;

  RCC_GetClocksFreq(&RCC_ClocksStatus);

  uint32_t _ADCClK = RCC_ClocksStatus.ADCCLK_Frequency;
  uint32_t _HCLK = RCC_ClocksStatus.HCLK_Frequency;
  uint32_t _PCLK1 = RCC_ClocksStatus.PCLK1_Frequency;
  uint32_t _PCLK2 = RCC_ClocksStatus.PCLK2_Frequency;
  uint32_t _SYSCLK = RCC_ClocksStatus.SYSCLK_Frequency;


and the values for _PCLK1 and _SYSCLK are 54 MHz and 108 MHz respectively which are much higher than 36 MHz and 72 MHz which are the maximum speeds for PCLK1 and SYSCLK and I am not sure why these values are that high. I should note that at the start up of processor I call the function "SystemCoreClockUpdate()" if that would related any how.

I would appreciate any help that will help me to fix the communication problem with the smartcard.

Thanks in advance,

D.



Outcomes