aboutsummaryrefslogtreecommitdiff
path: root/include/utils
diff options
context:
space:
mode:
Diffstat (limited to 'include/utils')
-rw-r--r--include/utils/buffer.h86
-rw-r--r--include/utils/narray.h21
-rw-r--r--include/utils/timers_div.h81
3 files changed, 188 insertions, 0 deletions
diff --git a/include/utils/buffer.h b/include/utils/buffer.h
new file mode 100644
index 0000000..66b68ad
--- /dev/null
+++ b/include/utils/buffer.h
@@ -0,0 +1,86 @@
+#ifndef _IOE_BUFFER_H_
+#define _IOE_BUFFER_H_
+
+// Define new buffer
+#define IOEBUFFER(type, name, size) struct { \
+ uint8_t rindex, windex; \
+ type data[size]; \
+ } name;
+
+#define IOEBUFFER_INIT(name, size) { \
+ name.windex = size - 1; \
+ name.rindex = size - 1; \
+ }
+
+#define IOEBUFFER_MODE_BLOCK 0
+#define IOEBUFFER_MODE_OVERWRITE 1
+#define IOEBUFFER_MODE_DROP 2
+// Add data to buffer. If buffer is full then behavior is defined by selected mode.
+#define IOEBUFFER_PUT(name, size, idata, mode) \
+ if (mode == IOEBUFFER_MODE_BLOCK) { \
+ if (name.windex == 0) { \
+ while (name.rindex == size - 1); \
+ } else { \
+ while (name.rindex == name.windex - 1); \
+ } \
+ } \
+ name.data[name.windex] = idata; \
+ if (mode != IOEBUFFER_MODE_DROP || \
+ (name.windex == 0 && name.rindex == size - 1) || \
+ (name.rindex + 1 == name.windex)) { \
+ if (name.windex == 0) \
+ name.windex = size - 1; \
+ else \
+ name.windex--; \
+ } \
+ if (mode == IOEBUFFER_MODE_OVERWRITE && name.windex == name.rindex) { \
+ if (name.windex == size - 1) \
+ name.windex = 0; \
+ else \
+ name.windex++; \
+ } else;
+
+// Get data from buffer and store it to variable
+#define IOEBUFFER_GET(name, size, variable) \
+ if (name.rindex != name.windex) { \
+ variable = name.data[name.rindex]; \
+ if (name.rindex == 0) \
+ name.rindex = size - 1; \
+ else \
+ name.rindex--; \
+ } else { \
+ variable = 0; \
+ }
+
+// Set count of buffered data to variable
+#define IOEBUFFER_CNT(name, size, variable) \
+ if (name.windex < name.rindex) \
+ variable = name.rindex - name.windex; \
+ else if (name.windex > name.rindex) \
+ variable = size - name.windex + name.rindex; \
+ else \
+ variable = 0;
+
+/////////////////////////////////////////////////////////////////////
+
+#ifdef CONFIG_IOE_BUFFER
+#include <stdint.h>
+
+#define IOEBUFFER_F_MODE_BLOCK 0x0
+#define IOEBUFFER_F_MODE_OVERWRITE 0x1
+#define IOEBUFFER_F_MODE_DROP 0x2
+
+typedef struct {
+ uint8_t rindex, windex, size, flags;
+ void **data;
+} IOEBuffer;
+
+int8_t ioebuffer_init(IOEBuffer * buf, uint8_t size, uint8_t flags);
+void ioebuffer_uninit(IOEBuffer * buf);
+int8_t ioebuffer_put(IOEBuffer * buf, void *data);
+int8_t ioebuffer_get(IOEBuffer * buf, void **data);
+void ioebuffer_clean(IOEBuffer * buf);
+uint8_t ioebuffer_cnt(IOEBuffer * buf);
+
+#endif /* CONFIG_IOE_BUFFER */
+#endif /* _IOE_BUFFER_H_ */
diff --git a/include/utils/narray.h b/include/utils/narray.h
new file mode 100644
index 0000000..814d87d
--- /dev/null
+++ b/include/utils/narray.h
@@ -0,0 +1,21 @@
+#include <stdlib.h>
+#include <stdint.h>
+
+#ifndef _IOE_NARRAY_H_
+#define _IOE_NARRAY_H_
+#ifdef CONFIG_IOE_NARRAY
+
+void narray_add(void ***array, void *data);
+void narray_remove(void ***array, void *data);
+size_t narray_size(void ***array);
+inline void narray_free(void ***array) {
+ if (array != NULL) {
+ free(*array);
+ *array = NULL;
+ }
+}
+
+#define fornarray(array, i, data) for (i = 0; (data = array[i]) != 0; i++)
+
+#endif /* CONFIG_IOE_NARRAY */
+#endif /* _IOE_NARRAY_H_ */
diff --git a/include/utils/timers_div.h b/include/utils/timers_div.h
new file mode 100644
index 0000000..9077aac
--- /dev/null
+++ b/include/utils/timers_div.h
@@ -0,0 +1,81 @@
+//#include "../timer.h"
+enum timerDivider {
+ TIMER_DIVIDER_1,
+ TIMER_DIVIDER_8,
+ TIMER_DIVIDER_64,
+ TIMER_DIVIDER_256,
+ TIMER_DIVIDER_1024
+};
+
+#ifndef TIMER_DIV_RESTIME
+#error Please define minimal time (us) before include timers_div.h in TIMER_DIV_RESTIME
+#endif
+
+// Calculate optimal division
+#define _TD_OPTIMAL (TIMER_DIV_RESTIME * F_CPU/1000000L)
+
+// Set minimal and maximal division closest to optimal
+#if _TD_OPTIMAL < 8
+#define _TD_MIN 1
+#define _TD_MAX 8
+#elif _TD_OPTIMAL < 64
+#define _TD_MIN 8
+#define _TD_MAX 64
+#elif _TD_OPTIMAL < 256
+#define _TD_MIN 64
+#define _TD_MAX 256
+#elif _TD_OPTIMAL < 1024
+#define _TD_MIN 256
+#define _TD_MAX 1024
+#else
+#define _TD_MIN 1024
+#define _TD_MAX 1024
+#endif
+
+// Calculate time difference between required time and got time resolution
+#define _TD_MIN_TIME (TIMER_DIV_RESTIME - (_TD_MIN * 1000000L / F_CPU))
+#define _TD_MAX_TIME (TIMER_DIV_RESTIME - (_TD_MAX * 1000000L / F_CPU))
+
+// Do absolute value of calculated numbers
+#if _TD_MIN_TIME < 0
+#define _TD_MIN_TIMEF (_TD_MIN_TIME * -1)
+#else
+#define _TD_MIN_TIMEF _TD_MIN_TIME
+#endif
+#if _TD_MAX_TIME < 0
+#define _TD_MAX_TIMEF (_TD_MAX_TIME * -1)
+#else
+#define _TD_MAX_TIMEF _TD_MAX_TIME
+#endif
+
+// Select closest one
+#if _TD_MIN_TIMEF < _TD_MAX_TIMEF
+#define _TD_TIMER_DIV _TD_MIN
+#else
+#define _TD_TIMER_DIV _TD_MAX
+#endif
+
+// Set macro to enum value
+#if _TD_TIMER_DIV == 1
+#define TIMER_DIV TIMER_DIVIDER_1
+#elif _TD_TIMER_DIV == 8
+#define TIMER_DIV TIMER_DIVIDER_8
+#elif _TD_TIMER_DIV == 64
+#define TIMER_DIV TIMER_DIVIDER_64
+#elif _TD_TIMER_DIV == 256
+#define TIMER_DIV TIMER_DIVIDER_256
+#elif _TD_TIMER_DIV == 1024
+#define TIMER_DIV TIMER_DIVIDER_1024
+#else
+#error Generated unknown timer division. Something is wrong with timers_div.h
+#endif
+
+// Undefine all used variables
+#undef _TD_OPTIMAL
+#undef _TD_MIN
+#undef _TD_MAX
+#undef _TD_MIN_TIME
+#undef _TD_MAX_TIME
+#undef _TD_MIN_TIMEF
+#undef _TD_MAX_TIMEF
+#undef _TD_TIMER_DIV