logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

is31fl3733-mono.c (8799B)


  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 "i2c_master.h"
  22. #include "gpio.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. const uint8_t i2c_addresses[IS31FL3733_DRIVER_COUNT] = {
  57. IS31FL3733_I2C_ADDRESS_1,
  58. #ifdef IS31FL3733_I2C_ADDRESS_2
  59. IS31FL3733_I2C_ADDRESS_2,
  60. # ifdef IS31FL3733_I2C_ADDRESS_3
  61. IS31FL3733_I2C_ADDRESS_3,
  62. # ifdef IS31FL3733_I2C_ADDRESS_4
  63. IS31FL3733_I2C_ADDRESS_4,
  64. # endif
  65. # endif
  66. #endif
  67. };
  68. const uint8_t driver_sync[IS31FL3733_DRIVER_COUNT] = {
  69. IS31FL3733_SYNC_1,
  70. #ifdef IS31FL3733_I2C_ADDRESS_2
  71. IS31FL3733_SYNC_2,
  72. # ifdef IS31FL3733_I2C_ADDRESS_3
  73. IS31FL3733_SYNC_3,
  74. # ifdef IS31FL3733_I2C_ADDRESS_4
  75. IS31FL3733_SYNC_4,
  76. # endif
  77. # endif
  78. #endif
  79. };
  80. // These buffers match the IS31FL3733 PWM registers.
  81. // The control buffers match the page 0 LED On/Off registers.
  82. // Storing them like this is optimal for I2C transfers to the registers.
  83. // We could optimize this and take out the unused registers from these
  84. // buffers and the transfers in is31fl3733_write_pwm_buffer() but it's
  85. // probably not worth the extra complexity.
  86. typedef struct is31fl3733_driver_t {
  87. uint8_t pwm_buffer[IS31FL3733_PWM_REGISTER_COUNT];
  88. bool pwm_buffer_dirty;
  89. uint8_t led_control_buffer[IS31FL3733_LED_CONTROL_REGISTER_COUNT];
  90. bool led_control_buffer_dirty;
  91. } PACKED is31fl3733_driver_t;
  92. is31fl3733_driver_t driver_buffers[IS31FL3733_DRIVER_COUNT] = {{
  93. .pwm_buffer = {0},
  94. .pwm_buffer_dirty = false,
  95. .led_control_buffer = {0},
  96. .led_control_buffer_dirty = false,
  97. }};
  98. void is31fl3733_write_register(uint8_t index, uint8_t reg, uint8_t data) {
  99. #if IS31FL3733_I2C_PERSISTENCE > 0
  100. for (uint8_t i = 0; i < IS31FL3733_I2C_PERSISTENCE; i++) {
  101. if (i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3733_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
  102. }
  103. #else
  104. i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3733_I2C_TIMEOUT);
  105. #endif
  106. }
  107. void is31fl3733_select_page(uint8_t index, uint8_t page) {
  108. is31fl3733_write_register(index, IS31FL3733_REG_COMMAND_WRITE_LOCK, IS31FL3733_COMMAND_WRITE_LOCK_MAGIC);
  109. is31fl3733_write_register(index, IS31FL3733_REG_COMMAND, page);
  110. }
  111. void is31fl3733_write_pwm_buffer(uint8_t index) {
  112. // Assumes page 1 is already selected.
  113. // Transmit PWM registers in 12 transfers of 16 bytes.
  114. // Iterate over the pwm_buffer contents at 16 byte intervals.
  115. for (uint8_t i = 0; i < IS31FL3733_PWM_REGISTER_COUNT; i += 16) {
  116. #if IS31FL3733_I2C_PERSISTENCE > 0
  117. for (uint8_t j = 0; j < IS31FL3733_I2C_PERSISTENCE; j++) {
  118. if (i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer + i, 16, IS31FL3733_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
  119. }
  120. #else
  121. i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer + i, 16, IS31FL3733_I2C_TIMEOUT);
  122. #endif
  123. }
  124. }
  125. void is31fl3733_init_drivers(void) {
  126. i2c_init();
  127. #if defined(IS31FL3733_SDB_PIN)
  128. gpio_set_pin_output(IS31FL3733_SDB_PIN);
  129. gpio_write_pin_high(IS31FL3733_SDB_PIN);
  130. #endif
  131. for (uint8_t i = 0; i < IS31FL3733_DRIVER_COUNT; i++) {
  132. is31fl3733_init(i);
  133. }
  134. for (int i = 0; i < IS31FL3733_LED_COUNT; i++) {
  135. is31fl3733_set_led_control_register(i, true);
  136. }
  137. for (uint8_t i = 0; i < IS31FL3733_DRIVER_COUNT; i++) {
  138. is31fl3733_update_led_control_registers(i);
  139. }
  140. }
  141. void is31fl3733_init(uint8_t index) {
  142. // In order to avoid the LEDs being driven with garbage data
  143. // in the LED driver's PWM registers, shutdown is enabled last.
  144. // Set up the mode and other settings, clear the PWM registers,
  145. // then disable software shutdown.
  146. is31fl3733_select_page(index, IS31FL3733_COMMAND_LED_CONTROL);
  147. // Turn off all LEDs.
  148. for (uint8_t i = 0; i < IS31FL3733_LED_CONTROL_REGISTER_COUNT; i++) {
  149. is31fl3733_write_register(index, i, 0x00);
  150. }
  151. is31fl3733_select_page(index, IS31FL3733_COMMAND_PWM);
  152. // Set PWM on all LEDs to 0
  153. // No need to setup Breath registers to PWM as that is the default.
  154. for (uint8_t i = 0; i < IS31FL3733_PWM_REGISTER_COUNT; i++) {
  155. is31fl3733_write_register(index, i, 0x00);
  156. }
  157. is31fl3733_select_page(index, IS31FL3733_COMMAND_FUNCTION);
  158. uint8_t sync = driver_sync[index];
  159. // Set de-ghost pull-up resistors (SWx)
  160. is31fl3733_write_register(index, IS31FL3733_FUNCTION_REG_SW_PULLUP, IS31FL3733_SW_PULLUP);
  161. // Set de-ghost pull-down resistors (CSx)
  162. is31fl3733_write_register(index, IS31FL3733_FUNCTION_REG_CS_PULLDOWN, IS31FL3733_CS_PULLDOWN);
  163. // Set global current to maximum.
  164. is31fl3733_write_register(index, IS31FL3733_FUNCTION_REG_GLOBAL_CURRENT, IS31FL3733_GLOBAL_CURRENT);
  165. // Disable software shutdown.
  166. is31fl3733_write_register(index, IS31FL3733_FUNCTION_REG_CONFIGURATION, ((sync & 0b11) << 6) | ((IS31FL3733_PWM_FREQUENCY & 0b111) << 3) | 0x01);
  167. // Wait 10ms to ensure the device has woken up.
  168. wait_ms(10);
  169. }
  170. void is31fl3733_set_value(int index, uint8_t value) {
  171. is31fl3733_led_t led;
  172. if (index >= 0 && index < IS31FL3733_LED_COUNT) {
  173. memcpy_P(&led, (&g_is31fl3733_leds[index]), sizeof(led));
  174. if (driver_buffers[led.driver].pwm_buffer[led.v] == value) {
  175. return;
  176. }
  177. driver_buffers[led.driver].pwm_buffer[led.v] = value;
  178. driver_buffers[led.driver].pwm_buffer_dirty = true;
  179. }
  180. }
  181. void is31fl3733_set_value_all(uint8_t value) {
  182. for (int i = 0; i < IS31FL3733_LED_COUNT; i++) {
  183. is31fl3733_set_value(i, value);
  184. }
  185. }
  186. void is31fl3733_set_led_control_register(uint8_t index, bool value) {
  187. is31fl3733_led_t led;
  188. memcpy_P(&led, (&g_is31fl3733_leds[index]), sizeof(led));
  189. uint8_t control_register = led.v / 8;
  190. uint8_t bit_value = led.v % 8;
  191. if (value) {
  192. driver_buffers[led.driver].led_control_buffer[control_register] |= (1 << bit_value);
  193. } else {
  194. driver_buffers[led.driver].led_control_buffer[control_register] &= ~(1 << bit_value);
  195. }
  196. driver_buffers[led.driver].led_control_buffer_dirty = true;
  197. }
  198. void is31fl3733_update_pwm_buffers(uint8_t index) {
  199. if (driver_buffers[index].pwm_buffer_dirty) {
  200. is31fl3733_select_page(index, IS31FL3733_COMMAND_PWM);
  201. is31fl3733_write_pwm_buffer(index);
  202. driver_buffers[index].pwm_buffer_dirty = false;
  203. }
  204. }
  205. void is31fl3733_update_led_control_registers(uint8_t index) {
  206. if (driver_buffers[index].led_control_buffer_dirty) {
  207. is31fl3733_select_page(index, IS31FL3733_COMMAND_LED_CONTROL);
  208. for (uint8_t i = 0; i < IS31FL3733_LED_CONTROL_REGISTER_COUNT; i++) {
  209. is31fl3733_write_register(index, i, driver_buffers[index].led_control_buffer[i]);
  210. }
  211. driver_buffers[index].led_control_buffer_dirty = false;
  212. }
  213. }
  214. void is31fl3733_flush(void) {
  215. for (uint8_t i = 0; i < IS31FL3733_DRIVER_COUNT; i++) {
  216. is31fl3733_update_pwm_buffers(i);
  217. }
  218. }