cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_UART_Transmit_IT only send first letter

DU2
Associate II

I am very new to use hal_usartTransmit_it because I use only usart_transmit() , which is the blocking mode.

I changed all of my transmit() code to transmit_it() for better performance, but after that my ble send message only shows buffer's first letter

Could you give some advice?

my usart init code

 
huart2.Instance = USART2;
 
 huart2.Init.BaudRate = 9600;
 
 huart2.Init.WordLength = UART_WORDLENGTH_8B;
 
 huart2.Init.StopBits = UART_STOPBITS_1;
 
 huart2.Init.Parity = UART_PARITY_NONE;
 
 huart2.Init.Mode = UART_MODE_TX_RX;
 
 huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
 
 huart2.Init.OverSampling = UART_OVERSAMPLING_16;
 
 huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
 
 huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
 
 if (HAL_UART_Init(&huart2) != HAL_OK)
 
 {
 
  Error_Handler();
 
 }
 
 /* USER CODE BEGIN USART2_Init 2 */
 
 
 
 /* USER CODE END USART2_Init 2 */
 
 
 
  
 
 HAL_NVIC_EnableIRQ(USART2_IRQn);
 
  
 
 HAL_NVIC_SetPriority(USART2_IRQn, 1, 0);
 
 
  __HAL_UART_ENABLE_IT(&huart2 , UART_IT_RXNE ); //uart rx it ON
 

in Main

 
txBuffer = "ABCDEF";
 
txSize=6
 
 
 
if(HAL_UART_Transmit_IT(&huart2 , (uint8_t*)txBuffer , txSize)!= HAL_OK)
 
       {
 
        Error_Handler();
 
       }
 
        
 
but I only receive 'A'.
 

my Usart Interrupt Handler, mainly for the receive_it handling.

 
*/
 
void USART2_IRQHandler(void)
 
{
 
  HAL_NVIC_DisableIRQ(USART2_IRQn);
 
  HAL_NVIC_DisableIRQ(DMA1_Channel1_IRQn); 
 
  usart_intCount++;
 
   
 
  uint8_t tempBuffer[20]={0,};
 
 HAL_UART_IRQHandler(&huart2);
 
  
 
 HAL_UART_Receive(&huart2,tempBuffer,20, 0 );
 
  
 
  
 
   int i=0;
 
   
 
  while(tempBuffer[i] ){
 
    
 
   if(tempBuffer[i]>47 && tempBuffer[i]<58 ){ //숫�??
 
    buffer[bufferIndex] = tempBuffer[i];
 
    bufferIndex+=1;
 
   }
 
    
 
   else if(tempBuffer[i] >64 && tempBuffer[i] < 91){ //대문�??
 
     buffer[bufferIndex] = tempBuffer[i];
 
     bufferIndex+=1;
 
   }
 
    
 
   i++;
 
  }
 
    
 
   
 
   
 
  for(int i=0;i<bufferIndex;i++){
 
    
 
   if(buffer[i] == 'F'){ //F RECEIVED? -> anyway, read the buffer !
 
     
 
    needCallback=1;
 
    bufferIndex=0;
 
    break;
 
   }
 
    
 
  } //reading tempBuffer and move to -> buffer
 
  
 
   
 
  rxReady=1;
 
   
 
  HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn); 
 
   HAL_NVIC_EnableIRQ(USART2_IRQn);
 
}
 

Thanks for reading.

1 REPLY 1
KnarfB
Principal III

I won't go through all details of our code. If you are using HAL, use a completion callback too:

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
...
}

and don't fiddle in the USART2_IRQHandler directly. Especially don't call a blocking function (HAL_UART_Receive) in the IRQ handler.

The basic flow is

char ch;
 
main:
  HAL_UART_Receive_IT( &huart2, &ch, 1 );
 
HAL_UART_RxCpltCallback:
   // process ch (global variable)
   // re-enable interrupt handling for receiving next char
   HAL_UART_Receive_IT( &huart2, &ch, 1 );

There are more advanced solutions see https://stm32f4-discovery.net/2017/07/stm32-tutorial-efficiently-receive-uart-data-using-dma/