cancel
Showing results for 
Search instead for 
Did you mean: 

UART DMA

GGill.1
Associate II

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​

5 REPLIES 5

Chain a secondary transfer on the DMA Completion?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
berendi
Principal

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.1
Associate II

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.

KnarfB
Principal III

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

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.)