cancel
Showing results for 
Search instead for 
Did you mean: 

Incorrect baudrate with STM32F103 and Smartcard on USART2

danesh
Associate II
Posted on October 07, 2015 at 15:53

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 / 😎 */

   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.

2 REPLIES 2
Posted on October 07, 2015 at 18:02

HSE_VALUE is provided so the internal math can figure out it's basis. You STILL need to rewrite the PLL configuration code in system_stm32f10x.c to reflect the multiplier and divider settings to get the part to 72 MHz, because it's not going to be 8 x9 any more, and the values are hard coded rather than computed/fitted on the fly.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
danesh
Associate II
Posted on October 08, 2015 at 17:19

Thanks for thep tip. I changed the PLL configuration and it works. It was apparently some kind of synch, problem.

Thanks again.