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

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