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.

259 lines
8.9 KiB

  1. /* Copyright 2017 Jason Williams
  2. * Copyright 2018 Jack Humbert
  3. * Copyright 2018 Yiancar
  4. * Copyright 2020 MelGeek
  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 "is31fl3741.h"
  20. #include "i2c_master.h"
  21. #include "wait.h"
  22. #define IS31FL3741_PWM_REGISTER_COUNT 351
  23. #define IS31FL3741_SCALING_REGISTER_COUNT 351
  24. #ifndef IS31FL3741_I2C_TIMEOUT
  25. # define IS31FL3741_I2C_TIMEOUT 100
  26. #endif
  27. #ifndef IS31FL3741_I2C_PERSISTENCE
  28. # define IS31FL3741_I2C_PERSISTENCE 0
  29. #endif
  30. #ifndef IS31FL3741_CONFIGURATION
  31. # define IS31FL3741_CONFIGURATION 0x01
  32. #endif
  33. #ifndef IS31FL3741_PWM_FREQUENCY
  34. # define IS31FL3741_PWM_FREQUENCY IS31FL3741_PWM_FREQUENCY_29K_HZ
  35. #endif
  36. #ifndef IS31FL3741_SW_PULLUP
  37. # define IS31FL3741_SW_PULLUP IS31FL3741_PUR_32K_OHM
  38. #endif
  39. #ifndef IS31FL3741_CS_PULLDOWN
  40. # define IS31FL3741_CS_PULLDOWN IS31FL3741_PDR_32K_OHM
  41. #endif
  42. #ifndef IS31FL3741_GLOBAL_CURRENT
  43. # define IS31FL3741_GLOBAL_CURRENT 0xFF
  44. #endif
  45. const uint8_t i2c_addresses[IS31FL3741_DRIVER_COUNT] = {
  46. IS31FL3741_I2C_ADDRESS_1,
  47. #ifdef IS31FL3741_I2C_ADDRESS_2
  48. IS31FL3741_I2C_ADDRESS_2,
  49. # ifdef IS31FL3741_I2C_ADDRESS_3
  50. IS31FL3741_I2C_ADDRESS_3,
  51. # ifdef IS31FL3741_I2C_ADDRESS_4
  52. IS31FL3741_I2C_ADDRESS_4,
  53. # endif
  54. # endif
  55. #endif
  56. };
  57. // These buffers match the IS31FL3741 and IS31FL3741A PWM registers.
  58. // The scaling buffers match the page 2 and 3 LED On/Off registers.
  59. // Storing them like this is optimal for I2C transfers to the registers.
  60. // We could optimize this and take out the unused registers from these
  61. // buffers and the transfers in is31fl3741_write_pwm_buffer() but it's
  62. // probably not worth the extra complexity.
  63. uint8_t g_pwm_buffer[IS31FL3741_DRIVER_COUNT][IS31FL3741_PWM_REGISTER_COUNT];
  64. bool g_pwm_buffer_update_required[IS31FL3741_DRIVER_COUNT] = {false};
  65. bool g_scaling_registers_update_required[IS31FL3741_DRIVER_COUNT] = {false};
  66. uint8_t g_scaling_registers[IS31FL3741_DRIVER_COUNT][IS31FL3741_SCALING_REGISTER_COUNT];
  67. void is31fl3741_write_register(uint8_t index, uint8_t reg, uint8_t data) {
  68. #if IS31FL3741_I2C_PERSISTENCE > 0
  69. for (uint8_t i = 0; i < IS31FL3741_I2C_PERSISTENCE; i++) {
  70. if (i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3741_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
  71. }
  72. #else
  73. i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3741_I2C_TIMEOUT);
  74. #endif
  75. }
  76. void is31fl3741_select_page(uint8_t index, uint8_t page) {
  77. is31fl3741_write_register(index, IS31FL3741_REG_COMMAND_WRITE_LOCK, IS31FL3741_COMMAND_WRITE_LOCK_MAGIC);
  78. is31fl3741_write_register(index, IS31FL3741_REG_COMMAND, page);
  79. }
  80. void is31fl3741_write_pwm_buffer(uint8_t index) {
  81. // Assume page 0 is already selected
  82. for (uint16_t i = 0; i < 342; i += 18) {
  83. if (i == 180) {
  84. is31fl3741_select_page(index, IS31FL3741_COMMAND_PWM_1);
  85. }
  86. #if IS31FL3741_I2C_PERSISTENCE > 0
  87. for (uint8_t j = 0; j < IS31FL3741_I2C_PERSISTENCE; j++) {
  88. if (i2c_write_register(i2c_addresses[index] << 1, i % 180, g_pwm_buffer[index] + i, 18, IS31FL3741_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
  89. }
  90. #else
  91. i2c_write_register(i2c_addresses[index] << 1, i % 180, g_pwm_buffer[index] + i, 18, IS31FL3741_I2C_TIMEOUT);
  92. #endif
  93. }
  94. // transfer the left cause the total number is 351
  95. #if IS31FL3741_I2C_PERSISTENCE > 0
  96. for (uint8_t i = 0; i < IS31FL3741_I2C_PERSISTENCE; i++) {
  97. if (i2c_write_register(i2c_addresses[index] << 1, 162, g_pwm_buffer[index] + 342, 9, IS31FL3741_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
  98. }
  99. #else
  100. i2c_write_register(i2c_addresses[index] << 1, 162, g_pwm_buffer[index] + 342, 9, IS31FL3741_I2C_TIMEOUT);
  101. #endif
  102. }
  103. void is31fl3741_init_drivers(void) {
  104. i2c_init();
  105. for (uint8_t i = 0; i < IS31FL3741_DRIVER_COUNT; i++) {
  106. is31fl3741_init(i);
  107. }
  108. for (int i = 0; i < IS31FL3741_LED_COUNT; i++) {
  109. is31fl3741_set_led_control_register(i, true, true, true);
  110. }
  111. for (uint8_t i = 0; i < IS31FL3741_DRIVER_COUNT; i++) {
  112. is31fl3741_update_led_control_registers(i);
  113. }
  114. }
  115. void is31fl3741_init(uint8_t index) {
  116. // In order to avoid the LEDs being driven with garbage data
  117. // in the LED driver's PWM registers, shutdown is enabled last.
  118. // Set up the mode and other settings, clear the PWM registers,
  119. // then disable software shutdown.
  120. // Unlock the command register.
  121. is31fl3741_select_page(index, IS31FL3741_COMMAND_FUNCTION);
  122. // Set to Normal operation
  123. is31fl3741_write_register(index, IS31FL3741_FUNCTION_REG_CONFIGURATION, IS31FL3741_CONFIGURATION);
  124. // Set Golbal Current Control Register
  125. is31fl3741_write_register(index, IS31FL3741_FUNCTION_REG_GLOBAL_CURRENT, IS31FL3741_GLOBAL_CURRENT);
  126. // Set Pull up & Down for SWx CSy
  127. is31fl3741_write_register(index, IS31FL3741_FUNCTION_REG_PULLDOWNUP, ((IS31FL3741_CS_PULLDOWN << 4) | IS31FL3741_SW_PULLUP));
  128. // Set PWM frequency
  129. is31fl3741_write_register(index, IS31FL3741_FUNCTION_REG_PWM_FREQUENCY, (IS31FL3741_PWM_FREQUENCY & 0b1111));
  130. // is31fl3741_update_led_scaling_registers(index, 0xFF, 0xFF, 0xFF);
  131. // Wait 10ms to ensure the device has woken up.
  132. wait_ms(10);
  133. }
  134. void is31fl3741_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) {
  135. is31fl3741_led_t led;
  136. if (index >= 0 && index < IS31FL3741_LED_COUNT) {
  137. memcpy_P(&led, (&g_is31fl3741_leds[index]), sizeof(led));
  138. 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) {
  139. return;
  140. }
  141. g_pwm_buffer_update_required[led.driver] = true;
  142. g_pwm_buffer[led.driver][led.r] = red;
  143. g_pwm_buffer[led.driver][led.g] = green;
  144. g_pwm_buffer[led.driver][led.b] = blue;
  145. }
  146. }
  147. void is31fl3741_set_color_all(uint8_t red, uint8_t green, uint8_t blue) {
  148. for (int i = 0; i < IS31FL3741_LED_COUNT; i++) {
  149. is31fl3741_set_color(i, red, green, blue);
  150. }
  151. }
  152. void is31fl3741_set_led_control_register(uint8_t index, bool red, bool green, bool blue) {
  153. is31fl3741_led_t led;
  154. memcpy_P(&led, (&g_is31fl3741_leds[index]), sizeof(led));
  155. if (red) {
  156. g_scaling_registers[led.driver][led.r] = 0xFF;
  157. } else {
  158. g_scaling_registers[led.driver][led.r] = 0x00;
  159. }
  160. if (green) {
  161. g_scaling_registers[led.driver][led.g] = 0xFF;
  162. } else {
  163. g_scaling_registers[led.driver][led.g] = 0x00;
  164. }
  165. if (blue) {
  166. g_scaling_registers[led.driver][led.b] = 0xFF;
  167. } else {
  168. g_scaling_registers[led.driver][led.b] = 0x00;
  169. }
  170. g_scaling_registers_update_required[led.driver] = true;
  171. }
  172. void is31fl3741_update_pwm_buffers(uint8_t index) {
  173. if (g_pwm_buffer_update_required[index]) {
  174. is31fl3741_select_page(index, IS31FL3741_COMMAND_PWM_0);
  175. is31fl3741_write_pwm_buffer(index);
  176. g_pwm_buffer_update_required[index] = false;
  177. }
  178. }
  179. void is31fl3741_set_pwm_buffer(const is31fl3741_led_t *pled, uint8_t red, uint8_t green, uint8_t blue) {
  180. g_pwm_buffer[pled->driver][pled->r] = red;
  181. g_pwm_buffer[pled->driver][pled->g] = green;
  182. g_pwm_buffer[pled->driver][pled->b] = blue;
  183. g_pwm_buffer_update_required[pled->driver] = true;
  184. }
  185. void is31fl3741_update_led_control_registers(uint8_t index) {
  186. if (g_scaling_registers_update_required[index]) {
  187. is31fl3741_select_page(index, IS31FL3741_COMMAND_SCALING_0);
  188. // CS1_SW1 to CS30_SW6 are on page 2
  189. for (int i = CS1_SW1; i <= CS30_SW6; ++i) {
  190. is31fl3741_write_register(index, i, g_scaling_registers[index][i]);
  191. }
  192. is31fl3741_select_page(index, IS31FL3741_COMMAND_SCALING_1);
  193. // CS1_SW7 to CS39_SW9 are on page 3
  194. for (int i = CS1_SW7; i <= CS39_SW9; ++i) {
  195. is31fl3741_write_register(index, i - CS1_SW7, g_scaling_registers[index][i]);
  196. }
  197. g_scaling_registers_update_required[index] = false;
  198. }
  199. }
  200. void is31fl3741_set_scaling_registers(const is31fl3741_led_t *pled, uint8_t red, uint8_t green, uint8_t blue) {
  201. g_scaling_registers[pled->driver][pled->r] = red;
  202. g_scaling_registers[pled->driver][pled->g] = green;
  203. g_scaling_registers[pled->driver][pled->b] = blue;
  204. g_scaling_registers_update_required[pled->driver] = true;
  205. }
  206. void is31fl3741_flush(void) {
  207. for (uint8_t i = 0; i < IS31FL3741_DRIVER_COUNT; i++) {
  208. is31fl3741_update_pwm_buffers(i);
  209. }
  210. }