Kiibohd Controller
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

connect_scan.c 35KB

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