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.c128
1 files changed, 128 insertions, 0 deletions
diff --git a/quantum/serial_link/protocol/transport.c b/quantum/serial_link/protocol/transport.c
new file mode 100644
index 000000000..ff795fe20
--- /dev/null
+++ b/quantum/serial_link/protocol/transport.c
@@ -0,0 +1,128 @@
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) {
35 num_remote_objects = 0;
36}
37
38void add_remote_objects(remote_object_t** _remote_objects, uint32_t _num_remote_objects) {
39 unsigned int i;
40 for(i=0;i<_num_remote_objects;i++) {
41 remote_object_t* obj = _remote_objects[i];
42 remote_objects[num_remote_objects++] = obj;
43 if (obj->object_type == MASTER_TO_ALL_SLAVES) {
44 triple_buffer_object_t* tb = (triple_buffer_object_t*)obj->buffer;
45 triple_buffer_init(tb);
46 uint8_t* start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size);
47 tb = (triple_buffer_object_t*)start;
48 triple_buffer_init(tb);
49 }
50 else if(obj->object_type == MASTER_TO_SINGLE_SLAVE) {
51 uint8_t* start = obj->buffer;
52 unsigned int j;
53 for (j=0;j<NUM_SLAVES;j++) {
54 triple_buffer_object_t* tb = (triple_buffer_object_t*)start;
55 triple_buffer_init(tb);
56 start += LOCAL_OBJECT_SIZE(obj->object_size);
57 }
58 triple_buffer_object_t* tb = (triple_buffer_object_t*)start;
59 triple_buffer_init(tb);
60 }
61 else {
62 uint8_t* start = obj->buffer;
63 triple_buffer_object_t* tb = (triple_buffer_object_t*)start;
64 triple_buffer_init(tb);
65 start += LOCAL_OBJECT_SIZE(obj->object_size);
66 unsigned int j;
67 for (j=0;j<NUM_SLAVES;j++) {
68 tb = (triple_buffer_object_t*)start;
69 triple_buffer_init(tb);
70 start += REMOTE_OBJECT_SIZE(obj->object_size);
71 }
72 }
73 }
74}
75
76void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size) {
77 uint8_t id = data[size-1];
78 if (id < num_remote_objects) {
79 remote_object_t* obj = remote_objects[id];
80 if (obj->object_size == size - 1) {
81 uint8_t* start;
82 if (obj->object_type == MASTER_TO_ALL_SLAVES) {
83 start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size);
84 }
85 else if(obj->object_type == SLAVE_TO_MASTER) {
86 start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size);
87 start += (from - 1) * REMOTE_OBJECT_SIZE(obj->object_size);
88 }
89 else {
90 start = obj->buffer + NUM_SLAVES * LOCAL_OBJECT_SIZE(obj->object_size);
91 }
92 triple_buffer_object_t* tb = (triple_buffer_object_t*)start;
93 void* ptr = triple_buffer_begin_write_internal(obj->object_size, tb);
94 memcpy(ptr, data, size - 1);
95 triple_buffer_end_write_internal(tb);
96 }
97 }
98}
99
100void update_transport(void) {
101 unsigned int i;
102 for(i=0;i<num_remote_objects;i++) {
103 remote_object_t* obj = remote_objects[i];
104 if (obj->object_type == MASTER_TO_ALL_SLAVES || obj->object_type == SLAVE_TO_MASTER) {
105 triple_buffer_object_t* tb = (triple_buffer_object_t*)obj->buffer;
106 uint8_t* ptr = (uint8_t*)triple_buffer_read_internal(obj->object_size + LOCAL_OBJECT_EXTRA, tb);
107 if (ptr) {
108 ptr[obj->object_size] = i;
109 uint8_t dest = obj->object_type == MASTER_TO_ALL_SLAVES ? 0xFF : 0;
110 router_send_frame(dest, ptr, obj->object_size + 1);
111 }
112 }
113 else {
114 uint8_t* start = obj->buffer;
115 unsigned int j;
116 for (j=0;j<NUM_SLAVES;j++) {
117 triple_buffer_object_t* tb = (triple_buffer_object_t*)start;
118 uint8_t* ptr = (uint8_t*)triple_buffer_read_internal(obj->object_size + LOCAL_OBJECT_EXTRA, tb);
119 if (ptr) {
120 ptr[obj->object_size] = i;
121 uint8_t dest = j + 1;
122 router_send_frame(dest, ptr, obj->object_size + 1);
123 }
124 start += LOCAL_OBJECT_SIZE(obj->object_size);
125 }
126 }
127 }
128}