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.

266 lines
9.1 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 "is31fl3733.h"
  20. #include "i2c_master.h"
  21. #include "wait.h"
  22. #define IS31FL3733_PWM_REGISTER_COUNT 192
  23. #define IS31FL3733_LED_CONTROL_REGISTER_COUNT 24
  24. #ifndef IS31FL3733_I2C_TIMEOUT
  25. # define IS31FL3733_I2C_TIMEOUT 100
  26. #endif
  27. #ifndef IS31FL3733_I2C_PERSISTENCE
  28. # define IS31FL3733_I2C_PERSISTENCE 0
  29. #endif
  30. #ifndef IS31FL3733_PWM_FREQUENCY
  31. # define IS31FL3733_PWM_FREQUENCY IS31FL3733_PWM_FREQUENCY_8K4_HZ // PFS - IS31FL3733B only
  32. #endif
  33. #ifndef IS31FL3733_SW_PULLUP
  34. # define IS31FL3733_SW_PULLUP IS31FL3733_PUR_0_OHM
  35. #endif
  36. #ifndef IS31FL3733_CS_PULLDOWN
  37. # define IS31FL3733_CS_PULLDOWN IS31FL3733_PDR_0_OHM
  38. #endif
  39. #ifndef IS31FL3733_GLOBAL_CURRENT
  40. # define IS31FL3733_GLOBAL_CURRENT 0xFF
  41. #endif
  42. #ifndef IS31FL3733_SYNC_1
  43. # define IS31FL3733_SYNC_1 IS31FL3733_SYNC_NONE
  44. #endif
  45. #ifndef IS31FL3733_SYNC_2
  46. # define IS31FL3733_SYNC_2 IS31FL3733_SYNC_NONE
  47. #endif
  48. #ifndef IS31FL3733_SYNC_3
  49. # define IS31FL3733_SYNC_3 IS31FL3733_SYNC_NONE
  50. #endif
  51. #ifndef IS31FL3733_SYNC_4
  52. # define IS31FL3733_SYNC_4 IS31FL3733_SYNC_NONE
  53. #endif
  54. const uint8_t i2c_addresses[IS31FL3733_DRIVER_COUNT] = {
  55. IS31FL3733_I2C_ADDRESS_1,
  56. #ifdef IS31FL3733_I2C_ADDRESS_2
  57. IS31FL3733_I2C_ADDRESS_2,
  58. # ifdef IS31FL3733_I2C_ADDRESS_3
  59. IS31FL3733_I2C_ADDRESS_3,
  60. # ifdef IS31FL3733_I2C_ADDRESS_4
  61. IS31FL3733_I2C_ADDRESS_4,
  62. # endif
  63. # endif
  64. #endif
  65. };
  66. const uint8_t driver_sync[IS31FL3733_DRIVER_COUNT] = {
  67. IS31FL3733_SYNC_1,
  68. #ifdef IS31FL3733_I2C_ADDRESS_2
  69. IS31FL3733_SYNC_2,
  70. # ifdef IS31FL3733_I2C_ADDRESS_3
  71. IS31FL3733_SYNC_3,
  72. # ifdef IS31FL3733_I2C_ADDRESS_4
  73. IS31FL3733_SYNC_4,
  74. # endif
  75. # endif
  76. #endif
  77. };
  78. // These buffers match the IS31FL3733 PWM registers.
  79. // The control buffers match the page 0 LED On/Off registers.
  80. // Storing them like this is optimal for I2C transfers to the registers.
  81. // We could optimize this and take out the unused registers from these
  82. // buffers and the transfers in is31fl3733_write_pwm_buffer() but it's
  83. // probably not worth the extra complexity.
  84. uint8_t g_pwm_buffer[IS31FL3733_DRIVER_COUNT][IS31FL3733_PWM_REGISTER_COUNT];
  85. bool g_pwm_buffer_update_required[IS31FL3733_DRIVER_COUNT] = {false};
  86. uint8_t g_led_control_registers[IS31FL3733_DRIVER_COUNT][IS31FL3733_LED_CONTROL_REGISTER_COUNT] = {0};
  87. bool g_led_control_registers_update_required[IS31FL3733_DRIVER_COUNT] = {false};
  88. void is31fl3733_write_register(uint8_t index, uint8_t reg, uint8_t data) {
  89. #if IS31FL3733_I2C_PERSISTENCE > 0
  90. for (uint8_t i = 0; i < IS31FL3733_I2C_PERSISTENCE; i++) {
  91. if (i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3733_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
  92. }
  93. #else
  94. i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3733_I2C_TIMEOUT);
  95. #endif
  96. }
  97. void is31fl3733_select_page(uint8_t index, uint8_t page) {
  98. is31fl3733_write_register(index, IS31FL3733_REG_COMMAND_WRITE_LOCK, IS31FL3733_COMMAND_WRITE_LOCK_MAGIC);
  99. is31fl3733_write_register(index, IS31FL3733_REG_COMMAND, page);
  100. }
  101. void is31fl3733_write_pwm_buffer(uint8_t index) {
  102. // Assumes page 1 is already selected.
  103. // Transmit PWM registers in 12 transfers of 16 bytes.
  104. // Iterate over the pwm_buffer contents at 16 byte intervals.
  105. for (uint8_t i = 0; i < IS31FL3733_PWM_REGISTER_COUNT; i += 16) {
  106. #if IS31FL3733_I2C_PERSISTENCE > 0
  107. for (uint8_t j = 0; j < IS31FL3733_I2C_PERSISTENCE; j++) {
  108. if (i2c_write_register(i2c_addresses[index] << 1, i, g_pwm_buffer[index] + i, 16, IS31FL3733_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
  109. }
  110. #else
  111. i2c_write_register(i2c_addresses[index] << 1, i, g_pwm_buffer[index] + i, 16, IS31FL3733_I2C_TIMEOUT);
  112. #endif
  113. }
  114. }
  115. void is31fl3733_init_drivers(void) {
  116. i2c_init();
  117. for (uint8_t i = 0; i < IS31FL3733_DRIVER_COUNT; i++) {
  118. is31fl3733_init(i);
  119. }
  120. for (int i = 0; i < IS31FL3733_LED_COUNT; i++) {
  121. is31fl3733_set_led_control_register(i, true, true, true);
  122. }
  123. for (uint8_t i = 0; i < IS31FL3733_DRIVER_COUNT; i++) {
  124. is31fl3733_update_led_control_registers(i);
  125. }
  126. }
  127. void is31fl3733_init(uint8_t index) {
  128. // In order to avoid the LEDs being driven with garbage data
  129. // in the LED driver's PWM registers, shutdown is enabled last.
  130. // Set up the mode and other settings, clear the PWM registers,
  131. // then disable software shutdown.
  132. is31fl3733_select_page(index, IS31FL3733_COMMAND_LED_CONTROL);
  133. // Turn off all LEDs.
  134. for (uint8_t i = 0; i < IS31FL3733_LED_CONTROL_REGISTER_COUNT; i++) {
  135. is31fl3733_write_register(index, i, 0x00);
  136. }
  137. is31fl3733_select_page(index, IS31FL3733_COMMAND_PWM);
  138. // Set PWM on all LEDs to 0
  139. // No need to setup Breath registers to PWM as that is the default.
  140. for (uint8_t i = 0; i < IS31FL3733_PWM_REGISTER_COUNT; i++) {
  141. is31fl3733_write_register(index, i, 0x00);
  142. }
  143. is31fl3733_select_page(index, IS31FL3733_COMMAND_FUNCTION);
  144. uint8_t sync = driver_sync[index];
  145. // Set de-ghost pull-up resistors (SWx)
  146. is31fl3733_write_register(index, IS31FL3733_FUNCTION_REG_SW_PULLUP, IS31FL3733_SW_PULLUP);
  147. // Set de-ghost pull-down resistors (CSx)
  148. is31fl3733_write_register(index, IS31FL3733_FUNCTION_REG_CS_PULLDOWN, IS31FL3733_CS_PULLDOWN);
  149. // Set global current to maximum.
  150. is31fl3733_write_register(index, IS31FL3733_FUNCTION_REG_GLOBAL_CURRENT, IS31FL3733_GLOBAL_CURRENT);
  151. // Disable software shutdown.
  152. is31fl3733_write_register(index, IS31FL3733_FUNCTION_REG_CONFIGURATION, ((sync & 0b11) << 6) | ((IS31FL3733_PWM_FREQUENCY & 0b111) << 3) | 0x01);
  153. // Wait 10ms to ensure the device has woken up.
  154. wait_ms(10);
  155. }
  156. void is31fl3733_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) {
  157. is31fl3733_led_t led;
  158. if (index >= 0 && index < IS31FL3733_LED_COUNT) {
  159. memcpy_P(&led, (&g_is31fl3733_leds[index]), sizeof(led));
  160. 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) {
  161. return;
  162. }
  163. g_pwm_buffer[led.driver][led.r] = red;
  164. g_pwm_buffer[led.driver][led.g] = green;
  165. g_pwm_buffer[led.driver][led.b] = blue;
  166. g_pwm_buffer_update_required[led.driver] = true;
  167. }
  168. }
  169. void is31fl3733_set_color_all(uint8_t red, uint8_t green, uint8_t blue) {
  170. for (int i = 0; i < IS31FL3733_LED_COUNT; i++) {
  171. is31fl3733_set_color(i, red, green, blue);
  172. }
  173. }
  174. void is31fl3733_set_led_control_register(uint8_t index, bool red, bool green, bool blue) {
  175. is31fl3733_led_t led;
  176. memcpy_P(&led, (&g_is31fl3733_leds[index]), sizeof(led));
  177. uint8_t control_register_r = led.r / 8;
  178. uint8_t control_register_g = led.g / 8;
  179. uint8_t control_register_b = led.b / 8;
  180. uint8_t bit_r = led.r % 8;
  181. uint8_t bit_g = led.g % 8;
  182. uint8_t bit_b = led.b % 8;
  183. if (red) {
  184. g_led_control_registers[led.driver][control_register_r] |= (1 << bit_r);
  185. } else {
  186. g_led_control_registers[led.driver][control_register_r] &= ~(1 << bit_r);
  187. }
  188. if (green) {
  189. g_led_control_registers[led.driver][control_register_g] |= (1 << bit_g);
  190. } else {
  191. g_led_control_registers[led.driver][control_register_g] &= ~(1 << bit_g);
  192. }
  193. if (blue) {
  194. g_led_control_registers[led.driver][control_register_b] |= (1 << bit_b);
  195. } else {
  196. g_led_control_registers[led.driver][control_register_b] &= ~(1 << bit_b);
  197. }
  198. g_led_control_registers_update_required[led.driver] = true;
  199. }
  200. void is31fl3733_update_pwm_buffers(uint8_t index) {
  201. if (g_pwm_buffer_update_required[index]) {
  202. is31fl3733_select_page(index, IS31FL3733_COMMAND_PWM);
  203. is31fl3733_write_pwm_buffer(index);
  204. g_pwm_buffer_update_required[index] = false;
  205. }
  206. }
  207. void is31fl3733_update_led_control_registers(uint8_t index) {
  208. if (g_led_control_registers_update_required[index]) {
  209. is31fl3733_select_page(index, IS31FL3733_COMMAND_LED_CONTROL);
  210. for (uint8_t i = 0; i < IS31FL3733_LED_CONTROL_REGISTER_COUNT; i++) {
  211. is31fl3733_write_register(index, i, g_led_control_registers[index][i]);
  212. }
  213. g_led_control_registers_update_required[index] = false;
  214. }
  215. }
  216. void is31fl3733_flush(void) {
  217. for (uint8_t i = 0; i < IS31FL3733_DRIVER_COUNT; i++) {
  218. is31fl3733_update_pwm_buffers(i);
  219. }
  220. }