diff options
author | Markus Grüneis <gimpf@gimpf.org> | 2012-06-12 21:09:10 +0000 |
---|---|---|
committer | Markus Grüneis <gimpf@gimpf.org> | 2012-06-12 21:09:10 +0000 |
commit | 7d07a73afcc15e102be3144013127ba28ee716d8 (patch) | |
tree | 7b00c2dcc37ae3e4540fbcf660435bae60d1dfa9 /tools/idm-ringbuffer/ringbuffer.c | |
parent | INT Pin now follows UNDERRUN (diff) |
added idm-ringbuffer/ringbuffer.c, which contains test code to analyze IDM ringbuffer algorithm
git-svn-id: https://svn.spreadspace.org/mur.sat@519 7de4ea59-55d0-425e-a1af-a3118ea81d4c
Diffstat (limited to 'tools/idm-ringbuffer/ringbuffer.c')
-rw-r--r-- | tools/idm-ringbuffer/ringbuffer.c | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/tools/idm-ringbuffer/ringbuffer.c b/tools/idm-ringbuffer/ringbuffer.c new file mode 100644 index 0000000..ea912b3 --- /dev/null +++ b/tools/idm-ringbuffer/ringbuffer.c @@ -0,0 +1,143 @@ +#include <stdio.h> +#include <unistd.h> +#include <errno.h> +#include <stdlib.h> +#include <termios.h> + +#define BUFFER_START 0x2000u +//#define BUFFER_SIZE 960u +#define BUFFER_SIZE 10 +#define BUFFER_END (BUFFER_START + BUFFER_SIZE) +#define RAM_SIZE (BUFFER_END + 1) + +#define BOOL int +#define FALSE 0 +#define TRUE 1 +BOOL overrun = FALSE; +BOOL overrunForReader = FALSE; +BOOL underrun = TRUE; +char picRam[RAM_SIZE] = { 0 }; +char* bufferStart = &(picRam[BUFFER_START]); +char* bufferEnd = &(picRam[BUFFER_END - 1]); +char* writePtr = 0; +char* readPtr = 0; +BOOL currentByteLoaded = FALSE; +char spiData = 0; + +void debugPrint() { + printf("wPtr: %d, rPtr: %d, cbl: %d, ovr: %d, udr: %d\n", + (int) (writePtr - bufferStart), + (int) (readPtr - bufferStart), + (int) currentByteLoaded, + (int) overrun, + (int) underrun); + int i; + for(i = 0; i < BUFFER_SIZE; ++i) { + printf("%02x ", bufferStart[i]); + } + printf("\n"); +} + +int getch(void) +{ + int ch; + struct termios oldt; + struct termios newt; + tcgetattr(STDIN_FILENO, &oldt); /*store old settings */ + newt = oldt; /* copy old settings to new settings */ + newt.c_lflag &= ~(ICANON | ECHO); /* make one change to old settings in new settings */ + tcsetattr(STDIN_FILENO, TCSANOW, &newt); /*apply the new settings immediatly */ + ch = getchar(); /* standard getchar call */ + tcsetattr(STDIN_FILENO, TCSANOW, &oldt); /*reapply the old settings */ + return ch; /*return received char */ +} + +void incrementWriteIdx() { + if (!underrun && (writePtr == readPtr)) { + overrun = TRUE; + overrunForReader = TRUE; + } else { + if (!overrun) { + writePtr++; + writePtr = writePtr > bufferEnd ? bufferStart : writePtr; + } + overrun = FALSE; + overrunForReader = FALSE; + underrun = FALSE; + } +} + +void incrementReadIdx() { + if (!overrunForReader && (readPtr == writePtr)) { + underrun = TRUE; + } else { + readPtr++; + readPtr = readPtr > bufferEnd ? bufferStart : readPtr; + underrun = FALSE; + overrunForReader = FALSE; + // overrun = FALSE; + } +} + +void beginRead() { + if (!currentByteLoaded) { + incrementReadIdx(); + } + if (!underrun) { + spiData = *readPtr; + printf("SPI out: %02x\n", (int)spiData); + } + currentByteLoaded = TRUE; +} + +void endRead() { + currentByteLoaded = FALSE; +} + +void storeSample() { + static char value = 0; + value++; + incrementWriteIdx(); + if (!overrun) { + *writePtr = value; + } +} + +void triggerByteTransferred() { + if (spiData == 0) { + endRead(); + beginRead(); + } else if (spiData == 'r') { + beginRead(); + } else { + printf("%s", "I don't like you!"); + } +} + +int main() +{ + writePtr = bufferStart; + readPtr = bufferStart; + + char key; + for(;;) { + /*ssize_t len = read(0, &key, 1); + if (len < 0 && (errno == EINTR || errno == EAGAIN)) { + continue; + } else if (len == 0) { + return 0; + } else if (len != 1) { abort(); }*/ + + debugPrint(); + key = getch(); + + switch(key) { + case '+': storeSample(); break; + case '0': spiData = 0; triggerByteTransferred(); break; + case 'q': return 0; + default: spiData = key; triggerByteTransferred(); break; + } + } + + return 0; +} |