logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

is31fl3729.c (7973B)


  1. /* Copyright 2024 HorrorTroll <https://github.com/HorrorTroll>
  2. * Copyright 2024 Harrison Chan (Xelus)
  3. * Copyright 2024 Dimitris Mantzouranis <d3xter93@gmail.com>
  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. #include "is31fl3729.h"
  19. #include "i2c_master.h"
  20. #include "gpio.h"
  21. #include "wait.h"
  22. #define IS31FL3729_PWM_REGISTER_COUNT 143
  23. #define IS31FL3729_SCALING_REGISTER_COUNT 16
  24. #ifndef IS31FL3729_I2C_TIMEOUT
  25. # define IS31FL3729_I2C_TIMEOUT 100
  26. #endif
  27. #ifndef IS31FL3729_I2C_PERSISTENCE
  28. # define IS31FL3729_I2C_PERSISTENCE 0
  29. #endif
  30. #ifndef IS31FL3729_CONFIGURATION
  31. # define IS31FL3729_CONFIGURATION IS31FL3729_CONFIG_SWS_15_9
  32. #endif
  33. #ifndef IS31FL3729_GLOBAL_CURRENT
  34. # define IS31FL3729_GLOBAL_CURRENT 0x40
  35. #endif
  36. #ifndef IS31FL3729_SW_PULLDOWN
  37. # define IS31FL3729_SW_PULLDOWN IS31FL3729_SW_PULLDOWN_2K_OHM_SW_OFF
  38. #endif
  39. #ifndef IS31FL3729_CS_PULLUP
  40. # define IS31FL3729_CS_PULLUP IS31FL3729_CS_PULLUP_2K_OHM_CS_OFF
  41. #endif
  42. #ifndef IS31FL3729_SPREAD_SPECTRUM
  43. # define IS31FL3729_SPREAD_SPECTRUM IS31FL3729_SPREAD_SPECTRUM_DISABLE
  44. #endif
  45. #ifndef IS31FL3729_SPREAD_SPECTRUM_RANGE
  46. # define IS31FL3729_SPREAD_SPECTRUM_RANGE IS31FL3729_SPREAD_SPECTRUM_RANGE_5_PERCENT
  47. #endif
  48. #ifndef IS31FL3729_SPREAD_SPECTRUM_CYCLE_TIME
  49. # define IS31FL3729_SPREAD_SPECTRUM_CYCLE_TIME IS31FL3729_SPREAD_SPECTRUM_CYCLE_TIME_1980_US
  50. #endif
  51. #ifndef IS31FL3729_PWM_FREQUENCY
  52. # define IS31FL3729_PWM_FREQUENCY IS31FL3729_PWM_FREQUENCY_32K_HZ
  53. #endif
  54. const uint8_t i2c_addresses[IS31FL3729_DRIVER_COUNT] = {
  55. IS31FL3729_I2C_ADDRESS_1,
  56. #ifdef IS31FL3729_I2C_ADDRESS_2
  57. IS31FL3729_I2C_ADDRESS_2,
  58. # ifdef IS31FL3729_I2C_ADDRESS_3
  59. IS31FL3729_I2C_ADDRESS_3,
  60. # ifdef IS31FL3729_I2C_ADDRESS_4
  61. IS31FL3729_I2C_ADDRESS_4,
  62. # endif
  63. # endif
  64. #endif
  65. };
  66. // These buffers match the PWM & scaling registers.
  67. // Storing them like this is optimal for I2C transfers to the registers.
  68. typedef struct is31fl3729_driver_t {
  69. uint8_t pwm_buffer[IS31FL3729_PWM_REGISTER_COUNT];
  70. bool pwm_buffer_dirty;
  71. uint8_t scaling_buffer[IS31FL3729_SCALING_REGISTER_COUNT];
  72. bool scaling_buffer_dirty;
  73. } PACKED is31fl3729_driver_t;
  74. is31fl3729_driver_t driver_buffers[IS31FL3729_DRIVER_COUNT] = {{
  75. .pwm_buffer = {0},
  76. .pwm_buffer_dirty = false,
  77. .scaling_buffer = {0},
  78. .scaling_buffer_dirty = false,
  79. }};
  80. void is31fl3729_write_register(uint8_t index, uint8_t reg, uint8_t data) {
  81. #if IS31FL3729_I2C_PERSISTENCE > 0
  82. for (uint8_t i = 0; i < IS31FL3729_I2C_PERSISTENCE; i++) {
  83. if (i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3729_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
  84. }
  85. #else
  86. i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3729_I2C_TIMEOUT);
  87. #endif
  88. }
  89. void is31fl3729_write_pwm_buffer(uint8_t index) {
  90. // Transmit PWM registers in 11 transfers of 13 bytes.
  91. // Iterate over the pwm_buffer contents at 13 byte intervals.
  92. for (uint8_t i = 0; i <= IS31FL3729_PWM_REGISTER_COUNT; i += 13) {
  93. #if IS31FL3729_I2C_PERSISTENCE > 0
  94. for (uint8_t j = 0; j < IS31FL3729_I2C_PERSISTENCE; j++) {
  95. if (i2c_write_register(i2c_addresses[index] << 1, IS31FL3729_REG_PWM + i, driver_buffers[index].pwm_buffer + i, 13, IS31FL3729_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
  96. }
  97. #else
  98. i2c_write_register(i2c_addresses[index] << 1, IS31FL3729_REG_PWM + i, driver_buffers[index].pwm_buffer + i, 13, IS31FL3729_I2C_TIMEOUT);
  99. #endif
  100. }
  101. }
  102. void is31fl3729_init_drivers(void) {
  103. i2c_init();
  104. #if defined(IS31FL3729_SDB_PIN)
  105. gpio_set_pin_output(IS31FL3729_SDB_PIN);
  106. gpio_write_pin_high(IS31FL3729_SDB_PIN);
  107. #endif
  108. for (uint8_t i = 0; i < IS31FL3729_DRIVER_COUNT; i++) {
  109. is31fl3729_init(i);
  110. }
  111. for (int i = 0; i < IS31FL3729_LED_COUNT; i++) {
  112. is31fl3729_set_scaling_register(i, 0xFF, 0xFF, 0xFF);
  113. }
  114. for (uint8_t i = 0; i < IS31FL3729_DRIVER_COUNT; i++) {
  115. is31fl3729_update_scaling_registers(i);
  116. }
  117. }
  118. void is31fl3729_init(uint8_t index) {
  119. // In order to avoid the LEDs being driven with garbage data
  120. // in the LED driver's PWM registers, shutdown is enabled last.
  121. // Set up the mode and other settings, clear the PWM registers,
  122. // then disable software shutdown.
  123. is31fl3729_write_register(index, IS31FL3729_REG_PULLDOWNUP, ((IS31FL3729_SW_PULLDOWN & 0b111) << 4) | (IS31FL3729_CS_PULLUP & 0b111));
  124. is31fl3729_write_register(index, IS31FL3729_REG_SPREAD_SPECTRUM, ((IS31FL3729_SPREAD_SPECTRUM & 0b1) << 4) | ((IS31FL3729_SPREAD_SPECTRUM_RANGE & 0b11) << 2) | (IS31FL3729_SPREAD_SPECTRUM_CYCLE_TIME & 0b11));
  125. is31fl3729_write_register(index, IS31FL3729_REG_PWM_FREQUENCY, IS31FL3729_PWM_FREQUENCY);
  126. is31fl3729_write_register(index, IS31FL3729_REG_GLOBAL_CURRENT, IS31FL3729_GLOBAL_CURRENT);
  127. is31fl3729_write_register(index, IS31FL3729_REG_CONFIGURATION, IS31FL3729_CONFIGURATION);
  128. // Wait 10ms to ensure the device has woken up.
  129. wait_ms(10);
  130. }
  131. void is31fl3729_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) {
  132. is31fl3729_led_t led;
  133. if (index >= 0 && index < IS31FL3729_LED_COUNT) {
  134. memcpy_P(&led, (&g_is31fl3729_leds[index]), sizeof(led));
  135. if (driver_buffers[led.driver].pwm_buffer[led.r] == red && driver_buffers[led.driver].pwm_buffer[led.g] == green && driver_buffers[led.driver].pwm_buffer[led.b] == blue) {
  136. return;
  137. }
  138. driver_buffers[led.driver].pwm_buffer[led.r] = red;
  139. driver_buffers[led.driver].pwm_buffer[led.g] = green;
  140. driver_buffers[led.driver].pwm_buffer[led.b] = blue;
  141. driver_buffers[led.driver].pwm_buffer_dirty = true;
  142. }
  143. }
  144. void is31fl3729_set_color_all(uint8_t red, uint8_t green, uint8_t blue) {
  145. for (int i = 0; i < IS31FL3729_LED_COUNT; i++) {
  146. is31fl3729_set_color(i, red, green, blue);
  147. }
  148. }
  149. void is31fl3729_set_scaling_register(uint8_t index, uint8_t red, uint8_t green, uint8_t blue) {
  150. is31fl3729_led_t led;
  151. memcpy_P(&led, (&g_is31fl3729_leds[index]), sizeof(led));
  152. // need to do a bit of checking here since 3729 scaling is per CS pin.
  153. // not the usual per RGB key as per other ISSI drivers
  154. // only enable them, since they should be default disabled
  155. int cs_red = (led.r & 0x0F) - 1;
  156. int cs_green = (led.g & 0x0F) - 1;
  157. int cs_blue = (led.b & 0x0F) - 1;
  158. driver_buffers[led.driver].scaling_buffer[cs_red] = red;
  159. driver_buffers[led.driver].scaling_buffer[cs_green] = green;
  160. driver_buffers[led.driver].scaling_buffer[cs_blue] = blue;
  161. driver_buffers[led.driver].scaling_buffer_dirty = true;
  162. }
  163. void is31fl3729_update_pwm_buffers(uint8_t index) {
  164. if (driver_buffers[index].pwm_buffer_dirty) {
  165. is31fl3729_write_pwm_buffer(index);
  166. driver_buffers[index].pwm_buffer_dirty = false;
  167. }
  168. }
  169. void is31fl3729_update_scaling_registers(uint8_t index) {
  170. if (driver_buffers[index].scaling_buffer_dirty) {
  171. for (uint8_t i = 0; i < IS31FL3729_SCALING_REGISTER_COUNT; i++) {
  172. is31fl3729_write_register(index, IS31FL3729_REG_SCALING + i, driver_buffers[index].scaling_buffer[i]);
  173. }
  174. driver_buffers[index].scaling_buffer_dirty = false;
  175. }
  176. }
  177. void is31fl3729_flush(void) {
  178. for (uint8_t i = 0; i < IS31FL3729_DRIVER_COUNT; i++) {
  179. is31fl3729_update_pwm_buffers(i);
  180. }
  181. }