diff --git a/serial_link/protocol/triple_buffered_object.c b/serial_link/protocol/triple_buffered_object.c index 77e45ec41..3fabb7d84 100644 --- a/serial_link/protocol/triple_buffered_object.c +++ b/serial_link/protocol/triple_buffered_object.c @@ -23,6 +23,7 @@ SOFTWARE. */ #include "protocol/triple_buffered_object.h" +#include "system/system.h" #define GET_READ_INDEX() object->state & 3 #define GET_WRITE_INDEX() (object->state >> 2) & 3 @@ -42,11 +43,21 @@ void triple_buffer_init(triple_buffer_object_t* object) { SET_DATA_AVAILABLE(0); } -static void triple_buffer_begin_read(uint16_t object_size, triple_buffer_object_t* object) { - uint8_t shared_index = GET_SHARED_INDEX(); - uint8_t read_index = GET_READ_INDEX(); - SET_READ_INDEX(shared_index); - SET_SHARED_INDEX(read_index); +static bool triple_buffer_begin_read(uint16_t object_size, triple_buffer_object_t* object) { + serial_link_lock(); + if (GET_DATA_AVAILABLE()) { + uint8_t shared_index = GET_SHARED_INDEX(); + uint8_t read_index = GET_READ_INDEX(); + SET_READ_INDEX(shared_index); + SET_SHARED_INDEX(read_index); + SET_DATA_AVAILABLE(false); + serial_link_unlock(); + return true; + } + else { + serial_link_unlock(); + return false; + } } static void triple_buffer_actual_read(uint16_t object_size, triple_buffer_object_t* object, void* dst) { @@ -61,13 +72,21 @@ void triple_buffer_write(uint16_t object_size, triple_buffer_object_t* object, v uint8_t write_index = GET_WRITE_INDEX(); memcpy(object->buffer + object_size * write_index, src, object_size); + serial_link_lock(); uint8_t shared_index = GET_SHARED_INDEX(); SET_SHARED_INDEX(write_index); SET_WRITE_INDEX(shared_index); + SET_DATA_AVAILABLE(true); + serial_link_unlock(); } -void triple_buffer_read(uint16_t object_size, triple_buffer_object_t* object, void* dst) { - triple_buffer_begin_read(object_size, object); - triple_buffer_actual_read(object_size, object, dst); - triple_buffer_end_read(object_size, object); +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 ebdbcae79..705f0c49f 100644 --- a/serial_link/protocol/triple_buffered_object.h +++ b/serial_link/protocol/triple_buffered_object.h @@ -30,8 +30,9 @@ typedef struct { uint8_t buffer[]; }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); -void triple_buffer_read(uint16_t object_size, triple_buffer_object_t* object, void* dst); +bool triple_buffer_read(uint16_t object_size, triple_buffer_object_t* object, void* dst); #endif diff --git a/serial_link/system/system.h b/serial_link/system/system.h new file mode 100644 index 000000000..c798e6477 --- /dev/null +++ b/serial_link/system/system.h @@ -0,0 +1,36 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#ifndef SERIAL_LINK_SYSTEM_H +#define SERIAL_LINK_SYSTEM_H + +void serial_link_lock() { + +} + +void serial_link_unlock() { + +} + +#endif diff --git a/serial_link/tests/triple_buffered_object_tests.c b/serial_link/tests/triple_buffered_object_tests.c index 5fa1b8b62..cd3ecb6a2 100644 --- a/serial_link/tests/triple_buffered_object_tests.c +++ b/serial_link/tests/triple_buffered_object_tests.c @@ -47,6 +47,12 @@ Ensure(TripleBufferedObject, writes_and_reads_object) { assert_that(dst, is_equal_to(src)); } +Ensure(TripleBufferedObject, does_not_read_empty) { + uint32_t dst; + bool res = triple_buffer_read(4, (triple_buffer_object_t*)&test_object, &dst); + assert_that(res, is_equal_to(false)); +} + Ensure(TripleBufferedObject, writes_and_reads_object_decomposed) { uint32_t src = 0x3456ABCC; uint32_t dst; @@ -79,6 +85,7 @@ Ensure(TripleBufferedObject, performs_another_write_in_the_middle_of_read) { assert_that(dst, is_equal_to(1)); triple_buffer_read(4, (triple_buffer_object_t*)&test_object, &dst); assert_that(dst, is_equal_to(2)); + assert_that(triple_buffer_read(4, (triple_buffer_object_t*)&test_object, &dst), is_equal_to(false)); } Ensure(TripleBufferedObject, performs_two_writes_in_the_middle_of_read) { @@ -95,4 +102,5 @@ Ensure(TripleBufferedObject, performs_two_writes_in_the_middle_of_read) { assert_that(dst, is_equal_to(1)); triple_buffer_read(4, (triple_buffer_object_t*)&test_object, &dst); assert_that(dst, is_equal_to(3)); + assert_that(triple_buffer_read(4, (triple_buffer_object_t*)&test_object, &dst), is_equal_to(false)); }