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