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
Using C Syntax Highlighting
- //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
Syntax parsed in 0.010 seconds
buffer.c
Using C Syntax Highlighting
- #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);}
Syntax parsed in 0.012 seconds
ISR for USART_RXC_vect





