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