aboutsummaryrefslogtreecommitdiff
path: root/quantum/serial_link/protocol/transport.c
diff options
context:
space:
mode:
Diffstat (limited to 'quantum/serial_link/protocol/transport.c')
-rw-r--r--quantum/serial_link/protocol/transport.c121
1 files changed, 0 insertions, 121 deletions
diff --git a/quantum/serial_link/protocol/transport.c b/quantum/serial_link/protocol/transport.c
deleted file mode 100644
index 73b8dc62e..000000000
--- a/quantum/serial_link/protocol/transport.c
+++ /dev/null
@@ -1,121 +0,0 @@
1/*
2The MIT License (MIT)
3
4Copyright (c) 2016 Fred Sundvik
5
6Permission is hereby granted, free of charge, to any person obtaining a copy
7of this software and associated documentation files (the "Software"), to deal
8in the Software without restriction, including without limitation the rights
9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10copies of the Software, and to permit persons to whom the Software is
11furnished to do so, subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22SOFTWARE.
23*/
24
25#include "serial_link/protocol/transport.h"
26#include "serial_link/protocol/frame_router.h"
27#include "serial_link/protocol/triple_buffered_object.h"
28#include <string.h>
29
30#define MAX_REMOTE_OBJECTS 16
31static remote_object_t* remote_objects[MAX_REMOTE_OBJECTS];
32static uint32_t num_remote_objects = 0;
33
34void reinitialize_serial_link_transport(void) { num_remote_objects = 0; }
35
36void add_remote_objects(remote_object_t** _remote_objects, uint32_t _num_remote_objects) {
37 unsigned int i;
38 for (i = 0; i < _num_remote_objects; i++) {
39 remote_object_t* obj = _remote_objects[i];
40 remote_objects[num_remote_objects++] = obj;
41 if (obj->object_type == MASTER_TO_ALL_SLAVES) {
42 triple_buffer_object_t* tb = (triple_buffer_object_t*)obj->buffer;
43 triple_buffer_init(tb);
44 uint8_t* start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size);
45 tb = (triple_buffer_object_t*)start;
46 triple_buffer_init(tb);
47 } else if (obj->object_type == MASTER_TO_SINGLE_SLAVE) {
48 uint8_t* start = obj->buffer;
49 unsigned int j;
50 for (j = 0; j < NUM_SLAVES; j++) {
51 triple_buffer_object_t* tb = (triple_buffer_object_t*)start;
52 triple_buffer_init(tb);
53 start += LOCAL_OBJECT_SIZE(obj->object_size);
54 }
55 triple_buffer_object_t* tb = (triple_buffer_object_t*)start;
56 triple_buffer_init(tb);
57 } else {
58 uint8_t* start = obj->buffer;
59 triple_buffer_object_t* tb = (triple_buffer_object_t*)start;
60 triple_buffer_init(tb);
61 start += LOCAL_OBJECT_SIZE(obj->object_size);
62 unsigned int j;
63 for (j = 0; j < NUM_SLAVES; j++) {
64 tb = (triple_buffer_object_t*)start;
65 triple_buffer_init(tb);
66 start += REMOTE_OBJECT_SIZE(obj->object_size);
67 }
68 }
69 }
70}
71
72void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size) {
73 uint8_t id = data[size - 1];
74 if (id < num_remote_objects) {
75 remote_object_t* obj = remote_objects[id];
76 if (obj->object_size == size - 1) {
77 uint8_t* start;
78 if (obj->object_type == MASTER_TO_ALL_SLAVES) {
79 start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size);
80 } else if (obj->object_type == SLAVE_TO_MASTER) {
81 start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size);
82 start += (from - 1) * REMOTE_OBJECT_SIZE(obj->object_size);
83 } else {
84 start = obj->buffer + NUM_SLAVES * LOCAL_OBJECT_SIZE(obj->object_size);
85 }
86 triple_buffer_object_t* tb = (triple_buffer_object_t*)start;
87 void* ptr = triple_buffer_begin_write_internal(obj->object_size, tb);
88 memcpy(ptr, data, size - 1);
89 triple_buffer_end_write_internal(tb);
90 }
91 }
92}
93
94void update_transport(void) {
95 unsigned int i;
96 for (i = 0; i < num_remote_objects; i++) {
97 remote_object_t* obj = remote_objects[i];
98 if (obj->object_type == MASTER_TO_ALL_SLAVES || obj->object_type == SLAVE_TO_MASTER) {
99 triple_buffer_object_t* tb = (triple_buffer_object_t*)obj->buffer;
100 uint8_t* ptr = (uint8_t*)triple_buffer_read_internal(obj->object_size + LOCAL_OBJECT_EXTRA, tb);
101 if (ptr) {
102 ptr[obj->object_size] = i;
103 uint8_t dest = obj->object_type == MASTER_TO_ALL_SLAVES ? 0xFF : 0;
104 router_send_frame(dest, ptr, obj->object_size + 1);
105 }
106 } else {
107 uint8_t* start = obj->buffer;
108 unsigned int j;
109 for (j = 0; j < NUM_SLAVES; j++) {
110 triple_buffer_object_t* tb = (triple_buffer_object_t*)start;
111 uint8_t* ptr = (uint8_t*)triple_buffer_read_internal(obj->object_size + LOCAL_OBJECT_EXTRA, tb);
112 if (ptr) {
113 ptr[obj->object_size] = i;
114 uint8_t dest = j + 1;
115 router_send_frame(dest, ptr, obj->object_size + 1);
116 }
117 start += LOCAL_OBJECT_SIZE(obj->object_size);
118 }
119 }
120 }
121}