You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

235 lines
7.7 KiB

  1. /* Copyright 2017 Jason Williams
  2. * Copyright 2018 Jack Humbert
  3. * Copyright 2018 Yiancar
  4. * Copyright 2020 MelGeek
  5. * Copyright 2021 MasterSpoon
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include "is31fl3745.h"
  21. #include "i2c_master.h"
  22. #include "wait.h"
  23. #define IS31FL3745_PWM_REGISTER_COUNT 144
  24. #define IS31FL3745_SCALING_REGISTER_COUNT 144
  25. #ifndef IS31FL3745_I2C_TIMEOUT
  26. # define IS31FL3745_I2C_TIMEOUT 100
  27. #endif
  28. #ifndef IS31FL3745_I2C_PERSISTENCE
  29. # define IS31FL3745_I2C_PERSISTENCE 0
  30. #endif
  31. #ifndef IS31FL3745_CONFIGURATION
  32. # define IS31FL3745_CONFIGURATION 0x31
  33. #endif
  34. #ifndef IS31FL3745_SW_PULLDOWN
  35. # define IS31FL3745_SW_PULLDOWN IS31FL3745_PDR_2K_OHM_SW_OFF
  36. #endif
  37. #ifndef IS31FL3745_CS_PULLUP
  38. # define IS31FL3745_CS_PULLUP IS31FL3745_PUR_2K_OHM_CS_OFF
  39. #endif
  40. #ifndef IS31FL3745_GLOBAL_CURRENT
  41. # define IS31FL3745_GLOBAL_CURRENT 0xFF
  42. #endif
  43. #ifndef IS31FL3745_SYNC_1
  44. # define IS31FL3745_SYNC_1 IS31FL3745_SYNC_NONE
  45. #endif
  46. #ifndef IS31FL3745_SYNC_2
  47. # define IS31FL3745_SYNC_2 IS31FL3745_SYNC_NONE
  48. #endif
  49. #ifndef IS31FL3745_SYNC_3
  50. # define IS31FL3745_SYNC_3 IS31FL3745_SYNC_NONE
  51. #endif
  52. #ifndef IS31FL3745_SYNC_4
  53. # define IS31FL3745_SYNC_4 IS31FL3745_SYNC_NONE
  54. #endif
  55. const uint8_t i2c_addresses[IS31FL3745_DRIVER_COUNT] = {
  56. IS31FL3745_I2C_ADDRESS_1,
  57. #ifdef IS31FL3745_I2C_ADDRESS_2
  58. IS31FL3745_I2C_ADDRESS_2,
  59. # ifdef IS31FL3745_I2C_ADDRESS_3
  60. IS31FL3745_I2C_ADDRESS_3,
  61. # ifdef IS31FL3745_I2C_ADDRESS_4
  62. IS31FL3745_I2C_ADDRESS_4,
  63. # endif
  64. # endif
  65. #endif
  66. };
  67. const uint8_t driver_sync[IS31FL3745_DRIVER_COUNT] = {
  68. IS31FL3745_SYNC_1,
  69. #ifdef IS31FL3745_I2C_ADDRESS_2
  70. IS31FL3745_SYNC_2,
  71. # ifdef IS31FL3745_I2C_ADDRESS_3
  72. IS31FL3745_SYNC_3,
  73. # ifdef IS31FL3745_I2C_ADDRESS_4
  74. IS31FL3745_SYNC_4,
  75. # endif
  76. # endif
  77. #endif
  78. };
  79. uint8_t g_pwm_buffer[IS31FL3745_DRIVER_COUNT][IS31FL3745_PWM_REGISTER_COUNT];
  80. bool g_pwm_buffer_update_required[IS31FL3745_DRIVER_COUNT] = {false};
  81. bool g_scaling_registers_update_required[IS31FL3745_DRIVER_COUNT] = {false};
  82. uint8_t g_scaling_registers[IS31FL3745_DRIVER_COUNT][IS31FL3745_SCALING_REGISTER_COUNT];
  83. void is31fl3745_write_register(uint8_t index, uint8_t reg, uint8_t data) {
  84. #if IS31FL3745_I2C_PERSISTENCE > 0
  85. for (uint8_t i = 0; i < IS31FL3745_I2C_PERSISTENCE; i++) {
  86. if (i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3745_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
  87. }
  88. #else
  89. i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3745_I2C_TIMEOUT);
  90. #endif
  91. }
  92. void is31fl3745_select_page(uint8_t index, uint8_t page) {
  93. is31fl3745_write_register(index, IS31FL3745_REG_COMMAND_WRITE_LOCK, IS31FL3745_COMMAND_WRITE_LOCK_MAGIC);
  94. is31fl3745_write_register(index, IS31FL3745_REG_COMMAND, page);
  95. }
  96. void is31fl3745_write_pwm_buffer(uint8_t index) {
  97. // Assumes page 0 is already selected.
  98. // Transmit PWM registers in 8 transfers of 18 bytes.
  99. // Iterate over the pwm_buffer contents at 18 byte intervals.
  100. for (uint8_t i = 0; i < IS31FL3745_PWM_REGISTER_COUNT; i += 18) {
  101. #if IS31FL3745_I2C_PERSISTENCE > 0
  102. for (uint8_t j = 0; j < IS31FL3745_I2C_PERSISTENCE; j++) {
  103. if (i2c_write_register(i2c_addresses[index] << 1, i + 1, g_pwm_buffer[index] + i, 18, IS31FL3745_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
  104. }
  105. #else
  106. i2c_write_register(i2c_addresses[index] << 1, i + 1, g_pwm_buffer[index] + i, 18, IS31FL3745_I2C_TIMEOUT);
  107. #endif
  108. }
  109. }
  110. void is31fl3745_init_drivers(void) {
  111. i2c_init();
  112. for (uint8_t i = 0; i < IS31FL3745_DRIVER_COUNT; i++) {
  113. is31fl3745_init(i);
  114. }
  115. for (int i = 0; i < IS31FL3745_LED_COUNT; i++) {
  116. is31fl3745_set_scaling_register(i, 0xFF, 0xFF, 0xFF);
  117. }
  118. for (uint8_t i = 0; i < IS31FL3745_DRIVER_COUNT; i++) {
  119. is31fl3745_update_scaling_registers(i);
  120. }
  121. }
  122. void is31fl3745_init(uint8_t index) {
  123. // In order to avoid the LEDs being driven with garbage data
  124. // in the LED driver's PWM registers, shutdown is enabled last.
  125. // Set up the mode and other settings, clear the PWM registers,
  126. // then disable software shutdown.
  127. is31fl3745_select_page(index, IS31FL3745_COMMAND_SCALING);
  128. // Turn off all LEDs.
  129. for (uint8_t i = 0; i < IS31FL3745_SCALING_REGISTER_COUNT; i++) {
  130. is31fl3745_write_register(index, i + 1, 0x00);
  131. }
  132. is31fl3745_select_page(index, IS31FL3745_COMMAND_PWM);
  133. for (uint8_t i = 0; i < IS31FL3745_PWM_REGISTER_COUNT; i++) {
  134. is31fl3745_write_register(index, i + 1, 0x00);
  135. }
  136. is31fl3745_select_page(index, IS31FL3745_COMMAND_FUNCTION);
  137. uint8_t sync = driver_sync[index];
  138. is31fl3745_write_register(index, IS31FL3745_FUNCTION_REG_PULLDOWNUP, (IS31FL3745_SW_PULLDOWN << 4) | IS31FL3745_CS_PULLUP);
  139. is31fl3745_write_register(index, IS31FL3745_FUNCTION_REG_GLOBAL_CURRENT, IS31FL3745_GLOBAL_CURRENT);
  140. is31fl3745_write_register(index, IS31FL3745_FUNCTION_REG_SPREAD_SPECTRUM, (sync & 0b11) << 6);
  141. is31fl3745_write_register(index, IS31FL3745_FUNCTION_REG_CONFIGURATION, IS31FL3745_CONFIGURATION);
  142. // Wait 10ms to ensure the device has woken up.
  143. wait_ms(10);
  144. }
  145. void is31fl3745_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) {
  146. is31fl3745_led_t led;
  147. if (index >= 0 && index < IS31FL3745_LED_COUNT) {
  148. memcpy_P(&led, (&g_is31fl3745_leds[index]), sizeof(led));
  149. if (g_pwm_buffer[led.driver][led.r] == red && g_pwm_buffer[led.driver][led.g] == green && g_pwm_buffer[led.driver][led.b] == blue) {
  150. return;
  151. }
  152. g_pwm_buffer_update_required[led.driver] = true;
  153. g_pwm_buffer[led.driver][led.r] = red;
  154. g_pwm_buffer[led.driver][led.g] = green;
  155. g_pwm_buffer[led.driver][led.b] = blue;
  156. }
  157. }
  158. void is31fl3745_set_color_all(uint8_t red, uint8_t green, uint8_t blue) {
  159. for (int i = 0; i < IS31FL3745_LED_COUNT; i++) {
  160. is31fl3745_set_color(i, red, green, blue);
  161. }
  162. }
  163. void is31fl3745_set_scaling_register(uint8_t index, uint8_t red, uint8_t green, uint8_t blue) {
  164. is31fl3745_led_t led;
  165. memcpy_P(&led, (&g_is31fl3745_leds[index]), sizeof(led));
  166. g_scaling_registers[led.driver][led.r] = red;
  167. g_scaling_registers[led.driver][led.g] = green;
  168. g_scaling_registers[led.driver][led.b] = blue;
  169. g_scaling_registers_update_required[led.driver] = true;
  170. }
  171. void is31fl3745_update_pwm_buffers(uint8_t index) {
  172. if (g_pwm_buffer_update_required[index]) {
  173. is31fl3745_select_page(index, IS31FL3745_COMMAND_PWM);
  174. is31fl3745_write_pwm_buffer(index);
  175. g_pwm_buffer_update_required[index] = false;
  176. }
  177. }
  178. void is31fl3745_update_scaling_registers(uint8_t index) {
  179. if (g_scaling_registers_update_required[index]) {
  180. is31fl3745_select_page(index, IS31FL3745_COMMAND_SCALING);
  181. for (uint8_t i = 0; i < IS31FL3745_SCALING_REGISTER_COUNT; i++) {
  182. is31fl3745_write_register(index, i + 1, g_scaling_registers[index][i]);
  183. }
  184. g_scaling_registers_update_required[index] = false;
  185. }
  186. }
  187. void is31fl3745_flush(void) {
  188. for (uint8_t i = 0; i < IS31FL3745_DRIVER_COUNT; i++) {
  189. is31fl3745_update_pwm_buffers(i);
  190. }
  191. }