Skip to main content
kosan
Associate II
June 19, 2018
Question

uart receive buffer queue

Posted on June 19, 2018 at 15:00

hi everyone,

i am using stm32f103 mcu and silicon labs' bgm113 ble module in ncp mode. bgm113 sends datas over uart and mcu handles the operation. in ncp mode there two types receiving data: command and event. command data is response to commands and that type is works good. but event data comes when event trigered and unexpected time and that is creates bugs sometimes. Also if i connect more than 2 modules problems occured again. a data packet comes and while mcu is busy (for example sending data to internet) another data packet comes too. So mcu doesn't know previous packet. i tried receive data with two dimension receive buffer like circular buffer i failed again. is there anyone worked bgm modules or what can you give me algorithm offer about that ?
    This topic has been closed for replies.

    6 replies

    T J
    Senior III
    June 19, 2018
    Posted on June 20, 2018 at 01:36

    you have two serial devices on two different UARTS ?

    you have DMA circular mode on both UARTS with a buffer of 1024 bytes each ?

    You will receive everything sent to either port

    So what is the problem ?

    kosan
    kosanAuthor
    Associate II
    June 20, 2018
    Posted on June 20, 2018 at 09:30

    yes i have two serial devices and circular dma mode on both uarts. there is no cr+lf byte or anything in bgm's data package. i use a 3ms timer interrupt to understand receiving finished. when a receiving finishes    i reset receiveBufferIndex.  so if any package comes before other package implemented, i lose previous package.  Actually problem is that loosing previous data.

    i have not a problem receiving data.   for example i received data from bgm module, and i will send to server over gsm. mcu tries to connect server and send data, sometimes it takes 2-3 seconds, in that 2 seconds i may take more than one data package from bgm modules. when sending data to server is finishes, mcu looks what received from bgm module but it knows just last one, and it doesn't know anything about previous  data.

    i hope it is understandable now and sorry for english. thanks for your replyy

    T J
    Senior III
    June 20, 2018
    Posted on June 20, 2018 at 11:44

    maybe change your approach a little.

    I track the DMA buffer size, do you know the incoming packet length ?

    with this code style, I can peek at the receive buffer before I receive it.

    lets say you are in the mS systick process, you can call these ;

    char readableU1(void) {   // incoming bytes ptr is calculated from the DMA pointer

        U1RxBufferPtrIN =  U1RxBufSize - huart1.hdmarx->Instance->CNDTR; 

    return (U1RxBufferPtrIN - U1RxBufferPtrOUT) & (U1RxBufSize-1); // bufSize must be 2^n  // unread buffer length

    }

    char getc1(void) {

        char Rxbyte = Usart1RxDMABuffer[U1RxBufferPtrOUT++];

        if (U1RxBufferPtrOUT >= U1RxBufSize) U1RxBufferPtrOUT = 0;

        return Rxbyte;

    }

    char peekRxU1(int offset) {      // offset of zero returns the next byte to be read

       int relativePtr = (U1RxBufferPtrOUT+ offset) & (U1RxBufSize-1); // bufSize must be 2^n

        char PeekRx= Usart1RxDMABuffer[relativePtr ]; // just looking Don't increment pointer

    return PeekRx;

    }

    you know the current output pointer U1RxBufferPtrOUT

    and you can read the current DMA input pointer U1RxBufferPtrIN

    lets say your frame is 3- 10 bytes long

    currentUnreadBufferSize = (U1RxBufferPtrIN - U1RxBufferPtrOUT) & (U1RxBufSize-1); // bufSize must be 2^n