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