forked from gioblu/PJON
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PJONSlave.h
388 lines (324 loc) · 11.5 KB
/
PJONSlave.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*-O//\ __ __
|-gfo\ |__| | | | |\ | ®
|!y°o:\ | __| |__| | \| v10.1
|y"s§+`\ multi-master, multi-media bus network protocol
/so+:-..`\ Copyright 2010-2018 by Giovanni Blu Mitolo [email protected]
|+/:ngr-*.`\
|5/:%&-a3f.:;\
\+//u/+g%{osv,,\
\=+&/osw+olds.\\
\:/+-.-°-:+oss\
| | \oy\\
> <
______-| |-__________________________________________________________________
PJONSlave has been created by Giovanni Blu Mitolo with the support
of Fred Larsen and is inspired by the work of Thomas Snaidero:
"Modular components for eye tracking, in the interest of helping persons with
severely impaired motor skills."
Master Thesis, IT University of Copenhagen, Denmark, September 2016
PJON® Dynamic addressing specification:
- v1.0 specification/PJON-dynamic-addressing-specification-v1.0.md
If you believe in this project and you appreciate our work, please, make a
donation. The PJON Foundation is entirely financed by contributions of wise
people like you and its resources are solely invested to cover the development
and maintainance costs.
- Paypal: https://www.paypal.me/PJON
- Bitcoin: 1FupxAyDTuAMGz33PtwnhwBm4ppc7VLwpD
- Ethereum: 0xf34AEAF3B149454522019781668F9a2d1762559b
Thank you and happy tinkering!
_____________________________________________________________________________
Copyright 2010-2018 by Giovanni Blu Mitolo [email protected]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#pragma once
#include <PJON.h>
template<typename Strategy = SoftwareBitBang>
class PJONSlave : public PJON<Strategy> {
public:
uint8_t required_config =
PJON_PORT_BIT | PJON_TX_INFO_BIT | PJON_CRC_BIT;
/* PJONSlave bus default initialization:
State: Local (bus_id: 0.0.0.0)
Acknowledge: true (Acknowledge is requested)
device id: PJON_NOT_ASSIGNED (255)
Mode: PJON_HALF_DUPLEX
Sender info: true (Sender info are included in the packet)
Strategy: SoftwareBitBang */
PJONSlave() : PJON<Strategy>() {
set_default();
};
/* PJONSlave initialization passing device id:
PJONSlave bus(1); */
PJONSlave(uint8_t device_id) : PJON<Strategy>(device_id) {
set_default();
};
/* PJONSlave initialization passing bus and device id:
uint8_t my_bus = {1, 1, 1, 1};
PJONSlave bus(my_bys, 1); */
PJONSlave(
const uint8_t *b_id,
uint8_t device_id
) : PJON<Strategy>(b_id, device_id) {
set_default();
};
/* Acquire a device id: */
void acquire_id() {
if(!acquire_id_master_slave())
acquire_id_multi_master();
};
/* Acquire an id in multi-master configuration: */
void acquire_id_multi_master(uint8_t limit = 0) {
if(limit >= PJON_MAX_ACQUIRE_ID_COLLISIONS)
return _slave_error(PJON_ID_ACQUISITION_FAIL, PJON_FAIL);
PJON_DELAY_MICROSECONDS(PJON_RANDOM(PJON_ACQUIRE_ID_DELAY));
char msg = PJON_ID_ACQUIRE;
char head = this->config | required_config | PJON_ACK_REQ_BIT;
this->_device_id = PJON_NOT_ASSIGNED;
uint8_t id = PJON_RANDOM(1, PJON_MAX_DEVICES);
if(
id == PJON_NOT_ASSIGNED ||
id == PJON_MASTER_ID ||
id == PJON_BROADCAST
) acquire_id_multi_master(limit);
if(
this->send_packet_blocking(
id,
this->bus_id,
&msg,
1,
head,
PJON_DYNAMIC_ADDRESSING_PORT
) == PJON_ACK
) acquire_id_multi_master(limit++);
this->_device_id = id;
receive(PJON_RANDOM(PJON_ACQUIRE_ID_DELAY) * 1000);
if(
this->send_packet_blocking(
this->_device_id,
this->bus_id,
&msg,
1,
head,
PJON_DYNAMIC_ADDRESSING_PORT
) == PJON_ACK
) acquire_id_multi_master(limit++);
};
/* Acquire id in master-slave configuration: */
bool acquire_id_master_slave() {
generate_rid();
char response[5];
response[0] = PJON_ID_REQUEST;
response[1] = (uint32_t)(_rid) >> 24;
response[2] = (uint32_t)(_rid) >> 16;
response[3] = (uint32_t)(_rid) >> 8;
response[4] = (uint32_t)(_rid);
if(this->send_packet_blocking(
PJON_MASTER_ID,
this->bus_id,
response,
5,
this->config | PJON_ACK_REQ_BIT | required_config,
PJON_DYNAMIC_ADDRESSING_PORT
) == PJON_ACK) return true;
return false;
};
/* Begin function to be called in setup: */
void begin() {
PJON<Strategy>::begin();
if(this->_device_id == PJON_NOT_ASSIGNED)
acquire_id();
};
/* Release device id (Master-slave only): */
bool discard_device_id() {
char request[6] = {
PJON_ID_NEGATE,
_rid >> 24,
_rid >> 16,
_rid >> 8,
_rid,
this->_device_id
};
if(this->send_packet_blocking(
PJON_MASTER_ID,
this->bus_id,
request,
6,
this->config | PJON_ACK_REQ_BIT | required_config,
PJON_DYNAMIC_ADDRESSING_PORT
) == PJON_ACK) {
this->_device_id = PJON_NOT_ASSIGNED;
return true;
}
return false;
};
/* Master error handler: */
void error_handler(uint8_t code, uint8_t data) {
_slave_error(code, data);
};
static void static_error_handler(uint8_t code, uint8_t data) {
PJONSlave<Strategy> *slave = _current_pjon_slave;
if(slave != NULL) slave->error_handler(code, data);
};
/* Generate a new device rid: */
void generate_rid() {
_rid = (
(uint32_t)(PJON_RANDOM()) ^
(uint32_t)(PJON_ANALOG_READ(this->random_seed)) ^
(uint32_t)(PJON_MICROS())
) ^ _rid ^ _last_request_time;
};
/* Get device rid: */
uint32_t get_rid() {
return _rid;
};
/* Handle dynamic addressing requests and responses: */
bool handle_addressing() {
if( // Detect mult-master dynamic addressing
(this->last_packet_info.header & PJON_PORT_BIT) &&
(this->last_packet_info.header & PJON_TX_INFO_BIT) &&
(this->last_packet_info.header & PJON_CRC_BIT) &&
(this->last_packet_info.port == PJON_DYNAMIC_ADDRESSING_PORT) &&
(
(this->last_packet_info.sender_id == PJON_NOT_ASSIGNED) ||
(this->last_packet_info.sender_id == this->_device_id)
)
) return true;
if( // Handle master-slave dynamic addressing
(this->last_packet_info.header & PJON_PORT_BIT) &&
(this->last_packet_info.header & PJON_TX_INFO_BIT) &&
(this->last_packet_info.header & PJON_CRC_BIT) &&
(this->last_packet_info.port == PJON_DYNAMIC_ADDRESSING_PORT) &&
(this->_device_id != PJON_MASTER_ID) &&
(this->last_packet_info.sender_id == PJON_MASTER_ID)
) {
uint8_t overhead =
this->packet_overhead(this->last_packet_info.header);
uint8_t CRC_overhead =
(this->last_packet_info.header & PJON_CRC_BIT) ? 4 : 1;
uint8_t rid[4] = {_rid >> 24, _rid >> 16, _rid >> 8, _rid};
char response[6];
response[1] = rid[0];
response[2] = rid[1];
response[3] = rid[2];
response[4] = rid[3];
if(this->data[overhead - CRC_overhead] == PJON_ID_REQUEST)
if(
this->bus_id_equality(
this->data + ((overhead - CRC_overhead) + 1),
rid
)
) {
response[0] = PJON_ID_CONFIRM;
response[5] = this->data[(overhead - CRC_overhead) + 5];
this->set_id(response[5]);
if(this->send_packet_blocking(
PJON_MASTER_ID,
this->bus_id,
response,
6,
this->config | PJON_ACK_REQ_BIT | required_config,
PJON_DYNAMIC_ADDRESSING_PORT
) != PJON_ACK) {
this->set_id(PJON_NOT_ASSIGNED);
_slave_error(PJON_ID_ACQUISITION_FAIL, PJON_ID_CONFIRM);
}
}
if(this->data[overhead - CRC_overhead] == PJON_ID_NEGATE)
if(
this->bus_id_equality(
this->data + ((overhead - CRC_overhead) + 1),
rid
) && this->_device_id == this->data[0]
) acquire_id();
if(this->data[overhead - CRC_overhead] == PJON_ID_LIST)
if(this->_device_id != PJON_NOT_ASSIGNED) {
if(
(uint32_t)(PJON_MICROS() - _last_request_time) >
(PJON_ADDRESSING_TIMEOUT * 1.125)
) {
_last_request_time = PJON_MICROS();
response[0] = PJON_ID_REFRESH;
response[5] = this->_device_id;
this->send(
PJON_MASTER_ID,
this->bus_id,
response,
6,
this->config | PJON_ACK_REQ_BIT | required_config,
0,
PJON_DYNAMIC_ADDRESSING_PORT
);
}
} else if(
(uint32_t)(PJON_MICROS() - _last_request_time) >
(PJON_ADDRESSING_TIMEOUT * 1.125)
) {
_last_request_time = PJON_MICROS();
acquire_id();
}
return true;
}
return false;
};
/* Slave receive function: */
uint16_t receive() {
_current_pjon_slave = this;
uint16_t received_data = PJON<Strategy>::receive();
if(received_data != PJON_ACK) return received_data;
uint8_t overhead = this->packet_overhead(this->data[1]);
uint16_t length = 0;
if(this->last_packet_info.header & PJON_EXT_LEN_BIT)
length = (this->data[2] << 8) | (this->data[3] & 0xFF);
else length = this->data[2];
if(!handle_addressing())
_slave_receiver(
this->data + (overhead - (this->data[1] & PJON_CRC_BIT ? 4 : 1)),
length - overhead,
this->last_packet_info
);
return PJON_ACK;
};
/* Try to receive a packet repeatedly with a maximum duration: */
uint16_t receive(uint32_t duration) {
uint32_t time = PJON_MICROS();
while((uint32_t)(PJON_MICROS() - time) <= duration)
if(receive() == PJON_ACK) return PJON_ACK;
return PJON_FAIL;
};
/* Set default configuration: */
void set_default() {
PJON<Strategy>::set_error(static_error_handler);
set_error(PJON_dummy_error_handler);
set_receiver(PJON_dummy_receiver_handler);
this->include_port(true);
};
/* Slave receiver function setter: */
void set_receiver(PJON_Receiver r) {
_slave_receiver = r;
};
/* Master error receiver function: */
void set_error(PJON_Error e) {
_slave_error = e;
};
/* Slave packet handling update: */
uint8_t update() {
_current_pjon_slave = this;
return PJON<Strategy>::update();
};
private:
uint32_t _last_request_time;
PJON_Receiver _slave_receiver;
PJON_Error _slave_error;
uint32_t _rid;
static PJONSlave<Strategy> *_current_pjon_slave;
};
/* Shared callback function definition: */
template<typename Strategy>
PJONSlave<Strategy> * PJONSlave<Strategy>::_current_pjon_slave = NULL;