5

I'm using a STM32L432 device with FreeRTOS and STM32CubeMX.

I try to implement a M2M-Communication via USART based on an ASCII protocol. The protocol sequences can differ in length but have a maximum length and a defined end character ('\r' / 0x0D).

So I thought about collecting all RX-USART data with DMA (like a FIFO) and using the address match isr based on the USART_ICR_CMCF flag to determine an end character.

Initialize USART1 and enable address match isr

void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle) {
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(uartHandle->Instance==USART1) {
    /* USART1 clock enable */
    __HAL_RCC_USART1_CLK_ENABLE();
    __HAL_RCC_GPIOA_CLK_ENABLE();
    GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    /* USART1 interrupt Init */
    HAL_NVIC_SetPriority(USART1_IRQn, 5, 0);
    HAL_NVIC_EnableIRQ(USART1_IRQn);

    /* USER CODE BEGIN USART1_MspInit 1 */
    USART1->CR2 |= 0x0D000000; // \r 0x0D
    __HAL_UART_ENABLE_IT(&huart1,UART_IT_CM);
  }
}

USART1 isr handler

void USART1_IRQHandler(void) {
  if (USART1->ISR & USART_ISR_CMF) {
    data = USART1->RDR;  
    SET_BIT(USART1->ICR,USART_ICR_CMCF);
  }
  HAL_UART_IRQHandler(&huart1);
}

Right now, the address match isr works fine, but I don't have a idea how to implement the DMA / FIFO support.

BTW:

I was very surprised, that the device doesn't support a USART HW FIFO. Is my idea to use DMA to reproduce the FIFO commonly used?

1 Answer 1

5
+50

The point of DMA is to not involve the CPU in every byte being transferred. If your ISR is being called for every byte then CPU gets involved so simultaneously enabling DMA, if at all that is possible, won't yield any performance benefits. Get rid of any one of the two - per byte interrupts or the DMA. If you most definitely want to check for a particular character as it arrives then DMA would not help.

Another popular approach to detect end of input when using arbitrary length input along with DMA is to use the USART idle interrupt. This interrupt is triggered when one byte time (time required to transfer one byte at current baud rate) elapses without any transfer. In this interrupt you can transfer the DMA buffer contents to another memory location then reinitialize DMA for future input and leave. Or you can process the input then and there. You can do whatever you want in the Idle ISR as long as the ISR completes execution quickly.

If your input has large continuous runs of data then the idle interrupt would trigger after a long time and you might have overwritten your buffer by then. You can use other DMA interrupts like Half Complete and Full Complete to handle this. So that can be taken care of too. I personally found this method to be buggy during stress testing. But there is no reason for it to be so, I didn't get enough time to debug it when I tried to use it but you will find articles online about this technique.

5
  • Can you explain -- why can't the character match be used to generate an interrupt IF and only IF the character matches with DMA? I looked at the reference manual and it's not clear to me why idle-detection could be used with DMA but not character match. Commented Aug 20, 2020 at 13:25
  • DMA does not have any mechanism to execute custom logic. It will simply populate the buffer in memory with the received bytes and raise an interrupt when buffer is full (or even half full). The interrupt can be used to trigger examination of received data but if the buffer is not full, you would never get the interrupt and examination won't be triggered. '
    – Dojo
    Commented Aug 21, 2020 at 10:42
  • I've seen many examples now where the DMA is setup to use circular mode, and then the UART's idle-line detection or character-match flags are used to create an interrupt. The interrupt is triggered by the UART, not by the DMA. Commented Aug 22, 2020 at 13:20
  • And that's what I have written in my answer
    – Dojo
    Commented Aug 25, 2020 at 5:06
  • Perhaps I misunderstood, but I was confused by " If you most definitely want to check for a particular character as it arrives then DMA would not help." Because I did want to check for a particular character, and I wanted to use DMA since it was more CPU efficient. So in my case, DMA does help when checking for a particular character (using the match flag) since it is more efficient than not using DMA. Commented Aug 26, 2020 at 8:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.