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

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