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.

215 lines
7.3 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 "is31fl3742a-mono.h"
  21. #include "i2c_master.h"
  22. #include "wait.h"
  23. #define IS31FL3742A_PWM_REGISTER_COUNT 180
  24. #define IS31FL3742A_SCALING_REGISTER_COUNT 180
  25. #ifndef IS31FL3742A_I2C_TIMEOUT
  26. # define IS31FL3742A_I2C_TIMEOUT 100
  27. #endif
  28. #ifndef IS31FL3742A_I2C_PERSISTENCE
  29. # define IS31FL3742A_I2C_PERSISTENCE 0
  30. #endif
  31. #ifndef IS31FL3742A_CONFIGURATION
  32. # define IS31FL3742A_CONFIGURATION 0x31
  33. #endif
  34. #ifndef IS31FL3742A_PWM_FREQUENCY
  35. # define IS31FL3742A_PWM_FREQUENCY IS31FL3742A_PWM_FREQUENCY_29K_HZ
  36. #endif
  37. #ifndef IS31FL3742A_SW_PULLDOWN
  38. # define IS31FL3742A_SW_PULLDOWN IS31FL3742A_PDR_8K_OHM
  39. #endif
  40. #ifndef IS31FL3742A_CS_PULLUP
  41. # define IS31FL3742A_CS_PULLUP IS31FL3742A_PUR_8K_OHM
  42. #endif
  43. #ifndef IS31FL3742A_GLOBAL_CURRENT
  44. # define IS31FL3742A_GLOBAL_CURRENT 0xFF
  45. #endif
  46. uint8_t g_pwm_buffer[IS31FL3742A_DRIVER_COUNT][IS31FL3742A_PWM_REGISTER_COUNT];
  47. bool g_pwm_buffer_update_required[IS31FL3742A_DRIVER_COUNT] = {false};
  48. bool g_scaling_registers_update_required[IS31FL3742A_DRIVER_COUNT] = {false};
  49. uint8_t g_scaling_registers[IS31FL3742A_DRIVER_COUNT][IS31FL3742A_SCALING_REGISTER_COUNT];
  50. void is31fl3742a_write_register(uint8_t addr, uint8_t reg, uint8_t data) {
  51. #if IS31FL3742A_I2C_PERSISTENCE > 0
  52. for (uint8_t i = 0; i < IS31FL3742A_I2C_PERSISTENCE; i++) {
  53. if (i2c_writeReg(addr << 1, reg, &data, 1, IS31FL3742A_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
  54. }
  55. #else
  56. i2c_writeReg(addr << 1, reg, &data, 1, IS31FL3742A_I2C_TIMEOUT);
  57. #endif
  58. }
  59. void is31fl3742a_select_page(uint8_t addr, uint8_t page) {
  60. is31fl3742a_write_register(addr, IS31FL3742A_REG_COMMAND_WRITE_LOCK, IS31FL3742A_COMMAND_WRITE_LOCK_MAGIC);
  61. is31fl3742a_write_register(addr, IS31FL3742A_REG_COMMAND, page);
  62. }
  63. void is31fl3742a_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer) {
  64. // Assumes page 0 is already selected.
  65. // Transmit PWM registers in 6 transfers of 30 bytes.
  66. // Iterate over the pwm_buffer contents at 30 byte intervals.
  67. for (uint8_t i = 0; i < IS31FL3742A_PWM_REGISTER_COUNT; i += 30) {
  68. #if IS31FL3742A_I2C_PERSISTENCE > 0
  69. for (uint8_t j = 0; j < IS31FL3742A_I2C_PERSISTENCE; j++) {
  70. if (i2c_writeReg(addr << 1, i + 1, pwm_buffer + i, 30, IS31FL3742A_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
  71. }
  72. #else
  73. i2c_writeReg(addr << 1, i + 1, pwm_buffer + i, 30, IS31FL3742A_I2C_TIMEOUT);
  74. #endif
  75. }
  76. }
  77. void is31fl3742a_init_drivers(void) {
  78. i2c_init();
  79. is31fl3742a_init(IS31FL3742A_I2C_ADDRESS_1);
  80. #if defined(IS31FL3742A_I2C_ADDRESS_2)
  81. is31fl3742a_init(IS31FL3742A_I2C_ADDRESS_2);
  82. # if defined(IS31FL3742A_I2C_ADDRESS_3)
  83. is31fl3742a_init(IS31FL3742A_I2C_ADDRESS_3);
  84. # if defined(IS31FL3742A_I2C_ADDRESS_4)
  85. is31fl3742a_init(IS31FL3742A_I2C_ADDRESS_4);
  86. # endif
  87. # endif
  88. #endif
  89. for (int i = 0; i < IS31FL3742A_LED_COUNT; i++) {
  90. is31fl3742a_set_scaling_register(i, 0xFF);
  91. }
  92. is31fl3742a_update_scaling_registers(IS31FL3742A_I2C_ADDRESS_1, 0);
  93. #if defined(IS31FL3742A_I2C_ADDRESS_2)
  94. is31fl3742a_update_scaling_registers(IS31FL3742A_I2C_ADDRESS_2, 1);
  95. # if defined(IS31FL3742A_I2C_ADDRESS_3)
  96. is31fl3742a_update_scaling_registers(IS31FL3742A_I2C_ADDRESS_3, 2);
  97. # if defined(IS31FL3742A_I2C_ADDRESS_4)
  98. is31fl3742a_update_scaling_registers(IS31FL3742A_I2C_ADDRESS_4, 3);
  99. # endif
  100. # endif
  101. #endif
  102. }
  103. void is31fl3742a_init(uint8_t addr) {
  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. is31fl3742a_select_page(addr, IS31FL3742A_COMMAND_SCALING);
  109. // Turn off all LEDs.
  110. for (int i = 0; i < IS31FL3742A_SCALING_REGISTER_COUNT; i++) {
  111. is31fl3742a_write_register(addr, i, 0x00);
  112. }
  113. is31fl3742a_select_page(addr, IS31FL3742A_COMMAND_PWM);
  114. for (int i = 0; i < IS31FL3742A_PWM_REGISTER_COUNT; i++) {
  115. is31fl3742a_write_register(addr, i, 0x00);
  116. }
  117. is31fl3742a_select_page(addr, IS31FL3742A_COMMAND_FUNCTION);
  118. is31fl3742a_write_register(addr, IS31FL3742A_FUNCTION_REG_PULLDOWNUP, (IS31FL3742A_SW_PULLDOWN << 4) | IS31FL3742A_CS_PULLUP);
  119. is31fl3742a_write_register(addr, IS31FL3742A_FUNCTION_REG_GLOBAL_CURRENT, IS31FL3742A_GLOBAL_CURRENT);
  120. is31fl3742a_write_register(addr, IS31FL3742A_FUNCTION_REG_PWM_FREQUENCY, (IS31FL3742A_PWM_FREQUENCY & 0b0111));
  121. is31fl3742a_write_register(addr, IS31FL3742A_FUNCTION_REG_CONFIGURATION, IS31FL3742A_CONFIGURATION);
  122. // Wait 10ms to ensure the device has woken up.
  123. wait_ms(10);
  124. }
  125. void is31fl3742a_set_value(int index, uint8_t value) {
  126. is31fl3742a_led_t led;
  127. if (index >= 0 && index < IS31FL3742A_LED_COUNT) {
  128. memcpy_P(&led, (&g_is31fl3742a_leds[index]), sizeof(led));
  129. if (g_pwm_buffer[led.driver][led.v] == value) {
  130. return;
  131. }
  132. g_pwm_buffer[led.driver][led.v] = value;
  133. g_pwm_buffer_update_required[led.driver] = true;
  134. }
  135. }
  136. void is31fl3742a_set_value_all(uint8_t value) {
  137. for (int i = 0; i < IS31FL3742A_LED_COUNT; i++) {
  138. is31fl3742a_set_value(i, value);
  139. }
  140. }
  141. void is31fl3742a_set_scaling_register(uint8_t index, uint8_t value) {
  142. is31fl3742a_led_t led;
  143. memcpy_P(&led, (&g_is31fl3742a_leds[index]), sizeof(led));
  144. g_scaling_registers[led.driver][led.v] = value;
  145. g_scaling_registers_update_required[led.driver] = true;
  146. }
  147. void is31fl3742a_update_pwm_buffers(uint8_t addr, uint8_t index) {
  148. if (g_pwm_buffer_update_required[index]) {
  149. is31fl3742a_select_page(addr, IS31FL3742A_COMMAND_PWM);
  150. is31fl3742a_write_pwm_buffer(addr, g_pwm_buffer[index]);
  151. g_pwm_buffer_update_required[index] = false;
  152. }
  153. }
  154. void is31fl3742a_update_scaling_registers(uint8_t addr, uint8_t index) {
  155. if (g_scaling_registers_update_required[index]) {
  156. is31fl3742a_select_page(addr, IS31FL3742A_COMMAND_SCALING);
  157. for (int i = 0; i < IS31FL3742A_SCALING_REGISTER_COUNT; i++) {
  158. is31fl3742a_write_register(addr, i, g_scaling_registers[index][i]);
  159. }
  160. g_scaling_registers_update_required[index] = false;
  161. }
  162. }
  163. void is31fl3742a_flush(void) {
  164. is31fl3742a_update_pwm_buffers(IS31FL3742A_I2C_ADDRESS_1, 0);
  165. #if defined(IS31FL3742A_I2C_ADDRESS_2)
  166. is31fl3742a_update_pwm_buffers(IS31FL3742A_I2C_ADDRESS_2, 1);
  167. # if defined(IS31FL3742A_I2C_ADDRESS_3)
  168. is31fl3742a_update_pwm_buffers(IS31FL3742A_I2C_ADDRESS_3, 2);
  169. # if defined(IS31FL3742A_I2C_ADDRESS_4)
  170. is31fl3742a_update_pwm_buffers(IS31FL3742A_I2C_ADDRESS_4, 3);
  171. # endif
  172. # endif
  173. #endif
  174. }