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

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