cancel
Showing results for 
Search instead for 
Did you mean: 

uart receive buffer queue

kosan
Associate II
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 ?
6 REPLIES 6
T J
Lead
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
Associate II
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

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

kosan
Associate II
Posted on June 20, 2018 at 15:04

i don't know 

incoming packet length, every time it changes. it is not fixed length. if i get clearly what you mean i have to know every package's length and start adress. when i read i must use next data's adress pointer and length. 

Posted on June 20, 2018 at 15:25

>>

i have to know every package's length

You might have to be able to figure out the size on the fly, and separate individual packets.

If that is all based on 'timing', then you'd likely want to use the interrupt to process each byte at arrival, and manage a timeout count down.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on June 20, 2018 at 15:34

yes, i open timer when a byte comes, if there is no new byte in 3ms, timer interrupt runs and says package is finished.