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.

led_scan.c 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  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. // Local Includes
  25. #include "led_scan.h"
  26. // ----- Defines -----
  27. #define I2C_TxBufferLength 300
  28. #define I2C_RxBufferLength 8
  29. #define LED_BufferLength 144
  30. // ----- Structs -----
  31. typedef struct I2C_Buffer {
  32. uint16_t head;
  33. uint16_t tail;
  34. uint8_t sequencePos;
  35. uint16_t size;
  36. uint8_t *buffer;
  37. } I2C_Buffer;
  38. typedef struct LED_Buffer {
  39. uint8_t buffer[LED_BufferLength];
  40. } LED_Buffer;
  41. // ----- Function Declarations -----
  42. // CLI Functions
  43. void cliFunc_i2cRecv ( char* args );
  44. void cliFunc_i2cSend ( char* args );
  45. void cliFunc_ledRPage( char* args );
  46. void cliFunc_ledStart( char* args );
  47. void cliFunc_ledTest ( char* args );
  48. void cliFunc_ledWPage( char* args );
  49. void cliFunc_ledZero ( char* args );
  50. uint8_t I2C_TxBufferPop();
  51. void I2C_BufferPush( uint8_t byte, I2C_Buffer *buffer );
  52. uint16_t I2C_BufferLen( I2C_Buffer *buffer );
  53. uint8_t I2C_Send( uint8_t *data, uint8_t sendLen, uint8_t recvLen );
  54. // ----- Variables -----
  55. // Scan Module command dictionary
  56. CLIDict_Entry( i2cRecv, "Send I2C sequence of bytes and expect a reply of 1 byte on the last sequence." NL "\t\tUse |'s to split sequences with a stop." );
  57. CLIDict_Entry( i2cSend, "Send I2C sequence of bytes. Use |'s to split sequences with a stop." );
  58. CLIDict_Entry( ledRPage, "Read the given register page." );
  59. CLIDict_Entry( ledStart, "Disable software shutdown." );
  60. CLIDict_Entry( ledTest, "Test out the led pages." );
  61. CLIDict_Entry( ledWPage, "Write to given register page starting at address. i.e. 0x2 0x24 0xF0 0x12" );
  62. CLIDict_Entry( ledZero, "Zero out LED register pages (non-configuration)." );
  63. CLIDict_Def( ledCLIDict, "ISSI LED Module Commands" ) = {
  64. CLIDict_Item( i2cRecv ),
  65. CLIDict_Item( i2cSend ),
  66. CLIDict_Item( ledRPage ),
  67. CLIDict_Item( ledStart ),
  68. CLIDict_Item( ledTest ),
  69. CLIDict_Item( ledWPage ),
  70. CLIDict_Item( ledZero ),
  71. { 0, 0, 0 } // Null entry for dictionary end
  72. };
  73. // Before sending the sequence, I2C_TxBuffer_CurLen is assigned and as each byte is sent, it is decremented
  74. // Once I2C_TxBuffer_CurLen reaches zero, a STOP on the I2C bus is sent
  75. volatile uint8_t I2C_TxBufferPtr[ I2C_TxBufferLength ];
  76. volatile uint8_t I2C_RxBufferPtr[ I2C_TxBufferLength ];
  77. volatile I2C_Buffer I2C_TxBuffer = { 0, 0, 0, I2C_TxBufferLength, (uint8_t*)I2C_TxBufferPtr };
  78. volatile I2C_Buffer I2C_RxBuffer = { 0, 0, 0, I2C_RxBufferLength, (uint8_t*)I2C_RxBufferPtr };
  79. LED_Buffer LED_pageBuffer;
  80. // A bit mask determining which LEDs are enabled in the ISSI chip
  81. const uint8_t LED_ledEnableMask1[] = {
  82. 0xE8, // I2C address
  83. 0x00, // Starting register address
  84. ISSILedMask1_define
  85. };
  86. // Default LED brightness
  87. const uint8_t LED_defaultBrightness1[] = {
  88. 0xE8, // I2C address
  89. 0x24, // Starting register address
  90. ISSILedBrightness1_define
  91. };
  92. // ----- Interrupt Functions -----
  93. void i2c0_isr()
  94. {
  95. cli(); // Disable Interrupts
  96. uint8_t status = I2C0_S; // Read I2C Bus status
  97. // Master Mode Transmit
  98. if ( I2C0_C1 & I2C_C1_TX )
  99. {
  100. // Check current use of the I2C bus
  101. // Currently sending data
  102. if ( I2C_TxBuffer.sequencePos > 0 )
  103. {
  104. // Make sure slave sent an ACK
  105. if ( status & I2C_S_RXAK )
  106. {
  107. // NACK Detected, disable interrupt
  108. erro_print("I2C NAK detected...");
  109. I2C0_C1 = I2C_C1_IICEN;
  110. // Abort Tx Buffer
  111. I2C_TxBuffer.head = 0;
  112. I2C_TxBuffer.tail = 0;
  113. I2C_TxBuffer.sequencePos = 0;
  114. }
  115. else
  116. {
  117. // Transmit byte
  118. I2C0_D = I2C_TxBufferPop();
  119. }
  120. }
  121. // Receiving data
  122. else if ( I2C_RxBuffer.sequencePos > 0 )
  123. {
  124. // Master Receive, addr sent
  125. if ( status & I2C_S_ARBL )
  126. {
  127. // Arbitration Lost
  128. erro_print("Arbitration lost...");
  129. // TODO Abort Rx
  130. I2C0_C1 = I2C_C1_IICEN;
  131. I2C0_S = I2C_S_ARBL | I2C_S_IICIF; // Clear ARBL flag and interrupt
  132. }
  133. if ( status & I2C_S_RXAK )
  134. {
  135. // Slave Address NACK Detected, disable interrupt
  136. erro_print("Slave Address I2C NAK detected...");
  137. // TODO Abort Rx
  138. I2C0_C1 = I2C_C1_IICEN;
  139. }
  140. else
  141. {
  142. dbug_print("Attempting to read byte");
  143. I2C0_C1 = I2C_RxBuffer.sequencePos == 1
  144. ? I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_MST | I2C_C1_TXAK // Single byte read
  145. : I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_MST; // Multi-byte read
  146. }
  147. }
  148. else
  149. {
  150. /*
  151. dbug_msg("STOP - ");
  152. printHex( I2C_BufferLen( (I2C_Buffer*)&I2C_TxBuffer ) );
  153. print(NL);
  154. */
  155. // Delay around STOP to make sure it actually happens...
  156. delayMicroseconds( 1 );
  157. I2C0_C1 = I2C_C1_IICEN; // Send STOP
  158. delayMicroseconds( 7 );
  159. // If there is another sequence, start sending
  160. if ( I2C_BufferLen( (I2C_Buffer*)&I2C_TxBuffer ) < I2C_TxBuffer.size )
  161. {
  162. // Clear status flags
  163. I2C0_S = I2C_S_IICIF | I2C_S_ARBL;
  164. // Wait...till the master dies
  165. while ( I2C0_S & I2C_S_BUSY );
  166. // Enable I2C interrupt
  167. I2C0_C1 = I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_MST | I2C_C1_TX;
  168. // Transmit byte
  169. I2C0_D = I2C_TxBufferPop();
  170. }
  171. }
  172. }
  173. // Master Mode Receive
  174. else
  175. {
  176. // XXX Do we need to handle 2nd last byte?
  177. //I2C0_C1 = I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_MST | I2C_C1_TXAK; // No STOP, Rx, NAK on recv
  178. // Last byte
  179. if ( I2C_TxBuffer.sequencePos <= 1 )
  180. {
  181. // Change to Tx mode
  182. I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TX;
  183. // Grab last byte
  184. I2C_BufferPush( I2C0_D, (I2C_Buffer*)&I2C_RxBuffer );
  185. delayMicroseconds( 1 ); // Should be enough time before issuing the stop
  186. I2C0_C1 = I2C_C1_IICEN; // Send STOP
  187. }
  188. else
  189. {
  190. // Retrieve data
  191. I2C_BufferPush( I2C0_D, (I2C_Buffer*)&I2C_RxBuffer );
  192. }
  193. }
  194. I2C0_S = I2C_S_IICIF; // Clear interrupt
  195. sei(); // Re-enable Interrupts
  196. }
  197. // ----- Functions -----
  198. inline void I2C_setup()
  199. {
  200. // Enable I2C internal clock
  201. SIM_SCGC4 |= SIM_SCGC4_I2C0; // Bus 0
  202. // External pull-up resistor
  203. PORTB_PCR0 = PORT_PCR_ODE | PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(2);
  204. PORTB_PCR1 = PORT_PCR_ODE | PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(2);
  205. // SCL Frequency Divider
  206. // 400kHz -> 120 (0x85) @ 48 MHz F_BUS
  207. I2C0_F = 0x85;
  208. I2C0_FLT = 4;
  209. I2C0_C1 = I2C_C1_IICEN;
  210. I2C0_C2 = I2C_C2_HDRS; // High drive select
  211. // Enable I2C Interrupt
  212. NVIC_ENABLE_IRQ( IRQ_I2C0 );
  213. }
  214. void LED_zeroPages( uint8_t startPage, uint8_t numPages, uint8_t startReg, uint8_t endReg )
  215. {
  216. // Page Setup
  217. uint8_t pageSetup[] = { 0xE8, 0xFD, 0x00 };
  218. // Max length of a page + chip id + reg start
  219. uint8_t fullPage[ 0xB4 + 2 ] = { 0 }; // Max size of page
  220. fullPage[0] = 0xE8; // Set chip id
  221. fullPage[1] = startReg; // Set start reg
  222. // Iterate through given pages, zero'ing out the given register regions
  223. for ( uint8_t page = startPage; page < startPage + numPages; page++ )
  224. {
  225. // Set page
  226. pageSetup[2] = page;
  227. // Setup page
  228. while ( I2C_Send( pageSetup, sizeof( pageSetup ), 0 ) == 0 )
  229. delay(1);
  230. // Zero out page
  231. while ( I2C_Send( fullPage, endReg - startReg + 2, 0 ) == 0 )
  232. delay(1);
  233. }
  234. }
  235. void LED_sendPage( uint8_t *buffer, uint8_t len, uint8_t page )
  236. {
  237. // Page Setup
  238. uint8_t pageSetup[] = { 0xE8, 0xFD, page };
  239. // Setup page
  240. while ( I2C_Send( pageSetup, sizeof( pageSetup ), 0 ) == 0 )
  241. delay(1);
  242. // Write page to I2C Tx Buffer
  243. while ( I2C_Send( buffer, len, 0 ) == 0 )
  244. delay(1);
  245. }
  246. void LED_readPage( uint8_t len, uint8_t page )
  247. {
  248. // Page Setup
  249. uint8_t pageSetup[] = { 0xE8, 0xFD, page };
  250. // Setup page
  251. while ( I2C_Send( pageSetup, sizeof( pageSetup ), 0 ) == 0 )
  252. delay(1);
  253. // Register Setup
  254. uint8_t regSetup[] = { 0xE8, 0x00 };
  255. // Setup starting register
  256. while ( I2C_Send( regSetup, sizeof( regSetup ), 0 ) == 0 )
  257. delay(1);
  258. // Register Read Command
  259. uint8_t regReadCmd[] = { 0xE9 };
  260. // Read each register in the page
  261. for ( uint8_t reg = 0; reg < len; reg++ )
  262. {
  263. // Request register data
  264. while ( I2C_Send( regReadCmd, sizeof( regReadCmd ), 0 ) == 0 )
  265. delay(1);
  266. }
  267. }
  268. void LED_writeReg( uint8_t reg, uint8_t val, uint8_t page )
  269. {
  270. // Page Setup
  271. uint8_t pageSetup[] = { 0xE8, 0xFD, page };
  272. // Reg Write Setup
  273. uint8_t writeData[] = { 0xE8, reg, val };
  274. // Setup page
  275. while ( I2C_Send( pageSetup, sizeof( pageSetup ), 0 ) == 0 )
  276. delay(1);
  277. while ( I2C_Send( writeData, sizeof( writeData ), 0 ) == 0 )
  278. delay(1);
  279. }
  280. // Setup
  281. inline void LED_setup()
  282. {
  283. // Register Scan CLI dictionary
  284. CLI_registerDictionary( ledCLIDict, ledCLIDictName );
  285. // Initialize I2C
  286. I2C_setup();
  287. // Zero out Frame Registers
  288. // This needs to be done before disabling the hardware shutdown (or the leds will do undefined things)
  289. LED_zeroPages( 0x0B, 1, 0x00, 0x0C ); // Control Registers
  290. // Disable Hardware shutdown of ISSI chip (pull high)
  291. GPIOB_PDDR |= (1<<16);
  292. PORTB_PCR16 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  293. GPIOB_PSOR |= (1<<16);
  294. // Clear LED Pages
  295. LED_zeroPages( 0x00, 8, 0x00, 0xB4 ); // LED Registers
  296. // Enable LEDs based upon mask
  297. LED_sendPage( (uint8_t*)LED_ledEnableMask1, sizeof( LED_ledEnableMask1 ), 0 );
  298. // Set default brightness
  299. LED_sendPage( (uint8_t*)LED_defaultBrightness1, sizeof( LED_defaultBrightness1 ), 0 );
  300. // Disable Software shutdown of ISSI chip
  301. LED_writeReg( 0x0A, 0x01, 0x0B );
  302. }
  303. inline uint8_t I2C_BufferCopy( uint8_t *data, uint8_t sendLen, uint8_t recvLen, I2C_Buffer *buffer )
  304. {
  305. uint8_t reTurn = 0;
  306. // If sendLen is greater than buffer fail right away
  307. if ( sendLen > buffer->size )
  308. return 0;
  309. // Calculate new tail to determine if buffer has enough space
  310. // The first element specifies the expected number of bytes from the slave (+1)
  311. // The second element in the new buffer is the length of the buffer sequence (+1)
  312. uint16_t newTail = buffer->tail + sendLen + 2;
  313. if ( newTail >= buffer->size )
  314. newTail -= buffer->size;
  315. if ( I2C_BufferLen( buffer ) < sendLen + 2 )
  316. return 0;
  317. /*
  318. print("|");
  319. printHex( sendLen + 2 );
  320. print("|");
  321. printHex( *tail );
  322. print("@");
  323. printHex( newTail );
  324. print("@");
  325. */
  326. // If buffer is clean, return 1, otherwise 2
  327. reTurn = buffer->head == buffer->tail ? 1 : 2;
  328. // Add to buffer, already know there is enough room (simplifies adding logic)
  329. uint8_t bufferHeaderPos = 0;
  330. for ( uint16_t c = 0; c < sendLen; c++ )
  331. {
  332. // Add data to buffer
  333. switch ( bufferHeaderPos )
  334. {
  335. case 0:
  336. buffer->buffer[ buffer->tail ] = recvLen;
  337. bufferHeaderPos++;
  338. c--;
  339. break;
  340. case 1:
  341. buffer->buffer[ buffer->tail ] = sendLen;
  342. bufferHeaderPos++;
  343. c--;
  344. break;
  345. default:
  346. buffer->buffer[ buffer->tail ] = data[ c ];
  347. break;
  348. }
  349. // Check for wrap-around case
  350. if ( buffer->tail + 1 >= buffer->size )
  351. {
  352. buffer->tail = 0;
  353. }
  354. // Normal case
  355. else
  356. {
  357. buffer->tail++;
  358. }
  359. }
  360. return reTurn;
  361. }
  362. inline uint16_t I2C_BufferLen( I2C_Buffer *buffer )
  363. {
  364. // Tail >= Head
  365. if ( buffer->tail >= buffer->head )
  366. return buffer->head + buffer->size - buffer->tail;
  367. // Head > Tail
  368. return buffer->head - buffer->tail;
  369. }
  370. void I2C_BufferPush( uint8_t byte, I2C_Buffer *buffer )
  371. {
  372. dbug_msg("DATA: ");
  373. printHex( byte );
  374. // Make sure buffer isn't full
  375. if ( buffer->tail + 1 == buffer->head || ( buffer->head > buffer->tail && buffer->tail + 1 - buffer->size == buffer->head ) )
  376. {
  377. warn_msg("I2C_BufferPush failed, buffer full: ");
  378. printHex( byte );
  379. print( NL );
  380. return;
  381. }
  382. // Check for wrap-around case
  383. if ( buffer->tail + 1 >= buffer->size )
  384. {
  385. buffer->tail = 0;
  386. }
  387. // Normal case
  388. else
  389. {
  390. buffer->tail++;
  391. }
  392. // Add byte to buffer
  393. buffer->buffer[ buffer->tail ] = byte;
  394. }
  395. uint8_t I2C_TxBufferPop()
  396. {
  397. // Return 0xFF if no buffer left (do not rely on this)
  398. if ( I2C_BufferLen( (I2C_Buffer*)&I2C_TxBuffer ) >= I2C_TxBuffer.size )
  399. {
  400. erro_msg("No buffer to pop an entry from... ");
  401. printHex( I2C_TxBuffer.head );
  402. print(" ");
  403. printHex( I2C_TxBuffer.tail );
  404. print(" ");
  405. printHex( I2C_TxBuffer.sequencePos );
  406. print(NL);
  407. return 0xFF;
  408. }
  409. // If there is currently no sequence being sent, the first entry in the RingBuffer is the length
  410. if ( I2C_TxBuffer.sequencePos == 0 )
  411. {
  412. I2C_TxBuffer.sequencePos = 0xFF; // So this doesn't become an infinite loop
  413. I2C_RxBuffer.sequencePos = I2C_TxBufferPop();
  414. I2C_TxBuffer.sequencePos = I2C_TxBufferPop();
  415. }
  416. uint8_t data = I2C_TxBuffer.buffer[ I2C_TxBuffer.head ];
  417. // Prune head
  418. I2C_TxBuffer.head++;
  419. // Wrap-around case
  420. if ( I2C_TxBuffer.head >= I2C_TxBuffer.size )
  421. I2C_TxBuffer.head = 0;
  422. // Decrement buffer sequence (until next stop will be sent)
  423. I2C_TxBuffer.sequencePos--;
  424. /*
  425. dbug_msg("Popping: ");
  426. printHex( data );
  427. print(" ");
  428. printHex( I2C_TxBuffer.head );
  429. print(" ");
  430. printHex( I2C_TxBuffer.tail );
  431. print(" ");
  432. printHex( I2C_TxBuffer.sequencePos );
  433. print(NL);
  434. */
  435. return data;
  436. }
  437. uint8_t I2C_Send( uint8_t *data, uint8_t sendLen, uint8_t recvLen )
  438. {
  439. // Check head and tail pointers
  440. // If full, return 0
  441. // If empty, start up I2C Master Tx
  442. // If buffer is non-empty and non-full, just append to the buffer
  443. switch ( I2C_BufferCopy( data, sendLen, recvLen, (I2C_Buffer*)&I2C_TxBuffer ) )
  444. {
  445. // Not enough buffer space...
  446. case 0:
  447. /*
  448. erro_msg("Not enough Tx buffer space... ");
  449. printHex( I2C_TxBuffer.head );
  450. print(":");
  451. printHex( I2C_TxBuffer.tail );
  452. print("+");
  453. printHex( sendLen );
  454. print("|");
  455. printHex( I2C_TxBuffer.size );
  456. print( NL );
  457. */
  458. return 0;
  459. // Empty buffer, initialize I2C
  460. case 1:
  461. // Clear status flags
  462. I2C0_S = I2C_S_IICIF | I2C_S_ARBL;
  463. // Check to see if we already have control of the bus
  464. if ( I2C0_C1 & I2C_C1_MST )
  465. {
  466. // Already the master (ah yeah), send a repeated start
  467. I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_RSTA | I2C_C1_TX;
  468. }
  469. // Otherwise, seize control
  470. else
  471. {
  472. // Wait...till the master dies
  473. while ( I2C0_S & I2C_S_BUSY );
  474. // Now we're the master (ah yisss), get ready to send stuffs
  475. I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TX;
  476. }
  477. // Enable I2C interrupt
  478. I2C0_C1 = I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_MST | I2C_C1_TX;
  479. // Depending on what type of transfer, the first byte is configured for R or W
  480. I2C0_D = I2C_TxBufferPop();
  481. return 1;
  482. }
  483. // Dirty buffer, I2C already initialized
  484. return 2;
  485. }
  486. // LED State processing loop
  487. inline uint8_t LED_scan()
  488. {
  489. // I2C Busy
  490. // S & I2C_S_BUSY
  491. //I2C_S_BUSY
  492. return 0;
  493. }
  494. // ----- CLI Command Functions -----
  495. void cliFunc_i2cSend( char* args )
  496. {
  497. char* curArgs;
  498. char* arg1Ptr;
  499. char* arg2Ptr = args;
  500. // Buffer used after interpretting the args, will be sent to I2C functions
  501. // NOTE: Limited to 8 bytes currently (can be increased if necessary
  502. #define i2cSend_BuffLenMax 8
  503. uint8_t buffer[ i2cSend_BuffLenMax ];
  504. uint8_t bufferLen = 0;
  505. // No \r\n by default after the command is entered
  506. print( NL );
  507. info_msg("Sending: ");
  508. // Parse args until a \0 is found
  509. while ( bufferLen < i2cSend_BuffLenMax )
  510. {
  511. curArgs = arg2Ptr; // Use the previous 2nd arg pointer to separate the next arg from the list
  512. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  513. // Stop processing args if no more are found
  514. if ( *arg1Ptr == '\0' )
  515. break;
  516. // If | is found, end sequence and start new one
  517. if ( *arg1Ptr == '|' )
  518. {
  519. print("| ");
  520. I2C_Send( buffer, bufferLen, 0 );
  521. bufferLen = 0;
  522. continue;
  523. }
  524. // Interpret the argument
  525. buffer[ bufferLen++ ] = (uint8_t)numToInt( arg1Ptr );
  526. // Print out the arg
  527. dPrint( arg1Ptr );
  528. print(" ");
  529. }
  530. print( NL );
  531. I2C_Send( buffer, bufferLen, 0 );
  532. }
  533. void cliFunc_i2cRecv( char* args )
  534. {
  535. char* curArgs;
  536. char* arg1Ptr;
  537. char* arg2Ptr = args;
  538. // Buffer used after interpretting the args, will be sent to I2C functions
  539. // NOTE: Limited to 8 bytes currently (can be increased if necessary
  540. #define i2cSend_BuffLenMax 8
  541. uint8_t buffer[ i2cSend_BuffLenMax ];
  542. uint8_t bufferLen = 0;
  543. // No \r\n by default after the command is entered
  544. print( NL );
  545. info_msg("Sending: ");
  546. // Parse args until a \0 is found
  547. while ( bufferLen < i2cSend_BuffLenMax )
  548. {
  549. curArgs = arg2Ptr; // Use the previous 2nd arg pointer to separate the next arg from the list
  550. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  551. // Stop processing args if no more are found
  552. if ( *arg1Ptr == '\0' )
  553. break;
  554. // If | is found, end sequence and start new one
  555. if ( *arg1Ptr == '|' )
  556. {
  557. print("| ");
  558. I2C_Send( buffer, bufferLen, 0 );
  559. bufferLen = 0;
  560. continue;
  561. }
  562. // Interpret the argument
  563. buffer[ bufferLen++ ] = (uint8_t)numToInt( arg1Ptr );
  564. // Print out the arg
  565. dPrint( arg1Ptr );
  566. print(" ");
  567. }
  568. print( NL );
  569. I2C_Send( buffer, bufferLen, 1 ); // Only 1 byte is ever read at a time with the ISSI chip
  570. }
  571. void cliFunc_ledRPage( char* args )
  572. {
  573. // Parse number from argument
  574. // NOTE: Only first argument is used
  575. char* arg1Ptr;
  576. char* arg2Ptr;
  577. CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
  578. // Default to 0 if no argument is given
  579. uint8_t page = 0;
  580. if ( arg1Ptr[0] != '\0' )
  581. {
  582. page = (uint8_t)numToInt( arg1Ptr );
  583. }
  584. // No \r\n by default after the command is entered
  585. print( NL );
  586. LED_readPage( 0xB4, page );
  587. }
  588. void cliFunc_ledWPage( char* args )
  589. {
  590. char* curArgs;
  591. char* arg1Ptr;
  592. char* arg2Ptr = args;
  593. // First process page and starting address
  594. curArgs = arg2Ptr;
  595. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  596. // Stop processing args if no more are found
  597. if ( *arg1Ptr == '\0' )
  598. return;
  599. uint8_t page[] = { 0xE8, 0xFD, numToInt( arg1Ptr ) };
  600. curArgs = arg2Ptr;
  601. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  602. // Stop processing args if no more are found
  603. if ( *arg1Ptr == '\0' )
  604. return;
  605. uint8_t data[] = { 0xE8, numToInt( arg1Ptr ), 0 };
  606. // Set the register page
  607. while ( I2C_Send( page, sizeof( page ), 0 ) == 0 )
  608. delay(1);
  609. // Process all args
  610. for ( ;; )
  611. {
  612. curArgs = arg2Ptr;
  613. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  614. // Stop processing args if no more are found
  615. if ( *arg1Ptr == '\0' )
  616. break;
  617. data[2] = numToInt( arg1Ptr );
  618. // Write register location and data to I2C
  619. while ( I2C_Send( data, sizeof( data ), 0 ) == 0 )
  620. delay(1);
  621. // Increment address
  622. data[1]++;
  623. }
  624. }
  625. void cliFunc_ledStart( char* args )
  626. {
  627. print( NL ); // No \r\n by default after the command is entered
  628. LED_zeroPages( 0x0B, 1, 0x00, 0x0C ); // Control Registers
  629. //LED_zeroPages( 0x00, 8, 0x00, 0xB4 ); // LED Registers
  630. LED_writeReg( 0x0A, 0x01, 0x0B );
  631. LED_sendPage( (uint8_t*)LED_ledEnableMask1, sizeof( LED_ledEnableMask1 ), 0 );
  632. }
  633. void cliFunc_ledTest( char* args )
  634. {
  635. print( NL ); // No \r\n by default after the command is entered
  636. LED_sendPage( (uint8_t*)LED_defaultBrightness1, sizeof( LED_defaultBrightness1 ), 0 );
  637. }
  638. void cliFunc_ledZero( char* args )
  639. {
  640. print( NL ); // No \r\n by default after the command is entered
  641. LED_zeroPages( 0x00, 8, 0x24, 0xB4 ); // Only PWMs
  642. }