added ringbuffer from thermocouple-project

master
Paul Goeser 14 years ago
parent 0f4c42b594
commit a3c1164442

@ -3,8 +3,13 @@
- programmieren
- sdcard
- timing von soft-spi
- streaming funktion
- pwm
- 11bit-pwm-emu
- drumherum
- data-streaming von sdcard
- ringbuf (ryx)
- testen
- sdcard
@ -19,5 +24,4 @@
== ryx ==
- belastbarkeit von kleinen Tastern nachschlagen

@ -0,0 +1,66 @@
#include "ringbuf_small.h"
#include <util/atomic.h>
/* Very generic implementation of an interrupt-safe ringbuffer
*
* Contention handling:
* - writing of pointers only with interrupt lock
* - reading of pointers allowed at any time
* - volatile not really necessary, unless you really need the functions to
* react to freespace/newdata while they're running
*
* PUT AND GET ARE NOT REENTRANT!
*/
void ringbuf_init(ringbuf_t *rb, char* buf, int size){
rb->startptr = buf;
rb->size = size;
rb->readpos = 0;
rb->writepos = 0;
}
/* pushes a value onto the ringbuffer
* returns 0 on success, 1 on buffer full
*/
uint8_t ringbuf_put(ringbuf_t *rb, uint8_t value){
// calculate next ptr pos
uint8_t next = rb->writepos + 1;
if(next >= rb->size){
next = 0;
}
// check for space
if(next == rb->readpos){
return(1); // give up
}
*(rb->startptr + rb->writepos) = value;
// do we need a barrier here?
rb->writepos = next;
return(0);
}
/* gets a value from the ringbuffer
* returns value on success, -1 on buffer empty
*/
int16_t ringbuf_get(ringbuf_t *rb){
uint8_t value;
uint8_t next;
// calculate next ptr pos
next = rb->readpos + 1;
if(next >= rb->size){
next = 0;
}
//check for empty
if(rb->readpos == rb->writepos){
return(-1);
}
value = *(rb->startptr + rb->readpos);
rb->readpos = next;
return(value);
}

@ -0,0 +1,18 @@
#ifndef __RINGBSML_H
#define __RINGBSML_H
#include <stdint.h>
typedef struct {
char* startptr;
uint8_t size;
uint8_t readpos;
uint8_t writepos;
} ringbuf_t;
void ringbuf_init(ringbuf_t* rb, char* buf, int size);
uint8_t ringbuf_put(ringbuf_t *rb, uint8_t value);
int16_t ringbuf_get(ringbuf_t *rb);
#endif
Loading…
Cancel
Save