AnsweredAssumed Answered

USART RX interrupt driven ring buffer

Question asked by crow.john on Oct 14, 2015
Latest reply on Oct 14, 2015 by Clive One
Hey guys. First of all im new to this kind of stuff, so dont judge me please. 
I've been working for days on USART. I learned to transmit and receive strings via usart in normal mode, but as i try to do it with interrupts everything fall apart.

First I have tried to init usart interupts, and use it with single characters - it worked nicely, but now im trying to do it with multiple chars using ring buffer as a storage. After all i will try to parse the string that i got. I searched every thread but it seems that i cant find the right answer to this.

Maybe anyone have some examples of this? (ring buffer struct functions and implementation of it into usart interrupt receiver?)


struct bufer {
     char buf[buffsize];
     int start;
     int end;
     int count;
};
struct bufer buf = {0};


void UartBufferInit(void) {
     buf.start = 0;
     buf.end = 0;
}


bool UartBufferEmpty(void) {
     return (buf.start == buf.end) ? TRUE : FALSE;
}


bool UartBufferFull(uint8_t c) {
     return (((buf.end + 1) % buffsize) == buf.start) ? TRUE : FALSE;
}


bool PutIntoBuffer(uint8_t c) {
     if(!UartBufferFull()){
          buf.buf[buf.end] = c;
          buf.end = (buf.end +1) % buffsize;
          return TRUE;
     } else {
          return FALSE;
     }
}




void USART1_IRQHandler(void) {
     
     static char CopyOfBuf[buffsize];
     static int rx_index = 0;
     
     if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) {
          char rx = USART_ReceiveData(USART1);
          if ((rx == '\r') || (rx == '\n')) {
               if(rx_index != 0) {
                    memcpy(CopyOfBuf, buf.buf, sizeof(buf.buf)+1);
                    PutIntoBuffer(CopyOfBuf);     
               }
          }
     }
}

Outcomes