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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  1. /* Copyright (C) 2014-2015 by Jacob Alexander
  2. *
  3. * This file is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This file is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this file. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. // ----- Includes -----
  17. // Compiler Includes
  18. #include <Lib/ScanLib.h>
  19. // Project Includes
  20. #include <cli.h>
  21. #include <kll_defs.h>
  22. #include <led.h>
  23. #include <print.h>
  24. #include <macro.h>
  25. // Local Includes
  26. #include "connect_scan.h"
  27. // ----- Macros -----
  28. // Macro for adding to each uart Tx ring buffer
  29. #define uart_addTxBuffer( uartNum ) \
  30. case uartNum: \
  31. while ( uart##uartNum##_buffer_items + count > uart_buffer_size ) \
  32. { \
  33. warn_msg("Too much data to send on UART0, waiting..."); \
  34. delay( 1 ); \
  35. } \
  36. for ( uint8_t c = 0; c < count; c++ ) \
  37. { \
  38. if ( Connect_debug ) \
  39. { \
  40. printHex( buffer[ c ] ); \
  41. print( " +" #uartNum NL ); \
  42. } \
  43. uart##uartNum##_buffer[ uart##uartNum##_buffer_tail++ ] = buffer[ c ]; \
  44. uart##uartNum##_buffer_items++; \
  45. if ( uart##uartNum##_buffer_tail >= uart_buffer_size ) \
  46. uart##uartNum##_buffer_tail = 0; \
  47. if ( uart##uartNum##_buffer_head == uart##uartNum##_buffer_tail ) \
  48. uart##uartNum##_buffer_head++; \
  49. if ( uart##uartNum##_buffer_head >= uart_buffer_size ) \
  50. uart##uartNum##_buffer_head = 0; \
  51. } \
  52. break
  53. // Macro for popping from Tx ring buffer
  54. #define uart_fillTxFifo( uartNum ) \
  55. { \
  56. uint8_t fifoSize = ( ( UART##uartNum##_PFIFO & UART_PFIFO_TXFIFOSIZE ) >> 2 ); \
  57. if ( fifoSize == 0 ) \
  58. fifoSize = 1; \
  59. while ( UART##uartNum##_TCFIFO < fifoSize ) \
  60. { \
  61. if ( uart##uartNum##_buffer_items == 0 ) \
  62. break; \
  63. UART##uartNum##_D = uart##uartNum##_buffer[ uart##uartNum##_buffer_head++ ]; \
  64. uart##uartNum##_buffer_items--; \
  65. if ( uart##uartNum##_buffer_head >= uart_buffer_size ) \
  66. uart##uartNum##_buffer_head = 0; \
  67. } \
  68. }
  69. // Macro for processing UART Rx
  70. #define uart_processRx( uartNum ) \
  71. { \
  72. if ( !( UART##uartNum##_S1 & UART_S1_RDRF ) ) \
  73. return; \
  74. uint8_t available = UART##uartNum##_RCFIFO; \
  75. if ( available == 0 ) \
  76. { \
  77. available = UART##uartNum##_D; \
  78. UART##uartNum##_CFIFO = UART_CFIFO_RXFLUSH; \
  79. return; \
  80. } \
  81. while ( available-- > 0 ) \
  82. { \
  83. uint8_t byteRead = UART##uartNum##_D; \
  84. if ( Connect_debug ) \
  85. { \
  86. printHex( byteRead ); \
  87. print( "(" ); \
  88. printInt8( available ); \
  89. print( ") <-" ); \
  90. } \
  91. switch ( uart##uartNum##_rx_status ) \
  92. { \
  93. case UARTStatus_Wait: \
  94. if ( Connect_debug ) \
  95. { \
  96. print(" SYN "); \
  97. } \
  98. uart##uartNum##_rx_status = byteRead == 0x16 ? UARTStatus_SYN : UARTStatus_Wait; \
  99. break; \
  100. case UARTStatus_SYN: \
  101. if ( Connect_debug ) \
  102. { \
  103. print(" SOH "); \
  104. } \
  105. uart##uartNum##_rx_status = byteRead == 0x01 ? UARTStatus_SOH : UARTStatus_Wait; \
  106. break; \
  107. case UARTStatus_SOH: \
  108. { \
  109. if ( Connect_debug ) \
  110. { \
  111. print(" CMD "); \
  112. } \
  113. uint8_t byte = byteRead; \
  114. if ( byte <= Animation ) \
  115. { \
  116. uart##uartNum##_rx_status = UARTStatus_Command; \
  117. uart##uartNum##_rx_command = byte; \
  118. uart##uartNum##_rx_bytes_waiting = 0xFFFF; \
  119. } \
  120. else \
  121. { \
  122. uart##uartNum##_rx_status = UARTStatus_Wait; \
  123. } \
  124. switch ( uart##uartNum##_rx_command ) \
  125. { \
  126. case IdRequest: \
  127. Connect_receive_IdRequest( 0, (uint16_t*)&uart##uartNum##_rx_bytes_waiting, uartNum ); \
  128. uart##uartNum##_rx_status = UARTStatus_Wait; \
  129. break; \
  130. default: \
  131. if ( Connect_debug ) \
  132. { \
  133. print("###"); \
  134. } \
  135. break; \
  136. } \
  137. break; \
  138. } \
  139. case UARTStatus_Command: \
  140. { \
  141. if ( Connect_debug ) \
  142. { \
  143. print(" CMD "); \
  144. } \
  145. uint8_t (*rcvFunc)(uint8_t, uint16_t(*), uint8_t) = (uint8_t(*)(uint8_t, uint16_t(*), uint8_t))(Connect_receiveFunctions[ uart##uartNum##_rx_command ]); \
  146. if ( rcvFunc( byteRead, (uint16_t*)&uart##uartNum##_rx_bytes_waiting, uartNum ) ) \
  147. uart##uartNum##_rx_status = UARTStatus_Wait; \
  148. break; \
  149. } \
  150. default: \
  151. erro_msg("Invalid UARTStatus..."); \
  152. uart##uartNum##_rx_status = UARTStatus_Wait; \
  153. available++; \
  154. continue; \
  155. } \
  156. if ( Connect_debug ) \
  157. { \
  158. print( NL ); \
  159. } \
  160. } \
  161. }
  162. // Macros for locking/unlock Tx buffers
  163. #define uart_lockTx( uartNum ) \
  164. { \
  165. while ( uart##uartNum##_tx_status == UARTStatus_Wait ); \
  166. uart##uartNum##_tx_status = UARTStatus_Wait; \
  167. }
  168. #define uart_unlockTx( uartNum ) \
  169. { \
  170. uart##uartNum##_tx_status = UARTStatus_Ready; \
  171. }
  172. // ----- Function Declarations -----
  173. // CLI Functions
  174. void cliFunc_connectCmd ( char *args );
  175. void cliFunc_connectIdl ( char *args );
  176. void cliFunc_connectLst ( char *args );
  177. void cliFunc_connectMst ( char *args );
  178. void cliFunc_connectRst ( char *args );
  179. void cliFunc_connectSts ( char *args );
  180. // ----- Variables -----
  181. // Connect Module command dictionary
  182. CLIDict_Entry( connectCmd, "Sends a command via UART Connect, first arg is which uart, next arg is the command, rest are the arguments." );
  183. CLIDict_Entry( connectIdl, "Sends N number of Idle commands, 2 is the default value, and should be sufficient in most cases." );
  184. CLIDict_Entry( connectLst, "Lists available UARTConnect commands and index id" );
  185. CLIDict_Entry( connectMst, "Sets the device as master. Use argument of s to set as slave." );
  186. CLIDict_Entry( connectRst, "Resets both Rx and Tx connect buffers and state variables." );
  187. CLIDict_Entry( connectSts, "UARTConnect status." );
  188. CLIDict_Def( uartConnectCLIDict, "UARTConnect Module Commands" ) = {
  189. CLIDict_Item( connectCmd ),
  190. CLIDict_Item( connectIdl ),
  191. CLIDict_Item( connectLst ),
  192. CLIDict_Item( connectMst ),
  193. CLIDict_Item( connectRst ),
  194. CLIDict_Item( connectSts ),
  195. { 0, 0, 0 } // Null entry for dictionary end
  196. };
  197. // -- Connect Device Id Variables --
  198. uint8_t Connect_id = 255; // Invalid, unset
  199. uint8_t Connect_master = 0;
  200. // -- Control Variables --
  201. uint32_t Connect_lastCheck = 0; // Cable Check scheduler
  202. uint8_t Connect_debug = 0; // Set 1 for debug
  203. // -- Rx Status Variables --
  204. volatile UARTStatus uart0_rx_status;
  205. volatile UARTStatus uart1_rx_status;
  206. volatile uint16_t uart0_rx_bytes_waiting;
  207. volatile uint16_t uart1_rx_bytes_waiting;
  208. volatile Command uart0_rx_command;
  209. volatile Command uart1_rx_command;
  210. // -- Tx Status Variables --
  211. volatile UARTStatus uart0_tx_status;
  212. volatile UARTStatus uart1_tx_status;
  213. // -- Ring Buffer Variables --
  214. #define uart_buffer_size UARTConnectBufSize_define
  215. volatile uint8_t uart0_buffer_head;
  216. volatile uint8_t uart0_buffer_tail;
  217. volatile uint8_t uart0_buffer_items;
  218. volatile uint8_t uart0_buffer[uart_buffer_size];
  219. volatile uint8_t uart1_buffer_head;
  220. volatile uint8_t uart1_buffer_tail;
  221. volatile uint8_t uart1_buffer_items;
  222. volatile uint8_t uart1_buffer[uart_buffer_size];
  223. volatile uint8_t uarts_configured = 0;
  224. // -- Ring Buffer Convenience Functions --
  225. void Connect_addBytes( uint8_t *buffer, uint8_t count, uint8_t uart )
  226. {
  227. // Too big to fit into buffer
  228. if ( count > uart_buffer_size )
  229. {
  230. erro_msg("Too big of a command to fit into the buffer...");
  231. return;
  232. }
  233. // Choose the uart
  234. switch ( uart )
  235. {
  236. uart_addTxBuffer( 0 );
  237. uart_addTxBuffer( 1 );
  238. default:
  239. erro_msg("Invalid UART to send from...");
  240. break;
  241. }
  242. }
  243. // -- Connect send functions --
  244. // patternLen defines how many bytes should the incrementing pattern have
  245. void Connect_send_CableCheck( uint8_t patternLen )
  246. {
  247. // Wait until the Tx buffers are ready, then lock them
  248. uart_lockTx( 0 );
  249. uart_lockTx( 1 );
  250. // Prepare header
  251. uint8_t header[] = { 0x16, 0x01, CableCheck, patternLen };
  252. // Send header
  253. Connect_addBytes( header, sizeof( header ), 1 ); // Master
  254. Connect_addBytes( header, sizeof( header ), 0 ); // Slave
  255. // Send 0xD2 (11010010) for each argument
  256. uint8_t value = 0xD2;
  257. for ( uint8_t c = 0; c < patternLen; c++ )
  258. {
  259. Connect_addBytes( &value, 1, 1 ); // Master
  260. Connect_addBytes( &value, 1, 0 ); // Slave
  261. }
  262. // Release Tx buffers
  263. uart_unlockTx( 0 );
  264. uart_unlockTx( 1 );
  265. }
  266. void Connect_send_IdRequest()
  267. {
  268. // Lock master bound Tx
  269. uart_lockTx( 1 );
  270. // Prepare header
  271. uint8_t header[] = { 0x16, 0x01, IdRequest };
  272. // Send header
  273. Connect_addBytes( header, sizeof( header ), 1 ); // Master
  274. // Unlock Tx
  275. uart_unlockTx( 1 );
  276. }
  277. // id is the value the next slave should enumerate as
  278. void Connect_send_IdEnumeration( uint8_t id )
  279. {
  280. // Lock slave bound Tx
  281. uart_lockTx( 0 );
  282. // Prepare header
  283. uint8_t header[] = { 0x16, 0x01, IdEnumeration, id };
  284. // Send header
  285. Connect_addBytes( header, sizeof( header ), 0 ); // Slave
  286. // Unlock Tx
  287. uart_unlockTx( 0 );
  288. }
  289. // id is the currently assigned id to the slave
  290. void Connect_send_IdReport( uint8_t id )
  291. {
  292. // Lock master bound Tx
  293. uart_lockTx( 1 );
  294. // Prepare header
  295. uint8_t header[] = { 0x16, 0x01, IdReport, id };
  296. // Send header
  297. Connect_addBytes( header, sizeof( header ), 1 ); // Master
  298. // Unlock Tx
  299. uart_unlockTx( 1 );
  300. }
  301. // id is the currently assigned id to the slave
  302. // scanCodeStateList is an array of [scancode, state]'s (8 bit values)
  303. // numScanCodes is the number of scan codes to parse from array
  304. void Connect_send_ScanCode( uint8_t id, TriggerGuide *scanCodeStateList, uint8_t numScanCodes )
  305. {
  306. // Lock master bound Tx
  307. uart_lockTx( 1 );
  308. // Prepare header
  309. uint8_t header[] = { 0x16, 0x01, ScanCode, id, numScanCodes };
  310. // Send header
  311. Connect_addBytes( header, sizeof( header ), 1 ); // Master
  312. // Send each of the scan codes
  313. Connect_addBytes( (uint8_t*)scanCodeStateList, numScanCodes * TriggerGuideSize, 1 ); // Master
  314. // Unlock Tx
  315. uart_unlockTx( 1 );
  316. }
  317. // id is the currently assigned id to the slave
  318. // paramList is an array of [param, value]'s (8 bit values)
  319. // numParams is the number of params to parse from the array
  320. void Connect_send_Animation( uint8_t id, uint8_t *paramList, uint8_t numParams )
  321. {
  322. // Lock slave bound Tx
  323. uart_lockTx( 0 );
  324. // Prepare header
  325. uint8_t header[] = { 0x16, 0x01, Animation, id, numParams };
  326. // Send header
  327. Connect_addBytes( header, sizeof( header ), 0 ); // Slave
  328. // Send each of the scan codes
  329. Connect_addBytes( paramList, numParams, 0 ); // Slave
  330. // Unlock Tx
  331. uart_unlockTx( 0 );
  332. }
  333. void Connect_send_Idle( uint8_t num )
  334. {
  335. // Wait until the Tx buffers are ready, then lock them
  336. uart_lockTx( 0 );
  337. uart_lockTx( 1 );
  338. // Send n number of idles to reset link status (if in a bad state)
  339. uint8_t value = 0x16;
  340. for ( uint8_t c = 0; c < num; c++ )
  341. {
  342. Connect_addBytes( &value, 1, 1 ); // Master
  343. Connect_addBytes( &value, 1, 0 ); // Slave
  344. }
  345. // Release Tx buffers
  346. uart_unlockTx( 0 );
  347. uart_unlockTx( 1 );
  348. }
  349. // -- Connect receive functions --
  350. // - Cable Check variables -
  351. uint32_t Connect_cableFaultsMaster = 0;
  352. uint32_t Connect_cableFaultsSlave = 0;
  353. uint8_t Connect_cableOkMaster = 0;
  354. uint8_t Connect_cableOkSlave = 0;
  355. uint8_t Connect_receive_CableCheck( uint8_t byte, uint16_t *pending_bytes, uint8_t to_master )
  356. {
  357. // Check if this is the first byte
  358. if ( *pending_bytes == 0xFFFF )
  359. {
  360. *pending_bytes = byte;
  361. if ( Connect_debug )
  362. {
  363. dbug_msg("PENDING SET -> ");
  364. printHex( byte );
  365. print(" ");
  366. printHex( *pending_bytes );
  367. print( NL );
  368. }
  369. }
  370. // Verify byte
  371. else
  372. {
  373. (*pending_bytes)--;
  374. // The argument bytes are always 0xD2 (11010010)
  375. if ( byte != 0xD2 )
  376. {
  377. warn_print("Cable Fault!");
  378. // Check which side of the chain
  379. if ( to_master )
  380. {
  381. Connect_cableFaultsMaster++;
  382. Connect_cableOkMaster = 0;
  383. print(" Master ");
  384. }
  385. else
  386. {
  387. Connect_cableFaultsSlave++;
  388. Connect_cableOkSlave = 0;
  389. print(" Slave ");
  390. }
  391. printHex( byte );
  392. print( NL );
  393. // Signal that the command should wait for a SYN again
  394. return 1;
  395. }
  396. }
  397. // If cable check was successful, set cable ok
  398. if ( *pending_bytes == 0 )
  399. {
  400. if ( to_master )
  401. {
  402. Connect_cableOkMaster = 1;
  403. }
  404. else
  405. {
  406. Connect_cableOkSlave = 1;
  407. }
  408. }
  409. if ( Connect_debug )
  410. {
  411. dbug_msg("CABLECHECK RECEIVE - ");
  412. printHex( byte );
  413. print(" ");
  414. printHex( *pending_bytes );
  415. print( NL );
  416. }
  417. // Check whether the cable check has finished
  418. return *pending_bytes == 0 ? 1 : 0;
  419. }
  420. uint8_t Connect_receive_IdRequest( uint8_t byte, uint16_t *pending_bytes, uint8_t to_master )
  421. {
  422. dbug_print("IdRequest");
  423. // Check the directionality
  424. if ( to_master )
  425. {
  426. erro_print("Invalid IdRequest direction...");
  427. }
  428. // Check if master, begin IdEnumeration
  429. if ( Connect_master )
  430. {
  431. // The first device is always id 1
  432. // Id 0 is reserved for the master
  433. Connect_send_IdEnumeration( 1 );
  434. }
  435. // Propagate IdRequest
  436. else
  437. {
  438. Connect_send_IdRequest();
  439. }
  440. return 1;
  441. }
  442. uint8_t Connect_receive_IdEnumeration( uint8_t id, uint16_t *pending_bytes, uint8_t to_master )
  443. {
  444. dbug_print("IdEnumeration");
  445. // Check the directionality
  446. if ( !to_master )
  447. {
  448. erro_print("Invalid IdEnumeration direction...");
  449. }
  450. // Set the device id
  451. Connect_id = id;
  452. // Send reponse back to master
  453. Connect_send_IdReport( id );
  454. // Propogate next Id if the connection is ok
  455. if ( Connect_cableOkSlave )
  456. {
  457. Connect_send_IdEnumeration( id + 1 );
  458. }
  459. return 1;
  460. }
  461. uint8_t Connect_receive_IdReport( uint8_t id, uint16_t *pending_bytes, uint8_t to_master )
  462. {
  463. dbug_print("IdReport");
  464. // Check the directionality
  465. if ( to_master )
  466. {
  467. erro_print("Invalid IdRequest direction...");
  468. }
  469. // Track Id response if master
  470. if ( Connect_master )
  471. {
  472. // TODO, setup id's
  473. info_msg("Id Reported: ");
  474. printHex( id );
  475. print( NL );
  476. return 1;
  477. }
  478. // Propagate id if yet another slave
  479. else
  480. {
  481. Connect_send_IdReport( id );
  482. }
  483. return 1;
  484. }
  485. // - Scan Code Variables -
  486. TriggerGuide Connect_receive_ScanCodeBuffer;
  487. uint8_t Connect_receive_ScanCodeBufferPos;
  488. uint8_t Connect_receive_ScanCodeDeviceId;
  489. uint8_t Connect_receive_ScanCode( uint8_t byte, uint16_t *pending_bytes, uint8_t to_master )
  490. {
  491. dbug_print("ScanCode");
  492. // Check the directionality
  493. if ( !to_master )
  494. {
  495. erro_print("Invalid ScanCode direction...");
  496. }
  497. // Master node, trigger scan codes
  498. if ( Connect_master ) switch ( (*pending_bytes)-- )
  499. {
  500. case 0xFFFF: // Device Id
  501. Connect_receive_ScanCodeDeviceId = byte;
  502. break;
  503. case 0xFFFE: // Number of TriggerGuides in bytes (byte * 3)
  504. *pending_bytes = byte * 3;
  505. Connect_receive_ScanCodeBufferPos = 0;
  506. break;
  507. default:
  508. // Set the specific TriggerGuide entry
  509. ((uint8_t*)&Connect_receive_ScanCodeBuffer)[ Connect_receive_ScanCodeBufferPos++ ] = byte;
  510. // Reset the BufferPos if higher than 3
  511. // And send the TriggerGuide to the Macro Module
  512. if ( Connect_receive_ScanCodeBufferPos > 3 )
  513. {
  514. Connect_receive_ScanCodeBufferPos = 0;
  515. Macro_triggerState( &Connect_receive_ScanCodeBuffer, 1 );
  516. }
  517. break;
  518. }
  519. // Propagate ScanCode packet
  520. else switch ( (*pending_bytes)-- )
  521. {
  522. case 0xFFFF: // Device Id
  523. {
  524. Connect_receive_ScanCodeDeviceId = byte;
  525. // Lock the master Tx buffer
  526. uart_lockTx( 1 );
  527. // Send header + Id byte
  528. uint8_t header[] = { 0x16, 0x01, ScanCode, byte };
  529. Connect_addBytes( header, sizeof( header ), 1 ); // Master
  530. break;
  531. }
  532. case 0xFFFE: // Number of TriggerGuides in bytes (byte * 3)
  533. *pending_bytes = byte * 3;
  534. Connect_receive_ScanCodeBufferPos = 0;
  535. // Pass through byte
  536. Connect_addBytes( &byte, 1, 1 ); // Master
  537. break;
  538. default:
  539. // Pass through byte
  540. Connect_addBytes( &byte, 1, 1 ); // Master
  541. // Unlock Tx Buffer after sending last byte
  542. if ( *pending_bytes == 0 )
  543. uart_unlockTx( 1 );
  544. break;
  545. }
  546. // Check whether the scan codes have finished sending
  547. return *pending_bytes == 0 ? 1 : 0;
  548. }
  549. uint8_t Connect_receive_Animation( uint8_t byte, uint16_t *pending_bytes, uint8_t to_master )
  550. {
  551. dbug_print("Animation");
  552. return 1;
  553. }
  554. // Baud Rate
  555. // NOTE: If finer baud adjustment is needed see UARTx_C4 -> BRFA in the datasheet
  556. uint16_t Connect_baud = UARTConnectBaud_define; // Max setting of 8191
  557. uint16_t Connect_baudFine = UARTConnectBaudFine_define;
  558. // Connect receive function lookup
  559. void *Connect_receiveFunctions[] = {
  560. Connect_receive_CableCheck,
  561. Connect_receive_IdRequest,
  562. Connect_receive_IdEnumeration,
  563. Connect_receive_IdReport,
  564. Connect_receive_ScanCode,
  565. Connect_receive_Animation,
  566. };
  567. // ----- Interrupt Functions -----
  568. // Master / UART0 ISR
  569. void uart0_status_isr()
  570. {
  571. // Process Rx buffer
  572. uart_processRx( 0 );
  573. }
  574. // Slave / UART1 ISR
  575. void uart1_status_isr()
  576. {
  577. // Process Rx buffer
  578. uart_processRx( 1 );
  579. }
  580. // ----- Functions -----
  581. // Resets the state of the UART buffers and state variables
  582. void Connect_reset()
  583. {
  584. // Rx Status Variables
  585. uart0_rx_status = UARTStatus_Wait;
  586. uart1_rx_status = UARTStatus_Wait;
  587. uart0_rx_bytes_waiting = 0;
  588. uart1_rx_bytes_waiting = 0;
  589. // Tx Status Variables
  590. uart0_tx_status = UARTStatus_Ready;
  591. uart1_tx_status = UARTStatus_Ready;
  592. // Ring Buffer Variables
  593. uart0_buffer_head = 0;
  594. uart0_buffer_tail = 0;
  595. uart0_buffer_items = 0;
  596. uart1_buffer_head = 0;
  597. uart1_buffer_tail = 0;
  598. uart1_buffer_items = 0;
  599. }
  600. // Setup connection to other side
  601. // - Only supports a single slave and master
  602. // - If USB has been initiallized at this point, this side is the master
  603. // - If both sides assert master, flash error leds
  604. void Connect_setup( uint8_t master )
  605. {
  606. // Indication that UARTs are not ready
  607. uarts_configured = 0;
  608. // Register Connect CLI dictionary
  609. CLI_registerDictionary( uartConnectCLIDict, uartConnectCLIDictName );
  610. // Check if master
  611. Connect_master = master;
  612. if ( Connect_master )
  613. Connect_id = 0; // 0x00 is always the master Id
  614. // Master / UART0 setup
  615. // Slave / UART1 setup
  616. // Setup the the UART interface for keyboard data input
  617. SIM_SCGC4 |= SIM_SCGC4_UART0; // Disable clock gating
  618. SIM_SCGC4 |= SIM_SCGC4_UART1; // Disable clock gating
  619. // Pin Setup for UART0 / UART1
  620. PORTA_PCR1 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(2); // RX Pin
  621. PORTA_PCR2 = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(2); // TX Pin
  622. PORTE_PCR0 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); // RX Pin
  623. PORTE_PCR1 = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); // TX Pin
  624. // Baud Rate setting
  625. UART0_BDH = (uint8_t)(Connect_baud >> 8);
  626. UART0_BDL = (uint8_t)Connect_baud;
  627. UART0_C4 = Connect_baudFine;
  628. UART1_BDH = (uint8_t)(Connect_baud >> 8);
  629. UART1_BDL = (uint8_t)Connect_baud;
  630. UART1_C4 = Connect_baudFine;
  631. // 8 bit, Even Parity, Idle Character bit after stop
  632. // NOTE: For 8 bit with Parity you must enable 9 bit transmission (pg. 1065)
  633. // You only need to use UART0_D for 8 bit reading/writing though
  634. // UART_C1_M UART_C1_PE UART_C1_PT UART_C1_ILT
  635. UART0_C1 = UART_C1_M | UART_C1_PE | UART_C1_ILT;
  636. UART1_C1 = UART_C1_M | UART_C1_PE | UART_C1_ILT;
  637. // Number of bytes in FIFO before TX Interrupt
  638. // TODO Set 0
  639. UART0_TWFIFO = 1;
  640. UART1_TWFIFO = 1;
  641. // Number of bytes in FIFO before RX Interrupt
  642. UART0_RWFIFO = 1;
  643. UART1_RWFIFO = 1;
  644. // Enable TX and RX FIFOs
  645. UART0_PFIFO = UART_PFIFO_TXFE | UART_PFIFO_RXFE;
  646. UART1_PFIFO = UART_PFIFO_TXFE | UART_PFIFO_RXFE;
  647. // Reciever Inversion Disabled, LSBF
  648. // UART_S2_RXINV UART_S2_MSBF
  649. UART0_S2 |= 0x00;
  650. UART1_S2 |= 0x00;
  651. // Transmit Inversion Disabled
  652. // UART_C3_TXINV
  653. UART0_C3 |= 0x00;
  654. UART1_C3 |= 0x00;
  655. // TX Enabled, RX Enabled, RX Interrupt Enabled
  656. // UART_C2_TE UART_C2_RE UART_C2_RIE
  657. UART0_C2 = UART_C2_TE | UART_C2_RE | UART_C2_RIE;
  658. UART1_C2 = UART_C2_TE | UART_C2_RE | UART_C2_RIE;
  659. // Add interrupts to the vector table
  660. NVIC_ENABLE_IRQ( IRQ_UART0_STATUS );
  661. NVIC_ENABLE_IRQ( IRQ_UART1_STATUS );
  662. // UARTs are now ready to go
  663. uarts_configured = 1;
  664. // Reset the state of the UART variables
  665. Connect_reset();
  666. }
  667. // Scan for updates in the master/slave
  668. // - Interrupts will deal with most input functions
  669. // - Used to send queries
  670. // - SyncEvent is sent immediately once the current command is sent
  671. // - SyncEvent is also blocking until sent
  672. void Connect_scan()
  673. {
  674. // Check if initially configured as a slave and usb comes up
  675. // Then reconfigure as a master
  676. if ( !Connect_master && Output_Available )
  677. {
  678. Connect_setup( Output_Available );
  679. }
  680. // Limit how often we do cable checks
  681. uint32_t time_compare = 0x7FF; // Must be all 1's, 0x3FF is valid, 0x4FF is not
  682. uint32_t current_time = systick_millis_count;
  683. if ( Connect_lastCheck != current_time
  684. && ( current_time & time_compare ) == time_compare
  685. )
  686. {
  687. // Make sure we don't double check if the clock speed is too high
  688. Connect_lastCheck = current_time;
  689. // Send a cable check command of 2 bytes
  690. Connect_send_CableCheck( UARTConnectCableCheckLength_define );
  691. // If this is a slave, and we don't have an id yeth
  692. // Don't bother sending if there are cable issues
  693. if ( !Connect_master && Connect_id == 0xFF && Connect_cableOkMaster )
  694. {
  695. Connect_send_IdRequest();
  696. }
  697. }
  698. // Only process commands if uarts have been configured
  699. if ( uarts_configured )
  700. {
  701. // Check if Tx Buffers are empty and the Tx Ring buffers have data to send
  702. // This happens if there was previously nothing to send
  703. if ( uart0_buffer_items > 0 && UART0_TCFIFO == 0 )
  704. uart_fillTxFifo( 0 );
  705. if ( uart1_buffer_items > 0 && UART1_TCFIFO == 0 )
  706. uart_fillTxFifo( 1 );
  707. }
  708. }
  709. // ----- CLI Command Functions -----
  710. void cliFunc_connectCmd( char* args )
  711. {
  712. // Parse number from argument
  713. // NOTE: Only first argument is used
  714. char* arg1Ptr;
  715. char* arg2Ptr;
  716. CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
  717. print( NL );
  718. switch ( numToInt( &arg1Ptr[0] ) )
  719. {
  720. case CableCheck:
  721. Connect_send_CableCheck( UARTConnectCableCheckLength_define );
  722. break;
  723. case IdRequest:
  724. Connect_send_IdRequest();
  725. break;
  726. case IdEnumeration:
  727. Connect_send_IdEnumeration( 5 );
  728. break;
  729. case IdReport:
  730. Connect_send_IdReport( 8 );
  731. break;
  732. case ScanCode:
  733. {
  734. TriggerGuide scanCodes[] = { { 0x00, 0x01, 0x05 }, { 0x00, 0x03, 0x16 } };
  735. Connect_send_ScanCode( 10, scanCodes, 2 );
  736. break;
  737. }
  738. case Animation:
  739. break;
  740. case RemoteCapability:
  741. // TODO
  742. break;
  743. case RemoteOutput:
  744. // TODO
  745. break;
  746. case RemoteInput:
  747. // TODO
  748. break;
  749. default:
  750. break;
  751. }
  752. }
  753. void cliFunc_connectIdl( char* args )
  754. {
  755. // Parse number from argument
  756. // NOTE: Only first argument is used
  757. char* arg1Ptr;
  758. char* arg2Ptr;
  759. CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
  760. print( NL );
  761. info_msg("Sending Sync Idles...");
  762. uint8_t count = numToInt( &arg1Ptr[0] );
  763. // Default to 2 idles
  764. if ( count == 0 )
  765. count = 2;
  766. Connect_send_Idle( count );
  767. }
  768. void cliFunc_connectLst( char* args )
  769. {
  770. const char *Command_strs[] = {
  771. "CableCheck",
  772. "IdRequest",
  773. "IdEnumeration",
  774. "IdReport",
  775. "ScanCode",
  776. "Animation",
  777. "RemoteCapability",
  778. "RemoteOutput",
  779. "RemoteInput",
  780. };
  781. print( NL );
  782. info_msg("List of UARTConnect commands");
  783. for ( uint8_t cmd = 0; cmd < Command_TOP; cmd++ )
  784. {
  785. print( NL );
  786. printInt8( cmd );
  787. print(" - ");
  788. dPrint( (char*)Command_strs[ cmd ] );
  789. }
  790. }
  791. void cliFunc_connectMst( char* args )
  792. {
  793. // Parse number from argument
  794. // NOTE: Only first argument is used
  795. char* arg1Ptr;
  796. char* arg2Ptr;
  797. CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
  798. print( NL );
  799. switch ( arg1Ptr[0] )
  800. {
  801. case 's':
  802. case 'S':
  803. info_msg("Setting device as slave.");
  804. Connect_master = 0;
  805. Connect_id = 0xFF;
  806. break;
  807. case 'm':
  808. case 'M':
  809. default:
  810. info_msg("Setting device as master.");
  811. Connect_master = 1;
  812. Connect_id = 0;
  813. break;
  814. }
  815. }
  816. void cliFunc_connectRst( char* args )
  817. {
  818. print( NL );
  819. info_msg("Resetting UARTConnect state...");
  820. Connect_reset();
  821. // Reset node id
  822. Connect_id = 0xFF;
  823. }
  824. void cliFunc_connectSts( char* args )
  825. {
  826. print( NL );
  827. info_msg("UARTConnect Status");
  828. print( NL "Device Type:\t" );
  829. print( Connect_master ? "Master" : "Slave" );
  830. print( NL "Device Id:\t" );
  831. printHex( Connect_id );
  832. print( NL "Master <=" NL "\tStatus:\t");
  833. printHex( Connect_cableOkMaster );
  834. print( NL "\tFaults:\t");
  835. printHex( Connect_cableFaultsMaster );
  836. print( NL "\tRx:\t");
  837. printHex( uart1_rx_status );
  838. print( NL "\tTx:\t");
  839. printHex( uart1_tx_status );
  840. print( NL "Slave <=" NL "\tStatus:\t");
  841. printHex( Connect_cableOkSlave );
  842. print( NL "\tFaults:\t");
  843. printHex( Connect_cableFaultsSlave );
  844. print( NL "\tRx:\t");
  845. printHex( uart0_rx_status );
  846. print( NL "\tTx:\t");
  847. printHex( uart0_tx_status );
  848. }