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

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