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 36KB

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