cancel
Showing results for 
Search instead for 
Did you mean: 

Multiple USART using DMA and USB

SGonz.2
Associate II

Hello! I'm using a STM32F407VET6 to receive data from USART1 and USART2 with DMA and send it by USB, but I can only receive and send data from USART1.

This is my callback function

void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size){
  if(huart->Instance == USART1){
	  static uint8_t old_pos1 = 0;
	  uint8_t *ptemp1;
	  uint8_t i;
	  if (Size != old_pos1){
		if (Size > old_pos1){
		  ReceivedChars1 = Size - old_pos1;
		  for (i = 0; i < ReceivedChars1; i++){
			pBufferReadyForUser1[i] = RXBufferUser1[old_pos1 + i];
		  }
		}else{
		  ReceivedChars1 = RX_BUFFER_SIZE - old_pos1;
		  for (i = 0; i < ReceivedChars1; i++){
			pBufferReadyForUser1[i] = RXBufferUser1[old_pos1 + i];
		  }
		  if (Size > 0){
			for (i = 0; i < Size; i++){
			  pBufferReadyForUser1[ReceivedChars1 + i] = RXBufferUser1[i];
			}
			ReceivedChars1 += Size;
		  }
		}
		UserDataTreatment(huart, pBufferReadyForUser1, ReceivedChars1);
		ptemp1 = pBufferReadyForUser1;
		pBufferReadyForUser1 = pBufferReadyForReception1;
		pBufferReadyForReception1 = ptemp1;
	  }
	  old_pos1 = Size;
  }else if(huart->Instance == USART2){
	  static uint8_t old_pos2 = 0;
	  uint8_t *ptemp2;
	  uint8_t i;
	  if (Size != old_pos2){
		if (Size > old_pos2){
		  ReceivedChars2 = Size - old_pos2;
		  for (i = 0; i < ReceivedChars2; i++){
			pBufferReadyForUser2[i] = RXBufferUser2[old_pos2 + i];
		  }
		}else{
		  ReceivedChars2 = RX_BUFFER_SIZE - old_pos2;
		  for (i = 0; i < ReceivedChars2; i++){
			pBufferReadyForUser2[i] = RXBufferUser2[old_pos2 + i];
		  }
		  if (Size > 0){
			for (i = 0; i < Size; i++){
			  pBufferReadyForUser2[ReceivedChars2 + i] = RXBufferUser2[i];
			}
			ReceivedChars2 += Size;
		  }
		}
		UserDataTreatment(huart, pBufferReadyForUser2, ReceivedChars2);
		ptemp2 = pBufferReadyForUser2;
		pBufferReadyForUser2 = pBufferReadyForReception2;
		pBufferReadyForReception2 = ptemp2;
	  }
	  old_pos2 = Size;
  }
}

And this is my user data treatment function, where I use the USB.

void UserDataTreatment(UART_HandleTypeDef *huart, uint8_t* pData, uint16_t Size){
  uint8_t* pBuff = pData;
  uint8_t  i;
  uint8_t DataArray[100];
  for (i = 0; i < Size; i++){
	DataArray[i] = *pBuff;
    pBuff++;
  }
  CDC_Transmit_FS(DataArray, Size);
}

I'm calling HAL_UARTEx_ReceiveToIdle_DMA for each USART in main() and using separate buffers, but I can't receive data from USART2. Any suggestion?

Thank you!

1 REPLY 1
KDJEM.1
ST Employee

Hi @SGonz.2​ ,

If you use only USART2, can you send and receive data? 

Kaouthar

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.