cancel
Showing results for 
Search instead for 
Did you mean: 

read from UART DMA buffer on the fly

Wleon.1
Associate II

Hi, I have the below code where I trigger the UART DMA and write to a buffer, and in the while loop I simply do a read from the buffer 1st element:

MX_UART7_Init();
  Uartret = HAL_UART_Receive_DMA(&huart7, uartbuf, 300);
  while(1){
  
      if(Uartret == HAL_OK)
      {
 
          timeBuf[0] = uartbuf[0];
 
      }
 
  }

here is the uartbuf:

0693W000001sOXLQA2.png

however if I remover the read line the 00s are will with values which I am expected.

Is there a way to read from dma on the fly ?

13 REPLIES 13

Unusual is the unconditional busy loop reading from memory in a potential race condition with the DMA in the above code pesented by the OP. That is totally independent of HAL or whatever you prefer to use.

yup in the debugger, I do read the mem out to a variable (timeBuf[0] = uartbuf[0];) and timeBuf is zero too.

yup I did that, and no zeros were found if it is only a empty while loop, so I wonder if we can read from DMA buffer without stopping the DMA.

RMcCa
Senior II

Note that each message ends in a newline (0x0a). Use the uart character match interrupt with dma. When the character match interrupt happens, stop the dma, read the ntdr and calculate the size of the message and reset the ntdr and destination address. You can easily implement double buffering this way by alternating buffer pointers and using a global flag to keep track. With this method, you can receive the message completely in the background and get an interrupt when the last byte has arrived.​

Do you have any example I can follow ?