-
Notifications
You must be signed in to change notification settings - Fork 271
/
Copy pathconnect_scan.c
1497 lines (1236 loc) · 38.2 KB
/
connect_scan.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (C) 2014-2019 by Jacob Alexander
*
* This file is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This file is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this file. If not, see <http://www.gnu.org/licenses/>.
*/
// ----- Includes -----
// Compiler Includes
#include <Lib/ScanLib.h>
// Project Includes
#include <cli.h>
#include <kll_defs.h>
#include <latency.h>
#include <led.h>
#include <print.h>
#include <macro.h>
// Local Includes
#include "connect_scan.h"
// ----- Defines -----
#define UART_Num_Interfaces 2
#define UART_Master 1
#define UART_Slave 0
#define UART_Buffer_Size UARTConnectBufSize_define
// ----- Macros -----
// Macro for popping from Tx ring buffer
#define uart_fillTxFifo( uartNum ) \
{ \
uint8_t fifoSize = ( ( UART##uartNum##_PFIFO & UART_PFIFO_TXFIFOSIZE ) >> 2 ); \
if ( fifoSize == 0 ) \
fifoSize = 1; \
if ( Connect_debug ) \
{ \
print( "TxFIFO " #uartNum " - " ); \
printHex( fifoSize ); \
print("/"); \
printHex( UART##uartNum##_TCFIFO ); \
print("/"); \
printHex( uart_tx_buf[ uartNum ].items ); \
print( NL ); \
} \
/* XXX Doesn't work well */ \
/* while ( UART##uartNum##_TCFIFO < fifoSize ) */ \
/* More reliable, albeit slower */ \
fifoSize -= UART##uartNum##_TCFIFO; \
while ( fifoSize-- != 0 ) \
{ \
if ( uart_tx_buf[ uartNum ].items == 0 ) \
break; \
UART##uartNum##_D = uart_tx_buf[ uartNum ].buffer[ uart_tx_buf[ uartNum ].head++ ]; \
uart_tx_buf[ uartNum ].items--; \
if ( uart_tx_buf[ uartNum ].head >= UART_Buffer_Size ) \
uart_tx_buf[ uartNum ].head = 0; \
} \
}
// Macros for locking/unlock Tx buffers
#define uart_lockTx( uartNum ) \
{ \
/* First, secure place in line for the resource */ \
while ( uart_tx_status[ uartNum ].lock ); \
uart_tx_status[ uartNum ].lock = 1; \
/* Next, wait unit the UART is ready */ \
while ( uart_tx_status[ uartNum ].status != UARTStatus_Ready ); \
uart_tx_status[ uartNum ].status = UARTStatus_Wait; \
}
#define uart_lockBothTx( uartNum1, uartNum2 ) \
{ \
/* First, secure place in line for the resource */ \
while ( uart_tx_status[ uartNum1 ].lock || uart_tx_status[ uartNum2 ].lock ); \
uart_tx_status[ uartNum1 ].lock = 1; \
uart_tx_status[ uartNum2 ].lock = 1; \
/* Next, wait unit the UARTs are ready */ \
while ( uart_tx_status[ uartNum1 ].status != UARTStatus_Ready || uart_tx_status[ uartNum2 ].status != UARTStatus_Ready ); \
uart_tx_status[ uartNum1 ].status = UARTStatus_Wait; \
uart_tx_status[ uartNum2 ].status = UARTStatus_Wait; \
}
#define uart_unlockTx( uartNum ) \
{ \
/* Ready the UART */ \
uart_tx_status[ uartNum ].status = UARTStatus_Ready; \
/* Unlock the resource */ \
uart_tx_status[ uartNum ].lock = 0; \
}
// ----- Function Declarations -----
// CLI Functions
void cliFunc_connectCmd ( char *args );
void cliFunc_connectDbg ( char *args );
void cliFunc_connectIdl ( char *args );
void cliFunc_connectLst ( char *args );
void cliFunc_connectMst ( char *args );
void cliFunc_connectRst ( char *args );
void cliFunc_connectSts ( char *args );
// ----- Structs -----
typedef struct UARTRingBuf {
uint8_t head;
uint8_t tail;
uint8_t items;
uint8_t buffer[UART_Buffer_Size];
} UARTRingBuf;
typedef struct UARTDMABuf {
uint8_t buffer[UART_Buffer_Size];
uint16_t last_read;
} UARTDMABuf;
typedef struct UARTStatusRx {
UARTStatus status;
Command command;
uint16_t bytes_waiting;
} UARTStatusRx;
typedef struct UARTStatusTx {
UARTStatus status;
uint8_t lock;
} UARTStatusTx;
// ----- Variables -----
// Connect Module command dictionary
CLIDict_Entry( connectCmd, "Sends a command via UART Connect, first arg is which uart, next arg is the command, rest are the arguments." );
CLIDict_Entry( connectDbg, "Toggle UARTConnect debug mode." );
CLIDict_Entry( connectIdl, "Sends N number of Idle commands, 2 is the default value, and should be sufficient in most cases." );
CLIDict_Entry( connectLst, "Lists available UARTConnect commands and index id" );
CLIDict_Entry( connectMst, "Sets the device as master. Use argument of s to set as slave." );
CLIDict_Entry( connectRst, "Resets both Rx and Tx connect buffers and state variables." );
CLIDict_Entry( connectSts, "UARTConnect status." );
CLIDict_Def( uartConnectCLIDict, "UARTConnect Module Commands" ) = {
CLIDict_Item( connectCmd ),
CLIDict_Item( connectDbg ),
CLIDict_Item( connectIdl ),
CLIDict_Item( connectLst ),
CLIDict_Item( connectMst ),
CLIDict_Item( connectRst ),
CLIDict_Item( connectSts ),
{ 0, 0, 0 } // Null entry for dictionary end
};
// Latency measurement resource
static uint8_t connectLatencyResource;
static uint16_t Connect_LastCurrentValue;
// -- Connect Device Id Variables --
uint8_t Connect_id = 255; // Invalid, unset
uint8_t Connect_master = 0;
uint8_t Connect_maxId = 0;
// -- Control Variables --
uint32_t Connect_lastCheck = 0; // Cable Check scheduler
uint8_t Connect_debug = 0; // Set 1 for debug
uint8_t Connect_override = 0; // Prevents master from automatically being set
volatile uint8_t uarts_configured = 0;
// -- Rx Variables --
volatile UARTDMABuf uart_rx_buf[UART_Num_Interfaces];
volatile UARTStatusRx uart_rx_status[UART_Num_Interfaces];
// -- Tx Variables --
UARTRingBuf uart_tx_buf [UART_Num_Interfaces];
UARTStatusTx uart_tx_status[UART_Num_Interfaces];
// -- Ring Buffer Convenience Functions --
void Connect_addBytes( uint8_t *buffer, uint8_t count, uint8_t uart )
{
// Too big to fit into buffer
if ( count > UART_Buffer_Size )
{
erro_print("Too big of a command to fit into the buffer...");
return;
}
// Invalid UART
if ( uart >= UART_Num_Interfaces )
{
erro_printNL("Invalid UART to send from...");
return;
}
// Delay UART copy until there's some space left
while ( uart_tx_buf[ uart ].items + count > UART_Buffer_Size )
{
warn_print("Too much data to send on UART");
printInt8( uart );
print( ", waiting..." NL );
delay_ms( 1 );
// FIXME Buffer will not drain here....
}
// Append data to ring buffer
for ( uint8_t c = 0; c < count; c++ )
{
if ( Connect_debug )
{
printHex( buffer[ c ] );
print(" +");
printInt8( uart );
print( NL );
}
uart_tx_buf[ uart ].buffer[ uart_tx_buf[ uart ].tail++ ] = buffer[ c ];
uart_tx_buf[ uart ].items++;
if ( uart_tx_buf[ uart ].tail >= UART_Buffer_Size )
uart_tx_buf[ uart ].tail = 0;
if ( uart_tx_buf[ uart ].head == uart_tx_buf[ uart ].tail )
uart_tx_buf[ uart ].head++;
if ( uart_tx_buf[ uart ].head >= UART_Buffer_Size )
uart_tx_buf[ uart ].head = 0;
}
}
// -- Connect send functions --
// patternLen defines how many bytes should the incrementing pattern have
void Connect_send_CableCheck( uint8_t patternLen )
{
// Wait until the Tx buffers are ready, then lock them
uart_lockBothTx( UART_Master, UART_Slave );
// Prepare header
uint8_t header[] = { Command_SYN, SOH, CableCheck, patternLen };
// Send header
Connect_addBytes( header, sizeof( header ), UART_Master );
Connect_addBytes( header, sizeof( header ), UART_Slave );
// Send 0xD2 (11010010) for each argument
uint8_t value = CABLE_CHECK_ARG;
for ( uint8_t c = 0; c < patternLen; c++ )
{
Connect_addBytes( &value, 1, UART_Master );
Connect_addBytes( &value, 1, UART_Slave );
}
// Release Tx buffers
uart_unlockTx( UART_Master );
uart_unlockTx( UART_Slave );
}
void Connect_send_IdRequest()
{
// Lock master bound Tx
uart_lockTx( UART_Master );
// Prepare header
uint8_t header[] = { Command_SYN, SOH, IdRequest };
// Send header
Connect_addBytes( header, sizeof( header ), UART_Master );
// Unlock Tx
uart_unlockTx( UART_Master );
}
// id is the value the next slave should enumerate as
void Connect_send_IdEnumeration( uint8_t id )
{
// Lock slave bound Tx
uart_lockTx( UART_Slave );
// Prepare header
uint8_t header[] = { Command_SYN, SOH, IdEnumeration, id };
// Send header
Connect_addBytes( header, sizeof( header ), UART_Slave );
// Unlock Tx
uart_unlockTx( UART_Slave );
}
// id is the currently assigned id to the slave
void Connect_send_IdReport( uint8_t id )
{
// Lock master bound Tx
uart_lockTx( UART_Master );
// Prepare header
uint8_t header[] = { Command_SYN, SOH, IdReport, id };
// Send header
Connect_addBytes( header, sizeof( header ), UART_Master );
// Unlock Tx
uart_unlockTx( UART_Master );
}
// id is the currently assigned id to the slave
// scanCodeStateList is an array of [scancode, state]'s (8 bit values)
// numScanCodes is the number of scan codes to parse from array
void Connect_send_ScanCode( uint8_t id, TriggerEvent *scanCodeStateList, uint8_t numScanCodes )
{
// Lock master bound Tx
uart_lockTx( UART_Master );
// Prepare header
uint8_t header[] = { Command_SYN, SOH, ScanCode, id, numScanCodes };
// Send header
Connect_addBytes( header, sizeof( header ), UART_Master );
// Send each of the scan codes
Connect_addBytes( (uint8_t*)scanCodeStateList, numScanCodes * TriggerGuideSize, UART_Master );
// Unlock Tx
uart_unlockTx( UART_Master );
}
// Send a remote capability command using capability index
// This may not be what's expected (especially if the firmware is not the same on each node)
// To broadcast to all slave nodes, set id to 255 instead of a specific id
void Connect_send_RemoteCapability( uint8_t id, uint8_t capabilityIndex, uint8_t state, uint8_t stateType, uint8_t numArgs, uint8_t *args )
{
// Prepare header
uint8_t header[] = { Command_SYN, SOH, RemoteCapability, id, capabilityIndex, state, stateType, numArgs };
// Ignore current id
if ( id == Connect_id )
return;
// Send towards slave node
if ( id > Connect_id )
{
// Lock slave bound Tx
uart_lockTx( UART_Slave );
// Send header
Connect_addBytes( header, sizeof( header ), UART_Slave );
// Send arguments
Connect_addBytes( args, numArgs, UART_Slave );
// Unlock Tx
uart_unlockTx( UART_Slave );
}
// Send towards master node
if ( id < Connect_id || id == 255 )
{
// Lock slave bound Tx
uart_lockTx( UART_Master );
// Send header
Connect_addBytes( header, sizeof( header ), UART_Master );
// Send arguments
Connect_addBytes( args, numArgs, UART_Master );
// Unlock Tx
uart_unlockTx( UART_Master );
}
}
void Connect_send_Idle( uint8_t num )
{
// Wait until the Tx buffers are ready, then lock them
uart_lockBothTx( UART_Slave, UART_Master );
// Send n number of idles to reset link status (if in a bad state)
uint8_t value = Command_SYN;
for ( uint8_t c = 0; c < num; c++ )
{
Connect_addBytes( &value, 1, UART_Master );
Connect_addBytes( &value, 1, UART_Slave );
}
// Release Tx buffers
uart_unlockTx( UART_Master );
uart_unlockTx( UART_Slave );
}
void Connect_send_CurrentEvent( uint16_t current )
{
// Lock master bound Tx
uart_lockTx( UART_Slave );
// Prepare header
uint8_t header[] = { 0x16, 0x01, CurrentEvent, current & 0xFF, (current >> 8) & 0xFF };
// Send header
Connect_addBytes( header, sizeof( header ), UART_Slave );
// Unlock Tx
uart_unlockTx( UART_Slave );
}
// -- Connect receive functions --
// - Cable Check variables -
uint32_t Connect_cableFaultsMaster = 0;
uint32_t Connect_cableFaultsSlave = 0;
uint32_t Connect_cableChecksMaster = 0;
uint32_t Connect_cableChecksSlave = 0;
uint8_t Connect_cableOkMaster = 0;
uint8_t Connect_cableOkSlave = 0;
uint8_t Connect_receive_CableCheck( uint8_t byte, uint16_t *pending_bytes, uint8_t uart_num )
{
// Check if this is the first byte
if ( *pending_bytes == BYTE_COUNT_START )
{
*pending_bytes = byte;
if ( Connect_debug )
{
dbug_print("PENDING SET -> ");
printHex( byte );
print(" ");
printHex( *pending_bytes );
print( NL );
}
}
// Verify byte
else
{
(*pending_bytes)--;
// The argument bytes are always 0xD2 (11010010)
if ( byte != CABLE_CHECK_ARG )
{
warn_printNL("Cable Fault!");
// Check which side of the chain
if ( uart_num == UART_Slave )
{
Connect_cableFaultsSlave++;
Connect_cableOkSlave = 0;
print(" Slave ");
}
else
{
// Lower current requirement during errors
// Half of USB negotiation minimum (50 mA)
// Only if this is not the master node
if ( Connect_id != 0 )
{
Output_update_external_current( 50 );
}
Connect_cableFaultsMaster++;
Connect_cableOkMaster = 0;
print(" Master ");
}
printHex( byte );
print( NL );
// Signal that the command should wait for a SYN again
return 1;
}
else
{
// Check which side of the chain
if ( uart_num == UART_Slave )
{
Connect_cableChecksSlave++;
}
else
{
// If we already have an Id, then set max current again
if ( Connect_id != 255 && Connect_id != 0 )
{
Output_update_external_current( Output_current_available() );
}
Connect_cableChecksMaster++;
}
}
}
// If cable check was successful, set cable ok
if ( *pending_bytes == 0 )
{
if ( uart_num == UART_Slave )
{
Connect_cableOkSlave = 1;
}
else
{
Connect_cableOkMaster = 1;
}
}
if ( Connect_debug )
{
dbug_print("CABLECHECK RECEIVE - ");
printHex( byte );
print(" ");
printHex( *pending_bytes );
print( NL );
}
// Check whether the cable check has finished
return *pending_bytes == 0 ? 1 : 0;
}
uint8_t Connect_receive_IdRequest( uint8_t byte, uint16_t *pending_bytes, uint8_t uart_num )
{
dbug_printNL("IdRequest");
// Check the directionality
if ( uart_num == UART_Master )
{
erro_printNL("Invalid IdRequest direction...");
}
// Check if master, begin IdEnumeration
if ( Connect_master )
{
// The first device is always id 1
// Id 0 is reserved for the master
Connect_send_IdEnumeration( 1 );
}
// Propagate IdRequest
else
{
Connect_send_IdRequest();
}
return 1;
}
uint8_t Connect_receive_IdEnumeration( uint8_t id, uint16_t *pending_bytes, uint8_t uart_num )
{
dbug_printNL("IdEnumeration");
// Check the directionality
if ( uart_num == UART_Slave )
{
erro_printNL("Invalid IdEnumeration direction...");
}
// Set the device id
Connect_id = id;
// Send reponse back to master
Connect_send_IdReport( id );
// Node now enumerated, set current to last received current setting
// Only set if this is not the master node
if ( Connect_id != 0 )
{
Output_update_external_current( Connect_LastCurrentValue );
}
// Propogate next Id if the connection is ok
if ( Connect_cableOkSlave )
{
Connect_send_IdEnumeration( id + 1 );
}
return 1;
}
uint8_t Connect_receive_IdReport( uint8_t id, uint16_t *pending_bytes, uint8_t uart_num )
{
dbug_printNL("IdReport");
// Check the directionality
if ( uart_num == UART_Master )
{
erro_printNL("Invalid IdRequest direction...");
}
// Track Id response if master
if ( Connect_master )
{
info_print("Id Reported: ");
printHex( id );
print( NL );
// Check if this is the highest ID
if ( id > Connect_maxId )
{
Connect_maxId = id;
}
// Send available current
Connect_currentChange( Output_current_available() );
return 1;
}
// Propagate id if yet another slave
else
{
Connect_send_IdReport( id );
}
return 1;
}
// - Scan Code Variables -
static TriggerGuide Connect_receive_ScanCodeBuffer;
static uint8_t Connect_receive_ScanCodeBufferPos;
static uint8_t Connect_receive_ScanCodeDeviceId;
uint8_t Connect_receive_ScanCode( uint8_t byte, uint16_t *pending_bytes, uint8_t uart_num )
{
// Check the directionality
if ( uart_num == UART_Master )
{
erro_printNL("Invalid ScanCode direction...");
}
// Master node, trigger scan codes
if ( Connect_master ) switch ( (*pending_bytes)-- )
{
case BYTE_COUNT_START - 0: // Device Id
Connect_receive_ScanCodeDeviceId = byte;
break;
case BYTE_COUNT_START - 1: // Number of TriggerGuides in bytes (byte * 3)
*pending_bytes = byte * sizeof( TriggerGuide );
Connect_receive_ScanCodeBufferPos = 0;
break;
default:
// Set the specific TriggerGuide entry
((uint8_t*)&Connect_receive_ScanCodeBuffer)[ Connect_receive_ScanCodeBufferPos++ ] = byte;
// Reset the BufferPos if higher than sizeof TriggerGuide
// And send the TriggerGuide to the Macro Module
if ( Connect_receive_ScanCodeBufferPos >= sizeof( TriggerGuide ) )
{
Connect_receive_ScanCodeBufferPos = 0;
// Adjust ScanCode offset
if ( Connect_receive_ScanCodeDeviceId > 0 )
{
// Check if this node is too large
if ( Connect_receive_ScanCodeDeviceId >= InterconnectNodeMax )
{
warn_print("Not enough interconnect layout nodes configured: ");
printHex( Connect_receive_ScanCodeDeviceId );
print( NL );
break;
}
// This variable is in generatedKeymaps.h
extern uint8_t InterconnectOffsetList[];
Connect_receive_ScanCodeBuffer.scanCode = Connect_receive_ScanCodeBuffer.scanCode + InterconnectOffsetList[ Connect_receive_ScanCodeDeviceId ];
}
// ScanCode receive debug
if ( Connect_debug )
{
dbug_print("");
printHex( Connect_receive_ScanCodeBuffer.type );
print(" ");
printHex( Connect_receive_ScanCodeBuffer.state );
print(" ");
printHex( Connect_receive_ScanCodeBuffer.scanCode );
print( NL );
}
// Send ScanCode to macro module
Macro_pressReleaseAdd( &Connect_receive_ScanCodeBuffer );
}
break;
}
// Propagate ScanCode packet
// XXX It would be safer to buffer the scancodes first, before transmitting the packet -Jacob
// The current method is the more efficient/aggressive, but could cause issues if there were errors during transmission
else switch ( (*pending_bytes)-- )
{
case BYTE_COUNT_START - 0: // Device Id
{
Connect_receive_ScanCodeDeviceId = byte;
// Lock the master Tx buffer
uart_lockTx( UART_Master );
// Send header + Id byte
uint8_t header[] = { Command_SYN, SOH, ScanCode, byte };
Connect_addBytes( header, sizeof( header ), UART_Master );
break;
}
case BYTE_COUNT_START - 1: // Number of TriggerGuides in bytes
*pending_bytes = byte * sizeof( TriggerGuide );
Connect_receive_ScanCodeBufferPos = 0;
// Pass through byte
Connect_addBytes( &byte, 1, UART_Master );
break;
default:
// Pass through byte
Connect_addBytes( &byte, 1, UART_Master );
// Unlock Tx Buffer after sending last byte
if ( *pending_bytes == 0 )
uart_unlockTx( UART_Master );
break;
}
// Check whether the scan codes have finished sending
return *pending_bytes == 0 ? 1 : 0;
}
// - Remote Capability Variables -
#define Connect_receive_RemoteCapabilityMaxArgs 25 // XXX Calculate the max using kll
RemoteCapabilityCommand Connect_receive_RemoteCapabilityBuffer;
uint8_t Connect_receive_RemoteCapabilityArgs[Connect_receive_RemoteCapabilityMaxArgs];
uint8_t Connect_receive_RemoteCapability( uint8_t byte, uint16_t *pending_bytes, uint8_t uart_num )
{
// Check which byte in the packet we are at
switch ( (*pending_bytes)-- )
{
case BYTE_COUNT_START - 0: // Device Id
Connect_receive_RemoteCapabilityBuffer.id = byte;
break;
case BYTE_COUNT_START - 1: // Capability Index
Connect_receive_RemoteCapabilityBuffer.capabilityIndex = byte;
break;
case BYTE_COUNT_START - 2: // State
Connect_receive_RemoteCapabilityBuffer.state = byte;
break;
case BYTE_COUNT_START - 3: // StateType
Connect_receive_RemoteCapabilityBuffer.stateType = byte;
break;
case BYTE_COUNT_START - 4: // Number of args
Connect_receive_RemoteCapabilityBuffer.numArgs = byte;
*pending_bytes = byte;
break;
default: // Args (# defined by previous byte)
Connect_receive_RemoteCapabilityArgs[
Connect_receive_RemoteCapabilityBuffer.numArgs - *pending_bytes + 1
] = byte;
// If entire packet has been fully received
if ( *pending_bytes == 0 )
{
// Determine if this is the node to run the capability on
// Conditions: Matches or broadcast (0xFF)
if ( Connect_receive_RemoteCapabilityBuffer.id == BROADCAST_ID
|| Connect_receive_RemoteCapabilityBuffer.id == Connect_id )
{
extern const Capability CapabilitiesList[]; // See generatedKeymap.h
void (*capability)(TriggerMacro*, uint8_t, uint8_t, uint8_t*) = \
(void(*)(TriggerMacro*, uint8_t, uint8_t, uint8_t*))(
CapabilitiesList[
Connect_receive_RemoteCapabilityBuffer.capabilityIndex
].func
);
// TODO (HaaTa) - Send some sort of TriggerMacro information as a hint for the capability
capability(
0,
Connect_receive_RemoteCapabilityBuffer.state,
Connect_receive_RemoteCapabilityBuffer.stateType,
&Connect_receive_RemoteCapabilityArgs[2]
);
}
// If this is not the correct node, keep sending it in the same direction (doesn't matter if more nodes exist)
// or if this is a broadcast
if ( Connect_receive_RemoteCapabilityBuffer.id == BROADCAST_ID
|| Connect_receive_RemoteCapabilityBuffer.id != Connect_id )
{
// Prepare outgoing packet
Connect_receive_RemoteCapabilityBuffer.command = RemoteCapability;
// Send to the other UART (not the one receiving the packet from
uint8_t uart_direction = uart_num == UART_Master ? UART_Slave : UART_Master;
// Lock Tx UART
switch ( uart_direction )
{
case UART_Master: uart_lockTx( UART_Master ); break;
case UART_Slave: uart_lockTx( UART_Slave ); break;
}
// Send header
uint8_t header[] = { Command_SYN, SOH };
Connect_addBytes( header, sizeof( header ), uart_direction );
// Send Remote Capability and arguments
Connect_addBytes( (uint8_t*)&Connect_receive_RemoteCapabilityBuffer, sizeof( RemoteCapabilityCommand ), uart_direction );
Connect_addBytes( Connect_receive_RemoteCapabilityArgs, Connect_receive_RemoteCapabilityBuffer.numArgs, uart_direction );
// Unlock Tx UART
switch ( uart_direction )
{
case UART_Master: uart_unlockTx( UART_Master ); break;
case UART_Slave: uart_unlockTx( UART_Slave ); break;
}
}
}
break;
}
// Check whether the scan codes have finished sending
return *pending_bytes == 0 ? 1 : 0;
}
uint8_t Connect_receive_RemoteOutput( uint8_t byte, uint16_t *pending_bytes, uint8_t uart_num )
{
// TODO
(*pending_bytes)--;
*pending_bytes = 0;
// Check whether the scan codes have finished sending
return *pending_bytes == 0 ? 1 : 0;
}
uint8_t Connect_receive_RemoteInput( uint8_t byte, uint16_t *pending_bytes, uint8_t uart_num )
{
// TODO
(*pending_bytes)--;
*pending_bytes = 0;
// Check whether the scan codes have finished sending
return *pending_bytes == 0 ? 1 : 0;
}
static uint16_t Connect_receive_CurrentEvent_current;
uint8_t Connect_receive_CurrentEvent( uint8_t byte, uint16_t *pending_bytes, uint8_t uart_num )
{
// Check the directionality
if ( uart_num == UART_Slave )
{
erro_printNL("Invalid CurrentEvent direction...");
}
switch ( (*pending_bytes)-- )
{
// Byte count always starts at 0xFFFF
case 0xFFFF: // Current (LSB)
Connect_receive_CurrentEvent_current = byte;
break;
case 0xFFFE: // Current (MSB)
Connect_receive_CurrentEvent_current |= (byte << 8);
// We now have all the necessary arguments (this will update all current monitors)
Output_update_external_current( Connect_receive_CurrentEvent_current );
// All done
*pending_bytes = 0;
break;
}
// Check whether the scan codes have finished sending
return *pending_bytes == 0 ? 1 : 0;
}
// Baud Rate
// NOTE: If finer baud adjustment is needed see UARTx_C4 -> BRFA in the datasheet
uint16_t Connect_baud = UARTConnectBaud_define; // Max setting of 8191
uint16_t Connect_baudFine = UARTConnectBaudFine_define;
// Connect receive function lookup
void *Connect_receiveFunctions[] = {
Connect_receive_CableCheck,
Connect_receive_IdRequest,
Connect_receive_IdEnumeration,
Connect_receive_IdReport,
Connect_receive_ScanCode,
Connect_receive_RemoteCapability,
Connect_receive_RemoteOutput,
Connect_receive_RemoteInput,
Connect_receive_CurrentEvent,
};
// ----- Functions -----
// Resets the state of the UART buffers and state variables
void Connect_reset()
{
// Reset Rx
memset( (void*)uart_rx_status, 0, sizeof( UARTStatusRx ) * UART_Num_Interfaces );
// Reset Tx
memset( (void*)uart_tx_buf, 0, sizeof( UARTRingBuf ) * UART_Num_Interfaces );
memset( (void*)uart_tx_status, 0, sizeof( UARTStatusTx ) * UART_Num_Interfaces );
// Set Rx/Tx buffers as ready
for ( uint8_t inter = 0; inter < UART_Num_Interfaces; inter++ )
{
uart_tx_status[ inter ].status = UARTStatus_Ready;
uart_rx_buf[ inter ].last_read = UART_Buffer_Size;
}
}
// Setup connection to other side
// - Only supports a single slave and master
// - If USB has been initiallized at this point, this side is the master
// - If both sides assert master, flash error leds
void Connect_setup( uint8_t master, uint8_t first )
{
// Indication that UARTs are not ready
uarts_configured = 0;
// Register Connect CLI dictionary
if ( first )
{
CLI_registerDictionary( uartConnectCLIDict, uartConnectCLIDictName );
}
// Check if master
Connect_master = master;
if ( Connect_master )
{
Connect_id = MASTER_ID; // 0x00 is always the master Id
}
#if defined(_kinetis_)
// UART0 setup
// UART1 setup
// Setup the the UART interface for keyboard data input
SIM_SCGC4 |= SIM_SCGC4_UART0; // Disable clock gating
SIM_SCGC4 |= SIM_SCGC4_UART1; // Disable clock gating
// Pin Setup for UART0 / UART1
PORTA_PCR1 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(2); // RX Pin
PORTA_PCR2 = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(2); // TX Pin
PORTE_PCR0 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); // RX Pin
PORTE_PCR1 = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); // TX Pin
// Baud Rate setting
UART0_BDH = (uint8_t)(Connect_baud >> 8);
UART0_BDL = (uint8_t)Connect_baud;
UART0_C4 = Connect_baudFine;
UART1_BDH = (uint8_t)(Connect_baud >> 8);
UART1_BDL = (uint8_t)Connect_baud;
UART1_C4 = Connect_baudFine;
// 8 bit, Even Parity, Idle Character bit after stop
// NOTE: For 8 bit with Parity you must enable 9 bit transmission (pg. 1065)
// You only need to use UART0_D for 8 bit reading/writing though
// UART_C1_M UART_C1_PE UART_C1_PT UART_C1_ILT
UART0_C1 = UART_C1_M | UART_C1_PE | UART_C1_ILT;
UART1_C1 = UART_C1_M | UART_C1_PE | UART_C1_ILT;
// Only using Tx Fifos
UART0_PFIFO = UART_PFIFO_TXFE;
UART1_PFIFO = UART_PFIFO_TXFE;
// Setup DMA clocks
SIM_SCGC6 |= SIM_SCGC6_DMAMUX;
SIM_SCGC7 |= SIM_SCGC7_DMA;
// Start with channels disabled first
DMAMUX0_CHCFG0 = 0;
DMAMUX0_CHCFG1 = 0;
// Configure DMA channels
//DMA_DSR_BCR0 |= DMA_DSR_BCR_DONE_MASK; // TODO What's this?
DMA_TCD0_CSR = 0;
DMA_TCD1_CSR = 0;