]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Scan/UARTConnect/connect_scan.c
Adding basic remote capabilities + UART Rx DMA buffers
[kiibohd-controller.git] / Scan / UARTConnect / connect_scan.c
1 /* Copyright (C) 2014-2015 by Jacob Alexander
2  *
3  * This file is free software: you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 3 of the License, or
6  * (at your option) any later version.
7  *
8  * This file is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this file.  If not, see <http://www.gnu.org/licenses/>.
15  */
16
17 // ----- Includes -----
18
19 // Compiler Includes
20 #include <Lib/ScanLib.h>
21
22 // Project Includes
23 #include <cli.h>
24 #include <kll_defs.h>
25 #include <led.h>
26 #include <print.h>
27 #include <macro.h>
28
29 // Local Includes
30 #include "connect_scan.h"
31
32
33
34 // ----- Defines -----
35
36 #define UART_Num_Interfaces 2
37 #define UART_Master 1
38 #define UART_Slave  0
39 #define UART_Buffer_Size UARTConnectBufSize_define
40
41
42
43 // ----- Macros -----
44
45 // Macro for adding to each uart Tx ring buffer
46 #define uart_addTxBuffer( uartNum ) \
47 case uartNum: \
48         /* Delay UART copy until there's some space left */ \
49         while ( uart_tx_buf[ uartNum ].items + count > UART_Buffer_Size ) \
50         { \
51                 warn_msg("Too much data to send on UART0, waiting..."); \
52                 delay( 1 ); \
53         } \
54         /* Append data to ring buffer */ \
55         for ( uint8_t c = 0; c < count; c++ ) \
56         { \
57                 if ( Connect_debug ) \
58                 { \
59                         printHex( buffer[ c ] ); \
60                         print( " +" #uartNum NL ); \
61                 } \
62                 uart_tx_buf[ uartNum ].buffer[ uart_tx_buf[ uartNum ].tail++ ] = buffer[ c ]; \
63                 uart_tx_buf[ uartNum ].items++; \
64                 if ( uart_tx_buf[ uartNum ].tail >= UART_Buffer_Size ) \
65                         uart_tx_buf[ uartNum ].tail = 0; \
66                 if ( uart_tx_buf[ uartNum ].head == uart_tx_buf[ uartNum ].tail ) \
67                         uart_tx_buf[ uartNum ].head++; \
68                 if ( uart_tx_buf[ uartNum ].head >= UART_Buffer_Size ) \
69                         uart_tx_buf[ uartNum ].head = 0; \
70         } \
71         break
72
73 // Macro for popping from Tx ring buffer
74 #define uart_fillTxFifo( uartNum ) \
75 { \
76         uint8_t fifoSize = ( ( UART##uartNum##_PFIFO & UART_PFIFO_TXFIFOSIZE ) >> 2 ); \
77         if ( fifoSize == 0 ) \
78                 fifoSize = 1; \
79         if ( Connect_debug ) \
80         { \
81                 print( "TxFIFO " #uartNum " - " ); \
82                 printHex( fifoSize ); \
83                 print("/"); \
84                 printHex( UART##uartNum##_TCFIFO ); \
85                 print("/"); \
86                 printHex( uart_tx_buf[ uartNum ].items ); \
87                 print( NL ); \
88         } \
89         /* XXX Doesn't work well */ \
90         /* while ( UART##uartNum##_TCFIFO < fifoSize ) */ \
91         /* More reliable, albeit slower */ \
92         fifoSize -= UART##uartNum##_TCFIFO; \
93         while ( fifoSize-- != 0 ) \
94         { \
95                 if ( uart_tx_buf[ uartNum ].items == 0 ) \
96                         break; \
97                 UART##uartNum##_D = uart_tx_buf[ uartNum ].buffer[ uart_tx_buf[ uartNum ].head++ ]; \
98                 uart_tx_buf[ uartNum ].items--; \
99                 if ( uart_tx_buf[ uartNum ].head >= UART_Buffer_Size ) \
100                         uart_tx_buf[ uartNum ].head = 0; \
101         } \
102 }
103
104 // Macros for locking/unlock Tx buffers
105 #define uart_lockTx( uartNum ) \
106 { \
107         /* First, secure place in line for the resource */ \
108         while ( uart_tx_status[ uartNum ].lock ); \
109         uart_tx_status[ uartNum ].lock = 1; \
110         /* Next, wait unit the UART is ready */ \
111         while ( uart_tx_status[ uartNum ].status != UARTStatus_Ready ); \
112         uart_tx_status[ uartNum ].status = UARTStatus_Wait; \
113 }
114
115 #define uart_lockBothTx( uartNum1, uartNum2 ) \
116 { \
117         /* First, secure place in line for the resource */ \
118         while ( uart_tx_status[ uartNum1 ].lock || uart_tx_status[ uartNum2 ].lock ); \
119         uart_tx_status[ uartNum1 ].lock = 1; \
120         uart_tx_status[ uartNum2 ].lock = 1; \
121         /* Next, wait unit the UARTs are ready */ \
122         while ( uart_tx_status[ uartNum1 ].status != UARTStatus_Ready || uart_tx_status[ uartNum2 ].status != UARTStatus_Ready ); \
123         uart_tx_status[ uartNum1 ].status = UARTStatus_Wait; \
124         uart_tx_status[ uartNum2 ].status = UARTStatus_Wait; \
125 }
126
127 #define uart_unlockTx( uartNum ) \
128 { \
129         /* Ready the UART */ \
130         uart_tx_status[ uartNum ].status = UARTStatus_Ready; \
131         /* Unlock the resource */ \
132         uart_tx_status[ uartNum ].lock = 0; \
133 }
134
135
136
137 // ----- Function Declarations -----
138
139 // CLI Functions
140 void cliFunc_connectCmd ( char *args );
141 void cliFunc_connectDbg ( char *args );
142 void cliFunc_connectIdl ( char *args );
143 void cliFunc_connectLst ( char *args );
144 void cliFunc_connectMst ( char *args );
145 void cliFunc_connectRst ( char *args );
146 void cliFunc_connectSts ( char *args );
147
148
149
150 // ----- Structs -----
151
152 typedef struct UARTRingBuf {
153         uint8_t head;
154         uint8_t tail;
155         uint8_t items;
156         uint8_t buffer[UART_Buffer_Size];
157 } UARTRingBuf;
158
159 typedef struct UARTDMABuf {
160         uint8_t  buffer[UART_Buffer_Size];
161         uint16_t last_read;
162 } UARTDMABuf;
163
164 typedef struct UARTStatusRx {
165         UARTStatus status;
166         Command    command;
167         uint16_t   bytes_waiting;
168 } UARTStatusRx;
169
170 typedef struct UARTStatusTx {
171         UARTStatus status;
172         uint8_t    lock;
173 } UARTStatusTx;
174
175
176
177 // ----- Variables -----
178
179 // Connect Module command dictionary
180 CLIDict_Entry( connectCmd,  "Sends a command via UART Connect, first arg is which uart, next arg is the command, rest are the arguments." );
181 CLIDict_Entry( connectDbg,  "Toggle UARTConnect debug mode." );
182 CLIDict_Entry( connectIdl,  "Sends N number of Idle commands, 2 is the default value, and should be sufficient in most cases." );
183 CLIDict_Entry( connectLst,  "Lists available UARTConnect commands and index id" );
184 CLIDict_Entry( connectMst,  "Sets the device as master. Use argument of s to set as slave." );
185 CLIDict_Entry( connectRst,  "Resets both Rx and Tx connect buffers and state variables." );
186 CLIDict_Entry( connectSts,  "UARTConnect status." );
187 CLIDict_Def( uartConnectCLIDict, "UARTConnect Module Commands" ) = {
188         CLIDict_Item( connectCmd ),
189         CLIDict_Item( connectDbg ),
190         CLIDict_Item( connectIdl ),
191         CLIDict_Item( connectLst ),
192         CLIDict_Item( connectMst ),
193         CLIDict_Item( connectRst ),
194         CLIDict_Item( connectSts ),
195         { 0, 0, 0 } // Null entry for dictionary end
196 };
197
198
199 // -- Connect Device Id Variables --
200 uint8_t Connect_id = 255; // Invalid, unset
201 uint8_t Connect_master = 0;
202 uint8_t Connect_maxId = 0;
203
204
205 // -- Control Variables --
206 uint32_t Connect_lastCheck = 0; // Cable Check scheduler
207 uint8_t Connect_debug = 0;      // Set 1 for debug
208 uint8_t Connect_override = 0;   // Prevents master from automatically being set
209
210 volatile uint8_t uarts_configured = 0;
211
212
213 // -- Rx Variables --
214
215 volatile UARTDMABuf   uart_rx_buf[UART_Num_Interfaces];
216 volatile UARTStatusRx uart_rx_status[UART_Num_Interfaces];
217
218
219 // -- Tx Variables --
220
221 UARTRingBuf  uart_tx_buf   [UART_Num_Interfaces];
222 UARTStatusTx uart_tx_status[UART_Num_Interfaces];
223
224
225 // -- Ring Buffer Convenience Functions --
226
227 void Connect_addBytes( uint8_t *buffer, uint8_t count, uint8_t uart )
228 {
229         // Too big to fit into buffer
230         if ( count > UART_Buffer_Size )
231         {
232                 erro_msg("Too big of a command to fit into the buffer...");
233                 return;
234         }
235
236         // Choose the uart
237         switch ( uart )
238         {
239         uart_addTxBuffer( UART_Master );
240         uart_addTxBuffer( UART_Slave );
241         default:
242                 erro_msg("Invalid UART to send from...");
243                 break;
244         }
245 }
246
247
248 // -- Connect send functions --
249
250 // patternLen defines how many bytes should the incrementing pattern have
251 void Connect_send_CableCheck( uint8_t patternLen )
252 {
253         // Wait until the Tx buffers are ready, then lock them
254         uart_lockBothTx( UART_Master, UART_Slave );
255
256         // Prepare header
257         uint8_t header[] = { 0x16, 0x01, CableCheck, patternLen };
258
259         // Send header
260         Connect_addBytes( header, sizeof( header ), UART_Master );
261         Connect_addBytes( header, sizeof( header ), UART_Slave );
262
263         // Send 0xD2 (11010010) for each argument
264         uint8_t value = 0xD2;
265         for ( uint8_t c = 0; c < patternLen; c++ )
266         {
267                 Connect_addBytes( &value, 1, UART_Master );
268                 Connect_addBytes( &value, 1, UART_Slave );
269         }
270
271         // Release Tx buffers
272         uart_unlockTx( UART_Master );
273         uart_unlockTx( UART_Slave );
274 }
275
276 void Connect_send_IdRequest()
277 {
278         // Lock master bound Tx
279         uart_lockTx( UART_Master );
280
281         // Prepare header
282         uint8_t header[] = { 0x16, 0x01, IdRequest };
283
284         // Send header
285         Connect_addBytes( header, sizeof( header ), UART_Master );
286
287         // Unlock Tx
288         uart_unlockTx( UART_Master );
289 }
290
291 // id is the value the next slave should enumerate as
292 void Connect_send_IdEnumeration( uint8_t id )
293 {
294         // Lock slave bound Tx
295         uart_lockTx( UART_Slave );
296
297         // Prepare header
298         uint8_t header[] = { 0x16, 0x01, IdEnumeration, id };
299
300         // Send header
301         Connect_addBytes( header, sizeof( header ), UART_Slave );
302
303         // Unlock Tx
304         uart_unlockTx( UART_Slave );
305 }
306
307 // id is the currently assigned id to the slave
308 void Connect_send_IdReport( uint8_t id )
309 {
310         // Lock master bound Tx
311         uart_lockTx( UART_Master );
312
313         // Prepare header
314         uint8_t header[] = { 0x16, 0x01, IdReport, id };
315
316         // Send header
317         Connect_addBytes( header, sizeof( header ), UART_Master );
318
319         // Unlock Tx
320         uart_unlockTx( UART_Master );
321 }
322
323 // id is the currently assigned id to the slave
324 // scanCodeStateList is an array of [scancode, state]'s (8 bit values)
325 // numScanCodes is the number of scan codes to parse from array
326 void Connect_send_ScanCode( uint8_t id, TriggerGuide *scanCodeStateList, uint8_t numScanCodes )
327 {
328         // Lock master bound Tx
329         uart_lockTx( UART_Master );
330
331         // Prepare header
332         uint8_t header[] = { 0x16, 0x01, ScanCode, id, numScanCodes };
333
334         // Send header
335         Connect_addBytes( header, sizeof( header ), UART_Master );
336
337         // Send each of the scan codes
338         Connect_addBytes( (uint8_t*)scanCodeStateList, numScanCodes * TriggerGuideSize, UART_Master );
339
340         // Unlock Tx
341         uart_unlockTx( UART_Master );
342 }
343
344 // id is the currently assigned id to the slave
345 // paramList is an array of [param, value]'s (8 bit values)
346 // numParams is the number of params to parse from the array
347 void Connect_send_Animation( uint8_t id, uint8_t *paramList, uint8_t numParams )
348 {
349         // Lock slave bound Tx
350         uart_lockTx( UART_Slave );
351
352         // Prepare header
353         uint8_t header[] = { 0x16, 0x01, Animation, id, numParams };
354
355         // Send header
356         Connect_addBytes( header, sizeof( header ), UART_Slave );
357
358         // Send each of the scan codes
359         Connect_addBytes( paramList, numParams, UART_Slave );
360
361         // Unlock Tx
362         uart_unlockTx( UART_Slave );
363 }
364
365 // Send a remote capability command using capability index
366 // This may not be what's expected (especially if the firmware is not the same on each node)
367 // To broadcast to all slave nodes, set id to 255 instead of a specific id
368 void Connect_send_RemoteCapability( uint8_t id, uint8_t capabilityIndex, uint8_t state, uint8_t stateType, uint8_t numArgs, uint8_t *args )
369 {
370         // Prepare header
371         uint8_t header[] = { 0x16, 0x01, RemoteCapability, id, capabilityIndex, state, stateType, numArgs };
372
373         // Ignore current id
374         if ( id == Connect_id )
375                 return;
376
377         // Send towards slave node
378         if ( id > Connect_id )
379         {
380                 // Lock slave bound Tx
381                 uart_lockTx( UART_Slave );
382
383                 // Send header
384                 Connect_addBytes( header, sizeof( header ), UART_Slave );
385
386                 // Send arguments
387                 Connect_addBytes( args, numArgs, UART_Slave );
388
389                 // Unlock Tx
390                 uart_unlockTx( UART_Slave );
391         }
392
393         // Send towards master node
394         if ( id < Connect_id || id == 255 )
395         {
396                 // Lock slave bound Tx
397                 uart_lockTx( UART_Master );
398
399                 // Send header
400                 Connect_addBytes( header, sizeof( header ), UART_Master );
401
402                 // Send arguments
403                 Connect_addBytes( args, numArgs, UART_Master );
404
405                 // Unlock Tx
406                 uart_unlockTx( UART_Master );
407         }
408 }
409
410 void Connect_send_Idle( uint8_t num )
411 {
412         // Wait until the Tx buffers are ready, then lock them
413         uart_lockBothTx( UART_Slave, UART_Master );
414
415         // Send n number of idles to reset link status (if in a bad state)
416         uint8_t value = 0x16;
417         for ( uint8_t c = 0; c < num; c++ )
418         {
419                 Connect_addBytes( &value, 1, UART_Master );
420                 Connect_addBytes( &value, 1, UART_Slave );
421         }
422
423         // Release Tx buffers
424         uart_unlockTx( UART_Master );
425         uart_unlockTx( UART_Slave );
426 }
427
428
429 // -- Connect receive functions --
430
431 // - Cable Check variables -
432 uint32_t Connect_cableFaultsMaster = 0;
433 uint32_t Connect_cableFaultsSlave  = 0;
434 uint32_t Connect_cableChecksMaster = 0;
435 uint32_t Connect_cableChecksSlave  = 0;
436 uint8_t  Connect_cableOkMaster = 0;
437 uint8_t  Connect_cableOkSlave  = 0;
438
439 uint8_t Connect_receive_CableCheck( uint8_t byte, uint16_t *pending_bytes, uint8_t uart_num )
440 {
441         // Check if this is the first byte
442         if ( *pending_bytes == 0xFFFF )
443         {
444                 *pending_bytes = byte;
445
446                 if ( Connect_debug )
447                 {
448                         dbug_msg("PENDING SET -> ");
449                         printHex( byte );
450                         print(" ");
451                         printHex( *pending_bytes );
452                         print( NL );
453                 }
454         }
455         // Verify byte
456         else
457         {
458                 (*pending_bytes)--;
459
460                 // The argument bytes are always 0xD2 (11010010)
461                 if ( byte != 0xD2 )
462                 {
463                         warn_print("Cable Fault!");
464
465                         // Check which side of the chain
466                         if ( uart_num == UART_Slave )
467                         {
468                                 Connect_cableFaultsSlave++;
469                                 Connect_cableOkSlave = 0;
470                                 print(" Slave ");
471                         }
472                         else
473                         {
474                                 Connect_cableFaultsMaster++;
475                                 Connect_cableOkMaster = 0;
476                                 print(" Master ");
477                         }
478                         printHex( byte );
479                         print( NL );
480
481                         // Signal that the command should wait for a SYN again
482                         return 1;
483                 }
484                 else
485                 {
486                         // Check which side of the chain
487                         if ( uart_num == UART_Slave )
488                         {
489                                 Connect_cableChecksSlave++;
490                         }
491                         else
492                         {
493                                 Connect_cableChecksMaster++;
494                         }
495                 }
496         }
497
498         // If cable check was successful, set cable ok
499         if ( *pending_bytes == 0 )
500         {
501                 if ( uart_num == UART_Slave )
502                 {
503                         Connect_cableOkSlave = 1;
504                 }
505                 else
506                 {
507                         Connect_cableOkMaster = 1;
508                 }
509         }
510
511         if ( Connect_debug )
512         {
513                 dbug_msg("CABLECHECK RECEIVE - ");
514                 printHex( byte );
515                 print(" ");
516                 printHex( *pending_bytes );
517                 print( NL );
518         }
519
520         // Check whether the cable check has finished
521         return *pending_bytes == 0 ? 1 : 0;
522 }
523
524 uint8_t Connect_receive_IdRequest( uint8_t byte, uint16_t *pending_bytes, uint8_t uart_num )
525 {
526         dbug_print("IdRequest");
527         // Check the directionality
528         if ( uart_num == UART_Master )
529         {
530                 erro_print("Invalid IdRequest direction...");
531         }
532
533         // Check if master, begin IdEnumeration
534         if ( Connect_master )
535         {
536                 // The first device is always id 1
537                 // Id 0 is reserved for the master
538                 Connect_send_IdEnumeration( 1 );
539         }
540         // Propagate IdRequest
541         else
542         {
543                 Connect_send_IdRequest();
544         }
545
546         return 1;
547 }
548
549 uint8_t Connect_receive_IdEnumeration( uint8_t id, uint16_t *pending_bytes, uint8_t uart_num )
550 {
551         dbug_print("IdEnumeration");
552         // Check the directionality
553         if ( uart_num == UART_Slave )
554         {
555                 erro_print("Invalid IdEnumeration direction...");
556         }
557
558         // Set the device id
559         Connect_id = id;
560
561         // Send reponse back to master
562         Connect_send_IdReport( id );
563
564         // Propogate next Id if the connection is ok
565         if ( Connect_cableOkSlave )
566         {
567                 Connect_send_IdEnumeration( id + 1 );
568         }
569
570         return 1;
571 }
572
573 uint8_t Connect_receive_IdReport( uint8_t id, uint16_t *pending_bytes, uint8_t uart_num )
574 {
575         dbug_print("IdReport");
576         // Check the directionality
577         if ( uart_num == UART_Master )
578         {
579                 erro_print("Invalid IdRequest direction...");
580         }
581
582         // Track Id response if master
583         if ( Connect_master )
584         {
585                 info_msg("Id Reported: ");
586                 printHex( id );
587                 print( NL );
588
589                 // Check if this is the highest ID
590                 if ( id > Connect_maxId )
591                         Connect_maxId = id;
592                 return 1;
593         }
594         // Propagate id if yet another slave
595         else
596         {
597                 Connect_send_IdReport( id );
598         }
599
600         return 1;
601 }
602
603 // - Scan Code Variables -
604 TriggerGuide Connect_receive_ScanCodeBuffer;
605 uint8_t Connect_receive_ScanCodeBufferPos;
606 uint8_t Connect_receive_ScanCodeDeviceId;
607
608 uint8_t Connect_receive_ScanCode( uint8_t byte, uint16_t *pending_bytes, uint8_t uart_num )
609 {
610         // Check the directionality
611         if ( uart_num == UART_Master )
612         {
613                 erro_print("Invalid ScanCode direction...");
614         }
615
616         // Master node, trigger scan codes
617         if ( Connect_master ) switch ( (*pending_bytes)-- )
618         {
619         // Byte count always starts at 0xFFFF
620         case 0xFFFF: // Device Id
621                 Connect_receive_ScanCodeDeviceId = byte;
622                 break;
623
624         case 0xFFFE: // Number of TriggerGuides in bytes (byte * 3)
625                 *pending_bytes = byte * sizeof( TriggerGuide );
626                 Connect_receive_ScanCodeBufferPos = 0;
627                 break;
628
629         default:
630                 // Set the specific TriggerGuide entry
631                 ((uint8_t*)&Connect_receive_ScanCodeBuffer)[ Connect_receive_ScanCodeBufferPos++ ] = byte;
632
633                 // Reset the BufferPos if higher than sizeof TriggerGuide
634                 // And send the TriggerGuide to the Macro Module
635                 if ( Connect_receive_ScanCodeBufferPos >= sizeof( TriggerGuide ) )
636                 {
637                         Connect_receive_ScanCodeBufferPos = 0;
638
639                         // Adjust ScanCode offset
640                         if ( Connect_receive_ScanCodeDeviceId > 0 )
641                         {
642                                 // Check if this node is too large
643                                 if ( Connect_receive_ScanCodeDeviceId >= InterconnectNodeMax )
644                                 {
645                                         warn_msg("Not enough interconnect layout nodes configured: ");
646                                         printHex( Connect_receive_ScanCodeDeviceId );
647                                         print( NL );
648                                         break;
649                                 }
650
651                                 // This variable is in generatedKeymaps.h
652                                 extern uint8_t InterconnectOffsetList[];
653                                 Connect_receive_ScanCodeBuffer.scanCode = Connect_receive_ScanCodeBuffer.scanCode + InterconnectOffsetList[ Connect_receive_ScanCodeDeviceId - 1 ];
654                         }
655
656                         // ScanCode receive debug
657                         if ( Connect_debug )
658                         {
659                                 dbug_msg("");
660                                 printHex( Connect_receive_ScanCodeBuffer.type );
661                                 print(" ");
662                                 printHex( Connect_receive_ScanCodeBuffer.state );
663                                 print(" ");
664                                 printHex( Connect_receive_ScanCodeBuffer.scanCode );
665                                 print( NL );
666                         }
667
668                         // Send ScanCode to macro module
669                         Macro_interconnectAdd( &Connect_receive_ScanCodeBuffer );
670                 }
671
672                 break;
673         }
674         // Propagate ScanCode packet
675         // XXX It would be safer to buffer the scancodes first, before transmitting the packet -Jacob
676         //     The current method is the more efficient/aggressive, but could cause issues if there were errors during transmission
677         else switch ( (*pending_bytes)-- )
678         {
679         // Byte count always starts at 0xFFFF
680         case 0xFFFF: // Device Id
681         {
682                 Connect_receive_ScanCodeDeviceId = byte;
683
684                 // Lock the master Tx buffer
685                 uart_lockTx( UART_Master );
686
687                 // Send header + Id byte
688                 uint8_t header[] = { 0x16, 0x01, ScanCode, byte };
689                 Connect_addBytes( header, sizeof( header ), UART_Master );
690                 break;
691         }
692         case 0xFFFE: // Number of TriggerGuides in bytes
693                 *pending_bytes = byte * sizeof( TriggerGuide );
694                 Connect_receive_ScanCodeBufferPos = 0;
695
696                 // Pass through byte
697                 Connect_addBytes( &byte, 1, UART_Master );
698                 break;
699
700         default:
701                 // Pass through byte
702                 Connect_addBytes( &byte, 1, UART_Master );
703
704                 // Unlock Tx Buffer after sending last byte
705                 if ( *pending_bytes == 0 )
706                         uart_unlockTx( UART_Master );
707                 break;
708         }
709
710         // Check whether the scan codes have finished sending
711         return *pending_bytes == 0 ? 1 : 0;
712 }
713
714 uint8_t Connect_receive_Animation( uint8_t byte, uint16_t *pending_bytes, uint8_t uart_num )
715 {
716         dbug_print("Animation");
717         return 1;
718 }
719
720 // - Remote Capability Variables -
721 #define Connect_receive_RemoteCapabilityMaxArgs 5 // XXX Calculate the max using kll
722 RemoteCapabilityCommand Connect_receive_RemoteCapabilityBuffer;
723 uint8_t Connect_receive_RemoteCapabilityArgs[Connect_receive_RemoteCapabilityMaxArgs];
724
725 uint8_t Connect_receive_RemoteCapability( uint8_t byte, uint16_t *pending_bytes, uint8_t uart_num )
726 {
727         // Check which byte in the packet we are at
728         switch ( (*pending_bytes)-- )
729         {
730         // Byte count always starts at 0xFFFF
731         case 0xFFFF: // Device Id
732                 Connect_receive_RemoteCapabilityBuffer.id = byte;
733                 break;
734
735         case 0xFFFE: // Capability Index
736                 Connect_receive_RemoteCapabilityBuffer.capabilityIndex = byte;
737                 break;
738
739         case 0xFFFD: // State
740                 Connect_receive_RemoteCapabilityBuffer.state = byte;
741                 break;
742
743         case 0xFFFC: // StateType
744                 Connect_receive_RemoteCapabilityBuffer.stateType = byte;
745                 break;
746
747         case 0xFFFB: // Number of args
748                 Connect_receive_RemoteCapabilityBuffer.numArgs = byte;
749                 *pending_bytes = byte;
750                 break;
751
752         default:     // Args (# defined by previous byte)
753                 Connect_receive_RemoteCapabilityArgs[
754                         Connect_receive_RemoteCapabilityBuffer.numArgs - *pending_bytes + 1
755                 ] = byte;
756
757                 // If entire packet has been fully received
758                 if ( *pending_bytes == 0 )
759                 {
760                         // Determine if this is the node to run the capability on
761                         // Conditions: Matches or broadcast (0xFF)
762                         if ( Connect_receive_RemoteCapabilityBuffer.id == 0xFF
763                                 || Connect_receive_RemoteCapabilityBuffer.id == Connect_id )
764                         {
765                                 extern const Capability CapabilitiesList[]; // See generatedKeymap.h
766                                 void (*capability)(uint8_t, uint8_t, uint8_t*) = (void(*)(uint8_t, uint8_t, uint8_t*))(
767                                         CapabilitiesList[ Connect_receive_RemoteCapabilityBuffer.capabilityIndex ].func
768                                 );
769                                 capability(
770                                         Connect_receive_RemoteCapabilityBuffer.state,
771                                         Connect_receive_RemoteCapabilityBuffer.stateType,
772                                         &Connect_receive_RemoteCapabilityArgs[2]
773                                 );
774                         }
775
776                         // If this is not the correct node, keep sending it in the same direction (doesn't matter if more nodes exist)
777                         // or if this is a broadcast
778                         if ( Connect_receive_RemoteCapabilityBuffer.id == 0xFF
779                                 || Connect_receive_RemoteCapabilityBuffer.id != Connect_id )
780                         {
781                                 // Prepare outgoing packet
782                                 Connect_receive_RemoteCapabilityBuffer.command = RemoteCapability;
783
784                                 // Send to the other UART (not the one receiving the packet from
785                                 uint8_t uart_direction = uart_num == UART_Master ? UART_Slave : UART_Master;
786
787                                 // Lock Tx UART
788                                 switch ( uart_direction )
789                                 {
790                                 case UART_Master: uart_lockTx( UART_Master ); break;
791                                 case UART_Slave:  uart_lockTx( UART_Slave );  break;
792                                 }
793
794                                 // Send header
795                                 uint8_t header[] = { 0x16, 0x01 };
796                                 Connect_addBytes( header, sizeof( header ), uart_direction );
797
798                                 // Send Remote Capability and arguments
799                                 Connect_addBytes( (uint8_t*)&Connect_receive_RemoteCapabilityBuffer, sizeof( RemoteCapabilityCommand ), uart_direction );
800                                 Connect_addBytes( Connect_receive_RemoteCapabilityArgs, Connect_receive_RemoteCapabilityBuffer.numArgs, uart_direction );
801
802                                 // Unlock Tx UART
803                                 switch ( uart_direction )
804                                 {
805                                 case UART_Master: uart_unlockTx( UART_Master ); break;
806                                 case UART_Slave:  uart_unlockTx( UART_Slave );  break;
807                                 }
808                         }
809                 }
810                 break;
811         }
812
813         // Check whether the scan codes have finished sending
814         return *pending_bytes == 0 ? 1 : 0;
815 }
816
817
818 // Baud Rate
819 // NOTE: If finer baud adjustment is needed see UARTx_C4 -> BRFA in the datasheet
820 uint16_t Connect_baud = UARTConnectBaud_define; // Max setting of 8191
821 uint16_t Connect_baudFine = UARTConnectBaudFine_define;
822
823 // Connect receive function lookup
824 void *Connect_receiveFunctions[] = {
825         Connect_receive_CableCheck,
826         Connect_receive_IdRequest,
827         Connect_receive_IdEnumeration,
828         Connect_receive_IdReport,
829         Connect_receive_ScanCode,
830         Connect_receive_Animation,
831         Connect_receive_RemoteCapability,
832 };
833
834
835
836 // ----- Functions -----
837
838 // Resets the state of the UART buffers and state variables
839 void Connect_reset()
840 {
841         // Reset Rx
842         memset( (void*)uart_rx_status, 0, sizeof( UARTStatusRx ) * UART_Num_Interfaces );
843
844         // Reset Tx
845         memset( (void*)uart_tx_buf,    0, sizeof( UARTRingBuf )  * UART_Num_Interfaces );
846         memset( (void*)uart_tx_status, 0, sizeof( UARTStatusTx ) * UART_Num_Interfaces );
847
848         // Set Rx/Tx buffers as ready
849         for ( uint8_t inter = 0; inter < UART_Num_Interfaces; inter++ )
850         {
851                 uart_tx_status[ inter ].status = UARTStatus_Ready;
852                 uart_rx_buf[ inter ].last_read = UART_Buffer_Size;
853         }
854 }
855
856
857 // Setup connection to other side
858 // - Only supports a single slave and master
859 // - If USB has been initiallized at this point, this side is the master
860 // - If both sides assert master, flash error leds
861 void Connect_setup( uint8_t master )
862 {
863         // Indication that UARTs are not ready
864         uarts_configured = 0;
865
866         // Register Connect CLI dictionary
867         CLI_registerDictionary( uartConnectCLIDict, uartConnectCLIDictName );
868
869         // Check if master
870         Connect_master = master;
871         if ( Connect_master )
872                 Connect_id = 0; // 0x00 is always the master Id
873
874         // UART0 setup
875         // UART1 setup
876         // Setup the the UART interface for keyboard data input
877         SIM_SCGC4 |= SIM_SCGC4_UART0; // Disable clock gating
878         SIM_SCGC4 |= SIM_SCGC4_UART1; // Disable clock gating
879
880         // Pin Setup for UART0 / UART1
881         PORTA_PCR1 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(2); // RX Pin
882         PORTA_PCR2 = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(2); // TX Pin
883         PORTE_PCR0 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); // RX Pin
884         PORTE_PCR1 = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); // TX Pin
885
886         // Baud Rate setting
887         UART0_BDH = (uint8_t)(Connect_baud >> 8);
888         UART0_BDL = (uint8_t)Connect_baud;
889         UART0_C4  = Connect_baudFine;
890         UART1_BDH = (uint8_t)(Connect_baud >> 8);
891         UART1_BDL = (uint8_t)Connect_baud;
892         UART1_C4  = Connect_baudFine;
893
894         // 8 bit, Even Parity, Idle Character bit after stop
895         // NOTE: For 8 bit with Parity you must enable 9 bit transmission (pg. 1065)
896         //       You only need to use UART0_D for 8 bit reading/writing though
897         // UART_C1_M UART_C1_PE UART_C1_PT UART_C1_ILT
898         UART0_C1 = UART_C1_M | UART_C1_PE | UART_C1_ILT;
899         UART1_C1 = UART_C1_M | UART_C1_PE | UART_C1_ILT;
900
901         // Only using Tx Fifos
902         UART0_PFIFO = UART_PFIFO_TXFE;
903         UART1_PFIFO = UART_PFIFO_TXFE;
904
905         // Setup DMA clocks
906         SIM_SCGC6 |= SIM_SCGC6_DMAMUX;
907         SIM_SCGC7 |= SIM_SCGC7_DMA;
908
909         // Start with channels disabled first
910         DMAMUX0_CHCFG0 = 0;
911         DMAMUX0_CHCFG1 = 0;
912
913         // Configure DMA channels
914         //DMA_DSR_BCR0 |= DMA_DSR_BCR_DONE_MASK; // TODO What's this?
915         DMA_TCD0_CSR = 0;
916         DMA_TCD1_CSR = 0;
917
918         // Default control register
919         DMA_CR = 0;
920
921         // DMA Priority
922         DMA_DCHPRI0 = 0; // Ch 0, priority 0
923         DMA_DCHPRI1 = 1; // ch 1, priority 1
924
925         // Clear error interrupts
926         DMA_EEI = 0;
927
928         // Setup TCD
929         DMA_TCD0_SADDR = (uint32_t*)&UART0_D;
930         DMA_TCD1_SADDR = (uint32_t*)&UART1_D;
931         DMA_TCD0_SOFF = 0;
932         DMA_TCD1_SOFF = 0;
933
934         // No modulo, 8-bit transfer size
935         DMA_TCD0_ATTR = DMA_TCD_ATTR_SMOD(0) | DMA_TCD_ATTR_SSIZE(0) | DMA_TCD_ATTR_DMOD(0) | DMA_TCD_ATTR_DSIZE(0);
936         DMA_TCD1_ATTR = DMA_TCD_ATTR_SMOD(0) | DMA_TCD_ATTR_SSIZE(0) | DMA_TCD_ATTR_DMOD(0) | DMA_TCD_ATTR_DSIZE(0);
937
938         // One byte transferred at a time
939         DMA_TCD0_NBYTES_MLNO = 1;
940         DMA_TCD1_NBYTES_MLNO = 1;
941
942         // Source address does not change
943         DMA_TCD0_SLAST = 0;
944         DMA_TCD1_SLAST = 0;
945
946         // Destination buffer
947         DMA_TCD0_DADDR = (uint32_t*)uart_rx_buf[0].buffer;
948         DMA_TCD1_DADDR = (uint32_t*)uart_rx_buf[1].buffer;
949
950         // Incoming byte, increment by 1 in the rx buffer
951         DMA_TCD0_DOFF = 1;
952         DMA_TCD1_DOFF = 1;
953
954         // Single major loop, must be the same value
955         DMA_TCD0_CITER_ELINKNO = UART_Buffer_Size;
956         DMA_TCD1_CITER_ELINKNO = UART_Buffer_Size;
957         DMA_TCD0_BITER_ELINKNO = UART_Buffer_Size;
958         DMA_TCD1_BITER_ELINKNO = UART_Buffer_Size;
959
960         // Reset buffer when full
961         DMA_TCD0_DLASTSGA = -( UART_Buffer_Size );
962         DMA_TCD1_DLASTSGA = -( UART_Buffer_Size );
963
964         // Enable DMA channels
965         DMA_ERQ |= DMA_ERQ_ERQ0 | DMA_ERQ_ERQ1;
966
967         // Setup DMA channel routing
968         DMAMUX0_CHCFG0 = DMAMUX_ENABLE | DMAMUX_SOURCE_UART0_RX;
969         DMAMUX0_CHCFG1 = DMAMUX_ENABLE | DMAMUX_SOURCE_UART1_RX;
970
971         // Enable DMA requests (requires Rx interrupts)
972         UART0_C5 = UART_C5_RDMAS;
973         UART1_C5 = UART_C5_RDMAS;
974
975         // TX Enabled, RX Enabled, RX Interrupt Enabled
976         UART0_C2 = UART_C2_TE | UART_C2_RE | UART_C2_RIE;
977         UART1_C2 = UART_C2_TE | UART_C2_RE | UART_C2_RIE;
978
979         // Add interrupts to the vector table
980         NVIC_ENABLE_IRQ( IRQ_UART0_STATUS );
981         NVIC_ENABLE_IRQ( IRQ_UART1_STATUS );
982
983         // UARTs are now ready to go
984         uarts_configured = 1;
985
986         // Reset the state of the UART variables
987         Connect_reset();
988 }
989
990
991 #define DMA_BUF_POS( x, pos ) \
992         case x: \
993                 pos = DMA_TCD##x##_CITER_ELINKNO; \
994                 break
995 void Connect_rx_process( uint8_t uartNum )
996 {
997         // Determine current position to read until
998         uint16_t bufpos = 0;
999         switch ( uartNum )
1000         {
1001         DMA_BUF_POS( 0, bufpos );
1002         DMA_BUF_POS( 1, bufpos );
1003         }
1004
1005         // Process each of the new bytes
1006         // Even if we receive more bytes during processing, wait until the next check so we don't starve other tasks
1007         while ( bufpos != uart_rx_buf[ uartNum ].last_read )
1008         {
1009                 // If the last_read byte is at the buffer edge, roll back to beginning
1010                 if ( uart_rx_buf[ uartNum ].last_read == 0 )
1011                 {
1012                         uart_rx_buf[ uartNum ].last_read = UART_Buffer_Size;
1013
1014                         // Check to see if we're at the boundary
1015                         if ( bufpos == UART_Buffer_Size )
1016                                 break;
1017                 }
1018
1019                 // Read the byte out of Rx DMA buffer
1020                 uint8_t byte = uart_rx_buf[ uartNum ].buffer[ UART_Buffer_Size - uart_rx_buf[ uartNum ].last_read-- ];
1021
1022                 if ( Connect_debug )
1023                 {
1024                         printHex( byte );
1025                         print(" ");
1026                 }
1027
1028                 // Process UART byte
1029                 switch ( uart_rx_status[ uartNum ].status )
1030                 {
1031                 // Every packet must start with a SYN / 0x16
1032                 case UARTStatus_Wait:
1033                         if ( Connect_debug )
1034                         {
1035                                 print(" Wait ");
1036                         }
1037                         uart_rx_status[ uartNum ].status = byte == 0x16 ? UARTStatus_SYN : UARTStatus_Wait;
1038                         break;
1039
1040                 // After a SYN, there must be a SOH / 0x01
1041                 case UARTStatus_SYN:
1042                         if ( Connect_debug )
1043                         {
1044                                 print(" SYN ");
1045                         }
1046                         uart_rx_status[ uartNum ].status = byte == 0x01 ? UARTStatus_SOH : UARTStatus_Wait;
1047                         break;
1048
1049                 // After a SOH the packet structure may diverge a bit
1050                 // This is the packet type field (refer to the Command enum)
1051                 // For very small packets (e.g. IdRequest) this is all that's required to take action
1052                 case UARTStatus_SOH:
1053                 {
1054                         if ( Connect_debug )
1055                         {
1056                                 print(" SOH ");
1057                         }
1058
1059                         // Check if this is actually a reserved CMD 0x16 (Error condition)
1060                         if ( byte == Command_SYN )
1061                         {
1062                                 uart_rx_status[ uartNum ].status = UARTStatus_SYN;
1063                                 break;
1064                         }
1065
1066                         // Otherwise process the command
1067                         if ( byte < Command_TOP )
1068                         {
1069                                 uart_rx_status[ uartNum ].status = UARTStatus_Command;
1070                                 uart_rx_status[ uartNum ].command = byte;
1071                                 uart_rx_status[ uartNum ].bytes_waiting = 0xFFFF;
1072                         }
1073                         // Invalid packet type, ignore
1074                         else
1075                         {
1076                                 uart_rx_status[ uartNum ].status = UARTStatus_Wait;
1077                         }
1078
1079                         // Check if this is a very short packet
1080                         switch ( uart_rx_status[ uartNum ].command )
1081                         {
1082                         case IdRequest:
1083                                 Connect_receive_IdRequest( 0, (uint16_t*)&uart_rx_status[ uartNum ].bytes_waiting, uartNum );
1084                                 uart_rx_status[ uartNum ].status = UARTStatus_Wait;
1085                                 break;
1086
1087                         default:
1088                                 if ( Connect_debug )
1089                                 {
1090                                         print(" ### ");
1091                                         printHex( uart_rx_status[ uartNum ].command );
1092                                 }
1093                                 break;
1094                         }
1095                         break;
1096                 }
1097
1098                 // After the packet type has been deciphered do Command specific processing
1099                 // Until the Command has received all the bytes it requires the UART buffer stays in this state
1100                 case UARTStatus_Command:
1101                 {
1102                         if ( Connect_debug )
1103                         {
1104                                 print(" CMD ");
1105                         }
1106                         /* Call specific UARTConnect command receive function */
1107                         uint8_t (*rcvFunc)(uint8_t, uint16_t(*), uint8_t) = (uint8_t(*)(uint8_t, uint16_t(*), uint8_t))(Connect_receiveFunctions[ uart_rx_status[ uartNum ].command ]);
1108                         if ( rcvFunc( byte, (uint16_t*)&uart_rx_status[ uartNum ].bytes_waiting, uartNum ) )
1109                                 uart_rx_status[ uartNum ].status = UARTStatus_Wait;
1110                         break;
1111                 }
1112
1113                 // Unknown status, should never get here
1114                 default:
1115                         erro_msg("Invalid UARTStatus...");
1116                         uart_rx_status[ uartNum ].status = UARTStatus_Wait;
1117                         continue;
1118                 }
1119
1120                 if ( Connect_debug )
1121                 {
1122                         print( NL );
1123                 }
1124         }
1125 }
1126
1127
1128 // Scan for updates in the master/slave
1129 // - Interrupts will deal with most input functions
1130 // - Used to send queries
1131 // - SyncEvent is sent immediately once the current command is sent
1132 // - SyncEvent is also blocking until sent
1133 void Connect_scan()
1134 {
1135         // Check if initially configured as a slave and usb comes up
1136         // Then reconfigure as a master
1137         if ( !Connect_master && Output_Available && !Connect_override )
1138         {
1139                 Connect_setup( Output_Available );
1140         }
1141
1142         // Limit how often we do cable checks
1143         uint32_t time_compare = 0x7FF; // Must be all 1's, 0x3FF is valid, 0x4FF is not
1144         uint32_t current_time = systick_millis_count;
1145         if ( Connect_lastCheck != current_time
1146                 && ( current_time & time_compare ) == time_compare
1147         )
1148         {
1149                 // Make sure we don't double check if the clock speed is too high
1150                 Connect_lastCheck = current_time;
1151
1152                 // Send a cable check command of 2 bytes
1153                 Connect_send_CableCheck( UARTConnectCableCheckLength_define );
1154
1155                 // If this is a slave, and we don't have an id yeth
1156                 // Don't bother sending if there are cable issues
1157                 if ( !Connect_master && Connect_id == 0xFF && Connect_cableOkMaster )
1158                 {
1159                         Connect_send_IdRequest();
1160                 }
1161         }
1162
1163         // Only process commands if uarts have been configured
1164         if ( uarts_configured )
1165         {
1166                 // Check if Tx Buffers are empty and the Tx Ring buffers have data to send
1167                 // This happens if there was previously nothing to send
1168                 if ( uart_tx_buf[ 0 ].items > 0 && UART0_TCFIFO == 0 )
1169                         uart_fillTxFifo( 0 );
1170                 if ( uart_tx_buf[ 1 ].items > 0 && UART1_TCFIFO == 0 )
1171                         uart_fillTxFifo( 1 );
1172
1173                 // Process Rx Buffers
1174                 Connect_rx_process( 0 );
1175                 Connect_rx_process( 1 );
1176         }
1177 }
1178
1179
1180
1181 // ----- CLI Command Functions -----
1182
1183 void cliFunc_connectCmd( char* args )
1184 {
1185         // Parse number from argument
1186         //  NOTE: Only first argument is used
1187         char* arg1Ptr;
1188         char* arg2Ptr;
1189         CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
1190
1191         print( NL );
1192
1193         switch ( numToInt( &arg1Ptr[0] ) )
1194         {
1195         case CableCheck:
1196                 Connect_send_CableCheck( UARTConnectCableCheckLength_define );
1197                 break;
1198
1199         case IdRequest:
1200                 Connect_send_IdRequest();
1201                 break;
1202
1203         case IdEnumeration:
1204                 Connect_send_IdEnumeration( 5 );
1205                 break;
1206
1207         case IdReport:
1208                 Connect_send_IdReport( 8 );
1209                 break;
1210
1211         case ScanCode:
1212         {
1213                 TriggerGuide scanCodes[] = { { 0x00, 0x01, 0x05 }, { 0x00, 0x03, 0x16 } };
1214                 Connect_send_ScanCode( 10, scanCodes, 2 );
1215                 break;
1216         }
1217         case Animation:
1218                 break;
1219
1220         case RemoteCapability:
1221                 // TODO
1222                 break;
1223
1224         case RemoteOutput:
1225                 // TODO
1226                 break;
1227
1228         case RemoteInput:
1229                 // TODO
1230                 break;
1231
1232         default:
1233                 break;
1234         }
1235 }
1236
1237 void cliFunc_connectDbg( char* args )
1238 {
1239         print( NL );
1240         info_msg("Connect Debug Mode Toggle");
1241         Connect_debug = !Connect_debug;
1242 }
1243
1244 void cliFunc_connectIdl( char* args )
1245 {
1246         // Parse number from argument
1247         //  NOTE: Only first argument is used
1248         char* arg1Ptr;
1249         char* arg2Ptr;
1250         CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
1251
1252         print( NL );
1253         info_msg("Sending Sync Idles...");
1254
1255         uint8_t count = numToInt( &arg1Ptr[0] );
1256         // Default to 2 idles
1257         if ( count == 0 )
1258                 count = 2;
1259
1260         Connect_send_Idle( count );
1261 }
1262
1263 void cliFunc_connectLst( char* args )
1264 {
1265         const char *Command_strs[] = {
1266                 "CableCheck",
1267                 "IdRequest",
1268                 "IdEnumeration",
1269                 "IdReport",
1270                 "ScanCode",
1271                 "Animation",
1272                 "RemoteCapability",
1273                 "RemoteOutput",
1274                 "RemoteInput",
1275         };
1276
1277         print( NL );
1278         info_msg("List of UARTConnect commands");
1279         for ( uint8_t cmd = 0; cmd < Command_TOP; cmd++ )
1280         {
1281                 print( NL );
1282                 printInt8( cmd );
1283                 print(" - ");
1284                 dPrint( (char*)Command_strs[ cmd ] );
1285         }
1286 }
1287
1288 void cliFunc_connectMst( char* args )
1289 {
1290         // Parse number from argument
1291         //  NOTE: Only first argument is used
1292         char* arg1Ptr;
1293         char* arg2Ptr;
1294         CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
1295
1296         print( NL );
1297
1298         // Set override
1299         Connect_override = 1;
1300
1301         switch ( arg1Ptr[0] )
1302         {
1303         // Disable override
1304         case 'd':
1305         case 'D':
1306                 Connect_override = 0;
1307         case 's':
1308         case 'S':
1309                 info_msg("Setting device as slave.");
1310                 Connect_master = 0;
1311                 Connect_id = 0xFF;
1312                 break;
1313
1314         case 'm':
1315         case 'M':
1316         default:
1317                 info_msg("Setting device as master.");
1318                 Connect_master = 1;
1319                 Connect_id = 0;
1320                 break;
1321         }
1322 }
1323
1324 void cliFunc_connectRst( char* args )
1325 {
1326         print( NL );
1327         info_msg("Resetting UARTConnect state...");
1328         Connect_reset();
1329
1330         // Reset node id
1331         Connect_id = 0xFF;
1332 }
1333
1334 void cliFunc_connectSts( char* args )
1335 {
1336         print( NL );
1337         info_msg("UARTConnect Status");
1338         print( NL "Device Type:\t" );
1339         print( Connect_master ? "Master" : "Slave" );
1340         print( NL "Device Id:\t" );
1341         printHex( Connect_id );
1342         print( NL "Max Id:\t" );
1343         printHex( Connect_maxId );
1344         print( NL "Master <=" NL "\tStatus:\t");
1345         printHex( Connect_cableOkMaster );
1346         print( NL "\tFaults:\t");
1347         printHex32( Connect_cableFaultsMaster );
1348         print("/");
1349         printHex32( Connect_cableChecksMaster );
1350         print( NL "\tRx:\t");
1351         printHex( uart_rx_status[UART_Master].status );
1352         print( NL "\tTx:\t");
1353         printHex( uart_tx_status[UART_Master].status );
1354         print( NL "Slave <=" NL "\tStatus:\t");
1355         printHex( Connect_cableOkSlave );
1356         print( NL "\tFaults:\t");
1357         printHex32( Connect_cableFaultsSlave );
1358         print("/");
1359         printHex32( Connect_cableChecksSlave );
1360         print( NL "\tRx:\t");
1361         printHex( uart_rx_status[UART_Slave].status );
1362         print( NL "\tTx:\t");
1363         printHex( uart_tx_status[UART_Slave].status );
1364 }
1365