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.

258 lines
9.0 KiB

  1. /* Copyright 2017 Jason Williams
  2. * Copyright 2018 Jack Humbert
  3. * Copyright 2018 Yiancar
  4. * Copyright 2021 Doni Crosby
  5. * Copyright 2021 Leo Deng
  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 "is31fl3733-mono.h"
  21. #include <string.h>
  22. #include "i2c_master.h"
  23. #include "wait.h"
  24. #define IS31FL3733_PWM_REGISTER_COUNT 192
  25. #define IS31FL3733_LED_CONTROL_REGISTER_COUNT 24
  26. #ifndef IS31FL3733_I2C_TIMEOUT
  27. # define IS31FL3733_I2C_TIMEOUT 100
  28. #endif
  29. #ifndef IS31FL3733_I2C_PERSISTENCE
  30. # define IS31FL3733_I2C_PERSISTENCE 0
  31. #endif
  32. #ifndef IS31FL3733_PWM_FREQUENCY
  33. # define IS31FL3733_PWM_FREQUENCY IS31FL3733_PWM_FREQUENCY_8K4_HZ // PFS - IS31FL3733B only
  34. #endif
  35. #ifndef IS31FL3733_SW_PULLUP
  36. # define IS31FL3733_SW_PULLUP IS31FL3733_PUR_0_OHM
  37. #endif
  38. #ifndef IS31FL3733_CS_PULLDOWN
  39. # define IS31FL3733_CS_PULLDOWN IS31FL3733_PDR_0_OHM
  40. #endif
  41. #ifndef IS31FL3733_GLOBAL_CURRENT
  42. # define IS31FL3733_GLOBAL_CURRENT 0xFF
  43. #endif
  44. #ifndef IS31FL3733_SYNC_1
  45. # define IS31FL3733_SYNC_1 IS31FL3733_SYNC_NONE
  46. #endif
  47. #ifndef IS31FL3733_SYNC_2
  48. # define IS31FL3733_SYNC_2 IS31FL3733_SYNC_NONE
  49. #endif
  50. #ifndef IS31FL3733_SYNC_3
  51. # define IS31FL3733_SYNC_3 IS31FL3733_SYNC_NONE
  52. #endif
  53. #ifndef IS31FL3733_SYNC_4
  54. # define IS31FL3733_SYNC_4 IS31FL3733_SYNC_NONE
  55. #endif
  56. uint8_t i2c_transfer_buffer[20];
  57. // These buffers match the IS31FL3733 PWM registers.
  58. // The control buffers match the page 0 LED On/Off registers.
  59. // Storing them like this is optimal for I2C transfers to the registers.
  60. // We could optimize this and take out the unused registers from these
  61. // buffers and the transfers in is31fl3733_write_pwm_buffer() but it's
  62. // probably not worth the extra complexity.
  63. uint8_t g_pwm_buffer[IS31FL3733_DRIVER_COUNT][IS31FL3733_PWM_REGISTER_COUNT];
  64. bool g_pwm_buffer_update_required[IS31FL3733_DRIVER_COUNT] = {false};
  65. uint8_t g_led_control_registers[IS31FL3733_DRIVER_COUNT][IS31FL3733_LED_CONTROL_REGISTER_COUNT] = {0};
  66. bool g_led_control_registers_update_required[IS31FL3733_DRIVER_COUNT] = {false};
  67. void is31fl3733_write_register(uint8_t addr, uint8_t reg, uint8_t data) {
  68. i2c_transfer_buffer[0] = reg;
  69. i2c_transfer_buffer[1] = data;
  70. #if IS31FL3733_I2C_PERSISTENCE > 0
  71. for (uint8_t i = 0; i < IS31FL3733_I2C_PERSISTENCE; i++) {
  72. if (i2c_transmit(addr << 1, i2c_transfer_buffer, 2, IS31FL3733_I2C_TIMEOUT) == 0) break;
  73. }
  74. #else
  75. i2c_transmit(addr << 1, i2c_transfer_buffer, 2, IS31FL3733_I2C_TIMEOUT);
  76. #endif
  77. }
  78. void is31fl3733_select_page(uint8_t addr, uint8_t page) {
  79. is31fl3733_write_register(addr, IS31FL3733_REG_COMMAND_WRITE_LOCK, IS31FL3733_COMMAND_WRITE_LOCK_MAGIC);
  80. is31fl3733_write_register(addr, IS31FL3733_REG_COMMAND, page);
  81. }
  82. void is31fl3733_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer) {
  83. // Assumes page 1 is already selected.
  84. // Transmit PWM registers in 12 transfers of 16 bytes.
  85. // i2c_transfer_buffer[] is 20 bytes
  86. // Iterate over the pwm_buffer contents at 16 byte intervals.
  87. for (int i = 0; i < IS31FL3733_PWM_REGISTER_COUNT; i += 16) {
  88. i2c_transfer_buffer[0] = i;
  89. // Copy the data from i to i+15.
  90. // Device will auto-increment register for data after the first byte
  91. // Thus this sets registers 0x00-0x0F, 0x10-0x1F, etc. in one transfer.
  92. memcpy(i2c_transfer_buffer + 1, pwm_buffer + i, 16);
  93. #if IS31FL3733_I2C_PERSISTENCE > 0
  94. for (uint8_t i = 0; i < IS31FL3733_I2C_PERSISTENCE; i++) {
  95. if (i2c_transmit(addr << 1, i2c_transfer_buffer, 17, IS31FL3733_I2C_TIMEOUT) == 0) break;
  96. }
  97. #else
  98. i2c_transmit(addr << 1, i2c_transfer_buffer, 17, IS31FL3733_I2C_TIMEOUT);
  99. #endif
  100. }
  101. }
  102. void is31fl3733_init_drivers(void) {
  103. i2c_init();
  104. is31fl3733_init(IS31FL3733_I2C_ADDRESS_1, IS31FL3733_SYNC_1);
  105. #if defined(IS31FL3733_I2C_ADDRESS_2)
  106. is31fl3733_init(IS31FL3733_I2C_ADDRESS_2, IS31FL3733_SYNC_2);
  107. # if defined(IS31FL3733_I2C_ADDRESS_3)
  108. is31fl3733_init(IS31FL3733_I2C_ADDRESS_3, IS31FL3733_SYNC_3);
  109. # if defined(IS31FL3733_I2C_ADDRESS_4)
  110. is31fl3733_init(IS31FL3733_I2C_ADDRESS_4, IS31FL3733_SYNC_4);
  111. # endif
  112. # endif
  113. #endif
  114. for (int i = 0; i < IS31FL3733_LED_COUNT; i++) {
  115. is31fl3733_set_led_control_register(i, true);
  116. }
  117. is31fl3733_update_led_control_registers(IS31FL3733_I2C_ADDRESS_1, 0);
  118. #if defined(IS31FL3733_I2C_ADDRESS_2)
  119. is31fl3733_update_led_control_registers(IS31FL3733_I2C_ADDRESS_2, 1);
  120. # if defined(IS31FL3733_I2C_ADDRESS_3)
  121. is31fl3733_update_led_control_registers(IS31FL3733_I2C_ADDRESS_3, 2);
  122. # if defined(IS31FL3733_I2C_ADDRESS_4)
  123. is31fl3733_update_led_control_registers(IS31FL3733_I2C_ADDRESS_4, 3);
  124. # endif
  125. # endif
  126. #endif
  127. }
  128. void is31fl3733_init(uint8_t addr, uint8_t sync) {
  129. // In order to avoid the LEDs being driven with garbage data
  130. // in the LED driver's PWM registers, shutdown is enabled last.
  131. // Set up the mode and other settings, clear the PWM registers,
  132. // then disable software shutdown.
  133. // Sync is passed so set it according to the datasheet.
  134. is31fl3733_select_page(addr, IS31FL3733_COMMAND_LED_CONTROL);
  135. // Turn off all LEDs.
  136. for (int i = 0; i < IS31FL3733_LED_CONTROL_REGISTER_COUNT; i++) {
  137. is31fl3733_write_register(addr, i, 0x00);
  138. }
  139. is31fl3733_select_page(addr, IS31FL3733_COMMAND_PWM);
  140. // Set PWM on all LEDs to 0
  141. // No need to setup Breath registers to PWM as that is the default.
  142. for (int i = 0; i < IS31FL3733_PWM_REGISTER_COUNT; i++) {
  143. is31fl3733_write_register(addr, i, 0x00);
  144. }
  145. is31fl3733_select_page(addr, IS31FL3733_COMMAND_FUNCTION);
  146. // Set de-ghost pull-up resistors (SWx)
  147. is31fl3733_write_register(addr, IS31FL3733_FUNCTION_REG_SW_PULLUP, IS31FL3733_SW_PULLUP);
  148. // Set de-ghost pull-down resistors (CSx)
  149. is31fl3733_write_register(addr, IS31FL3733_FUNCTION_REG_CS_PULLDOWN, IS31FL3733_CS_PULLDOWN);
  150. // Set global current to maximum.
  151. is31fl3733_write_register(addr, IS31FL3733_FUNCTION_REG_GLOBAL_CURRENT, IS31FL3733_GLOBAL_CURRENT);
  152. // Disable software shutdown.
  153. is31fl3733_write_register(addr, IS31FL3733_FUNCTION_REG_CONFIGURATION, ((sync & 0b11) << 6) | ((IS31FL3733_PWM_FREQUENCY & 0b111) << 3) | 0x01);
  154. // Wait 10ms to ensure the device has woken up.
  155. wait_ms(10);
  156. }
  157. void is31fl3733_set_value(int index, uint8_t value) {
  158. is31fl3733_led_t led;
  159. if (index >= 0 && index < IS31FL3733_LED_COUNT) {
  160. memcpy_P(&led, (&g_is31fl3733_leds[index]), sizeof(led));
  161. if (g_pwm_buffer[led.driver][led.v] == value) {
  162. return;
  163. }
  164. g_pwm_buffer[led.driver][led.v] = value;
  165. g_pwm_buffer_update_required[led.driver] = true;
  166. }
  167. }
  168. void is31fl3733_set_value_all(uint8_t value) {
  169. for (int i = 0; i < IS31FL3733_LED_COUNT; i++) {
  170. is31fl3733_set_value(i, value);
  171. }
  172. }
  173. void is31fl3733_set_led_control_register(uint8_t index, bool value) {
  174. is31fl3733_led_t led;
  175. memcpy_P(&led, (&g_is31fl3733_leds[index]), sizeof(led));
  176. uint8_t control_register = led.v / 8;
  177. uint8_t bit_value = led.v % 8;
  178. if (value) {
  179. g_led_control_registers[led.driver][control_register] |= (1 << bit_value);
  180. } else {
  181. g_led_control_registers[led.driver][control_register] &= ~(1 << bit_value);
  182. }
  183. g_led_control_registers_update_required[led.driver] = true;
  184. }
  185. void is31fl3733_update_pwm_buffers(uint8_t addr, uint8_t index) {
  186. if (g_pwm_buffer_update_required[index]) {
  187. is31fl3733_select_page(addr, IS31FL3733_COMMAND_PWM);
  188. is31fl3733_write_pwm_buffer(addr, g_pwm_buffer[index]);
  189. g_pwm_buffer_update_required[index] = false;
  190. }
  191. }
  192. void is31fl3733_update_led_control_registers(uint8_t addr, uint8_t index) {
  193. if (g_led_control_registers_update_required[index]) {
  194. is31fl3733_select_page(addr, IS31FL3733_COMMAND_LED_CONTROL);
  195. for (int i = 0; i < IS31FL3733_LED_CONTROL_REGISTER_COUNT; i++) {
  196. is31fl3733_write_register(addr, i, g_led_control_registers[index][i]);
  197. }
  198. g_led_control_registers_update_required[index] = false;
  199. }
  200. }
  201. void is31fl3733_flush(void) {
  202. is31fl3733_update_pwm_buffers(IS31FL3733_I2C_ADDRESS_1, 0);
  203. #if defined(IS31FL3733_I2C_ADDRESS_2)
  204. is31fl3733_update_pwm_buffers(IS31FL3733_I2C_ADDRESS_2, 1);
  205. # if defined(IS31FL3733_I2C_ADDRESS_3)
  206. is31fl3733_update_pwm_buffers(IS31FL3733_I2C_ADDRESS_3, 2);
  207. # if defined(IS31FL3733_I2C_ADDRESS_4)
  208. is31fl3733_update_pwm_buffers(IS31FL3733_I2C_ADDRESS_4, 3);
  209. # endif
  210. # endif
  211. #endif
  212. }