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