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.

262 lines
9.6 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. // Transfer buffer for TWITransmitData()
  42. uint8_t g_twi_transfer_buffer[20];
  43. // These buffers match the IS31FL3736 PWM registers.
  44. // The control buffers match the page 0 LED On/Off registers.
  45. // Storing them like this is optimal for I2C transfers to the registers.
  46. // We could optimize this and take out the unused registers from these
  47. // buffers and the transfers in is31fl3736_write_pwm_buffer() but it's
  48. // probably not worth the extra complexity.
  49. uint8_t g_pwm_buffer[IS31FL3736_DRIVER_COUNT][IS31FL3736_PWM_REGISTER_COUNT];
  50. bool g_pwm_buffer_update_required[IS31FL3736_DRIVER_COUNT] = {false};
  51. uint8_t g_led_control_registers[IS31FL3736_DRIVER_COUNT][IS31FL3736_LED_CONTROL_REGISTER_COUNT] = {0};
  52. bool g_led_control_registers_update_required[IS31FL3736_DRIVER_COUNT] = {false};
  53. void is31fl3736_write_register(uint8_t addr, uint8_t reg, uint8_t data) {
  54. g_twi_transfer_buffer[0] = reg;
  55. g_twi_transfer_buffer[1] = data;
  56. #if IS31FL3736_I2C_PERSISTENCE > 0
  57. for (uint8_t i = 0; i < IS31FL3736_I2C_PERSISTENCE; i++) {
  58. if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 2, IS31FL3736_I2C_TIMEOUT) == 0) break;
  59. }
  60. #else
  61. i2c_transmit(addr << 1, g_twi_transfer_buffer, 2, IS31FL3736_I2C_TIMEOUT);
  62. #endif
  63. }
  64. void is31fl3736_select_page(uint8_t addr, uint8_t page) {
  65. is31fl3736_write_register(addr, IS31FL3736_REG_COMMAND_WRITE_LOCK, IS31FL3736_COMMAND_WRITE_LOCK_MAGIC);
  66. is31fl3736_write_register(addr, IS31FL3736_REG_COMMAND, page);
  67. }
  68. void is31fl3736_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer) {
  69. // assumes page 1 is already selected
  70. // transmit PWM registers in 12 transfers of 16 bytes
  71. // g_twi_transfer_buffer[] is 20 bytes
  72. // iterate over the pwm_buffer contents at 16 byte intervals
  73. for (int i = 0; i < IS31FL3736_PWM_REGISTER_COUNT; i += 16) {
  74. g_twi_transfer_buffer[0] = i;
  75. // copy the data from i to i+15
  76. // device will auto-increment register for data after the first byte
  77. // thus this sets registers 0x00-0x0F, 0x10-0x1F, etc. in one transfer
  78. memcpy(g_twi_transfer_buffer + 1, pwm_buffer + i, 16);
  79. #if IS31FL3736_I2C_PERSISTENCE > 0
  80. for (uint8_t i = 0; i < IS31FL3736_I2C_PERSISTENCE; i++) {
  81. if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 17, IS31FL3736_I2C_TIMEOUT) == 0) break;
  82. }
  83. #else
  84. i2c_transmit(addr << 1, g_twi_transfer_buffer, 17, IS31FL3736_I2C_TIMEOUT);
  85. #endif
  86. }
  87. }
  88. void is31fl3736_init_drivers(void) {
  89. i2c_init();
  90. is31fl3736_init(IS31FL3736_I2C_ADDRESS_1);
  91. #if defined(IS31FL3736_I2C_ADDRESS_2)
  92. is31fl3736_init(IS31FL3736_I2C_ADDRESS_2);
  93. # if defined(IS31FL3736_I2C_ADDRESS_3)
  94. is31fl3736_init(IS31FL3736_I2C_ADDRESS_3);
  95. # if defined(IS31FL3736_I2C_ADDRESS_4)
  96. is31fl3736_init(IS31FL3736_I2C_ADDRESS_4);
  97. # endif
  98. # endif
  99. #endif
  100. for (int i = 0; i < IS31FL3736_LED_COUNT; i++) {
  101. is31fl3736_set_led_control_register(i, true, true, true);
  102. }
  103. is31fl3736_update_led_control_registers(IS31FL3736_I2C_ADDRESS_1, 0);
  104. #if defined(IS31FL3736_I2C_ADDRESS_2)
  105. is31fl3736_update_led_control_registers(IS31FL3736_I2C_ADDRESS_2, 1);
  106. # if defined(IS31FL3736_I2C_ADDRESS_3)
  107. is31fl3736_update_led_control_registers(IS31FL3736_I2C_ADDRESS_3, 2);
  108. # if defined(IS31FL3736_I2C_ADDRESS_4)
  109. is31fl3736_update_led_control_registers(IS31FL3736_I2C_ADDRESS_4, 3);
  110. # endif
  111. # endif
  112. #endif
  113. }
  114. void is31fl3736_init(uint8_t addr) {
  115. // In order to avoid the LEDs being driven with garbage data
  116. // in the LED driver's PWM registers, shutdown is enabled last.
  117. // Set up the mode and other settings, clear the PWM registers,
  118. // then disable software shutdown.
  119. is31fl3736_select_page(addr, IS31FL3736_REG_LED_CONTROL);
  120. // Turn off all LEDs.
  121. for (int i = 0; i < IS31FL3736_LED_CONTROL_REGISTER_COUNT; i++) {
  122. is31fl3736_write_register(addr, i, 0x00);
  123. }
  124. is31fl3736_select_page(addr, IS31FL3736_COMMAND_PWM);
  125. // Set PWM on all LEDs to 0
  126. // No need to setup Breath registers to PWM as that is the default.
  127. for (int i = 0; i < IS31FL3736_PWM_REGISTER_COUNT; i++) {
  128. is31fl3736_write_register(addr, i, 0x00);
  129. }
  130. is31fl3736_select_page(addr, IS31FL3736_COMMAND_FUNCTION);
  131. // Set de-ghost pull-up resistors (SWx)
  132. is31fl3736_write_register(addr, IS31FL3736_FUNCTION_REG_SW_PULLUP, IS31FL3736_SW_PULLUP);
  133. // Set de-ghost pull-down resistors (CSx)
  134. is31fl3736_write_register(addr, IS31FL3736_FUNCTION_REG_CS_PULLDOWN, IS31FL3736_CS_PULLDOWN);
  135. // Set global current to maximum.
  136. is31fl3736_write_register(addr, IS31FL3736_FUNCTION_REG_GLOBAL_CURRENT, IS31FL3736_GLOBAL_CURRENT);
  137. // Disable software shutdown.
  138. is31fl3736_write_register(addr, IS31FL3736_FUNCTION_REG_CONFIGURATION, ((IS31FL3736_PWM_FREQUENCY & 0b111) << 3) | 0x01);
  139. // Wait 10ms to ensure the device has woken up.
  140. wait_ms(10);
  141. }
  142. void is31fl3736_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) {
  143. is31fl3736_led_t led;
  144. if (index >= 0 && index < IS31FL3736_LED_COUNT) {
  145. memcpy_P(&led, (&g_is31fl3736_leds[index]), sizeof(led));
  146. 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) {
  147. return;
  148. }
  149. g_pwm_buffer[led.driver][led.r] = red;
  150. g_pwm_buffer[led.driver][led.g] = green;
  151. g_pwm_buffer[led.driver][led.b] = blue;
  152. g_pwm_buffer_update_required[led.driver] = true;
  153. }
  154. }
  155. void is31fl3736_set_color_all(uint8_t red, uint8_t green, uint8_t blue) {
  156. for (int i = 0; i < IS31FL3736_LED_COUNT; i++) {
  157. is31fl3736_set_color(i, red, green, blue);
  158. }
  159. }
  160. void is31fl3736_set_led_control_register(uint8_t index, bool red, bool green, bool blue) {
  161. is31fl3736_led_t led;
  162. memcpy_P(&led, (&g_is31fl3736_leds[index]), sizeof(led));
  163. // The PWM register for a matrix position (0x00 to 0xBF) is interleaved, so:
  164. // A1=0x00 A2=0x02 A3=0x04 A4=0x06 A5=0x08 A6=0x0A A7=0x0C A8=0x0E
  165. // B1=0x10 B2=0x12 B3=0x14
  166. // But also, the LED control registers (0x00 to 0x17) are also interleaved, so:
  167. // A1-A4=0x00 A5-A8=0x01
  168. uint8_t control_register_r = led.r / 8;
  169. uint8_t control_register_g = led.g / 8;
  170. uint8_t control_register_b = led.b / 8;
  171. uint8_t bit_r = led.r % 8;
  172. uint8_t bit_g = led.g % 8;
  173. uint8_t bit_b = led.b % 8;
  174. if (red) {
  175. g_led_control_registers[led.driver][control_register_r] |= (1 << bit_r);
  176. } else {
  177. g_led_control_registers[led.driver][control_register_r] &= ~(1 << bit_r);
  178. }
  179. if (green) {
  180. g_led_control_registers[led.driver][control_register_g] |= (1 << bit_g);
  181. } else {
  182. g_led_control_registers[led.driver][control_register_g] &= ~(1 << bit_g);
  183. }
  184. if (blue) {
  185. g_led_control_registers[led.driver][control_register_b] |= (1 << bit_b);
  186. } else {
  187. g_led_control_registers[led.driver][control_register_b] &= ~(1 << bit_b);
  188. }
  189. g_led_control_registers_update_required[led.driver] = true;
  190. }
  191. void is31fl3736_update_pwm_buffers(uint8_t addr, uint8_t index) {
  192. if (g_pwm_buffer_update_required[index]) {
  193. is31fl3736_select_page(addr, IS31FL3736_COMMAND_PWM);
  194. is31fl3736_write_pwm_buffer(addr, g_pwm_buffer[index]);
  195. g_pwm_buffer_update_required[index] = false;
  196. }
  197. }
  198. void is31fl3736_update_led_control_registers(uint8_t addr, uint8_t index) {
  199. if (g_led_control_registers_update_required[index]) {
  200. is31fl3736_select_page(addr, IS31FL3736_COMMAND_LED_CONTROL);
  201. for (int i = 0; i < IS31FL3736_LED_CONTROL_REGISTER_COUNT; i++) {
  202. is31fl3736_write_register(addr, i, g_led_control_registers[index][i]);
  203. }
  204. g_led_control_registers_update_required[index] = false;
  205. }
  206. }
  207. void is31fl3736_flush(void) {
  208. is31fl3736_update_pwm_buffers(IS31FL3736_I2C_ADDRESS_1, 0);
  209. #if defined(IS31FL3736_I2C_ADDRESS_2)
  210. is31fl3736_update_pwm_buffers(IS31FL3736_I2C_ADDRESS_2, 1);
  211. # if defined(IS31FL3736_I2C_ADDRESS_3)
  212. is31fl3736_update_pwm_buffers(IS31FL3736_I2C_ADDRESS_3, 2);
  213. # if defined(IS31FL3736_I2C_ADDRESS_4)
  214. is31fl3736_update_pwm_buffers(IS31FL3736_I2C_ADDRESS_4, 3);
  215. # endif
  216. # endif
  217. #endif
  218. }