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.

257 lines
9.0 KiB

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