Skip to main content
Visitor II
June 6, 2022
Question

Multiple USART using DMA and USB

  • June 6, 2022
  • 1 reply
  • 553 views

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!

    This topic has been closed for replies.

    1 reply

    Technical Moderator
    June 9, 2022

    Hi @SGonz.2​ ,

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

    Kaouthar