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.

241 lines
8.5 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 "is31fl3737-mono.h"
  20. #include <string.h>
  21. #include "i2c_master.h"
  22. #include "wait.h"
  23. #define IS31FL3737_PWM_REGISTER_COUNT 192 // actually 144
  24. #define IS31FL3737_LED_CONTROL_REGISTER_COUNT 24
  25. #ifndef IS31FL3737_I2C_TIMEOUT
  26. # define IS31FL3737_I2C_TIMEOUT 100
  27. #endif
  28. #ifndef IS31FL3737_I2C_PERSISTENCE
  29. # define IS31FL3737_I2C_PERSISTENCE 0
  30. #endif
  31. #ifndef IS31FL3737_PWM_FREQUENCY
  32. # define IS31FL3737_PWM_FREQUENCY IS31FL3737_PWM_FREQUENCY_8K4_HZ // PFS - IS31FL3737B only
  33. #endif
  34. #ifndef IS31FL3737_SW_PULLUP
  35. # define IS31FL3737_SW_PULLUP IS31FL3737_PUR_0_OHM
  36. #endif
  37. #ifndef IS31FL3737_CS_PULLDOWN
  38. # define IS31FL3737_CS_PULLDOWN IS31FL3737_PDR_0_OHM
  39. #endif
  40. #ifndef IS31FL3737_GLOBAL_CURRENT
  41. # define IS31FL3737_GLOBAL_CURRENT 0xFF
  42. #endif
  43. uint8_t i2c_transfer_buffer[20];
  44. // These buffers match the IS31FL3737 PWM registers.
  45. // The control buffers match the page 0 LED On/Off registers.
  46. // Storing them like this is optimal for I2C transfers to the registers.
  47. // We could optimize this and take out the unused registers from these
  48. // buffers and the transfers in is31fl3737_write_pwm_buffer() but it's
  49. // probably not worth the extra complexity.
  50. uint8_t g_pwm_buffer[IS31FL3737_DRIVER_COUNT][IS31FL3737_PWM_REGISTER_COUNT];
  51. bool g_pwm_buffer_update_required[IS31FL3737_DRIVER_COUNT] = {false};
  52. uint8_t g_led_control_registers[IS31FL3737_DRIVER_COUNT][IS31FL3737_LED_CONTROL_REGISTER_COUNT] = {0};
  53. bool g_led_control_registers_update_required[IS31FL3737_DRIVER_COUNT] = {false};
  54. void is31fl3737_write_register(uint8_t addr, uint8_t reg, uint8_t data) {
  55. i2c_transfer_buffer[0] = reg;
  56. i2c_transfer_buffer[1] = data;
  57. #if IS31FL3737_I2C_PERSISTENCE > 0
  58. for (uint8_t i = 0; i < IS31FL3737_I2C_PERSISTENCE; i++) {
  59. if (i2c_transmit(addr << 1, i2c_transfer_buffer, 2, IS31FL3737_I2C_TIMEOUT) == 0) break;
  60. }
  61. #else
  62. i2c_transmit(addr << 1, i2c_transfer_buffer, 2, IS31FL3737_I2C_TIMEOUT);
  63. #endif
  64. }
  65. void is31fl3737_select_page(uint8_t addr, uint8_t page) {
  66. is31fl3737_write_register(addr, IS31FL3737_REG_COMMAND_WRITE_LOCK, IS31FL3737_COMMAND_WRITE_LOCK_MAGIC);
  67. is31fl3737_write_register(addr, IS31FL3737_REG_COMMAND, page);
  68. }
  69. void is31fl3737_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer) {
  70. // assumes page 1 is already selected
  71. // transmit PWM registers in 12 transfers of 16 bytes
  72. // i2c_transfer_buffer[] is 20 bytes
  73. // iterate over the pwm_buffer contents at 16 byte intervals
  74. for (int i = 0; i < IS31FL3737_PWM_REGISTER_COUNT; i += 16) {
  75. i2c_transfer_buffer[0] = i;
  76. // copy the data from i to i+15
  77. // device will auto-increment register for data after the first byte
  78. // thus this sets registers 0x00-0x0F, 0x10-0x1F, etc. in one transfer
  79. memcpy(i2c_transfer_buffer + 1, pwm_buffer + i, 16);
  80. #if IS31FL3737_I2C_PERSISTENCE > 0
  81. for (uint8_t i = 0; i < IS31FL3737_I2C_PERSISTENCE; i++) {
  82. if (i2c_transmit(addr << 1, i2c_transfer_buffer, 17, IS31FL3737_I2C_TIMEOUT) == 0) break;
  83. }
  84. #else
  85. i2c_transmit(addr << 1, i2c_transfer_buffer, 17, IS31FL3737_I2C_TIMEOUT);
  86. #endif
  87. }
  88. }
  89. void is31fl3737_init_drivers(void) {
  90. i2c_init();
  91. is31fl3737_init(IS31FL3737_I2C_ADDRESS_1);
  92. #if defined(IS31FL3737_I2C_ADDRESS_2)
  93. is31fl3737_init(IS31FL3737_I2C_ADDRESS_2);
  94. # if defined(IS31FL3737_I2C_ADDRESS_3)
  95. is31fl3737_init(IS31FL3737_I2C_ADDRESS_3);
  96. # if defined(IS31FL3737_I2C_ADDRESS_4)
  97. is31fl3737_init(IS31FL3737_I2C_ADDRESS_4);
  98. # endif
  99. # endif
  100. #endif
  101. for (int i = 0; i < IS31FL3737_LED_COUNT; i++) {
  102. is31fl3737_set_led_control_register(i, true);
  103. }
  104. is31fl3737_update_led_control_registers(IS31FL3737_I2C_ADDRESS_1, 0);
  105. #if defined(IS31FL3737_I2C_ADDRESS_2)
  106. is31fl3737_update_led_control_registers(IS31FL3737_I2C_ADDRESS_2, 1);
  107. # if defined(IS31FL3737_I2C_ADDRESS_3)
  108. is31fl3737_update_led_control_registers(IS31FL3737_I2C_ADDRESS_3, 2);
  109. # if defined(IS31FL3737_I2C_ADDRESS_4)
  110. is31fl3737_update_led_control_registers(IS31FL3737_I2C_ADDRESS_4, 3);
  111. # endif
  112. # endif
  113. #endif
  114. }
  115. void is31fl3737_init(uint8_t addr) {
  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. is31fl3737_select_page(addr, IS31FL3737_COMMAND_LED_CONTROL);
  121. // Turn off all LEDs.
  122. for (int i = 0; i < IS31FL3737_LED_CONTROL_REGISTER_COUNT; i++) {
  123. is31fl3737_write_register(addr, i, 0x00);
  124. }
  125. is31fl3737_select_page(addr, IS31FL3737_COMMAND_PWM);
  126. // Set PWM on all LEDs to 0
  127. // No need to setup Breath registers to PWM as that is the default.
  128. for (int i = 0; i < IS31FL3737_PWM_REGISTER_COUNT; i++) {
  129. is31fl3737_write_register(addr, i, 0x00);
  130. }
  131. is31fl3737_select_page(addr, IS31FL3737_COMMAND_FUNCTION);
  132. // Set de-ghost pull-up resistors (SWx)
  133. is31fl3737_write_register(addr, IS31FL3737_FUNCTION_REG_SW_PULLUP, IS31FL3737_SW_PULLUP);
  134. // Set de-ghost pull-down resistors (CSx)
  135. is31fl3737_write_register(addr, IS31FL3737_FUNCTION_REG_CS_PULLDOWN, IS31FL3737_CS_PULLDOWN);
  136. // Set global current to maximum.
  137. is31fl3737_write_register(addr, IS31FL3737_FUNCTION_REG_GLOBAL_CURRENT, IS31FL3737_GLOBAL_CURRENT);
  138. // Disable software shutdown.
  139. is31fl3737_write_register(addr, IS31FL3737_FUNCTION_REG_CONFIGURATION, ((IS31FL3737_PWM_FREQUENCY & 0b111) << 3) | 0x01);
  140. // Wait 10ms to ensure the device has woken up.
  141. wait_ms(10);
  142. }
  143. void is31fl3737_set_value(int index, uint8_t value) {
  144. is31fl3737_led_t led;
  145. if (index >= 0 && index < IS31FL3737_LED_COUNT) {
  146. memcpy_P(&led, (&g_is31fl3737_leds[index]), sizeof(led));
  147. if (g_pwm_buffer[led.driver][led.v] == value) {
  148. return;
  149. }
  150. g_pwm_buffer[led.driver][led.v] = value;
  151. g_pwm_buffer_update_required[led.driver] = true;
  152. }
  153. }
  154. void is31fl3737_set_value_all(uint8_t value) {
  155. for (int i = 0; i < IS31FL3737_LED_COUNT; i++) {
  156. is31fl3737_set_value(i, value);
  157. }
  158. }
  159. void is31fl3737_set_led_control_register(uint8_t index, bool value) {
  160. is31fl3737_led_t led;
  161. memcpy_P(&led, (&g_is31fl3737_leds[index]), sizeof(led));
  162. uint8_t control_register = led.v / 8;
  163. uint8_t bit_value = led.v % 8;
  164. if (value) {
  165. g_led_control_registers[led.driver][control_register] |= (1 << bit_value);
  166. } else {
  167. g_led_control_registers[led.driver][control_register] &= ~(1 << bit_value);
  168. }
  169. g_led_control_registers_update_required[led.driver] = true;
  170. }
  171. void is31fl3737_update_pwm_buffers(uint8_t addr, uint8_t index) {
  172. if (g_pwm_buffer_update_required[index]) {
  173. is31fl3737_select_page(addr, IS31FL3737_COMMAND_PWM);
  174. is31fl3737_write_pwm_buffer(addr, g_pwm_buffer[index]);
  175. g_pwm_buffer_update_required[index] = false;
  176. }
  177. }
  178. void is31fl3737_update_led_control_registers(uint8_t addr, uint8_t index) {
  179. if (g_led_control_registers_update_required[index]) {
  180. is31fl3737_select_page(addr, IS31FL3737_COMMAND_LED_CONTROL);
  181. for (int i = 0; i < IS31FL3737_LED_CONTROL_REGISTER_COUNT; i++) {
  182. is31fl3737_write_register(addr, i, g_led_control_registers[index][i]);
  183. }
  184. g_led_control_registers_update_required[index] = false;
  185. }
  186. }
  187. void is31fl3737_flush(void) {
  188. is31fl3737_update_pwm_buffers(IS31FL3737_I2C_ADDRESS_1, 0);
  189. #if defined(IS31FL3737_I2C_ADDRESS_2)
  190. is31fl3737_update_pwm_buffers(IS31FL3737_I2C_ADDRESS_2, 1);
  191. # if defined(IS31FL3737_I2C_ADDRESS_3)
  192. is31fl3737_update_pwm_buffers(IS31FL3737_I2C_ADDRESS_3, 2);
  193. # if defined(IS31FL3737_I2C_ADDRESS_4)
  194. is31fl3737_update_pwm_buffers(IS31FL3737_I2C_ADDRESS_4, 3);
  195. # endif
  196. # endif
  197. #endif
  198. }