]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/serial_link/protocol/transport.h
Merge branch 'master' of https://github.com/jackhumbert/qmk_firmware
[qmk_firmware.git] / quantum / serial_link / protocol / transport.h
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 #ifndef SERIAL_LINK_TRANSPORT_H
26 #define SERIAL_LINK_TRANSPORT_H
27
28 #include "serial_link/protocol/triple_buffered_object.h"
29 #include "serial_link/system/serial_link.h"
30
31 #define NUM_SLAVES 8
32 #define LOCAL_OBJECT_EXTRA 16
33
34 // master -> slave = 1 local(target all), 1 remote object
35 // slave -> master = 1 local(target 0), multiple remote objects
36 // master -> single slave (multiple local, target id), 1 remote object
37 typedef enum {
38     MASTER_TO_ALL_SLAVES,
39     MASTER_TO_SINGLE_SLAVE,
40     SLAVE_TO_MASTER,
41 } remote_object_type;
42
43 typedef struct {
44     remote_object_type object_type;
45     uint16_t object_size;
46     uint8_t buffer[] __attribute__((aligned(4)));
47 } remote_object_t;
48
49 #define REMOTE_OBJECT_SIZE(objectsize) \
50     (sizeof(triple_buffer_object_t) + objectsize * 3)
51 #define LOCAL_OBJECT_SIZE(objectsize) \
52     (sizeof(triple_buffer_object_t) + (objectsize + LOCAL_OBJECT_EXTRA) * 3)
53
54 #define REMOTE_OBJECT_HELPER(name, type, num_local, num_remote) \
55 typedef struct { \
56     remote_object_t object; \
57     uint8_t buffer[ \
58         num_remote * REMOTE_OBJECT_SIZE(sizeof(type)) + \
59         num_local * LOCAL_OBJECT_SIZE(sizeof(type))]; \
60 } remote_object_##name##_t;
61
62 #define MASTER_TO_ALL_SLAVES_OBJECT(name, type) \
63     REMOTE_OBJECT_HELPER(name, type, 1, 1) \
64     remote_object_##name##_t remote_object_##name = { \
65         .object = { \
66             .object_type = MASTER_TO_ALL_SLAVES, \
67             .object_size = sizeof(type), \
68         } \
69     }; \
70     type* begin_write_##name(void) { \
71         remote_object_t* obj = (remote_object_t*)&remote_object_##name; \
72         triple_buffer_object_t* tb = (triple_buffer_object_t*)obj->buffer; \
73         return (type*)triple_buffer_begin_write_internal(sizeof(type) + LOCAL_OBJECT_EXTRA, tb); \
74     }\
75     void end_write_##name(void) { \
76         remote_object_t* obj = (remote_object_t*)&remote_object_##name; \
77         triple_buffer_object_t* tb = (triple_buffer_object_t*)obj->buffer; \
78         triple_buffer_end_write_internal(tb); \
79         signal_data_written(); \
80     }\
81     type* read_##name(void) { \
82         remote_object_t* obj = (remote_object_t*)&remote_object_##name; \
83         uint8_t* start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size);\
84         triple_buffer_object_t* tb = (triple_buffer_object_t*)start; \
85         return triple_buffer_read_internal(obj->object_size, tb); \
86     }
87
88 #define MASTER_TO_SINGLE_SLAVE_OBJECT(name, type) \
89     REMOTE_OBJECT_HELPER(name, type, NUM_SLAVES, 1) \
90     remote_object_##name##_t remote_object_##name = { \
91         .object = { \
92             .object_type = MASTER_TO_SINGLE_SLAVE, \
93             .object_size = sizeof(type), \
94         } \
95     }; \
96     type* begin_write_##name(uint8_t slave) { \
97         remote_object_t* obj = (remote_object_t*)&remote_object_##name; \
98         uint8_t* start = obj->buffer;\
99         start += slave * LOCAL_OBJECT_SIZE(obj->object_size); \
100         triple_buffer_object_t* tb = (triple_buffer_object_t*)start; \
101         return (type*)triple_buffer_begin_write_internal(sizeof(type) + LOCAL_OBJECT_EXTRA, tb); \
102     }\
103     void end_write_##name(uint8_t slave) { \
104         remote_object_t* obj = (remote_object_t*)&remote_object_##name; \
105         uint8_t* start = obj->buffer;\
106         start += slave * LOCAL_OBJECT_SIZE(obj->object_size); \
107         triple_buffer_object_t* tb = (triple_buffer_object_t*)start; \
108         triple_buffer_end_write_internal(tb); \
109         signal_data_written(); \
110     }\
111     type* read_##name() { \
112         remote_object_t* obj = (remote_object_t*)&remote_object_##name; \
113         uint8_t* start = obj->buffer + NUM_SLAVES * LOCAL_OBJECT_SIZE(obj->object_size);\
114         triple_buffer_object_t* tb = (triple_buffer_object_t*)start; \
115         return triple_buffer_read_internal(obj->object_size, tb); \
116     }
117
118 #define SLAVE_TO_MASTER_OBJECT(name, type) \
119     REMOTE_OBJECT_HELPER(name, type, 1, NUM_SLAVES) \
120     remote_object_##name##_t remote_object_##name = { \
121         .object = { \
122             .object_type = SLAVE_TO_MASTER, \
123             .object_size = sizeof(type), \
124         } \
125     }; \
126     type* begin_write_##name(void) { \
127         remote_object_t* obj = (remote_object_t*)&remote_object_##name; \
128         triple_buffer_object_t* tb = (triple_buffer_object_t*)obj->buffer; \
129         return (type*)triple_buffer_begin_write_internal(sizeof(type) + LOCAL_OBJECT_EXTRA, tb); \
130     }\
131     void end_write_##name(void) { \
132         remote_object_t* obj = (remote_object_t*)&remote_object_##name; \
133         triple_buffer_object_t* tb = (triple_buffer_object_t*)obj->buffer; \
134         triple_buffer_end_write_internal(tb); \
135         signal_data_written(); \
136     }\
137     type* read_##name(uint8_t slave) { \
138         remote_object_t* obj = (remote_object_t*)&remote_object_##name; \
139         uint8_t* start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size);\
140         start+=slave * REMOTE_OBJECT_SIZE(obj->object_size); \
141         triple_buffer_object_t* tb = (triple_buffer_object_t*)start; \
142         return triple_buffer_read_internal(obj->object_size, tb); \
143     }
144
145 #define REMOTE_OBJECT(name) (remote_object_t*)&remote_object_##name
146
147 void add_remote_objects(remote_object_t** remote_objects, uint32_t num_remote_objects);
148 void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size);
149 void update_transport(void);
150
151 #endif