65 lines
1.4 KiB
C
65 lines
1.4 KiB
C
|
/*
|
||
|
* ringbuffer.c
|
||
|
*
|
||
|
* Created on: Jul 24, 2012
|
||
|
* Author: tkl
|
||
|
*/
|
||
|
#include "irq.h"
|
||
|
#include "ringbuffer.h"
|
||
|
|
||
|
//-----------------------------------------------------------------------------
|
||
|
int ringbuffer_read(struct ringbuffer *this, char *buffer, int size) {
|
||
|
int i;
|
||
|
unsigned int irq;
|
||
|
for(i = 0; i < size; i++) {
|
||
|
if(this->used > 0) {
|
||
|
irq = disable_irq();
|
||
|
buffer[i] = *this->read;
|
||
|
this->read++;
|
||
|
this->used--;
|
||
|
if(this->read >= (this->buffer + this->size)) {
|
||
|
this->read = this->buffer;
|
||
|
}
|
||
|
restore_irq(irq);
|
||
|
}
|
||
|
else {
|
||
|
return i;
|
||
|
}
|
||
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
//-----------------------------------------------------------------------------
|
||
|
int ringbuffer_write(struct ringbuffer *this, const char *buffer,
|
||
|
int size)
|
||
|
{
|
||
|
int i = 0;
|
||
|
unsigned int irq;
|
||
|
for(i = 0; i < size; i++) {
|
||
|
if(this->used < this->size) {
|
||
|
irq = disable_irq();
|
||
|
*this->write = buffer[i];
|
||
|
this->write++;
|
||
|
this->used++;
|
||
|
if(this->write >= (this->buffer + this->size)) {
|
||
|
this->write = this->buffer;
|
||
|
}
|
||
|
restore_irq(irq);
|
||
|
}
|
||
|
else {
|
||
|
return i;
|
||
|
}
|
||
|
}
|
||
|
return i;
|
||
|
}
|
||
|
|
||
|
//-----------------------------------------------------------------------------
|
||
|
int ringbuffer_is_full(const struct ringbuffer *this) {
|
||
|
return (this->used == this->size) ? 1 : 0;
|
||
|
}
|
||
|
|
||
|
//-----------------------------------------------------------------------------
|
||
|
int ringbuffer_is_empty(const struct ringbuffer *this) {
|
||
|
return (this->used == 0) ? 1 : 0;
|
||
|
}
|