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.

261 lines
9.5 KiB

  1. /* Copyright 2018 Jason Williams (Wilba)
  2. * Copyright 2021 Doni Crosby
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "is31fl3736.h"
  18. #include <string.h>
  19. #include "i2c_master.h"
  20. #include "wait.h"
  21. #define IS31FL3736_PWM_REGISTER_COUNT 192 // actually 96
  22. #define IS31FL3736_LED_CONTROL_REGISTER_COUNT 24
  23. #ifndef IS31FL3736_I2C_TIMEOUT
  24. # define IS31FL3736_I2C_TIMEOUT 100
  25. #endif
  26. #ifndef IS31FL3736_I2C_PERSISTENCE
  27. # define IS31FL3736_I2C_PERSISTENCE 0
  28. #endif
  29. #ifndef IS31FL3736_PWM_FREQUENCY
  30. # define IS31FL3736_PWM_FREQUENCY IS31FL3736_PWM_FREQUENCY_8K4_HZ // PFS - IS31FL3736B only
  31. #endif
  32. #ifndef IS31FL3736_SW_PULLUP
  33. # define IS31FL3736_SW_PULLUP IS31FL3736_PUR_0_OHM
  34. #endif
  35. #ifndef IS31FL3736_CS_PULLDOWN
  36. # define IS31FL3736_CS_PULLDOWN IS31FL3736_PDR_0_OHM
  37. #endif
  38. #ifndef IS31FL3736_GLOBAL_CURRENT
  39. # define IS31FL3736_GLOBAL_CURRENT 0xFF
  40. #endif
  41. uint8_t i2c_transfer_buffer[20];
  42. // These buffers match the IS31FL3736 PWM registers.
  43. // The control buffers match the page 0 LED On/Off registers.
  44. // Storing them like this is optimal for I2C transfers to the registers.
  45. // We could optimize this and take out the unused registers from these
  46. // buffers and the transfers in is31fl3736_write_pwm_buffer() but it's
  47. // probably not worth the extra complexity.
  48. uint8_t g_pwm_buffer[IS31FL3736_DRIVER_COUNT][IS31FL3736_PWM_REGISTER_COUNT];
  49. bool g_pwm_buffer_update_required[IS31FL3736_DRIVER_COUNT] = {false};
  50. uint8_t g_led_control_registers[IS31FL3736_DRIVER_COUNT][IS31FL3736_LED_CONTROL_REGISTER_COUNT] = {0};
  51. bool g_led_control_registers_update_required[IS31FL3736_DRIVER_COUNT] = {false};
  52. void is31fl3736_write_register(uint8_t addr, uint8_t reg, uint8_t data) {
  53. i2c_transfer_buffer[0] = reg;
  54. i2c_transfer_buffer[1] = data;
  55. #if IS31FL3736_I2C_PERSISTENCE > 0
  56. for (uint8_t i = 0; i < IS31FL3736_I2C_PERSISTENCE; i++) {
  57. if (i2c_transmit(addr << 1, i2c_transfer_buffer, 2, IS31FL3736_I2C_TIMEOUT) == 0) break;
  58. }
  59. #else
  60. i2c_transmit(addr << 1, i2c_transfer_buffer, 2, IS31FL3736_I2C_TIMEOUT);
  61. #endif
  62. }
  63. void is31fl3736_select_page(uint8_t addr, uint8_t page) {
  64. is31fl3736_write_register(addr, IS31FL3736_REG_COMMAND_WRITE_LOCK, IS31FL3736_COMMAND_WRITE_LOCK_MAGIC);
  65. is31fl3736_write_register(addr, IS31FL3736_REG_COMMAND, page);
  66. }
  67. void is31fl3736_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer) {
  68. // assumes page 1 is already selected
  69. // transmit PWM registers in 12 transfers of 16 bytes
  70. // i2c_transfer_buffer[] is 20 bytes
  71. // iterate over the pwm_buffer contents at 16 byte intervals
  72. for (int i = 0; i < IS31FL3736_PWM_REGISTER_COUNT; i += 16) {
  73. i2c_transfer_buffer[0] = i;
  74. // copy the data from i to i+15
  75. // device will auto-increment register for data after the first byte
  76. // thus this sets registers 0x00-0x0F, 0x10-0x1F, etc. in one transfer
  77. memcpy(i2c_transfer_buffer + 1, pwm_buffer + i, 16);
  78. #if IS31FL3736_I2C_PERSISTENCE > 0
  79. for (uint8_t i = 0; i < IS31FL3736_I2C_PERSISTENCE; i++) {
  80. if (i2c_transmit(addr << 1, i2c_transfer_buffer, 17, IS31FL3736_I2C_TIMEOUT) == 0) break;
  81. }
  82. #else
  83. i2c_transmit(addr << 1, i2c_transfer_buffer, 17, IS31FL3736_I2C_TIMEOUT);
  84. #endif
  85. }
  86. }
  87. void is31fl3736_init_drivers(void) {
  88. i2c_init();
  89. is31fl3736_init(IS31FL3736_I2C_ADDRESS_1);
  90. #if defined(IS31FL3736_I2C_ADDRESS_2)
  91. is31fl3736_init(IS31FL3736_I2C_ADDRESS_2);
  92. # if defined(IS31FL3736_I2C_ADDRESS_3)
  93. is31fl3736_init(IS31FL3736_I2C_ADDRESS_3);
  94. # if defined(IS31FL3736_I2C_ADDRESS_4)
  95. is31fl3736_init(IS31FL3736_I2C_ADDRESS_4);
  96. # endif
  97. # endif
  98. #endif
  99. for (int i = 0; i < IS31FL3736_LED_COUNT; i++) {
  100. is31fl3736_set_led_control_register(i, true, true, true);
  101. }
  102. is31fl3736_update_led_control_registers(IS31FL3736_I2C_ADDRESS_1, 0);
  103. #if defined(IS31FL3736_I2C_ADDRESS_2)
  104. is31fl3736_update_led_control_registers(IS31FL3736_I2C_ADDRESS_2, 1);
  105. # if defined(IS31FL3736_I2C_ADDRESS_3)
  106. is31fl3736_update_led_control_registers(IS31FL3736_I2C_ADDRESS_3, 2);
  107. # if defined(IS31FL3736_I2C_ADDRESS_4)
  108. is31fl3736_update_led_control_registers(IS31FL3736_I2C_ADDRESS_4, 3);
  109. # endif
  110. # endif
  111. #endif
  112. }
  113. void is31fl3736_init(uint8_t addr) {
  114. // In order to avoid the LEDs being driven with garbage data
  115. // in the LED driver's PWM registers, shutdown is enabled last.
  116. // Set up the mode and other settings, clear the PWM registers,
  117. // then disable software shutdown.
  118. is31fl3736_select_page(addr, IS31FL3736_COMMAND_LED_CONTROL);
  119. // Turn off all LEDs.
  120. for (int i = 0; i < IS31FL3736_LED_CONTROL_REGISTER_COUNT; i++) {
  121. is31fl3736_write_register(addr, i, 0x00);
  122. }
  123. is31fl3736_select_page(addr, IS31FL3736_COMMAND_PWM);
  124. // Set PWM on all LEDs to 0
  125. // No need to setup Breath registers to PWM as that is the default.
  126. for (int i = 0; i < IS31FL3736_PWM_REGISTER_COUNT; i++) {
  127. is31fl3736_write_register(addr, i, 0x00);
  128. }
  129. is31fl3736_select_page(addr, IS31FL3736_COMMAND_FUNCTION);
  130. // Set de-ghost pull-up resistors (SWx)
  131. is31fl3736_write_register(addr, IS31FL3736_FUNCTION_REG_SW_PULLUP, IS31FL3736_SW_PULLUP);
  132. // Set de-ghost pull-down resistors (CSx)
  133. is31fl3736_write_register(addr, IS31FL3736_FUNCTION_REG_CS_PULLDOWN, IS31FL3736_CS_PULLDOWN);
  134. // Set global current to maximum.
  135. is31fl3736_write_register(addr, IS31FL3736_FUNCTION_REG_GLOBAL_CURRENT, IS31FL3736_GLOBAL_CURRENT);
  136. // Disable software shutdown.
  137. is31fl3736_write_register(addr, IS31FL3736_FUNCTION_REG_CONFIGURATION, ((IS31FL3736_PWM_FREQUENCY & 0b111) << 3) | 0x01);
  138. // Wait 10ms to ensure the device has woken up.
  139. wait_ms(10);
  140. }
  141. void is31fl3736_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) {
  142. is31fl3736_led_t led;
  143. if (index >= 0 && index < IS31FL3736_LED_COUNT) {
  144. memcpy_P(&led, (&g_is31fl3736_leds[index]), sizeof(led));
  145. 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) {
  146. return;
  147. }
  148. g_pwm_buffer[led.driver][led.r] = red;
  149. g_pwm_buffer[led.driver][led.g] = green;
  150. g_pwm_buffer[led.driver][led.b] = blue;
  151. g_pwm_buffer_update_required[led.driver] = true;
  152. }
  153. }
  154. void is31fl3736_set_color_all(uint8_t red, uint8_t green, uint8_t blue) {
  155. for (int i = 0; i < IS31FL3736_LED_COUNT; i++) {
  156. is31fl3736_set_color(i, red, green, blue);
  157. }
  158. }
  159. void is31fl3736_set_led_control_register(uint8_t index, bool red, bool green, bool blue) {
  160. is31fl3736_led_t led;
  161. memcpy_P(&led, (&g_is31fl3736_leds[index]), sizeof(led));
  162. // The PWM register for a matrix position (0x00 to 0xBF) is interleaved, so:
  163. // A1=0x00 A2=0x02 A3=0x04 A4=0x06 A5=0x08 A6=0x0A A7=0x0C A8=0x0E
  164. // B1=0x10 B2=0x12 B3=0x14
  165. // But also, the LED control registers (0x00 to 0x17) are also interleaved, so:
  166. // A1-A4=0x00 A5-A8=0x01
  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 is31fl3736_update_pwm_buffers(uint8_t addr, uint8_t index) {
  191. if (g_pwm_buffer_update_required[index]) {
  192. is31fl3736_select_page(addr, IS31FL3736_COMMAND_PWM);
  193. is31fl3736_write_pwm_buffer(addr, g_pwm_buffer[index]);
  194. g_pwm_buffer_update_required[index] = false;
  195. }
  196. }
  197. void is31fl3736_update_led_control_registers(uint8_t addr, uint8_t index) {
  198. if (g_led_control_registers_update_required[index]) {
  199. is31fl3736_select_page(addr, IS31FL3736_COMMAND_LED_CONTROL);
  200. for (int i = 0; i < IS31FL3736_LED_CONTROL_REGISTER_COUNT; i++) {
  201. is31fl3736_write_register(addr, i, g_led_control_registers[index][i]);
  202. }
  203. g_led_control_registers_update_required[index] = false;
  204. }
  205. }
  206. void is31fl3736_flush(void) {
  207. is31fl3736_update_pwm_buffers(IS31FL3736_I2C_ADDRESS_1, 0);
  208. #if defined(IS31FL3736_I2C_ADDRESS_2)
  209. is31fl3736_update_pwm_buffers(IS31FL3736_I2C_ADDRESS_2, 1);
  210. # if defined(IS31FL3736_I2C_ADDRESS_3)
  211. is31fl3736_update_pwm_buffers(IS31FL3736_I2C_ADDRESS_3, 2);
  212. # if defined(IS31FL3736_I2C_ADDRESS_4)
  213. is31fl3736_update_pwm_buffers(IS31FL3736_I2C_ADDRESS_4, 3);
  214. # endif
  215. # endif
  216. #endif
  217. }