AnsweredAssumed Answered

usart communication using interrupts

Question asked by m.anjana on Oct 13, 2016
Latest reply on Oct 19, 2016 by Clive One
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  from the terminal(Tera Term) to the board using interrupts.The logic is that when we type "hello"or anything in the terminal,I should receive "helloanjana"or whatever i have written followed by "anjana".But I am getting  "helloanjana" and also if I type "hai" Iam getting "hellohaianjana" and so on.And also this code is not working as an interrupt, ie the interrupt routine is not used here.Can anyone please help me and send me the correct code?Iam new to this field.Eagerly waiting for reply.I have attached my code.


uint8_t data[10];
uint8_t data3[10];
uint8_t data1[]={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();
int flag=0;
   /* Configure the system clock */
   SystemClock_Config();

   /* Initialize all configured peripherals */
   MX_GPIO_Init();
   MX_USART1_UART_Init();

   /* USER CODE BEGIN 2 */
   /* USER CODE END 2 */
while(1){

  /* Infinite loop */
   /* USER CODE BEGIN WHILE */


     if(HAL_UART_Receive_IT(&huart1,data,10))
     {
     HAL_GPIO_TogglePin(LED1_GPIO_Port,LED1_Pin);

     }
     for(int i=0;i<sizeof(data3);i++)
         {
             if(data3[i]!=data[i])
                 {flag=1;
             break;}

         }
     if(flag==1){
     HAL_UART_Transmit_IT(&huart1,data,10);

     }
     for(int i=0;i<sizeof(data3);i++)
     {
         data3[i]=data[i];
     }

flag=0;



   /* USER CODE END WHILE */

   /* USER CODE BEGIN 3 */
      // HAL_UART_Receive_IT(&huart1,data,1000);

      }


     // __HAL_UART_ENABLE_IT(&huart1,UART_IT_RXNE);
     // HAL_UART_IRQHandler(&huart1);

       //__HAL_UART_ENABLE_IT(&huart1,UART_IT_TC);
       //HAL_UART_Transmit(&huart1,data1,7,7);
       //HAL_UART_Receive_IT(&huart1,data,1000);
       //HAL_UART_Transmit_IT(&huart1,data,1000);
       //__HAL_UART_ENABLE_IT(&huart1,UART_IT_TC);
      //HAL_UART_Transmit_IT(&huart1,data1,7);


          //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_InitTypeDef GPIO_InitStruct;

   /* GPIO Ports Clock Enable */
   __HAL_RCC_GPIOB_CLK_ENABLE();
   __HAL_RCC_GPIOA_CLK_ENABLE();

   /*Configure GPIO pin Output Level */
   HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_RESET);

   /*Configure GPIO pin : LED1_Pin */
   GPIO_InitStruct.Pin = LED1_Pin;
   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
   HAL_GPIO_Init(LED1_GPIO_Port, &GPIO_InitStruct);

}

/* USER CODE BEGIN 4 */
void
HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
__NOP();
//test if we reach this position
}
void
HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
__NOP();
//test if we reach this position
}

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


inside stm32f1xx_it.c

void USART1_IRQHandler(void)
{
   /* USER CODE BEGIN USART1_IRQn 0 */
     uint8_t data1[]={97,110,106,97,110,97,012};
     HAL_UART_Transmit(&huart1,data1,7,7);

 

Outcomes