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.

239 lines
8.4 KiB

  1. /* Copyright 2017 Jason Williams
  2. * Copyright 2018 Jack Humbert
  3. * Copyright 2018 Yiancar
  4. * Copyright 2021 Doni Crosby
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "is31fl3737.h"
  20. #include "i2c_master.h"
  21. #include "wait.h"
  22. #define IS31FL3737_PWM_REGISTER_COUNT 192 // actually 144
  23. #define IS31FL3737_LED_CONTROL_REGISTER_COUNT 24
  24. #ifndef IS31FL3737_I2C_TIMEOUT
  25. # define IS31FL3737_I2C_TIMEOUT 100
  26. #endif
  27. #ifndef IS31FL3737_I2C_PERSISTENCE
  28. # define IS31FL3737_I2C_PERSISTENCE 0
  29. #endif
  30. #ifndef IS31FL3737_PWM_FREQUENCY
  31. # define IS31FL3737_PWM_FREQUENCY IS31FL3737_PWM_FREQUENCY_8K4_HZ // PFS - IS31FL3737B only
  32. #endif
  33. #ifndef IS31FL3737_SW_PULLUP
  34. # define IS31FL3737_SW_PULLUP IS31FL3737_PUR_0_OHM
  35. #endif
  36. #ifndef IS31FL3737_CS_PULLDOWN
  37. # define IS31FL3737_CS_PULLDOWN IS31FL3737_PDR_0_OHM
  38. #endif
  39. #ifndef IS31FL3737_GLOBAL_CURRENT
  40. # define IS31FL3737_GLOBAL_CURRENT 0xFF
  41. #endif
  42. const uint8_t i2c_addresses[IS31FL3737_DRIVER_COUNT] = {
  43. IS31FL3737_I2C_ADDRESS_1,
  44. #ifdef IS31FL3737_I2C_ADDRESS_2
  45. IS31FL3737_I2C_ADDRESS_2,
  46. # ifdef IS31FL3737_I2C_ADDRESS_3
  47. IS31FL3737_I2C_ADDRESS_3,
  48. # ifdef IS31FL3737_I2C_ADDRESS_4
  49. IS31FL3737_I2C_ADDRESS_4,
  50. # endif
  51. # endif
  52. #endif
  53. };
  54. // These buffers match the IS31FL3737 PWM registers.
  55. // The control buffers match the page 0 LED On/Off registers.
  56. // Storing them like this is optimal for I2C transfers to the registers.
  57. // We could optimize this and take out the unused registers from these
  58. // buffers and the transfers in is31fl3737_write_pwm_buffer() but it's
  59. // probably not worth the extra complexity.
  60. uint8_t g_pwm_buffer[IS31FL3737_DRIVER_COUNT][IS31FL3737_PWM_REGISTER_COUNT];
  61. bool g_pwm_buffer_update_required[IS31FL3737_DRIVER_COUNT] = {false};
  62. uint8_t g_led_control_registers[IS31FL3737_DRIVER_COUNT][IS31FL3737_LED_CONTROL_REGISTER_COUNT] = {0};
  63. bool g_led_control_registers_update_required[IS31FL3737_DRIVER_COUNT] = {false};
  64. void is31fl3737_write_register(uint8_t index, uint8_t reg, uint8_t data) {
  65. #if IS31FL3737_I2C_PERSISTENCE > 0
  66. for (uint8_t i = 0; i < IS31FL3737_I2C_PERSISTENCE; i++) {
  67. if (i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3737_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
  68. }
  69. #else
  70. i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3737_I2C_TIMEOUT);
  71. #endif
  72. }
  73. void is31fl3737_select_page(uint8_t index, uint8_t page) {
  74. is31fl3737_write_register(index, IS31FL3737_REG_COMMAND_WRITE_LOCK, IS31FL3737_COMMAND_WRITE_LOCK_MAGIC);
  75. is31fl3737_write_register(index, IS31FL3737_REG_COMMAND, page);
  76. }
  77. void is31fl3737_write_pwm_buffer(uint8_t index) {
  78. // Assumes page 1 is already selected.
  79. // Transmit PWM registers in 12 transfers of 16 bytes.
  80. // Iterate over the pwm_buffer contents at 16 byte intervals.
  81. for (uint8_t i = 0; i < IS31FL3737_PWM_REGISTER_COUNT; i += 16) {
  82. #if IS31FL3737_I2C_PERSISTENCE > 0
  83. for (uint8_t j = 0; j < IS31FL3737_I2C_PERSISTENCE; j++) {
  84. if (i2c_write_register(i2c_addresses[index] << 1, i, g_pwm_buffer[index] + i, 16, IS31FL3737_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
  85. }
  86. #else
  87. i2c_write_register(i2c_addresses[index] << 1, i, g_pwm_buffer[index] + i, 16, IS31FL3737_I2C_TIMEOUT);
  88. #endif
  89. }
  90. }
  91. void is31fl3737_init_drivers(void) {
  92. i2c_init();
  93. for (uint8_t i = 0; i < IS31FL3737_DRIVER_COUNT; i++) {
  94. is31fl3737_init(i);
  95. }
  96. for (int i = 0; i < IS31FL3737_LED_COUNT; i++) {
  97. is31fl3737_set_led_control_register(i, true, true, true);
  98. }
  99. for (uint8_t i = 0; i < IS31FL3737_DRIVER_COUNT; i++) {
  100. is31fl3737_update_led_control_registers(i);
  101. }
  102. }
  103. void is31fl3737_init(uint8_t index) {
  104. // In order to avoid the LEDs being driven with garbage data
  105. // in the LED driver's PWM registers, shutdown is enabled last.
  106. // Set up the mode and other settings, clear the PWM registers,
  107. // then disable software shutdown.
  108. is31fl3737_select_page(index, IS31FL3737_COMMAND_LED_CONTROL);
  109. // Turn off all LEDs.
  110. for (uint8_t i = 0; i < IS31FL3737_LED_CONTROL_REGISTER_COUNT; i++) {
  111. is31fl3737_write_register(index, i, 0x00);
  112. }
  113. is31fl3737_select_page(index, IS31FL3737_COMMAND_PWM);
  114. // Set PWM on all LEDs to 0
  115. // No need to setup Breath registers to PWM as that is the default.
  116. for (uint8_t i = 0; i < IS31FL3737_PWM_REGISTER_COUNT; i++) {
  117. is31fl3737_write_register(index, i, 0x00);
  118. }
  119. is31fl3737_select_page(index, IS31FL3737_COMMAND_FUNCTION);
  120. // Set de-ghost pull-up resistors (SWx)
  121. is31fl3737_write_register(index, IS31FL3737_FUNCTION_REG_SW_PULLUP, IS31FL3737_SW_PULLUP);
  122. // Set de-ghost pull-down resistors (CSx)
  123. is31fl3737_write_register(index, IS31FL3737_FUNCTION_REG_CS_PULLDOWN, IS31FL3737_CS_PULLDOWN);
  124. // Set global current to maximum.
  125. is31fl3737_write_register(index, IS31FL3737_FUNCTION_REG_GLOBAL_CURRENT, IS31FL3737_GLOBAL_CURRENT);
  126. // Disable software shutdown.
  127. is31fl3737_write_register(index, IS31FL3737_FUNCTION_REG_CONFIGURATION, ((IS31FL3737_PWM_FREQUENCY & 0b111) << 3) | 0x01);
  128. // Wait 10ms to ensure the device has woken up.
  129. wait_ms(10);
  130. }
  131. void is31fl3737_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) {
  132. is31fl3737_led_t led;
  133. if (index >= 0 && index < IS31FL3737_LED_COUNT) {
  134. memcpy_P(&led, (&g_is31fl3737_leds[index]), sizeof(led));
  135. 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) {
  136. return;
  137. }
  138. g_pwm_buffer[led.driver][led.r] = red;
  139. g_pwm_buffer[led.driver][led.g] = green;
  140. g_pwm_buffer[led.driver][led.b] = blue;
  141. g_pwm_buffer_update_required[led.driver] = true;
  142. }
  143. }
  144. void is31fl3737_set_color_all(uint8_t red, uint8_t green, uint8_t blue) {
  145. for (int i = 0; i < IS31FL3737_LED_COUNT; i++) {
  146. is31fl3737_set_color(i, red, green, blue);
  147. }
  148. }
  149. void is31fl3737_set_led_control_register(uint8_t index, bool red, bool green, bool blue) {
  150. is31fl3737_led_t led;
  151. memcpy_P(&led, (&g_is31fl3737_leds[index]), sizeof(led));
  152. uint8_t control_register_r = led.r / 8;
  153. uint8_t control_register_g = led.g / 8;
  154. uint8_t control_register_b = led.b / 8;
  155. uint8_t bit_r = led.r % 8;
  156. uint8_t bit_g = led.g % 8;
  157. uint8_t bit_b = led.b % 8;
  158. if (red) {
  159. g_led_control_registers[led.driver][control_register_r] |= (1 << bit_r);
  160. } else {
  161. g_led_control_registers[led.driver][control_register_r] &= ~(1 << bit_r);
  162. }
  163. if (green) {
  164. g_led_control_registers[led.driver][control_register_g] |= (1 << bit_g);
  165. } else {
  166. g_led_control_registers[led.driver][control_register_g] &= ~(1 << bit_g);
  167. }
  168. if (blue) {
  169. g_led_control_registers[led.driver][control_register_b] |= (1 << bit_b);
  170. } else {
  171. g_led_control_registers[led.driver][control_register_b] &= ~(1 << bit_b);
  172. }
  173. g_led_control_registers_update_required[led.driver] = true;
  174. }
  175. void is31fl3737_update_pwm_buffers(uint8_t index) {
  176. if (g_pwm_buffer_update_required[index]) {
  177. is31fl3737_select_page(index, IS31FL3737_COMMAND_PWM);
  178. is31fl3737_write_pwm_buffer(index);
  179. g_pwm_buffer_update_required[index] = false;
  180. }
  181. }
  182. void is31fl3737_update_led_control_registers(uint8_t index) {
  183. if (g_led_control_registers_update_required[index]) {
  184. is31fl3737_select_page(index, IS31FL3737_COMMAND_LED_CONTROL);
  185. for (uint8_t i = 0; i < IS31FL3737_LED_CONTROL_REGISTER_COUNT; i++) {
  186. is31fl3737_write_register(index, i, g_led_control_registers[index][i]);
  187. }
  188. g_led_control_registers_update_required[index] = false;
  189. }
  190. }
  191. void is31fl3737_flush(void) {
  192. for (uint8_t i = 0; i < IS31FL3737_DRIVER_COUNT; i++) {
  193. is31fl3737_update_pwm_buffers(i);
  194. }
  195. }