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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  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. // Local Includes
  24. #include "led_scan.h"
  25. // ----- Defines -----
  26. #define I2C_TxBufferLength 300
  27. #define I2C_RxBufferLength 8
  28. #define LED_BufferLength 144
  29. // ----- Structs -----
  30. typedef struct I2C_Buffer {
  31. uint16_t head;
  32. uint16_t tail;
  33. uint8_t sequencePos;
  34. uint16_t size;
  35. uint8_t *buffer;
  36. } I2C_Buffer;
  37. typedef struct LED_Buffer {
  38. uint8_t buffer[LED_BufferLength];
  39. } LED_Buffer;
  40. // ----- Function Declarations -----
  41. // CLI Functions
  42. void cliFunc_echo( char* args );
  43. void cliFunc_i2cRecv( char* args );
  44. void cliFunc_i2cSend( char* args );
  45. void cliFunc_ledTest( char* args );
  46. void cliFunc_ledZero( char* args );
  47. uint8_t I2C_TxBufferPop();
  48. void I2C_BufferPush( uint8_t byte, I2C_Buffer *buffer );
  49. uint16_t I2C_BufferLen( I2C_Buffer *buffer );
  50. uint8_t I2C_Send( uint8_t *data, uint8_t sendLen, uint8_t recvLen );
  51. // ----- Variables -----
  52. // Scan Module command dictionary
  53. 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." );
  54. CLIDict_Entry( i2cSend, "Send I2C sequence of bytes. Use |'s to split sequences with a stop." );
  55. CLIDict_Entry( ledTest, "Test out the led pages." );
  56. CLIDict_Entry( ledZero, "Zero out LED register pages (non-configuration)." );
  57. CLIDict_Def( ledCLIDict, "ISSI LED Module Commands" ) = {
  58. CLIDict_Item( i2cRecv ),
  59. CLIDict_Item( i2cSend ),
  60. CLIDict_Item( ledTest ),
  61. CLIDict_Item( ledZero ),
  62. { 0, 0, 0 } // Null entry for dictionary end
  63. };
  64. // Before sending the sequence, I2C_TxBuffer_CurLen is assigned and as each byte is sent, it is decremented
  65. // Once I2C_TxBuffer_CurLen reaches zero, a STOP on the I2C bus is sent
  66. volatile uint8_t I2C_TxBufferPtr[ I2C_TxBufferLength ];
  67. volatile uint8_t I2C_RxBufferPtr[ I2C_TxBufferLength ];
  68. volatile I2C_Buffer I2C_TxBuffer = { 0, 0, 0, I2C_TxBufferLength, (uint8_t*)I2C_TxBufferPtr };
  69. volatile I2C_Buffer I2C_RxBuffer = { 0, 0, 0, I2C_RxBufferLength, (uint8_t*)I2C_RxBufferPtr };
  70. LED_Buffer LED_pageBuffer;
  71. // A bit mask determining which LEDs are enabled in the ISSI chip
  72. // 0x00 -> 0x11
  73. const uint8_t LED_ledEnableMask[] = {
  74. 0xE8, // I2C address
  75. 0x00, // Starting register address
  76. 0xFF, 0xFF, // C1-1 -> C1-16
  77. 0xFF, 0xFF, // C2-1 -> C2-16
  78. 0xFF, 0xFF, // C3-1 -> C3-16
  79. 0xFF, 0xFF, // C4-1 -> C4-16
  80. 0xFF, 0xFF, // C5-1 -> C5-16
  81. 0xFF, 0xFF, // C6-1 -> C6-16
  82. 0xFF, 0xFF, // C7-1 -> C7-16
  83. 0xFF, 0xFF, // C8-1 -> C8-16
  84. 0xFF, 0xFF, // C9-1 -> C9-16
  85. };
  86. // XXX Pre-fill example of buffers
  87. const uint8_t examplePage[] = {
  88. 0xE8, // I2C address
  89. 0x24, // Starting register address
  90. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, // C1-1 -> C1-16
  91. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, // C2-1 -> C2-16
  92. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, // C3-1 -> C3-16
  93. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, // C4-1 -> C4-16
  94. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, // C5-1 -> C5-16
  95. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, // C6-1 -> C6-16
  96. 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, // C7-1 -> C7-16
  97. 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, // C8-1 -> C8-16
  98. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, // C9-1 -> C9-16
  99. };
  100. // ----- Interrupt Functions -----
  101. void i2c0_isr()
  102. {
  103. cli(); // Disable Interrupts
  104. uint8_t status = I2C0_S; // Read I2C Bus status
  105. // Master Mode Transmit
  106. if ( I2C0_C1 & I2C_C1_TX )
  107. {
  108. // Check current use of the I2C bus
  109. // Currently sending data
  110. if ( I2C_TxBuffer.sequencePos > 0 )
  111. {
  112. // Make sure slave sent an ACK
  113. if ( status & I2C_S_RXAK )
  114. {
  115. // NACK Detected, disable interrupt
  116. erro_print("I2C NAK detected...");
  117. I2C0_C1 = I2C_C1_IICEN;
  118. // Abort Tx Buffer
  119. I2C_TxBuffer.head = 0;
  120. I2C_TxBuffer.tail = 0;
  121. I2C_TxBuffer.sequencePos = 0;
  122. }
  123. else
  124. {
  125. // Transmit byte
  126. I2C0_D = I2C_TxBufferPop();
  127. }
  128. }
  129. // Receiving data
  130. else if ( I2C_RxBuffer.sequencePos > 0 )
  131. {
  132. // Master Receive, addr sent
  133. if ( status & I2C_S_ARBL )
  134. {
  135. // Arbitration Lost
  136. erro_print("Arbitration lost...");
  137. // TODO Abort Rx
  138. I2C0_C1 = I2C_C1_IICEN;
  139. I2C0_S = I2C_S_ARBL | I2C_S_IICIF; // Clear ARBL flag and interrupt
  140. }
  141. if ( status & I2C_S_RXAK )
  142. {
  143. // Slave Address NACK Detected, disable interrupt
  144. erro_print("Slave Address I2C NAK detected...");
  145. // TODO Abort Rx
  146. I2C0_C1 = I2C_C1_IICEN;
  147. }
  148. else
  149. {
  150. dbug_print("Attempting to read byte");
  151. I2C0_C1 = I2C_RxBuffer.sequencePos == 1
  152. ? I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_MST | I2C_C1_TXAK // Single byte read
  153. : I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_MST; // Multi-byte read
  154. }
  155. }
  156. else
  157. {
  158. /*
  159. dbug_msg("STOP - ");
  160. printHex( I2C_BufferLen( (I2C_Buffer*)&I2C_TxBuffer ) );
  161. print(NL);
  162. */
  163. // Delay around STOP to make sure it actually happens...
  164. delayMicroseconds( 1 );
  165. I2C0_C1 = I2C_C1_IICEN; // Send STOP
  166. delayMicroseconds( 7 );
  167. // If there is another sequence, start sending
  168. if ( I2C_BufferLen( (I2C_Buffer*)&I2C_TxBuffer ) < I2C_TxBuffer.size )
  169. {
  170. // Clear status flags
  171. I2C0_S = I2C_S_IICIF | I2C_S_ARBL;
  172. // Wait...till the master dies
  173. while ( I2C0_S & I2C_S_BUSY );
  174. // Enable I2C interrupt
  175. I2C0_C1 = I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_MST | I2C_C1_TX;
  176. // Transmit byte
  177. I2C0_D = I2C_TxBufferPop();
  178. }
  179. }
  180. }
  181. // Master Mode Receive
  182. else
  183. {
  184. // XXX Do we need to handle 2nd last byte?
  185. //I2C0_C1 = I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_MST | I2C_C1_TXAK; // No STOP, Rx, NAK on recv
  186. // Last byte
  187. if ( I2C_TxBuffer.sequencePos <= 1 )
  188. {
  189. // Change to Tx mode
  190. I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TX;
  191. // Grab last byte
  192. I2C_BufferPush( I2C0_D, (I2C_Buffer*)&I2C_RxBuffer );
  193. delayMicroseconds( 1 ); // Should be enough time before issuing the stop
  194. I2C0_C1 = I2C_C1_IICEN; // Send STOP
  195. }
  196. else
  197. {
  198. // Retrieve data
  199. I2C_BufferPush( I2C0_D, (I2C_Buffer*)&I2C_RxBuffer );
  200. }
  201. }
  202. I2C0_S = I2C_S_IICIF; // Clear interrupt
  203. sei(); // Re-enable Interrupts
  204. }
  205. // ----- Functions -----
  206. inline void I2C_setup()
  207. {
  208. // Enable I2C internal clock
  209. SIM_SCGC4 |= SIM_SCGC4_I2C0; // Bus 0
  210. // External pull-up resistor
  211. PORTB_PCR0 = PORT_PCR_ODE | PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(2);
  212. PORTB_PCR1 = PORT_PCR_ODE | PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(2);
  213. // SCL Frequency Divider
  214. // 400kHz -> 120 (0x85) @ 48 MHz F_BUS
  215. I2C0_F = 0x85;
  216. I2C0_FLT = 4;
  217. I2C0_C1 = I2C_C1_IICEN;
  218. I2C0_C2 = I2C_C2_HDRS; // High drive select
  219. // Enable I2C Interrupt
  220. NVIC_ENABLE_IRQ( IRQ_I2C0 );
  221. }
  222. void LED_zeroPages( uint8_t startPage, uint8_t numPages, uint8_t startReg, uint8_t endReg )
  223. {
  224. // Page Setup
  225. uint8_t pageSetup[] = { 0xE8, 0xFD, 0x00 };
  226. // Max length of a page + chip id + reg start
  227. uint8_t fullPage[ 0xB4 + 2 ] = { 0 }; // Max size of page
  228. fullPage[0] = 0xE8; // Set chip id
  229. fullPage[1] = startReg; // Set start reg
  230. // Iterate through given pages, zero'ing out the given register regions
  231. for ( uint8_t page = startPage; page < startPage + numPages; page++ )
  232. {
  233. // Set page
  234. pageSetup[2] = page;
  235. // Setup page
  236. while ( I2C_Send( pageSetup, sizeof( pageSetup ), 0 ) == 0 )
  237. delay(1);
  238. // Zero out page
  239. while ( I2C_Send( fullPage, endReg - startReg + 2, 0 ) == 0 )
  240. delay(1);
  241. }
  242. }
  243. void LED_sendPage( uint8_t *buffer, uint8_t len, uint8_t page )
  244. {
  245. // Page Setup
  246. uint8_t pageSetup[] = { 0xE8, 0xFD, page };
  247. // Setup page
  248. while ( I2C_Send( pageSetup, sizeof( pageSetup ), 0 ) == 0 )
  249. delay(1);
  250. // Write page to I2C Tx Buffer
  251. while ( I2C_Send( buffer, len, 0 ) == 0 )
  252. delay(1);
  253. }
  254. void LED_writeReg( uint8_t reg, uint8_t val, uint8_t page )
  255. {
  256. // Page Setup
  257. uint8_t pageSetup[] = { 0xE8, 0xFD, page };
  258. // Reg Write Setup
  259. uint8_t writeData[] = { 0xE8, reg, val };
  260. // Setup page
  261. while ( I2C_Send( pageSetup, sizeof( pageSetup ), 0 ) == 0 )
  262. delay(1);
  263. while ( I2C_Send( writeData, sizeof( writeData ), 0 ) == 0 )
  264. delay(1);
  265. }
  266. // Setup
  267. inline void LED_setup()
  268. {
  269. // Register Scan CLI dictionary
  270. CLI_registerDictionary( ledCLIDict, ledCLIDictName );
  271. // Initialize I2C
  272. I2C_setup();
  273. // Zero out Frame Registers
  274. // This needs to be done before disabling the hardware shutdown (or the leds will do undefined things)
  275. LED_zeroPages( 0x0B, 1, 0x00, 0x0C ); // Control Registers
  276. // Disable Hardware shutdown of ISSI chip (pull high)
  277. GPIOD_PDDR |= (1<<1);
  278. PORTD_PCR1 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  279. GPIOD_PSOR |= (1<<1);
  280. // Clear LED Pages
  281. LED_zeroPages( 0x00, 8, 0x00, 0xB4 ); // LED Registers
  282. // Enable LEDs based upon mask
  283. LED_sendPage( (uint8_t*)LED_ledEnableMask, sizeof( LED_ledEnableMask ), 0 );
  284. // Disable Software shutdown of ISSI chip
  285. LED_writeReg( 0x0A, 0x01, 0x0B );
  286. }
  287. inline uint8_t I2C_BufferCopy( uint8_t *data, uint8_t sendLen, uint8_t recvLen, I2C_Buffer *buffer )
  288. {
  289. uint8_t reTurn = 0;
  290. // If sendLen is greater than buffer fail right away
  291. if ( sendLen > buffer->size )
  292. return 0;
  293. // Calculate new tail to determine if buffer has enough space
  294. // The first element specifies the expected number of bytes from the slave (+1)
  295. // The second element in the new buffer is the length of the buffer sequence (+1)
  296. uint16_t newTail = buffer->tail + sendLen + 2;
  297. if ( newTail >= buffer->size )
  298. newTail -= buffer->size;
  299. if ( I2C_BufferLen( buffer ) < sendLen + 2 )
  300. return 0;
  301. /*
  302. print("|");
  303. printHex( sendLen + 2 );
  304. print("|");
  305. printHex( *tail );
  306. print("@");
  307. printHex( newTail );
  308. print("@");
  309. */
  310. // If buffer is clean, return 1, otherwise 2
  311. reTurn = buffer->head == buffer->tail ? 1 : 2;
  312. // Add to buffer, already know there is enough room (simplifies adding logic)
  313. uint8_t bufferHeaderPos = 0;
  314. for ( uint16_t c = 0; c < sendLen; c++ )
  315. {
  316. // Add data to buffer
  317. switch ( bufferHeaderPos )
  318. {
  319. case 0:
  320. buffer->buffer[ buffer->tail ] = recvLen;
  321. bufferHeaderPos++;
  322. c--;
  323. break;
  324. case 1:
  325. buffer->buffer[ buffer->tail ] = sendLen;
  326. bufferHeaderPos++;
  327. c--;
  328. break;
  329. default:
  330. buffer->buffer[ buffer->tail ] = data[ c ];
  331. break;
  332. }
  333. // Check for wrap-around case
  334. if ( buffer->tail + 1 >= buffer->size )
  335. {
  336. buffer->tail = 0;
  337. }
  338. // Normal case
  339. else
  340. {
  341. buffer->tail++;
  342. }
  343. }
  344. return reTurn;
  345. }
  346. inline uint16_t I2C_BufferLen( I2C_Buffer *buffer )
  347. {
  348. // Tail >= Head
  349. if ( buffer->tail >= buffer->head )
  350. return buffer->head + buffer->size - buffer->tail;
  351. // Head > Tail
  352. return buffer->head - buffer->tail;
  353. }
  354. void I2C_BufferPush( uint8_t byte, I2C_Buffer *buffer )
  355. {
  356. // Make sure buffer isn't full
  357. if ( buffer->tail + 1 == buffer->head || ( buffer->head > buffer->tail && buffer->tail + 1 - buffer->size == buffer->head ) )
  358. {
  359. warn_msg("I2C_BufferPush failed, buffer full: ");
  360. printHex( byte );
  361. print( NL );
  362. return;
  363. }
  364. // Check for wrap-around case
  365. if ( buffer->tail + 1 >= buffer->size )
  366. {
  367. buffer->tail = 0;
  368. }
  369. // Normal case
  370. else
  371. {
  372. buffer->tail++;
  373. }
  374. // Add byte to buffer
  375. buffer->buffer[ buffer->tail ] = byte;
  376. }
  377. uint8_t I2C_TxBufferPop()
  378. {
  379. // Return 0xFF if no buffer left (do not rely on this)
  380. if ( I2C_BufferLen( (I2C_Buffer*)&I2C_TxBuffer ) >= I2C_TxBuffer.size )
  381. {
  382. erro_msg("No buffer to pop an entry from... ");
  383. printHex( I2C_TxBuffer.head );
  384. print(" ");
  385. printHex( I2C_TxBuffer.tail );
  386. print(" ");
  387. printHex( I2C_TxBuffer.sequencePos );
  388. print(NL);
  389. return 0xFF;
  390. }
  391. // If there is currently no sequence being sent, the first entry in the RingBuffer is the length
  392. if ( I2C_TxBuffer.sequencePos == 0 )
  393. {
  394. I2C_TxBuffer.sequencePos = 0xFF; // So this doesn't become an infinite loop
  395. I2C_RxBuffer.sequencePos = I2C_TxBufferPop();
  396. I2C_TxBuffer.sequencePos = I2C_TxBufferPop();
  397. }
  398. uint8_t data = I2C_TxBuffer.buffer[ I2C_TxBuffer.head ];
  399. // Prune head
  400. I2C_TxBuffer.head++;
  401. // Wrap-around case
  402. if ( I2C_TxBuffer.head >= I2C_TxBuffer.size )
  403. I2C_TxBuffer.head = 0;
  404. // Decrement buffer sequence (until next stop will be sent)
  405. I2C_TxBuffer.sequencePos--;
  406. /*
  407. dbug_msg("Popping: ");
  408. printHex( data );
  409. print(" ");
  410. printHex( I2C_TxBuffer.head );
  411. print(" ");
  412. printHex( I2C_TxBuffer.tail );
  413. print(" ");
  414. printHex( I2C_TxBuffer.sequencePos );
  415. print(NL);
  416. */
  417. return data;
  418. }
  419. uint8_t I2C_Send( uint8_t *data, uint8_t sendLen, uint8_t recvLen )
  420. {
  421. // Check head and tail pointers
  422. // If full, return 0
  423. // If empty, start up I2C Master Tx
  424. // If buffer is non-empty and non-full, just append to the buffer
  425. switch ( I2C_BufferCopy( data, sendLen, recvLen, (I2C_Buffer*)&I2C_TxBuffer ) )
  426. {
  427. // Not enough buffer space...
  428. case 0:
  429. /*
  430. erro_msg("Not enough Tx buffer space... ");
  431. printHex( I2C_TxBuffer.head );
  432. print(":");
  433. printHex( I2C_TxBuffer.tail );
  434. print("+");
  435. printHex( sendLen );
  436. print("|");
  437. printHex( I2C_TxBuffer.size );
  438. print( NL );
  439. */
  440. return 0;
  441. // Empty buffer, initialize I2C
  442. case 1:
  443. // Clear status flags
  444. I2C0_S = I2C_S_IICIF | I2C_S_ARBL;
  445. // Check to see if we already have control of the bus
  446. if ( I2C0_C1 & I2C_C1_MST )
  447. {
  448. // Already the master (ah yeah), send a repeated start
  449. I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_RSTA | I2C_C1_TX;
  450. }
  451. // Otherwise, seize control
  452. else
  453. {
  454. // Wait...till the master dies
  455. while ( I2C0_S & I2C_S_BUSY );
  456. // Now we're the master (ah yisss), get ready to send stuffs
  457. I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TX;
  458. }
  459. // Enable I2C interrupt
  460. I2C0_C1 = I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_MST | I2C_C1_TX;
  461. // Depending on what type of transfer, the first byte is configured for R or W
  462. I2C0_D = I2C_TxBufferPop();
  463. return 1;
  464. }
  465. // Dirty buffer, I2C already initialized
  466. return 2;
  467. }
  468. // LED State processing loop
  469. inline uint8_t LED_scan()
  470. {
  471. // I2C Busy
  472. // S & I2C_S_BUSY
  473. //I2C_S_BUSY
  474. return 0;
  475. }
  476. // ----- CLI Command Functions -----
  477. void cliFunc_i2cSend( char* args )
  478. {
  479. char* curArgs;
  480. char* arg1Ptr;
  481. char* arg2Ptr = args;
  482. // Buffer used after interpretting the args, will be sent to I2C functions
  483. // NOTE: Limited to 8 bytes currently (can be increased if necessary
  484. #define i2cSend_BuffLenMax 8
  485. uint8_t buffer[ i2cSend_BuffLenMax ];
  486. uint8_t bufferLen = 0;
  487. // No \r\n by default after the command is entered
  488. print( NL );
  489. info_msg("Sending: ");
  490. // Parse args until a \0 is found
  491. while ( bufferLen < i2cSend_BuffLenMax )
  492. {
  493. curArgs = arg2Ptr; // Use the previous 2nd arg pointer to separate the next arg from the list
  494. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  495. // Stop processing args if no more are found
  496. if ( *arg1Ptr == '\0' )
  497. break;
  498. // If | is found, end sequence and start new one
  499. if ( *arg1Ptr == '|' )
  500. {
  501. print("| ");
  502. I2C_Send( buffer, bufferLen, 0 );
  503. bufferLen = 0;
  504. continue;
  505. }
  506. // Interpret the argument
  507. buffer[ bufferLen++ ] = (uint8_t)numToInt( arg1Ptr );
  508. // Print out the arg
  509. dPrint( arg1Ptr );
  510. print(" ");
  511. }
  512. print( NL );
  513. I2C_Send( buffer, bufferLen, 0 );
  514. }
  515. void cliFunc_i2cRecv( char* args )
  516. {
  517. char* curArgs;
  518. char* arg1Ptr;
  519. char* arg2Ptr = args;
  520. // Buffer used after interpretting the args, will be sent to I2C functions
  521. // NOTE: Limited to 8 bytes currently (can be increased if necessary
  522. #define i2cSend_BuffLenMax 8
  523. uint8_t buffer[ i2cSend_BuffLenMax ];
  524. uint8_t bufferLen = 0;
  525. // No \r\n by default after the command is entered
  526. print( NL );
  527. info_msg("Sending: ");
  528. // Parse args until a \0 is found
  529. while ( bufferLen < i2cSend_BuffLenMax )
  530. {
  531. curArgs = arg2Ptr; // Use the previous 2nd arg pointer to separate the next arg from the list
  532. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  533. // Stop processing args if no more are found
  534. if ( *arg1Ptr == '\0' )
  535. break;
  536. // If | is found, end sequence and start new one
  537. if ( *arg1Ptr == '|' )
  538. {
  539. print("| ");
  540. I2C_Send( buffer, bufferLen, 0 );
  541. bufferLen = 0;
  542. continue;
  543. }
  544. // Interpret the argument
  545. buffer[ bufferLen++ ] = (uint8_t)numToInt( arg1Ptr );
  546. // Print out the arg
  547. dPrint( arg1Ptr );
  548. print(" ");
  549. }
  550. print( NL );
  551. I2C_Send( buffer, bufferLen, 1 ); // Only 1 byte is ever read at a time with the ISSI chip
  552. }
  553. void cliFunc_ledTest( char* args )
  554. {
  555. print( NL ); // No \r\n by default after the command is entered
  556. LED_sendPage( (uint8_t*)examplePage, sizeof( examplePage ), 0 );
  557. }
  558. void cliFunc_ledZero( char* args )
  559. {
  560. print( NL ); // No \r\n by default after the command is entered
  561. LED_zeroPages( 0x00, 8, 0x24, 0xB4 ); // Only PWMs
  562. }