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.

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