diff options
Diffstat (limited to 'quantum/serial_link/protocol/triple_buffered_object.c')
-rw-r--r-- | quantum/serial_link/protocol/triple_buffered_object.c | 77 |
1 files changed, 0 insertions, 77 deletions
diff --git a/quantum/serial_link/protocol/triple_buffered_object.c b/quantum/serial_link/protocol/triple_buffered_object.c deleted file mode 100644 index e0c6d702a..000000000 --- a/quantum/serial_link/protocol/triple_buffered_object.c +++ /dev/null | |||
@@ -1,77 +0,0 @@ | |||
1 | /* | ||
2 | The MIT License (MIT) | ||
3 | |||
4 | Copyright (c) 2016 Fred Sundvik | ||
5 | |||
6 | Permission is hereby granted, free of charge, to any person obtaining a copy | ||
7 | of this software and associated documentation files (the "Software"), to deal | ||
8 | in the Software without restriction, including without limitation the rights | ||
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
10 | copies of the Software, and to permit persons to whom the Software is | ||
11 | furnished to do so, subject to the following conditions: | ||
12 | |||
13 | The above copyright notice and this permission notice shall be included in all | ||
14 | copies or substantial portions of the Software. | ||
15 | |||
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
22 | SOFTWARE. | ||
23 | */ | ||
24 | |||
25 | #include "serial_link/protocol/triple_buffered_object.h" | ||
26 | #include "serial_link/system/serial_link.h" | ||
27 | #include <stdbool.h> | ||
28 | #include <stddef.h> | ||
29 | |||
30 | #define GET_READ_INDEX() object->state & 3 | ||
31 | #define GET_WRITE_INDEX() (object->state >> 2) & 3 | ||
32 | #define GET_SHARED_INDEX() (object->state >> 4) & 3 | ||
33 | #define GET_DATA_AVAILABLE() (object->state >> 6) & 1 | ||
34 | |||
35 | #define SET_READ_INDEX(i) object->state = ((object->state & ~3) | i) | ||
36 | #define SET_WRITE_INDEX(i) object->state = ((object->state & ~(3 << 2)) | (i << 2)) | ||
37 | #define SET_SHARED_INDEX(i) object->state = ((object->state & ~(3 << 4)) | (i << 4)) | ||
38 | #define SET_DATA_AVAILABLE(i) object->state = ((object->state & ~(1 << 6)) | (i << 6)) | ||
39 | |||
40 | void triple_buffer_init(triple_buffer_object_t* object) { | ||
41 | object->state = 0; | ||
42 | SET_WRITE_INDEX(0); | ||
43 | SET_READ_INDEX(1); | ||
44 | SET_SHARED_INDEX(2); | ||
45 | SET_DATA_AVAILABLE(0); | ||
46 | } | ||
47 | |||
48 | void* triple_buffer_read_internal(uint16_t object_size, triple_buffer_object_t* object) { | ||
49 | serial_link_lock(); | ||
50 | if (GET_DATA_AVAILABLE()) { | ||
51 | uint8_t shared_index = GET_SHARED_INDEX(); | ||
52 | uint8_t read_index = GET_READ_INDEX(); | ||
53 | SET_READ_INDEX(shared_index); | ||
54 | SET_SHARED_INDEX(read_index); | ||
55 | SET_DATA_AVAILABLE(false); | ||
56 | serial_link_unlock(); | ||
57 | return object->buffer + object_size * shared_index; | ||
58 | } else { | ||
59 | serial_link_unlock(); | ||
60 | return NULL; | ||
61 | } | ||
62 | } | ||
63 | |||
64 | void* triple_buffer_begin_write_internal(uint16_t object_size, triple_buffer_object_t* object) { | ||
65 | uint8_t write_index = GET_WRITE_INDEX(); | ||
66 | return object->buffer + object_size * write_index; | ||
67 | } | ||
68 | |||
69 | void triple_buffer_end_write_internal(triple_buffer_object_t* object) { | ||
70 | serial_link_lock(); | ||
71 | uint8_t shared_index = GET_SHARED_INDEX(); | ||
72 | uint8_t write_index = GET_WRITE_INDEX(); | ||
73 | SET_SHARED_INDEX(write_index); | ||
74 | SET_WRITE_INDEX(shared_index); | ||
75 | SET_DATA_AVAILABLE(true); | ||
76 | serial_link_unlock(); | ||
77 | } | ||