
I am writing a circullar buffer which will be used by USART RX Complete ISR to write and by a NMEA parser to read. Therefore, I have declared the array holding the buffer data as "volatile". Is this necessary?. Thanks.
buffer.h
Code: Select all
//Buffer for UART
#ifndef BUFFER_H
#define BUFFER_H
#include<stdint.h>
#define BUFFER_SIZE 128
extern void writeToBuffer(uint8_t data);
//extern uint8_t readFromBuffer();
extern int isFull();
extern int isEmpty();
extern void emptyBuffer();
#endif
Code: Select all
#include "buffer.h"
#include<stdlib.h>
short writePointer=0;
short readPointer=0;
short currentSize=0;
volatile uint8_t data[BUFFER_SIZE];//data
uint8_t readFromBuffer(){
uint8_t ret=0;
if(!isEmpty()){
ret = data[readPointer];
currentSize--;
readPointer = (readPointer + 1) & (BUFFER_SIZE-1); // This is the useful point is choosing the array length to be a power of two
}
return ret;
}
void writeToBuffer(uint8_t newByte){
if(!isFull()){
data[writePointer] = newByte;
currentSize++;
writePointer = (writePointer + 1) & (BUFFER_SIZE-1); // This is the useful point is choosing the array length to be a power of two
}
}
void flushBuffer(){}
int isFull(){return (currentSize==BUFFER_SIZE);}
int isEmpty(){return (currentSize==0);}
Code: Select all
ISR(USART_RXC_vect){
//copy data into buffer
writeToBuffer((uint8_t)UDR);
}