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

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