cancel
Showing results for 
Search instead for 
Did you mean: 

usart communication using interrupts

anjana087
Associate II
Posted on September 30, 2016 at 09:25

Hi everyone!

I am doing a project using the HY-MiniSTM32V board.the software used are STM32CubeMX and the IDE is Atollic TrueStudio. I wrote the following code for receiving data back from the terminal(Tera Term) to the board using interrupts.The logic is that when we type ''hello'' in the terminal,I should receive ''helloanjana''.But I am getting ''anjana'' at first and then only Iam getting ''helloanjana'' and also if I type ''hello'' i get back ''helloanjana''and again if i type ''hai'' Iam getting ''hellohaianjana'' and so on.Can anyone please help me.Iam new to this field.Eagerly waiting for reply.

/* Includes ------------------------------------------------------------------*/

#include ''stm32f1xx_hal.h''

/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private variables ---------------------------------------------------------*/

UART_HandleTypeDef huart1;

/* USER CODE BEGIN PV */

/* Private variables ---------------------------------------------------------*/

uint8_t buffrec[1000];

uint8_t data[]={97,110,106,97,110,97,012};

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);

void Error_Handler(void);

static void MX_GPIO_Init(void);

static void MX_USART1_UART_Init(void);

/* USER CODE BEGIN PFP */

/* Private function prototypes -----------------------------------------------*/

/* USER CODE END PFP */

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

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();

  /* Configure the system clock */

  SystemClock_Config();

  /* Initialize all configured peripherals */

  MX_GPIO_Init();

  MX_USART1_UART_Init();

  /* USER CODE BEGIN 2 */

  __HAL_UART_ENABLE_IT(&huart1, UART_IT_RXNE);//activated flag receptile

 

  /* USER CODE END 2 */

  /* Infinite loop */

  /* USER CODE BEGIN WHILE */

  while (1)

  {

  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

     HAL_UART_Receive_IT(&huart1,buffrec,1000);

     HAL_UART_Transmit(&huart1,data,7,10);

     HAL_UART_Transmit_IT(&huart1,buffrec,1000);

     HAL_Delay(2000);

  }

  /* USER CODE END 3 */

}

/** System Clock Configuration

*/

void SystemClock_Config(void)

{

  RCC_OscInitTypeDef RCC_OscInitStruct;

  RCC_ClkInitTypeDef RCC_ClkInitStruct;

  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;

  RCC_OscInitStruct.HSEState = RCC_HSE_ON;

  RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;

  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;

  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;

  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL2;

  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)

  {

    Error_Handler();

  }

  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_DIV1;

  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)

  {

    Error_Handler();

  }

  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  /* SysTick_IRQn interrupt configuration */

  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);

}

/* USART1 init function */

static void MX_USART1_UART_Init(void)

{

  huart1.Instance = USART1;

  huart1.Init.BaudRate = 115200;

  huart1.Init.WordLength = UART_WORDLENGTH_8B;

  huart1.Init.StopBits = UART_STOPBITS_1;

  huart1.Init.Parity = UART_PARITY_NONE;

  huart1.Init.Mode = UART_MODE_TX_RX;

  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;

  huart1.Init.OverSampling = UART_OVERSAMPLING_16;

  if (HAL_UART_Init(&huart1) != HAL_OK)

  {

    Error_Handler();

  }

}

/** Configure pins as

        * Analog

        * Input

        * Output

        * EVENT_OUT

        * EXTI

*/

static void MX_GPIO_Init(void)

{

  /* GPIO Ports Clock Enable */

  __HAL_RCC_GPIOA_CLK_ENABLE();

}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**

  * @brief  This function is executed in case of error occurrence.

  * @param  None

  * @retval None

  */

void Error_Handler(void)

{

  /* USER CODE BEGIN Error_Handler */

  /* User can add his own implementation to report the HAL error return state */

  while(1)

  {

  }

  /* USER CODE END Error_Handler */

}

#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

/**

  * @}

  */

/**

  * @}

*/

2 REPLIES 2
Walid FTITI_O
Senior II
Posted on September 30, 2016 at 12:09

Him.anjana,

You are not well managing your transmit and receive operation. You should wait for the end of transfer and receive in case of with interrupt. You can initialize a volatile variable as transmission flag:

__IO ITStatus UartReady = RESET;

Then You should put the wait statement after each transfer and receive IT:

/* Wait for the end of the transfer */ 
while (UartReady != SET)
{
}

And in the call back function you set the uartReady flag:

void HAL_UART_TxCpltCallback(UART_HandleTypeDef *UartHandle)
{
/* Set transmission flag: transfer complete */
UartReady = SET;
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)
{
/* Set transmission flag: transfer complete */
UartReady = SET;
}

Note: The best way to use either IT transfer or pulling transfer and avoid to put them together. There is a ready to use example in the STM32CubeF1 that similar to this case at this path:STM32Cube_FW_F1_V1.3.0\Projects\STM3210C_EVAL\Examples\UART\UART_TwoBoards_ComIT -Hannibal-
anjana087
Associate II
Posted on October 01, 2016 at 06:08

Hi Hannibal,

Thank you for the response.Can you just send me the entire code for this program? Since I am totally new to this field,I am finding it a bit difficult to understand things.I think the code which I have written needs some changes. The idea of interrupt is that we have to make a routine which will process it when interrupt will appear isn't it?

But here the program receives, places in buffer, but that is made all around delay function, which reduces benefits on interrupt to zero.Also if there is no input data it will still transmit something, that is wrong too.Can you please send me the correct code which satisfies all these?Eagerly waiting for the reply.