diff options
Diffstat (limited to 'drivers/issi/is31fl3731-simple.c')
-rw-r--r-- | drivers/issi/is31fl3731-simple.c | 240 |
1 files changed, 240 insertions, 0 deletions
diff --git a/drivers/issi/is31fl3731-simple.c b/drivers/issi/is31fl3731-simple.c new file mode 100644 index 000000000..46d51dac7 --- /dev/null +++ b/drivers/issi/is31fl3731-simple.c | |||
@@ -0,0 +1,240 @@ | |||
1 | /* Copyright 2017 Jason Williams | ||
2 | * Copyright 2018 Jack Humbert | ||
3 | * | ||
4 | * This program is free software: you can redistribute it and/or modify | ||
5 | * it under the terms of the GNU General Public License as published by | ||
6 | * the Free Software Foundation, either version 2 of the License, or | ||
7 | * (at your option) any later version. | ||
8 | * | ||
9 | * This program is distributed in the hope that it will be useful, | ||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | * GNU General Public License for more details. | ||
13 | * | ||
14 | * You should have received a copy of the GNU General Public License | ||
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
16 | */ | ||
17 | |||
18 | #ifdef __AVR__ | ||
19 | #include <avr/interrupt.h> | ||
20 | #include <avr/io.h> | ||
21 | #include <util/delay.h> | ||
22 | #else | ||
23 | #include "wait.h" | ||
24 | #endif | ||
25 | |||
26 | #include <stdint.h> | ||
27 | #include <stdbool.h> | ||
28 | #include <string.h> | ||
29 | #include "is31fl3731-simple.h" | ||
30 | #include "i2c_master.h" | ||
31 | #include "progmem.h" | ||
32 | |||
33 | // This is a 7-bit address, that gets left-shifted and bit 0 | ||
34 | // set to 0 for write, 1 for read (as per I2C protocol) | ||
35 | // The address will vary depending on your wiring: | ||
36 | // 0b1110100 AD <-> GND | ||
37 | // 0b1110111 AD <-> VCC | ||
38 | // 0b1110101 AD <-> SCL | ||
39 | // 0b1110110 AD <-> SDA | ||
40 | #define ISSI_ADDR_DEFAULT 0x74 | ||
41 | |||
42 | #define ISSI_REG_CONFIG 0x00 | ||
43 | #define ISSI_REG_CONFIG_PICTUREMODE 0x00 | ||
44 | #define ISSI_REG_CONFIG_AUTOPLAYMODE 0x08 | ||
45 | #define ISSI_REG_CONFIG_AUDIOPLAYMODE 0x18 | ||
46 | |||
47 | #define ISSI_CONF_PICTUREMODE 0x00 | ||
48 | #define ISSI_CONF_AUTOFRAMEMODE 0x04 | ||
49 | #define ISSI_CONF_AUDIOMODE 0x08 | ||
50 | |||
51 | #define ISSI_REG_PICTUREFRAME 0x01 | ||
52 | |||
53 | #define ISSI_REG_SHUTDOWN 0x0A | ||
54 | #define ISSI_REG_AUDIOSYNC 0x06 | ||
55 | |||
56 | #define ISSI_COMMANDREGISTER 0xFD | ||
57 | #define ISSI_BANK_FUNCTIONREG 0x0B // helpfully called 'page nine' | ||
58 | |||
59 | #ifndef ISSI_TIMEOUT | ||
60 | #define ISSI_TIMEOUT 100 | ||
61 | #endif | ||
62 | |||
63 | #ifndef ISSI_PERSISTENCE | ||
64 | #define ISSI_PERSISTENCE 0 | ||
65 | #endif | ||
66 | |||
67 | // Transfer buffer for TWITransmitData() | ||
68 | uint8_t g_twi_transfer_buffer[20]; | ||
69 | |||
70 | // These buffers match the IS31FL3731 PWM registers 0x24-0xB3. | ||
71 | // Storing them like this is optimal for I2C transfers to the registers. | ||
72 | // We could optimize this and take out the unused registers from these | ||
73 | // buffers and the transfers in IS31FL3731_write_pwm_buffer() but it's | ||
74 | // probably not worth the extra complexity. | ||
75 | uint8_t g_pwm_buffer[DRIVER_COUNT][144]; | ||
76 | bool g_pwm_buffer_update_required = false; | ||
77 | |||
78 | uint8_t g_led_control_registers[DRIVER_COUNT][18] = { { 0 }, { 0 } }; | ||
79 | bool g_led_control_registers_update_required = false; | ||
80 | |||
81 | // This is the bit pattern in the LED control registers | ||
82 | // (for matrix A, add one to register for matrix B) | ||
83 | // | ||
84 | // reg - b7 b6 b5 b4 b3 b2 b1 b0 | ||
85 | // 0x00 - R08,R07,R06,R05,R04,R03,R02,R01 | ||
86 | // 0x02 - G08,G07,G06,G05,G04,G03,G02,R00 | ||
87 | // 0x04 - B08,B07,B06,B05,B04,B03,G01,G00 | ||
88 | // 0x06 - - , - , - , - , - ,B02,B01,B00 | ||
89 | // 0x08 - - , - , - , - , - , - , - , - | ||
90 | // 0x0A - B17,B16,B15, - , - , - , - , - | ||
91 | // 0x0C - G17,G16,B14,B13,B12,B11,B10,B09 | ||
92 | // 0x0E - R17,G15,G14,G13,G12,G11,G10,G09 | ||
93 | // 0x10 - R16,R15,R14,R13,R12,R11,R10,R09 | ||
94 | |||
95 | |||
96 | void IS31FL3731_write_register(uint8_t addr, uint8_t reg, uint8_t data) { | ||
97 | g_twi_transfer_buffer[0] = reg; | ||
98 | g_twi_transfer_buffer[1] = data; | ||
99 | |||
100 | #if ISSI_PERSISTENCE > 0 | ||
101 | for (uint8_t i = 0; i < ISSI_PERSISTENCE; i++) { | ||
102 | if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 2, ISSI_TIMEOUT) == 0) { | ||
103 | break; | ||
104 | } | ||
105 | } | ||
106 | #else | ||
107 | i2c_transmit(addr << 1, g_twi_transfer_buffer, 2, ISSI_TIMEOUT); | ||
108 | #endif | ||
109 | } | ||
110 | |||
111 | void IS31FL3731_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer) { | ||
112 | // assumes bank is already selected | ||
113 | |||
114 | // transmit PWM registers in 9 transfers of 16 bytes | ||
115 | // g_twi_transfer_buffer[] is 20 bytes | ||
116 | |||
117 | // iterate over the pwm_buffer contents at 16 byte intervals | ||
118 | for (int i = 0; i < 144; i += 16) { | ||
119 | // set the first register, e.g. 0x24, 0x34, 0x44, etc. | ||
120 | g_twi_transfer_buffer[0] = 0x24 + i; | ||
121 | // copy the data from i to i+15 | ||
122 | // device will auto-increment register for data after the first byte | ||
123 | // thus this sets registers 0x24-0x33, 0x34-0x43, etc. in one transfer | ||
124 | for (int j = 0; j < 16; j++) { | ||
125 | g_twi_transfer_buffer[1 + j] = pwm_buffer[i + j]; | ||
126 | } | ||
127 | |||
128 | #if ISSI_PERSISTENCE > 0 | ||
129 | for (uint8_t i = 0; i < ISSI_PERSISTENCE; i++) { | ||
130 | if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 17, ISSI_TIMEOUT) == 0) | ||
131 | break; | ||
132 | } | ||
133 | #else | ||
134 | i2c_transmit(addr << 1, g_twi_transfer_buffer, 17, ISSI_TIMEOUT); | ||
135 | #endif | ||
136 | } | ||
137 | } | ||
138 | |||
139 | void IS31FL3731_init(uint8_t addr) { | ||
140 | // In order to avoid the LEDs being driven with garbage data | ||
141 | // in the LED driver's PWM registers, first enable software shutdown, | ||
142 | // then set up the mode and other settings, clear the PWM registers, | ||
143 | // then disable software shutdown. | ||
144 | |||
145 | // select "function register" bank | ||
146 | IS31FL3731_write_register(addr, ISSI_COMMANDREGISTER, ISSI_BANK_FUNCTIONREG); | ||
147 | |||
148 | // enable software shutdown | ||
149 | IS31FL3731_write_register(addr, ISSI_REG_SHUTDOWN, 0x00); | ||
150 | // this delay was copied from other drivers, might not be needed | ||
151 | // FIXME: Don't we have a wrapper for this already? | ||
152 | #ifdef __AVR__ | ||
153 | _delay_ms(10); | ||
154 | #else | ||
155 | wait_ms(10); | ||
156 | #endif | ||
157 | |||
158 | // picture mode | ||
159 | IS31FL3731_write_register(addr, ISSI_REG_CONFIG, ISSI_REG_CONFIG_PICTUREMODE); | ||
160 | // display frame 0 | ||
161 | IS31FL3731_write_register(addr, ISSI_REG_PICTUREFRAME, 0x00); | ||
162 | // audio sync off | ||
163 | IS31FL3731_write_register(addr, ISSI_REG_AUDIOSYNC, 0x00); | ||
164 | |||
165 | // select bank 0 | ||
166 | IS31FL3731_write_register(addr, ISSI_COMMANDREGISTER, 0); | ||
167 | |||
168 | // turn off all LEDs in the LED control register | ||
169 | for (int i = 0x00; i <= 0x11; i++) { | ||
170 | IS31FL3731_write_register(addr, i, 0x00); | ||
171 | } | ||
172 | |||
173 | // turn off all LEDs in the blink control register (not really needed) | ||
174 | for (int i = 0x12; i <= 0x23; i++) { | ||
175 | IS31FL3731_write_register(addr, i, 0x00); | ||
176 | } | ||
177 | |||
178 | // set PWM on all LEDs to 0 | ||
179 | for (int i = 0x24; i <= 0xB3; i++) { | ||
180 | IS31FL3731_write_register(addr, i, 0x00); | ||
181 | } | ||
182 | |||
183 | // select "function register" bank | ||
184 | IS31FL3731_write_register(addr, ISSI_COMMANDREGISTER, ISSI_BANK_FUNCTIONREG); | ||
185 | |||
186 | // disable software shutdown | ||
187 | IS31FL3731_write_register(addr, ISSI_REG_SHUTDOWN, 0x01); | ||
188 | |||
189 | // select bank 0 and leave it selected. | ||
190 | // most usage after initialization is just writing PWM buffers in bank 0 | ||
191 | // as there's not much point in double-buffering | ||
192 | IS31FL3731_write_register(addr, ISSI_COMMANDREGISTER, 0); | ||
193 | |||
194 | } | ||
195 | |||
196 | void IS31FL3731_set_value(int index, uint8_t value) { | ||
197 | if (index >= 0 && index < DRIVER_LED_TOTAL) { | ||
198 | is31_led led = g_is31_leds[index]; | ||
199 | |||
200 | // Subtract 0x24 to get the second index of g_pwm_buffer | ||
201 | g_pwm_buffer[led.driver][led.v - 0x24] = value; | ||
202 | g_pwm_buffer_update_required = true; | ||
203 | } | ||
204 | } | ||
205 | |||
206 | void IS31FL3731_set_value_all(uint8_t value) { | ||
207 | for (int i = 0; i < DRIVER_LED_TOTAL; i++) { | ||
208 | IS31FL3731_set_value(i, value); | ||
209 | } | ||
210 | } | ||
211 | |||
212 | void IS31FL3731_set_led_control_register(uint8_t index, bool value) { | ||
213 | is31_led led = g_is31_leds[index]; | ||
214 | |||
215 | uint8_t control_register = (led.v - 0x24) / 8; | ||
216 | uint8_t bit_value = (led.v - 0x24) % 8; | ||
217 | |||
218 | if (value) { | ||
219 | g_led_control_registers[led.driver][control_register] |= (1 << bit_value); | ||
220 | } else { | ||
221 | g_led_control_registers[led.driver][control_register] &= ~(1 << bit_value); | ||
222 | } | ||
223 | |||
224 | g_led_control_registers_update_required = true; | ||
225 | } | ||
226 | |||
227 | void IS31FL3731_update_pwm_buffers(uint8_t addr, uint8_t index) { | ||
228 | if (g_pwm_buffer_update_required) { | ||
229 | IS31FL3731_write_pwm_buffer(addr, g_pwm_buffer[index]); | ||
230 | g_pwm_buffer_update_required = false; | ||
231 | } | ||
232 | } | ||
233 | |||
234 | void IS31FL3731_update_led_control_registers(uint8_t addr, uint8_t index) { | ||
235 | if (g_led_control_registers_update_required) { | ||
236 | for (int i=0; i<18; i++) { | ||
237 | IS31FL3731_write_register(addr, i, g_led_control_registers[index][i]); | ||
238 | } | ||
239 | } | ||
240 | } | ||