cancel
Showing results for 
Search instead for 
Did you mean: 

UART transmit works succesfully but reciever don't

SinanOZ
Associate

The device I am using is F303K8T6.

I am trying to learn. I have no problem with transmitting.

But when it comes to receive data it don't works. Interrupt not rises.

But when I connect tx pin to rx pin it works well. Interrput rised.

The code and concerning images is as following.

void _baslat_(void);
void USART2_IRQHandler(void);
void _yaz_(uint8_t yaz);
char USART_Read(void);
char AAA = 0;
volatile int tetiklendim = 0;
void print_console(char *A){
	int i;
	for(i=0;A[i]!='\0';i++){
		_yaz_(A[i]);
	}
}
////////////////////////////////////// inside while loop
  while (1)
  {
    /* USER CODE END WHILE */
	  if(tetiklendim){
		  print_console("Data Deteceted\n");
		  tetiklendim = 0;
	  }
	  else{
		  print_console("Failed to take\n");
	  }
	  HAL_Delay(1000);
 
 
 
 
    /* USER CODE
     *  BEGIN 3 */
  }
 
 
///////////////////////////////////////
void _baslat_(void){
//Activating Clocks
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_USART2_CLK_ENABLE();
//GPIO
GPIO_InitTypeDef decoy = {0};
decoy.Pin = GPIO_PIN_2|GPIO_PIN_3;
decoy.Alternate = GPIO_AF7_USART2;
decoy.Mode = GPIO_MODE_AF_PP;
decoy.Pull = GPIO_PULLUP;
decoy.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA,&decoy);
 
 
 
 
 
 
 
//USART2 settings
USART2->CR1 = 0;
USART2->BRR = (SystemCoreClock)/(9600);
USART2->CR1 |= (1<<5)|(1<<3)|(1<<2);
USART2->CR3 |= (1<<6);
USART2->CR1 |= 1;
//USART2 interrupts
NVIC_EnableIRQ(USART2_IRQn);
NVIC_SetPriority(USART2_IRQn,0);
}
 
volatile int trig = 0;
void USART2_IRQHandler(void){
	trig ^= 1;
	tetiklendim = 1;
	AAA = USART_Read();
}
void _yaz_(uint8_t yaz){
	//while (!(USART2->ISR & USART_ISR_TXE)); // Check to see if transmit data register is empty
	USART2->TDR = (yaz & 0xFF); // Load TX data register with ch
	int x = GPIOA->AFR;
	while (!(USART2->ISR & (1<<6))); // Check to see if transmit data register is empty
}
 
char USART_Read(void)
{
	//while (!(USART2->ISR & (1 << 5))){} // Check to see if recieve data register is empty
		return USART2->RDR; // Load TX data register with ch
}

0693W00000Y9or4QAB.pngThe picture above shows that transmisson works. But receiving don't works well.

0693W00000Y9orFQAR.pngThe picture above shot when TX RX pin are shorted. Interrupts are rising.

Any idea why I can't receive data through my computer ?

1 REPLY 1
SinanOZ
Associate

The full code and clock scheme is as follows.

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2023 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
 
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
void _baslat_(void);
void USART2_IRQHandler(void);
void _yaz_(uint8_t yaz);
char USART_Read(void);
char AAA = 0;
volatile int tetiklendim = 0;
void print_console(char *A){
	int i;
	for(i=0;A[i]!='\0';i++){
		_yaz_(A[i]);
	}
}
/* USER CODE END Includes */
 
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
 
/* USER CODE END PTD */
 
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
 
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
 
/* USER CODE END PM */
 
/* Private variables ---------------------------------------------------------*/
 
/* USER CODE BEGIN PV */
 
/* USER CODE END PV */
 
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
 
/* USER CODE END PFP */
 
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
 
/* USER CODE END 0 */
 
/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */
 
  /* USER CODE END 1 */
 
  /* MCU Configuration--------------------------------------------------------*/
 
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
  _baslat_();
  /* USER CODE BEGIN Init */
 
  /* USER CODE END Init */
 
  /* Configure the system clock */
  SystemClock_Config();
 
  /* USER CODE BEGIN SysInit */
 
  /* USER CODE END SysInit */
 
  /* Initialize all configured peripherals */
  /* USER CODE BEGIN 2 */
 
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
 
 
  while (1)
  {
    /* USER CODE END WHILE */
	  if(tetiklendim){
		  print_console("Data Deteceted\n");
		  tetiklendim = 0;
	  }
	  else{
		  print_console("Failed to take\n");
	  }
	  HAL_Delay(1000);
 
 
 
 
    /* USER CODE
     *  BEGIN 3 */
  }
  /* USER CODE END 3 */
}
 
/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
 
  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
 
  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
 
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  {
    Error_Handler();
  }
}
 
/* USER CODE BEGIN 4 */
void _baslat_(void){
//Activating Clocks
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_USART2_CLK_ENABLE();
//GPIO
GPIO_InitTypeDef decoy = {0};
decoy.Pin = GPIO_PIN_2|GPIO_PIN_3;
decoy.Alternate = GPIO_AF7_USART2;
decoy.Mode = GPIO_MODE_AF_PP;
decoy.Pull = GPIO_PULLUP;
decoy.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA,&decoy);
 
 
 
 
 
 
 
//USART2 settings
USART2->CR1 = 0;
USART2->BRR = (SystemCoreClock)/(9600);
USART2->CR1 |= (1<<5)|(1<<3)|(1<<2);
USART2->CR3 |= (1<<6);
USART2->CR1 |= 1;
//USART2 interrupts
NVIC_EnableIRQ(USART2_IRQn);
NVIC_SetPriority(USART2_IRQn,0);
}
 
volatile int trig = 0;
void USART2_IRQHandler(void){
	trig ^= 1;
	tetiklendim = 1;
	AAA = USART_Read();
}
void _yaz_(uint8_t yaz){
	//while (!(USART2->ISR & USART_ISR_TXE)); // Check to see if transmit data register is empty
	USART2->TDR = (yaz & 0xFF); // Load TX data register with ch
	int x = GPIOA->AFR;
	while (!(USART2->ISR & (1<<6))); // Check to see if transmit data register is empty
}
 
char USART_Read(void)
{
	//while (!(USART2->ISR & (1 << 5))){} // Check to see if recieve data register is empty
		return USART2->RDR; // Load TX data register with ch
}
 
 
 
 
 
 
 
 
 
 
 
/* USER CODE END 4 */
 
/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}
 
#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

0693W00000Y9otAQAR.png