Because of the Thanksgiving holiday in the U.S., TI E2E™ design support forum responses may be delayed the week of November 23.

This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

AM2434: AM243X: Problem with partial read of a UART message with DMA

Part Number: AM2434

Hi experts,

i encountered a problem when using UART with DMA to partially read a UART message.

I'm trying to read a package of unknown size with UART and DMA (PKTDMA instance) in two read calls. The first call reads the header with the length and the second call reads the rest of the package according to the length. With this implementation im getting random package drops though, because the DMA rearm between the first and second read doesnt work properly. In case of a package drop the UART read on the payload part finishes with less bytes read than requested, but the FIFO is empty. 

If i am using an interrupt based transfer (no DMA) or a delay between the header and payload part (with DMA) it works flawlessly, so i guess it is a timing issue with the rearm while the old DMA request is not properly finished.

Can you help me, how to fix this problem?

Cheers, 
Robin

  • Hi Robin,

    Please allow me sometime to revert back to this thread as I was caught up with some other tasks.

    Regards,

    Vaibhav

  • Hi Robin,

    Thanks for your patience.

    For a use case where the number of bytes is unknown, ad lets suppose we need to read the same.

    A combination of the following can be used to achieve it for non-DMA mode for UART:

    1. Timeout
    2. And basically getting to know exactly how many bytes have been read

    The timeout can be set pretty easily in the transaction before the UART_read call is issued inside your application.

    For reading the number of bytes read(this can be dynamic), you can refer this dedicated FAQ:  [FAQ] PROCESSOR-SDK-AM64X: Variable number of bytes read by UART 

    For UART DMA, the driver does not currently support partial UART reads or reads when the number of bytes to be received is unknown.

    Additionally, could you explain the flow of your application in points, so that I can help answer better.

    Regards,

    Vaibhav

  • Hi Vaibhav,

    thank you for your response.

    I am interested in solving my problem with DMA, but I will try and explain it better:

    I want to read one incoming UART transmission in the FIFO with two separate UART DMA calls. The first read tells me the size of the whole transmission, so both reads have a known number of bytes I want to recieve. But that implementation doesn't work. I keep getting random lost packages with the back to back UART DMA reads (on the already filled FIFO). 

    Should this theoretically be possible? And if yes, what might be the problems with the drivers at the moment?

    Regards, 
    Robin

  • Hi Robin,

    I want to read one incoming UART transmission in the FIFO with two separate UART DMA calls. The first read tells me the size of the whole transmission, so both reads have a known number of bytes I want to recieve. But that implementation doesn't work. I keep getting random lost packages with the back to back UART DMA reads (on the already filled FIFO). 

    Should this theoretically be possible? And if yes, what might be the problems with the drivers at the moment?

    This should not be a problem. I understood your statement.

    Is it true that UART RX on your board is connected to a UART TX or another external transmitter? If yes, then I would assume that you can fire or call UART Read with DMA enabled for UART twice in a row.

    Could you please share your application?

    Regards,

    Vaibhav

  • Hi Vaibhav,

    yes the UART RX on my board is connected externally. The application waits for an external message on UART, tries to read it in 2 DMA calls and on positive result, returns it back to the external part. I am using a custom board, but i tried to paste together all the important parts of the application and send it to you. 

    Cheers, 
    Robin
    UARTDMAexample.zip

  • Hi Robin,

    Have you incorporated cache writeback and invalidate in your application? I do see it, but can you incorporate writeback invalidate instead of just writeback and see?

    Regards,

    Vaibhav

  • Hi Vaibhav,

    yes i tried that just now with both read and send, but i still have the same behaviour. 

    Regards,
    Robin

  • yes the UART RX on my board is connected externally. The application waits for an external message on UART, tries to read it in 2 DMA calls and on positive result, returns it back to the external part. I am using a custom board, but i tried to paste together all the important parts of the application and send it to you. 

    I am going to try cloning the setup using just TI EVM.

    I am planning to use two UARTs in my application, lets say uart0 and uart1.

    This is the flow which I will follow:

    1. uart1 will be having some known data at its end in a buffer, lets say the data length will be 64 bytes.
    2. uart0 RX will receive 1 byte from uart1 TX with the value 64, as this value will determine that the next read which uart0 RX has to do is 64 bytes.
    3. Then the next read which uart0 RX does is of 64 bytes, in this case my uart0 RX buffer will be a big size lets suppose 1024 bytes as it will accommodate all the bytes sent from uart1 TX.
    4. Application ends.

    NOTE: This will be UART DMA reads itself.

    Do you want me to add anything else to the flow? Looking forward to your response.

    Regards,

    Vaibhav

  • Hi Vaibhav,
    that would be very helpful! 

    Two things i want to mention for the flow.

    1. The important thing is that when uart0 is recieving the 1 byte, uart1 needs to send the whole 65 bytes already, so uart0 needs to try to read the second part with a filled FIFO.
    2. Maybe try and loop the whole test a couple times (10-20), because it doesnt always fail for me.

    This isolated test will help me a lot, i'm looking forward to your results!

    Regards, 

    Robin

  • Hi Robin,

    So uart1 should just send 65 bytes once and be done, for example uart write for uart1 will be called just once.

    On the other hand, for uart0, uart read will be called twice, the first call being for the number of bytes to be read on the second call, and the second uart read for reading the n number of bytes.

    Do you want the structure to look like:

    • application starts
    • while loop(for about 20 times as per your test case)
    • uart1 TX sends data to uart0 RX
    • on uart0 RX side I call uart read twice
    • loop ends
    • application ends

    If this is something you also align on, I can test this tomorrow and let you know.

    Thanks,

    Vaibhav

  • Hi Vaibhav,

    yes that sounds perfect! And then maybe just some verification, whether the uart0 reads the second n number of bytes correct.

    Regards,
    Robin

  • Hi,

    Please expect responses in sometime.

    Thanks,

    Vaibhav

  • Hi Robin,

    I will test this tomorrow and update you.

    Thanks for your patience.

    Regards,

    Vaibhav

  • Hi Vaibhav,
    is there any update on this topic?

    Regards,
    Robin

  • Hi,

    I have started the test with MCU_UART1_TX sending data to MAIN_UART5_RXD, will update you with the results in sometime.

    Thanks for your patience.

    Regards,

    Vaibhav

  • Hi Robin,

    Could you please specify the exact UART instance over which you are receiving data on the board?

    Regards,

    Vaibhav

  • Hi Vaibhav,
    i tested it with UART Instance 3, but any of the UART instances from PDMA1 (UART 2 - 6) are interesting for me.

    Regards,
    Robin

  • Hi Robin,

    You mentioned the following:

    If i am using an interrupt based transfer (no DMA) or a delay between the header and payload part (with DMA) it works flawlessly

    I believe Interrupt mode as with Blocking.

    Could you walk me through the DMA one? Was it a callback or blocking mode for DMA?

    If it was Callback mode, then the wait/delay introduced is justified!

    Regards,

    Vaibhav

  • Hi Vaibhav,

    no it was blocking mode for DMA.

    Regards,

    Robin

  • Hi Robin,

    So the DMA rearm is required for each uart read operation.

    I am assuming before you issue a UART read, you are not setting a very low timeout on the uart transaction parameter called UART_transaction --> timeout!

    Apart from this, I am hoping you took care of the Cache writeback and invalidate calls as demonstrated here: https://github.com/TexasInstruments/mcupsdk-core/blob/next/examples/drivers/uart/uart_echo_dma/uart_echo_dma.c

    Upon iterating over a similar type of test, this is what I have observed:

    1. There are no failures observed soo far.

    2. My loop contains two uart reads the first one reading the first byte and the second one reading the rxBuffer[0] many bytes.

    3. Also the the data is received in one shot, thatis 64 bytes in one shot, out of which the first byte is 63, and the remaining bytes are 1, 2, 3...., 62, 63 and I am comparing at the end of the transaction, if the bytes received are correct or not.

    4. I am maintaining a count and a flag as well, when the flag is hit, I will take a look at the count var.

    Regards,

    Vaibhav

  • Hi Vaibhav,

    the first UART read has a rather long timeout, because im waiting for my external UART source to start with a message. 

    Could you send me the project of your example test on the EVM Board? I could have a look and maybe test it myself. This would help me a lot to try and get to the root of it not working on my board.

    Thanks in advance.

    Regards, 
    Robin

  • Could you send me the project of your example test on the EVM Board? I could have a look and maybe test it myself. This would help me a lot to try and get to the root of it not working on my board.

    Hi,

    I have tested for 14,540 cycles.

    There has been no failures observed soo far.

    All I did was modify the already existing example uart_echo_dma.

    My example is modified in the following way:

    1. The uart instance is firstly initialized.
    2. This is followed by an infinite loop until a faulty condition is met
      1. Faulty condition for me is a data mismatch.
    3. I am mimicking a RX transfer to the uart instance via a python script that I wrote.
    4. The python script is a basic script that opens a specific UART COM Port, in my case I am using: UART0 instance.
    5. My application is then going to wait for the script to send data which the UART0 will pick up on UART0_RXD and read.
    6. I read in two steps, firstly I read the first byte and then the remaining bytes that is denoted by (first byte's value)
    7. This loop goes on.
    8. Note, the python script has a delay of 5 seconds before it sends 64 bytes of data to UART0_RXD

    Here is the code snippet, please ignore the comments. 

    /*
     *  Copyright (C) 2021 Texas Instruments Incorporated
     *
     *  Redistribution and use in source and binary forms, with or without
     *  modification, are permitted provided that the following conditions
     *  are met:
     *
     *    Redistributions of source code must retain the above copyright
     *    notice, this list of conditions and the following disclaimer.
     *
     *    Redistributions in binary form must reproduce the above copyright
     *    notice, this list of conditions and the following disclaimer in the
     *    documentation and/or other materials provided with the
     *    distribution.
     *
     *    Neither the name of Texas Instruments Incorporated nor the names of
     *    its contributors may be used to endorse or promote products derived
     *    from this software without specific prior written permission.
     *
     *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     */
    
    /* This example demonstrates the UART RX and TX operation by echoing char
     * that it recieves in blocking, interrupt mode of operation.
     * When user types 'quit', the application ends.
     */
    
    #include <string.h>
    #include <stdio.h>
    #include <kernel/dpl/DebugP.h>
    #include "ti_drivers_config.h"
    #include "ti_drivers_open_close.h"
    #include "ti_board_open_close.h"
    
    #define APP_UART_BUFSIZE              (200U)
    #define APP_UART_RECEIVE_BUFSIZE      (64U)
    
    uint8_t gUartBuffer[APP_UART_BUFSIZE] __attribute__((aligned(CacheP_CACHELINE_ALIGNMENT)));
    uint8_t gUartReceiveBuffer[APP_UART_RECEIVE_BUFSIZE] __attribute__((aligned(CacheP_CACHELINE_ALIGNMENT)));
    volatile uint32_t gNumBytesRead = 0U, gNumBytesWritten = 0U;
    
    #define APP_UART_ASSERT_ON_FAILURE(transferOK, transaction) \
        do { \
            if((SystemP_SUCCESS != (transferOK)) || (UART_TRANSFER_STATUS_SUCCESS != transaction.status)) \
            { \
                DebugP_assert(FALSE); /* UART TX/RX failed!! */ \
            } \
        } while(0) \
    
    void uart_echo_dma(void *args)
    {
        int32_t          transferOK;
        UART_Transaction trans;
        #if defined (SOC_AM65X)
        int32_t         retVal = UDMA_SOK;
        uint32_t        instId;
        #endif
    
        Drivers_open();
        Board_driversOpen();
    
        DebugP_log("[UART] Echo example DMA mode started ...\r\n");
    
        UART_Transaction_init(&trans);
    
        // /* Send entry string */
        // gNumBytesWritten = 0U;
        // trans.buf   = &gUartBuffer[0U];
        // strncpy(trans.buf,"This is uart echo test DMA blocking mode\r\nReceives 8 characters then echo's back. Please input..\r\n", APP_UART_BUFSIZE);
        // trans.count = strlen(trans.buf);
        // CacheP_wb((void *)trans.buf, trans.count, CacheP_TYPE_ALL);
        // transferOK = UART_write(gUartHandle[CONFIG_UART_CONSOLE], &trans);
        // APP_UART_ASSERT_ON_FAILURE(transferOK, trans);
    
        int flag = 0;
        int32_t count = 0;
        while(flag == 0)
        {
            CacheP_wbInv((void *)&gUartReceiveBuffer[0U], APP_UART_RECEIVE_BUFSIZE, CacheP_TYPE_ALL);
            /* Read 8 chars */
            gNumBytesRead = 0U;
            trans.buf   = &gUartReceiveBuffer[0U];
            trans.count = 1;
            transferOK = UART_read(gUartHandle[CONFIG_UART_CONSOLE], &trans);
            APP_UART_ASSERT_ON_FAILURE(transferOK, trans);
    
    
            trans.count = gUartReceiveBuffer[0];
            CacheP_wbInv((void *)&gUartReceiveBuffer[0U], APP_UART_RECEIVE_BUFSIZE, CacheP_TYPE_ALL);
            /* Read 8 chars */
            gNumBytesRead = 0U;
            trans.buf   = &gUartReceiveBuffer[0U];
    
            transferOK = UART_read(gUartHandle[CONFIG_UART_CONSOLE], &trans);
            APP_UART_ASSERT_ON_FAILURE(transferOK, trans);
    
            int i = 0;
            for(i = 0; i < 63; i++)
            {
            if(gUartReceiveBuffer[i] != i+1)
                {
                    flag = 1;
                    break;
                }
            }
            if(flag == 0) count++;
        }
    
        printf("%d", count);
    
        // /* Echo chars entered */
        // gNumBytesWritten = 0U;
        // trans.buf   = &gUartReceiveBuffer[0U];
        // trans.count = APP_UART_RECEIVE_BUFSIZE;
        // transferOK = UART_write(gUartHandle[CONFIG_UART_CONSOLE], &trans);
        // APP_UART_ASSERT_ON_FAILURE(transferOK, trans);
    
        // /* Send exit string */
        // gNumBytesWritten = 0U;
        // trans.buf   = &gUartBuffer[0U];
        // strncpy(trans.buf, "\r\nAll tests have passed!!\r\n", APP_UART_BUFSIZE);
        // trans.count = strlen(trans.buf);
        // CacheP_wb((void *)trans.buf, trans.count, CacheP_TYPE_ALL);
        // transferOK = UART_write(gUartHandle[CONFIG_UART_CONSOLE], &trans);
        // APP_UART_ASSERT_ON_FAILURE(transferOK, trans);
    
        // DebugP_log("All tests have passed!!\r\n");
    
        Board_driversClose();
        Drivers_close();
        #if defined (SOC_AM65X)
        for(instId = 0U; instId < CONFIG_UDMA_NUM_INSTANCES; instId++)
        {
            retVal = Udma_deinit(&gUdmaDrvObj[instId]);
            DebugP_assert(UDMA_SOK == retVal);
        }
        #endif
    
        return;
    }
    

    Here's the python script:

    #!/usr/bin/env python3
    import serial
    import time
    
    def main():
        # Configuration
        COM_PORT = 'COM6'
        BAUD_RATE = 115200  # Adjust based on your device
        INTERVAL = 5 # seconds
        DATA_SIZE = 64  # bytes
    
        try:
            # Open serial connection
            ser = serial.Serial(COM_PORT, BAUD_RATE, timeout=1)
            print(f"Connected to {COM_PORT} at {BAUD_RATE} baud")
    
            # Create 64 bytes of data (you can customize this)
            data = [i for i in range(0, 64)]
            data[0] = 63
    
            print(f"Sending {len(data)} bytes every {INTERVAL} seconds...")
            print("Press Ctrl+C to stop\n")
    
            # Send data at intervals
            while True:
                try:
                    bytes_written = ser.write(data)
                    timestamp = time.strftime("%Y-%m-%d %H:%M:%S")
                    print(f"[{timestamp}] Sent {bytes_written} bytes")
                    time.sleep(INTERVAL)
                except serial.SerialException as e:
                    print(f"Error sending data: {e}")
                    break
    
        except serial.SerialException as e:
            print(f"Error opening COM port: {e}")
            print(f"Make sure {COM_PORT} exists and is not in use")
        except KeyboardInterrupt:
            print("\nStopped by user")
        finally:
            if 'ser' in locals() and ser.is_open:
                ser.close()
                print("Serial connection closed")
    
    if __name__ == "__main__":
        main()
    

    To run this you just type: python .\uart_sender.py

    Hope this helps.

    Regards,

    Vaibhav