Skip to main content
Visitor II
February 1, 2020
Question

UART DMA

  • February 1, 2020
  • 4 replies
  • 1294 views

I am using an STM32L0XX on a Nucleo-64. I am Trying to communicate with a sensor using SDI12 protocol ​(1200baud 8-bit even parity).

To communicate properly you have to implement a question and answer session. To start with a 2-byte question response is a 3-byte answer. The second question is a 3-byte question with a 32-byte answer. The third question is a 4-byte question with a 16-byte answer.

My question is how can one "redefine" the UART DMA statement that you start off with? say HAL_UART_DMA (huart2, (uint8_t)rx_buffer, 3) to accomodate the differing answers?

Graham

Durban​

    This topic has been closed for replies.

    4 replies

    Graduate II
    February 1, 2020

    Chain a secondary transfer on the DMA Completion?

    Visitor II
    February 1, 2020

    This is usually handled by a state machine.

    while(1) {
     switch(sensorstate) {
     case 1:
     // send 1st request with dma
     // receive 3 bytes with dma
     sensorstate = 2;
     break;
     case 2:
     if(/* dma receive completed */) {
     // send 2nd request
     // receive 32 bytes
     sensorstate = 3;
     }
     break;
     case 3:
     // etc
     }
     // process other stuff
    }

    GGill.1Author
    Visitor II
    February 2, 2020

    Neither of these answers really helps. I realise that a state machine would work but my question was how do you redeine the statement HAL_UART_DMA (huart2, (uint8_t)rx_buffer, 3) to say HAL_UART_DMA (huart2, (uint8_t)rx_dataBuffer, 32), or whatever, as the STM32 doesn't seem to change the receive array and keeps the first defined statement.

    Visitor II
    February 2, 2020

    What do you want to receive into what? You write HAL_UART_Receive_DMA (huart2, (uint8_t)rx_buffer, 3) then it sets DMA up to receive 3 bytes. You write HAL_UART_Receive_DMA (huart2, (uint8_t)rx_buffer, 32) then it sets DMA up to receive 32 bytes. You can make the last parameter a variable, then you can assign it a different value after each call, depending on the previous value (effectively creating a state machine, with the receive calls moved outside the switch). What is keeping the first defined statement where? What do you mean by the first defined statement? (One can define macros, types, variables and functions in C, but no statements. Statements are part of a function definition.)

    Super User
    February 2, 2020

    HAL+UART+DMA might by tricky. Do you really need DMA at such a low speed as 1200 baud? Can you simply use a sequence of HAL_UART_Transmit / HAL_UART_Receive blocking calls? Alternatively, because there may be transmission errors in a long serial connection, you might implemend a two level parsing process. the lower level could receive single char's (using UART Rx interrupts) until an entire line ending with <CR><LF> is accumulated. Once the line is complete, you pass the whole line to am "upper level" line parser. A similar parsing process is often used with GPS NMEA telegrams (another variable length line oriented serial protocol)

    hth

    KnarfB