updated serial system

This commit is contained in:
IMback
2018-11-14 11:29:12 +01:00
parent 18dc36e928
commit b72ec4ba8a
3 changed files with 42 additions and 28 deletions

View File

@ -18,14 +18,15 @@
#include <stdint.h> #include <stdint.h>
template < int BUFFER_SIZE > template < int BUFFER_SIZE, typename T = uint8_t >
class RingBuffer class RingBuffer
{ {
private: private:
uint_fast16_t _headIndex = 0; volatile uint_fast16_t _headIndex = 0;
uint_fast16_t _tailIndex = 0; volatile uint_fast16_t _tailIndex = 0;
uint8_t _buffer[BUFFER_SIZE]; volatile bool _overrun = false;
volatile T _buffer[BUFFER_SIZE];
public: public:
@ -34,22 +35,29 @@ public:
flush(); flush();
} }
uint_fast16_t remaining() uint_fast16_t remaining() const volatile
{ {
return (_headIndex-_tailIndex); return (_headIndex-_tailIndex);
} }
bool isOverun(uint_fast16_t margin = 0) uint_fast16_t remainingCapacity() const volatile
{ {
return remaining() >= BUFFER_SIZE - margin; return BUFFER_SIZE - (_headIndex-_tailIndex);
} }
bool isEmpty() bool isOverun() volatile
{
bool returnVal = _overrun;
_overrun = false;
return returnVal;
}
bool isEmpty() const volatile
{ {
return _tailIndex >= _headIndex; return _tailIndex >= _headIndex;
} }
uint8_t read() T read() volatile
{ {
if(!isEmpty()) if(!isEmpty())
{ {
@ -59,7 +67,7 @@ public:
else return '\0'; else return '\0';
} }
unsigned int read( uint8_t* buffer, unsigned int length ) unsigned int read( T* buffer, unsigned int length ) volatile
{ {
unsigned int i = 0; unsigned int i = 0;
for(; i < length && !isEmpty(); i++) for(; i < length && !isEmpty(); i++)
@ -69,7 +77,7 @@ public:
return i; return i;
} }
void write( uint8_t in ) void write( T in ) volatile
{ {
if (_headIndex - BUFFER_SIZE > 0 && _tailIndex - BUFFER_SIZE > 0) if (_headIndex - BUFFER_SIZE > 0 && _tailIndex - BUFFER_SIZE > 0)
{ {
@ -78,21 +86,26 @@ public:
} }
_buffer[_headIndex % BUFFER_SIZE] = in; _buffer[_headIndex % BUFFER_SIZE] = in;
_headIndex++; _headIndex++;
} if(remaining() > BUFFER_SIZE)
void write( uint8_t* buffer, unsigned int length )
{ {
for(uint8_t i = 0; i < length; i++) write(buffer[i]); _overrun = true;
_tailIndex = _headIndex - BUFFER_SIZE;
}
} }
void flush() void write( T* buffer, const unsigned int length ) volatile
{
for(unsigned int i = 0; i < length; i++) write(buffer[i]);
}
void flush(T flushCharacter = ' ') volatile
{ {
_headIndex = 0; _headIndex = 0;
_tailIndex = 0; _tailIndex = 0;
for(int i = 0; i < BUFFER_SIZE; i++) _buffer[i] = ' '; for(int i = 0; i < BUFFER_SIZE; i++) _buffer[i] = flushCharacter;
} }
unsigned int getString(uint8_t terminator, char* buffer, const unsigned int bufferLength) unsigned int getString(T terminator, T* buffer, const unsigned int bufferLength) volatile
{ {
unsigned int i = 0; unsigned int i = 0;
for(; i <= remaining() && i <= BUFFER_SIZE && _buffer[(_tailIndex+i) % BUFFER_SIZE] != terminator; i++); for(; i <= remaining() && i <= BUFFER_SIZE && _buffer[(_tailIndex+i) % BUFFER_SIZE] != terminator; i++);
@ -100,7 +113,7 @@ public:
if( i < remaining() && i > 0) if( i < remaining() && i > 0)
{ {
if(i > bufferLength-1) i = bufferLength-1; if(i > bufferLength-1) i = bufferLength-1;
read((uint8_t*)buffer, i); read(buffer, i);
buffer[i]='\0'; buffer[i]='\0';
_tailIndex++; _tailIndex++;
} }

View File

@ -1,14 +1,14 @@
#include "serial.h" #include "serial.h"
#include "ringbuffer.h" #include "ringbuffer.h"
RingBuffer<SERIAL_BUFFER_SIZE> rxBuffer; volatile RingBuffer<SERIAL_BUFFER_SIZE, volatile uint8_t> rxBuffer;
bool stopped = false; bool stopped = false;
ISR(USART_RX_vect) //I have seen worse interrupt sintax ISR(USART_RX_vect) //I have seen worse interrupt sintax
{ {
rxBuffer.write(UDR0); rxBuffer.write(UDR0);
if(serialFlowControl && !stopped && rxBuffer.isOverun(32)) if(serialFlowControl && !stopped && rxBuffer.remainingCapacity() < 32)
{ {
loop_until_bit_is_set(UCSR0A, UDRE0); loop_until_bit_is_set(UCSR0A, UDRE0);
UDR0 = 0x13; UDR0 = 0x13;
@ -23,6 +23,7 @@ Serial::Serial()
UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); UCSR0C = _BV(UCSZ01) | _BV(UCSZ00);
UCSR0B = _BV(RXEN0) | _BV(TXEN0); //Enable RX and TX UCSR0B = _BV(RXEN0) | _BV(TXEN0); //Enable RX and TX
UCSR0B |= (1 << RXCIE0); //Enable Rx interuppt UCSR0B |= (1 << RXCIE0); //Enable Rx interuppt
sei();
} }
void Serial::putChar(const char c) void Serial::putChar(const char c)
@ -98,7 +99,7 @@ char Serial::getChar()
{ {
if(!rxBuffer.isEmpty()) if(!rxBuffer.isEmpty())
{ {
if(serialFlowControl && stopped && !rxBuffer.isOverun(32)) if(serialFlowControl && stopped && rxBuffer.remainingCapacity() > 32 )
{ {
loop_until_bit_is_set(UCSR0A, UDRE0); loop_until_bit_is_set(UCSR0A, UDRE0);
UDR0 = 0x11; UDR0 = 0x11;
@ -111,7 +112,7 @@ char Serial::getChar()
unsigned int Serial::getString(char* buffer, const int bufferLength) unsigned int Serial::getString(char* buffer, const int bufferLength)
{ {
return rxBuffer.getString(_terminator, buffer, bufferLength); return rxBuffer.getString(_terminator, (uint8_t*)buffer, bufferLength);
} }
unsigned int Serial::read(char* buffer, const unsigned int length ) unsigned int Serial::read(char* buffer, const unsigned int length )

View File

@ -2,7 +2,7 @@
#define SERIAL_H #define SERIAL_H
#define BAUD 38400 #define BAUD 38400
#define SERIAL_BUFFER_SIZE 128 #define SERIAL_BUFFER_SIZE 256
#include <util/setbaud.h> #include <util/setbaud.h>
#include <avr/io.h> #include <avr/io.h>