forked from franckmarini/KnxDevice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KnxTpUart.cpp
executable file
·623 lines (551 loc) · 24.3 KB
/
KnxTpUart.cpp
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
// This file is part of Arduino Knx Bus Device library.
// The Arduino Knx Bus Device library allows to turn Arduino into "self-made" KNX bus device.
// Copyright (C) 2014 2015 2016 Franck MARINI ([email protected])
// The Arduino Knx Bus Device library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// File : KnxTpUart.cpp
// Author : Franck Marini
// Description : Communication with TPUART
// Module dependencies : HardwareSerial, KnxTelegram, KnxComObject
#include "KnxTpUart.h"
static inline word TimeDeltaWord(word now, word before) { return (word)(now - before); }
#ifdef KNXTPUART_DEBUG_INFO
const char KnxTpUart::_debugInfoText[] = "KNXTPUART INFO: ";
#endif
#ifdef KNXTPUART_DEBUG_ERROR
const char KnxTpUart::_debugErrorText[] = "KNXTPUART ERROR: ";
#endif
extern uint8_t Ga_data[20]; //qwq
extern uint16_t Ga_int; //qwq
// extern const byte _comObjectsNb;
// Constructor
KnxTpUart::KnxTpUart(HardwareSerial& serial, word physicalAddr, type_KnxTpUartMode mode)
: _serial(serial), _physicalAddr(physicalAddr), _mode(mode)
{
_rx.state = RX_RESET;
_rx.addressedComObjectIndex = 0;
_tx.state = TX_RESET;
_tx.sentTelegram = NULL;
_tx.ackFctPtr = NULL;
_tx.nbRemainingBytes = 0;
_tx.txByteIndex = 0;
_stateIndication = 0;
_evtCallbackFct = NULL;
_comObjectsList = NULL;
_assignedComObjectsNb = 0;
_orderedIndexTable = NULL;
_stateIndication = 0;
#if defined(KNXTPUART_DEBUG_INFO) || defined(KNXTPUART_DEBUG_ERROR)
_debugStrPtr = NULL;
#endif
}
// Destructor
KnxTpUart::~KnxTpUart()
{
if (_orderedIndexTable) free(_orderedIndexTable);
// close the serial communication if opened
if ( (_rx.state > RX_RESET) || (_tx.state > TX_RESET) )
{
_serial.end();
#if defined(KNXTPUART_DEBUG_INFO)
DebugInfo("Destructor: connection closed, byebye\n");
#endif
}
#if defined(KNXTPUART_DEBUG_INFO)
else DebugInfo("Desctructor: byebye\n");
#endif
}
// Reset the Arduino UART port and the TPUART device
// Return KNX_TPUART_ERROR in case of TPUART Reset failure
byte KnxTpUart::Reset(void)
{
word startTime, nowTime;
byte attempts = 15;
if ( (_rx.state > RX_RESET) || (_tx.state > TX_RESET) )
{ // HOT RESET case
_serial.end(); // stop the serial communication before restarting it
_rx.state = RX_RESET; _tx.state = TX_RESET;
}
// CONFIGURATION OF THE ARDUINO USART WITH CORRECT FRAME FORMAT (19200, 8 bits, parity even, 1 stop bit)
#ifdef ESP8266
_serial.begin(19200, SERIAL_8E1);
_serial.swap();
#else
_serial.begin(19200, SERIAL_8E1, 5,17); // ESP32 hack for knx server qwq
#endif
while(attempts--)
{ // we send a RESET REQUEST and wait for the reset indication answer
// the sequence is repeated every sec as long as we do not get the reset indication
_serial.write(TPUART_RESET_REQ); // send RESET REQUEST
for (nowTime = startTime = (word) millis() ; TimeDeltaWord(nowTime,startTime) < 400 /* 1 sec */ ; nowTime = (word)millis()) //qwq 1000
{
if (_serial.available() > 0)
{
if (_serial.read() == TPUART_RESET_INDICATION)
{
_rx.state = RX_INIT; _tx.state = TX_INIT;
#if defined(KNXTPUART_DEBUG_INFO)
DebugInfo("Reset successful\n");
#endif
return KNX_TPUART_OK;
}
}
} // 1 sec ellapsed
} // while(attempts--)
_serial.end();
#if defined(KNXTPUART_DEBUG_ERROR)
DebugError("Reset failed, no answer from TPUART device\n");
#endif
return KNX_TPUART_ERROR;
}
// Attach a list of com objects
// NB1 : only the objects with "communication" attribute are considered by the TPUART
// NB2 : In case of objects with identical address, the object with highest index only is considered
// return KNX_TPUART_ERROR_NOT_INIT_STATE (254) if the TPUART is not in Init state
// The function must be called prior to Init() execution
byte KnxTpUart::AttachComObjectsList(KnxComObject comObjectsList[], byte listSize)
{
#define IS_COM(index) (comObjectsList[index].GetIndicator() & KNX_COM_OBJ_C_INDICATOR)
#define ADDR(index) (comObjectsList[index].GetAddr())
if ((_rx.state!=RX_INIT) || (_tx.state!=TX_INIT)) return KNX_TPUART_ERROR_NOT_INIT_STATE;
if (_orderedIndexTable)
{ // a list is already attached, we detach it
free(_orderedIndexTable);
_orderedIndexTable = NULL;
_comObjectsList = NULL;
_assignedComObjectsNb = 0;
}
if ((!comObjectsList) || (!listSize))
{
#if defined(KNXTPUART_DEBUG_INFO)
DebugInfo("AttachComObjectsList : warning : empty object list!\n");
#endif
return KNX_TPUART_OK;
}
// Count all the com objects with communication indicator
for (byte i=0; i < listSize ; i++) if (IS_COM(i)) _assignedComObjectsNb++;
if (!_assignedComObjectsNb)
{
#if defined(KNXTPUART_DEBUG_INFO)
DebugInfo("AttachComObjectsList : warning : no object with com attribute in the list!\n");
#endif
return KNX_TPUART_OK;
}
// Deduct the duplicate addresses
for (byte i=0; i < listSize ; i++)
{
if (!IS_COM(i)) continue;
for (byte j=0; j < listSize ; j++)
{
if ( (i!=j) && (ADDR(j) == ADDR(i)) && (IS_COM(j)) )
{ // duplicate address found
if (j<i) break; // duplicate address already treated
else
{
_assignedComObjectsNb--;
#if defined(KNXTPUART_DEBUG_INFO)
DebugInfo("AttachComObjectsList : warning : duplicate address found!\n");
#endif
}
}
}
}
_comObjectsList = comObjectsList;
// Creation of the ordered index table
_orderedIndexTable = (byte*) malloc(_assignedComObjectsNb);
word minMin = 0x0000; // minimum min value searched
word foundMin = 0xFFFF; // min value found so far
for (byte i=0; i < _assignedComObjectsNb; i++)
{
for (byte j=0; j < listSize ; j++)
{
if ( (IS_COM(j)) && (ADDR(j)>=minMin) && (ADDR(j)<=foundMin) )
{
foundMin = ADDR(j);
_orderedIndexTable[i] = j;
}
}
minMin = foundMin + 1;
foundMin = 0xFFFF;
}
#if defined(KNXTPUART_DEBUG_INFO)
DebugInfo("AttachComObjectsList successful\n");
#endif
return KNX_TPUART_OK;
}
// Init
// returns ERROR (255) if the TP-UART is not in INIT state, else returns OK (0)
// Init must be called after every reset() execution
byte KnxTpUart::Init(void)
{
byte tpuartCmd[3];
if ((_rx.state!=RX_INIT) || (_tx.state!=TX_INIT)) return KNX_TPUART_ERROR_NOT_INIT_STATE;
// BUS MONITORING MODE in case it is selected
if (_mode == BUS_MONITOR)
{
_serial.write(TPUART_ACTIVATEBUSMON_REQ); // Send bus monitoring activation request
#if defined(KNXTPUART_DEBUG_INFO)
DebugInfo("Init : Monitoring mode started\n");
#endif
}
else // NORMAL mode by default
{
#if defined(KNXTPUART_DEBUG_INFO)
if (_comObjectsList == NULL) DebugInfo("Init : warning : empty object list!\n");
#endif
if (_evtCallbackFct == NULL) return KNX_TPUART_ERROR_NULL_EVT_CALLBACK_FCT;
if (_tx.ackFctPtr == NULL) return KNX_TPUART_ERROR_NULL_ACK_CALLBACK_FCT;
// Set Physical address. This allows to activate address evaluation by the TPUART
tpuartCmd[0] = TPUART_SET_ADDR_REQ;
tpuartCmd[1] = (byte)(_physicalAddr>>8);
tpuartCmd[2] = (byte)_physicalAddr;
_serial.write(tpuartCmd,3);
// Call U_State.request-Service in order to have the field _stateIndication up-to-date
_serial.write(TPUART_STATE_REQ);
_rx.state = RX_IDLE_WAITING_FOR_CTRL_FIELD; // Setze auf Prüfung erstes Byte
_tx.state = TX_IDLE;
#if defined(KNXTPUART_DEBUG_INFO)
DebugInfo("Init : Normal mode started\n");
#endif
}
return KNX_TPUART_OK;
}
// Send a KNX telegram
// returns ERROR (255) if TX is not available, or if the telegram is not valid, else returns OK (0)
// NB : the source address is forced to TPUART physical address value
byte KnxTpUart::SendTelegram(KnxTelegram& sentTelegram)
{
if (_tx.state != TX_IDLE) return KNX_TPUART_ERROR; // TX not initialized or busy
if (sentTelegram.GetSourceAddress() != _physicalAddr) // Check that source addr equals TPUART physical addr
{ // if not, let's force source addr to the correct value
sentTelegram.SetSourceAddress(_physicalAddr);
sentTelegram.UpdateChecksum();
}
_tx.sentTelegram = &sentTelegram;
_tx.nbRemainingBytes = sentTelegram.GetTelegramLength();
_tx.txByteIndex = 0; // Set index to 0
_tx.state = TX_TELEGRAM_SENDING_ONGOING;
return KNX_TPUART_OK;
}
// Reception task
// This function shall be called periodically in order to allow a correct reception of the EIB bus data
// Assuming the TPUART speed is configured to 19200 baud, a character (8 data + 1 start + 1 parity + 1 stop)
// is transmitted in 0,58ms.
// In order not to miss any End Of Packets (i.e. a gap from 2 to 2,5ms), the function shall be called at a max period of 0,5ms.
// Typical calling period is 400 usec.
void KnxTpUart::RXTask(void)
{
byte incomingByte;
word nowTime;
static byte readBytesNb; // Nb of read bytes during an EIB telegram reception
static KnxTelegram telegram; // telegram being received
static byte addressedComObjectIndex; // index of the com object targeted by the received telegram
static word lastByteRxTimeMicrosec;
// === STEP 1 : Check EOP in case a Telegram is being received ===
if (_rx.state >= RX_EIB_TELEGRAM_RECEPTION_STARTED)
{ // a telegram reception is ongoing
nowTime = (word) micros(); // word cast because a 65ms looping counter is long enough
if(TimeDeltaWord(nowTime,lastByteRxTimeMicrosec) > 2000 /* 2 ms */ )
{ // EOP detected, the telegram reception is completed
// Serial.print("Telegram fertig: Daten: ");Serial.println(readBytesNb);
switch (_rx.state) // execute _rx State 1. Verarbeitung
{
case RX_EIB_TELEGRAM_RECEPTION_STARTED : // we are not supposed to get EOP now, the telegram is incomplete
case RX_EIB_TELEGRAM_RECEPTION_LENGTH_INVALID :
_evtCallbackFct(TPUART_EVENT_EIB_TELEGRAM_RECEPTION_ERROR); // Notify telegram reception error
break;
case RX_EIB_TELEGRAM_RECEPTION_ADDRESSED :
if (telegram.IsChecksumCorrect())
{ // checksum correct, let's update the _rx struct with the received telegram and correct index
telegram.Copy(_rx.receivedTelegram);
_rx.addressedComObjectIndex = addressedComObjectIndex;
_evtCallbackFct(TPUART_EVENT_RECEIVED_EIB_TELEGRAM); // Notify the new received telegram
}
else
{ // checksum incorrect, notify error
_evtCallbackFct(TPUART_EVENT_EIB_TELEGRAM_RECEPTION_ERROR); // Notify telegram reception error
}
break;
case RX_EIB_TELEGRAM_RECEPTION_NOT_ADDRESSED : // qwq nicht addressierte Telegramme sollen trotzdem mit Index 0 nach oben gegeben werden
{ // checksum correct, let's update the _rx struct with the received telegram and correct index
//Ga_data[0] = telegram.ReadRawByte(8);
//Ga_data[1] = telegram.ReadRawByte(9);
//Ga_data[2] = telegram.ReadRawByte(10);
//Ga_data[3] = telegram.ReadRawByte(11);
//Ga_data[4] = telegram.GetPayloadLength();
telegram.Copy(_rx.receivedTelegram); //void KnxTelegram::Copy(KnxTelegram& dest) const
_rx.addressedComObjectIndex = addressedComObjectIndex; //qwq was 14
//Serial.print("Tpuart:Wert Index :");
//Serial.println(addressedComObjectIndex);
_evtCallbackFct(TPUART_EVENT_RECEIVED_EIB_TELEGRAM); // Notify the new received telegram
}
break; // nothing to do!
default : break;
} // end of switch
// we move state back to RX IDLE in any case
_rx.state = RX_IDLE_WAITING_FOR_CTRL_FIELD; // nach dem Empfangs des letzten Bytes mit einer gro0en Pause geht er immer auf diesen State zurück
// warten auf nächstes Telegramm
} // end EOP detected
}
// === STEP 2 : Get New RX Data ===
if (_serial.available() > 0)
{
incomingByte = (byte)(_serial.read());
lastByteRxTimeMicrosec = (word)micros();
switch (_rx.state) // calculate Rx State depending of incoming message
{
case RX_IDLE_WAITING_FOR_CTRL_FIELD: // check first received byte Kontrollfeld
// CASE OF EIB MESSAGE
if ((incomingByte & EIB_CONTROL_FIELD_PATTERN_MASK) == EIB_CONTROL_FIELD_VALID_PATTERN) // 0x90 = BC (msg) oder 9C (wdh)
{
_rx.state = RX_EIB_TELEGRAM_RECEPTION_STARTED;
readBytesNb = 1; // 1. Byte empfangen
telegram.WriteRawByte(incomingByte,0); // und in _telegram gespeichert
}
// CASE OF TPUART_DATA_CONFIRM_SUCCESS NOTIFICATION
else if (incomingByte == TPUART_DATA_CONFIRM_SUCCESS)
{
if (_tx.state == TX_WAITING_ACK)
{
_tx.ackFctPtr(ACK_RESPONSE);
_tx.state = TX_IDLE;
}
#if defined(KNXTPUART_DEBUG_ERROR)
else DebugError("Rx: unexpected TPUART_DATA_CONFIRM_SUCCESS received!\n");
#endif
}
// CASE OF TPUART_RESET NOTIFICATION
else if (incomingByte == TPUART_RESET_INDICATION)
{
if ( (_tx.state == TX_TELEGRAM_SENDING_ONGOING ) || (_tx.state == TX_WAITING_ACK ) )
{ // response to the TP UART transmission
_tx.ackFctPtr(TPUART_RESET_RESPONSE);
}
_tx.state = TX_STOPPED;
_rx.state = RX_STOPPED;
_evtCallbackFct(TPUART_EVENT_RESET); // Notify RESET
return;
}
// CASE OF STATE_INDICATION RESPONSE
else if ((incomingByte & TPUART_STATE_INDICATION_MASK) == TPUART_STATE_INDICATION)
{
_evtCallbackFct(TPUART_EVENT_STATE_INDICATION); // Notify STATE INDICATION
_stateIndication = incomingByte;
#if defined(KNXTPUART_DEBUG_INFO)
DebugInfo("Rx: State Indication Received\n");
#endif
}
// CASE OF TPUART_DATA_CONFIRM_FAILED NOTIFICATION
else if (incomingByte == TPUART_DATA_CONFIRM_FAILED)
{
// NACK following Telegram transmission
if (_tx.state == TX_WAITING_ACK)
{
_tx.ackFctPtr(NACK_RESPONSE);
_tx.state = TX_IDLE;
}
#if defined(KNXTPUART_DEBUG_ERROR)
else DebugError("Rx: unexpected TPUART_DATA_CONFIRM_FAILED received!\n");
#endif
}
#if defined(KNXTPUART_DEBUG_ERROR)
// UNKNOWN CONTROL FIELD RECEIVED
else if (incomingByte)
DebugError("Rx: Unknown Control Field received\n");
#endif
// else ignore "0" value sent on Reset by TPUART prior to TPUART_RESET_INDICATION
break;
case RX_EIB_TELEGRAM_RECEPTION_STARTED :
telegram.WriteRawByte(incomingByte,readBytesNb);
readBytesNb++;
if (readBytesNb==3)
{ // We have just received the source address
// we check whether the received EIB telegram is coming from us (i.e. telegram is sent by the TPUART itself)
if ( telegram.GetSourceAddress() == _physicalAddr )
{ // the message is coming from us, we consider it as not addressed and we don't send any ACK service
_rx.state = RX_EIB_TELEGRAM_RECEPTION_OWN_MESSAGE;
}
}
else if (readBytesNb==6) // We have just read the routing field containing the address type and the payload length
{ // We check if the message is addressed to us in order to send the appropriate acknowledge
// boolean IsAddressAssigned(word addr, byte &index) const;
if ((IsAddressAssigned(telegram.GetTargetAddress(), addressedComObjectIndex))&&(addressedComObjectIndex)) // Das letzte Com Obj ist immer der GMonitor _comObjectsNb
{ // Message addressed to us AND NOT Index 0
_rx.state = RX_EIB_TELEGRAM_RECEPTION_ADDRESSED;
//sent the correct ACK service now
// the ACK info must be sent latest 1,7 ms after receiving the address type octet of an addressed frame
_serial.write(TPUART_RX_ACK_SERVICE_ADDRESSED);
}
else
{ // Message NOT addressed to us or Index 0
_rx.state = RX_EIB_TELEGRAM_RECEPTION_NOT_ADDRESSED;
//sent the correct ACK service now
// the ACK info must be sent latest 1,7 ms after receiving the address type octet of an addressed frame
_serial.write(TPUART_RX_ACK_SERVICE_NOT_ADDRESSED);
//_serial.write(TPUART_RX_ACK_SERVICE_ADDRESSED);
}
Ga_int = telegram.GetTargetAddress(); //qwq
}
break;
case RX_EIB_TELEGRAM_RECEPTION_ADDRESSED :
if (readBytesNb == KNX_TELEGRAM_MAX_SIZE) _rx.state = RX_EIB_TELEGRAM_RECEPTION_LENGTH_INVALID;
else
{
telegram.WriteRawByte(incomingByte,readBytesNb); //_telegram[readBytesNb] = incomingByte;
readBytesNb++;
}
break;
// case RX_EIB_TELEGRAM_RECEPTION_LENGTH_INVALID : break; // if the message is too long, nothing to do except waiting for EOP
case RX_EIB_TELEGRAM_RECEPTION_NOT_ADDRESSED :
if (readBytesNb == KNX_TELEGRAM_MAX_SIZE) _rx.state = RX_EIB_TELEGRAM_RECEPTION_LENGTH_INVALID;
else
{ // Obwohl das Telegramm nicht für uns ist, wird es trotzdem in den Empfangspuffer kopiert !! GMonitor
telegram.WriteRawByte(incomingByte,readBytesNb); //_telegram[readBytesNb] = incomingByte;
readBytesNb++;
// Serial.println("Not addressed Telegram Byted copied");
}
break; // if the message is not addressed, nothing to do except waiting for EOP
default : break;
} // switch (_rx.state)
} // if (_serial.available() > 0)
}
// Transmission task
// This function shall be called periodically in order to allow a correct transmission of the EIB bus data
// Assuming the TP-Uart speed is configured to 19200 baud, a character (8 data + 1 start + 1 parity + 1 stop)
// is transmitted in 0,58ms.
// Sending one byte of a telegram consists in transmitting 2 characters (1,16ms)
// Let's wait around 800us between each telegram piece sending so that the 64byte TX buffer remains almost empty.
// Typical calling period is 800 usec.
void KnxTpUart::TXTask(void)
{
word nowTime;
byte txByte[2];
static word sentMessageTimeMillisec;
// STEP 1 : Manage Message Acknowledge timeout
switch (_tx.state)
{
case TX_WAITING_ACK :
// A transmission ACK is awaited, increment Acknowledge timeout
nowTime = (word) millis(); // word is enough to count up to 500
if(TimeDeltaWord(nowTime,sentMessageTimeMillisec) > 500 /* 500 ms */ )
{ // The no-answer timeout value is defined as follows :
// - The emission duration for a single max sized telegram is 40ms
// - The telegram emission might be repeated 3 times (120ms)
// - The telegram emission might be delayed by another message transmission ongoing
// - The telegram emission might be delayed by the simultaneous transmission of higher prio messages
// Let's take around 3 times the max emission duration (160ms) as arbitrary value
_tx.ackFctPtr(NO_ANSWER_TIMEOUT); // Send a No Answer TIMEOUT
_tx.state = TX_IDLE;
}
break;
case TX_TELEGRAM_SENDING_ONGOING :
// STEP 2 : send message if any to send
// In case a telegram reception has just started, and the ACK has not been sent yet,
// we block the transmission (for around 3,3ms) till the ACK is sent
// In that way, the TX buffer will remain empty and the ACK will be sent immediately
if (_rx.state != RX_EIB_TELEGRAM_RECEPTION_STARTED)
{
{
if (_tx.nbRemainingBytes == 1)
{ // We are sending the last byte, i.e checksum
txByte[0] = TPUART_DATA_END_REQ + _tx.txByteIndex;
txByte[1] = _tx.sentTelegram->ReadRawByte(_tx.txByteIndex);
_serial.write(txByte,2); // write the UART control field and the data byte
// Message sending completed
sentMessageTimeMillisec = (word)millis(); // memorize sending time in order to manage ACK timeout
_tx.state = TX_WAITING_ACK;
}
else
{
txByte[0] = TPUART_DATA_START_CONTINUE_REQ + _tx.txByteIndex;
txByte[1] = _tx.sentTelegram->ReadRawByte(_tx.txByteIndex);
_serial.write(txByte,2); // write the UART control field and the data byte
_tx.txByteIndex++;
_tx.nbRemainingBytes--;
}
}
}
break;
default : break;
} // switch
}
// Get Bus monitoring data (BUS MONITORING mode)
// The function returns true if a new data has been retrieved (data pointer in argument), else false
// It shall be called periodically (max period of 0,5ms) in order to allow correct data reception
// Typical calling period is 400 usec.
boolean KnxTpUart::GetMonitoringData(type_MonitorData& data)
{
word nowTime;
static type_MonitorData currentData={true,0};
static word lastByteRxTimeMicrosec;
// STEP 1 : Check EOP
if (!(currentData.isEOP)) // check that we have not already detected an EOP
{
nowTime = (word) micros(); // word cast because a 65ms counter is enough
if(TimeDeltaWord(nowTime,lastByteRxTimeMicrosec) > 2000 /* 2 ms */ )
{ // EOP detected
currentData.isEOP = true;
currentData.dataByte = 0;
data= currentData;
return true;
}
}
// STEP 2 : Get New RX Data
if (_serial.available() > 0)
{
currentData.dataByte = (byte)(_serial.read());
currentData.isEOP = false;
data= currentData;
lastByteRxTimeMicrosec = (word) micros();
return true;
}
return false; // No data received
}
// Check if the target address is an assigned com object one
// if yes, then update index parameter with the index (in the list) of the targeted com object and return true
// else return false
boolean KnxTpUart::IsAddressAssigned(word addr, byte &index) const
{
byte divisionCounter=0;
byte i, searchIndexStart, searchIndexStop, searchIndexRange;
if (!_assignedComObjectsNb) return false; // in case of empty list, we return immediately
// Define how many divisions by 2 shall be done in order to reduce the search list by 8 Addr max
// if _assignedComObjectsNb >= 16 => divisionCounter = 1
// if _assignedComObjectsNb >= 32 => divisionCounter = 2
// if _assignedComObjectsNb >= 64 => divisionCounter = 3
// if _assignedComObjectsNb >= 128 => divisionCounter = 4
for (i=4; _assignedComObjectsNb >>i ; i++) divisionCounter++;
// the starting point is to search on the whole address range (0 -> _assignedComObjectsNb -1)
searchIndexStart = 0; searchIndexStop = _assignedComObjectsNb - 1; searchIndexRange = _assignedComObjectsNb;
// reduce the address range if needed
while(divisionCounter)
{
searchIndexRange>>=1; // Divide range width by 2
if ( addr >= _comObjectsList[_orderedIndexTable[searchIndexStart+searchIndexRange]].GetAddr())
searchIndexStart += searchIndexRange ;
else searchIndexStop-=searchIndexRange;
divisionCounter --;
}
// search the address value and index in the reduced range
for (i = searchIndexStart; ((_comObjectsList[_orderedIndexTable[i]].GetAddr() != addr) && (i <= searchIndexStop)); i++);
if (i > searchIndexStop)
{
index = 0; // Setze auf GMonitor
return false; // Address is NOT part of the assigned addresses
}
// Address is part of the assigned addresses
index = _orderedIndexTable[i]; // Kann auch Index 0 sein, wenn die GA von GMonitor zufällig erkannt wurde
return true;
}
// DEBUG purpose functions
void KnxTpUart::DEBUG_SendResetCommand() { _serial.write(TPUART_RESET_REQ); }
void KnxTpUart::DEBUG_SendStateReqCommand() { _serial.write(TPUART_STATE_REQ); }
//EOF