cancel
Showing results for 
Search instead for 
Did you mean: 

Connect STM32F4 with smart card ISO7816

Egor Vasilyev
Associate II

Hi, everybody. My task is to connect STM32F4 discovery Board with Smart Card ISO7816. I don't use interface ST8024CDR only hooked paid directly by the scheme specified in the image:0690X000008ANWeQAO.png

I tried to adapt the Cubex project to STM32F1xx-EVAL. ATR reception does not work. The oscilloscope shows that the I/O port from the map is something similar to a sawtooth signal, but more flat (on three different maps it is different). So I guess it's still an ATR.

The configuration of the UART and frequency:

void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
 
  /** Configure the main internal regulator output voltage 
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLM = 8;
  RCC_OscInitStruct.PLL.PLLN = 168;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 4;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
 
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  {
    Error_Handler();
  }
}
static void MX_USART1_SMARTCARD_Init(void){
  /* USER CODE BEGIN USART1_Init 0 */
	__HAL_RCC_USART1_CLK_ENABLE();
  /* USER CODE END USART1_Init 0 */
 
  /* USER CODE BEGIN USART1_Init 1 */
	/* USART Clock set to 3,5 MHz (PCLK2 (84 MHz) / 24) */
  /* USER CODE END USART1_Init 1 */
  SmartCardHandle.Instance = USART1;
  SmartCardHandle.Init.BaudRate = 9408;	 /* Starting baudrate = 3,5MHz / 372etu */
  SmartCardHandle.Init.WordLength = SMARTCARD_WORDLENGTH_9B;
  SmartCardHandle.Init.StopBits = SMARTCARD_STOPBITS_1_5;
  SmartCardHandle.Init.Parity = SMARTCARD_PARITY_EVEN;
  SmartCardHandle.Init.Mode = SMARTCARD_MODE_TX_RX;
  SmartCardHandle.Init.CLKPolarity = SMARTCARD_POLARITY_LOW;
  SmartCardHandle.Init.CLKPhase = SMARTCARD_PHASE_1EDGE;
  SmartCardHandle.Init.CLKLastBit = SMARTCARD_LASTBIT_DISABLE;
  SmartCardHandle.Init.Prescaler = 12;
  SmartCardHandle.Init.GuardTime = 16;
  SmartCardHandle.Init.NACKState = SMARTCARD_NACK_DISABLE;
  if (HAL_SMARTCARD_Init(&SmartCardHandle) != HAL_OK)
  {
    Error_Handler();
  }
	__HAL_SMARTCARD_ENABLE(&SmartCardHandle);
  /* USER CODE BEGIN USART1_Init 2 */
	HAL_SMARTCARD_Init(&SmartCardHandle);
	SC_RESET(GPIO_PIN_SET);
  /* USER CODE END USART1_Init 2 */
}
void HAL_SMARTCARD_MspInit(SMARTCARD_HandleTypeDef* hsmartcard)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(hsmartcard->Instance==USART1)
  {
  /* USER CODE BEGIN USART1_MspInit 0 */
 
  /* USER CODE END USART1_MspInit 0 */
    /* Peripheral clock enable */
    __HAL_RCC_USART1_CLK_ENABLE();
  
    __HAL_RCC_GPIOA_CLK_ENABLE();
    /**USART1 GPIO Configuration    
    PA8     ------> USART1_CK
    PA9     ------> USART1_TX 
    */
    GPIO_InitStruct.Pin = GPIO_PIN_8;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
    GPIO_InitStruct.Pin = GPIO_PIN_9;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
    /* USART1 interrupt Init */
    HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(USART1_IRQn);
  /* USER CODE BEGIN USART1_MspInit 1 */
 
  /* USER CODE END USART1_MspInit 1 */
  }
 
}

Trying to read directly:

HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_RESET);
HAL_Delay(500);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_SET);
convention=HAL_SMARTCARD_Receive(&SmartCardHandle, atr_buff, atr_size, 500);
HAL_Delay(12);
/* Insert delay of 20ms for signal stabilization */
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_SET);
HAL_Delay(300);

PD15 - Reset.

I always get zeros. What could be the problem?

1 ACCEPTED SOLUTION

Accepted Solutions
Egor Vasilyev
Associate II

If anyone is interested, the problem is solved very simply. I used USART3 instead of USART1 and everything was considered successful.

View solution in original post

1 REPLY 1
Egor Vasilyev
Associate II

If anyone is interested, the problem is solved very simply. I used USART3 instead of USART1 and everything was considered successful.