summaryrefslogtreecommitdiff
path: root/serial_link/protocol
diff options
context:
space:
mode:
Diffstat (limited to 'serial_link/protocol')
-rw-r--r--serial_link/protocol/triple_buffered_object.c32
-rw-r--r--serial_link/protocol/triple_buffered_object.h15
2 files changed, 21 insertions, 26 deletions
diff --git a/serial_link/protocol/triple_buffered_object.c b/serial_link/protocol/triple_buffered_object.c
index 3fabb7d84b..6b3cf75adf 100644
--- a/serial_link/protocol/triple_buffered_object.c
+++ b/serial_link/protocol/triple_buffered_object.c
@@ -43,7 +43,7 @@ void triple_buffer_init(triple_buffer_object_t* object) {
SET_DATA_AVAILABLE(0);
}
-static bool triple_buffer_begin_read(uint16_t object_size, triple_buffer_object_t* object) {
+void* triple_buffer_read_internal(uint16_t object_size, triple_buffer_object_t* object) {
serial_link_lock();
if (GET_DATA_AVAILABLE()) {
uint8_t shared_index = GET_SHARED_INDEX();
@@ -52,41 +52,25 @@ static bool triple_buffer_begin_read(uint16_t object_size, triple_buffer_object_
SET_SHARED_INDEX(read_index);
SET_DATA_AVAILABLE(false);
serial_link_unlock();
- return true;
+ return object->buffer + object_size * shared_index;
}
else {
serial_link_unlock();
- return false;
+ return NULL;
}
}
-static void triple_buffer_actual_read(uint16_t object_size, triple_buffer_object_t* object, void* dst) {
- uint8_t read_index = GET_READ_INDEX();
- memcpy(dst, object->buffer + object_size*read_index, object_size);
-}
-
-static void triple_buffer_end_read(uint16_t object_size, triple_buffer_object_t* object) {
+void* triple_buffer_begin_write_internal(uint16_t object_size, triple_buffer_object_t* object) {
+ uint8_t write_index = GET_WRITE_INDEX();
+ return object->buffer + object_size * write_index;
}
-void triple_buffer_write(uint16_t object_size, triple_buffer_object_t* object, void* src) {
-
- uint8_t write_index = GET_WRITE_INDEX();
- memcpy(object->buffer + object_size * write_index, src, object_size);
+void triple_buffer_end_write_internal(triple_buffer_object_t* object) {
serial_link_lock();
uint8_t shared_index = GET_SHARED_INDEX();
+ uint8_t write_index = GET_WRITE_INDEX();
SET_SHARED_INDEX(write_index);
SET_WRITE_INDEX(shared_index);
SET_DATA_AVAILABLE(true);
serial_link_unlock();
}
-
-bool triple_buffer_read(uint16_t object_size, triple_buffer_object_t* object, void* dst) {
- if (triple_buffer_begin_read(object_size, object)) {
- triple_buffer_actual_read(object_size, object, dst);
- triple_buffer_end_read(object_size, object);
- return true;
- }
- else {
- return false;
- }
-}
diff --git a/serial_link/protocol/triple_buffered_object.h b/serial_link/protocol/triple_buffered_object.h
index d224f36a2e..03209709cb 100644
--- a/serial_link/protocol/triple_buffered_object.h
+++ b/serial_link/protocol/triple_buffered_object.h
@@ -31,8 +31,19 @@ typedef struct {
}triple_buffer_object_t;
void triple_buffer_init(triple_buffer_object_t* object);
-void triple_buffer_write(uint16_t object_size, triple_buffer_object_t* object, void* src);
-bool triple_buffer_read(uint16_t object_size, triple_buffer_object_t* object, void* dst);
+
+#define triple_buffer_begin_write(object) \
+ (typeof(*object.buffer[0])*)triple_buffer_begin_write_internal(sizeof(*object.buffer[0]), (triple_buffer_object_t*)object)
+
+#define triple_buffer_end_write(object) \
+ triple_buffer_end_write_internal((triple_buffer_object_t*)object)
+
+#define triple_buffer_read(object) \
+ (typeof(*object.buffer[0])*)triple_buffer_read_internal(sizeof(*object.buffer[0]), (triple_buffer_object_t*)object)
+
+void* triple_buffer_begin_write_internal(uint16_t object_size, triple_buffer_object_t* object);
+void triple_buffer_end_write_internal(triple_buffer_object_t* object);
+void* triple_buffer_read_internal(uint16_t object_size, triple_buffer_object_t* object);
#endif