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.

245 lines
7.6 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-mono.h"
  21. #include "i2c_master.h"
  22. #include "gpio.h"
  23. #include "wait.h"
  24. #define IS31FL3745_PWM_REGISTER_COUNT 144
  25. #define IS31FL3745_SCALING_REGISTER_COUNT 144
  26. #ifndef IS31FL3745_I2C_TIMEOUT
  27. # define IS31FL3745_I2C_TIMEOUT 100
  28. #endif
  29. #ifndef IS31FL3745_I2C_PERSISTENCE
  30. # define IS31FL3745_I2C_PERSISTENCE 0
  31. #endif
  32. #ifndef IS31FL3745_CONFIGURATION
  33. # define IS31FL3745_CONFIGURATION 0x31
  34. #endif
  35. #ifndef IS31FL3745_SW_PULLDOWN
  36. # define IS31FL3745_SW_PULLDOWN IS31FL3745_PDR_2K_OHM_SW_OFF
  37. #endif
  38. #ifndef IS31FL3745_CS_PULLUP
  39. # define IS31FL3745_CS_PULLUP IS31FL3745_PUR_2K_OHM_CS_OFF
  40. #endif
  41. #ifndef IS31FL3745_GLOBAL_CURRENT
  42. # define IS31FL3745_GLOBAL_CURRENT 0xFF
  43. #endif
  44. #ifndef IS31FL3745_SYNC_1
  45. # define IS31FL3745_SYNC_1 IS31FL3745_SYNC_NONE
  46. #endif
  47. #ifndef IS31FL3745_SYNC_2
  48. # define IS31FL3745_SYNC_2 IS31FL3745_SYNC_NONE
  49. #endif
  50. #ifndef IS31FL3745_SYNC_3
  51. # define IS31FL3745_SYNC_3 IS31FL3745_SYNC_NONE
  52. #endif
  53. #ifndef IS31FL3745_SYNC_4
  54. # define IS31FL3745_SYNC_4 IS31FL3745_SYNC_NONE
  55. #endif
  56. const uint8_t i2c_addresses[IS31FL3745_DRIVER_COUNT] = {
  57. IS31FL3745_I2C_ADDRESS_1,
  58. #ifdef IS31FL3745_I2C_ADDRESS_2
  59. IS31FL3745_I2C_ADDRESS_2,
  60. # ifdef IS31FL3745_I2C_ADDRESS_3
  61. IS31FL3745_I2C_ADDRESS_3,
  62. # ifdef IS31FL3745_I2C_ADDRESS_4
  63. IS31FL3745_I2C_ADDRESS_4,
  64. # endif
  65. # endif
  66. #endif
  67. };
  68. const uint8_t driver_sync[IS31FL3745_DRIVER_COUNT] = {
  69. IS31FL3745_SYNC_1,
  70. #ifdef IS31FL3745_I2C_ADDRESS_2
  71. IS31FL3745_SYNC_2,
  72. # ifdef IS31FL3745_I2C_ADDRESS_3
  73. IS31FL3745_SYNC_3,
  74. # ifdef IS31FL3745_I2C_ADDRESS_4
  75. IS31FL3745_SYNC_4,
  76. # endif
  77. # endif
  78. #endif
  79. };
  80. typedef struct is31fl3745_driver_t {
  81. uint8_t pwm_buffer[IS31FL3745_PWM_REGISTER_COUNT];
  82. bool pwm_buffer_dirty;
  83. uint8_t scaling_buffer[IS31FL3745_SCALING_REGISTER_COUNT];
  84. bool scaling_buffer_dirty;
  85. } PACKED is31fl3745_driver_t;
  86. is31fl3745_driver_t driver_buffers[IS31FL3745_DRIVER_COUNT] = {{
  87. .pwm_buffer = {0},
  88. .pwm_buffer_dirty = false,
  89. .scaling_buffer = {0},
  90. .scaling_buffer_dirty = false,
  91. }};
  92. void is31fl3745_write_register(uint8_t index, uint8_t reg, uint8_t data) {
  93. #if IS31FL3745_I2C_PERSISTENCE > 0
  94. for (uint8_t i = 0; i < IS31FL3745_I2C_PERSISTENCE; i++) {
  95. if (i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3745_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
  96. }
  97. #else
  98. i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3745_I2C_TIMEOUT);
  99. #endif
  100. }
  101. void is31fl3745_select_page(uint8_t index, uint8_t page) {
  102. is31fl3745_write_register(index, IS31FL3745_REG_COMMAND_WRITE_LOCK, IS31FL3745_COMMAND_WRITE_LOCK_MAGIC);
  103. is31fl3745_write_register(index, IS31FL3745_REG_COMMAND, page);
  104. }
  105. void is31fl3745_write_pwm_buffer(uint8_t index) {
  106. // Assumes page 0 is already selected.
  107. // Transmit PWM registers in 8 transfers of 18 bytes.
  108. // Iterate over the pwm_buffer contents at 18 byte intervals.
  109. for (uint8_t i = 0; i < IS31FL3745_PWM_REGISTER_COUNT; i += 18) {
  110. #if IS31FL3745_I2C_PERSISTENCE > 0
  111. for (uint8_t j = 0; j < IS31FL3745_I2C_PERSISTENCE; j++) {
  112. if (i2c_write_register(i2c_addresses[index] << 1, i + 1, driver_buffers[index].pwm_buffer + i, 18, IS31FL3745_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
  113. }
  114. #else
  115. i2c_write_register(i2c_addresses[index] << 1, i + 1, driver_buffers[index].pwm_buffer + i, 18, IS31FL3745_I2C_TIMEOUT);
  116. #endif
  117. }
  118. }
  119. void is31fl3745_init_drivers(void) {
  120. i2c_init();
  121. #if defined(IS31FL3745_SDB_PIN)
  122. gpio_set_pin_output(IS31FL3745_SDB_PIN);
  123. gpio_write_pin_high(IS31FL3745_SDB_PIN);
  124. #endif
  125. for (uint8_t i = 0; i < IS31FL3745_DRIVER_COUNT; i++) {
  126. is31fl3745_init(i);
  127. }
  128. for (int i = 0; i < IS31FL3745_LED_COUNT; i++) {
  129. is31fl3745_set_scaling_register(i, 0xFF);
  130. }
  131. for (uint8_t i = 0; i < IS31FL3745_DRIVER_COUNT; i++) {
  132. is31fl3745_update_scaling_registers(i);
  133. }
  134. }
  135. void is31fl3745_init(uint8_t index) {
  136. // In order to avoid the LEDs being driven with garbage data
  137. // in the LED driver's PWM registers, shutdown is enabled last.
  138. // Set up the mode and other settings, clear the PWM registers,
  139. // then disable software shutdown.
  140. is31fl3745_select_page(index, IS31FL3745_COMMAND_SCALING);
  141. // Turn off all LEDs.
  142. for (uint8_t i = 0; i < IS31FL3745_SCALING_REGISTER_COUNT; i++) {
  143. is31fl3745_write_register(index, i + 1, 0x00);
  144. }
  145. is31fl3745_select_page(index, IS31FL3745_COMMAND_PWM);
  146. for (uint8_t i = 0; i < IS31FL3745_PWM_REGISTER_COUNT; i++) {
  147. is31fl3745_write_register(index, i + 1, 0x00);
  148. }
  149. is31fl3745_select_page(index, IS31FL3745_COMMAND_FUNCTION);
  150. uint8_t sync = driver_sync[index];
  151. is31fl3745_write_register(index, IS31FL3745_FUNCTION_REG_PULLDOWNUP, (IS31FL3745_SW_PULLDOWN << 4) | IS31FL3745_CS_PULLUP);
  152. is31fl3745_write_register(index, IS31FL3745_FUNCTION_REG_GLOBAL_CURRENT, IS31FL3745_GLOBAL_CURRENT);
  153. is31fl3745_write_register(index, IS31FL3745_FUNCTION_REG_SPREAD_SPECTRUM, (sync & 0b11) << 6);
  154. is31fl3745_write_register(index, IS31FL3745_FUNCTION_REG_CONFIGURATION, IS31FL3745_CONFIGURATION);
  155. // Wait 10ms to ensure the device has woken up.
  156. wait_ms(10);
  157. }
  158. void is31fl3745_set_value(int index, uint8_t value) {
  159. is31fl3745_led_t led;
  160. if (index >= 0 && index < IS31FL3745_LED_COUNT) {
  161. memcpy_P(&led, (&g_is31fl3745_leds[index]), sizeof(led));
  162. if (driver_buffers[led.driver].pwm_buffer[led.v] == value) {
  163. return;
  164. }
  165. driver_buffers[led.driver].pwm_buffer[led.v] = value;
  166. driver_buffers[led.driver].pwm_buffer_dirty = true;
  167. }
  168. }
  169. void is31fl3745_set_value_all(uint8_t value) {
  170. for (int i = 0; i < IS31FL3745_LED_COUNT; i++) {
  171. is31fl3745_set_value(i, value);
  172. }
  173. }
  174. void is31fl3745_set_scaling_register(uint8_t index, uint8_t value) {
  175. is31fl3745_led_t led;
  176. memcpy_P(&led, (&g_is31fl3745_leds[index]), sizeof(led));
  177. driver_buffers[led.driver].scaling_buffer[led.v] = value;
  178. driver_buffers[led.driver].scaling_buffer_dirty = true;
  179. }
  180. void is31fl3745_update_pwm_buffers(uint8_t index) {
  181. if (driver_buffers[index].pwm_buffer_dirty) {
  182. is31fl3745_select_page(index, IS31FL3745_COMMAND_PWM);
  183. is31fl3745_write_pwm_buffer(index);
  184. driver_buffers[index].pwm_buffer_dirty = false;
  185. }
  186. }
  187. void is31fl3745_update_scaling_registers(uint8_t index) {
  188. if (driver_buffers[index].scaling_buffer_dirty) {
  189. is31fl3745_select_page(index, IS31FL3745_COMMAND_SCALING);
  190. for (uint8_t i = 0; i < IS31FL3745_SCALING_REGISTER_COUNT; i++) {
  191. is31fl3745_write_register(index, i + 1, driver_buffers[index].scaling_buffer[i]);
  192. }
  193. driver_buffers[index].scaling_buffer_dirty = false;
  194. }
  195. }
  196. void is31fl3745_flush(void) {
  197. for (uint8_t i = 0; i < IS31FL3745_DRIVER_COUNT; i++) {
  198. is31fl3745_update_pwm_buffers(i);
  199. }
  200. }